Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6634 in orxonox.OLD for trunk/src/lib/network/converter.cc


Ignore:
Timestamp:
Jan 21, 2006, 1:18:19 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the network-branche back to the trunk

merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r6500:HEAD
minor conflicts in texture and one Makefile resolved to the trunk

also made a small patch to texture, so it Modulates with GL_REPEAT

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/network/converter.cc

    r6341 r6634  
    2222/* include your own header */
    2323#include "converter.h"
     24#include "shell_command.h"
     25
     26#include <limits>
     27
     28SHELL_COMMAND_STATIC(debug, Converter, Converter::debug);
    2429
    2530
     
    182187const int expmult = 8388608; //2^23
    183188
     189float Converter::getDenormConst()
     190{
     191  const int exp = 126;
     192  float result = 1.0f;
     193  for (int i = 0; i < exp; i++)
     194    result /= 2.0f;
     195  return result;
     196}
     197
    184198/*!
    185199 * Converts a float value into a byte-array
     
    189203byte* Converter::floatToByteArray(float x)
    190204{
     205  byte* result = new byte[4];
     206  floatToByteArray(x, result, 4);
     207  return result;
     208  /*
    191209  int mantisse = 0;
    192210  int exponent = 128;
     
    201219    sgn = 1;
    202220
    203   float sub = 1;
    204   while (sub < x)
    205   {
    206     sub *= 2;
     221  if (x == 0)
     222  {
     223    exponent = 255;
     224    mantisse = 0;
     225  }
     226  else
     227  {
     228    //if (x < getDenormConst())
     229    //  printf("Denormalisiert!\n");
     230    //printf("DenormConst = %e", getDenormConst());
     231
     232    float sub = 1;
     233    while (sub < x)
     234    {
     235      sub *= 2;
     236      exponent++;
     237    }
     238
     239    while (x > 0)
     240    {
     241      if (x >= sub)
     242      {
     243        mantisse += 1;
     244        x -= sub;
     245      }
     246
     247      mantisse *= 2;
     248      exponent--;
     249      sub /= 2;
     250    }
    207251    exponent++;
    208   }
    209 
    210   while (x > 0)
    211   {
    212     if (x >= sub)
    213     {
    214       mantisse += 1;
    215       x -= sub;
    216     }
    217 
    218     mantisse *= 2;
    219     exponent--;
    220     sub /= 2;
    221   }
    222   exponent++;
    223   mantisse /= 2;
    224   while (mantisse < expmult)
    225   {
    226     mantisse *= 2;
    227     exponent--;
    228   }
    229 
    230   //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
    231 
    232   mantisse -= expmult;
     252    mantisse /= 2;
     253
     254    printf("Conv:        mantisse = %i exponent = %i \n", mantisse, exponent);
     255
     256
     257    if (mantisse != 0)
     258    {
     259      while (mantisse < expmult)
     260      {
     261        mantisse *= 2;
     262        exponent--;
     263      }
     264
     265      mantisse -= expmult;
     266    }
     267  }
     268
     269  printf("Conv: mantisse = %i exponent = %i \n", mantisse, exponent);
     270
    233271
    234272  int hx = mantisse + expmult * exponent;
     
    238276
    239277  return result;
     278  */
    240279}
    241280
     
    248287float Converter::byteArrayToFloat(byte* a)
    249288{
     289  byte* h = new byte[4];
     290  float result = 0.0f;
     291  byteArrayToFloat(a, &result);
     292  return result;
     293  /*
     294  int hexp = a[2] + a[3] * 256;
     295  int exponent = (hexp / 128) % 256;
     296
    250297  int hmant = a[0] + a[1] * 256 + a[2] * 65536;
    251298  int mantisse = hmant % expmult;
     299  if (mantisse == 0 && exponent == 255)
     300    return 0;
     301
    252302  mantisse += expmult;
    253 
    254   int hexp = a[2] + a[3] * 256;
    255   int exponent = (hexp / 128) % 256 - 128;
     303  exponent -= 128;
     304
    256305
    257306  int sgn;
     
    261310    sgn = 1;
    262311
    263   //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
     312  printf("ReConv: mantisse = %i exponent = %i \n", mantisse, exponent);
    264313
    265314  float emult = 1;
     
    276325
    277326  return result;
    278 }
    279 
    280 /*!
    281  * Converts a float value into a byte-array
    282  * @param x: The float which is to convert
    283  * @return: A byte-array which accords the given float
    284  */
    285 byte* Converter::_floatToByteArray(float x)
    286 {
    287   byte* p = (byte*)&x;
    288   byte* result = new byte[4];
    289   for (int i = 0; i < 4; i++)
    290     result[i] = p[i];
    291   return result;
    292 }
    293 
    294 
    295 /*!
    296  * Converts a byte-array into a float value
    297  * @param a: The byte-array which is to convert
    298  * @return: A float value which accords the given byte-array
    299  */
    300 float Converter::_byteArrayToFloat(byte* a)
    301 {
    302   float* p = (float*)a;
    303   float result = *p;
    304   return result;
     327  */
    305328}
    306329
     
    310333 * @return: A byte-array which accords the given float
    311334 */
    312 int Converter::floatToByteArray(float x, byte* a, int length)
     335int Converter::_floatToByteArray(float x, byte* a, int length)
    313336{
    314337  if (length < FLOATSIZE)
     
    319342
    320343  //handle 0 else function will loop for ever
    321   if ( x == 0 )
     344  /*if ( x == 0 )
    322345  {
    323346    for ( int i = 0; i<FLOATSIZE; i++)
    324347      a[i] = 0;
    325348    return FLOATSIZE;
    326   }
     349}*/
    327350
    328351  int mantisse = 0;
     
    338361    sgn = 1;
    339362
    340   float sub = 1;
    341   while (sub < x)
    342   {
    343     sub *= 2;
     363  if (x == 0)
     364  {
     365    exponent = 255;
     366    mantisse = 0;
     367  }
     368  else
     369  {
     370    //if (x < getDenormConst())
     371    //  printf("Denormalisiert!\n");
     372    //printf("DenormConst = %e", getDenormConst());
     373
     374    float sub = 1;
     375    while (sub < x)
     376    {
     377      sub *= 2;
     378      exponent++;
     379    }
     380
     381    while (x > 0)
     382    {
     383      if (x >= sub)
     384      {
     385        mantisse += 1;
     386        x -= sub;
     387      }
     388
     389      mantisse *= 2;
     390      exponent--;
     391      sub /= 2;
     392    }
    344393    exponent++;
    345   }
    346 
    347   while (x > 0)
    348   {
    349     if (x >= sub)
    350     {
    351       mantisse += 1;
    352       x -= sub;
    353     }
    354 
    355     mantisse *= 2;
    356     exponent--;
    357     sub /= 2;
    358   }
    359   exponent++;
    360   mantisse /= 2;
    361   while (mantisse < expmult)
    362   {
    363     mantisse *= 2;
    364     exponent--;
    365   }
    366 
    367   //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
    368 
    369   mantisse -= expmult;
     394    mantisse /= 2;
     395
     396
     397///    printf("Conv:        mantisse = %i exponent = %i \n", mantisse, exponent);
     398
     399
     400    if (mantisse != 0)
     401    {
     402      while (mantisse < expmult)
     403      {
     404        mantisse *= 2;
     405        exponent--;
     406      }
     407
     408      mantisse -= expmult;
     409    }
     410  }
     411
     412///  printf("Conv: mantisse = %i exponent = %i \n", mantisse, exponent);
     413
    370414
    371415  int hx = mantisse + expmult * exponent;
     
    374418    a[3] += sgnadd;
    375419
     420
     421//  int hx = mantisse + expmult * exponent;
     422//  byte* result = intToByteArray(hx);
     423//  if (sgn == -1)
     424//    result[3] += sgnadd;
     425
    376426  return result;
    377427}
     
    383433 * @return: A float value which accords the given byte-array
    384434 */
    385 int Converter::byteArrayToFloat(const byte* a, float* x)
    386 {
    387   //handle 0
    388   for (int i = 0; i<FLOATSIZE; i++)
     435int Converter::_byteArrayToFloat(const byte* a, float* x)
     436{
     437    //handle 0
     438  /*for (int i = 0; i<FLOATSIZE; i++)
    389439  {
    390440    if (a[i]!=0)
     
    395445      return FLOATSIZE;
    396446    }
    397   }
    398 
     447}*/
     448
     449  int hexp = a[2] + a[3] * 256;
     450  int exponent = (hexp / 128) % 256;
    399451
    400452  int hmant = a[0] + a[1] * 256 + a[2] * 65536;
    401453  int mantisse = hmant % expmult;
     454
     455  //handle 0
     456  if (mantisse == 0 && exponent == 255)
     457  {
     458    *x = 0;
     459    return FLOATSIZE;
     460  }
     461
    402462  mantisse += expmult;
    403 
    404   int hexp = a[2] + a[3] * 256;
    405   int exponent = (hexp / 128) % 256 - 128;
     463  exponent -= 128;
     464
    406465
    407466  int sgn;
     
    411470    sgn = 1;
    412471
    413   //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
     472///  printf("ReConv: mantisse = %i exponent = %i \n", mantisse, exponent);
    414473
    415474  float emult = 1;
     
    421480      emult /= 2;
    422481
     482  /*
     483  float result = mantisse * emult;
     484  if (sgn == -1)
     485    result = -result;
     486
     487  return result;
     488  */
     489
    423490  *x = mantisse * emult;
    424491  if (sgn == -1)
     
    426493
    427494  return FLOATSIZE;
     495}
     496
     497/*!
     498 * Converts a float value into a byte-array
     499 * @param x: The float which is to convert
     500 * @return: A byte-array which accords the given float
     501 */
     502int Converter::floatToByteArray(float x, byte* a, int length)
     503{
     504  if ( length<4 )
     505  {
     506    PRINTF(1)("Byte Array to small\n");
     507    return 0;
     508  }
     509  byte* p = (byte*)&x;
     510
     511  for (int i = 0; i < 4; i++)
     512    a[i] = p[i];
     513  return 4;
     514}
     515
     516
     517/*!
     518 * Converts a byte-array into a float value
     519 * @param a: The byte-array which is to convert
     520 * @return: A float value which accords the given byte-array
     521 */
     522int Converter::byteArrayToFloat(const byte* a, float* x)
     523{
     524  *x = *((float*)a);
     525
     526  return 4;
    428527}
    429528
     
    504603}
    505604
     605
     606
     607
     608void Converter::floatTest(float x)
     609{
     610  //float x = 8.0f;
     611  //float x = numeric_limits<float>::infinity();
     612
     613  printf("To Convert: %e\n", x);
     614
     615  byte* res = floatToByteArray(x);
     616  for (int i = 0; i < 4; i++)
     617    printf("%i ", res[i]);
     618  printf("\n");
     619
     620  float y = byteArrayToFloat(res);
     621  printf("ReConvert: %e\n", y);
     622
     623  if (x == y)
     624    printf("equal\n");
     625  else
     626    printf("different\n");
     627}
     628
     629void Converter::debug()
     630{
     631  printf("We rulez\n");
     632
     633  //Denormalized?
     634  //floatTest(-9.87624e-38f);
     635  //floatTest(-9.87624e-37f);
     636  //floatTest(-9.87624e-36f);
     637  //floatTest(-9.87624e-35f);
     638
     639  /*
     640  floatTest(14.7f);
     641  floatTest(12.07e15f);
     642  floatTest(0.0f);
     643  floatTest(5.67e-15f);
     644  */
     645
     646  //floatTest(-18.0098f);
     647  //floatTest(-24.07e23f);
     648  //floatTest(-0.0f);
     649  //floatTest(-5.67e-29f);
     650  floatTest(-45.7e-32f);
     651  floatTest(45.7e-33f);
     652  floatTest(-45.7e-34f);
     653  floatTest(45.7e-35f);
     654}
Note: See TracChangeset for help on using the changeset viewer.