1//===-- Definition for Float128 data type -----------------------*- C++ -*-===//
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#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOAT128_H
10#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOAT128_H
11
12#include "hdr/stdint_proxy.h"
13#include "src/__support/CPP/limits.h"
14#include "src/__support/CPP/type_traits.h"
15#include "src/__support/FPUtil/cast.h"
16#include "src/__support/FPUtil/comparison_operations.h"
17#include "src/__support/FPUtil/dyadic_float.h"
18#include "src/__support/FPUtil/generic/add_sub.h"
19#include "src/__support/FPUtil/generic/div.h"
20#include "src/__support/FPUtil/generic/mul.h"
21#include "src/__support/macros/attributes.h"
22#include "src/__support/macros/config.h"
23#include "src/__support/uint128.h"
24
25namespace LIBC_NAMESPACE_DECL {
26namespace fputil {
27
28struct Float128 {
29 UInt128 bits;
30
31 // Returns Float128 with exact bits.
32 static LIBC_INLINE constexpr Float128 from_bits(UInt128 bits) {
33 Float128 v{};
34 v.bits = bits;
35 return v;
36 }
37
38 LIBC_INLINE Float128() = default;
39 LIBC_INLINE constexpr Float128(const Float128 &) = default;
40 LIBC_INLINE constexpr Float128(Float128 &&) = default;
41 LIBC_INLINE constexpr Float128 &operator=(const Float128 &) = default;
42 LIBC_INLINE constexpr Float128 &operator=(Float128 &&) = default;
43
44 // Floating point type and integer type
45 template <typename T>
46 LIBC_INLINE constexpr explicit Float128(T value) : bits(0U) {
47 if constexpr (cpp::is_floating_point_v<T>) {
48 bits = fputil::cast<Float128>(value).bits;
49 } else if constexpr (cpp::is_integral_v<T>) {
50 Sign sign = Sign::POS;
51 auto unsigned_value = static_cast<cpp::make_unsigned_t<T>>(value);
52
53 if constexpr (cpp::is_signed_v<T>) {
54 if (value < 0) {
55 sign = Sign::NEG;
56 unsigned_value = -unsigned_value;
57 }
58 }
59
60 fputil::DyadicFloat<FPBits<Float128>::STORAGE_LEN> xd(sign, 0,
61 unsigned_value);
62 bits = xd.template as<Float128, /*ShouldSignalExceptions=*/true>().bits;
63
64 } else if constexpr (cpp::is_convertible_v<T, Float128>) {
65 bits = value.operator Float128().bits;
66 } else {
67 bits = fputil::cast<Float128>(x: static_cast<float>(value)).bits;
68 }
69 }
70
71 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T> &&
72 !cpp::is_same_v<T, Float128>,
73 int> = 0>
74 LIBC_INLINE LIBC_CONSTEXPR_DEFAULT operator T() const {
75 return fputil::cast<T>(*this);
76 }
77
78 template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
79 LIBC_INLINE constexpr explicit operator T() const {
80 constexpr T MIN_T = cpp::numeric_limits<T>::min();
81 constexpr T MAX_T = cpp::numeric_limits<T>::max();
82 FPBits<Float128> x_bits(*this);
83 // Raise FE_INVALID for inf and NaN
84 if (x_bits.is_inf_or_nan()) {
85 raise_except_if_required(FE_INVALID);
86 return x_bits.is_neg() ? MIN_T : MAX_T;
87 }
88 int exponent = x_bits.get_explicit_exponent();
89 constexpr int EXPONENT_LIMIT = cpp::numeric_limits<T>::digits;
90 if (exponent > EXPONENT_LIMIT) {
91 raise_except_if_required(FE_INVALID);
92 return x_bits.is_neg() ? MIN_T : MAX_T;
93 } else if (exponent == EXPONENT_LIMIT) {
94 if (x_bits.is_pos() || x_bits.get_mantissa() != 0) {
95 raise_except_if_required(FE_INVALID);
96 return x_bits.is_neg() ? MIN_T : MAX_T;
97 }
98 }
99
100 int x_bits_exp = exponent - FPBits<Float128>::FRACTION_LEN;
101 // sign * 2^(exp-bias) * mantissa
102 DyadicFloat<FPBits<Float128>::STORAGE_LEN> xd(
103 x_bits.sign(), x_bits_exp, x_bits.get_explicit_mantissa());
104 return static_cast<T>(xd.as_mantissa_type());
105 }
106
107 // unary
108 LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR Float128 operator-() const {
109 fputil::FPBits<Float128> result(*this);
110 result.set_sign(result.is_pos() ? Sign::NEG : Sign::POS);
111 return result.get_val();
112 }
113 // operator overloads
114 LIBC_INLINE constexpr Float128 operator+(const Float128 &other) const {
115 return fputil::generic::add<Float128>(x: *this, y: other);
116 }
117
118 LIBC_INLINE constexpr Float128 operator-(const Float128 &other) const {
119 return fputil::generic::sub<Float128>(x: *this, y: other);
120 }
121
122 LIBC_INLINE constexpr Float128 operator*(const Float128 &other) const {
123 return fputil::generic::mul<Float128>(x: *this, y: other);
124 }
125
126 LIBC_INLINE constexpr Float128 operator/(const Float128 &other) const {
127 return fputil::generic::div<Float128>(x: *this, y: other);
128 }
129
130 LIBC_INLINE constexpr Float128 &operator*=(const Float128 &other) {
131 *this = *this * other;
132 return *this;
133 }
134
135 LIBC_INLINE constexpr Float128 &operator+=(const Float128 &other) {
136 *this = *this + other;
137 return *this;
138 }
139
140 LIBC_INLINE constexpr Float128 &operator-=(const Float128 &other) {
141 *this = *this - other;
142 return *this;
143 }
144
145 LIBC_INLINE constexpr Float128 &operator/=(const Float128 &other) {
146 *this = *this / other;
147 return *this;
148 }
149
150 LIBC_INLINE constexpr bool operator==(const Float128 &other) const {
151 return fputil::equals(x: *this, y: other);
152 }
153
154 LIBC_INLINE constexpr bool operator!=(const Float128 &other) const {
155 return !fputil::equals(x: *this, y: other);
156 }
157
158 LIBC_INLINE constexpr bool operator<(const Float128 &other) const {
159 return fputil::less_than(x: *this, y: other);
160 }
161
162 LIBC_INLINE constexpr bool operator<=(const Float128 &other) const {
163 return fputil::less_than_or_equals(x: *this, y: other);
164 }
165
166 LIBC_INLINE constexpr bool operator>(const Float128 &other) const {
167 return fputil::greater_than(x: *this, y: other);
168 }
169
170 LIBC_INLINE constexpr bool operator>=(const Float128 &other) const {
171 return fputil::greater_than_or_equals(x: *this, y: other);
172 }
173};
174
175static_assert(LIBC_NAMESPACE::cpp::is_trivially_constructible<
176 LIBC_NAMESPACE::fputil::Float128>::value);
177static_assert(LIBC_NAMESPACE::cpp::is_trivially_copyable<
178 LIBC_NAMESPACE::fputil::Float128>::value);
179
180} // namespace fputil
181} // namespace LIBC_NAMESPACE_DECL
182
183#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOAT128_H
184