2 * Copyright (c) 2017-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #ifndef __ARM_COMPUTE_TEST_VALIDATION_FIXEDPOINT_H__
25 #define __ARM_COMPUTE_TEST_VALIDATION_FIXEDPOINT_H__
27 #include "support/ToolchainSupport.h"
28 #include "tests/Utils.h"
35 #include <type_traits>
41 namespace fixed_point_arithmetic
45 // Forward declare structs
51 /** Fixed point traits */
58 template <typename T> struct promote { };
59 /** Promote uint8_t to uint16_t */
60 template <> struct promote<uint8_t> { using type = uint16_t; /**< Promoted type */ };
61 /** Promote int8_t to int16_t */
62 template <> struct promote<int8_t> { using type = int16_t; /**< Promoted type */ };
63 /** Promote uint16_t to uint32_t */
64 template <> struct promote<uint16_t> { using type = uint32_t; /**< Promoted type */ };
65 /** Promote int16_t to int32_t */
66 template <> struct promote<int16_t> { using type = int32_t; /**< Promoted type */ };
67 /** Promote uint32_t to uint64_t */
68 template <> struct promote<uint32_t> { using type = uint64_t; /**< Promoted type */ };
69 /** Promote int32_t to int64_t */
70 template <> struct promote<int32_t> { using type = int64_t; /**< Promoted type */ };
71 /** Promote float to float */
72 template <> struct promote<float> { using type = float; /**< Promoted type */ };
73 /** Promote half to half */
74 template <> struct promote<half> { using type = half; /**< Promoted type */ };
76 /** Get promoted type */
78 using promote_t = typename promote<T>::type;
83 /** Strongly typed enum class representing the overflow policy */
84 enum class OverflowPolicy
86 WRAP, /**< Wrap policy */
87 SATURATE /**< Saturate policy */
89 /** Strongly typed enum class representing the rounding policy */
90 enum class RoundingPolicy
92 TO_ZERO, /**< Round to zero policy */
93 TO_NEAREST_EVEN /**< Round to nearest even policy */
96 /** Arbitrary fixed-point arithmetic class */
102 static_assert(std::is_integral<T>::value, "Type is not an integer");
104 /** Constructor (from different fixed point type)
106 * @param[in] val Fixed point
107 * @param[in] p Fixed point precision
109 template <typename U>
110 fixed_point(fixed_point<U> val, uint8_t p)
111 : _value(0), _fixed_point_position(p)
113 assert(p > 0 && p < std::numeric_limits<T>::digits);
116 if(std::numeric_limits<T>::digits < std::numeric_limits<U>::digits)
119 v = detail::constant_expr<T>::saturate_cast(val.raw());
123 auto v_cast = static_cast<fixed_point<T>>(val);
127 _value = static_cast<T>(v);
129 /** Constructor (from integer)
131 * @param[in] val Integer value to be represented as fixed point
132 * @param[in] p Fixed point precision
133 * @param[in] is_raw If true val is a raw fixed point value else an integer
135 template <typename U, typename = typename std::enable_if<std::is_integral<U>::value>::type>
136 fixed_point(U val, uint8_t p, bool is_raw = false)
137 : _value(val << p), _fixed_point_position(p)
144 /** Constructor (from float)
146 * @param[in] val Float value to be represented as fixed point
147 * @param[in] p Fixed point precision
149 fixed_point(float val, uint8_t p)
150 : _value(detail::constant_expr<T>::to_fixed(val, p)), _fixed_point_position(p)
152 assert(p > 0 && p < std::numeric_limits<T>::digits);
154 /** Constructor (from float string)
156 * @param[in] str Float string to be represented as fixed point
157 * @param[in] p Fixed point precision
159 fixed_point(std::string str, uint8_t p)
160 : _value(detail::constant_expr<T>::to_fixed(support::cpp11::stof(str), p)), _fixed_point_position(p)
162 assert(p > 0 && p < std::numeric_limits<T>::digits);
164 /** Default copy constructor */
165 fixed_point &operator=(const fixed_point &) = default;
166 /** Default move constructor */
167 fixed_point &operator=(fixed_point &&) = default;
168 /** Default copy assignment operator */
169 fixed_point(const fixed_point &) = default;
170 /** Default move assignment operator */
171 fixed_point(fixed_point &&) = default;
173 /** Float conversion operator
175 * @return Float representation of fixed point
177 operator float() const
179 return detail::constant_expr<T>::to_float(_value, _fixed_point_position);
181 /** Integer conversion operator
183 * @return Integer representation of fixed point
185 template <typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
188 return detail::constant_expr<T>::to_int(_value, _fixed_point_position);
190 /** Convert to different fixed point of different type but same precision
192 * @note Down-conversion might fail.
194 template <typename U>
195 operator fixed_point<U>()
197 U val = static_cast<U>(_value);
198 if(std::numeric_limits<U>::digits < std::numeric_limits<T>::digits)
200 val = detail::constant_expr<U>::saturate_cast(_value);
202 return fixed_point<U>(val, _fixed_point_position, true);
205 /** Arithmetic += assignment operator
207 * @param[in] rhs Fixed point operand
209 * @return Reference to this fixed point
211 template <typename U>
212 fixed_point<T> &operator+=(const fixed_point<U> &rhs)
214 fixed_point<T> val(rhs, _fixed_point_position);
218 /** Arithmetic -= assignment operator
220 * @param[in] rhs Fixed point operand
222 * @return Reference to this fixed point
224 template <typename U>
225 fixed_point<T> &operator-=(const fixed_point<U> &rhs)
227 fixed_point<T> val(rhs, _fixed_point_position);
232 /** Raw value accessor
234 * @return Raw fixed point value
240 /** Precision accessor
242 * @return Precision of fixed point
244 uint8_t precision() const
246 return _fixed_point_position;
248 /** Rescale a fixed point to a new precision
250 * @param[in] p New fixed point precision
252 void rescale(uint8_t p)
254 assert(p > 0 && p < std::numeric_limits<T>::digits);
256 using promoted_T = typename traits::promote<T>::type;
257 promoted_T val = _value;
258 if(p > _fixed_point_position)
260 val <<= (p - _fixed_point_position);
262 else if(p < _fixed_point_position)
264 uint8_t pbar = _fixed_point_position - p;
265 val += (pbar != 0) ? (1 << (pbar - 1)) : 0;
269 _value = detail::constant_expr<T>::saturate_cast(val);
270 _fixed_point_position = p;
274 T _value; /**< Fixed point raw value */
275 uint8_t _fixed_point_position; /**< Fixed point precision */
280 /** Count the number of leading zero bits in the given value.
282 * @param[in] value Input value.
284 * @return Number of leading zero bits.
286 template <typename T>
287 constexpr int clz(T value)
289 using unsigned_T = typename std::make_unsigned<T>::type;
290 // __builtin_clz is available for int. Need to correct reported number to
291 // match the original type.
292 return __builtin_clz(value) - (32 - std::numeric_limits<unsigned_T>::digits);
295 /** Constant expressions */
296 template <typename T>
299 /** Calculate representation of 1 in fixed point given a fixed point precision
301 * @param[in] p Fixed point precision
303 * @return Representation of value 1 in fixed point.
305 static constexpr T fixed_one(uint8_t p)
309 /** Calculate fixed point precision step given a fixed point precision
311 * @param[in] p Fixed point precision
313 * @return Fixed point precision step
315 static constexpr float fixed_step(uint8_t p)
317 return (1.0f / static_cast<float>(1 << p));
320 /** Convert a fixed point value to float given its precision.
322 * @param[in] val Fixed point value
323 * @param[in] p Fixed point precision
325 * @return Float representation of the fixed point number
327 static constexpr float to_float(T val, uint8_t p)
329 return static_cast<float>(val * fixed_step(p));
331 /** Convert a fixed point value to integer given its precision.
333 * @param[in] val Fixed point value
334 * @param[in] p Fixed point precision
336 * @return Integer of the fixed point number
338 static constexpr T to_int(T val, uint8_t p)
342 /** Convert a single precision floating point value to a fixed point representation given its precision.
344 * @param[in] val Floating point value
345 * @param[in] p Fixed point precision
347 * @return The raw fixed point representation
349 static constexpr T to_fixed(float val, uint8_t p)
351 return static_cast<T>(saturate_cast<float>(val * fixed_one(p) + ((val >= 0) ? 0.5 : -0.5)));
353 /** Clamp value between two ranges
355 * @param[in] val Value to clamp
356 * @param[in] min Minimum value to clamp to
357 * @param[in] max Maximum value to clamp to
359 * @return clamped value
361 static constexpr T clamp(T val, T min, T max)
363 return std::min(std::max(val, min), max);
365 /** Saturate given number
367 * @param[in] val Value to saturate
369 * @return Saturated value
371 template <typename U>
372 static constexpr T saturate_cast(U val)
374 return static_cast<T>(std::min<U>(std::max<U>(val, static_cast<U>(std::numeric_limits<T>::min())), static_cast<U>(std::numeric_limits<T>::max())));
380 /** Output stream operator
382 * @param[in] s Output stream
383 * @param[in] x Fixed point value
385 * @return Reference output to updated stream
387 template <typename T, typename U, typename traits>
388 static std::basic_ostream<T, traits> &write(std::basic_ostream<T, traits> &s, fixed_point<U> &x)
390 return s << static_cast<float>(x);
392 /** Signbit of a fixed point number.
394 * @param[in] x Fixed point number
396 * @return True if negative else false.
398 template <typename T>
399 static bool signbit(fixed_point<T> x)
401 return ((x.raw() >> std::numeric_limits<T>::digits) != 0);
403 /** Checks if two fixed point numbers are equal
405 * @param[in] x First fixed point operand
406 * @param[in] y Second fixed point operand
408 * @return True if fixed points are equal else false
410 template <typename T>
411 static bool isequal(fixed_point<T> x, fixed_point<T> y)
413 uint8_t p = std::min(x.precision(), y.precision());
416 return (x.raw() == y.raw());
418 /** Checks if two fixed point number are not equal
420 * @param[in] x First fixed point operand
421 * @param[in] y Second fixed point operand
423 * @return True if fixed points are not equal else false
425 template <typename T>
426 static bool isnotequal(fixed_point<T> x, fixed_point<T> y)
428 return !isequal(x, y);
430 /** Checks if one fixed point is greater than the other
432 * @param[in] x First fixed point operand
433 * @param[in] y Second fixed point operand
435 * @return True if fixed point is greater than other
437 template <typename T>
438 static bool isgreater(fixed_point<T> x, fixed_point<T> y)
440 uint8_t p = std::min(x.precision(), y.precision());
443 return (x.raw() > y.raw());
445 /** Checks if one fixed point is greater or equal than the other
447 * @param[in] x First fixed point operand
448 * @param[in] y Second fixed point operand
450 * @return True if fixed point is greater or equal than other
452 template <typename T>
453 static bool isgreaterequal(fixed_point<T> x, fixed_point<T> y)
455 uint8_t p = std::min(x.precision(), y.precision());
458 return (x.raw() >= y.raw());
460 /** Checks if one fixed point is less than the other
462 * @param[in] x First fixed point operand
463 * @param[in] y Second fixed point operand
465 * @return True if fixed point is less than other
467 template <typename T>
468 static bool isless(fixed_point<T> x, fixed_point<T> y)
470 uint8_t p = std::min(x.precision(), y.precision());
473 return (x.raw() < y.raw());
475 /** Checks if one fixed point is less or equal than the other
477 * @param[in] x First fixed point operand
478 * @param[in] y Second fixed point operand
480 * @return True if fixed point is less or equal than other
482 template <typename T>
483 static bool islessequal(fixed_point<T> x, fixed_point<T> y)
485 uint8_t p = std::min(x.precision(), y.precision());
488 return (x.raw() <= y.raw());
490 /** Checks if one fixed point is less or greater than the other
492 * @param[in] x First fixed point operand
493 * @param[in] y Second fixed point operand
495 * @return True if fixed point is less or greater than other
497 template <typename T>
498 static bool islessgreater(fixed_point<T> x, fixed_point<T> y)
500 return isnotequal(x, y);
502 /** Clamp fixed point to specific range.
504 * @param[in] x Fixed point operand
505 * @param[in] min Minimum value to clamp to
506 * @param[in] max Maximum value to clamp to
508 * @return Clamped result
510 template <typename T>
511 static fixed_point<T> clamp(fixed_point<T> x, T min, T max)
513 return fixed_point<T>(constant_expr<T>::clamp(x.raw(), min, max), x.precision(), true);
517 * @param[in] x Fixed point operand
519 * @return Negated fixed point result
521 template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T>
522 static fixed_point<T> negate(fixed_point<T> x)
524 using promoted_T = typename traits::promote<T>::type;
525 promoted_T val = -x.raw();
526 if(OP == OverflowPolicy::SATURATE)
528 val = constant_expr<T>::saturate_cast(val);
530 return fixed_point<T>(static_cast<T>(val), x.precision(), true);
532 /** Perform addition among two fixed point numbers
534 * @param[in] x First fixed point operand
535 * @param[in] y Second fixed point operand
537 * @return Result fixed point with precision equal to minimum precision of both operands
539 template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T>
540 static fixed_point<T> add(fixed_point<T> x, fixed_point<T> y)
542 uint8_t p = std::min(x.precision(), y.precision());
545 if(OP == OverflowPolicy::SATURATE)
547 using type = typename traits::promote<T>::type;
548 type val = static_cast<type>(x.raw()) + static_cast<type>(y.raw());
549 val = constant_expr<T>::saturate_cast(val);
550 return fixed_point<T>(static_cast<T>(val), p, true);
554 return fixed_point<T>(x.raw() + y.raw(), p, true);
557 /** Perform subtraction among two fixed point numbers
559 * @param[in] x First fixed point operand
560 * @param[in] y Second fixed point operand
562 * @return Result fixed point with precision equal to minimum precision of both operands
564 template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T>
565 static fixed_point<T> sub(fixed_point<T> x, fixed_point<T> y)
567 uint8_t p = std::min(x.precision(), y.precision());
570 if(OP == OverflowPolicy::SATURATE)
572 using type = typename traits::promote<T>::type;
573 type val = static_cast<type>(x.raw()) - static_cast<type>(y.raw());
574 val = constant_expr<T>::saturate_cast(val);
575 return fixed_point<T>(static_cast<T>(val), p, true);
579 return fixed_point<T>(x.raw() - y.raw(), p, true);
582 /** Perform multiplication among two fixed point numbers
584 * @param[in] x First fixed point operand
585 * @param[in] y Second fixed point operand
587 * @return Result fixed point with precision equal to minimum precision of both operands
589 template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T>
590 static fixed_point<T> mul(fixed_point<T> x, fixed_point<T> y)
592 using promoted_T = typename traits::promote<T>::type;
593 uint8_t p_min = std::min(x.precision(), y.precision());
594 uint8_t p_max = std::max(x.precision(), y.precision());
595 promoted_T round_factor = (1 << (p_max - 1));
596 promoted_T val = ((static_cast<promoted_T>(x.raw()) * static_cast<promoted_T>(y.raw())) + round_factor) >> p_max;
597 if(OP == OverflowPolicy::SATURATE)
599 val = constant_expr<T>::saturate_cast(val);
601 return fixed_point<T>(static_cast<T>(val), p_min, true);
603 /** Perform division among two fixed point numbers
605 * @param[in] x First fixed point operand
606 * @param[in] y Second fixed point operand
608 * @return Result fixed point with precision equal to minimum precision of both operands
610 template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T>
611 static fixed_point<T> div(fixed_point<T> x, fixed_point<T> y)
613 using promoted_T = typename traits::promote<T>::type;
614 uint8_t p = std::min(x.precision(), y.precision());
615 promoted_T denom = static_cast<promoted_T>(y.raw());
618 promoted_T val = (static_cast<promoted_T>(x.raw()) << std::max(x.precision(), y.precision())) / denom;
619 if(OP == OverflowPolicy::SATURATE)
621 val = constant_expr<T>::saturate_cast(val);
623 return fixed_point<T>(static_cast<T>(val), p, true);
627 T val = (x.raw() < 0) ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
628 return fixed_point<T>(val, p, true);
633 * @param[in] x Fixed point operand
634 * @param[in] shift Shift value
636 * @return Shifted value
638 template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T>
639 static fixed_point<T> shift_left(fixed_point<T> x, size_t shift)
641 using promoted_T = typename traits::promote<T>::type;
642 promoted_T val = static_cast<promoted_T>(x.raw()) << shift;
643 if(OP == OverflowPolicy::SATURATE)
645 val = constant_expr<T>::saturate_cast(val);
647 return fixed_point<T>(static_cast<T>(val), x.precision(), true);
651 * @param[in] x Fixed point operand
652 * @param[in] shift Shift value
654 * @return Shifted value
656 template <typename T>
657 static fixed_point<T> shift_right(fixed_point<T> x, size_t shift)
659 return fixed_point<T>(x.raw() >> shift, x.precision(), true);
661 /** Calculate absolute value
663 * @param[in] x Fixed point operand
665 * @return Absolute value of operand
667 template <typename T>
668 static fixed_point<T> abs(fixed_point<T> x)
670 using promoted_T = typename traits::promote<T>::type;
671 T val = (x.raw() < 0) ? constant_expr<T>::saturate_cast(-static_cast<promoted_T>(x.raw())) : x.raw();
672 return fixed_point<T>(val, x.precision(), true);
674 /** Calculate the logarithm of a fixed point number
676 * @param[in] x Fixed point operand
678 * @return Logarithm value of operand
680 template <typename T>
681 static fixed_point<T> log(fixed_point<T> x)
683 uint8_t p = x.precision();
684 auto const_one = fixed_point<T>(static_cast<T>(1), p);
686 // Logarithm of 1 is zero and logarithm of negative values is not defined in R, so return 0.
687 // Also, log(x) == -log(1/x) for 0 < x < 1.
688 if(isequal(x, const_one) || islessequal(x, fixed_point<T>(static_cast<T>(0), p)))
690 return fixed_point<T>(static_cast<T>(0), p, true);
692 else if(isless(x, const_one))
694 return mul(log(div(const_one, x)), fixed_point<T>(-1, p));
697 // Remove even powers of 2
698 T shift_val = 31 - __builtin_clz(x.raw() >> p);
699 x = shift_right(x, shift_val);
700 x = sub(x, const_one);
703 auto ln2 = fixed_point<T>(0.6931471, p);
704 auto A = fixed_point<T>(1.4384189, p);
705 auto B = fixed_point<T>(-0.67719, p);
706 auto C = fixed_point<T>(0.3218538, p);
707 auto D = fixed_point<T>(-0.0832229, p);
709 // Polynomial expansion
710 auto sum = add(mul(x, D), C);
711 sum = add(mul(x, sum), B);
712 sum = add(mul(x, sum), A);
715 return mul(add(sum, fixed_point<T>(static_cast<T>(shift_val), p)), ln2);
717 /** Calculate the exponential of a fixed point number.
719 * exp(x) = exp(floor(x)) * exp(x - floor(x))
720 * = pow(2, floor(x) / ln(2)) * exp(x - floor(x))
721 * = exp(x - floor(x)) << (floor(x) / ln(2))
723 * @param[in] x Fixed point operand
725 * @return Exponential value of operand
727 template <typename T>
728 static fixed_point<T> exp(fixed_point<T> x)
730 uint8_t p = x.precision();
732 auto const_one = fixed_point<T>(1, p);
733 auto ln2 = fixed_point<T>(0.6931471, p);
734 auto inv_ln2 = fixed_point<T>(1.442695, p);
735 auto A = fixed_point<T>(0.9978546, p);
736 auto B = fixed_point<T>(0.4994721, p);
737 auto C = fixed_point<T>(0.1763723, p);
738 auto D = fixed_point<T>(0.0435108, p);
740 T scaled_int_part = detail::constant_expr<T>::to_int(mul(x, inv_ln2).raw(), p);
742 // Polynomial expansion
743 auto frac_part = sub(x, mul(ln2, fixed_point<T>(scaled_int_part, p)));
744 auto taylor = add(mul(frac_part, D), C);
745 taylor = add(mul(frac_part, taylor), B);
746 taylor = add(mul(frac_part, taylor), A);
747 taylor = mul(frac_part, taylor);
748 taylor = add(taylor, const_one);
751 if(static_cast<T>(clz(taylor.raw())) <= scaled_int_part)
753 return fixed_point<T>(std::numeric_limits<T>::max(), p, true);
756 return (scaled_int_part < 0) ? shift_right(taylor, -scaled_int_part) : shift_left(taylor, scaled_int_part);
758 /** Calculate the inverse square root of a fixed point number
760 * @param[in] x Fixed point operand
762 * @return Inverse square root value of operand
764 template <typename T>
765 static fixed_point<T> inv_sqrt(fixed_point<T> x)
767 const uint8_t p = x.precision();
768 int8_t shift = std::numeric_limits<T>::digits - (p + detail::clz(x.raw()));
770 shift += std::numeric_limits<T>::is_signed ? 1 : 0;
772 // Use volatile to restrict compiler optimizations on shift as compiler reports maybe-uninitialized error on Android
773 volatile int8_t *shift_ptr = &shift;
775 auto const_three = fixed_point<T>(3, p);
776 auto a = (*shift_ptr < 0) ? shift_left(x, -(shift)) : shift_right(x, shift);
777 fixed_point<T> x2 = a;
779 // We need three iterations to find the result for QS8 and five for QS16
780 constexpr int num_iterations = std::is_same<T, int8_t>::value ? 3 : 5;
781 for(int i = 0; i < num_iterations; ++i)
783 fixed_point<T> three_minus_dx = sub(const_three, mul(a, mul(x2, x2)));
784 x2 = shift_right(mul(x2, three_minus_dx), 1);
787 return (shift < 0) ? shift_left(x2, (-shift) >> 1) : shift_right(x2, shift >> 1);
789 /** Calculate the hyperbolic tangent of a fixed point number
791 * @param[in] x Fixed point operand
793 * @return Hyperbolic tangent of the operand
795 template <typename T>
796 static fixed_point<T> tanh(fixed_point<T> x)
798 uint8_t p = x.precision();
800 auto const_one = fixed_point<T>(1, p);
801 auto const_two = fixed_point<T>(2, p);
803 auto exp2x = exp(const_two * x);
804 auto num = exp2x - const_one;
805 auto den = exp2x + const_one;
806 auto tanh = num / den;
810 /** Calculate the a-th power of a fixed point number.
812 * The power is computed as x^a = e^(log(x) * a)
814 * @param[in] x Fixed point operand
815 * @param[in] a Fixed point exponent
817 * @return a-th power of the operand
819 template <typename T>
820 static fixed_point<T> pow(fixed_point<T> x, fixed_point<T> a)
822 return exp(log(x) * a);
826 template <typename T>
827 bool operator==(const fixed_point<T> &lhs, const fixed_point<T> &rhs)
829 return functions::isequal(lhs, rhs);
831 template <typename T>
832 bool operator!=(const fixed_point<T> &lhs, const fixed_point<T> &rhs)
834 return !operator==(lhs, rhs);
836 template <typename T>
837 bool operator<(const fixed_point<T> &lhs, const fixed_point<T> &rhs)
839 return functions::isless(lhs, rhs);
841 template <typename T>
842 bool operator>(const fixed_point<T> &lhs, const fixed_point<T> &rhs)
844 return operator<(rhs, lhs);
846 template <typename T>
847 bool operator<=(const fixed_point<T> &lhs, const fixed_point<T> &rhs)
849 return !operator>(lhs, rhs);
851 template <typename T>
852 bool operator>=(const fixed_point<T> &lhs, const fixed_point<T> &rhs)
854 return !operator<(lhs, rhs);
856 template <typename T>
857 fixed_point<T> operator+(const fixed_point<T> &lhs, const fixed_point<T> &rhs)
859 return functions::add(lhs, rhs);
861 template <typename T>
862 fixed_point<T> operator-(const fixed_point<T> &lhs, const fixed_point<T> &rhs)
864 return functions::sub(lhs, rhs);
866 template <typename T>
867 fixed_point<T> operator-(const fixed_point<T> &rhs)
869 return functions::negate(rhs);
871 template <typename T>
872 fixed_point<T> operator*(fixed_point<T> x, fixed_point<T> y)
874 return functions::mul(x, y);
876 template <typename T>
877 fixed_point<T> operator/(fixed_point<T> x, fixed_point<T> y)
879 return functions::div(x, y);
881 template <typename T>
882 fixed_point<T> operator>>(fixed_point<T> x, size_t shift)
884 return functions::shift_right(x, shift);
886 template <typename T>
887 fixed_point<T> operator<<(fixed_point<T> x, size_t shift)
889 return functions::shift_left(x, shift);
891 template <typename T, typename U, typename traits>
892 std::basic_ostream<T, traits> &operator<<(std::basic_ostream<T, traits> &s, fixed_point<U> x)
894 return functions::write(s, x);
896 template <typename T>
897 inline fixed_point<T> min(fixed_point<T> x, fixed_point<T> y)
899 return x > y ? y : x;
901 template <typename T>
902 inline fixed_point<T> max(fixed_point<T> x, fixed_point<T> y)
904 return x > y ? x : y;
906 template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T>
907 inline fixed_point<T> add(fixed_point<T> x, fixed_point<T> y)
909 return functions::add<OP>(x, y);
911 template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T>
912 inline fixed_point<T> sub(fixed_point<T> x, fixed_point<T> y)
914 return functions::sub<OP>(x, y);
916 template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T>
917 inline fixed_point<T> mul(fixed_point<T> x, fixed_point<T> y)
919 return functions::mul<OP>(x, y);
921 template <typename T>
922 inline fixed_point<T> div(fixed_point<T> x, fixed_point<T> y)
924 return functions::div(x, y);
926 template <typename T>
927 inline fixed_point<T> abs(fixed_point<T> x)
929 return functions::abs(x);
931 template <typename T>
932 inline fixed_point<T> clamp(fixed_point<T> x, T min, T max)
934 return functions::clamp(x, min, max);
936 template <typename T>
937 inline fixed_point<T> exp(fixed_point<T> x)
939 return functions::exp(x);
941 template <typename T>
942 inline fixed_point<T> log(fixed_point<T> x)
944 return functions::log(x);
946 template <typename T>
947 inline fixed_point<T> inv_sqrt(fixed_point<T> x)
949 return functions::inv_sqrt(x);
951 template <typename T>
952 inline fixed_point<T> tanh(fixed_point<T> x)
954 return functions::tanh(x);
956 template <typename T>
957 inline fixed_point<T> pow(fixed_point<T> x, fixed_point<T> a)
959 return functions::pow(x, a);
961 } // namespace detail
964 using detail::operator==;
965 using detail::operator!=;
966 using detail::operator<;
967 using detail::operator>;
968 using detail::operator<=;
969 using detail::operator>=;
970 using detail::operator+;
971 using detail::operator-;
972 using detail::operator*;
973 using detail::operator/;
974 using detail::operator>>;
975 using detail::operator<<;
977 // Expose additional functions
988 using detail::inv_sqrt;
991 } // namespace fixed_point_arithmetic
993 } // namespace arm_compute
994 #endif /*__ARM_COMPUTE_TEST_VALIDATION_FIXEDPOINT_H__ */