/* * Copyright (c) Advanced Micro Devices, Inc., or its affiliates. * * SPDX-License-Identifier: MIT */ #include "half_precision_common.hh" /** * @addtogroup HalfPrecisionComparison HalfPrecisionComparison * @{ * @ingroup MathTest */ /********** Unary Functions **********/ #define MATH_BOOL_UNARY_HP_TEST_DEF(func_name, ref_func) \ __global__ void func_name##_kernel(bool* const ys, const size_t num_xs, Float16* const xs) { \ const auto tid = cg::this_grid().thread_rank(); \ const auto stride = cg::this_grid().size(); \ \ for (size_t i = tid; i < num_xs; i += stride) { \ ys[i] = func_name(xs[i]); \ } \ } \ \ HIP_TEST_CASE(Unit_Device_##func_name##_Accuracy_Positive) { \ UnaryHalfPrecisionTest(func_name##_kernel, ref_func, EqValidatorBuilderFactory()); \ } /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hisinf(x)` for all possible inputs. The results are * compared against reference function `bool std::isinf(float)`. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_BOOL_UNARY_HP_TEST_DEF(__hisinf, static_cast(std::isinf)) static float __hisinf2_ref(float x) { return static_cast(std::isinf(x)); } MATH_UNARY_HP_KERNEL_DEF(__hisinf2) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hisinf2(x)` for all possible inputs. The results are * compared against reference function `float std::isinf(float)`. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_UNARY_HP_TEST_DEF_IMPL(__hisinf2, __hisinf2_ref, EqValidatorBuilderFactory()); /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hisnan(x)` for all possible inputs. The results are * compared against reference function `bool std::isnan(float)`. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_BOOL_UNARY_HP_TEST_DEF(__hisnan, static_cast(std::isnan)) static float __hisnan2_ref(float x) { return static_cast(std::isnan(x)); } MATH_UNARY_HP_KERNEL_DEF(__hisnan2) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hisnan2(x)` for all possible inputs. The results are * compared against reference function `float std::isnan(float)`. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_UNARY_HP_TEST_DEF_IMPL(__hisnan2, __hisnan2_ref, EqValidatorBuilderFactory()); /********** Binary Functions **********/ #define MATH_COMPARISON_HP_TEST_DEF(func_name, ref_func, T, RT, nan_value) \ __global__ void func_name##_kernel(T* const ys, const size_t num_xs, Float16* const x1s, \ Float16* const x2s) { \ const auto tid = cg::this_grid().thread_rank(); \ const auto stride = cg::this_grid().size(); \ \ for (size_t i = tid; i < num_xs; i += stride) { \ ys[i] = func_name(x1s[i], x2s[i]); \ } \ } \ \ HIP_TEST_CASE(Unit_Device_##func_name##_Accuracy_Positive) { \ BinaryFloatingPointTest(func_name##_kernel, ref_func, \ EqValidatorBuilderFactory()); \ } template static T __heq_ref(float x1, float x2) { if (std::isnan(x1) || std::isnan(x2)) { return static_cast(nan_value); } return x1 == x2; } /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__heq(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'equal * to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__heq, __heq_ref, bool, bool, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hbeq2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'equal * to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hbeq2, __heq_ref, bool, bool, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hequ(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'equal * to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hequ, __heq_ref, bool, bool, true) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hbequ2(x,y)` against a table of difficult values, * followed by a large number of randomly generated values. The results are compared against result * of 'equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hbequ2, __heq_ref, bool, bool, true) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__heq2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'equal * to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__heq2, __heq_ref, Float16, float, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hequ2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'equal * to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hequ2, __heq_ref, Float16, float, true) template static T __hne_ref(float x1, float x2) { if (std::isnan(x1) || std::isnan(x2)) { return static_cast(nan_value); } return x1 != x2; } /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hne(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'not * equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hne, __hne_ref, bool, bool, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hbne2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'not * equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hbne2, __hne_ref, bool, bool, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hneu(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'not * equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hneu, __hne_ref, bool, bool, true) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hbneu2(x,y)` against a table of difficult values, * followed by a large number of randomly generated values. The results are compared against result * of 'not equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hbneu2, __hne_ref, bool, bool, true) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hne2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'not * equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hne2, __hne_ref, Float16, float, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hneu2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'not * equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hneu2, __hne_ref, Float16, float, true) template static T __hge_ref(float x1, float x2) { if (std::isnan(x1) || std::isnan(x2)) { return static_cast(nan_value); } return x1 >= x2; } /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hge(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of * 'greater than equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hge, __hge_ref, bool, bool, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hbge2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of * 'greater than equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hbge2, __hge_ref, bool, bool, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hgeu(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of * 'greater than equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hgeu, __hge_ref, bool, bool, true) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hbgeu2(x,y)` against a table of difficult values, * followed by a large number of randomly generated values. The results are compared against result * of 'greater than equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hbgeu2, __hge_ref, bool, bool, true) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hge2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of * 'greater than equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hge2, __hge_ref, Float16, float, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hgeu2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of * 'greater than equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hgeu2, __hge_ref, Float16, float, true) template static T __hgt_ref(float x1, float x2) { if (std::isnan(x1) || std::isnan(x2)) { return static_cast(nan_value); } return x1 > x2; } /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hgt(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of * 'greater than' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hgt, __hgt_ref, bool, bool, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hbgt2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of * 'greater than' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hbgt2, __hgt_ref, bool, bool, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hgtu(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of * 'greater than' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hgtu, __hgt_ref, bool, bool, true) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hbgtu2(x,y)` against a table of difficult values, * followed by a large number of randomly generated values. The results are compared against result * of 'greater than' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hbgtu2, __hgt_ref, bool, bool, true) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hgt2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of * 'greater than' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hgt2, __hgt_ref, Float16, float, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hgtu2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of * 'greater than' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hgtu2, __hgt_ref, Float16, float, true) template static T __hle_ref(float x1, float x2) { if (std::isnan(x1) || std::isnan(x2)) { return static_cast(nan_value); } return x1 <= x2; } /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hle(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'less * than equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hle, __hle_ref, bool, bool, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hble2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'less * than equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hble2, __hle_ref, bool, bool, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hleu(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'less * than equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hleu, __hle_ref, bool, bool, true) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hbleu2(x,y)` against a table of difficult values, * followed by a large number of randomly generated values. The results are compared against result * of 'less than equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hbleu2, __hle_ref, bool, bool, true) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hle2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'less * than equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hle2, __hle_ref, Float16, float, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hleu2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'less * than equal to' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hleu2, __hle_ref, Float16, float, true) template static T __hlt_ref(float x1, float x2) { if (std::isnan(x1) || std::isnan(x2)) { return static_cast(nan_value); } return x1 < x2; } /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hlt(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'less * than' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hlt, __hlt_ref, bool, bool, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hblt2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'less * than' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hblt2, __hlt_ref, bool, bool, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hltu(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'less * than' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hltu, __hlt_ref, bool, bool, true) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hbltu2(x,y)` against a table of difficult values, * followed by a large number of randomly generated values. The results are compared against result * of 'less than' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hbltu2, __hlt_ref, bool, bool, true) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hlt2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'less * than' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hlt2, __hlt_ref, Float16, float, false) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hltu2(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against result of 'less * than' relational operator for float operands. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_COMPARISON_HP_TEST_DEF(__hltu2, __hlt_ref, Float16, float, true) MATH_BINARY_HP_KERNEL_DEF(__hmax) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hmax(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against reference * function `float std::fmax(float, float)` * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_BINARY_HP_TEST_DEF_IMPL(__hmax, static_cast(std::fmax), EqValidatorBuilderFactory()) MATH_BINARY_HP_KERNEL_DEF(__hmin) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hmin(x,y)` against a table of difficult values, followed * by a large number of randomly generated values. The results are compared against reference * function `float std::fmin(float, float)` * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_BINARY_HP_TEST_DEF_IMPL(__hmin, static_cast(std::fmin), EqValidatorBuilderFactory()) static float __hmax_nan_ref(float x1, float x2) { if (std::isnan(x1)) return x1; else if (std::isnan(x2)) return x2; else return std::fmax(x1, x2); } MATH_BINARY_HP_KERNEL_DEF(__hmax_nan) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hmax_nan(x,y)` against a table of difficult values, * followed by a large number of randomly generated values. The results are compared against * reference function `float std::fmax(float, float)` with modified result when an operand is nan. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_BINARY_HP_TEST_DEF_IMPL(__hmax_nan, __hmax_nan_ref, EqValidatorBuilderFactory()) static float __hmin_nan_ref(float x1, float x2) { if (std::isnan(x1)) return x1; else if (std::isnan(x2)) return x2; else return std::fmin(x1, x2); } MATH_BINARY_HP_KERNEL_DEF(__hmin_nan) /** * Test Description * ------------------------ * - Tests the numerical accuracy of `__hmin_nan(x,y)` against a table of difficult values, * followed by a large number of randomly generated values. The results are compared against * reference function `float std::fmin(float, float)` with modified result when an operand is nan. * * Test source * ------------------------ * - unit/math/half_precision_comparison.cc * Test requirements * ------------------------ * - HIP_VERSION >= 5.2 */ MATH_BINARY_HP_TEST_DEF_IMPL(__hmin_nan, __hmin_nan_ref, EqValidatorBuilderFactory()) /** * End doxygen group MathTest. * @} */