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
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
pub struct AtomicFloat {
atomic: AtomicU32,
}
impl AtomicFloat {
pub fn new(value: f32) -> AtomicFloat {
AtomicFloat { atomic: AtomicU32::new(value.to_bits()) }
}
#[inline]
pub fn get(&self) -> f32 {
f32::from_bits(self.atomic.load(Ordering::Relaxed))
}
#[inline]
pub fn set(&self, value: f32) {
self.atomic.store(value.to_bits(), Ordering::Relaxed)
}
}
impl Default for AtomicFloat {
fn default() -> Self {
AtomicFloat::new(0.0)
}
}
impl std::fmt::Debug for AtomicFloat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.get(), f)
}
}
impl std::fmt::Display for AtomicFloat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.get(), f)
}
}
impl From<f32> for AtomicFloat {
fn from(value: f32) -> Self {
AtomicFloat::new(value)
}
}
impl From<AtomicFloat> for f32 {
fn from(value: AtomicFloat) -> Self {
value.get()
}
}
pub struct AtomicFloatPair {
atomic: AtomicU64,
}
impl AtomicFloatPair {
pub fn new(v: (f32, f32)) -> AtomicFloatPair {
AtomicFloatPair {
atomic: AtomicU64::new(((v.0.to_bits() as u64) << 32) | (v.1.to_bits() as u64)),
}
}
#[inline]
pub fn get(&self) -> (f32, f32) {
let v = self.atomic.load(Ordering::Relaxed);
(f32::from_bits((v >> 32 & 0xFFFFFFFF) as u32), f32::from_bits((v & 0xFFFFFFFF) as u32))
}
#[inline]
pub fn set(&self, v: (f32, f32)) {
let v = ((v.0.to_bits() as u64) << 32) | (v.1.to_bits()) as u64;
self.atomic.store(v, Ordering::Relaxed)
}
}
impl Default for AtomicFloatPair {
fn default() -> Self {
AtomicFloatPair::new((0.0, 0.0))
}
}
impl std::fmt::Debug for AtomicFloatPair {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let v = self.get();
write!(f, "({}, {})", v.0, v.1)
}
}
impl std::fmt::Display for AtomicFloatPair {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let v = self.get();
write!(f, "({}, {})", v.0, v.1)
}
}
impl From<(f32, f32)> for AtomicFloatPair {
fn from(value: (f32, f32)) -> Self {
AtomicFloatPair::new((value.0, value.1))
}
}
impl From<AtomicFloatPair> for (f32, f32) {
fn from(value: AtomicFloatPair) -> Self {
value.get()
}
}