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