@@ -154,28 +154,6 @@ class FloatVectorOperationsTests : public ::testing::Test
154154 FloatVectorOperations::fill (data1, (ValueType) 18 , num);
155155 FloatVectorOperations::divide (data2, data1, (ValueType) 6 , num);
156156 EXPECT_TRUE (areAllValuesEqual (data2, num, (ValueType) 3 ));
157-
158- fillRandomly (random, int1, num);
159- const ValueType multiplier = (ValueType) (1.0 / (1 << 16 ));
160-
161- if constexpr (std::is_same_v<ValueType, float >)
162- {
163- convertFixed (data1, int1, multiplier, num);
164- FloatVectorOperations::convertFixedToFloat (data2, int1, multiplier, num);
165- EXPECT_TRUE (buffersMatch (data1, data2, num));
166-
167- convertFloatToFixed (int1, data1, 1 .0f / multiplier, num);
168- HeapBlock<int > int2 (num + 16 );
169- #if YUP_ARM
170- int * const intData = int2;
171- #else
172- int * const intData = addBytesToPointer (int2.get (), random.nextInt (16 ));
173- #endif
174- FloatVectorOperations::convertFloatToFixed (intData, data1, 1 .0f / multiplier, num);
175-
176- for (int i = 0 ; i < num; ++i)
177- EXPECT_EQ (int1[i], intData[i]);
178- }
179157 }
180158
181159 static void fillRandomly (Random& random, ValueType* d, int num)
@@ -190,30 +168,6 @@ class FloatVectorOperationsTests : public ::testing::Test
190168 *d++ = random.nextInt ();
191169 }
192170
193- static void convertFixed (float * d, const int * s, ValueType multiplier, int num)
194- {
195- while (--num >= 0 )
196- *d++ = (float ) *s++ * multiplier;
197- }
198-
199- static void convertFixedToDouble (double * d, const int * s, double multiplier, int num)
200- {
201- while (--num >= 0 )
202- *d++ = (double ) *s++ * multiplier;
203- }
204-
205- static void convertFloatToFixed (int * d, const float * s, float multiplier, int num)
206- {
207- while (--num >= 0 )
208- *d++ = (int ) (*s++ * multiplier);
209- }
210-
211- static void convertDoubleToFixed (int * d, const double * s, double multiplier, int num)
212- {
213- while (--num >= 0 )
214- *d++ = (int ) (*s++ * multiplier);
215- }
216-
217171 static bool areAllValuesEqual (const ValueType* d, int num, ValueType target)
218172 {
219173 while (--num >= 0 )
@@ -237,6 +191,43 @@ class FloatVectorOperationsTests : public ::testing::Test
237191 return std::abs (v1 - v2) < std::numeric_limits<ValueType>::epsilon ();
238192 }
239193 };
194+
195+ template <class ValueType >
196+ static bool valuesMatch (ValueType v1, ValueType v2)
197+ {
198+ return std::abs (v1 - v2) < std::numeric_limits<ValueType>::epsilon ();
199+ }
200+
201+ template <class ValueType >
202+ static bool buffersMatch (const ValueType* d1, const ValueType* d2, int num)
203+ {
204+ while (--num >= 0 )
205+ {
206+ if (! valuesMatch (*d1++, *d2++))
207+ return false ;
208+ }
209+
210+ return true ;
211+ }
212+
213+ static void convertFixedToFloat (float * d, const int * s, float multiplier, int num)
214+ {
215+ while (--num >= 0 )
216+ *d++ = (float ) *s++ * multiplier;
217+ }
218+
219+ static void convertFloatToFixed (int * d, const float * s, float multiplier, int num)
220+ {
221+ while (--num >= 0 )
222+ *d++ = (int ) (*s++ * multiplier);
223+ }
224+
225+ template <class ValueType >
226+ static void fillRandomly (Random& random, ValueType* d, int num)
227+ {
228+ while (--num >= 0 )
229+ *d++ = (ValueType) (random.nextDouble () * 1000.0 );
230+ }
240231};
241232
242233TEST_F (FloatVectorOperationsTests, BasicOperations)
@@ -247,3 +238,83 @@ TEST_F (FloatVectorOperationsTests, BasicOperations)
247238 TestRunner<double >::runTest (Random::getSystemRandom ());
248239 }
249240}
241+
242+ TEST_F (FloatVectorOperationsTests, FloatToFixedAndBack)
243+ {
244+ Random& random = Random::getSystemRandom ();
245+
246+ for (int i = 1000 ; --i >= 0 ;)
247+ {
248+ const int range = random.nextBool () ? 500 : 10 ;
249+ const int num = random.nextInt (range) + 1 ;
250+
251+ HeapBlock<float > buffer1 (num + 16 ), buffer2 (num + 16 );
252+ HeapBlock<int > buffer3 (num + 16 , true );
253+
254+ #if YUP_ARM
255+ float * const data1 = buffer1;
256+ float * const data2 = buffer2;
257+ int * const int1 = buffer3;
258+ #else
259+ // These tests deliberately operate on misaligned memory and will be flagged up by
260+ // checks for undefined behavior!
261+ float * const data1 = addBytesToPointer (buffer1.get (), random.nextInt (16 ));
262+ float * const data2 = addBytesToPointer (buffer2.get (), random.nextInt (16 ));
263+ int * const int1 = addBytesToPointer (buffer3.get (), random.nextInt (16 ));
264+ #endif
265+
266+ fillRandomly (random, data1, num);
267+ fillRandomly (random, data2, num);
268+
269+ fillRandomly (random, int1, num);
270+ const auto multiplier = (float ) (1.0 / (1 << 16 ));
271+
272+ convertFixedToFloat (data1, int1, multiplier, num);
273+ FloatVectorOperations::convertFixedToFloat (data2, int1, multiplier, num);
274+ EXPECT_TRUE (buffersMatch (data1, data2, num));
275+
276+ convertFloatToFixed (int1, data1, 1 .0f / multiplier, num);
277+ HeapBlock<int > int2 (num + 16 );
278+ #if YUP_ARM
279+ int * const intData = int2;
280+ #else
281+ int * const intData = addBytesToPointer (int2.get (), random.nextInt (16 ));
282+ #endif
283+ FloatVectorOperations::convertFloatToFixed (intData, data1, 1 .0f / multiplier, num);
284+
285+ for (int i = 0 ; i < num; ++i)
286+ EXPECT_EQ (int1[i], intData[i]);
287+ }
288+ }
289+
290+ TEST_F (FloatVectorOperationsTests, FloatToDoubleAndBack)
291+ {
292+ Random& random = Random::getSystemRandom ();
293+
294+ for (int i = 1000 ; --i >= 0 ;)
295+ {
296+ const int range = random.nextBool () ? 500 : 10 ;
297+ const int num = random.nextInt (range) + 1 ;
298+
299+ HeapBlock<float > floatBuffer (num + 16 );
300+ HeapBlock<double > doubleBuffer (num + 16 );
301+
302+ #if YUP_ARM
303+ float * const floatData = floatBuffer;
304+ double * const doubleData = doubleBuffer;
305+ #else
306+ float * const floatData = addBytesToPointer (floatBuffer.get (), random.nextInt (16 ));
307+ double * const doubleData = addBytesToPointer (doubleBuffer.get (), random.nextInt (16 ));
308+ #endif
309+
310+ fillRandomly (random, floatData, num);
311+ FloatVectorOperations::convertFloatToDouble (doubleData, floatData, num);
312+ for (int i = 0 ; i < num; ++i)
313+ EXPECT_NEAR ((float ) doubleData[i], (float ) floatData[i], std::numeric_limits<float >::epsilon ());
314+
315+ fillRandomly (random, doubleData, num);
316+ FloatVectorOperations::convertDoubleToFloat (floatData, doubleData, num);
317+ for (int i = 0 ; i < num; ++i)
318+ EXPECT_NEAR ((float ) floatData[i], (float ) doubleData[i], std::numeric_limits<float >::epsilon ());
319+ }
320+ }
0 commit comments