You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
9.6 KiB

5 years ago
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. /// THE SOFTWARE.
  22. ///
  23. /// @ref gtc_ulp
  24. /// @file glm/gtc/ulp.inl
  25. /// @date 2011-03-07 / 2012-04-07
  26. /// @author Christophe Riccio
  27. ///////////////////////////////////////////////////////////////////////////////////
  28. /// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  29. ///
  30. /// Developed at SunPro, a Sun Microsystems, Inc. business.
  31. /// Permission to use, copy, modify, and distribute this
  32. /// software is freely granted, provided that this notice
  33. /// is preserved.
  34. ///////////////////////////////////////////////////////////////////////////////////
  35. #include <cmath>
  36. #include <cfloat>
  37. #if(GLM_COMPILER & GLM_COMPILER_VC)
  38. # pragma warning(push)
  39. # pragma warning(disable : 4127)
  40. #endif
  41. typedef union
  42. {
  43. float value;
  44. /* FIXME: Assumes 32 bit int. */
  45. unsigned int word;
  46. } ieee_float_shape_type;
  47. typedef union
  48. {
  49. double value;
  50. struct
  51. {
  52. glm::detail::int32 lsw;
  53. glm::detail::int32 msw;
  54. } parts;
  55. } ieee_double_shape_type;
  56. #define GLM_EXTRACT_WORDS(ix0,ix1,d) \
  57. do { \
  58. ieee_double_shape_type ew_u; \
  59. ew_u.value = (d); \
  60. (ix0) = ew_u.parts.msw; \
  61. (ix1) = ew_u.parts.lsw; \
  62. } while (0)
  63. #define GLM_GET_FLOAT_WORD(i,d) \
  64. do { \
  65. ieee_float_shape_type gf_u; \
  66. gf_u.value = (d); \
  67. (i) = gf_u.word; \
  68. } while (0)
  69. #define GLM_SET_FLOAT_WORD(d,i) \
  70. do { \
  71. ieee_float_shape_type sf_u; \
  72. sf_u.word = (i); \
  73. (d) = sf_u.value; \
  74. } while (0)
  75. #define GLM_INSERT_WORDS(d,ix0,ix1) \
  76. do { \
  77. ieee_double_shape_type iw_u; \
  78. iw_u.parts.msw = (ix0); \
  79. iw_u.parts.lsw = (ix1); \
  80. (d) = iw_u.value; \
  81. } while (0)
  82. namespace glm{
  83. namespace detail
  84. {
  85. GLM_FUNC_QUALIFIER float nextafterf(float x, float y)
  86. {
  87. volatile float t;
  88. glm::detail::int32 hx, hy, ix, iy;
  89. GLM_GET_FLOAT_WORD(hx, x);
  90. GLM_GET_FLOAT_WORD(hy, y);
  91. ix = hx&0x7fffffff; // |x|
  92. iy = hy&0x7fffffff; // |y|
  93. if((ix>0x7f800000) || // x is nan
  94. (iy>0x7f800000)) // y is nan
  95. return x+y;
  96. if(x==y) return y; // x=y, return y
  97. if(ix==0) { // x == 0
  98. GLM_SET_FLOAT_WORD(x,(hy&0x80000000)|1);// return +-minsubnormal
  99. t = x*x;
  100. if(t==x) return t; else return x; // raise underflow flag
  101. }
  102. if(hx>=0) { // x > 0
  103. if(hx>hy) { // x > y, x -= ulp
  104. hx -= 1;
  105. } else { // x < y, x += ulp
  106. hx += 1;
  107. }
  108. } else { // x < 0
  109. if(hy>=0||hx>hy){ // x < y, x -= ulp
  110. hx -= 1;
  111. } else { // x > y, x += ulp
  112. hx += 1;
  113. }
  114. }
  115. hy = hx&0x7f800000;
  116. if(hy>=0x7f800000) return x+x; // overflow
  117. if(hy<0x00800000) { // underflow
  118. t = x*x;
  119. if(t!=x) { // raise underflow flag
  120. GLM_SET_FLOAT_WORD(y,hx);
  121. return y;
  122. }
  123. }
  124. GLM_SET_FLOAT_WORD(x,hx);
  125. return x;
  126. }
  127. GLM_FUNC_QUALIFIER double nextafter(double x, double y)
  128. {
  129. volatile double t;
  130. glm::detail::int32 hx, hy, ix, iy;
  131. glm::detail::uint32 lx, ly;
  132. GLM_EXTRACT_WORDS(hx, lx, x);
  133. GLM_EXTRACT_WORDS(hy, ly, y);
  134. ix = hx & 0x7fffffff; // |x|
  135. iy = hy & 0x7fffffff; // |y|
  136. if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || // x is nan
  137. ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) // y is nan
  138. return x+y;
  139. if(x==y) return y; // x=y, return y
  140. if((ix|lx)==0) { // x == 0
  141. GLM_INSERT_WORDS(x, hy & 0x80000000, 1); // return +-minsubnormal
  142. t = x*x;
  143. if(t==x) return t; else return x; // raise underflow flag
  144. }
  145. if(hx>=0) { // x > 0
  146. if(hx>hy||((hx==hy)&&(lx>ly))) { // x > y, x -= ulp
  147. if(lx==0) hx -= 1;
  148. lx -= 1;
  149. } else { // x < y, x += ulp
  150. lx += 1;
  151. if(lx==0) hx += 1;
  152. }
  153. } else { // x < 0
  154. if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){// x < y, x -= ulp
  155. if(lx==0) hx -= 1;
  156. lx -= 1;
  157. } else { // x > y, x += ulp
  158. lx += 1;
  159. if(lx==0) hx += 1;
  160. }
  161. }
  162. hy = hx&0x7ff00000;
  163. if(hy>=0x7ff00000) return x+x; // overflow
  164. if(hy<0x00100000) { // underflow
  165. t = x*x;
  166. if(t!=x) { // raise underflow flag
  167. GLM_INSERT_WORDS(y,hx,lx);
  168. return y;
  169. }
  170. }
  171. GLM_INSERT_WORDS(x,hx,lx);
  172. return x;
  173. }
  174. }//namespace detail
  175. }//namespace glm
  176. #if(GLM_COMPILER & GLM_COMPILER_VC)
  177. # pragma warning(pop)
  178. #endif
  179. #if((GLM_COMPILER & GLM_COMPILER_VC) || ((GLM_COMPILER & GLM_COMPILER_INTEL) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS)))
  180. # define GLM_NEXT_AFTER_FLT(x, toward) glm::detail::nextafterf((x), (toward))
  181. # define GLM_NEXT_AFTER_DBL(x, toward) _nextafter((x), (toward))
  182. #else
  183. # define GLM_NEXT_AFTER_FLT(x, toward) nextafterf((x), (toward))
  184. # define GLM_NEXT_AFTER_DBL(x, toward) nextafter((x), (toward))
  185. #endif
  186. namespace glm
  187. {
  188. GLM_FUNC_QUALIFIER float next_float(float const & x)
  189. {
  190. return GLM_NEXT_AFTER_FLT(x, std::numeric_limits<float>::max());
  191. }
  192. GLM_FUNC_QUALIFIER double next_float(double const & x)
  193. {
  194. return GLM_NEXT_AFTER_DBL(x, std::numeric_limits<double>::max());
  195. }
  196. template<typename T, template<typename> class vecType>
  197. GLM_FUNC_QUALIFIER vecType<T> next_float(vecType<T> const & x)
  198. {
  199. vecType<T> Result;
  200. for(std::size_t i = 0; i < Result.length(); ++i)
  201. Result[i] = next_float(x[i]);
  202. return Result;
  203. }
  204. GLM_FUNC_QUALIFIER float prev_float(float const & x)
  205. {
  206. return GLM_NEXT_AFTER_FLT(x, std::numeric_limits<float>::min());
  207. }
  208. GLM_FUNC_QUALIFIER double prev_float(double const & x)
  209. {
  210. return GLM_NEXT_AFTER_DBL(x, std::numeric_limits<double>::min());
  211. }
  212. template<typename T, template<typename> class vecType>
  213. GLM_FUNC_QUALIFIER vecType<T> prev_float(vecType<T> const & x)
  214. {
  215. vecType<T> Result;
  216. for(std::size_t i = 0; i < Result.length(); ++i)
  217. Result[i] = prev_float(x[i]);
  218. return Result;
  219. }
  220. template <typename T>
  221. GLM_FUNC_QUALIFIER T next_float(T const & x, uint const & ulps)
  222. {
  223. T temp = x;
  224. for(std::size_t i = 0; i < ulps; ++i)
  225. temp = next_float(temp);
  226. return temp;
  227. }
  228. template<typename T, template<typename> class vecType>
  229. GLM_FUNC_QUALIFIER vecType<T> next_float(vecType<T> const & x, vecType<uint> const & ulps)
  230. {
  231. vecType<T> Result;
  232. for(std::size_t i = 0; i < Result.length(); ++i)
  233. Result[i] = next_float(x[i], ulps[i]);
  234. return Result;
  235. }
  236. template <typename T>
  237. GLM_FUNC_QUALIFIER T prev_float(T const & x, uint const & ulps)
  238. {
  239. T temp = x;
  240. for(std::size_t i = 0; i < ulps; ++i)
  241. temp = prev_float(temp);
  242. return temp;
  243. }
  244. template<typename T, template<typename> class vecType>
  245. GLM_FUNC_QUALIFIER vecType<T> prev_float(vecType<T> const & x, vecType<uint> const & ulps)
  246. {
  247. vecType<T> Result;
  248. for(std::size_t i = 0; i < Result.length(); ++i)
  249. Result[i] = prev_float(x[i], ulps[i]);
  250. return Result;
  251. }
  252. template <typename T>
  253. GLM_FUNC_QUALIFIER uint float_distance(T const & x, T const & y)
  254. {
  255. uint ulp = 0;
  256. if(x < y)
  257. {
  258. T temp = x;
  259. while(temp != y && ulp < std::numeric_limits<std::size_t>::max())
  260. {
  261. ++ulp;
  262. temp = next_float(temp);
  263. }
  264. }
  265. else if(y < x)
  266. {
  267. T temp = y;
  268. while(temp != x && ulp < std::numeric_limits<std::size_t>::max())
  269. {
  270. ++ulp;
  271. temp = next_float(temp);
  272. }
  273. }
  274. else // ==
  275. {
  276. }
  277. return ulp;
  278. }
  279. template<typename T, template<typename> class vecType>
  280. GLM_FUNC_QUALIFIER vecType<uint> float_distance(vecType<T> const & x, vecType<T> const & y)
  281. {
  282. vecType<uint> Result;
  283. for(std::size_t i = 0; i < Result.length(); ++i)
  284. Result[i] = float_distance(x[i], y[i]);
  285. return Result;
  286. }
  287. }//namespace glm