1#![doc(hidden)]
4
5#[cfg(all(not(feature = "std"), feature = "compact"))]
6use crate::libm::{powd, powf};
7#[cfg(not(feature = "compact"))]
8use crate::table::{SMALL_F32_POW10, SMALL_F64_POW10, SMALL_INT_POW10, SMALL_INT_POW5};
9#[cfg(not(feature = "compact"))]
10use core::hint;
11use core::ops;
12
13pub trait Float:
19 Sized
20 + Copy
21 + PartialEq
22 + PartialOrd
23 + Send
24 + Sync
25 + ops::Add<Output = Self>
26 + ops::AddAssign
27 + ops::Div<Output = Self>
28 + ops::DivAssign
29 + ops::Mul<Output = Self>
30 + ops::MulAssign
31 + ops::Rem<Output = Self>
32 + ops::RemAssign
33 + ops::Sub<Output = Self>
34 + ops::SubAssign
35 + ops::Neg<Output = Self>
36{
37 const MAX_DIGITS: usize;
62
63 const SIGN_MASK: u64;
67 const EXPONENT_MASK: u64;
69 const HIDDEN_BIT_MASK: u64;
71 const MANTISSA_MASK: u64;
73
74 const MANTISSA_SIZE: i32;
78 const EXPONENT_BIAS: i32;
80 const DENORMAL_EXPONENT: i32;
82 const MAX_EXPONENT: i32;
84
85 const CARRY_MASK: u64;
89
90 const INVALID_FP: i32 = -0x8000;
93
94 const MAX_MANTISSA_FAST_PATH: u64 = 2_u64 << Self::MANTISSA_SIZE;
96
97 const INFINITE_POWER: i32 = Self::MAX_EXPONENT + Self::EXPONENT_BIAS;
99
100 const MIN_EXPONENT_ROUND_TO_EVEN: i32;
119 const MAX_EXPONENT_ROUND_TO_EVEN: i32;
120
121 const MINIMUM_EXPONENT: i32;
123
124 const SMALLEST_POWER_OF_TEN: i32;
126
127 const LARGEST_POWER_OF_TEN: i32;
129
130 const MIN_EXPONENT_FAST_PATH: i32;
132
133 const MAX_EXPONENT_FAST_PATH: i32;
135
136 const MAX_EXPONENT_DISGUISED_FAST_PATH: i32;
139
140 fn from_u64(u: u64) -> Self;
142
143 fn from_bits(u: u64) -> Self;
145 fn to_bits(self) -> u64;
146
147 unsafe fn pow_fast_path(exponent: usize) -> Self;
153
154 #[inline(always)]
160 unsafe fn int_pow_fast_path(exponent: usize, radix: u32) -> u64 {
161 #[cfg(not(feature = "compact"))]
163 return match radix {
164 5 => unsafe { *SMALL_INT_POW5.get_unchecked(exponent) },
165 10 => unsafe { *SMALL_INT_POW10.get_unchecked(exponent) },
166 _ => unsafe { hint::unreachable_unchecked() },
167 };
168
169 #[cfg(feature = "compact")]
170 return (radix as u64).pow(exponent as u32);
171 }
172
173 #[inline]
175 fn is_denormal(self) -> bool {
176 self.to_bits() & Self::EXPONENT_MASK == 0
177 }
178
179 #[inline]
181 fn exponent(self) -> i32 {
182 if self.is_denormal() {
183 return Self::DENORMAL_EXPONENT;
184 }
185
186 let bits = self.to_bits();
187 let biased_e: i32 = ((bits & Self::EXPONENT_MASK) >> Self::MANTISSA_SIZE) as i32;
188 biased_e - Self::EXPONENT_BIAS
189 }
190
191 #[inline]
193 fn mantissa(self) -> u64 {
194 let bits = self.to_bits();
195 let s = bits & Self::MANTISSA_MASK;
196 if !self.is_denormal() {
197 s + Self::HIDDEN_BIT_MASK
198 } else {
199 s
200 }
201 }
202}
203
204impl Float for f32 {
205 const MAX_DIGITS: usize = 114;
206 const SIGN_MASK: u64 = 0x80000000;
207 const EXPONENT_MASK: u64 = 0x7F800000;
208 const HIDDEN_BIT_MASK: u64 = 0x00800000;
209 const MANTISSA_MASK: u64 = 0x007FFFFF;
210 const MANTISSA_SIZE: i32 = 23;
211 const EXPONENT_BIAS: i32 = 127 + Self::MANTISSA_SIZE;
212 const DENORMAL_EXPONENT: i32 = 1 - Self::EXPONENT_BIAS;
213 const MAX_EXPONENT: i32 = 0xFF - Self::EXPONENT_BIAS;
214 const CARRY_MASK: u64 = 0x1000000;
215 const MIN_EXPONENT_ROUND_TO_EVEN: i32 = -17;
216 const MAX_EXPONENT_ROUND_TO_EVEN: i32 = 10;
217 const MINIMUM_EXPONENT: i32 = -127;
218 const SMALLEST_POWER_OF_TEN: i32 = -65;
219 const LARGEST_POWER_OF_TEN: i32 = 38;
220 const MIN_EXPONENT_FAST_PATH: i32 = -10;
221 const MAX_EXPONENT_FAST_PATH: i32 = 10;
222 const MAX_EXPONENT_DISGUISED_FAST_PATH: i32 = 17;
223
224 #[inline(always)]
225 unsafe fn pow_fast_path(exponent: usize) -> Self {
226 #[cfg(not(feature = "compact"))]
228 return unsafe { *SMALL_F32_POW10.get_unchecked(exponent) };
229
230 #[cfg(feature = "compact")]
231 return powf(10.0f32, exponent as f32);
232 }
233
234 #[inline]
235 fn from_u64(u: u64) -> f32 {
236 u as _
237 }
238
239 #[inline]
240 fn from_bits(u: u64) -> f32 {
241 debug_assert!(u <= 0xffff_ffff);
243 f32::from_bits(u as u32)
244 }
245
246 #[inline]
247 fn to_bits(self) -> u64 {
248 f32::to_bits(self) as u64
249 }
250}
251
252impl Float for f64 {
253 const MAX_DIGITS: usize = 769;
254 const SIGN_MASK: u64 = 0x8000000000000000;
255 const EXPONENT_MASK: u64 = 0x7FF0000000000000;
256 const HIDDEN_BIT_MASK: u64 = 0x0010000000000000;
257 const MANTISSA_MASK: u64 = 0x000FFFFFFFFFFFFF;
258 const MANTISSA_SIZE: i32 = 52;
259 const EXPONENT_BIAS: i32 = 1023 + Self::MANTISSA_SIZE;
260 const DENORMAL_EXPONENT: i32 = 1 - Self::EXPONENT_BIAS;
261 const MAX_EXPONENT: i32 = 0x7FF - Self::EXPONENT_BIAS;
262 const CARRY_MASK: u64 = 0x20000000000000;
263 const MIN_EXPONENT_ROUND_TO_EVEN: i32 = -4;
264 const MAX_EXPONENT_ROUND_TO_EVEN: i32 = 23;
265 const MINIMUM_EXPONENT: i32 = -1023;
266 const SMALLEST_POWER_OF_TEN: i32 = -342;
267 const LARGEST_POWER_OF_TEN: i32 = 308;
268 const MIN_EXPONENT_FAST_PATH: i32 = -22;
269 const MAX_EXPONENT_FAST_PATH: i32 = 22;
270 const MAX_EXPONENT_DISGUISED_FAST_PATH: i32 = 37;
271
272 #[inline(always)]
273 unsafe fn pow_fast_path(exponent: usize) -> Self {
274 #[cfg(not(feature = "compact"))]
276 return unsafe { *SMALL_F64_POW10.get_unchecked(exponent) };
277
278 #[cfg(feature = "compact")]
279 return powd(10.0f64, exponent as f64);
280 }
281
282 #[inline]
283 fn from_u64(u: u64) -> f64 {
284 u as _
285 }
286
287 #[inline]
288 fn from_bits(u: u64) -> f64 {
289 f64::from_bits(u)
290 }
291
292 #[inline]
293 fn to_bits(self) -> u64 {
294 f64::to_bits(self)
295 }
296}
297
298#[inline(always)]
299#[cfg(all(feature = "std", feature = "compact"))]
300pub fn powf(x: f32, y: f32) -> f32 {
301 x.powf(y)
302}
303
304#[inline(always)]
305#[cfg(all(feature = "std", feature = "compact"))]
306pub fn powd(x: f64, y: f64) -> f64 {
307 x.powf(y)
308}