/* * Copyright (c) 2017 ARM Limited. * * SPDX-License-Identifier: MIT * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #ifndef __ARM_COMPUTE_TEST_VALIDATION_FIXEDPOINT_H__ #define __ARM_COMPUTE_TEST_VALIDATION_FIXEDPOINT_H__ #include "Utils.h" #include "support/ToolchainSupport.h" #include #include #include #include #include #include namespace arm_compute { namespace test { namespace fixed_point_arithmetic { namespace detail { // Forward declare structs struct functions; template struct constant_expr; } /** Fixed point traits */ namespace traits { // Promote types // *INDENT-OFF* // clang-format off template struct promote { }; template <> struct promote { using type = uint16_t; }; template <> struct promote { using type = int16_t; }; template <> struct promote { using type = uint32_t; }; template <> struct promote { using type = int32_t; }; template <> struct promote { using type = uint64_t; }; template <> struct promote { using type = int64_t; }; template <> struct promote { using type = uint64_t; }; template <> struct promote { using type = int64_t; }; // clang-format on // *INDENT-ON* } /** Strongly typed enum class representing the overflow policy */ enum class OverflowPolicy { WRAP, /**< Wrap policy */ SATURATE /**< Saturate policy */ }; /** Strongly typed enum class representing the rounding policy */ enum class RoundingPolicy { TO_ZERO, /**< Round to zero policy */ TO_NEAREST_EVEN /**< Round to nearest even policy */ }; /** Arbitrary fixed-point arithmetic class */ template class fixed_point { public: // Static Checks static_assert(std::is_integral::value, "Type is not an integer"); // Friends friend struct detail::functions; friend struct detail::constant_expr; /** Constructor (from different fixed point type) * * @param[in] val Fixed point * @param[in] p Fixed point precision */ template fixed_point(fixed_point val, uint8_t p) : _value(0), _fixed_point_position(p) { assert(p > 0 && p < std::numeric_limits::digits); T v = 0; if(std::numeric_limits::digits < std::numeric_limits::digits) { val.rescale(p); v = detail::constant_expr::saturate_cast(val.raw()); } else { auto v_cast = static_cast>(val); v_cast.rescale(p); v = v_cast.raw(); } _value = static_cast(v); } /** Constructor (from integer) * * @param[in] val Integer value to be represented as fixed point * @param[in] p Fixed point precision * @param[in] is_raw If true val is a raw fixed point value else an integer */ template ::value>::type> fixed_point(U val, uint8_t p, bool is_raw = false) : _value(val << p), _fixed_point_position(p) { if(is_raw) { _value = val; } } /** Constructor (from float) * * @param[in] val Float value to be represented as fixed point * @param[in] p Fixed point precision */ fixed_point(float val, uint8_t p) : _value(detail::constant_expr::to_fixed(val, p)), _fixed_point_position(p) { assert(p > 0 && p < std::numeric_limits::digits); } /** Constructor (from float string) * * @param[in] str Float string to be represented as fixed point * @param[in] p Fixed point precision */ fixed_point(std::string str, uint8_t p) : _value(detail::constant_expr::to_fixed(support::cpp11::stof(str), p)), _fixed_point_position(p) { assert(p > 0 && p < std::numeric_limits::digits); } /** Default copy constructor */ fixed_point &operator=(const fixed_point &) = default; /** Default move constructor */ fixed_point &operator=(fixed_point &&) = default; /** Default copy assignment operator */ fixed_point(const fixed_point &) = default; /** Default move assignment operator */ fixed_point(fixed_point &&) = default; /** Float conversion operator * * @return Float representation of fixed point */ operator float() const { return detail::constant_expr::to_float(_value, _fixed_point_position); } /** Integer conversion operator * * @return Integer representation of fixed point */ template ::value>::type> operator U() const { return detail::constant_expr::to_int(_value, _fixed_point_position); } /** Convert to different fixed point of different type but same precision * * @note Down-conversion might fail. */ template operator fixed_point() { U val = static_cast(_value); if(std::numeric_limits::digits < std::numeric_limits::digits) { val = detail::constant_expr::saturate_cast(_value); } return fixed_point(val, _fixed_point_position, true); } /** Arithmetic += assignment operator * * @param[in] rhs Fixed point operand * * @return Reference to this fixed point */ template fixed_point &operator+=(const fixed_point &rhs) { fixed_point val(rhs, _fixed_point_position); _value += val.raw(); return *this; } /** Arithmetic -= assignment operator * * @param[in] rhs Fixed point operand * * @return Reference to this fixed point */ template fixed_point &operator-=(const fixed_point &rhs) { fixed_point val(rhs, _fixed_point_position); _value -= val.raw(); return *this; } /** Raw value accessor * * @return Raw fixed point value */ T raw() const { return _value; } /** Precision accessor * * @return Precision of fixed point */ uint8_t precision() const { return _fixed_point_position; } /** Rescale a fixed point to a new precision * * @param[in] p New fixed point precision */ void rescale(uint8_t p) { assert(p > 0 && p < std::numeric_limits::digits); using promoted_T = typename traits::promote::type; promoted_T val = _value; if(p > _fixed_point_position) { val <<= (p - _fixed_point_position); } else if(p < _fixed_point_position) { uint8_t pbar = _fixed_point_position - p; val += (pbar != 0) ? (1 << (pbar - 1)) : 0; val >>= pbar; } _value = detail::constant_expr::saturate_cast(val); _fixed_point_position = p; } private: T _value; /**< Fixed point raw value */ uint8_t _fixed_point_position; /**< Fixed point precision */ }; namespace detail { /** Count the number of leading zero bits in the given value. * * @param[in] value Input value. * * @return Number of leading zero bits. */ template constexpr int clz(T value) { using unsigned_T = typename std::make_unsigned::type; // __builtin_clz is available for int. Need to correct reported number to // match the original type. return __builtin_clz(value) - (32 - std::numeric_limits::digits); } template struct constant_expr { /** Calculate representation of 1 in fixed point given a fixed point precision * * @param[in] p Fixed point precision * * @return Representation of value 1 in fixed point. */ static constexpr T fixed_one(uint8_t p) { return (1 << p); } /** Calculate fixed point precision step given a fixed point precision * * @param[in] p Fixed point precision * * @return Fixed point precision step */ static constexpr float fixed_step(uint8_t p) { return (1.0f / static_cast(1 << p)); } /** Convert a fixed point value to float given its precision. * * @param[in] val Fixed point value * @param[in] p Fixed point precision * * @return Float representation of the fixed point number */ static constexpr float to_float(T val, uint8_t p) { return static_cast(val * fixed_step(p)); } /** Convert a fixed point value to integer given its precision. * * @param[in] val Fixed point value * @param[in] p Fixed point precision * * @return Integer of the fixed point number */ static constexpr T to_int(T val, uint8_t p) { return val >> p; } /** Convert a single precision floating point value to a fixed point representation given its precision. * * @param[in] val Floating point value * @param[in] p Fixed point precision * * @return The raw fixed point representation */ static constexpr T to_fixed(float val, uint8_t p) { return static_cast(saturate_cast(val * fixed_one(p) + ((val >= 0) ? 0.5 : -0.5))); } /** Clamp value between two ranges * * @param[in] val Value to clamp * @param[in] min Minimum value to clamp to * @param[in] max Maximum value to clamp to * * @return clamped value */ static constexpr T clamp(T val, T min, T max) { return std::min(std::max(val, min), max); } /** Saturate given number * * @param[in] val Value to saturate * * @return Saturated value */ template static constexpr T saturate_cast(U val) { return static_cast(std::min(std::max(val, static_cast(std::numeric_limits::min())), static_cast(std::numeric_limits::max()))); } }; struct functions { /** Output stream operator * * @param[in] s Output stream * @param[in] x Fixed point value * * @return Reference output to updated stream */ template static std::basic_ostream &write(std::basic_ostream &s, fixed_point &x) { return s << static_cast(x); } /** Signbit of a fixed point number. * * @param[in] x Fixed point number * * @return True if negative else false. */ template static bool signbit(fixed_point x) { return ((x._value >> std::numeric_limits::digits) != 0); } /** Checks if two fixed point numbers are equal * * @param[in] x First fixed point operand * @param[in] y Second fixed point operand * * @return True if fixed points are equal else false */ template static bool isequal(fixed_point x, fixed_point y) { uint8_t p = std::min(x._fixed_point_position, y._fixed_point_position); x.rescale(p); y.rescale(p); return (x._value == y._value); } /** Checks if two fixed point number are not equal * * @param[in] x First fixed point operand * @param[in] y Second fixed point operand * * @return True if fixed points are not equal else false */ template static bool isnotequal(fixed_point x, fixed_point y) { return !isequal(x, y); } /** Checks if one fixed point is greater than the other * * @param[in] x First fixed point operand * @param[in] y Second fixed point operand * * @return True if fixed point is greater than other */ template static bool isgreater(fixed_point x, fixed_point y) { uint8_t p = std::min(x._fixed_point_position, y._fixed_point_position); x.rescale(p); y.rescale(p); return (x._value > y._value); } /** Checks if one fixed point is greater or equal than the other * * @param[in] x First fixed point operand * @param[in] y Second fixed point operand * * @return True if fixed point is greater or equal than other */ template static bool isgreaterequal(fixed_point x, fixed_point y) { uint8_t p = std::min(x._fixed_point_position, y._fixed_point_position); x.rescale(p); y.rescale(p); return (x._value >= y._value); } /** Checks if one fixed point is less than the other * * @param[in] x First fixed point operand * @param[in] y Second fixed point operand * * @return True if fixed point is less than other */ template static bool isless(fixed_point x, fixed_point y) { uint8_t p = std::min(x._fixed_point_position, y._fixed_point_position); x.rescale(p); y.rescale(p); return (x._value < y._value); } /** Checks if one fixed point is less or equal than the other * * @param[in] x First fixed point operand * @param[in] y Second fixed point operand * * @return True if fixed point is less or equal than other */ template static bool islessequal(fixed_point x, fixed_point y) { uint8_t p = std::min(x._fixed_point_position, y._fixed_point_position); x.rescale(p); y.rescale(p); return (x._value <= y._value); } /** Checks if one fixed point is less or greater than the other * * @param[in] x First fixed point operand * @param[in] y Second fixed point operand * * @return True if fixed point is less or greater than other */ template static bool islessgreater(fixed_point x, fixed_point y) { return isnotequal(x, y); } /** Clamp fixed point to specific range. * * @param[in] x Fixed point operand * @param[in] min Minimum value to clamp to * @param[in] max Maximum value to clamp to * * @return Clamped result */ template static fixed_point clamp(fixed_point x, T min, T max) { return fixed_point(constant_expr::clamp(x._value, min, max), x._fixed_point_position, true); } /** Negate number * * @param[in] x Fixed point operand * * @return Negated fixed point result */ template static fixed_point negate(fixed_point x) { using promoted_T = typename traits::promote::type; promoted_T val = -x._value; if(OP == OverflowPolicy::SATURATE) { val = constant_expr::saturate_cast(val); } return fixed_point(static_cast(val), x._fixed_point_position, true); } /** Perform addition among two fixed point numbers * * @param[in] x First fixed point operand * @param[in] y Second fixed point operand * * @return Result fixed point with precision equal to minimum precision of both operands */ template static fixed_point add(fixed_point x, fixed_point y) { uint8_t p = std::min(x._fixed_point_position, y._fixed_point_position); x.rescale(p); y.rescale(p); if(OP == OverflowPolicy::SATURATE) { using type = typename traits::promote::type; type val = static_cast(x._value) + static_cast(y._value); val = constant_expr::saturate_cast(val); return fixed_point(static_cast(val), p, true); } else { return fixed_point(x._value + y._value, p, true); } } /** Perform subtraction among two fixed point numbers * * @param[in] x First fixed point operand * @param[in] y Second fixed point operand * * @return Result fixed point with precision equal to minimum precision of both operands */ template static fixed_point sub(fixed_point x, fixed_point y) { uint8_t p = std::min(x._fixed_point_position, y._fixed_point_position); x.rescale(p); y.rescale(p); if(OP == OverflowPolicy::SATURATE) { using type = typename traits::promote::type; type val = static_cast(x._value) - static_cast(y._value); val = constant_expr::saturate_cast(val); return fixed_point(static_cast(val), p, true); } else { return fixed_point(x._value - y._value, p, true); } } /** Perform multiplication among two fixed point numbers * * @param[in] x First fixed point operand * @param[in] y Second fixed point operand * * @return Result fixed point with precision equal to minimum precision of both operands */ template static fixed_point mul(fixed_point x, fixed_point y) { using promoted_T = typename traits::promote::type; uint8_t p_min = std::min(x._fixed_point_position, y._fixed_point_position); uint8_t p_max = std::max(x._fixed_point_position, y._fixed_point_position); promoted_T round_factor = (1 << (p_max - 1)); promoted_T val = ((static_cast(x._value) * static_cast(y._value)) + round_factor) >> p_max; if(OP == OverflowPolicy::SATURATE) { val = constant_expr::saturate_cast(val); } return fixed_point(static_cast(val), p_min, true); } /** Perform division among two fixed point numbers * * @param[in] x First fixed point operand * @param[in] y Second fixed point operand * * @return Result fixed point with precision equal to minimum precision of both operands */ template static fixed_point div(fixed_point x, fixed_point y) { using promoted_T = typename traits::promote::type; uint8_t p = std::min(x._fixed_point_position, y._fixed_point_position); promoted_T denom = static_cast(y._value); if(denom != 0) { promoted_T val = (static_cast(x._value) << std::max(x._fixed_point_position, y._fixed_point_position)) / denom; if(OP == OverflowPolicy::SATURATE) { val = constant_expr::saturate_cast(val); } return fixed_point(static_cast(val), p, true); } else { T val = (x._value < 0) ? std::numeric_limits::min() : std::numeric_limits::max(); return fixed_point(val, p, true); } } /** Shift left * * @param[in] x Fixed point operand * @param[in] shift Shift value * * @return Shifted value */ template static fixed_point shift_left(fixed_point x, size_t shift) { using promoted_T = typename traits::promote::type; promoted_T val = static_cast(x._value) << shift; if(OP == OverflowPolicy::SATURATE) { val = constant_expr::saturate_cast(val); } return fixed_point(static_cast(val), x._fixed_point_position, true); } /** Shift right * * @param[in] x Fixed point operand * @param[in] shift Shift value * * @return Shifted value */ template static fixed_point shift_right(fixed_point x, size_t shift) { return fixed_point(x._value >> shift, x._fixed_point_position, true); } /** Calculate absolute value * * @param[in] x Fixed point operand * * @return Absolute value of operand */ template static fixed_point abs(fixed_point x) { using promoted_T = typename traits::promote::type; T val = (x._value < 0) ? constant_expr::saturate_cast(-static_cast(x._value)) : x._value; return fixed_point(val, x._fixed_point_position, true); } /** Calculate the logarithm of a fixed point number * * @param[in] x Fixed point operand * * @return Logarithm value of operand */ template static fixed_point log(fixed_point x) { uint8_t p = x._fixed_point_position; auto const_one = fixed_point(static_cast(1), p); // Logarithm of 1 is zero and logarithm of negative values is not defined in R, so return 0. // Also, log(x) == -log(1/x) for 0 < x < 1. if(isequal(x, const_one) || islessequal(x, fixed_point(static_cast(0), p))) { return fixed_point(static_cast(0), p, true); } else if(isless(x, const_one)) { return mul(log(div(const_one, x)), fixed_point(-1, p)); } // Remove even powers of 2 T shift_val = 31 - __builtin_clz(x._value >> p); x = shift_right(x, shift_val); x = sub(x, const_one); // Constants auto ln2 = fixed_point(0.6931471, p); auto A = fixed_point(1.4384189, p); auto B = fixed_point(-0.67719, p); auto C = fixed_point(0.3218538, p); auto D = fixed_point(-0.0832229, p); // Polynomial expansion auto sum = add(mul(x, D), C); sum = add(mul(x, sum), B); sum = add(mul(x, sum), A); sum = mul(x, sum); return mul(add(sum, fixed_point(static_cast(shift_val), p)), ln2); } /** Calculate the exponential of a fixed point number. * * exp(x) = exp(floor(x)) * exp(x - floor(x)) * = pow(2, floor(x) / ln(2)) * exp(x - floor(x)) * = exp(x - floor(x)) << (floor(x) / ln(2)) * * @param[in] x Fixed point operand * * @return Exponential value of operand */ template static fixed_point exp(fixed_point x) { uint8_t p = x._fixed_point_position; // Constants auto const_one = fixed_point(1, p); auto ln2 = fixed_point(0.6931471, p); auto inv_ln2 = fixed_point(1.442695, p); auto A = fixed_point(0.9978546, p); auto B = fixed_point(0.4994721, p); auto C = fixed_point(0.1763723, p); auto D = fixed_point(0.0435108, p); T scaled_int_part = detail::constant_expr::to_int(mul(x, inv_ln2)._value, p); // Polynomial expansion auto frac_part = sub(x, mul(ln2, fixed_point(scaled_int_part, p))); auto taylor = add(mul(frac_part, D), C); taylor = add(mul(frac_part, taylor), B); taylor = add(mul(frac_part, taylor), A); taylor = mul(frac_part, taylor); taylor = add(taylor, const_one); // Saturate value if(static_cast(clz(taylor.raw())) <= scaled_int_part) { return fixed_point(std::numeric_limits::max(), p, true); } return (scaled_int_part < 0) ? shift_right(taylor, -scaled_int_part) : shift_left(taylor, scaled_int_part); } /** Calculate the inverse square root of a fixed point number * * @param[in] x Fixed point operand * * @return Inverse square root value of operand */ template static fixed_point inv_sqrt(fixed_point x) { const uint8_t p = x._fixed_point_position; int8_t shift = std::numeric_limits::digits - (p + detail::clz(x._value)); shift += std::numeric_limits::is_signed ? 1 : 0; // Use volatile to restrict compiler optimizations on shift as compiler reports maybe-uninitialized error on Android volatile int8_t *shift_ptr = &shift; auto const_three = fixed_point(3, p); auto a = (*shift_ptr < 0) ? shift_left(x, -(shift)) : shift_right(x, shift); fixed_point x2 = a; // We need three iterations to find the result for QS8 and five for QS16 constexpr int num_iterations = std::is_same::value ? 3 : 5; for(int i = 0; i < num_iterations; ++i) { fixed_point three_minus_dx = sub(const_three, mul(a, mul(x2, x2))); x2 = shift_right(mul(x2, three_minus_dx), 1); } return (shift < 0) ? shift_left(x2, (-shift) >> 1) : shift_right(x2, shift >> 1); } /** Calculate the hyperbolic tangent of a fixed point number * * @param[in] x Fixed point operand * * @return Hyperbolic tangent of the operand */ template static fixed_point tanh(fixed_point x) { uint8_t p = x._fixed_point_position; // Constants auto const_one = fixed_point(1, p); auto const_two = fixed_point(2, p); auto exp2x = exp(const_two * x); auto num = exp2x - const_one; auto den = exp2x + const_one; auto tanh = num / den; return tanh; } /** Calculate the a-th power of a fixed point number. * * The power is computed as x^a = e^(log(x) * a) * * @param[in] x Fixed point operand * @param[in] a Fixed point exponent * * @return a-th power of the operand */ template static fixed_point pow(fixed_point x, fixed_point a) { return exp(log(x) * a); } }; template bool operator==(const fixed_point &lhs, const fixed_point &rhs) { return functions::isequal(lhs, rhs); } template bool operator!=(const fixed_point &lhs, const fixed_point &rhs) { return !operator==(lhs, rhs); } template bool operator<(const fixed_point &lhs, const fixed_point &rhs) { return functions::isless(lhs, rhs); } template bool operator>(const fixed_point &lhs, const fixed_point &rhs) { return operator<(rhs, lhs); } template bool operator<=(const fixed_point &lhs, const fixed_point &rhs) { return !operator>(lhs, rhs); } template bool operator>=(const fixed_point &lhs, const fixed_point &rhs) { return !operator<(lhs, rhs); } template fixed_point operator+(const fixed_point &lhs, const fixed_point &rhs) { return functions::add(lhs, rhs); } template fixed_point operator-(const fixed_point &lhs, const fixed_point &rhs) { return functions::sub(lhs, rhs); } template fixed_point operator-(const fixed_point &rhs) { return functions::negate(rhs); } template fixed_point operator*(fixed_point x, fixed_point y) { return functions::mul(x, y); } template fixed_point operator/(fixed_point x, fixed_point y) { return functions::div(x, y); } template fixed_point operator>>(fixed_point x, size_t shift) { return functions::shift_right(x, shift); } template fixed_point operator<<(fixed_point x, size_t shift) { return functions::shift_left(x, shift); } template std::basic_ostream &operator<<(std::basic_ostream &s, fixed_point x) { return functions::write(s, x); } template inline fixed_point min(fixed_point x, fixed_point y) { return x > y ? y : x; } template inline fixed_point max(fixed_point x, fixed_point y) { return x > y ? x : y; } template inline fixed_point add(fixed_point x, fixed_point y) { return functions::add(x, y); } template inline fixed_point sub(fixed_point x, fixed_point y) { return functions::sub(x, y); } template inline fixed_point mul(fixed_point x, fixed_point y) { return functions::mul(x, y); } template inline fixed_point div(fixed_point x, fixed_point y) { return functions::div(x, y); } template inline fixed_point abs(fixed_point x) { return functions::abs(x); } template inline fixed_point clamp(fixed_point x, T min, T max) { return functions::clamp(x, min, max); } template inline fixed_point exp(fixed_point x) { return functions::exp(x); } template inline fixed_point log(fixed_point x) { return functions::log(x); } template inline fixed_point inv_sqrt(fixed_point x) { return functions::inv_sqrt(x); } template inline fixed_point tanh(fixed_point x) { return functions::tanh(x); } template inline fixed_point pow(fixed_point x, fixed_point a) { return functions::pow(x, a); } } // namespace detail // Expose operators using detail::operator==; using detail::operator!=; using detail::operator<; using detail::operator>; using detail::operator<=; using detail::operator>=; using detail::operator+; using detail::operator-; using detail::operator*; using detail::operator/; using detail::operator>>; using detail::operator<<; // Expose additional functions using detail::min; using detail::max; using detail::add; using detail::sub; using detail::mul; using detail::div; using detail::abs; using detail::clamp; using detail::exp; using detail::log; using detail::inv_sqrt; using detail::tanh; using detail::pow; // TODO: floor // TODO: ceil // TODO: sqrt } // namespace fixed_point_arithmetic } // namespace test } // namespace arm_compute #endif /*__ARM_COMPUTE_TEST_VALIDATION_FIXEDPOINT_H__ */