//==---- math_internal.hpp -----------------------*- C++ -*----------------==// // // Copyright (C) Intel Corporation // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // See https://llvm.org/LICENSE.txt for license information. // //===---------------------------------------------------------------------===// #ifndef __DPCT_DETAIL_MATH_DETAIL_HPP__ #define __DPCT_DETAIL_MATH_DETAIL_HPP__ namespace dpct { // forward declar template inline T clamp(T val, T min_val, T max_val); namespace detail { template class vectorized_binary { public: inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op) { VecT v4; for (size_t i = 0; i < v4.size(); ++i) { v4[i] = binary_op(a[i], b[i]); } return v4; } }; template class vectorized_binary< VecT, BinaryOperation, std::void_t>> { public: inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op) { return binary_op(a, b).template as(); } }; template inline bool isnan(const T a) { return sycl::isnan(a); } #ifdef SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS inline bool isnan(const sycl::ext::oneapi::bfloat16 a) { return sycl::ext::oneapi::experimental::isnan(a); } #endif template constexpr bool is_floating_point = std::disjunction_v, std::is_same #ifdef SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS , std::is_same #endif >; struct shift_left { template auto operator()(const T x, const uint32_t offset) const { return x << offset; } }; struct shift_right { template auto operator()(const T x, const uint32_t offset) const { return x >> offset; } }; struct average { template auto operator()(const T x, const T y) const { return (x + y + (x + y >= 0)) >> 1; } }; /// Extend the 'val' to 'bit' size, zero extend for unsigned int and signed /// extend for signed int. template inline auto zero_or_signed_extent(T val, unsigned bit) { if constexpr (std::is_signed_v) { if constexpr (std::is_same_v) { assert(bit < 64 && "When extend int32 value, bit must be smaller than 64."); return int64_t(val) << (64 - bit) >> (64 - bit); } else if constexpr (std::is_same_v) { assert(bit < 32 && "When extend int16 value, bit must be smaller than 32."); return int32_t(val) << (32 - bit) >> (32 - bit); } else if constexpr (std::is_same_v) { assert(bit < 16 && "When extend int8 value, bit must be smaller than 16."); return int16_t(val) << (16 - bit) >> (16 - bit); } else { assert(bit < 64 && "Cannot extend int64 value."); return val; } } else return val; } template inline constexpr std::enable_if_t< std::is_integral_v && std::is_integral_v && std::is_integral_v && sizeof(AT) == 4 && sizeof(BT) == 4 && sizeof(RetT) == 4, RetT> extend_binary(AT a, BT b, BinaryOperation binary_op) { int64_t extend_a = zero_or_signed_extent(a, 33); int64_t extend_b = zero_or_signed_extent(b, 33); int64_t ret = binary_op(extend_a, extend_b); if constexpr (NeedSat) return dpct::clamp(ret, std::numeric_limits::min(), std::numeric_limits::max()); return ret; } template inline constexpr std::enable_if_t< std::is_integral_v && std::is_integral_v && std::is_integral_v && std::is_integral_v && sizeof(AT) == 4 && sizeof(BT) == 4 && sizeof(CT) == 4 && sizeof(RetT) == 4, RetT> extend_binary(AT a, BT b, CT c, BinaryOperation1 binary_op, BinaryOperation2 second_op) { int64_t extend_a = zero_or_signed_extent(a, 33); int64_t extend_b = zero_or_signed_extent(b, 33); int64_t extend_temp = zero_or_signed_extent(binary_op(extend_a, extend_b), 34); if constexpr (NeedSat) extend_temp = dpct::clamp(extend_temp, std::numeric_limits::min(), std::numeric_limits::max()); int64_t extend_c = zero_or_signed_extent(c, 33); return second_op(extend_temp, extend_c); } template sycl::vec extractAndExtend2(T a) { sycl::vec ret; sycl::vec va{a}; if constexpr (std::is_signed_v) { auto v = va.template as>(); ret[0] = zero_or_signed_extent(v[0], 17); ret[1] = zero_or_signed_extent(v[1], 17); } else { auto v = va.template as>(); ret[0] = zero_or_signed_extent(v[0], 17); ret[1] = zero_or_signed_extent(v[1], 17); } return ret; } template sycl::vec extractAndExtend4(T a) { sycl::vec ret; sycl::vec va{a}; if constexpr (std::is_signed_v) { auto v = va.template as>(); ret[0] = zero_or_signed_extent(v[0], 9); ret[1] = zero_or_signed_extent(v[1], 9); ret[2] = zero_or_signed_extent(v[2], 9); ret[3] = zero_or_signed_extent(v[3], 9); } else { auto v = va.template as>(); ret[0] = zero_or_signed_extent(v[0], 9); ret[1] = zero_or_signed_extent(v[1], 9); ret[2] = zero_or_signed_extent(v[2], 9); ret[3] = zero_or_signed_extent(v[3], 9); } return ret; } template inline constexpr std::enable_if_t< std::is_integral_v && std::is_integral_v && std::is_integral_v && sizeof(AT) == 4 && sizeof(BT) == 4 && sizeof(RetT) == 4, RetT> extend_vbinary2(AT a, BT b, RetT c, BinaryOperation binary_op) { sycl::vec extend_a = extractAndExtend2(a); sycl::vec extend_b = extractAndExtend2(b); sycl::vec temp{binary_op(extend_a[0], extend_b[0]), binary_op(extend_a[1], extend_b[1])}; if constexpr (NeedSat) { int32_t min_val = 0, max_val = 0; if constexpr (std::is_signed_v) { min_val = std::numeric_limits::min(); max_val = std::numeric_limits::max(); } else { min_val = std::numeric_limits::min(); max_val = std::numeric_limits::max(); } temp = dpct::clamp(temp, {min_val, min_val}, {max_val, max_val}); } if constexpr (NeedAdd) { return temp[0] + temp[1] + c; } if constexpr (std::is_signed_v) { return sycl::vec{temp[0], temp[1]}.as>(); } else { return sycl::vec{temp[0], temp[1]}.as>(); } } template inline constexpr std::enable_if_t< std::is_integral_v && std::is_integral_v && std::is_integral_v && sizeof(AT) == 4 && sizeof(BT) == 4 && sizeof(RetT) == 4, RetT> extend_vbinary4(AT a, BT b, RetT c, BinaryOperation binary_op) { sycl::vec extend_a = extractAndExtend4(a); sycl::vec extend_b = extractAndExtend4(b); sycl::vec temp{ binary_op(extend_a[0], extend_b[0]), binary_op(extend_a[1], extend_b[1]), binary_op(extend_a[2], extend_b[2]), binary_op(extend_a[3], extend_b[3])}; if constexpr (NeedSat) { int16_t min_val = 0, max_val = 0; if constexpr (std::is_signed_v) { min_val = std::numeric_limits::min(); max_val = std::numeric_limits::max(); } else { min_val = std::numeric_limits::min(); max_val = std::numeric_limits::max(); } temp = dpct::clamp(temp, {min_val, min_val, min_val, min_val}, {max_val, max_val, max_val, max_val}); } if constexpr (NeedAdd) { return temp[0] + temp[1] + temp[2] + temp[3] + c; } if constexpr (std::is_signed_v) { return sycl::vec{temp[0], temp[1], temp[2], temp[3]} .as>(); } else { return sycl::vec{temp[0], temp[1], temp[2], temp[3]} .as>(); } } template using dot_product_acc_t = std::conditional_t && std::is_unsigned_v, uint32_t, int32_t>; template sycl::vec extract_and_sign_or_zero_extend4(T val) { return sycl::vec(val) .template as, int8_t, uint8_t>, 4>>() .template convert(); } template sycl::vec extract_and_sign_or_zero_extend2(T val) { return sycl::vec(val) .template as, int16_t, uint16_t>, 2>>() .template convert(); } } // namespace detail } // namespace dpct #endif //!__DPCT_DETAIL_MATH_DETAIL_HPP__