| 1 | /// @ref gtc_ulp |
| 2 | |
| 3 | #include "../ext/scalar_ulp.hpp" |
| 4 | |
| 5 | namespace glm |
| 6 | { |
| 7 | template<> |
| 8 | GLM_FUNC_QUALIFIER float next_float(float x) |
| 9 | { |
| 10 | return std::nextafter(x, std::numeric_limits<float>::max()); |
| 11 | } |
| 12 | |
| 13 | template<> |
| 14 | GLM_FUNC_QUALIFIER double next_float(double x) |
| 15 | { |
| 16 | return std::nextafter(x, std::numeric_limits<double>::max()); |
| 17 | } |
| 18 | |
| 19 | template<typename T> |
| 20 | GLM_FUNC_QUALIFIER T next_float(T x, int ULPs) |
| 21 | { |
| 22 | static_assert(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT, "'next_float' only accept floating-point input"); |
| 23 | assert(ULPs >= 0); |
| 24 | |
| 25 | T temp = x; |
| 26 | for (int i = 0; i < ULPs; ++i) |
| 27 | temp = next_float(temp); |
| 28 | return temp; |
| 29 | } |
| 30 | |
| 31 | GLM_FUNC_QUALIFIER float prev_float(float x) |
| 32 | { |
| 33 | return std::nextafter(x, std::numeric_limits<float>::min()); |
| 34 | } |
| 35 | |
| 36 | GLM_FUNC_QUALIFIER double prev_float(double x) |
| 37 | { |
| 38 | return std::nextafter(x, std::numeric_limits<double>::min()); |
| 39 | } |
| 40 | |
| 41 | template<typename T> |
| 42 | GLM_FUNC_QUALIFIER T prev_float(T x, int ULPs) |
| 43 | { |
| 44 | static_assert(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT, "'prev_float' only accept floating-point input"); |
| 45 | assert(ULPs >= 0); |
| 46 | |
| 47 | T temp = x; |
| 48 | for (int i = 0; i < ULPs; ++i) |
| 49 | temp = prev_float(temp); |
| 50 | return temp; |
| 51 | } |
| 52 | |
| 53 | GLM_FUNC_QUALIFIER int float_distance(float x, float y) |
| 54 | { |
| 55 | detail::float_t<float> const a(x); |
| 56 | detail::float_t<float> const b(y); |
| 57 | |
| 58 | return abs(a.i - b.i); |
| 59 | } |
| 60 | |
| 61 | GLM_FUNC_QUALIFIER int64 float_distance(double x, double y) |
| 62 | { |
| 63 | detail::float_t<double> const a(x); |
| 64 | detail::float_t<double> const b(y); |
| 65 | |
| 66 | return abs(a.i - b.i); |
| 67 | } |
| 68 | |
| 69 | template<length_t L, typename T, qualifier Q> |
| 70 | GLM_FUNC_QUALIFIER vec<L, T, Q> next_float(vec<L, T, Q> const& x) |
| 71 | { |
| 72 | vec<L, T, Q> Result; |
| 73 | for (length_t i = 0, n = Result.length(); i < n; ++i) |
| 74 | Result[i] = next_float(x[i]); |
| 75 | return Result; |
| 76 | } |
| 77 | |
| 78 | template<length_t L, typename T, qualifier Q> |
| 79 | GLM_FUNC_QUALIFIER vec<L, T, Q> next_float(vec<L, T, Q> const& x, int ULPs) |
| 80 | { |
| 81 | vec<L, T, Q> Result; |
| 82 | for (length_t i = 0, n = Result.length(); i < n; ++i) |
| 83 | Result[i] = next_float(x[i], ULPs); |
| 84 | return Result; |
| 85 | } |
| 86 | |
| 87 | template<length_t L, typename T, qualifier Q> |
| 88 | GLM_FUNC_QUALIFIER vec<L, T, Q> next_float(vec<L, T, Q> const& x, vec<L, int, Q> const& ULPs) |
| 89 | { |
| 90 | vec<L, T, Q> Result; |
| 91 | for (length_t i = 0, n = Result.length(); i < n; ++i) |
| 92 | Result[i] = next_float(x[i], ULPs[i]); |
| 93 | return Result; |
| 94 | } |
| 95 | |
| 96 | template<length_t L, typename T, qualifier Q> |
| 97 | GLM_FUNC_QUALIFIER vec<L, T, Q> prev_float(vec<L, T, Q> const& x) |
| 98 | { |
| 99 | vec<L, T, Q> Result; |
| 100 | for (length_t i = 0, n = Result.length(); i < n; ++i) |
| 101 | Result[i] = prev_float(x[i]); |
| 102 | return Result; |
| 103 | } |
| 104 | |
| 105 | template<length_t L, typename T, qualifier Q> |
| 106 | GLM_FUNC_QUALIFIER vec<L, T, Q> prev_float(vec<L, T, Q> const& x, int ULPs) |
| 107 | { |
| 108 | vec<L, T, Q> Result; |
| 109 | for (length_t i = 0, n = Result.length(); i < n; ++i) |
| 110 | Result[i] = prev_float(x[i], ULPs); |
| 111 | return Result; |
| 112 | } |
| 113 | |
| 114 | template<length_t L, typename T, qualifier Q> |
| 115 | GLM_FUNC_QUALIFIER vec<L, T, Q> prev_float(vec<L, T, Q> const& x, vec<L, int, Q> const& ULPs) |
| 116 | { |
| 117 | vec<L, T, Q> Result; |
| 118 | for (length_t i = 0, n = Result.length(); i < n; ++i) |
| 119 | Result[i] = prev_float(x[i], ULPs[i]); |
| 120 | return Result; |
| 121 | } |
| 122 | |
| 123 | template<length_t L, qualifier Q> |
| 124 | GLM_FUNC_QUALIFIER vec<L, int, Q> float_distance(vec<L, float, Q> const& x, vec<L, float, Q> const& y) |
| 125 | { |
| 126 | vec<L, int, Q> Result; |
| 127 | for (length_t i = 0, n = Result.length(); i < n; ++i) |
| 128 | Result[i] = float_distance(x[i], y[i]); |
| 129 | return Result; |
| 130 | } |
| 131 | |
| 132 | template<length_t L, qualifier Q> |
| 133 | GLM_FUNC_QUALIFIER vec<L, int64, Q> float_distance(vec<L, double, Q> const& x, vec<L, double, Q> const& y) |
| 134 | { |
| 135 | vec<L, int64, Q> Result; |
| 136 | for (length_t i = 0, n = Result.length(); i < n; ++i) |
| 137 | Result[i] = float_distance(x[i], y[i]); |
| 138 | return Result; |
| 139 | } |
| 140 | }//namespace glm |
| 141 | |