@@ -57,21 +57,25 @@ struct SqrtConfig<unsigned accum> : SqrtConfig<unsigned long fract> {};
57
57
// fixed, absolute);
58
58
// print("{", coeff(P, 1), "uhr,", coeff(P, 0), "uhr},");
59
59
// };
60
- static constexpr unsigned short fract SQRT_FIRST_APPROX[12 ][2 ] = {
61
- {0x1 .e8p -1uhr, 0x1 .0cp-2uhr}, {0x1 .bap -1uhr, 0x1 .28p-2uhr},
62
- {0x1 .94p-1uhr, 0x1 .44p-2uhr}, {0x1 .74p-1uhr, 0x1 .6p-2uhr},
63
- {0x1 .6p-1uhr, 0x1 .74p-2uhr}, {0x1 .4ep-1uhr, 0x1 .88p-2uhr},
64
- {0x1 .3ep-1uhr, 0x1 .9cp-2uhr}, {0x1 .32p-1uhr, 0x1 .acp -2uhr},
65
- {0x1 .22p-1uhr, 0x1 .c4p -2uhr}, {0x1 .18p-1uhr, 0x1 .d4p -2uhr},
66
- {0x1 .08p-1uhr, 0x1 .fp -2uhr}, {0x1 .04p-1uhr, 0x1 .f8p -2uhr},
60
+ // Clang is having issues with this array:
61
+ // static constexpr unsigned short fract SQRT_FIRST_APPROX[12][2] = {
62
+ // {0x1.e8p-1uhr, 0x1.0cp-2uhr}, {0x1.bap-1uhr, 0x1.28p-2uhr},
63
+ // {0x1.94p-1uhr, 0x1.44p-2uhr}, {0x1.74p-1uhr, 0x1.6p-2uhr},
64
+ // {0x1.6p-1uhr, 0x1.74p-2uhr}, {0x1.4ep-1uhr, 0x1.88p-2uhr},
65
+ // {0x1.3ep-1uhr, 0x1.9cp-2uhr}, {0x1.32p-1uhr, 0x1.acp-2uhr},
66
+ // {0x1.22p-1uhr, 0x1.c4p-2uhr}, {0x1.18p-1uhr, 0x1.d4p-2uhr},
67
+ // {0x1.08p-1uhr, 0x1.fp-2uhr}, {0x1.04p-1uhr, 0x1.f8p-2uhr},
68
+ // };
69
+ // We are using their storage type instead.
70
+ static constexpr uint8_t SQRT_FIRST_APPROX[12 ][2 ] = {
71
+ {244 , 67 }, {221 , 74 }, {202 , 81 }, {186 , 88 }, {176 , 93 }, {167 , 98 },
72
+ {159 , 103 }, {153 , 107 }, {145 , 113 }, {140 , 117 }, {132 , 124 }, {130 , 126 },
67
73
};
68
74
69
75
} // namespace internal
70
76
71
77
template <typename T>
72
- LIBC_INLINE constexpr cpp::enable_if_t <
73
- cpp::is_fixed_point_v<T> && cpp::is_unsigned_v<T>, T>
74
- sqrt (T x) {
78
+ LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_fixed_point_v<T>, T> sqrt (T x) {
75
79
using BitType = typename FXRep<T>::StorageType;
76
80
BitType x_bit = cpp::bit_cast<BitType>(x);
77
81
@@ -95,8 +99,10 @@ sqrt(T x) {
95
99
96
100
// Use the leading 4 bits to do look up for sqrt(x).
97
101
int index = (x_bit >> (STORAGE_LENGTH - 4 )) - 4 ;
98
- FracType a = static_cast <FracType>(internal::SQRT_FIRST_APPROX[index][0 ]);
99
- FracType b = static_cast <FracType>(internal::SQRT_FIRST_APPROX[index][1 ]);
102
+ FracType a = static_cast <FracType>(cpp::bit_cast<unsigned short fract>(
103
+ internal::SQRT_FIRST_APPROX[index][0 ]));
104
+ FracType b = static_cast <FracType>(cpp::bit_cast<unsigned short fract>(
105
+ internal::SQRT_FIRST_APPROX[index][1 ]));
100
106
101
107
// Initial approximation step.
102
108
// Estimated error bounds: | r - sqrt(x_frac) | < max(1.5 * 2^-11, eps).
0 commit comments