Skip to main content

reifydb_core/encoded/
f32.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use std::{f32, ptr};
5
6use reifydb_type::value::r#type::Type;
7
8use crate::encoded::{encoded::EncodedValues, schema::Schema};
9
10impl Schema {
11	pub fn set_f32(&self, row: &mut EncodedValues, index: usize, value: impl Into<f32>) {
12		let field = &self.fields()[index];
13		debug_assert!(row.len() >= self.total_static_size());
14		debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Float4);
15		row.set_valid(index, true);
16		unsafe {
17			ptr::write_unaligned(
18				row.make_mut().as_mut_ptr().add(field.offset as usize) as *mut f32,
19				value.into(),
20			)
21		}
22	}
23
24	pub fn get_f32(&self, row: &EncodedValues, index: usize) -> f32 {
25		let field = &self.fields()[index];
26		debug_assert!(row.len() >= self.total_static_size());
27		debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Float4);
28		unsafe { (row.as_ptr().add(field.offset as usize) as *const f32).read_unaligned() }
29	}
30
31	pub fn try_get_f32(&self, row: &EncodedValues, index: usize) -> Option<f32> {
32		if row.is_defined(index) && self.fields()[index].constraint.get_type() == Type::Float4 {
33			Some(self.get_f32(row, index))
34		} else {
35			None
36		}
37	}
38}
39
40#[cfg(test)]
41#[allow(clippy::approx_constant)]
42pub mod tests {
43	use std::f32::consts::{E, PI};
44
45	use reifydb_type::value::r#type::Type;
46
47	use crate::encoded::schema::Schema;
48
49	#[test]
50	fn test_set_get_f32() {
51		let schema = Schema::testing(&[Type::Float4]);
52		let mut row = schema.allocate();
53		schema.set_f32(&mut row, 0, 1.25f32);
54		assert_eq!(schema.get_f32(&row, 0), 1.25f32);
55	}
56
57	#[test]
58	fn test_try_get_f32() {
59		let schema = Schema::testing(&[Type::Float4]);
60		let mut row = schema.allocate();
61
62		assert_eq!(schema.try_get_f32(&row, 0), None);
63
64		schema.set_f32(&mut row, 0, 1.25f32);
65		assert_eq!(schema.try_get_f32(&row, 0), Some(1.25f32));
66	}
67
68	#[test]
69	fn test_special_values() {
70		let schema = Schema::testing(&[Type::Float4]);
71		let mut row = schema.allocate();
72
73		// Test zero
74		schema.set_f32(&mut row, 0, 0.0f32);
75		assert_eq!(schema.get_f32(&row, 0), 0.0f32);
76
77		// Test negative zero
78		let mut row2 = schema.allocate();
79		schema.set_f32(&mut row2, 0, -0.0f32);
80		assert_eq!(schema.get_f32(&row2, 0), -0.0f32);
81
82		// Test infinity
83		let mut row3 = schema.allocate();
84		schema.set_f32(&mut row3, 0, f32::INFINITY);
85		assert_eq!(schema.get_f32(&row3, 0), f32::INFINITY);
86
87		// Test negative infinity
88		let mut row4 = schema.allocate();
89		schema.set_f32(&mut row4, 0, f32::NEG_INFINITY);
90		assert_eq!(schema.get_f32(&row4, 0), f32::NEG_INFINITY);
91
92		// Test NaN
93		let mut row5 = schema.allocate();
94		schema.set_f32(&mut row5, 0, f32::NAN);
95		assert!(schema.get_f32(&row5, 0).is_nan());
96	}
97
98	#[test]
99	fn test_extreme_values() {
100		let schema = Schema::testing(&[Type::Float4]);
101		let mut row = schema.allocate();
102
103		schema.set_f32(&mut row, 0, f32::MAX);
104		assert_eq!(schema.get_f32(&row, 0), f32::MAX);
105
106		let mut row2 = schema.allocate();
107		schema.set_f32(&mut row2, 0, f32::MIN);
108		assert_eq!(schema.get_f32(&row2, 0), f32::MIN);
109
110		let mut row3 = schema.allocate();
111		schema.set_f32(&mut row3, 0, f32::MIN_POSITIVE);
112		assert_eq!(schema.get_f32(&row3, 0), f32::MIN_POSITIVE);
113	}
114
115	#[test]
116	fn test_mixed_with_other_types() {
117		let schema = Schema::testing(&[Type::Float4, Type::Int4, Type::Float4]);
118		let mut row = schema.allocate();
119
120		schema.set_f32(&mut row, 0, 3.14f32);
121		schema.set_i32(&mut row, 1, 42);
122		schema.set_f32(&mut row, 2, -2.718f32);
123
124		assert_eq!(schema.get_f32(&row, 0), 3.14f32);
125		assert_eq!(schema.get_i32(&row, 1), 42);
126		assert_eq!(schema.get_f32(&row, 2), -2.718f32);
127	}
128
129	#[test]
130	fn test_undefined_handling() {
131		let schema = Schema::testing(&[Type::Float4, Type::Float4]);
132		let mut row = schema.allocate();
133
134		schema.set_f32(&mut row, 0, 3.14f32);
135
136		assert_eq!(schema.try_get_f32(&row, 0), Some(3.14f32));
137		assert_eq!(schema.try_get_f32(&row, 1), None);
138
139		schema.set_none(&mut row, 0);
140		assert_eq!(schema.try_get_f32(&row, 0), None);
141	}
142
143	#[test]
144	fn test_try_get_f32_wrong_type() {
145		let schema = Schema::testing(&[Type::Boolean]);
146		let mut row = schema.allocate();
147
148		schema.set_bool(&mut row, 0, true);
149
150		assert_eq!(schema.try_get_f32(&row, 0), None);
151	}
152
153	#[test]
154	fn test_subnormal_values() {
155		let schema = Schema::testing(&[Type::Float4]);
156		let mut row = schema.allocate();
157
158		// Test smallest positive subnormal
159		let min_subnormal = f32::from_bits(0x00000001);
160		schema.set_f32(&mut row, 0, min_subnormal);
161		assert_eq!(schema.get_f32(&row, 0).to_bits(), min_subnormal.to_bits());
162
163		// Test largest subnormal (just below MIN_POSITIVE)
164		let max_subnormal = f32::from_bits(0x007fffff);
165		schema.set_f32(&mut row, 0, max_subnormal);
166		assert_eq!(schema.get_f32(&row, 0).to_bits(), max_subnormal.to_bits());
167
168		// Test negative subnormals
169		let neg_subnormal = f32::from_bits(0x80000001);
170		schema.set_f32(&mut row, 0, neg_subnormal);
171		assert_eq!(schema.get_f32(&row, 0).to_bits(), neg_subnormal.to_bits());
172	}
173
174	#[test]
175	fn test_nan_payload_preservation() {
176		let schema = Schema::testing(&[Type::Float4]);
177		let mut row = schema.allocate();
178
179		// Test different NaN representations
180		let quiet_nan = f32::NAN;
181		schema.set_f32(&mut row, 0, quiet_nan);
182		assert!(schema.get_f32(&row, 0).is_nan());
183
184		// Test NaN with specific payload
185		let nan_with_payload = f32::from_bits(0x7fc00001);
186		schema.set_f32(&mut row, 0, nan_with_payload);
187		assert_eq!(schema.get_f32(&row, 0).to_bits(), nan_with_payload.to_bits());
188
189		// Test negative NaN
190		let neg_nan = f32::from_bits(0xffc00000);
191		schema.set_f32(&mut row, 0, neg_nan);
192		assert_eq!(schema.get_f32(&row, 0).to_bits(), neg_nan.to_bits());
193	}
194
195	#[test]
196	fn test_repeated_operations() {
197		let schema = Schema::testing(&[Type::Float4]);
198		let mut row = schema.allocate();
199		let initial_len = row.len();
200
201		// Set same field many times with different values
202		for i in 0..1000 {
203			let value = (i as f32) * 0.1;
204			schema.set_f32(&mut row, 0, value);
205			assert_eq!(schema.get_f32(&row, 0), value);
206		}
207
208		// Size shouldn't grow for static type
209		assert_eq!(row.len(), initial_len);
210	}
211
212	#[test]
213	fn test_unaligned_access() {
214		let schema = create_unaligned_layout(Type::Float4);
215		let mut row = schema.allocate();
216
217		// Test at odd offset (index 1)
218		schema.set_f32(&mut row, 1, PI);
219		assert_eq!(schema.get_f32(&row, 1), PI);
220
221		// Test at another odd offset (index 3)
222		schema.set_f32(&mut row, 3, E);
223		assert_eq!(schema.get_f32(&row, 3), E);
224
225		// Verify both values are preserved
226		assert_eq!(schema.get_f32(&row, 1), PI);
227		assert_eq!(schema.get_f32(&row, 3), E);
228	}
229
230	#[test]
231	fn test_denormalized_transitions() {
232		let schema = Schema::testing(&[Type::Float4]);
233		let mut row = schema.allocate();
234
235		// Test transition from normal to subnormal
236		let values = [
237			f32::MIN_POSITIVE,       // Smallest normal
238			f32::MIN_POSITIVE / 2.0, // Becomes subnormal
239			f32::MIN_POSITIVE / 4.0, // Smaller subnormal
240			0.0f32,                  // Underflows to zero
241		];
242
243		for value in values {
244			schema.set_f32(&mut row, 0, value);
245			let retrieved = schema.get_f32(&row, 0);
246			if value == 0.0 {
247				assert_eq!(retrieved, 0.0);
248			} else {
249				// For subnormals, compare bits to ensure exact
250				// preservation
251				assert_eq!(retrieved.to_bits(), value.to_bits());
252			}
253		}
254	}
255
256	/// Creates a layout with odd alignment to test unaligned access
257	pub fn create_unaligned_layout(target_type: Type) -> Schema {
258		// Use Int1 (1 byte) to create odd alignment
259		Schema::testing(&[
260			Type::Int1,          // 1 byte offset
261			target_type.clone(), // Now at odd offset
262			Type::Int1,          // Another odd-sized field
263			target_type,         /* Another instance at different odd
264			                      * offset */
265		])
266	}
267}