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