/src/FreeImage/Source/OpenEXR/Half/half.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 766 lines · 284 code · 147 blank · 335 comment · 19 complexity · 1c4e412a86f2556249d025b526c8ecf0 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. // Primary authors:
  35. // Florian Kainz <kainz@ilm.com>
  36. // Rod Bogart <rgb@ilm.com>
  37. //---------------------------------------------------------------------------
  38. //
  39. // half -- a 16-bit floating point number class:
  40. //
  41. // Type half can represent positive and negative numbers whose
  42. // magnitude is between roughly 6.1e-5 and 6.5e+4 with a relative
  43. // error of 9.8e-4; numbers smaller than 6.1e-5 can be represented
  44. // with an absolute error of 6.0e-8. All integers from -2048 to
  45. // +2048 can be represented exactly.
  46. //
  47. // Type half behaves (almost) like the built-in C++ floating point
  48. // types. In arithmetic expressions, half, float and double can be
  49. // mixed freely. Here are a few examples:
  50. //
  51. // half a (3.5);
  52. // float b (a + sqrt (a));
  53. // a += b;
  54. // b += a;
  55. // b = a + 7;
  56. //
  57. // Conversions from half to float are lossless; all half numbers
  58. // are exactly representable as floats.
  59. //
  60. // Conversions from float to half may not preserve a float's value
  61. // exactly. If a float is not representable as a half, then the
  62. // float value is rounded to the nearest representable half. If a
  63. // float value is exactly in the middle between the two closest
  64. // representable half values, then the float value is rounded to
  65. // the closest half whose least significant bit is zero.
  66. //
  67. // Overflows during float-to-half conversions cause arithmetic
  68. // exceptions. An overflow occurs when the float value to be
  69. // converted is too large to be represented as a half, or if the
  70. // float value is an infinity or a NAN.
  71. //
  72. // The implementation of type half makes the following assumptions
  73. // about the implementation of the built-in C++ types:
  74. //
  75. // float is an IEEE 754 single-precision number
  76. // sizeof (float) == 4
  77. // sizeof (unsigned int) == sizeof (float)
  78. // alignof (unsigned int) == alignof (float)
  79. // sizeof (unsigned short) == 2
  80. //
  81. //---------------------------------------------------------------------------
  82. #ifndef _HALF_H_
  83. #define _HALF_H_
  84. #include <iostream>
  85. #if defined(OPENEXR_DLL)
  86. #if defined(HALF_EXPORTS)
  87. #define HALF_EXPORT __declspec(dllexport)
  88. #else
  89. #define HALF_EXPORT __declspec(dllimport)
  90. #endif
  91. #define HALF_EXPORT_CONST
  92. #else
  93. #define HALF_EXPORT
  94. #define HALF_EXPORT_CONST const
  95. #endif
  96. class HALF_EXPORT half
  97. {
  98. public:
  99. //-------------
  100. // Constructors
  101. //-------------
  102. half (); // no initialization
  103. half (float f);
  104. //--------------------
  105. // Conversion to float
  106. //--------------------
  107. operator float () const;
  108. //------------
  109. // Unary minus
  110. //------------
  111. half operator - () const;
  112. //-----------
  113. // Assignment
  114. //-----------
  115. half & operator = (half h);
  116. half & operator = (float f);
  117. half & operator += (half h);
  118. half & operator += (float f);
  119. half & operator -= (half h);
  120. half & operator -= (float f);
  121. half & operator *= (half h);
  122. half & operator *= (float f);
  123. half & operator /= (half h);
  124. half & operator /= (float f);
  125. //---------------------------------------------------------
  126. // Round to n-bit precision (n should be between 0 and 10).
  127. // After rounding, the significand's 10-n least significant
  128. // bits will be zero.
  129. //---------------------------------------------------------
  130. half round (unsigned int n) const;
  131. //--------------------------------------------------------------------
  132. // Classification:
  133. //
  134. // h.isFinite() returns true if h is a normalized number,
  135. // a denormalized number or zero
  136. //
  137. // h.isNormalized() returns true if h is a normalized number
  138. //
  139. // h.isDenormalized() returns true if h is a denormalized number
  140. //
  141. // h.isZero() returns true if h is zero
  142. //
  143. // h.isNan() returns true if h is a NAN
  144. //
  145. // h.isInfinity() returns true if h is a positive
  146. // or a negative infinity
  147. //
  148. // h.isNegative() returns true if the sign bit of h
  149. // is set (negative)
  150. //--------------------------------------------------------------------
  151. bool isFinite () const;
  152. bool isNormalized () const;
  153. bool isDenormalized () const;
  154. bool isZero () const;
  155. bool isNan () const;
  156. bool isInfinity () const;
  157. bool isNegative () const;
  158. //--------------------------------------------
  159. // Special values
  160. //
  161. // posInf() returns +infinity
  162. //
  163. // negInf() returns -infinity
  164. //
  165. // qNan() returns a NAN with the bit
  166. // pattern 0111111111111111
  167. //
  168. // sNan() returns a NAN with the bit
  169. // pattern 0111110111111111
  170. //--------------------------------------------
  171. static half posInf ();
  172. static half negInf ();
  173. static half qNan ();
  174. static half sNan ();
  175. //--------------------------------------
  176. // Access to the internal representation
  177. //--------------------------------------
  178. unsigned short bits () const;
  179. void setBits (unsigned short bits);
  180. public:
  181. union uif
  182. {
  183. unsigned int i;
  184. float f;
  185. };
  186. private:
  187. static short convert (int i);
  188. static float overflow ();
  189. unsigned short _h;
  190. static HALF_EXPORT_CONST uif _toFloat[1 << 16];
  191. static HALF_EXPORT_CONST unsigned short _eLut[1 << 9];
  192. };
  193. //-----------
  194. // Stream I/O
  195. //-----------
  196. HALF_EXPORT std::ostream & operator << (std::ostream &os, half h);
  197. HALF_EXPORT std::istream & operator >> (std::istream &is, half &h);
  198. //----------
  199. // Debugging
  200. //----------
  201. HALF_EXPORT void printBits (std::ostream &os, half h);
  202. HALF_EXPORT void printBits (std::ostream &os, float f);
  203. HALF_EXPORT void printBits (char c[19], half h);
  204. HALF_EXPORT void printBits (char c[35], float f);
  205. //-------------------------------------------------------------------------
  206. // Limits
  207. //
  208. // Visual C++ will complain if HALF_MIN, HALF_NRM_MIN etc. are not float
  209. // constants, but at least one other compiler (gcc 2.96) produces incorrect
  210. // results if they are.
  211. //-------------------------------------------------------------------------
  212. #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
  213. #define HALF_MIN 5.96046448e-08f // Smallest positive half
  214. #define HALF_NRM_MIN 6.10351562e-05f // Smallest positive normalized half
  215. #define HALF_MAX 65504.0f // Largest positive half
  216. #define HALF_EPSILON 0.00097656f // Smallest positive e for which
  217. // half (1.0 + e) != half (1.0)
  218. #else
  219. #define HALF_MIN 5.96046448e-08 // Smallest positive half
  220. #define HALF_NRM_MIN 6.10351562e-05 // Smallest positive normalized half
  221. #define HALF_MAX 65504.0 // Largest positive half
  222. #define HALF_EPSILON 0.00097656 // Smallest positive e for which
  223. // half (1.0 + e) != half (1.0)
  224. #endif
  225. #define HALF_MANT_DIG 11 // Number of digits in mantissa
  226. // (significand + hidden leading 1)
  227. #define HALF_DIG 2 // Number of base 10 digits that
  228. // can be represented without change
  229. #define HALF_RADIX 2 // Base of the exponent
  230. #define HALF_MIN_EXP -13 // Minimum negative integer such that
  231. // HALF_RADIX raised to the power of
  232. // one less than that integer is a
  233. // normalized half
  234. #define HALF_MAX_EXP 16 // Maximum positive integer such that
  235. // HALF_RADIX raised to the power of
  236. // one less than that integer is a
  237. // normalized half
  238. #define HALF_MIN_10_EXP -4 // Minimum positive integer such
  239. // that 10 raised to that power is
  240. // a normalized half
  241. #define HALF_MAX_10_EXP 4 // Maximum positive integer such
  242. // that 10 raised to that power is
  243. // a normalized half
  244. //---------------------------------------------------------------------------
  245. //
  246. // Implementation --
  247. //
  248. // Representation of a float:
  249. //
  250. // We assume that a float, f, is an IEEE 754 single-precision
  251. // floating point number, whose bits are arranged as follows:
  252. //
  253. // 31 (msb)
  254. // |
  255. // | 30 23
  256. // | | |
  257. // | | | 22 0 (lsb)
  258. // | | | | |
  259. // X XXXXXXXX XXXXXXXXXXXXXXXXXXXXXXX
  260. //
  261. // s e m
  262. //
  263. // S is the sign-bit, e is the exponent and m is the significand.
  264. //
  265. // If e is between 1 and 254, f is a normalized number:
  266. //
  267. // s e-127
  268. // f = (-1) * 2 * 1.m
  269. //
  270. // If e is 0, and m is not zero, f is a denormalized number:
  271. //
  272. // s -126
  273. // f = (-1) * 2 * 0.m
  274. //
  275. // If e and m are both zero, f is zero:
  276. //
  277. // f = 0.0
  278. //
  279. // If e is 255, f is an "infinity" or "not a number" (NAN),
  280. // depending on whether m is zero or not.
  281. //
  282. // Examples:
  283. //
  284. // 0 00000000 00000000000000000000000 = 0.0
  285. // 0 01111110 00000000000000000000000 = 0.5
  286. // 0 01111111 00000000000000000000000 = 1.0
  287. // 0 10000000 00000000000000000000000 = 2.0
  288. // 0 10000000 10000000000000000000000 = 3.0
  289. // 1 10000101 11110000010000000000000 = -124.0625
  290. // 0 11111111 00000000000000000000000 = +infinity
  291. // 1 11111111 00000000000000000000000 = -infinity
  292. // 0 11111111 10000000000000000000000 = NAN
  293. // 1 11111111 11111111111111111111111 = NAN
  294. //
  295. // Representation of a half:
  296. //
  297. // Here is the bit-layout for a half number, h:
  298. //
  299. // 15 (msb)
  300. // |
  301. // | 14 10
  302. // | | |
  303. // | | | 9 0 (lsb)
  304. // | | | | |
  305. // X XXXXX XXXXXXXXXX
  306. //
  307. // s e m
  308. //
  309. // S is the sign-bit, e is the exponent and m is the significand.
  310. //
  311. // If e is between 1 and 30, h is a normalized number:
  312. //
  313. // s e-15
  314. // h = (-1) * 2 * 1.m
  315. //
  316. // If e is 0, and m is not zero, h is a denormalized number:
  317. //
  318. // S -14
  319. // h = (-1) * 2 * 0.m
  320. //
  321. // If e and m are both zero, h is zero:
  322. //
  323. // h = 0.0
  324. //
  325. // If e is 31, h is an "infinity" or "not a number" (NAN),
  326. // depending on whether m is zero or not.
  327. //
  328. // Examples:
  329. //
  330. // 0 00000 0000000000 = 0.0
  331. // 0 01110 0000000000 = 0.5
  332. // 0 01111 0000000000 = 1.0
  333. // 0 10000 0000000000 = 2.0
  334. // 0 10000 1000000000 = 3.0
  335. // 1 10101 1111000001 = -124.0625
  336. // 0 11111 0000000000 = +infinity
  337. // 1 11111 0000000000 = -infinity
  338. // 0 11111 1000000000 = NAN
  339. // 1 11111 1111111111 = NAN
  340. //
  341. // Conversion:
  342. //
  343. // Converting from a float to a half requires some non-trivial bit
  344. // manipulations. In some cases, this makes conversion relatively
  345. // slow, but the most common case is accelerated via table lookups.
  346. //
  347. // Converting back from a half to a float is easier because we don't
  348. // have to do any rounding. In addition, there are only 65536
  349. // different half numbers; we can convert each of those numbers once
  350. // and store the results in a table. Later, all conversions can be
  351. // done using only simple table lookups.
  352. //
  353. //---------------------------------------------------------------------------
  354. //--------------------
  355. // Simple constructors
  356. //--------------------
  357. inline
  358. half::half ()
  359. {
  360. // no initialization
  361. }
  362. //----------------------------
  363. // Half-from-float constructor
  364. //----------------------------
  365. inline
  366. half::half (float f)
  367. {
  368. uif x;
  369. x.f = f;
  370. if (f == 0)
  371. {
  372. //
  373. // Common special case - zero.
  374. // Preserve the zero's sign bit.
  375. //
  376. _h = (x.i >> 16);
  377. }
  378. else
  379. {
  380. //
  381. // We extract the combined sign and exponent, e, from our
  382. // floating-point number, f. Then we convert e to the sign
  383. // and exponent of the half number via a table lookup.
  384. //
  385. // For the most common case, where a normalized half is produced,
  386. // the table lookup returns a non-zero value; in this case, all
  387. // we have to do is round f's significand to 10 bits and combine
  388. // the result with e.
  389. //
  390. // For all other cases (overflow, zeroes, denormalized numbers
  391. // resulting from underflow, infinities and NANs), the table
  392. // lookup returns zero, and we call a longer, non-inline function
  393. // to do the float-to-half conversion.
  394. //
  395. register int e = (x.i >> 23) & 0x000001ff;
  396. e = _eLut[e];
  397. if (e)
  398. {
  399. //
  400. // Simple case - round the significand, m, to 10
  401. // bits and combine it with the sign and exponent.
  402. //
  403. register int m = x.i & 0x007fffff;
  404. _h = e + ((m + 0x00000fff + ((m >> 13) & 1)) >> 13);
  405. }
  406. else
  407. {
  408. //
  409. // Difficult case - call a function.
  410. //
  411. _h = convert (x.i);
  412. }
  413. }
  414. }
  415. //------------------------------------------
  416. // Half-to-float conversion via table lookup
  417. //------------------------------------------
  418. inline
  419. half::operator float () const
  420. {
  421. return _toFloat[_h].f;
  422. }
  423. //-------------------------
  424. // Round to n-bit precision
  425. //-------------------------
  426. inline half
  427. half::round (unsigned int n) const
  428. {
  429. //
  430. // Parameter check.
  431. //
  432. if (n >= 10)
  433. return *this;
  434. //
  435. // Disassemble h into the sign, s,
  436. // and the combined exponent and significand, e.
  437. //
  438. unsigned short s = _h & 0x8000;
  439. unsigned short e = _h & 0x7fff;
  440. //
  441. // Round the exponent and significand to the nearest value
  442. // where ones occur only in the (10-n) most significant bits.
  443. // Note that the exponent adjusts automatically if rounding
  444. // up causes the significand to overflow.
  445. //
  446. e >>= 9 - n;
  447. e += e & 1;
  448. e <<= 9 - n;
  449. //
  450. // Check for exponent overflow.
  451. //
  452. if (e >= 0x7c00)
  453. {
  454. //
  455. // Overflow occurred -- truncate instead of rounding.
  456. //
  457. e = _h;
  458. e >>= 10 - n;
  459. e <<= 10 - n;
  460. }
  461. //
  462. // Put the original sign bit back.
  463. //
  464. half h;
  465. h._h = s | e;
  466. return h;
  467. }
  468. //-----------------------
  469. // Other inline functions
  470. //-----------------------
  471. inline half
  472. half::operator - () const
  473. {
  474. half h;
  475. h._h = _h ^ 0x8000;
  476. return h;
  477. }
  478. inline half &
  479. half::operator = (half h)
  480. {
  481. _h = h._h;
  482. return *this;
  483. }
  484. inline half &
  485. half::operator = (float f)
  486. {
  487. *this = half (f);
  488. return *this;
  489. }
  490. inline half &
  491. half::operator += (half h)
  492. {
  493. *this = half (float (*this) + float (h));
  494. return *this;
  495. }
  496. inline half &
  497. half::operator += (float f)
  498. {
  499. *this = half (float (*this) + f);
  500. return *this;
  501. }
  502. inline half &
  503. half::operator -= (half h)
  504. {
  505. *this = half (float (*this) - float (h));
  506. return *this;
  507. }
  508. inline half &
  509. half::operator -= (float f)
  510. {
  511. *this = half (float (*this) - f);
  512. return *this;
  513. }
  514. inline half &
  515. half::operator *= (half h)
  516. {
  517. *this = half (float (*this) * float (h));
  518. return *this;
  519. }
  520. inline half &
  521. half::operator *= (float f)
  522. {
  523. *this = half (float (*this) * f);
  524. return *this;
  525. }
  526. inline half &
  527. half::operator /= (half h)
  528. {
  529. *this = half (float (*this) / float (h));
  530. return *this;
  531. }
  532. inline half &
  533. half::operator /= (float f)
  534. {
  535. *this = half (float (*this) / f);
  536. return *this;
  537. }
  538. inline bool
  539. half::isFinite () const
  540. {
  541. unsigned short e = (_h >> 10) & 0x001f;
  542. return e < 31;
  543. }
  544. inline bool
  545. half::isNormalized () const
  546. {
  547. unsigned short e = (_h >> 10) & 0x001f;
  548. return e > 0 && e < 31;
  549. }
  550. inline bool
  551. half::isDenormalized () const
  552. {
  553. unsigned short e = (_h >> 10) & 0x001f;
  554. unsigned short m = _h & 0x3ff;
  555. return e == 0 && m != 0;
  556. }
  557. inline bool
  558. half::isZero () const
  559. {
  560. return (_h & 0x7fff) == 0;
  561. }
  562. inline bool
  563. half::isNan () const
  564. {
  565. unsigned short e = (_h >> 10) & 0x001f;
  566. unsigned short m = _h & 0x3ff;
  567. return e == 31 && m != 0;
  568. }
  569. inline bool
  570. half::isInfinity () const
  571. {
  572. unsigned short e = (_h >> 10) & 0x001f;
  573. unsigned short m = _h & 0x3ff;
  574. return e == 31 && m == 0;
  575. }
  576. inline bool
  577. half::isNegative () const
  578. {
  579. return (_h & 0x8000) != 0;
  580. }
  581. inline half
  582. half::posInf ()
  583. {
  584. half h;
  585. h._h = 0x7c00;
  586. return h;
  587. }
  588. inline half
  589. half::negInf ()
  590. {
  591. half h;
  592. h._h = 0xfc00;
  593. return h;
  594. }
  595. inline half
  596. half::qNan ()
  597. {
  598. half h;
  599. h._h = 0x7fff;
  600. return h;
  601. }
  602. inline half
  603. half::sNan ()
  604. {
  605. half h;
  606. h._h = 0x7dff;
  607. return h;
  608. }
  609. inline unsigned short
  610. half::bits () const
  611. {
  612. return _h;
  613. }
  614. inline void
  615. half::setBits (unsigned short bits)
  616. {
  617. _h = bits;
  618. }
  619. #endif