/src/FreeImage/Source/OpenEXR/Half/toFloat.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 164 lines · 65 code · 30 blank · 69 comment · 14 complexity · 24afbf33ae9ba2c95b762924da158a41 MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. //---------------------------------------------------------------------------
  35. //
  36. // toFloat
  37. //
  38. // A program to generate the lookup table for half-to-float
  39. // conversion needed by class half.
  40. // The program loops over all 65536 possible half numbers,
  41. // converts each of them to a float, and prints the result.
  42. //
  43. //---------------------------------------------------------------------------
  44. #include <iostream>
  45. #include <iomanip>
  46. using namespace std;
  47. //---------------------------------------------------
  48. // Interpret an unsigned short bit pattern as a half,
  49. // and convert that half to the corresponding float's
  50. // bit pattern.
  51. //---------------------------------------------------
  52. unsigned int
  53. halfToFloat (unsigned short y)
  54. {
  55. int s = (y >> 15) & 0x00000001;
  56. int e = (y >> 10) & 0x0000001f;
  57. int m = y & 0x000003ff;
  58. if (e == 0)
  59. {
  60. if (m == 0)
  61. {
  62. //
  63. // Plus or minus zero
  64. //
  65. return s << 31;
  66. }
  67. else
  68. {
  69. //
  70. // Denormalized number -- renormalize it
  71. //
  72. while (!(m & 0x00000400))
  73. {
  74. m <<= 1;
  75. e -= 1;
  76. }
  77. e += 1;
  78. m &= ~0x00000400;
  79. }
  80. }
  81. else if (e == 31)
  82. {
  83. if (m == 0)
  84. {
  85. //
  86. // Positive or negative infinity
  87. //
  88. return (s << 31) | 0x7f800000;
  89. }
  90. else
  91. {
  92. //
  93. // Nan -- preserve sign and significand bits
  94. //
  95. return (s << 31) | 0x7f800000 | (m << 13);
  96. }
  97. }
  98. //
  99. // Normalized number
  100. //
  101. e = e + (127 - 15);
  102. m = m << 13;
  103. //
  104. // Assemble s, e and m.
  105. //
  106. return (s << 31) | (e << 23) | m;
  107. }
  108. //---------------------------------------------
  109. // Main - prints the half-to-float lookup table
  110. //---------------------------------------------
  111. int
  112. main ()
  113. {
  114. cout.precision (9);
  115. cout.setf (ios_base::hex, ios_base::basefield);
  116. cout << "//\n"
  117. "// This is an automatically generated file.\n"
  118. "// Do not edit.\n"
  119. "//\n\n";
  120. cout << "{\n ";
  121. const int iMax = (1 << 16);
  122. for (int i = 0; i < iMax; i++)
  123. {
  124. cout << "{0x" << setfill ('0') << setw (8) << halfToFloat (i) << "}, ";
  125. if (i % 4 == 3)
  126. {
  127. cout << "\n";
  128. if (i < iMax - 1)
  129. cout << " ";
  130. }
  131. }
  132. cout << "};\n";
  133. return 0;
  134. }