Skip to content

Commit d959ed9

Browse files
committed
Change long double to float128
Use __float128 for clang and _Float128 for gcc. Default to long double for other compilers.
1 parent af03313 commit d959ed9

File tree

7 files changed

+36
-16
lines changed

7 files changed

+36
-16
lines changed

llvm/include/llvm/ADT/APFloat.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/ArrayRef.h"
2020
#include "llvm/ADT/FloatingPointMode.h"
2121
#include "llvm/Support/ErrorHandling.h"
22+
#include "llvm/Support/float128.h"
2223
#include <memory>
2324

2425
#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
@@ -299,7 +300,7 @@ class IEEEFloat final : public APFloatBase {
299300
IEEEFloat(const fltSemantics &, integerPart);
300301
IEEEFloat(const fltSemantics &, uninitializedTag);
301302
IEEEFloat(const fltSemantics &, const APInt &);
302-
explicit IEEEFloat(long double ld);
303+
explicit IEEEFloat(float128 ld);
303304
explicit IEEEFloat(double d);
304305
explicit IEEEFloat(float f);
305306
IEEEFloat(const IEEEFloat &);
@@ -355,7 +356,7 @@ class IEEEFloat final : public APFloatBase {
355356
Expected<opStatus> convertFromString(StringRef, roundingMode);
356357
APInt bitcastToAPInt() const;
357358
double convertToDouble() const;
358-
long double convertToQuad() const;
359+
float128 convertToQuad() const;
359360
float convertToFloat() const;
360361

361362
/// @}
@@ -944,7 +945,7 @@ class APFloat : public APFloatBase {
944945
APFloat(const fltSemantics &Semantics, uninitializedTag)
945946
: U(Semantics, uninitialized) {}
946947
APFloat(const fltSemantics &Semantics, const APInt &I) : U(Semantics, I) {}
947-
explicit APFloat(long double ld) : U(IEEEFloat(ld), IEEEquad()) {}
948+
explicit APFloat(float128 ld) : U(IEEEFloat(ld), IEEEquad()) {}
948949
explicit APFloat(double d) : U(IEEEFloat(d), IEEEdouble()) {}
949950
explicit APFloat(float f) : U(IEEEFloat(f), IEEEsingle()) {}
950951
APFloat(const APFloat &RHS) = default;
@@ -1226,7 +1227,7 @@ class APFloat : public APFloatBase {
12261227
/// \pre The APFloat must be built using semantics, that can be represented by
12271228
/// the host float type without loss of precision. It can be IEEEquad and
12281229
/// shorter semantics, like IEEEdouble and others.
1229-
long double convertToQuad() const;
1230+
float128 convertToQuad() const;
12301231

12311232
/// Converts this APFloat to host float value.
12321233
///

llvm/include/llvm/ADT/APInt.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "llvm/Support/Compiler.h"
1919
#include "llvm/Support/MathExtras.h"
20+
#include "llvm/Support/float128.h"
2021
#include <cassert>
2122
#include <climits>
2223
#include <cstring>
@@ -1670,9 +1671,9 @@ class [[nodiscard]] APInt {
16701671
/// any bit width. Exactly 64 bits will be translated.
16711672
double bitsToDouble() const { return llvm::bit_cast<double>(getWord(0)); }
16721673

1673-
long double bitsToQuad() const {
1674+
float128 bitsToQuad() const {
16741675
__uint128_t ul = ((__uint128_t)U.pVal[1] << 64) + U.pVal[0];
1675-
return llvm::bit_cast<long double>(ul);
1676+
return llvm::bit_cast<float128>(ul);
16761677
}
16771678

16781679
/// Converts APInt bits to a float
@@ -1700,14 +1701,12 @@ class [[nodiscard]] APInt {
17001701
return APInt(sizeof(float) * CHAR_BIT, llvm::bit_cast<uint32_t>(V));
17011702
}
17021703

1703-
static APInt longDoubleToBits(long double V) {
1704-
assert(sizeof(long double) == 16 && "Expected 16 byte long double");
1705-
1704+
static APInt longDoubleToBits(float128 V) {
17061705
const uint64_t Words[2] = {
17071706
static_cast<uint64_t>(V),
17081707
static_cast<uint64_t>(llvm::bit_cast<__uint128_t>(V) >> 64),
17091708
};
1710-
return APInt(sizeof(long double) * CHAR_BIT, 2, Words);
1709+
return APInt(sizeof(float128) * CHAR_BIT, 2, Words);
17111710
}
17121711

17131712
/// @}

llvm/include/llvm/IR/Constants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ class ConstantFP final : public ConstantData {
290290
/// host double and as the target format.
291291
static Constant *get(Type *Ty, double V);
292292

293-
static Constant *get128(Type *Ty, long double V);
293+
static Constant *get128(Type *Ty, float128 V);
294294

295295
/// If Ty is a vector type, return a Constant with a splat of the given
296296
/// value. Otherwise return a ConstantFP for the given value.

llvm/include/llvm/Support/float128.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- llvm/Support/float128.h - Compiler abstraction support --*- 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_FLOAT128
10+
#define LLVM_FLOAT128
11+
12+
#if defined(__clang__)
13+
typedef __float128 float128;
14+
#elif defined(__GNUC__) || defined(__GNUG__)
15+
typedef _Float128 float128;
16+
#else
17+
typedef long double float128;
18+
#endif
19+
20+
#endif // LLVM_FLOAT128

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1760,7 +1760,7 @@ inline bool llvm_fenv_testexcept() {
17601760

17611761
Constant *ConstantFoldLogf128(const APFloat &V, Type *Ty) {
17621762
#ifdef HAS_LOGF128
1763-
long double l = logf128(V.convertToQuad());
1763+
float128 l = logf128(V.convertToQuad());
17641764
return ConstantFP::get128(Ty, l);
17651765
#else
17661766
return nullptr;

llvm/lib/IR/Constants.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ Constant *ConstantFP::get(Type *Ty, double V) {
976976
return C;
977977
}
978978

979-
Constant *ConstantFP::get128(Type *Ty, long double V) {
979+
Constant *ConstantFP::get128(Type *Ty, float128 V) {
980980
LLVMContext &Context = Ty->getContext();
981981

982982
APFloat FV(V);

llvm/lib/Support/APFloat.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3670,7 +3670,7 @@ double IEEEFloat::convertToDouble() const {
36703670
return api.bitsToDouble();
36713671
}
36723672

3673-
long double IEEEFloat::convertToQuad() const {
3673+
float128 IEEEFloat::convertToQuad() const {
36743674
assert(semantics == (const llvm::fltSemantics *)&semIEEEquad &&
36753675
"Float semantics are not IEEEquads");
36763676
APInt api = bitcastToAPInt();
@@ -3965,7 +3965,7 @@ IEEEFloat::IEEEFloat(double d) {
39653965
initFromAPInt(&semIEEEdouble, APInt::doubleToBits(d));
39663966
}
39673967

3968-
IEEEFloat::IEEEFloat(long double ld) {
3968+
IEEEFloat::IEEEFloat(float128 ld) {
39693969
initFromAPInt(&semIEEEquad, APInt::longDoubleToBits(ld));
39703970
}
39713971

@@ -5276,7 +5276,7 @@ double APFloat::convertToDouble() const {
52765276
return Temp.getIEEE().convertToDouble();
52775277
}
52785278

5279-
long double APFloat::convertToQuad() const {
5279+
float128 APFloat::convertToQuad() const {
52805280
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEquad)
52815281
return getIEEE().convertToQuad();
52825282
assert(getSemantics().isRepresentableBy(semIEEEquad) &&

0 commit comments

Comments
 (0)