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 128-bit unsigned fractional type.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIBC_SRC___SUPPORT_FRAC128_H
15#define LLVM_LIBC_SRC___SUPPORT_FRAC128_H
16
17#include "big_int.h"
18#include "frac64.h"
19#include "src/__support/macros/config.h"
20
21namespace LIBC_NAMESPACE_DECL {
22
23struct Frac128 : public UInt<128> {
24 using UInt<128>::UInt;
25
26 // Convert Frac128 number to Frac64 with truncation.
27 LIBC_INLINE constexpr Frac64 to_frac64() const { return Frac64(val[1]); }
28
29 LIBC_INLINE constexpr explicit operator Frac64() const { return to_frac64(); }
30
31 LIBC_INLINE constexpr Frac128 operator~() const {
32 Frac128 r{};
33 r.val[0] = ~val[0];
34 r.val[1] = ~val[1];
35 return r;
36 }
37
38 LIBC_INLINE constexpr Frac128 operator+(const Frac128 &other) const {
39 UInt<128> r = UInt<128>(*this) + (UInt<128>(other));
40 return Frac128(r.val);
41 }
42
43 LIBC_INLINE constexpr Frac128 operator-(const Frac128 &other) const {
44 UInt<128> r = UInt<128>(*this) - (UInt<128>(other));
45 return Frac128(r.val);
46 }
47
48 LIBC_INLINE constexpr Frac128 operator*(const Frac128 &other) const {
49 UInt<128> r = UInt<128>::quick_mul_hi(other: UInt<128>(other));
50 return Frac128(r.val);
51 }
52
53 LIBC_INLINE constexpr Frac128 &operator+=(const Frac128 &other) {
54 *this = *this + other;
55 return *this;
56 }
57
58 LIBC_INLINE constexpr Frac128 &operator-=(const Frac128 &other) {
59 *this = *this - other;
60 return *this;
61 }
62
63 LIBC_INLINE constexpr Frac128 &operator*=(const Frac128 &other) {
64 *this = *this * other;
65 return *this;
66 }
67
68 LIBC_INLINE constexpr Frac128 operator<<(size_t s) const {
69 return Frac128((UInt<128>(*this) << s).val);
70 }
71
72 LIBC_INLINE constexpr Frac128 operator>>(size_t s) const {
73 return Frac128((UInt<128>(*this) >> s).val);
74 }
75};
76
77} // namespace LIBC_NAMESPACE_DECL
78
79#endif // LLVM_LIBC_SRC___SUPPORT_FRAC128_H
80