| 1 | //===----------------------------------------------------------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
| 10 | /// This file contains the declaration of 256-bit unsigned fractional type. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_LIBC_SRC___SUPPORT_FRAC256_H |
| 15 | #define LLVM_LIBC_SRC___SUPPORT_FRAC256_H |
| 16 | |
| 17 | #include "big_int.h" |
| 18 | #include "frac128.h" |
| 19 | #include "src/__support/macros/config.h" |
| 20 | |
| 21 | namespace LIBC_NAMESPACE_DECL { |
| 22 | |
| 23 | struct Frac256 : public UInt<256> { |
| 24 | using UInt<256>::UInt; |
| 25 | |
| 26 | // Convert Frac256 number to Frac128 with truncation. |
| 27 | LIBC_INLINE constexpr Frac128 to_frac128() const { |
| 28 | return Frac128({val[2], val[3]}); |
| 29 | } |
| 30 | |
| 31 | LIBC_INLINE constexpr explicit operator Frac128() const { |
| 32 | return to_frac128(); |
| 33 | } |
| 34 | |
| 35 | // Convert Frac256 number to Frac64 with truncation. |
| 36 | LIBC_INLINE constexpr Frac64 to_frac64() const { return Frac64(val[3]); } |
| 37 | |
| 38 | LIBC_INLINE constexpr explicit operator Frac64() const { return to_frac64(); } |
| 39 | |
| 40 | LIBC_INLINE constexpr Frac256 operator~() const { |
| 41 | Frac256 r{}; |
| 42 | r.val[0] = ~val[0]; |
| 43 | r.val[1] = ~val[1]; |
| 44 | r.val[2] = ~val[2]; |
| 45 | r.val[3] = ~val[3]; |
| 46 | return r; |
| 47 | } |
| 48 | |
| 49 | LIBC_INLINE constexpr Frac256 operator+(const Frac256 &other) const { |
| 50 | UInt<256> r = UInt<256>(*this) + UInt<256>(other); |
| 51 | return Frac256(r.val); |
| 52 | } |
| 53 | |
| 54 | LIBC_INLINE constexpr Frac256 operator-(const Frac256 &other) const { |
| 55 | UInt<256> r = UInt<256>(*this) - UInt<256>(other); |
| 56 | return Frac256(r.val); |
| 57 | } |
| 58 | |
| 59 | LIBC_INLINE constexpr Frac256 operator*(const Frac256 &other) const { |
| 60 | UInt<256> r = UInt<256>::quick_mul_hi(other: UInt<256>(other)); |
| 61 | return Frac256(r.val); |
| 62 | } |
| 63 | |
| 64 | LIBC_INLINE constexpr Frac256 &operator+=(const Frac256 &other) { |
| 65 | *this = *this + other; |
| 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | LIBC_INLINE constexpr Frac256 &operator-=(const Frac256 &other) { |
| 70 | *this = *this - other; |
| 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | LIBC_INLINE constexpr Frac256 &operator*=(const Frac256 &other) { |
| 75 | *this = *this * other; |
| 76 | return *this; |
| 77 | } |
| 78 | |
| 79 | LIBC_INLINE constexpr Frac256 operator<<(size_t s) const { |
| 80 | return Frac256((UInt<256>(*this) << s).val); |
| 81 | } |
| 82 | |
| 83 | LIBC_INLINE constexpr Frac256 operator>>(size_t s) const { |
| 84 | return Frac256((UInt<256>(*this) >> s).val); |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | } // namespace LIBC_NAMESPACE_DECL |
| 89 | |
| 90 | #endif // LLVM_LIBC_SRC___SUPPORT_FRAC256_H |
| 91 |