Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:42

0001 #include "DataFormats/Math/interface/libminifloat.h"
0002 
0003 namespace {
0004   MiniFloatConverter dummy;  // so the constructor is called
0005 }
0006 uint32_t MiniFloatConverter::mantissatable[2048];
0007 uint32_t MiniFloatConverter::exponenttable[64];
0008 uint16_t MiniFloatConverter::offsettable[64];
0009 uint16_t MiniFloatConverter::basetable[512];
0010 uint8_t MiniFloatConverter::shifttable[512];
0011 
0012 MiniFloatConverter::MiniFloatConverter() {
0013   static bool once = false;
0014   if (!once) {
0015     filltables();
0016     once = true;
0017   }
0018 }
0019 
0020 void MiniFloatConverter::filltables() {
0021   // ==== mantissatable ===
0022   // -- zero --
0023   mantissatable[0] = 0;
0024   // -- denorm --
0025   for (unsigned int i = 1; i <= 1023; ++i) {
0026     unsigned int m = (i << 13), e = 0;
0027     while (!(m & 0x00800000)) {  // While not normalized
0028       e -= 0x00800000;           // Decrement exponent (1<<23)
0029       m <<= 1;                   // Shift mantissa
0030     }
0031     m &= ~0x00800000;  // Clear leading 1 bit
0032     e += 0x38800000;   // Adjust bias ((127-14)<<23)
0033     mantissatable[i] = m | e;
0034   }
0035   // -- norm --
0036   for (unsigned int i = 1024; i <= 2047; ++i) {
0037     mantissatable[i] = 0x38000000 + ((i - 1024) << 13);
0038   }
0039   // ==== exponenttable ===
0040   exponenttable[0] = 0;
0041   for (unsigned int i = 1; i <= 30; ++i)
0042     exponenttable[i] = i << 23;
0043   exponenttable[31] = 0x47800000;
0044   exponenttable[32] = 0x80000000u;
0045   for (unsigned int i = 33; i <= 62; ++i)
0046     exponenttable[i] = 0x80000000u | ((i - 32) << 23);
0047   exponenttable[63] = 0xC7800000;
0048 
0049   // ==== offsettable ====
0050   for (unsigned int i = 0; i <= 63; ++i)
0051     offsettable[i] = ((i == 0 || i == 32) ? 0 : 1024);
0052 
0053   // ==== basetable, shifttable ===
0054   for (unsigned i = 0; i < 256; ++i) {
0055     int e = int(i) - 127;
0056     if (e < -24) {  // Very small numbers map to zero
0057       basetable[i | 0x000] = 0x0000;
0058       basetable[i | 0x100] = 0x8000;
0059       shifttable[i | 0x000] = 24;
0060       shifttable[i | 0x100] = 24;
0061     } else if (e < -14) {  // Small numbers map to denorms
0062       basetable[i | 0x000] = (0x0400 >> (-e - 14));
0063       basetable[i | 0x100] = (0x0400 >> (-e - 14)) | 0x8000;
0064       shifttable[i | 0x000] = -e - 1;
0065       shifttable[i | 0x100] = -e - 1;
0066     } else if (e <= 15) {  // Normal numbers just lose precision
0067       basetable[i | 0x000] = ((e + 15) << 10);
0068       basetable[i | 0x100] = ((e + 15) << 10) | 0x8000;
0069       shifttable[i | 0x000] = 13;
0070       shifttable[i | 0x100] = 13;
0071     } else if (e < 128) {  // Large numbers map to Infinity
0072       basetable[i | 0x000] = 0x7C00;
0073       basetable[i | 0x100] = 0xFC00;
0074       shifttable[i | 0x000] = 24;
0075       shifttable[i | 0x100] = 24;
0076     } else {  // Infinity and NaN's stay Infinity and NaN's
0077       basetable[i | 0x000] = 0x7C00;
0078       basetable[i | 0x100] = 0xFC00;
0079       shifttable[i | 0x000] = 13;
0080       shifttable[i | 0x100] = 13;
0081     }
0082   }
0083 }