diff options
Diffstat (limited to 'src/common/fixed_point.h')
| -rw-r--r-- | src/common/fixed_point.h | 274 |
1 files changed, 110 insertions, 164 deletions
diff --git a/src/common/fixed_point.h b/src/common/fixed_point.h index 4a0f72cc9..f899b0d54 100644 --- a/src/common/fixed_point.h +++ b/src/common/fixed_point.h | |||
| @@ -4,14 +4,7 @@ | |||
| 4 | // From: https://github.com/eteran/cpp-utilities/blob/master/fixed/include/cpp-utilities/fixed.h | 4 | // From: https://github.com/eteran/cpp-utilities/blob/master/fixed/include/cpp-utilities/fixed.h |
| 5 | // See also: http://stackoverflow.com/questions/79677/whats-the-best-way-to-do-fixed-point-math | 5 | // See also: http://stackoverflow.com/questions/79677/whats-the-best-way-to-do-fixed-point-math |
| 6 | 6 | ||
| 7 | #ifndef FIXED_H_ | 7 | #pragma once |
| 8 | #define FIXED_H_ | ||
| 9 | |||
| 10 | #if __cplusplus >= 201402L | ||
| 11 | #define CONSTEXPR14 constexpr | ||
| 12 | #else | ||
| 13 | #define CONSTEXPR14 | ||
| 14 | #endif | ||
| 15 | 8 | ||
| 16 | #include <cstddef> // for size_t | 9 | #include <cstddef> // for size_t |
| 17 | #include <cstdint> | 10 | #include <cstdint> |
| @@ -19,6 +12,8 @@ | |||
| 19 | #include <ostream> | 12 | #include <ostream> |
| 20 | #include <type_traits> | 13 | #include <type_traits> |
| 21 | 14 | ||
| 15 | #include <common/concepts.h> | ||
| 16 | |||
| 22 | namespace Common { | 17 | namespace Common { |
| 23 | 18 | ||
| 24 | template <size_t I, size_t F> | 19 | template <size_t I, size_t F> |
| @@ -57,8 +52,8 @@ struct type_from_size<64> { | |||
| 57 | static constexpr size_t size = 64; | 52 | static constexpr size_t size = 64; |
| 58 | 53 | ||
| 59 | using value_type = int64_t; | 54 | using value_type = int64_t; |
| 60 | using unsigned_type = std::make_unsigned<value_type>::type; | 55 | using unsigned_type = std::make_unsigned_t<value_type>; |
| 61 | using signed_type = std::make_signed<value_type>::type; | 56 | using signed_type = std::make_signed_t<value_type>; |
| 62 | using next_size = type_from_size<128>; | 57 | using next_size = type_from_size<128>; |
| 63 | }; | 58 | }; |
| 64 | 59 | ||
| @@ -68,8 +63,8 @@ struct type_from_size<32> { | |||
| 68 | static constexpr size_t size = 32; | 63 | static constexpr size_t size = 32; |
| 69 | 64 | ||
| 70 | using value_type = int32_t; | 65 | using value_type = int32_t; |
| 71 | using unsigned_type = std::make_unsigned<value_type>::type; | 66 | using unsigned_type = std::make_unsigned_t<value_type>; |
| 72 | using signed_type = std::make_signed<value_type>::type; | 67 | using signed_type = std::make_signed_t<value_type>; |
| 73 | using next_size = type_from_size<64>; | 68 | using next_size = type_from_size<64>; |
| 74 | }; | 69 | }; |
| 75 | 70 | ||
| @@ -79,8 +74,8 @@ struct type_from_size<16> { | |||
| 79 | static constexpr size_t size = 16; | 74 | static constexpr size_t size = 16; |
| 80 | 75 | ||
| 81 | using value_type = int16_t; | 76 | using value_type = int16_t; |
| 82 | using unsigned_type = std::make_unsigned<value_type>::type; | 77 | using unsigned_type = std::make_unsigned_t<value_type>; |
| 83 | using signed_type = std::make_signed<value_type>::type; | 78 | using signed_type = std::make_signed_t<value_type>; |
| 84 | using next_size = type_from_size<32>; | 79 | using next_size = type_from_size<32>; |
| 85 | }; | 80 | }; |
| 86 | 81 | ||
| @@ -90,8 +85,8 @@ struct type_from_size<8> { | |||
| 90 | static constexpr size_t size = 8; | 85 | static constexpr size_t size = 8; |
| 91 | 86 | ||
| 92 | using value_type = int8_t; | 87 | using value_type = int8_t; |
| 93 | using unsigned_type = std::make_unsigned<value_type>::type; | 88 | using unsigned_type = std::make_unsigned_t<value_type>; |
| 94 | using signed_type = std::make_signed<value_type>::type; | 89 | using signed_type = std::make_signed_t<value_type>; |
| 95 | using next_size = type_from_size<16>; | 90 | using next_size = type_from_size<16>; |
| 96 | }; | 91 | }; |
| 97 | 92 | ||
| @@ -106,9 +101,9 @@ constexpr B next_to_base(N rhs) { | |||
| 106 | struct divide_by_zero : std::exception {}; | 101 | struct divide_by_zero : std::exception {}; |
| 107 | 102 | ||
| 108 | template <size_t I, size_t F> | 103 | template <size_t I, size_t F> |
| 109 | CONSTEXPR14 FixedPoint<I, F> divide( | 104 | constexpr FixedPoint<I, F> divide( |
| 110 | FixedPoint<I, F> numerator, FixedPoint<I, F> denominator, FixedPoint<I, F>& remainder, | 105 | FixedPoint<I, F> numerator, FixedPoint<I, F> denominator, FixedPoint<I, F>& remainder, |
| 111 | typename std::enable_if<type_from_size<I + F>::next_size::is_specialized>::type* = nullptr) { | 106 | std::enable_if_t<type_from_size<I + F>::next_size::is_specialized>* = nullptr) { |
| 112 | 107 | ||
| 113 | using next_type = typename FixedPoint<I, F>::next_type; | 108 | using next_type = typename FixedPoint<I, F>::next_type; |
| 114 | using base_type = typename FixedPoint<I, F>::base_type; | 109 | using base_type = typename FixedPoint<I, F>::base_type; |
| @@ -126,9 +121,9 @@ CONSTEXPR14 FixedPoint<I, F> divide( | |||
| 126 | } | 121 | } |
| 127 | 122 | ||
| 128 | template <size_t I, size_t F> | 123 | template <size_t I, size_t F> |
| 129 | CONSTEXPR14 FixedPoint<I, F> divide( | 124 | constexpr FixedPoint<I, F> divide( |
| 130 | FixedPoint<I, F> numerator, FixedPoint<I, F> denominator, FixedPoint<I, F>& remainder, | 125 | FixedPoint<I, F> numerator, FixedPoint<I, F> denominator, FixedPoint<I, F>& remainder, |
| 131 | typename std::enable_if<!type_from_size<I + F>::next_size::is_specialized>::type* = nullptr) { | 126 | std::enable_if_t<!type_from_size<I + F>::next_size::is_specialized>* = nullptr) { |
| 132 | 127 | ||
| 133 | using unsigned_type = typename FixedPoint<I, F>::unsigned_type; | 128 | using unsigned_type = typename FixedPoint<I, F>::unsigned_type; |
| 134 | 129 | ||
| @@ -196,9 +191,9 @@ CONSTEXPR14 FixedPoint<I, F> divide( | |||
| 196 | 191 | ||
| 197 | // this is the usual implementation of multiplication | 192 | // this is the usual implementation of multiplication |
| 198 | template <size_t I, size_t F> | 193 | template <size_t I, size_t F> |
| 199 | CONSTEXPR14 FixedPoint<I, F> multiply( | 194 | constexpr FixedPoint<I, F> multiply( |
| 200 | FixedPoint<I, F> lhs, FixedPoint<I, F> rhs, | 195 | FixedPoint<I, F> lhs, FixedPoint<I, F> rhs, |
| 201 | typename std::enable_if<type_from_size<I + F>::next_size::is_specialized>::type* = nullptr) { | 196 | std::enable_if_t<type_from_size<I + F>::next_size::is_specialized>* = nullptr) { |
| 202 | 197 | ||
| 203 | using next_type = typename FixedPoint<I, F>::next_type; | 198 | using next_type = typename FixedPoint<I, F>::next_type; |
| 204 | using base_type = typename FixedPoint<I, F>::base_type; | 199 | using base_type = typename FixedPoint<I, F>::base_type; |
| @@ -215,9 +210,9 @@ CONSTEXPR14 FixedPoint<I, F> multiply( | |||
| 215 | // it is slightly slower, but is more robust since it doesn't | 210 | // it is slightly slower, but is more robust since it doesn't |
| 216 | // require and upgraded type | 211 | // require and upgraded type |
| 217 | template <size_t I, size_t F> | 212 | template <size_t I, size_t F> |
| 218 | CONSTEXPR14 FixedPoint<I, F> multiply( | 213 | constexpr FixedPoint<I, F> multiply( |
| 219 | FixedPoint<I, F> lhs, FixedPoint<I, F> rhs, | 214 | FixedPoint<I, F> lhs, FixedPoint<I, F> rhs, |
| 220 | typename std::enable_if<!type_from_size<I + F>::next_size::is_specialized>::type* = nullptr) { | 215 | std::enable_if_t<!type_from_size<I + F>::next_size::is_specialized>* = nullptr) { |
| 221 | 216 | ||
| 222 | using base_type = typename FixedPoint<I, F>::base_type; | 217 | using base_type = typename FixedPoint<I, F>::base_type; |
| 223 | 218 | ||
| @@ -272,19 +267,20 @@ public: | |||
| 272 | static constexpr base_type one = base_type(1) << fractional_bits; | 267 | static constexpr base_type one = base_type(1) << fractional_bits; |
| 273 | 268 | ||
| 274 | public: // constructors | 269 | public: // constructors |
| 275 | FixedPoint() = default; | 270 | constexpr FixedPoint() = default; |
| 276 | FixedPoint(const FixedPoint&) = default; | 271 | |
| 277 | FixedPoint(FixedPoint&&) = default; | 272 | constexpr FixedPoint(const FixedPoint&) = default; |
| 278 | FixedPoint& operator=(const FixedPoint&) = default; | 273 | constexpr FixedPoint& operator=(const FixedPoint&) = default; |
| 274 | |||
| 275 | constexpr FixedPoint(FixedPoint&&) noexcept = default; | ||
| 276 | constexpr FixedPoint& operator=(FixedPoint&&) noexcept = default; | ||
| 279 | 277 | ||
| 280 | template <class Number> | 278 | template <IsArithmetic Number> |
| 281 | constexpr FixedPoint( | 279 | constexpr FixedPoint(Number n) : data_(static_cast<base_type>(n * one)) {} |
| 282 | Number n, typename std::enable_if<std::is_arithmetic<Number>::value>::type* = nullptr) | ||
| 283 | : data_(static_cast<base_type>(n * one)) {} | ||
| 284 | 280 | ||
| 285 | public: // conversion | 281 | public: // conversion |
| 286 | template <size_t I2, size_t F2> | 282 | template <size_t I2, size_t F2> |
| 287 | CONSTEXPR14 explicit FixedPoint(FixedPoint<I2, F2> other) { | 283 | constexpr explicit FixedPoint(FixedPoint<I2, F2> other) { |
| 288 | static_assert(I2 <= I && F2 <= F, "Scaling conversion can only upgrade types"); | 284 | static_assert(I2 <= I && F2 <= F, "Scaling conversion can only upgrade types"); |
| 289 | using T = FixedPoint<I2, F2>; | 285 | using T = FixedPoint<I2, F2>; |
| 290 | 286 | ||
| @@ -308,36 +304,14 @@ public: | |||
| 308 | } | 304 | } |
| 309 | 305 | ||
| 310 | public: // comparison operators | 306 | public: // comparison operators |
| 311 | constexpr bool operator==(FixedPoint rhs) const { | 307 | friend constexpr auto operator<=>(FixedPoint lhs, FixedPoint rhs) = default; |
| 312 | return data_ == rhs.data_; | ||
| 313 | } | ||
| 314 | |||
| 315 | constexpr bool operator!=(FixedPoint rhs) const { | ||
| 316 | return data_ != rhs.data_; | ||
| 317 | } | ||
| 318 | |||
| 319 | constexpr bool operator<(FixedPoint rhs) const { | ||
| 320 | return data_ < rhs.data_; | ||
| 321 | } | ||
| 322 | |||
| 323 | constexpr bool operator>(FixedPoint rhs) const { | ||
| 324 | return data_ > rhs.data_; | ||
| 325 | } | ||
| 326 | |||
| 327 | constexpr bool operator<=(FixedPoint rhs) const { | ||
| 328 | return data_ <= rhs.data_; | ||
| 329 | } | ||
| 330 | |||
| 331 | constexpr bool operator>=(FixedPoint rhs) const { | ||
| 332 | return data_ >= rhs.data_; | ||
| 333 | } | ||
| 334 | 308 | ||
| 335 | public: // unary operators | 309 | public: // unary operators |
| 336 | constexpr bool operator!() const { | 310 | [[nodiscard]] constexpr bool operator!() const { |
| 337 | return !data_; | 311 | return !data_; |
| 338 | } | 312 | } |
| 339 | 313 | ||
| 340 | constexpr FixedPoint operator~() const { | 314 | [[nodiscard]] constexpr FixedPoint operator~() const { |
| 341 | // NOTE(eteran): this will often appear to "just negate" the value | 315 | // NOTE(eteran): this will often appear to "just negate" the value |
| 342 | // that is not an error, it is because -x == (~x+1) | 316 | // that is not an error, it is because -x == (~x+1) |
| 343 | // and that "+1" is adding an infinitesimally small fraction to the | 317 | // and that "+1" is adding an infinitesimally small fraction to the |
| @@ -345,89 +319,87 @@ public: // unary operators | |||
| 345 | return FixedPoint::from_base(~data_); | 319 | return FixedPoint::from_base(~data_); |
| 346 | } | 320 | } |
| 347 | 321 | ||
| 348 | constexpr FixedPoint operator-() const { | 322 | [[nodiscard]] constexpr FixedPoint operator-() const { |
| 349 | return FixedPoint::from_base(-data_); | 323 | return FixedPoint::from_base(-data_); |
| 350 | } | 324 | } |
| 351 | 325 | ||
| 352 | constexpr FixedPoint operator+() const { | 326 | [[nodiscard]] constexpr FixedPoint operator+() const { |
| 353 | return FixedPoint::from_base(+data_); | 327 | return FixedPoint::from_base(+data_); |
| 354 | } | 328 | } |
| 355 | 329 | ||
| 356 | CONSTEXPR14 FixedPoint& operator++() { | 330 | constexpr FixedPoint& operator++() { |
| 357 | data_ += one; | 331 | data_ += one; |
| 358 | return *this; | 332 | return *this; |
| 359 | } | 333 | } |
| 360 | 334 | ||
| 361 | CONSTEXPR14 FixedPoint& operator--() { | 335 | constexpr FixedPoint& operator--() { |
| 362 | data_ -= one; | 336 | data_ -= one; |
| 363 | return *this; | 337 | return *this; |
| 364 | } | 338 | } |
| 365 | 339 | ||
| 366 | CONSTEXPR14 FixedPoint operator++(int) { | 340 | constexpr FixedPoint operator++(int) { |
| 367 | FixedPoint tmp(*this); | 341 | FixedPoint tmp(*this); |
| 368 | data_ += one; | 342 | data_ += one; |
| 369 | return tmp; | 343 | return tmp; |
| 370 | } | 344 | } |
| 371 | 345 | ||
| 372 | CONSTEXPR14 FixedPoint operator--(int) { | 346 | constexpr FixedPoint operator--(int) { |
| 373 | FixedPoint tmp(*this); | 347 | FixedPoint tmp(*this); |
| 374 | data_ -= one; | 348 | data_ -= one; |
| 375 | return tmp; | 349 | return tmp; |
| 376 | } | 350 | } |
| 377 | 351 | ||
| 378 | public: // basic math operators | 352 | public: // basic math operators |
| 379 | CONSTEXPR14 FixedPoint& operator+=(FixedPoint n) { | 353 | constexpr FixedPoint& operator+=(FixedPoint n) { |
| 380 | data_ += n.data_; | 354 | data_ += n.data_; |
| 381 | return *this; | 355 | return *this; |
| 382 | } | 356 | } |
| 383 | 357 | ||
| 384 | CONSTEXPR14 FixedPoint& operator-=(FixedPoint n) { | 358 | constexpr FixedPoint& operator-=(FixedPoint n) { |
| 385 | data_ -= n.data_; | 359 | data_ -= n.data_; |
| 386 | return *this; | 360 | return *this; |
| 387 | } | 361 | } |
| 388 | 362 | ||
| 389 | CONSTEXPR14 FixedPoint& operator*=(FixedPoint n) { | 363 | constexpr FixedPoint& operator*=(FixedPoint n) { |
| 390 | return assign(detail::multiply(*this, n)); | 364 | return assign(detail::multiply(*this, n)); |
| 391 | } | 365 | } |
| 392 | 366 | ||
| 393 | CONSTEXPR14 FixedPoint& operator/=(FixedPoint n) { | 367 | constexpr FixedPoint& operator/=(FixedPoint n) { |
| 394 | FixedPoint temp; | 368 | FixedPoint temp; |
| 395 | return assign(detail::divide(*this, n, temp)); | 369 | return assign(detail::divide(*this, n, temp)); |
| 396 | } | 370 | } |
| 397 | 371 | ||
| 398 | private: | 372 | private: |
| 399 | CONSTEXPR14 FixedPoint& assign(FixedPoint rhs) { | 373 | constexpr FixedPoint& assign(FixedPoint rhs) { |
| 400 | data_ = rhs.data_; | 374 | data_ = rhs.data_; |
| 401 | return *this; | 375 | return *this; |
| 402 | } | 376 | } |
| 403 | 377 | ||
| 404 | public: // binary math operators, effects underlying bit pattern since these | 378 | public: // binary math operators, effects underlying bit pattern since these |
| 405 | // don't really typically make sense for non-integer values | 379 | // don't really typically make sense for non-integer values |
| 406 | CONSTEXPR14 FixedPoint& operator&=(FixedPoint n) { | 380 | constexpr FixedPoint& operator&=(FixedPoint n) { |
| 407 | data_ &= n.data_; | 381 | data_ &= n.data_; |
| 408 | return *this; | 382 | return *this; |
| 409 | } | 383 | } |
| 410 | 384 | ||
| 411 | CONSTEXPR14 FixedPoint& operator|=(FixedPoint n) { | 385 | constexpr FixedPoint& operator|=(FixedPoint n) { |
| 412 | data_ |= n.data_; | 386 | data_ |= n.data_; |
| 413 | return *this; | 387 | return *this; |
| 414 | } | 388 | } |
| 415 | 389 | ||
| 416 | CONSTEXPR14 FixedPoint& operator^=(FixedPoint n) { | 390 | constexpr FixedPoint& operator^=(FixedPoint n) { |
| 417 | data_ ^= n.data_; | 391 | data_ ^= n.data_; |
| 418 | return *this; | 392 | return *this; |
| 419 | } | 393 | } |
| 420 | 394 | ||
| 421 | template <class Integer, | 395 | template <IsIntegral Integer> |
| 422 | class = typename std::enable_if<std::is_integral<Integer>::value>::type> | 396 | constexpr FixedPoint& operator>>=(Integer n) { |
| 423 | CONSTEXPR14 FixedPoint& operator>>=(Integer n) { | ||
| 424 | data_ >>= n; | 397 | data_ >>= n; |
| 425 | return *this; | 398 | return *this; |
| 426 | } | 399 | } |
| 427 | 400 | ||
| 428 | template <class Integer, | 401 | template <IsIntegral Integer> |
| 429 | class = typename std::enable_if<std::is_integral<Integer>::value>::type> | 402 | constexpr FixedPoint& operator<<=(Integer n) { |
| 430 | CONSTEXPR14 FixedPoint& operator<<=(Integer n) { | ||
| 431 | data_ <<= n; | 403 | data_ <<= n; |
| 432 | return *this; | 404 | return *this; |
| 433 | } | 405 | } |
| @@ -437,42 +409,42 @@ public: // conversion to basic types | |||
| 437 | data_ += (data_ & fractional_mask) >> 1; | 409 | data_ += (data_ & fractional_mask) >> 1; |
| 438 | } | 410 | } |
| 439 | 411 | ||
| 440 | constexpr int to_int() { | 412 | [[nodiscard]] constexpr int to_int() { |
| 441 | round_up(); | 413 | round_up(); |
| 442 | return static_cast<int>((data_ & integer_mask) >> fractional_bits); | 414 | return static_cast<int>((data_ & integer_mask) >> fractional_bits); |
| 443 | } | 415 | } |
| 444 | 416 | ||
| 445 | constexpr unsigned int to_uint() const { | 417 | [[nodiscard]] constexpr unsigned int to_uint() { |
| 446 | round_up(); | 418 | round_up(); |
| 447 | return static_cast<unsigned int>((data_ & integer_mask) >> fractional_bits); | 419 | return static_cast<unsigned int>((data_ & integer_mask) >> fractional_bits); |
| 448 | } | 420 | } |
| 449 | 421 | ||
| 450 | constexpr int64_t to_long() { | 422 | [[nodiscard]] constexpr int64_t to_long() { |
| 451 | round_up(); | 423 | round_up(); |
| 452 | return static_cast<int64_t>((data_ & integer_mask) >> fractional_bits); | 424 | return static_cast<int64_t>((data_ & integer_mask) >> fractional_bits); |
| 453 | } | 425 | } |
| 454 | 426 | ||
| 455 | constexpr int to_int_floor() const { | 427 | [[nodiscard]] constexpr int to_int_floor() const { |
| 456 | return static_cast<int>((data_ & integer_mask) >> fractional_bits); | 428 | return static_cast<int>((data_ & integer_mask) >> fractional_bits); |
| 457 | } | 429 | } |
| 458 | 430 | ||
| 459 | constexpr int64_t to_long_floor() { | 431 | [[nodiscard]] constexpr int64_t to_long_floor() const { |
| 460 | return static_cast<int64_t>((data_ & integer_mask) >> fractional_bits); | 432 | return static_cast<int64_t>((data_ & integer_mask) >> fractional_bits); |
| 461 | } | 433 | } |
| 462 | 434 | ||
| 463 | constexpr unsigned int to_uint_floor() const { | 435 | [[nodiscard]] constexpr unsigned int to_uint_floor() const { |
| 464 | return static_cast<unsigned int>((data_ & integer_mask) >> fractional_bits); | 436 | return static_cast<unsigned int>((data_ & integer_mask) >> fractional_bits); |
| 465 | } | 437 | } |
| 466 | 438 | ||
| 467 | constexpr float to_float() const { | 439 | [[nodiscard]] constexpr float to_float() const { |
| 468 | return static_cast<float>(data_) / FixedPoint::one; | 440 | return static_cast<float>(data_) / FixedPoint::one; |
| 469 | } | 441 | } |
| 470 | 442 | ||
| 471 | constexpr double to_double() const { | 443 | [[nodiscard]] constexpr double to_double() const { |
| 472 | return static_cast<double>(data_) / FixedPoint::one; | 444 | return static_cast<double>(data_) / FixedPoint::one; |
| 473 | } | 445 | } |
| 474 | 446 | ||
| 475 | constexpr base_type to_raw() const { | 447 | [[nodiscard]] constexpr base_type to_raw() const { |
| 476 | return data_; | 448 | return data_; |
| 477 | } | 449 | } |
| 478 | 450 | ||
| @@ -480,27 +452,27 @@ public: // conversion to basic types | |||
| 480 | data_ &= fractional_mask; | 452 | data_ &= fractional_mask; |
| 481 | } | 453 | } |
| 482 | 454 | ||
| 483 | constexpr base_type get_frac() const { | 455 | [[nodiscard]] constexpr base_type get_frac() const { |
| 484 | return data_ & fractional_mask; | 456 | return data_ & fractional_mask; |
| 485 | } | 457 | } |
| 486 | 458 | ||
| 487 | public: | 459 | public: |
| 488 | CONSTEXPR14 void swap(FixedPoint& rhs) { | 460 | constexpr void swap(FixedPoint& rhs) noexcept { |
| 489 | using std::swap; | 461 | using std::swap; |
| 490 | swap(data_, rhs.data_); | 462 | swap(data_, rhs.data_); |
| 491 | } | 463 | } |
| 492 | 464 | ||
| 493 | public: | 465 | public: |
| 494 | base_type data_; | 466 | base_type data_{}; |
| 495 | }; | 467 | }; |
| 496 | 468 | ||
| 497 | // if we have the same fractional portion, but differing integer portions, we trivially upgrade the | 469 | // if we have the same fractional portion, but differing integer portions, we trivially upgrade the |
| 498 | // smaller type | 470 | // smaller type |
| 499 | template <size_t I1, size_t I2, size_t F> | 471 | template <size_t I1, size_t I2, size_t F> |
| 500 | CONSTEXPR14 typename std::conditional<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>>::type | 472 | constexpr std::conditional_t<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>> operator+( |
| 501 | operator+(FixedPoint<I1, F> lhs, FixedPoint<I2, F> rhs) { | 473 | FixedPoint<I1, F> lhs, FixedPoint<I2, F> rhs) { |
| 502 | 474 | ||
| 503 | using T = typename std::conditional<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>>::type; | 475 | using T = std::conditional_t<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>>; |
| 504 | 476 | ||
| 505 | const T l = T::from_base(lhs.to_raw()); | 477 | const T l = T::from_base(lhs.to_raw()); |
| 506 | const T r = T::from_base(rhs.to_raw()); | 478 | const T r = T::from_base(rhs.to_raw()); |
| @@ -508,10 +480,10 @@ operator+(FixedPoint<I1, F> lhs, FixedPoint<I2, F> rhs) { | |||
| 508 | } | 480 | } |
| 509 | 481 | ||
| 510 | template <size_t I1, size_t I2, size_t F> | 482 | template <size_t I1, size_t I2, size_t F> |
| 511 | CONSTEXPR14 typename std::conditional<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>>::type | 483 | constexpr std::conditional_t<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>> operator-( |
| 512 | operator-(FixedPoint<I1, F> lhs, FixedPoint<I2, F> rhs) { | 484 | FixedPoint<I1, F> lhs, FixedPoint<I2, F> rhs) { |
| 513 | 485 | ||
| 514 | using T = typename std::conditional<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>>::type; | 486 | using T = std::conditional_t<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>>; |
| 515 | 487 | ||
| 516 | const T l = T::from_base(lhs.to_raw()); | 488 | const T l = T::from_base(lhs.to_raw()); |
| 517 | const T r = T::from_base(rhs.to_raw()); | 489 | const T r = T::from_base(rhs.to_raw()); |
| @@ -519,10 +491,10 @@ operator-(FixedPoint<I1, F> lhs, FixedPoint<I2, F> rhs) { | |||
| 519 | } | 491 | } |
| 520 | 492 | ||
| 521 | template <size_t I1, size_t I2, size_t F> | 493 | template <size_t I1, size_t I2, size_t F> |
| 522 | CONSTEXPR14 typename std::conditional<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>>::type | 494 | constexpr std::conditional_t<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>> operator*( |
| 523 | operator*(FixedPoint<I1, F> lhs, FixedPoint<I2, F> rhs) { | 495 | FixedPoint<I1, F> lhs, FixedPoint<I2, F> rhs) { |
| 524 | 496 | ||
| 525 | using T = typename std::conditional<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>>::type; | 497 | using T = std::conditional_t<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>>; |
| 526 | 498 | ||
| 527 | const T l = T::from_base(lhs.to_raw()); | 499 | const T l = T::from_base(lhs.to_raw()); |
| 528 | const T r = T::from_base(rhs.to_raw()); | 500 | const T r = T::from_base(rhs.to_raw()); |
| @@ -530,10 +502,10 @@ operator*(FixedPoint<I1, F> lhs, FixedPoint<I2, F> rhs) { | |||
| 530 | } | 502 | } |
| 531 | 503 | ||
| 532 | template <size_t I1, size_t I2, size_t F> | 504 | template <size_t I1, size_t I2, size_t F> |
| 533 | CONSTEXPR14 typename std::conditional<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>>::type | 505 | constexpr std::conditional_t<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>> operator/( |
| 534 | operator/(FixedPoint<I1, F> lhs, FixedPoint<I2, F> rhs) { | 506 | FixedPoint<I1, F> lhs, FixedPoint<I2, F> rhs) { |
| 535 | 507 | ||
| 536 | using T = typename std::conditional<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>>::type; | 508 | using T = std::conditional_t<I1 >= I2, FixedPoint<I1, F>, FixedPoint<I2, F>>; |
| 537 | 509 | ||
| 538 | const T l = T::from_base(lhs.to_raw()); | 510 | const T l = T::from_base(lhs.to_raw()); |
| 539 | const T r = T::from_base(rhs.to_raw()); | 511 | const T r = T::from_base(rhs.to_raw()); |
| @@ -548,159 +520,133 @@ std::ostream& operator<<(std::ostream& os, FixedPoint<I, F> f) { | |||
| 548 | 520 | ||
| 549 | // basic math operators | 521 | // basic math operators |
| 550 | template <size_t I, size_t F> | 522 | template <size_t I, size_t F> |
| 551 | CONSTEXPR14 FixedPoint<I, F> operator+(FixedPoint<I, F> lhs, FixedPoint<I, F> rhs) { | 523 | constexpr FixedPoint<I, F> operator+(FixedPoint<I, F> lhs, FixedPoint<I, F> rhs) { |
| 552 | lhs += rhs; | 524 | lhs += rhs; |
| 553 | return lhs; | 525 | return lhs; |
| 554 | } | 526 | } |
| 555 | template <size_t I, size_t F> | 527 | template <size_t I, size_t F> |
| 556 | CONSTEXPR14 FixedPoint<I, F> operator-(FixedPoint<I, F> lhs, FixedPoint<I, F> rhs) { | 528 | constexpr FixedPoint<I, F> operator-(FixedPoint<I, F> lhs, FixedPoint<I, F> rhs) { |
| 557 | lhs -= rhs; | 529 | lhs -= rhs; |
| 558 | return lhs; | 530 | return lhs; |
| 559 | } | 531 | } |
| 560 | template <size_t I, size_t F> | 532 | template <size_t I, size_t F> |
| 561 | CONSTEXPR14 FixedPoint<I, F> operator*(FixedPoint<I, F> lhs, FixedPoint<I, F> rhs) { | 533 | constexpr FixedPoint<I, F> operator*(FixedPoint<I, F> lhs, FixedPoint<I, F> rhs) { |
| 562 | lhs *= rhs; | 534 | lhs *= rhs; |
| 563 | return lhs; | 535 | return lhs; |
| 564 | } | 536 | } |
| 565 | template <size_t I, size_t F> | 537 | template <size_t I, size_t F> |
| 566 | CONSTEXPR14 FixedPoint<I, F> operator/(FixedPoint<I, F> lhs, FixedPoint<I, F> rhs) { | 538 | constexpr FixedPoint<I, F> operator/(FixedPoint<I, F> lhs, FixedPoint<I, F> rhs) { |
| 567 | lhs /= rhs; | 539 | lhs /= rhs; |
| 568 | return lhs; | 540 | return lhs; |
| 569 | } | 541 | } |
| 570 | 542 | ||
| 571 | template <size_t I, size_t F, class Number, | 543 | template <size_t I, size_t F, IsArithmetic Number> |
| 572 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | 544 | constexpr FixedPoint<I, F> operator+(FixedPoint<I, F> lhs, Number rhs) { |
| 573 | CONSTEXPR14 FixedPoint<I, F> operator+(FixedPoint<I, F> lhs, Number rhs) { | ||
| 574 | lhs += FixedPoint<I, F>(rhs); | 545 | lhs += FixedPoint<I, F>(rhs); |
| 575 | return lhs; | 546 | return lhs; |
| 576 | } | 547 | } |
| 577 | template <size_t I, size_t F, class Number, | 548 | template <size_t I, size_t F, IsArithmetic Number> |
| 578 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | 549 | constexpr FixedPoint<I, F> operator-(FixedPoint<I, F> lhs, Number rhs) { |
| 579 | CONSTEXPR14 FixedPoint<I, F> operator-(FixedPoint<I, F> lhs, Number rhs) { | ||
| 580 | lhs -= FixedPoint<I, F>(rhs); | 550 | lhs -= FixedPoint<I, F>(rhs); |
| 581 | return lhs; | 551 | return lhs; |
| 582 | } | 552 | } |
| 583 | template <size_t I, size_t F, class Number, | 553 | template <size_t I, size_t F, IsArithmetic Number> |
| 584 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | 554 | constexpr FixedPoint<I, F> operator*(FixedPoint<I, F> lhs, Number rhs) { |
| 585 | CONSTEXPR14 FixedPoint<I, F> operator*(FixedPoint<I, F> lhs, Number rhs) { | ||
| 586 | lhs *= FixedPoint<I, F>(rhs); | 555 | lhs *= FixedPoint<I, F>(rhs); |
| 587 | return lhs; | 556 | return lhs; |
| 588 | } | 557 | } |
| 589 | template <size_t I, size_t F, class Number, | 558 | template <size_t I, size_t F, IsArithmetic Number> |
| 590 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | 559 | constexpr FixedPoint<I, F> operator/(FixedPoint<I, F> lhs, Number rhs) { |
| 591 | CONSTEXPR14 FixedPoint<I, F> operator/(FixedPoint<I, F> lhs, Number rhs) { | ||
| 592 | lhs /= FixedPoint<I, F>(rhs); | 560 | lhs /= FixedPoint<I, F>(rhs); |
| 593 | return lhs; | 561 | return lhs; |
| 594 | } | 562 | } |
| 595 | 563 | ||
| 596 | template <size_t I, size_t F, class Number, | 564 | template <size_t I, size_t F, IsArithmetic Number> |
| 597 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | 565 | constexpr FixedPoint<I, F> operator+(Number lhs, FixedPoint<I, F> rhs) { |
| 598 | CONSTEXPR14 FixedPoint<I, F> operator+(Number lhs, FixedPoint<I, F> rhs) { | ||
| 599 | FixedPoint<I, F> tmp(lhs); | 566 | FixedPoint<I, F> tmp(lhs); |
| 600 | tmp += rhs; | 567 | tmp += rhs; |
| 601 | return tmp; | 568 | return tmp; |
| 602 | } | 569 | } |
| 603 | template <size_t I, size_t F, class Number, | 570 | template <size_t I, size_t F, IsArithmetic Number> |
| 604 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | 571 | constexpr FixedPoint<I, F> operator-(Number lhs, FixedPoint<I, F> rhs) { |
| 605 | CONSTEXPR14 FixedPoint<I, F> operator-(Number lhs, FixedPoint<I, F> rhs) { | ||
| 606 | FixedPoint<I, F> tmp(lhs); | 572 | FixedPoint<I, F> tmp(lhs); |
| 607 | tmp -= rhs; | 573 | tmp -= rhs; |
| 608 | return tmp; | 574 | return tmp; |
| 609 | } | 575 | } |
| 610 | template <size_t I, size_t F, class Number, | 576 | template <size_t I, size_t F, IsArithmetic Number> |
| 611 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | 577 | constexpr FixedPoint<I, F> operator*(Number lhs, FixedPoint<I, F> rhs) { |
| 612 | CONSTEXPR14 FixedPoint<I, F> operator*(Number lhs, FixedPoint<I, F> rhs) { | ||
| 613 | FixedPoint<I, F> tmp(lhs); | 578 | FixedPoint<I, F> tmp(lhs); |
| 614 | tmp *= rhs; | 579 | tmp *= rhs; |
| 615 | return tmp; | 580 | return tmp; |
| 616 | } | 581 | } |
| 617 | template <size_t I, size_t F, class Number, | 582 | template <size_t I, size_t F, IsArithmetic Number> |
| 618 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | 583 | constexpr FixedPoint<I, F> operator/(Number lhs, FixedPoint<I, F> rhs) { |
| 619 | CONSTEXPR14 FixedPoint<I, F> operator/(Number lhs, FixedPoint<I, F> rhs) { | ||
| 620 | FixedPoint<I, F> tmp(lhs); | 584 | FixedPoint<I, F> tmp(lhs); |
| 621 | tmp /= rhs; | 585 | tmp /= rhs; |
| 622 | return tmp; | 586 | return tmp; |
| 623 | } | 587 | } |
| 624 | 588 | ||
| 625 | // shift operators | 589 | // shift operators |
| 626 | template <size_t I, size_t F, class Integer, | 590 | template <size_t I, size_t F, IsIntegral Integer> |
| 627 | class = typename std::enable_if<std::is_integral<Integer>::value>::type> | 591 | constexpr FixedPoint<I, F> operator<<(FixedPoint<I, F> lhs, Integer rhs) { |
| 628 | CONSTEXPR14 FixedPoint<I, F> operator<<(FixedPoint<I, F> lhs, Integer rhs) { | ||
| 629 | lhs <<= rhs; | 592 | lhs <<= rhs; |
| 630 | return lhs; | 593 | return lhs; |
| 631 | } | 594 | } |
| 632 | template <size_t I, size_t F, class Integer, | 595 | template <size_t I, size_t F, IsIntegral Integer> |
| 633 | class = typename std::enable_if<std::is_integral<Integer>::value>::type> | 596 | constexpr FixedPoint<I, F> operator>>(FixedPoint<I, F> lhs, Integer rhs) { |
| 634 | CONSTEXPR14 FixedPoint<I, F> operator>>(FixedPoint<I, F> lhs, Integer rhs) { | ||
| 635 | lhs >>= rhs; | 597 | lhs >>= rhs; |
| 636 | return lhs; | 598 | return lhs; |
| 637 | } | 599 | } |
| 638 | 600 | ||
| 639 | // comparison operators | 601 | // comparison operators |
| 640 | template <size_t I, size_t F, class Number, | 602 | template <size_t I, size_t F, IsArithmetic Number> |
| 641 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | ||
| 642 | constexpr bool operator>(FixedPoint<I, F> lhs, Number rhs) { | 603 | constexpr bool operator>(FixedPoint<I, F> lhs, Number rhs) { |
| 643 | return lhs > FixedPoint<I, F>(rhs); | 604 | return lhs > FixedPoint<I, F>(rhs); |
| 644 | } | 605 | } |
| 645 | template <size_t I, size_t F, class Number, | 606 | template <size_t I, size_t F, IsArithmetic Number> |
| 646 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | ||
| 647 | constexpr bool operator<(FixedPoint<I, F> lhs, Number rhs) { | 607 | constexpr bool operator<(FixedPoint<I, F> lhs, Number rhs) { |
| 648 | return lhs < FixedPoint<I, F>(rhs); | 608 | return lhs < FixedPoint<I, F>(rhs); |
| 649 | } | 609 | } |
| 650 | template <size_t I, size_t F, class Number, | 610 | template <size_t I, size_t F, IsArithmetic Number> |
| 651 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | ||
| 652 | constexpr bool operator>=(FixedPoint<I, F> lhs, Number rhs) { | 611 | constexpr bool operator>=(FixedPoint<I, F> lhs, Number rhs) { |
| 653 | return lhs >= FixedPoint<I, F>(rhs); | 612 | return lhs >= FixedPoint<I, F>(rhs); |
| 654 | } | 613 | } |
| 655 | template <size_t I, size_t F, class Number, | 614 | template <size_t I, size_t F, IsArithmetic Number> |
| 656 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | ||
| 657 | constexpr bool operator<=(FixedPoint<I, F> lhs, Number rhs) { | 615 | constexpr bool operator<=(FixedPoint<I, F> lhs, Number rhs) { |
| 658 | return lhs <= FixedPoint<I, F>(rhs); | 616 | return lhs <= FixedPoint<I, F>(rhs); |
| 659 | } | 617 | } |
| 660 | template <size_t I, size_t F, class Number, | 618 | template <size_t I, size_t F, IsArithmetic Number> |
| 661 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | ||
| 662 | constexpr bool operator==(FixedPoint<I, F> lhs, Number rhs) { | 619 | constexpr bool operator==(FixedPoint<I, F> lhs, Number rhs) { |
| 663 | return lhs == FixedPoint<I, F>(rhs); | 620 | return lhs == FixedPoint<I, F>(rhs); |
| 664 | } | 621 | } |
| 665 | template <size_t I, size_t F, class Number, | 622 | template <size_t I, size_t F, IsArithmetic Number> |
| 666 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | ||
| 667 | constexpr bool operator!=(FixedPoint<I, F> lhs, Number rhs) { | 623 | constexpr bool operator!=(FixedPoint<I, F> lhs, Number rhs) { |
| 668 | return lhs != FixedPoint<I, F>(rhs); | 624 | return lhs != FixedPoint<I, F>(rhs); |
| 669 | } | 625 | } |
| 670 | 626 | ||
| 671 | template <size_t I, size_t F, class Number, | 627 | template <size_t I, size_t F, IsArithmetic Number> |
| 672 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | ||
| 673 | constexpr bool operator>(Number lhs, FixedPoint<I, F> rhs) { | 628 | constexpr bool operator>(Number lhs, FixedPoint<I, F> rhs) { |
| 674 | return FixedPoint<I, F>(lhs) > rhs; | 629 | return FixedPoint<I, F>(lhs) > rhs; |
| 675 | } | 630 | } |
| 676 | template <size_t I, size_t F, class Number, | 631 | template <size_t I, size_t F, IsArithmetic Number> |
| 677 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | ||
| 678 | constexpr bool operator<(Number lhs, FixedPoint<I, F> rhs) { | 632 | constexpr bool operator<(Number lhs, FixedPoint<I, F> rhs) { |
| 679 | return FixedPoint<I, F>(lhs) < rhs; | 633 | return FixedPoint<I, F>(lhs) < rhs; |
| 680 | } | 634 | } |
| 681 | template <size_t I, size_t F, class Number, | 635 | template <size_t I, size_t F, IsArithmetic Number> |
| 682 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | ||
| 683 | constexpr bool operator>=(Number lhs, FixedPoint<I, F> rhs) { | 636 | constexpr bool operator>=(Number lhs, FixedPoint<I, F> rhs) { |
| 684 | return FixedPoint<I, F>(lhs) >= rhs; | 637 | return FixedPoint<I, F>(lhs) >= rhs; |
| 685 | } | 638 | } |
| 686 | template <size_t I, size_t F, class Number, | 639 | template <size_t I, size_t F, IsArithmetic Number> |
| 687 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | ||
| 688 | constexpr bool operator<=(Number lhs, FixedPoint<I, F> rhs) { | 640 | constexpr bool operator<=(Number lhs, FixedPoint<I, F> rhs) { |
| 689 | return FixedPoint<I, F>(lhs) <= rhs; | 641 | return FixedPoint<I, F>(lhs) <= rhs; |
| 690 | } | 642 | } |
| 691 | template <size_t I, size_t F, class Number, | 643 | template <size_t I, size_t F, IsArithmetic Number> |
| 692 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | ||
| 693 | constexpr bool operator==(Number lhs, FixedPoint<I, F> rhs) { | 644 | constexpr bool operator==(Number lhs, FixedPoint<I, F> rhs) { |
| 694 | return FixedPoint<I, F>(lhs) == rhs; | 645 | return FixedPoint<I, F>(lhs) == rhs; |
| 695 | } | 646 | } |
| 696 | template <size_t I, size_t F, class Number, | 647 | template <size_t I, size_t F, IsArithmetic Number> |
| 697 | class = typename std::enable_if<std::is_arithmetic<Number>::value>::type> | ||
| 698 | constexpr bool operator!=(Number lhs, FixedPoint<I, F> rhs) { | 648 | constexpr bool operator!=(Number lhs, FixedPoint<I, F> rhs) { |
| 699 | return FixedPoint<I, F>(lhs) != rhs; | 649 | return FixedPoint<I, F>(lhs) != rhs; |
| 700 | } | 650 | } |
| 701 | 651 | ||
| 702 | } // namespace Common | 652 | } // namespace Common |
| 703 | |||
| 704 | #undef CONSTEXPR14 | ||
| 705 | |||
| 706 | #endif | ||