Collection of DPF-based plugins for packaging
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.

340 lines
8.3KB

  1. /// @ref gtc_ulp
  2. /// @file glm/gtc/ulp.inl
  3. ///
  4. /// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. ///
  6. /// Developed at SunPro, a Sun Microsystems, Inc. business.
  7. /// Permission to use, copy, modify, and distribute this
  8. /// software is freely granted, provided that this notice
  9. /// is preserved.
  10. #include "../detail/type_int.hpp"
  11. #include "epsilon.hpp"
  12. #include <cmath>
  13. #include <cfloat>
  14. #include <limits>
  15. #if(GLM_COMPILER & GLM_COMPILER_VC)
  16. # pragma warning(push)
  17. # pragma warning(disable : 4127)
  18. #endif
  19. typedef union
  20. {
  21. float value;
  22. /* FIXME: Assumes 32 bit int. */
  23. unsigned int word;
  24. } ieee_float_shape_type;
  25. typedef union
  26. {
  27. double value;
  28. struct
  29. {
  30. glm::detail::int32 lsw;
  31. glm::detail::int32 msw;
  32. } parts;
  33. } ieee_double_shape_type;
  34. #define GLM_EXTRACT_WORDS(ix0,ix1,d) \
  35. do { \
  36. ieee_double_shape_type ew_u; \
  37. ew_u.value = (d); \
  38. (ix0) = ew_u.parts.msw; \
  39. (ix1) = ew_u.parts.lsw; \
  40. } while (0)
  41. #define GLM_GET_FLOAT_WORD(i,d) \
  42. do { \
  43. ieee_float_shape_type gf_u; \
  44. gf_u.value = (d); \
  45. (i) = gf_u.word; \
  46. } while (0)
  47. #define GLM_SET_FLOAT_WORD(d,i) \
  48. do { \
  49. ieee_float_shape_type sf_u; \
  50. sf_u.word = (i); \
  51. (d) = sf_u.value; \
  52. } while (0)
  53. #define GLM_INSERT_WORDS(d,ix0,ix1) \
  54. do { \
  55. ieee_double_shape_type iw_u; \
  56. iw_u.parts.msw = (ix0); \
  57. iw_u.parts.lsw = (ix1); \
  58. (d) = iw_u.value; \
  59. } while (0)
  60. namespace glm{
  61. namespace detail
  62. {
  63. GLM_FUNC_QUALIFIER float nextafterf(float x, float y)
  64. {
  65. volatile float t;
  66. glm::detail::int32 hx, hy, ix, iy;
  67. GLM_GET_FLOAT_WORD(hx, x);
  68. GLM_GET_FLOAT_WORD(hy, y);
  69. ix = hx&0x7fffffff; // |x|
  70. iy = hy&0x7fffffff; // |y|
  71. if((ix>0x7f800000) || // x is nan
  72. (iy>0x7f800000)) // y is nan
  73. return x+y;
  74. if(compute_equal<float>::call(x, y))
  75. return y; // x=y, return y
  76. if(ix==0)
  77. { // x == 0
  78. GLM_SET_FLOAT_WORD(x,(hy&0x80000000)|1);// return +-minsubnormal
  79. t = x*x;
  80. if(detail::compute_equal<float>::call(t, x))
  81. return t;
  82. else
  83. return x; // raise underflow flag
  84. }
  85. if(hx>=0)
  86. { // x > 0
  87. if(hx>hy) // x > y, x -= ulp
  88. hx -= 1;
  89. else // x < y, x += ulp
  90. hx += 1;
  91. }
  92. else
  93. { // x < 0
  94. if(hy>=0||hx>hy) // x < y, x -= ulp
  95. hx -= 1;
  96. else // x > y, x += ulp
  97. hx += 1;
  98. }
  99. hy = hx&0x7f800000;
  100. if(hy>=0x7f800000)
  101. return x+x; // overflow
  102. if(hy<0x00800000) // underflow
  103. {
  104. t = x*x;
  105. if(!detail::compute_equal<float>::call(t, x))
  106. { // raise underflow flag
  107. GLM_SET_FLOAT_WORD(y,hx);
  108. return y;
  109. }
  110. }
  111. GLM_SET_FLOAT_WORD(x,hx);
  112. return x;
  113. }
  114. GLM_FUNC_QUALIFIER double nextafter(double x, double y)
  115. {
  116. volatile double t;
  117. glm::detail::int32 hx, hy, ix, iy;
  118. glm::detail::uint32 lx, ly;
  119. GLM_EXTRACT_WORDS(hx, lx, x);
  120. GLM_EXTRACT_WORDS(hy, ly, y);
  121. ix = hx & 0x7fffffff; // |x|
  122. iy = hy & 0x7fffffff; // |y|
  123. if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || // x is nan
  124. ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) // y is nan
  125. return x+y;
  126. if(detail::compute_equal<double>::call(x, y))
  127. return y; // x=y, return y
  128. if((ix|lx)==0)
  129. { // x == 0
  130. GLM_INSERT_WORDS(x, hy & 0x80000000, 1); // return +-minsubnormal
  131. t = x*x;
  132. if(detail::compute_equal<double>::call(t, x))
  133. return t;
  134. else
  135. return x; // raise underflow flag
  136. }
  137. if(hx>=0) { // x > 0
  138. if(hx>hy||((hx==hy)&&(lx>ly))) { // x > y, x -= ulp
  139. if(lx==0) hx -= 1;
  140. lx -= 1;
  141. } else { // x < y, x += ulp
  142. lx += 1;
  143. if(lx==0) hx += 1;
  144. }
  145. } else { // x < 0
  146. if(hy>=0||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. }
  154. hy = hx&0x7ff00000;
  155. if(hy>=0x7ff00000)
  156. return x+x; // overflow
  157. if(hy<0x00100000)
  158. { // underflow
  159. t = x*x;
  160. if(!detail::compute_equal<double>::call(t, x))
  161. { // raise underflow flag
  162. GLM_INSERT_WORDS(y,hx,lx);
  163. return y;
  164. }
  165. }
  166. GLM_INSERT_WORDS(x,hx,lx);
  167. return x;
  168. }
  169. }//namespace detail
  170. }//namespace glm
  171. #if(GLM_COMPILER & GLM_COMPILER_VC)
  172. # pragma warning(pop)
  173. #endif
  174. namespace glm
  175. {
  176. template<>
  177. GLM_FUNC_QUALIFIER float next_float(float const& x)
  178. {
  179. # if GLM_HAS_CXX11_STL
  180. return std::nextafter(x, std::numeric_limits<float>::max());
  181. # elif((GLM_COMPILER & GLM_COMPILER_VC) || ((GLM_COMPILER & GLM_COMPILER_INTEL) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS)))
  182. return detail::nextafterf(x, FLT_MAX);
  183. # elif(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
  184. return __builtin_nextafterf(x, FLT_MAX);
  185. # else
  186. return nextafterf(x, FLT_MAX);
  187. # endif
  188. }
  189. template<>
  190. GLM_FUNC_QUALIFIER double next_float(double const& x)
  191. {
  192. # if GLM_HAS_CXX11_STL
  193. return std::nextafter(x, std::numeric_limits<double>::max());
  194. # elif((GLM_COMPILER & GLM_COMPILER_VC) || ((GLM_COMPILER & GLM_COMPILER_INTEL) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS)))
  195. return detail::nextafter(x, std::numeric_limits<double>::max());
  196. # elif(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
  197. return __builtin_nextafter(x, FLT_MAX);
  198. # else
  199. return nextafter(x, DBL_MAX);
  200. # endif
  201. }
  202. template<length_t L, typename T, qualifier Q>
  203. GLM_FUNC_QUALIFIER vec<L, T, Q> next_float(vec<L, T, Q> const& x)
  204. {
  205. vec<L, T, Q> Result;
  206. for(length_t i = 0, n = Result.length(); i < n; ++i)
  207. Result[i] = next_float(x[i]);
  208. return Result;
  209. }
  210. GLM_FUNC_QUALIFIER float prev_float(float const& x)
  211. {
  212. # if GLM_HAS_CXX11_STL
  213. return std::nextafter(x, std::numeric_limits<float>::min());
  214. # elif((GLM_COMPILER & GLM_COMPILER_VC) || ((GLM_COMPILER & GLM_COMPILER_INTEL) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS)))
  215. return detail::nextafterf(x, FLT_MIN);
  216. # elif(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
  217. return __builtin_nextafterf(x, FLT_MIN);
  218. # else
  219. return nextafterf(x, FLT_MIN);
  220. # endif
  221. }
  222. GLM_FUNC_QUALIFIER double prev_float(double const& x)
  223. {
  224. # if GLM_HAS_CXX11_STL
  225. return std::nextafter(x, std::numeric_limits<double>::min());
  226. # elif((GLM_COMPILER & GLM_COMPILER_VC) || ((GLM_COMPILER & GLM_COMPILER_INTEL) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS)))
  227. return _nextafter(x, DBL_MIN);
  228. # elif(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
  229. return __builtin_nextafter(x, DBL_MIN);
  230. # else
  231. return nextafter(x, DBL_MIN);
  232. # endif
  233. }
  234. template<length_t L, typename T, qualifier Q>
  235. GLM_FUNC_QUALIFIER vec<L, T, Q> prev_float(vec<L, T, Q> const& x)
  236. {
  237. vec<L, T, Q> Result;
  238. for(length_t i = 0, n = Result.length(); i < n; ++i)
  239. Result[i] = prev_float(x[i]);
  240. return Result;
  241. }
  242. template<typename T>
  243. GLM_FUNC_QUALIFIER T next_float(T const& x, uint const& ulps)
  244. {
  245. T temp = x;
  246. for(uint i = 0; i < ulps; ++i)
  247. temp = next_float(temp);
  248. return temp;
  249. }
  250. template<length_t L, typename T, qualifier Q>
  251. GLM_FUNC_QUALIFIER vec<L, T, Q> next_float(vec<L, T, Q> const& x, vec<L, uint, Q> const& ulps)
  252. {
  253. vec<L, T, Q> Result;
  254. for(length_t i = 0, n = Result.length(); i < n; ++i)
  255. Result[i] = next_float(x[i], ulps[i]);
  256. return Result;
  257. }
  258. template<typename T>
  259. GLM_FUNC_QUALIFIER T prev_float(T const& x, uint const& ulps)
  260. {
  261. T temp = x;
  262. for(uint i = 0; i < ulps; ++i)
  263. temp = prev_float(temp);
  264. return temp;
  265. }
  266. template<length_t L, typename T, qualifier Q>
  267. GLM_FUNC_QUALIFIER vec<L, T, Q> prev_float(vec<L, T, Q> const& x, vec<L, uint, Q> const& ulps)
  268. {
  269. vec<L, T, Q> Result;
  270. for(length_t i = 0, n = Result.length(); i < n; ++i)
  271. Result[i] = prev_float(x[i], ulps[i]);
  272. return Result;
  273. }
  274. template<typename T>
  275. GLM_FUNC_QUALIFIER uint float_distance(T const& x, T const& y)
  276. {
  277. uint ulp = 0;
  278. if(x < y)
  279. {
  280. T temp = x;
  281. while(glm::epsilonNotEqual(temp, y, glm::epsilon<T>()))// && ulp < std::numeric_limits<std::size_t>::max())
  282. {
  283. ++ulp;
  284. temp = next_float(temp);
  285. }
  286. }
  287. else if(y < x)
  288. {
  289. T temp = y;
  290. while(glm::epsilonNotEqual(temp, x, glm::epsilon<T>()))// && ulp < std::numeric_limits<std::size_t>::max())
  291. {
  292. ++ulp;
  293. temp = next_float(temp);
  294. }
  295. }
  296. else // ==
  297. {
  298. }
  299. return ulp;
  300. }
  301. template<length_t L, typename T, qualifier Q>
  302. GLM_FUNC_QUALIFIER vec<L, uint, Q> float_distance(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
  303. {
  304. vec<L, uint, Q> Result;
  305. for(length_t i = 0, n = Result.length(); i < n; ++i)
  306. Result[i] = float_distance(x[i], y[i]);
  307. return Result;
  308. }
  309. }//namespace glm