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