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
/// A 128 bit floating point number stored in "raw".
///
/// This is needed as Rust does not support (and most systems)
/// don't support 128 bit floating point values.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct RawF128(u128);
impl RawF128 {
/// Create a floating point value from its representation as a
/// byte array in big endian.
#[inline]
pub const fn from_be_bytes(bytes: [u8; 16]) -> RawF128 {
RawF128(u128::from_be_bytes(bytes))
}
/// Create a floating point value from its representation as a
/// byte array in little endian.
#[inline]
pub const fn from_le_bytes(bytes: [u8; 16]) -> RawF128 {
RawF128(u128::from_le_bytes(bytes))
}
/// Create a floating point value from its representation as a byte
/// array in native endian.
#[inline]
pub const fn from_ne_bytes(bytes: [u8; 16]) -> RawF128 {
RawF128(u128::from_ne_bytes(bytes))
}
/// Return the memory representation of this floating point number
/// as a byte array in big-endian (network) byte order.
#[inline]
pub const fn to_be_bytes(self) -> [u8; 16] {
self.0.to_be_bytes()
}
/// Return the memory representation of this floating point number
/// as a byte array in little-endian byte order
#[inline]
pub const fn to_le_bytes(self) -> [u8; 16] {
self.0.to_le_bytes()
}
/// Return the memory representation of this floating point number as
/// a byte array in native byte order.
#[inline]
pub const fn to_ne_bytes(self) -> [u8; 16] {
self.0.to_ne_bytes()
}
/// Raw transmutation from `u128`.
#[inline]
pub const fn from_bits(bits: u128) -> RawF128 {
RawF128(bits)
}
/// Raw transmutation to `u1128`.
#[inline]
pub const fn to_bits(self) -> u128 {
self.0
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for RawF128 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_u128(self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
proptest! {
#[test]
fn debug_clone_eq(value in any::<u128>()) {
use alloc::format;
assert_eq!(
format!("{:?}", RawF128(value)),
format!("{:?}", RawF128(value))
);
assert_eq!(RawF128(value), RawF128(value).clone());
}
}
proptest! {
#[test]
fn from_be_bytes(value in any::<u128>()) {
assert_eq!(
value,
RawF128::from_be_bytes(value.to_be_bytes()).0
);
}
}
proptest! {
#[test]
fn from_le_bytes(value in any::<u128>()) {
assert_eq!(
value,
RawF128::from_le_bytes(value.to_le_bytes()).0
);
}
}
proptest! {
#[test]
fn from_ne_bytes(value in any::<u128>()) {
assert_eq!(
value,
RawF128::from_ne_bytes(value.to_ne_bytes()).0
);
}
}
proptest! {
#[test]
fn to_be_bytes(value in any::<u128>()) {
assert_eq!(
value.to_be_bytes(),
RawF128(value).to_be_bytes()
);
}
}
proptest! {
#[test]
fn to_le_bytes(value in any::<u128>()) {
assert_eq!(
value.to_le_bytes(),
RawF128(value).to_le_bytes()
);
}
}
proptest! {
#[test]
fn to_ne_bytes(value in any::<u128>()) {
assert_eq!(
value.to_ne_bytes(),
RawF128(value).to_ne_bytes()
);
}
}
proptest! {
#[test]
fn from_bits(value in any::<u128>()) {
assert_eq!(
value,
RawF128::from_bits(value).0
);
}
}
proptest! {
#[test]
fn to_bits(value in any::<u128>()) {
assert_eq!(
value,
RawF128(value).to_bits()
);
}
}
#[cfg(feature = "serde")]
proptest! {
#[test]
fn serialize(value in any::<u128>()) {
let v = RawF128(value);
assert_eq!(
serde_json::to_string(&v.to_bits()).unwrap(),
serde_json::to_string(&v).unwrap()
);
}
}
}