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 64-bit unsigned fractional type.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIBC_SRC___SUPPORT_FRAC64_H
15#define LLVM_LIBC_SRC___SUPPORT_FRAC64_H
16
17#include "src/__support/big_int.h"
18#include "src/__support/macros/attributes.h"
19#include "src/__support/macros/config.h"
20
21namespace LIBC_NAMESPACE_DECL {
22
23struct Frac64 : public UInt<64> {
24 using UInt<64>::UInt;
25
26 LIBC_ALWAYS_INLINE constexpr Frac64 operator~() const {
27 return Frac64(~val[0]);
28 }
29
30 LIBC_ALWAYS_INLINE constexpr Frac64 operator+(Frac64 other) const {
31 return Frac64(val[0] + other.val[0]);
32 }
33
34 LIBC_ALWAYS_INLINE constexpr Frac64 operator-(Frac64 other) const {
35 return Frac64(val[0] - other.val[0]);
36 }
37
38 // Fast hi part of the 128-bit product:
39 // 0 <= ((a * b) >> 64) - (Frac64(a) * Frac64(b)).val[0] <= 1.
40 LIBC_ALWAYS_INLINE constexpr Frac64 operator*(Frac64 other) const {
41 return Frac64(UInt<64>::quick_mul_hi(other).val);
42 }
43
44 LIBC_ALWAYS_INLINE constexpr Frac64 &operator+=(Frac64 other) {
45 *this = *this + other;
46 return *this;
47 }
48
49 LIBC_ALWAYS_INLINE constexpr Frac64 &operator-=(Frac64 other) {
50 *this = *this - other;
51 return *this;
52 }
53
54 LIBC_ALWAYS_INLINE constexpr Frac64 &operator*=(Frac64 other) {
55 *this = *this * other;
56 return *this;
57 }
58
59 LIBC_ALWAYS_INLINE constexpr Frac64 operator<<(size_t s) const {
60 return Frac64(val[0] << s);
61 }
62
63 LIBC_ALWAYS_INLINE constexpr Frac64 operator>>(size_t s) const {
64 return Frac64(val[0] >> s);
65 }
66};
67
68} // namespace LIBC_NAMESPACE_DECL
69
70#endif // LLVM_LIBC_SRC___SUPPORT_FRAC64_H
71