ImathFun.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002-2012, 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. #include "ImathFun.h"
  35. IMATH_INTERNAL_NAMESPACE_SOURCE_ENTER
  36. float
  37. succf (float f)
  38. {
  39. union {float f; int i;} u;
  40. u.f = f;
  41. if ((u.i & 0x7f800000) == 0x7f800000)
  42. {
  43. // Nan or infinity; don't change value.
  44. }
  45. else if (u.i == 0x00000000 || u.i == 0x80000000)
  46. {
  47. // Plus or minus zero.
  48. u.i = 0x00000001;
  49. }
  50. else if (u.i > 0)
  51. {
  52. // Positive float, normalized or denormalized.
  53. // Incrementing the largest positive float
  54. // produces +infinity.
  55. ++u.i;
  56. }
  57. else
  58. {
  59. // Negative normalized or denormalized float.
  60. --u.i;
  61. }
  62. return u.f;
  63. }
  64. float
  65. predf (float f)
  66. {
  67. union {float f; int i;} u;
  68. u.f = f;
  69. if ((u.i & 0x7f800000) == 0x7f800000)
  70. {
  71. // Nan or infinity; don't change value.
  72. }
  73. else if (u.i == 0x00000000 || u.i == 0x80000000)
  74. {
  75. // Plus or minus zero.
  76. u.i = 0x80000001;
  77. }
  78. else if (u.i > 0)
  79. {
  80. // Positive float, normalized or denormalized.
  81. --u.i;
  82. }
  83. else
  84. {
  85. // Negative normalized or denormalized float.
  86. // Decrementing the largest negative float
  87. // produces -infinity.
  88. ++u.i;
  89. }
  90. return u.f;
  91. }
  92. double
  93. succd (double d)
  94. {
  95. union {double d; Int64 i;} u;
  96. u.d = d;
  97. if ((u.i & 0x7ff0000000000000LL) == 0x7ff0000000000000LL)
  98. {
  99. // Nan or infinity; don't change value.
  100. }
  101. else if (u.i == 0x0000000000000000LL || u.i == 0x8000000000000000LL)
  102. {
  103. // Plus or minus zero.
  104. u.i = 0x0000000000000001LL;
  105. }
  106. else if (u.d > 0)
  107. {
  108. // Positive double, normalized or denormalized.
  109. // Incrementing the largest positive double
  110. // produces +infinity.
  111. ++u.i;
  112. }
  113. else
  114. {
  115. // Negative normalized or denormalized double.
  116. --u.i;
  117. }
  118. return u.d;
  119. }
  120. double
  121. predd (double d)
  122. {
  123. union {double d; Int64 i;} u;
  124. u.d = d;
  125. if ((u.i & 0x7ff0000000000000LL) == 0x7ff0000000000000LL)
  126. {
  127. // Nan or infinity; don't change value.
  128. }
  129. else if (u.i == 0x0000000000000000LL || u.i == 0x8000000000000000LL)
  130. {
  131. // Plus or minus zero.
  132. u.i = 0x8000000000000001LL;
  133. }
  134. else if (u.d > 0)
  135. {
  136. // Positive double, normalized or denormalized.
  137. --u.i;
  138. }
  139. else
  140. {
  141. // Negative normalized or denormalized double.
  142. // Decrementing the largest negative double
  143. // produces -infinity.
  144. ++u.i;
  145. }
  146. return u.d;
  147. }
  148. IMATH_INTERNAL_NAMESPACE_SOURCE_EXIT