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