// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // #include #include #include "inc/Test.h" #include "inc/Core/Common/SIMDUtils.h" template static void ComputeSum(T *pX, const T *pY, SPTAG::DimensionType length) { const T* pEnd1 = pX + length; while (pX < pEnd1) { *pX++ += *pY++; } } template T random(int high = RAND_MAX, int low = 0) // Generates a random value. { return (T)(low + float(high - low)*(std::rand()/static_cast(RAND_MAX + 1.0))); } template void test(int high) { SPTAG::DimensionType dimension = random(256, 2); T *X = new T[dimension], *Y = new T[dimension]; BOOST_ASSERT(X != nullptr && Y != nullptr); for (SPTAG::DimensionType i = 0; i < dimension; i++) { X[i] = random(high, -high); Y[i] = random(high, -high); } T *X_copy = new T[dimension]; for (SPTAG::DimensionType i = 0; i < dimension; i++) { X_copy[i] = X[i]; } ComputeSum(X, Y, dimension); SPTAG::COMMON::SIMDUtils::ComputeSum(X_copy, Y, dimension); for (SPTAG::DimensionType i = 0; i < dimension; i++) { BOOST_CHECK_CLOSE_FRACTION(double(X[i]), double(X_copy[i]), 1e-5); } delete[] X; delete[] Y; delete[] X_copy; } BOOST_AUTO_TEST_SUITE(SIMDTest) BOOST_AUTO_TEST_CASE(TestDistanceComputation) { test(1); test(127); test(32767); } BOOST_AUTO_TEST_SUITE_END()