CMS 3D CMS Logo

libminifloat.cc
Go to the documentation of this file.
2 
3 namespace {
4  MiniFloatConverter dummy; // so the constructor is called
5 }
11 
12 
14  static bool once = false;
15  if (!once) { filltables(); once = true; }
16 }
17 
19  // ==== mantissatable ===
20  // -- zero --
21  mantissatable[0] = 0;
22  // -- denorm --
23  for (unsigned int i = 1; i <= 1023; ++i) {
24  unsigned int m =(i<<13), e=0;
25  while(!(m&0x00800000)){ // While not normalized
26  e-=0x00800000; // Decrement exponent (1<<23)
27  m<<=1; // Shift mantissa
28  }
29  m&=~0x00800000; // Clear leading 1 bit
30  e+= 0x38800000; // Adjust bias ((127-14)<<23)
31  mantissatable[i] = m | e;
32  }
33  // -- norm --
34  for (unsigned int i = 1024; i <= 2047; ++i) {
35  mantissatable[i] = 0x38000000 + ((i-1024)<<13);
36  }
37  // ==== exponenttable ===
38  exponenttable[0] = 0;
39  for (unsigned int i = 1; i <= 30; ++i) exponenttable[i] = i<<23;
40  exponenttable[31] = 0x47800000;
41  exponenttable[32] = 0x80000000u;
42  for (unsigned int i = 33; i <= 62; ++i) exponenttable[i] = 0x80000000u | ((i-32)<<23);
43  exponenttable[63] = 0xC7800000;
44 
45  // ==== offsettable ====
46  for (unsigned int i = 0; i <= 63; ++i) offsettable[i] = ((i == 0 || i == 32) ? 0 : 1024);
47 
48  // ==== basetable, shifttable ===
49  for (unsigned i=0; i<256; ++i){
50  int e = int(i)-127;
51  if(e<-24){ // Very small numbers map to zero
52  basetable[i|0x000]=0x0000;
53  basetable[i|0x100]=0x8000;
54  shifttable[i|0x000]=24;
55  shifttable[i|0x100]=24;
56  }
57  else if(e<-14){ // Small numbers map to denorms
58  basetable[i|0x000]=(0x0400>>(-e-14));
59  basetable[i|0x100]=(0x0400>>(-e-14)) | 0x8000;
60  shifttable[i|0x000]=-e-1;
61  shifttable[i|0x100]=-e-1;
62  }
63  else if(e<=15){ // Normal numbers just lose precision
64  basetable[i|0x000]=((e+15)<<10);
65  basetable[i|0x100]=((e+15)<<10) | 0x8000;
66  shifttable[i|0x000]=13;
67  shifttable[i|0x100]=13;
68  }
69  else if(e<128){ // Large numbers map to Infinity
70  basetable[i|0x000]=0x7C00;
71  basetable[i|0x100]=0xFC00;
72  shifttable[i|0x000]=24;
73  shifttable[i|0x100]=24;
74  }
75  else{ // Infinity and NaN's stay Infinity and NaN's
76  basetable[i|0x000]=0x7C00;
77  basetable[i|0x100]=0xFC00;
78  shifttable[i|0x000]=13;
79  shifttable[i|0x100]=13;
80  }
81  }
82 }
static uint16_t offsettable[64]
Definition: libminifloat.h:123
static uint16_t basetable[512]
Definition: libminifloat.h:124
static uint8_t shifttable[512]
Definition: libminifloat.h:125
static uint32_t mantissatable[2048]
Definition: libminifloat.h:121
static void filltables()
Definition: libminifloat.cc:18
static uint32_t exponenttable[64]
Definition: libminifloat.h:122