1use std::{f32, ptr};
5
6use reifydb_value::{reifydb_assertions, value::value_type::ValueType};
7
8use crate::encoded::{row::EncodedRow, shape::RowShape};
9
10impl RowShape {
11 pub fn set_f32(&self, row: &mut EncodedRow, index: usize, value: impl Into<f32>) {
12 let field = &self.fields()[index];
13 reifydb_assertions! {
14 assert!(
15 row.len() >= self.total_static_size(),
16 "row/shape size mismatch: row.len()={} < total_static_size()={}",
17 row.len(),
18 self.total_static_size()
19 );
20 assert_eq!(*field.constraint.get_type().inner_type(), ValueType::Float4);
21 }
22 row.set_valid(index, true);
23 unsafe {
24 ptr::write_unaligned(
25 row.make_mut().as_mut_ptr().add(field.offset as usize) as *mut f32,
26 value.into(),
27 )
28 }
29 }
30
31 pub fn get_f32(&self, row: &EncodedRow, index: usize) -> f32 {
32 let field = &self.fields()[index];
33 reifydb_assertions! {
34 assert!(
35 row.len() >= self.total_static_size(),
36 "row/shape size mismatch: row.len()={} < total_static_size()={}",
37 row.len(),
38 self.total_static_size()
39 );
40 assert_eq!(*field.constraint.get_type().inner_type(), ValueType::Float4);
41 }
42 unsafe { (row.as_ptr().add(field.offset as usize) as *const f32).read_unaligned() }
43 }
44
45 pub fn try_get_f32(&self, row: &EncodedRow, index: usize) -> Option<f32> {
46 if row.is_defined(index) && self.fields()[index].constraint.get_type() == ValueType::Float4 {
47 Some(self.get_f32(row, index))
48 } else {
49 None
50 }
51 }
52}
53
54#[cfg(test)]
55#[allow(clippy::approx_constant)]
56pub mod tests {
57 use std::f32::consts::{E, PI};
58
59 use reifydb_value::value::value_type::ValueType;
60
61 use crate::encoded::shape::RowShape;
62
63 #[test]
64 fn test_set_get_f32() {
65 let shape = RowShape::testing(&[ValueType::Float4]);
66 let mut row = shape.allocate();
67 shape.set_f32(&mut row, 0, 1.25f32);
68 assert_eq!(shape.get_f32(&row, 0), 1.25f32);
69 }
70
71 #[test]
72 fn test_try_get_f32() {
73 let shape = RowShape::testing(&[ValueType::Float4]);
74 let mut row = shape.allocate();
75
76 assert_eq!(shape.try_get_f32(&row, 0), None);
77
78 shape.set_f32(&mut row, 0, 1.25f32);
79 assert_eq!(shape.try_get_f32(&row, 0), Some(1.25f32));
80 }
81
82 #[test]
83 fn test_special_values() {
84 let shape = RowShape::testing(&[ValueType::Float4]);
85 let mut row = shape.allocate();
86
87 shape.set_f32(&mut row, 0, 0.0f32);
89 assert_eq!(shape.get_f32(&row, 0), 0.0f32);
90
91 let mut row2 = shape.allocate();
93 shape.set_f32(&mut row2, 0, -0.0f32);
94 assert_eq!(shape.get_f32(&row2, 0), -0.0f32);
95
96 let mut row3 = shape.allocate();
98 shape.set_f32(&mut row3, 0, f32::INFINITY);
99 assert_eq!(shape.get_f32(&row3, 0), f32::INFINITY);
100
101 let mut row4 = shape.allocate();
103 shape.set_f32(&mut row4, 0, f32::NEG_INFINITY);
104 assert_eq!(shape.get_f32(&row4, 0), f32::NEG_INFINITY);
105
106 let mut row5 = shape.allocate();
108 shape.set_f32(&mut row5, 0, f32::NAN);
109 assert!(shape.get_f32(&row5, 0).is_nan());
110 }
111
112 #[test]
113 fn test_extreme_values() {
114 let shape = RowShape::testing(&[ValueType::Float4]);
115 let mut row = shape.allocate();
116
117 shape.set_f32(&mut row, 0, f32::MAX);
118 assert_eq!(shape.get_f32(&row, 0), f32::MAX);
119
120 let mut row2 = shape.allocate();
121 shape.set_f32(&mut row2, 0, f32::MIN);
122 assert_eq!(shape.get_f32(&row2, 0), f32::MIN);
123
124 let mut row3 = shape.allocate();
125 shape.set_f32(&mut row3, 0, f32::MIN_POSITIVE);
126 assert_eq!(shape.get_f32(&row3, 0), f32::MIN_POSITIVE);
127 }
128
129 #[test]
130 fn test_mixed_with_other_types() {
131 let shape = RowShape::testing(&[ValueType::Float4, ValueType::Int4, ValueType::Float4]);
132 let mut row = shape.allocate();
133
134 shape.set_f32(&mut row, 0, 3.14f32);
135 shape.set_i32(&mut row, 1, 42);
136 shape.set_f32(&mut row, 2, -2.718f32);
137
138 assert_eq!(shape.get_f32(&row, 0), 3.14f32);
139 assert_eq!(shape.get_i32(&row, 1), 42);
140 assert_eq!(shape.get_f32(&row, 2), -2.718f32);
141 }
142
143 #[test]
144 fn test_undefined_handling() {
145 let shape = RowShape::testing(&[ValueType::Float4, ValueType::Float4]);
146 let mut row = shape.allocate();
147
148 shape.set_f32(&mut row, 0, 3.14f32);
149
150 assert_eq!(shape.try_get_f32(&row, 0), Some(3.14f32));
151 assert_eq!(shape.try_get_f32(&row, 1), None);
152
153 shape.set_none(&mut row, 0);
154 assert_eq!(shape.try_get_f32(&row, 0), None);
155 }
156
157 #[test]
158 fn test_try_get_f32_wrong_type() {
159 let shape = RowShape::testing(&[ValueType::Boolean]);
160 let mut row = shape.allocate();
161
162 shape.set_bool(&mut row, 0, true);
163
164 assert_eq!(shape.try_get_f32(&row, 0), None);
165 }
166
167 #[test]
168 fn test_subnormal_values() {
169 let shape = RowShape::testing(&[ValueType::Float4]);
170 let mut row = shape.allocate();
171
172 let min_subnormal = f32::from_bits(0x00000001);
174 shape.set_f32(&mut row, 0, min_subnormal);
175 assert_eq!(shape.get_f32(&row, 0).to_bits(), min_subnormal.to_bits());
176
177 let max_subnormal = f32::from_bits(0x007fffff);
179 shape.set_f32(&mut row, 0, max_subnormal);
180 assert_eq!(shape.get_f32(&row, 0).to_bits(), max_subnormal.to_bits());
181
182 let neg_subnormal = f32::from_bits(0x80000001);
184 shape.set_f32(&mut row, 0, neg_subnormal);
185 assert_eq!(shape.get_f32(&row, 0).to_bits(), neg_subnormal.to_bits());
186 }
187
188 #[test]
189 fn test_nan_payload_preservation() {
190 let shape = RowShape::testing(&[ValueType::Float4]);
191 let mut row = shape.allocate();
192
193 let quiet_nan = f32::NAN;
195 shape.set_f32(&mut row, 0, quiet_nan);
196 assert!(shape.get_f32(&row, 0).is_nan());
197
198 let nan_with_payload = f32::from_bits(0x7fc00001);
200 shape.set_f32(&mut row, 0, nan_with_payload);
201 assert_eq!(shape.get_f32(&row, 0).to_bits(), nan_with_payload.to_bits());
202
203 let neg_nan = f32::from_bits(0xffc00000);
205 shape.set_f32(&mut row, 0, neg_nan);
206 assert_eq!(shape.get_f32(&row, 0).to_bits(), neg_nan.to_bits());
207 }
208
209 #[test]
210 fn test_repeated_operations() {
211 let shape = RowShape::testing(&[ValueType::Float4]);
212 let mut row = shape.allocate();
213 let initial_len = row.len();
214
215 for i in 0..1000 {
217 let value = (i as f32) * 0.1;
218 shape.set_f32(&mut row, 0, value);
219 assert_eq!(shape.get_f32(&row, 0), value);
220 }
221
222 assert_eq!(row.len(), initial_len);
224 }
225
226 #[test]
227 fn test_unaligned_access() {
228 let shape = create_unaligned_layout(ValueType::Float4);
229 let mut row = shape.allocate();
230
231 shape.set_f32(&mut row, 1, PI);
233 assert_eq!(shape.get_f32(&row, 1), PI);
234
235 shape.set_f32(&mut row, 3, E);
237 assert_eq!(shape.get_f32(&row, 3), E);
238
239 assert_eq!(shape.get_f32(&row, 1), PI);
241 assert_eq!(shape.get_f32(&row, 3), E);
242 }
243
244 #[test]
245 fn test_denormalized_transitions() {
246 let shape = RowShape::testing(&[ValueType::Float4]);
247 let mut row = shape.allocate();
248
249 let values = [
251 f32::MIN_POSITIVE, f32::MIN_POSITIVE / 2.0, f32::MIN_POSITIVE / 4.0, 0.0f32, ];
256
257 for value in values {
258 shape.set_f32(&mut row, 0, value);
259 let retrieved = shape.get_f32(&row, 0);
260 if value == 0.0 {
261 assert_eq!(retrieved, 0.0);
262 } else {
263 assert_eq!(retrieved.to_bits(), value.to_bits());
266 }
267 }
268 }
269
270 pub fn create_unaligned_layout(target_type: ValueType) -> RowShape {
272 RowShape::testing(&[
274 ValueType::Int1, target_type.clone(), ValueType::Int1, target_type, ])
280 }
281}