Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 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: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 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 |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef __ARM_COMPUTE_TEST_VALIDATION_FIXEDPOINT_H__ |
| 25 | #define __ARM_COMPUTE_TEST_VALIDATION_FIXEDPOINT_H__ |
| 26 | |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 27 | #include "support/ToolchainSupport.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 28 | #include "tests/Utils.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | |
| 30 | #include <cassert> |
| 31 | #include <cstdint> |
| 32 | #include <cstdlib> |
| 33 | #include <limits> |
| 34 | #include <string> |
| 35 | #include <type_traits> |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace fixed_point_arithmetic |
| 42 | { |
| 43 | namespace detail |
| 44 | { |
| 45 | // Forward declare structs |
| 46 | struct functions; |
| 47 | template <typename T> |
| 48 | struct constant_expr; |
| 49 | } |
| 50 | |
| 51 | /** Fixed point traits */ |
| 52 | namespace traits |
| 53 | { |
| 54 | // Promote types |
| 55 | // *INDENT-OFF* |
| 56 | // clang-format off |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 57 | /** Promote a type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | template <typename T> struct promote { }; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 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 */ }; |
| 75 | |
| 76 | /** Get promoted type */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 77 | template <typename T> |
| 78 | using promote_t = typename promote<T>::type; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | // clang-format on |
| 80 | // *INDENT-ON* |
| 81 | } |
| 82 | |
| 83 | /** Strongly typed enum class representing the overflow policy */ |
| 84 | enum class OverflowPolicy |
| 85 | { |
| 86 | WRAP, /**< Wrap policy */ |
| 87 | SATURATE /**< Saturate policy */ |
| 88 | }; |
| 89 | /** Strongly typed enum class representing the rounding policy */ |
| 90 | enum class RoundingPolicy |
| 91 | { |
| 92 | TO_ZERO, /**< Round to zero policy */ |
| 93 | TO_NEAREST_EVEN /**< Round to nearest even policy */ |
| 94 | }; |
| 95 | |
| 96 | /** Arbitrary fixed-point arithmetic class */ |
| 97 | template <typename T> |
| 98 | class fixed_point |
| 99 | { |
| 100 | public: |
| 101 | // Static Checks |
| 102 | static_assert(std::is_integral<T>::value, "Type is not an integer"); |
| 103 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | /** Constructor (from different fixed point type) |
| 105 | * |
| 106 | * @param[in] val Fixed point |
| 107 | * @param[in] p Fixed point precision |
| 108 | */ |
| 109 | template <typename U> |
| 110 | fixed_point(fixed_point<U> val, uint8_t p) |
| 111 | : _value(0), _fixed_point_position(p) |
| 112 | { |
| 113 | assert(p > 0 && p < std::numeric_limits<T>::digits); |
| 114 | T v = 0; |
| 115 | |
| 116 | if(std::numeric_limits<T>::digits < std::numeric_limits<U>::digits) |
| 117 | { |
| 118 | val.rescale(p); |
| 119 | v = detail::constant_expr<T>::saturate_cast(val.raw()); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | auto v_cast = static_cast<fixed_point<T>>(val); |
| 124 | v_cast.rescale(p); |
| 125 | v = v_cast.raw(); |
| 126 | } |
| 127 | _value = static_cast<T>(v); |
| 128 | } |
| 129 | /** Constructor (from integer) |
| 130 | * |
| 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 |
| 134 | */ |
| 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) |
| 138 | { |
| 139 | if(is_raw) |
| 140 | { |
| 141 | _value = val; |
| 142 | } |
| 143 | } |
| 144 | /** Constructor (from float) |
| 145 | * |
| 146 | * @param[in] val Float value to be represented as fixed point |
| 147 | * @param[in] p Fixed point precision |
| 148 | */ |
| 149 | fixed_point(float val, uint8_t p) |
| 150 | : _value(detail::constant_expr<T>::to_fixed(val, p)), _fixed_point_position(p) |
| 151 | { |
| 152 | assert(p > 0 && p < std::numeric_limits<T>::digits); |
| 153 | } |
| 154 | /** Constructor (from float string) |
| 155 | * |
| 156 | * @param[in] str Float string to be represented as fixed point |
| 157 | * @param[in] p Fixed point precision |
| 158 | */ |
| 159 | fixed_point(std::string str, uint8_t p) |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 160 | : _value(detail::constant_expr<T>::to_fixed(support::cpp11::stof(str), p)), _fixed_point_position(p) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 161 | { |
| 162 | assert(p > 0 && p < std::numeric_limits<T>::digits); |
| 163 | } |
| 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; |
| 172 | |
| 173 | /** Float conversion operator |
| 174 | * |
| 175 | * @return Float representation of fixed point |
| 176 | */ |
| 177 | operator float() const |
| 178 | { |
| 179 | return detail::constant_expr<T>::to_float(_value, _fixed_point_position); |
| 180 | } |
| 181 | /** Integer conversion operator |
| 182 | * |
| 183 | * @return Integer representation of fixed point |
| 184 | */ |
| 185 | template <typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
| 186 | operator U() const |
| 187 | { |
| 188 | return detail::constant_expr<T>::to_int(_value, _fixed_point_position); |
| 189 | } |
| 190 | /** Convert to different fixed point of different type but same precision |
| 191 | * |
| 192 | * @note Down-conversion might fail. |
| 193 | */ |
| 194 | template <typename U> |
| 195 | operator fixed_point<U>() |
| 196 | { |
| 197 | U val = static_cast<U>(_value); |
| 198 | if(std::numeric_limits<U>::digits < std::numeric_limits<T>::digits) |
| 199 | { |
| 200 | val = detail::constant_expr<U>::saturate_cast(_value); |
| 201 | } |
| 202 | return fixed_point<U>(val, _fixed_point_position, true); |
| 203 | } |
| 204 | |
| 205 | /** Arithmetic += assignment operator |
| 206 | * |
| 207 | * @param[in] rhs Fixed point operand |
| 208 | * |
| 209 | * @return Reference to this fixed point |
| 210 | */ |
| 211 | template <typename U> |
| 212 | fixed_point<T> &operator+=(const fixed_point<U> &rhs) |
| 213 | { |
| 214 | fixed_point<T> val(rhs, _fixed_point_position); |
| 215 | _value += val.raw(); |
| 216 | return *this; |
| 217 | } |
| 218 | /** Arithmetic -= assignment operator |
| 219 | * |
| 220 | * @param[in] rhs Fixed point operand |
| 221 | * |
| 222 | * @return Reference to this fixed point |
| 223 | */ |
| 224 | template <typename U> |
| 225 | fixed_point<T> &operator-=(const fixed_point<U> &rhs) |
| 226 | { |
| 227 | fixed_point<T> val(rhs, _fixed_point_position); |
| 228 | _value -= val.raw(); |
| 229 | return *this; |
| 230 | } |
| 231 | |
| 232 | /** Raw value accessor |
| 233 | * |
| 234 | * @return Raw fixed point value |
| 235 | */ |
| 236 | T raw() const |
| 237 | { |
| 238 | return _value; |
| 239 | } |
| 240 | /** Precision accessor |
| 241 | * |
| 242 | * @return Precision of fixed point |
| 243 | */ |
| 244 | uint8_t precision() const |
| 245 | { |
| 246 | return _fixed_point_position; |
| 247 | } |
| 248 | /** Rescale a fixed point to a new precision |
| 249 | * |
| 250 | * @param[in] p New fixed point precision |
| 251 | */ |
| 252 | void rescale(uint8_t p) |
| 253 | { |
| 254 | assert(p > 0 && p < std::numeric_limits<T>::digits); |
| 255 | |
Georgios Pinitas | e222941 | 2017-07-12 12:30:40 +0100 | [diff] [blame] | 256 | using promoted_T = typename traits::promote<T>::type; |
| 257 | promoted_T val = _value; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 258 | if(p > _fixed_point_position) |
| 259 | { |
Georgios Pinitas | e222941 | 2017-07-12 12:30:40 +0100 | [diff] [blame] | 260 | val <<= (p - _fixed_point_position); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 261 | } |
| 262 | else if(p < _fixed_point_position) |
| 263 | { |
Georgios Pinitas | e222941 | 2017-07-12 12:30:40 +0100 | [diff] [blame] | 264 | uint8_t pbar = _fixed_point_position - p; |
| 265 | val += (pbar != 0) ? (1 << (pbar - 1)) : 0; |
| 266 | val >>= pbar; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Georgios Pinitas | e222941 | 2017-07-12 12:30:40 +0100 | [diff] [blame] | 269 | _value = detail::constant_expr<T>::saturate_cast(val); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 270 | _fixed_point_position = p; |
| 271 | } |
| 272 | |
| 273 | private: |
| 274 | T _value; /**< Fixed point raw value */ |
| 275 | uint8_t _fixed_point_position; /**< Fixed point precision */ |
| 276 | }; |
| 277 | |
| 278 | namespace detail |
| 279 | { |
| 280 | /** Count the number of leading zero bits in the given value. |
| 281 | * |
| 282 | * @param[in] value Input value. |
| 283 | * |
| 284 | * @return Number of leading zero bits. |
| 285 | */ |
| 286 | template <typename T> |
| 287 | constexpr int clz(T value) |
| 288 | { |
| 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); |
| 293 | } |
| 294 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 295 | /** Constant expressions */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 296 | template <typename T> |
| 297 | struct constant_expr |
| 298 | { |
| 299 | /** Calculate representation of 1 in fixed point given a fixed point precision |
| 300 | * |
| 301 | * @param[in] p Fixed point precision |
| 302 | * |
| 303 | * @return Representation of value 1 in fixed point. |
| 304 | */ |
| 305 | static constexpr T fixed_one(uint8_t p) |
| 306 | { |
| 307 | return (1 << p); |
| 308 | } |
| 309 | /** Calculate fixed point precision step given a fixed point precision |
| 310 | * |
| 311 | * @param[in] p Fixed point precision |
| 312 | * |
| 313 | * @return Fixed point precision step |
| 314 | */ |
| 315 | static constexpr float fixed_step(uint8_t p) |
| 316 | { |
| 317 | return (1.0f / static_cast<float>(1 << p)); |
| 318 | } |
| 319 | |
| 320 | /** Convert a fixed point value to float given its precision. |
| 321 | * |
| 322 | * @param[in] val Fixed point value |
| 323 | * @param[in] p Fixed point precision |
| 324 | * |
| 325 | * @return Float representation of the fixed point number |
| 326 | */ |
| 327 | static constexpr float to_float(T val, uint8_t p) |
| 328 | { |
| 329 | return static_cast<float>(val * fixed_step(p)); |
| 330 | } |
| 331 | /** Convert a fixed point value to integer given its precision. |
| 332 | * |
| 333 | * @param[in] val Fixed point value |
| 334 | * @param[in] p Fixed point precision |
| 335 | * |
| 336 | * @return Integer of the fixed point number |
| 337 | */ |
| 338 | static constexpr T to_int(T val, uint8_t p) |
| 339 | { |
| 340 | return val >> p; |
| 341 | } |
| 342 | /** Convert a single precision floating point value to a fixed point representation given its precision. |
| 343 | * |
| 344 | * @param[in] val Floating point value |
| 345 | * @param[in] p Fixed point precision |
| 346 | * |
| 347 | * @return The raw fixed point representation |
| 348 | */ |
| 349 | static constexpr T to_fixed(float val, uint8_t p) |
| 350 | { |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 351 | return static_cast<T>(saturate_cast<float>(val * fixed_one(p) + ((val >= 0) ? 0.5 : -0.5))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 352 | } |
| 353 | /** Clamp value between two ranges |
| 354 | * |
| 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 |
| 358 | * |
| 359 | * @return clamped value |
| 360 | */ |
| 361 | static constexpr T clamp(T val, T min, T max) |
| 362 | { |
| 363 | return std::min(std::max(val, min), max); |
| 364 | } |
| 365 | /** Saturate given number |
| 366 | * |
| 367 | * @param[in] val Value to saturate |
| 368 | * |
| 369 | * @return Saturated value |
| 370 | */ |
| 371 | template <typename U> |
| 372 | static constexpr T saturate_cast(U val) |
| 373 | { |
| 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()))); |
| 375 | } |
| 376 | }; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 377 | /** Functions */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 378 | struct functions |
| 379 | { |
| 380 | /** Output stream operator |
| 381 | * |
| 382 | * @param[in] s Output stream |
| 383 | * @param[in] x Fixed point value |
| 384 | * |
| 385 | * @return Reference output to updated stream |
| 386 | */ |
| 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) |
| 389 | { |
| 390 | return s << static_cast<float>(x); |
| 391 | } |
| 392 | /** Signbit of a fixed point number. |
| 393 | * |
| 394 | * @param[in] x Fixed point number |
| 395 | * |
| 396 | * @return True if negative else false. |
| 397 | */ |
| 398 | template <typename T> |
| 399 | static bool signbit(fixed_point<T> x) |
| 400 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 401 | return ((x.raw() >> std::numeric_limits<T>::digits) != 0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 402 | } |
| 403 | /** Checks if two fixed point numbers are equal |
| 404 | * |
| 405 | * @param[in] x First fixed point operand |
| 406 | * @param[in] y Second fixed point operand |
| 407 | * |
| 408 | * @return True if fixed points are equal else false |
| 409 | */ |
| 410 | template <typename T> |
| 411 | static bool isequal(fixed_point<T> x, fixed_point<T> y) |
| 412 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 413 | uint8_t p = std::min(x.precision(), y.precision()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 414 | x.rescale(p); |
| 415 | y.rescale(p); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 416 | return (x.raw() == y.raw()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 417 | } |
| 418 | /** Checks if two fixed point number are not equal |
| 419 | * |
| 420 | * @param[in] x First fixed point operand |
| 421 | * @param[in] y Second fixed point operand |
| 422 | * |
| 423 | * @return True if fixed points are not equal else false |
| 424 | */ |
| 425 | template <typename T> |
| 426 | static bool isnotequal(fixed_point<T> x, fixed_point<T> y) |
| 427 | { |
| 428 | return !isequal(x, y); |
| 429 | } |
| 430 | /** Checks if one fixed point is greater than the other |
| 431 | * |
| 432 | * @param[in] x First fixed point operand |
| 433 | * @param[in] y Second fixed point operand |
| 434 | * |
| 435 | * @return True if fixed point is greater than other |
| 436 | */ |
| 437 | template <typename T> |
| 438 | static bool isgreater(fixed_point<T> x, fixed_point<T> y) |
| 439 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 440 | uint8_t p = std::min(x.precision(), y.precision()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 441 | x.rescale(p); |
| 442 | y.rescale(p); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 443 | return (x.raw() > y.raw()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 444 | } |
| 445 | /** Checks if one fixed point is greater or equal than the other |
| 446 | * |
| 447 | * @param[in] x First fixed point operand |
| 448 | * @param[in] y Second fixed point operand |
| 449 | * |
| 450 | * @return True if fixed point is greater or equal than other |
| 451 | */ |
| 452 | template <typename T> |
| 453 | static bool isgreaterequal(fixed_point<T> x, fixed_point<T> y) |
| 454 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 455 | uint8_t p = std::min(x.precision(), y.precision()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 456 | x.rescale(p); |
| 457 | y.rescale(p); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 458 | return (x.raw() >= y.raw()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 459 | } |
| 460 | /** Checks if one fixed point is less than the other |
| 461 | * |
| 462 | * @param[in] x First fixed point operand |
| 463 | * @param[in] y Second fixed point operand |
| 464 | * |
| 465 | * @return True if fixed point is less than other |
| 466 | */ |
| 467 | template <typename T> |
| 468 | static bool isless(fixed_point<T> x, fixed_point<T> y) |
| 469 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 470 | uint8_t p = std::min(x.precision(), y.precision()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 471 | x.rescale(p); |
| 472 | y.rescale(p); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 473 | return (x.raw() < y.raw()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 474 | } |
| 475 | /** Checks if one fixed point is less or equal than the other |
| 476 | * |
| 477 | * @param[in] x First fixed point operand |
| 478 | * @param[in] y Second fixed point operand |
| 479 | * |
| 480 | * @return True if fixed point is less or equal than other |
| 481 | */ |
| 482 | template <typename T> |
| 483 | static bool islessequal(fixed_point<T> x, fixed_point<T> y) |
| 484 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 485 | uint8_t p = std::min(x.precision(), y.precision()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 486 | x.rescale(p); |
| 487 | y.rescale(p); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 488 | return (x.raw() <= y.raw()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 489 | } |
| 490 | /** Checks if one fixed point is less or greater than the other |
| 491 | * |
| 492 | * @param[in] x First fixed point operand |
| 493 | * @param[in] y Second fixed point operand |
| 494 | * |
| 495 | * @return True if fixed point is less or greater than other |
| 496 | */ |
| 497 | template <typename T> |
| 498 | static bool islessgreater(fixed_point<T> x, fixed_point<T> y) |
| 499 | { |
| 500 | return isnotequal(x, y); |
| 501 | } |
| 502 | /** Clamp fixed point to specific range. |
| 503 | * |
| 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 |
| 507 | * |
| 508 | * @return Clamped result |
| 509 | */ |
| 510 | template <typename T> |
| 511 | static fixed_point<T> clamp(fixed_point<T> x, T min, T max) |
| 512 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 513 | return fixed_point<T>(constant_expr<T>::clamp(x.raw(), min, max), x.precision(), true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 514 | } |
| 515 | /** Negate number |
| 516 | * |
| 517 | * @param[in] x Fixed point operand |
| 518 | * |
| 519 | * @return Negated fixed point result |
| 520 | */ |
| 521 | template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T> |
| 522 | static fixed_point<T> negate(fixed_point<T> x) |
| 523 | { |
| 524 | using promoted_T = typename traits::promote<T>::type; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 525 | promoted_T val = -x.raw(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 526 | if(OP == OverflowPolicy::SATURATE) |
| 527 | { |
| 528 | val = constant_expr<T>::saturate_cast(val); |
| 529 | } |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 530 | return fixed_point<T>(static_cast<T>(val), x.precision(), true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 531 | } |
| 532 | /** Perform addition among two fixed point numbers |
| 533 | * |
| 534 | * @param[in] x First fixed point operand |
| 535 | * @param[in] y Second fixed point operand |
| 536 | * |
| 537 | * @return Result fixed point with precision equal to minimum precision of both operands |
| 538 | */ |
| 539 | template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T> |
| 540 | static fixed_point<T> add(fixed_point<T> x, fixed_point<T> y) |
| 541 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 542 | uint8_t p = std::min(x.precision(), y.precision()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 543 | x.rescale(p); |
| 544 | y.rescale(p); |
| 545 | if(OP == OverflowPolicy::SATURATE) |
| 546 | { |
| 547 | using type = typename traits::promote<T>::type; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 548 | type val = static_cast<type>(x.raw()) + static_cast<type>(y.raw()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 549 | val = constant_expr<T>::saturate_cast(val); |
| 550 | return fixed_point<T>(static_cast<T>(val), p, true); |
| 551 | } |
| 552 | else |
| 553 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 554 | return fixed_point<T>(x.raw() + y.raw(), p, true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | /** Perform subtraction among two fixed point numbers |
| 558 | * |
| 559 | * @param[in] x First fixed point operand |
| 560 | * @param[in] y Second fixed point operand |
| 561 | * |
| 562 | * @return Result fixed point with precision equal to minimum precision of both operands |
| 563 | */ |
| 564 | template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T> |
| 565 | static fixed_point<T> sub(fixed_point<T> x, fixed_point<T> y) |
| 566 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 567 | uint8_t p = std::min(x.precision(), y.precision()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 568 | x.rescale(p); |
| 569 | y.rescale(p); |
| 570 | if(OP == OverflowPolicy::SATURATE) |
| 571 | { |
| 572 | using type = typename traits::promote<T>::type; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 573 | type val = static_cast<type>(x.raw()) - static_cast<type>(y.raw()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 574 | val = constant_expr<T>::saturate_cast(val); |
| 575 | return fixed_point<T>(static_cast<T>(val), p, true); |
| 576 | } |
| 577 | else |
| 578 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 579 | return fixed_point<T>(x.raw() - y.raw(), p, true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | /** Perform multiplication among two fixed point numbers |
| 583 | * |
| 584 | * @param[in] x First fixed point operand |
| 585 | * @param[in] y Second fixed point operand |
| 586 | * |
| 587 | * @return Result fixed point with precision equal to minimum precision of both operands |
| 588 | */ |
| 589 | template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T> |
| 590 | static fixed_point<T> mul(fixed_point<T> x, fixed_point<T> y) |
| 591 | { |
| 592 | using promoted_T = typename traits::promote<T>::type; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 593 | uint8_t p_min = std::min(x.precision(), y.precision()); |
| 594 | uint8_t p_max = std::max(x.precision(), y.precision()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 595 | promoted_T round_factor = (1 << (p_max - 1)); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 596 | promoted_T val = ((static_cast<promoted_T>(x.raw()) * static_cast<promoted_T>(y.raw())) + round_factor) >> p_max; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 597 | if(OP == OverflowPolicy::SATURATE) |
| 598 | { |
| 599 | val = constant_expr<T>::saturate_cast(val); |
| 600 | } |
| 601 | return fixed_point<T>(static_cast<T>(val), p_min, true); |
| 602 | } |
| 603 | /** Perform division among two fixed point numbers |
| 604 | * |
| 605 | * @param[in] x First fixed point operand |
| 606 | * @param[in] y Second fixed point operand |
| 607 | * |
| 608 | * @return Result fixed point with precision equal to minimum precision of both operands |
| 609 | */ |
| 610 | template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T> |
| 611 | static fixed_point<T> div(fixed_point<T> x, fixed_point<T> y) |
| 612 | { |
| 613 | using promoted_T = typename traits::promote<T>::type; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 614 | uint8_t p = std::min(x.precision(), y.precision()); |
| 615 | promoted_T denom = static_cast<promoted_T>(y.raw()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 616 | if(denom != 0) |
| 617 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 618 | promoted_T val = (static_cast<promoted_T>(x.raw()) << std::max(x.precision(), y.precision())) / denom; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 619 | if(OP == OverflowPolicy::SATURATE) |
| 620 | { |
| 621 | val = constant_expr<T>::saturate_cast(val); |
| 622 | } |
| 623 | return fixed_point<T>(static_cast<T>(val), p, true); |
| 624 | } |
| 625 | else |
| 626 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 627 | T val = (x.raw() < 0) ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 628 | return fixed_point<T>(val, p, true); |
| 629 | } |
| 630 | } |
| 631 | /** Shift left |
| 632 | * |
| 633 | * @param[in] x Fixed point operand |
| 634 | * @param[in] shift Shift value |
| 635 | * |
| 636 | * @return Shifted value |
| 637 | */ |
| 638 | template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T> |
| 639 | static fixed_point<T> shift_left(fixed_point<T> x, size_t shift) |
| 640 | { |
| 641 | using promoted_T = typename traits::promote<T>::type; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 642 | promoted_T val = static_cast<promoted_T>(x.raw()) << shift; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 643 | if(OP == OverflowPolicy::SATURATE) |
| 644 | { |
| 645 | val = constant_expr<T>::saturate_cast(val); |
| 646 | } |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 647 | return fixed_point<T>(static_cast<T>(val), x.precision(), true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 648 | } |
| 649 | /** Shift right |
| 650 | * |
| 651 | * @param[in] x Fixed point operand |
| 652 | * @param[in] shift Shift value |
| 653 | * |
| 654 | * @return Shifted value |
| 655 | */ |
| 656 | template <typename T> |
| 657 | static fixed_point<T> shift_right(fixed_point<T> x, size_t shift) |
| 658 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 659 | return fixed_point<T>(x.raw() >> shift, x.precision(), true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 660 | } |
| 661 | /** Calculate absolute value |
| 662 | * |
| 663 | * @param[in] x Fixed point operand |
| 664 | * |
| 665 | * @return Absolute value of operand |
| 666 | */ |
| 667 | template <typename T> |
| 668 | static fixed_point<T> abs(fixed_point<T> x) |
| 669 | { |
| 670 | using promoted_T = typename traits::promote<T>::type; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 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); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 673 | } |
| 674 | /** Calculate the logarithm of a fixed point number |
| 675 | * |
| 676 | * @param[in] x Fixed point operand |
| 677 | * |
| 678 | * @return Logarithm value of operand |
| 679 | */ |
| 680 | template <typename T> |
| 681 | static fixed_point<T> log(fixed_point<T> x) |
| 682 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 683 | uint8_t p = x.precision(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 684 | auto const_one = fixed_point<T>(static_cast<T>(1), p); |
| 685 | |
| 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))) |
| 689 | { |
| 690 | return fixed_point<T>(static_cast<T>(0), p, true); |
| 691 | } |
| 692 | else if(isless(x, const_one)) |
| 693 | { |
| 694 | return mul(log(div(const_one, x)), fixed_point<T>(-1, p)); |
| 695 | } |
| 696 | |
| 697 | // Remove even powers of 2 |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 698 | T shift_val = 31 - __builtin_clz(x.raw() >> p); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 699 | x = shift_right(x, shift_val); |
| 700 | x = sub(x, const_one); |
| 701 | |
| 702 | // Constants |
| 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); |
| 708 | |
| 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); |
| 713 | sum = mul(x, sum); |
| 714 | |
| 715 | return mul(add(sum, fixed_point<T>(static_cast<T>(shift_val), p)), ln2); |
| 716 | } |
| 717 | /** Calculate the exponential of a fixed point number. |
| 718 | * |
| 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)) |
| 722 | * |
| 723 | * @param[in] x Fixed point operand |
| 724 | * |
| 725 | * @return Exponential value of operand |
| 726 | */ |
| 727 | template <typename T> |
| 728 | static fixed_point<T> exp(fixed_point<T> x) |
| 729 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 730 | uint8_t p = x.precision(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 731 | // Constants |
| 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); |
| 739 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 740 | T scaled_int_part = detail::constant_expr<T>::to_int(mul(x, inv_ln2).raw(), p); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 741 | |
| 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); |
| 749 | |
| 750 | // Saturate value |
| 751 | if(static_cast<T>(clz(taylor.raw())) <= scaled_int_part) |
| 752 | { |
| 753 | return fixed_point<T>(std::numeric_limits<T>::max(), p, true); |
| 754 | } |
| 755 | |
| 756 | return (scaled_int_part < 0) ? shift_right(taylor, -scaled_int_part) : shift_left(taylor, scaled_int_part); |
| 757 | } |
| 758 | /** Calculate the inverse square root of a fixed point number |
| 759 | * |
| 760 | * @param[in] x Fixed point operand |
| 761 | * |
| 762 | * @return Inverse square root value of operand |
| 763 | */ |
| 764 | template <typename T> |
| 765 | static fixed_point<T> inv_sqrt(fixed_point<T> x) |
| 766 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 767 | const uint8_t p = x.precision(); |
| 768 | int8_t shift = std::numeric_limits<T>::digits - (p + detail::clz(x.raw())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 769 | |
| 770 | shift += std::numeric_limits<T>::is_signed ? 1 : 0; |
| 771 | |
Georgios Pinitas | 6410fb2 | 2017-07-03 14:38:50 +0100 | [diff] [blame] | 772 | // Use volatile to restrict compiler optimizations on shift as compiler reports maybe-uninitialized error on Android |
| 773 | volatile int8_t *shift_ptr = &shift; |
| 774 | |
| 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; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 778 | |
Michalis Spyrou | 0a8334c | 2017-06-14 18:00:05 +0100 | [diff] [blame] | 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) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 782 | { |
Georgios Pinitas | 6410fb2 | 2017-07-03 14:38:50 +0100 | [diff] [blame] | 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); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 785 | } |
| 786 | |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 787 | return (shift < 0) ? shift_left(x2, (-shift) >> 1) : shift_right(x2, shift >> 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 788 | } |
| 789 | /** Calculate the hyperbolic tangent of a fixed point number |
| 790 | * |
| 791 | * @param[in] x Fixed point operand |
| 792 | * |
| 793 | * @return Hyperbolic tangent of the operand |
| 794 | */ |
| 795 | template <typename T> |
| 796 | static fixed_point<T> tanh(fixed_point<T> x) |
| 797 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 798 | uint8_t p = x.precision(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 799 | // Constants |
| 800 | auto const_one = fixed_point<T>(1, p); |
| 801 | auto const_two = fixed_point<T>(2, p); |
| 802 | |
| 803 | auto exp2x = exp(const_two * x); |
| 804 | auto num = exp2x - const_one; |
| 805 | auto den = exp2x + const_one; |
| 806 | auto tanh = num / den; |
| 807 | |
| 808 | return tanh; |
| 809 | } |
| 810 | /** Calculate the a-th power of a fixed point number. |
| 811 | * |
| 812 | * The power is computed as x^a = e^(log(x) * a) |
| 813 | * |
| 814 | * @param[in] x Fixed point operand |
| 815 | * @param[in] a Fixed point exponent |
| 816 | * |
| 817 | * @return a-th power of the operand |
| 818 | */ |
| 819 | template <typename T> |
| 820 | static fixed_point<T> pow(fixed_point<T> x, fixed_point<T> a) |
| 821 | { |
| 822 | return exp(log(x) * a); |
| 823 | } |
| 824 | }; |
| 825 | |
| 826 | template <typename T> |
| 827 | bool operator==(const fixed_point<T> &lhs, const fixed_point<T> &rhs) |
| 828 | { |
| 829 | return functions::isequal(lhs, rhs); |
| 830 | } |
| 831 | template <typename T> |
| 832 | bool operator!=(const fixed_point<T> &lhs, const fixed_point<T> &rhs) |
| 833 | { |
| 834 | return !operator==(lhs, rhs); |
| 835 | } |
| 836 | template <typename T> |
| 837 | bool operator<(const fixed_point<T> &lhs, const fixed_point<T> &rhs) |
| 838 | { |
| 839 | return functions::isless(lhs, rhs); |
| 840 | } |
| 841 | template <typename T> |
| 842 | bool operator>(const fixed_point<T> &lhs, const fixed_point<T> &rhs) |
| 843 | { |
| 844 | return operator<(rhs, lhs); |
| 845 | } |
| 846 | template <typename T> |
| 847 | bool operator<=(const fixed_point<T> &lhs, const fixed_point<T> &rhs) |
| 848 | { |
| 849 | return !operator>(lhs, rhs); |
| 850 | } |
| 851 | template <typename T> |
| 852 | bool operator>=(const fixed_point<T> &lhs, const fixed_point<T> &rhs) |
| 853 | { |
| 854 | return !operator<(lhs, rhs); |
| 855 | } |
| 856 | template <typename T> |
| 857 | fixed_point<T> operator+(const fixed_point<T> &lhs, const fixed_point<T> &rhs) |
| 858 | { |
| 859 | return functions::add(lhs, rhs); |
| 860 | } |
| 861 | template <typename T> |
| 862 | fixed_point<T> operator-(const fixed_point<T> &lhs, const fixed_point<T> &rhs) |
| 863 | { |
| 864 | return functions::sub(lhs, rhs); |
| 865 | } |
| 866 | template <typename T> |
| 867 | fixed_point<T> operator-(const fixed_point<T> &rhs) |
| 868 | { |
| 869 | return functions::negate(rhs); |
| 870 | } |
| 871 | template <typename T> |
| 872 | fixed_point<T> operator*(fixed_point<T> x, fixed_point<T> y) |
| 873 | { |
| 874 | return functions::mul(x, y); |
| 875 | } |
| 876 | template <typename T> |
| 877 | fixed_point<T> operator/(fixed_point<T> x, fixed_point<T> y) |
| 878 | { |
| 879 | return functions::div(x, y); |
| 880 | } |
| 881 | template <typename T> |
| 882 | fixed_point<T> operator>>(fixed_point<T> x, size_t shift) |
| 883 | { |
| 884 | return functions::shift_right(x, shift); |
| 885 | } |
| 886 | template <typename T> |
| 887 | fixed_point<T> operator<<(fixed_point<T> x, size_t shift) |
| 888 | { |
| 889 | return functions::shift_left(x, shift); |
| 890 | } |
| 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) |
| 893 | { |
| 894 | return functions::write(s, x); |
| 895 | } |
| 896 | template <typename T> |
| 897 | inline fixed_point<T> min(fixed_point<T> x, fixed_point<T> y) |
| 898 | { |
| 899 | return x > y ? y : x; |
| 900 | } |
| 901 | template <typename T> |
| 902 | inline fixed_point<T> max(fixed_point<T> x, fixed_point<T> y) |
| 903 | { |
| 904 | return x > y ? x : y; |
| 905 | } |
| 906 | template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T> |
| 907 | inline fixed_point<T> add(fixed_point<T> x, fixed_point<T> y) |
| 908 | { |
| 909 | return functions::add<OP>(x, y); |
| 910 | } |
| 911 | template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T> |
| 912 | inline fixed_point<T> sub(fixed_point<T> x, fixed_point<T> y) |
| 913 | { |
| 914 | return functions::sub<OP>(x, y); |
| 915 | } |
| 916 | template <OverflowPolicy OP = OverflowPolicy::SATURATE, typename T> |
| 917 | inline fixed_point<T> mul(fixed_point<T> x, fixed_point<T> y) |
| 918 | { |
| 919 | return functions::mul<OP>(x, y); |
| 920 | } |
| 921 | template <typename T> |
| 922 | inline fixed_point<T> div(fixed_point<T> x, fixed_point<T> y) |
| 923 | { |
| 924 | return functions::div(x, y); |
| 925 | } |
| 926 | template <typename T> |
| 927 | inline fixed_point<T> abs(fixed_point<T> x) |
| 928 | { |
| 929 | return functions::abs(x); |
| 930 | } |
| 931 | template <typename T> |
| 932 | inline fixed_point<T> clamp(fixed_point<T> x, T min, T max) |
| 933 | { |
| 934 | return functions::clamp(x, min, max); |
| 935 | } |
| 936 | template <typename T> |
| 937 | inline fixed_point<T> exp(fixed_point<T> x) |
| 938 | { |
| 939 | return functions::exp(x); |
| 940 | } |
| 941 | template <typename T> |
| 942 | inline fixed_point<T> log(fixed_point<T> x) |
| 943 | { |
| 944 | return functions::log(x); |
| 945 | } |
| 946 | template <typename T> |
| 947 | inline fixed_point<T> inv_sqrt(fixed_point<T> x) |
| 948 | { |
| 949 | return functions::inv_sqrt(x); |
| 950 | } |
| 951 | template <typename T> |
| 952 | inline fixed_point<T> tanh(fixed_point<T> x) |
| 953 | { |
| 954 | return functions::tanh(x); |
| 955 | } |
| 956 | template <typename T> |
| 957 | inline fixed_point<T> pow(fixed_point<T> x, fixed_point<T> a) |
| 958 | { |
| 959 | return functions::pow(x, a); |
| 960 | } |
| 961 | } // namespace detail |
| 962 | |
| 963 | // Expose operators |
| 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<<; |
| 976 | |
| 977 | // Expose additional functions |
| 978 | using detail::min; |
| 979 | using detail::max; |
| 980 | using detail::add; |
| 981 | using detail::sub; |
| 982 | using detail::mul; |
| 983 | using detail::div; |
| 984 | using detail::abs; |
| 985 | using detail::clamp; |
| 986 | using detail::exp; |
| 987 | using detail::log; |
| 988 | using detail::inv_sqrt; |
| 989 | using detail::tanh; |
| 990 | using detail::pow; |
| 991 | // TODO: floor |
| 992 | // TODO: ceil |
| 993 | // TODO: sqrt |
| 994 | } // namespace fixed_point_arithmetic |
| 995 | } // namespace test |
| 996 | } // namespace arm_compute |
| 997 | #endif /*__ARM_COMPUTE_TEST_VALIDATION_FIXEDPOINT_H__ */ |