1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use crate::{
Atomic, AtomicFiniteRangeNumber, AtomicNumber, False, FiniteRangeNumber, Float, IsFloat,
IsInteger, IsNonZero, IsSigned, Number, True,
};
use core::sync::atomic::{AtomicU32, AtomicU64, Ordering};
#[cfg(feature = "half")]
use core::sync::atomic::AtomicU16;
#[derive(Debug)]
#[repr(transparent)]
/// Atomic [`f64`] based on [`AtomicU64`]
pub struct AtomicF64(AtomicU64);
#[derive(Debug)]
#[repr(transparent)]
/// Atomic [`f32`] based on [`AtomicU32`]
pub struct AtomicF32(AtomicU32);
macro_rules! impl_atomic_float {
($ty:ty, $atomic:ty, $inner:ty) => {
impl core::default::Default for $atomic {
fn default() -> Self {
Self::new(<Self as Atomic>::NonAtomicType::ZERO)
}
}
impl Atomic for $atomic {
type NonAtomicType = $ty;
fn new(value: Self::NonAtomicType) -> Self {
Self(<$inner>::new(value.to_bits()))
}
fn load(&self, order: Ordering) -> Self::NonAtomicType {
Self::NonAtomicType::from_bits(self.0.load(order))
}
fn store(&self, value: Self::NonAtomicType, order: Ordering) {
self.0.store(value.to_bits(), order)
}
fn get_mut(&mut self) -> &mut Self::NonAtomicType {
unsafe { &mut *(self as *mut Self as *mut Self::NonAtomicType) }
}
fn into_inner(self) -> Self::NonAtomicType {
Self::NonAtomicType::from_bits(self.0.into_inner())
}
#[inline(always)]
fn into_non_atomic_array<const N: usize>(data: [Self; N]) -> [Self::NonAtomicType; N] {
unsafe { *(data.as_ptr() as *const [Self::NonAtomicType; N]) }
}
#[inline(always)]
fn from_non_atomic_array<const N: usize>(data: [Self::NonAtomicType; N]) -> [Self; N] {
let mut res: [Self; N] = unsafe { core::mem::MaybeUninit::uninit().assume_init() };
for i in 0..N {
res[i] = Self::new(data[i]);
}
res
}
#[inline(always)]
fn get_mut_slice(this: &mut [Self]) -> &mut [Self::NonAtomicType] {
unsafe { core::mem::transmute::<&mut [Self], &mut [Self::NonAtomicType]>(this) }
}
#[inline(always)]
fn from_mut_slice(this: &mut [Self::NonAtomicType]) -> &mut [Self] {
unsafe { core::mem::transmute::<&mut [Self::NonAtomicType], &mut [Self]>(this) }
}
#[inline(always)]
fn get_mut_array<const N: usize>(
this: &mut [Self; N],
) -> &mut [Self::NonAtomicType; N] {
unsafe {
core::mem::transmute::<&mut [Self; N], &mut [Self::NonAtomicType; N]>(this)
}
}
#[inline(always)]
fn from_mut_array<const N: usize>(
this: &mut [Self::NonAtomicType; N],
) -> &mut [Self; N] {
unsafe {
core::mem::transmute::<&mut [Self::NonAtomicType; N], &mut [Self; N]>(this)
}
}
fn compare_exchange(
&self,
current: Self::NonAtomicType,
new: Self::NonAtomicType,
success: Ordering,
failure: Ordering,
) -> Result<Self::NonAtomicType, Self::NonAtomicType> {
self.0
.compare_exchange(current.to_bits(), new.to_bits(), success, failure)
.map(Self::NonAtomicType::from_bits)
.map_err(Self::NonAtomicType::from_bits)
}
fn compare_exchange_weak(
&self,
current: Self::NonAtomicType,
new: Self::NonAtomicType,
success: Ordering,
failure: Ordering,
) -> Result<Self::NonAtomicType, Self::NonAtomicType> {
self.0
.compare_exchange_weak(current.to_bits(), new.to_bits(), success, failure)
.map(Self::NonAtomicType::from_bits)
.map_err(Self::NonAtomicType::from_bits)
}
fn swap(&self, value: Self::NonAtomicType, order: Ordering) -> Self::NonAtomicType {
Self::NonAtomicType::from_bits(self.0.swap(value.to_bits(), order))
}
fn fetch_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
mut f: F,
) -> Result<Self::NonAtomicType, Self::NonAtomicType>
where
F: FnMut(Self::NonAtomicType) -> Option<Self::NonAtomicType>,
{
self.0
.fetch_update(set_order, fetch_order, |x| {
f(Self::NonAtomicType::from_bits(x)).map(Self::NonAtomicType::to_bits)
})
.map(Self::NonAtomicType::from_bits)
.map_err(Self::NonAtomicType::from_bits)
}
}
impl AtomicNumber for $atomic {
fn fetch_min(
&self,
value: Self::NonAtomicType,
order: Ordering,
) -> Self::NonAtomicType {
self.fetch_update(Ordering::Relaxed, order, |x| {
Some(Self::NonAtomicType::min(x, value))
})
.unwrap()
}
fn fetch_max(
&self,
value: Self::NonAtomicType,
order: Ordering,
) -> Self::NonAtomicType {
self.fetch_update(Ordering::Relaxed, order, |x| {
Some(Self::NonAtomicType::max(x, value))
})
.unwrap()
}
fn fetch_add(
&self,
value: Self::NonAtomicType,
order: Ordering,
) -> Self::NonAtomicType {
self.fetch_update(Ordering::Relaxed, order, |x| Some(x + value))
.unwrap()
}
fn fetch_sub(
&self,
value: Self::NonAtomicType,
order: Ordering,
) -> Self::NonAtomicType {
self.fetch_update(Ordering::Relaxed, order, |x| Some(x - value))
.unwrap()
}
}
impl AtomicFiniteRangeNumber for $atomic {
#[inline(always)]
fn fetch_saturating_add(
&self,
value: Self::NonAtomicType,
set_order: Ordering,
fetch_order: Ordering,
) -> Self::NonAtomicType {
self.fetch_update(set_order, fetch_order, |x| Some(x.saturating_add(value)))
.unwrap()
}
#[inline(always)]
fn fetch_saturating_sub(
&self,
value: Self::NonAtomicType,
set_order: Ordering,
fetch_order: Ordering,
) -> Self::NonAtomicType {
self.fetch_update(set_order, fetch_order, |x| Some(x.saturating_sub(value)))
.unwrap()
}
#[inline(always)]
fn fetch_saturating_mul(
&self,
value: Self::NonAtomicType,
set_order: Ordering,
fetch_order: Ordering,
) -> Self::NonAtomicType {
self.fetch_update(set_order, fetch_order, |x| Some(x.saturating_mul(value)))
.unwrap()
}
#[inline(always)]
fn fetch_saturating_div(
&self,
value: Self::NonAtomicType,
set_order: Ordering,
fetch_order: Ordering,
) -> Self::NonAtomicType {
self.fetch_update(set_order, fetch_order, |x| Some(x.saturating_div(value)))
.unwrap()
}
#[cfg(feature = "std")]
#[inline(always)]
fn fetch_saturating_pow(
&self,
value: Self::NonAtomicType,
set_order: Ordering,
fetch_order: Ordering,
) -> Self::NonAtomicType {
self.fetch_update(set_order, fetch_order, |x| Some(x.saturating_pow(value)))
.unwrap()
}
}
};
}
impl_atomic_float!(f64, AtomicF64, AtomicU64);
impl_atomic_float!(f32, AtomicF32, AtomicU32);
#[cfg(feature = "half")]
#[derive(Debug)]
#[repr(transparent)]
/// Atomic [`half::f16`] based on [`AtomicU16`]
pub struct AtomicF16(pub(crate) AtomicU16);
#[cfg(feature = "half")]
#[derive(Debug)]
#[repr(transparent)]
/// Atomic [`half::bf16`] based on [`AtomicU16`]
pub struct AtomicBF16(pub(crate) AtomicU16);
/// An atomic float type.
pub trait AtomicFloat:
AtomicFiniteRangeNumber
+ IsFloat<Float = True>
+ IsInteger<Integer = False>
+ IsSigned<Signed = True>
+ IsNonZero<NonZero = False>
where
Self::NonAtomicType: Float,
{
/// Returns true if this value is NaN.
fn is_nan(&self, order: Ordering) -> bool;
/// Returns true if this value is positive infinity or negative infinity,
/// and false otherwise.
fn is_infinite(&self, order: Ordering) -> bool;
/// Returns true if this number is neither infinite nor NaN.
fn is_finite(&self, order: Ordering) -> bool;
/// Return `true` if the number is [subnormal](https://en.wikipedia.org/wiki/Subnormal_number)
fn is_subnormal(&self, order: Ordering) -> bool;
/// Return `true` if the number is neither zero, infinite, [subnormal](https://en.wikipedia.org/wiki/Subnormal_number), or NaN.
fn is_normal(&self, order: Ordering) -> bool;
/// Returns true if self has a positive sign, including +0.0, NaNs with
/// positive sign bit and positive infinity. Note that IEEE 754 doesn’t
/// assign any meaning to the sign bit in case of a NaN, and as Rust doesn’t
/// guarantee that the bit pattern of NaNs are conserved over arithmetic
/// operations, the result of is_sign_positive on a NaN might produce an
/// unexpected result in some cases. See explanation of NaN as a special
/// value for more info.
fn is_sign_positive(&self, order: Ordering) -> bool;
/// Returns true if self has a negative sign, including -0.0, NaNs with
/// egative sign bit and negative infinity. Note that IEEE 754 doesn’t a
/// ssign any meaning to the sign bit in case of a NaN, and as Rust doesn’t
/// guarantee that the bit pattern of NaNs are conserved over arithmetic
/// operations, the result of is_sign_negative on a NaN might produce an
/// unexpected result in some cases. See explanation of NaN as a special
/// value for more info.
fn is_sign_negative(&self, order: Ordering) -> bool;
/// Returns the floating point category of the number. If only one property
/// is going to be tested, it is generally faster to use the specific
/// predicate instead.
fn classify(&self, order: Ordering) -> core::num::FpCategory;
/// Atomically set self to the reciprocal (inverse) of a number, 1/x.
fn fetch_recip(&self, order: Ordering);
/// Converts radians to degrees.
fn fetch_to_degrees(&self, order: Ordering);
/// Converts degrees to radians.
fn fetch_to_radians(&self, order: Ordering);
/// Performs Euclidean division.
/// Since, for the positive integers, all common definitions of division are
/// equal, this is exactly equal to self / rhs.
#[cfg(feature = "std")]
fn fetch_div_euclid(&self, rhs: Self::NonAtomicType, order: Ordering);
/// Calculates the least remainder of self (mod rhs).
/// Since, for the positive integers, all common definitions of division are
/// equal, this is exactly equal to self % rhs.
#[cfg(feature = "std")]
fn fetch_rem_euclid(&self, rhs: Self::NonAtomicType, order: Ordering);
/// Returns the largest integer less than or equal to self.
#[cfg(feature = "std")]
fn fetch_floor(&self, order: Ordering);
/// Returns the smallest integer greater than or equal to self.
#[cfg(feature = "std")]
fn fetch_ceil(&self, order: Ordering);
/// Returns the nearest integer to self. Round half-way cases away from 0.0.
#[cfg(feature = "std")]
fn fetch_round(&self, order: Ordering);
/// Returns the integer part of self. This means that non-integer numbers
/// are always truncated towards zero.
#[cfg(feature = "std")]
fn fetch_trunc(&self, order: Ordering);
/// Returns the fractional part of self.
#[cfg(feature = "std")]
fn fetch_fract(&self, order: Ordering);
/// Computes the absolute value of self.
#[cfg(feature = "std")]
fn fetch_abs(&self, order: Ordering);
/// Returns a number that represents the sign of self.
///
/// - 1.0 if the number is positive, +0.0 or INFINITY
/// - -1.0 if the number is negative, -0.0 or NEG_INFINITY
/// - NaN if the number is NaN
#[cfg(feature = "std")]
fn fetch_signum(&self, order: Ordering);
/// Returns a number composed of the magnitude of self and the sign of sign.
///
/// Equal to self if the sign of self and sign are the same, otherwise equal
/// to -self. If self is a NaN, then a NaN with the sign bit of sign is
/// returned. Note, however, that conserving the sign bit on NaN across
/// arithmetical operations is not generally guaranteed. See explanation of
/// NaN as a special value for more info.
#[cfg(feature = "std")]
fn fetch_copysign(&self, sign: Self::NonAtomicType, order: Ordering);
/// Raises a number to an integer power.
///
/// Using this function is generally faster than using powf. It might have a
/// different sequence of rounding operations than powf, so the results are
/// not guaranteed to agree.
#[cfg(feature = "std")]
fn fetch_powi(&self, n: isize, order: Ordering);
/// Raises a number to a floating point power.
#[cfg(feature = "std")]
fn fetch_powf(&self, n: Self::NonAtomicType, order: Ordering);
/// Returns the square root of a number.
///
/// Returns NaN if self is a negative number other than -0.0.
#[cfg(feature = "std")]
fn fetch_sqrt(&self, order: Ordering);
/// Returns `e^(self)`, (the exponential function).
#[cfg(feature = "std")]
fn fetch_exp(&self, order: Ordering);
/// Returns 2^(self).
#[cfg(feature = "std")]
fn fetch_exp2(&self, order: Ordering);
/// Returns the natural logarithm of the number.
#[cfg(feature = "std")]
fn fetch_ln(&self, order: Ordering);
/// Returns the logarithm of the number with respect to an arbitrary base.
///
/// The result might not be correctly rounded owing to implementation
/// details; self.log2() can produce more accurate results for base 2,
/// and self.log10() can produce more accurate results for base 10.
#[cfg(feature = "std")]
fn fetch_log(&self, base: Self::NonAtomicType, order: Ordering);
/// Returns the base 2 logarithm of the number.
#[cfg(feature = "std")]
fn fetch_log2(&self, order: Ordering);
/// Returns the base 10 logarithm of the number.
#[cfg(feature = "std")]
fn fetch_log10(&self, order: Ordering);
/// Returns the cube root of a number.
#[cfg(feature = "std")]
fn fetch_cbrt(&self, order: Ordering);
/// Computes the sine of a number (in radians).
#[cfg(feature = "std")]
fn fetch_sin(&self, order: Ordering);
/// Computes the cosine of a number (in radians).
#[cfg(feature = "std")]
fn fetch_cos(&self, order: Ordering);
/// Computes the tangent of a number (in radians).
#[cfg(feature = "std")]
fn fetch_tan(&self, order: Ordering);
/// Computes the arcsine of a number. Return value is in radians in the
/// range [-pi/2, pi/2] or NaN if the number is outside the range [-1, 1].
#[cfg(feature = "std")]
fn fetch_asin(&self, order: Ordering);
/// Computes the arccosine of a number. Return value is in radians in the
/// range [0, pi] or NaN if the number is outside the range [-1, 1].
#[cfg(feature = "std")]
fn fetch_acos(&self, order: Ordering);
/// Computes the arctangent of a number. Return value is in radians in the
/// range [-pi/2, pi/2];
#[cfg(feature = "std")]
fn fetch_atan(&self, order: Ordering);
/// Returns e^(self) - 1 in a way that is accurate even if the number is
/// close to zero.
#[cfg(feature = "std")]
fn fetch_exp_m1(&self, order: Ordering);
/// Returns ln(1+n) (natural logarithm) more accurately than if the
/// operations were performed separately.
#[cfg(feature = "std")]
fn fetch_ln_1p(&self, order: Ordering);
/// Hyperbolic sine function.
#[cfg(feature = "std")]
fn fetch_sinh(&self, order: Ordering);
/// Hyperbolic cosine function.
#[cfg(feature = "std")]
fn fetch_cosh(&self, order: Ordering);
/// Hyperbolic tangent function.
#[cfg(feature = "std")]
fn fetch_tanh(&self, order: Ordering);
/// Inverse hyperbolic sine function.
#[cfg(feature = "std")]
fn fetch_asinh(&self, order: Ordering);
/// Inverse hyperbolic cosine function.
#[cfg(feature = "std")]
fn fetch_acosh(&self, order: Ordering);
/// Inverse hyperbolic tangent function.
#[cfg(feature = "std")]
fn fetch_atanh(&self, order: Ordering);
}