/*******************************************************
* Copyright (c) 2025, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include "half.hpp" //note: NOT common. From extern/half/include/half.hpp
#include
#include
using namespace std;
using namespace af;
using half_float_half = half_float::half;
const int num = 10000;
#define add(left, right) (left) + (right)
#define sub(left, right) (left) - (right)
#define mul(left, right) (left) * (right)
#define div(left, right) (left) / (right)
typedef std::complex complex_float;
typedef std::complex complex_double;
template
T mod(T a, T b) {
return std::fmod(a, b);
}
template
T rem(T x, T y) {
return remainder(x, y);
}
af::array randgen(const int num, dtype ty) {
af::array tmp = round(1 + 2 * af::randu(num, f32)).as(ty);
tmp.eval();
return tmp;
}
#define MY_ASSERT_NEAR(aa, bb, cc) ASSERT_NEAR(abs(aa), abs(bb), (cc))
#define BINARY_TESTS(Ta, Tb, Tc, func) \
TEST(BinaryTests, Test_##func##_##Ta##_##Tb) { \
SUPPORTED_TYPE_CHECK(Ta); \
SUPPORTED_TYPE_CHECK(Tb); \
SUPPORTED_TYPE_CHECK(Tc); \
\
af_dtype ta = (af_dtype)dtype_traits::af_type; \
af_dtype tb = (af_dtype)dtype_traits::af_type; \
af::array a = randgen(num, ta); \
af::array b = randgen(num, tb); \
af::array c = func(a, b); \
Ta *h_a = a.host(); \
Tb *h_b = b.host(); \
vector gold(num); \
for (int i = 0; i < num; i++) { gold[i] = func(h_a[i], h_b[i]); } \
ASSERT_VEC_ARRAY_EQ(gold, dim4(num), c); \
af_free_host(h_a); \
af_free_host(h_b); \
} \
\
TEST(BinaryTests, Test_##func##_##Ta##_##Tb##_left) { \
SUPPORTED_TYPE_CHECK(Ta); \
SUPPORTED_TYPE_CHECK(Tb); \
\
af_dtype ta = (af_dtype)dtype_traits::af_type; \
af::array a = randgen(num, ta); \
Tb h_b = 3.0; \
af::array c = func(a, h_b); \
Ta *h_a = a.host(); \
vector gold(num); \
for (int i = 0; i < num; i++) { gold[i] = func(h_a[i], h_b); } \
ASSERT_VEC_ARRAY_EQ(gold, dim4(num), c); \
af_free_host(h_a); \
} \
\
TEST(BinaryTests, Test_##func##_##Ta##_##Tb##_right) { \
SUPPORTED_TYPE_CHECK(Ta); \
SUPPORTED_TYPE_CHECK(Tb); \
\
af_dtype tb = (af_dtype)dtype_traits::af_type; \
Ta h_a = 5.0; \
af::array b = randgen(num, tb); \
af::array c = func(h_a, b); \
Tb *h_b = b.host(); \
vector gold(num); \
for (int i = 0; i < num; i++) { gold[i] = func(h_a, h_b[i]); } \
ASSERT_VEC_ARRAY_EQ(gold, dim4(num), c); \
af_free_host(h_b); \
}
#define BINARY_TESTS_NEAR_GENERAL(Ta, Tb, Tc, Td, Te, func, err) \
TEST(BinaryTestsFloating, Test_##func##_##Ta##_##Tb) { \
SUPPORTED_TYPE_CHECK(Ta); \
SUPPORTED_TYPE_CHECK(Tb); \
SUPPORTED_TYPE_CHECK(Tc); \
\
af_dtype ta = (af_dtype)dtype_traits::af_type; \
af_dtype tb = (af_dtype)dtype_traits::af_type; \
af::array a = randgen(num, ta); \
af::array b = randgen(num, tb); \
af::array c = func(a, b); \
Ta *h_a = a.host(); \
Tb *h_b = b.host(); \
Tc *h_c = c.host(); \
for (int i = 0; i < num; i++) \
MY_ASSERT_NEAR(h_c[i], func(h_a[i], h_b[i]), (err)) \