File: ctfloat.cpp

package info (click to toggle)
ldc 1%3A1.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 59,248 kB
  • sloc: cpp: 61,598; ansic: 14,545; sh: 1,014; makefile: 972; asm: 510; objc: 135; exp: 48; python: 12
file content (216 lines) | stat: -rw-r--r-- 5,740 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//===-- ctfloat.cpp -------------------------------------------------------===//
//
//                         LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//

#include "dmd/root/ctfloat.h"
#include "gen/llvm.h"
#include "llvm/Support/Error.h"

using llvm::APFloat;

namespace {

const llvm::fltSemantics *apSemantics = nullptr;

constexpr unsigned numUint64Parts = (sizeof(real_t) + 7) / 8;
union CTFloatUnion {
  real_t fp;
  uint64_t bits[numUint64Parts];
};

APFloat parseLiteral(const llvm::fltSemantics &semantics, const char *literal,
                     bool *isOutOfRange = nullptr) {
  APFloat ap(semantics, APFloat::uninitialized);
  auto r =
#if LDC_LLVM_VER >= 1000
      llvm::cantFail
#endif
      (ap.convertFromString(literal, APFloat::rmNearestTiesToEven));
  if (isOutOfRange) {
    *isOutOfRange = (r & (APFloat::opOverflow | APFloat::opUnderflow)) != 0;
  }
  return ap;
}

} // anonymous namespace

////////////////////////////////////////////////////////////////////////////////

void CTFloat::initialize() {
  if (apSemantics)
    return;

#ifdef _MSC_VER
  // MSVC hosts use dmd.root.longdouble (80-bit x87)
  apSemantics = &APFloat::x87DoubleExtended();
#else
  static_assert(std::numeric_limits<real_t>::is_specialized,
                "real_t is not an arithmetic type");
  constexpr int digits = std::numeric_limits<real_t>::digits;
  if (digits == 53) {
    apSemantics = &APFloat::IEEEdouble();
  } else if (digits == 64) {
    apSemantics = &APFloat::x87DoubleExtended();
  } else if (digits == 113) {
    apSemantics = &APFloat::IEEEquad();
  } else if (digits == 106) {
    apSemantics = &APFloat::PPCDoubleDouble();
  } else {
    llvm_unreachable("Unknown host real_t type for compile-time reals");
  }
#endif

  zero = 0;
  one = 1;
  minusone = -1;
  half = 0.5;

  nan = fromAPFloat(APFloat::getQNaN(*apSemantics));
  infinity = fromAPFloat(APFloat::getInf(*apSemantics));
}

////////////////////////////////////////////////////////////////////////////////

void CTFloat::toAPFloat(const real_t src, APFloat &dst) {
  if (sizeof(real_t) == 8) {
    dst = APFloat(static_cast<double>(src));
    return;
  }

  CTFloatUnion u;
  u.fp = src;

  const unsigned sizeInBits = APFloat::getSizeInBits(*apSemantics);
  const APInt bits = APInt(sizeInBits, numUint64Parts, u.bits);

  dst = APFloat(*apSemantics, bits);
}

////////////////////////////////////////////////////////////////////////////////

real_t CTFloat::fromAPFloat(const APFloat &src_) {
  APFloat src = src_;
  if (&src.getSemantics() != apSemantics) {
    bool ignored;
    src.convert(*apSemantics, APFloat::rmNearestTiesToEven, &ignored);
  }

  const APInt bits = src.bitcastToAPInt();

  CTFloatUnion u;
  memcpy(u.bits, bits.getRawData(), bits.getBitWidth() / 8);
  return u.fp;
}

////////////////////////////////////////////////////////////////////////////////

real_t CTFloat::parse(const char *literal, bool *isOutOfRange) {
  const APFloat ap = parseLiteral(*apSemantics, literal, isOutOfRange);
  return fromAPFloat(ap);
}

bool CTFloat::isFloat32LiteralOutOfRange(const char *literal) {
  bool isOutOfRange;
  parseLiteral(APFloat::IEEEsingle(), literal, &isOutOfRange);
  return isOutOfRange;
}

bool CTFloat::isFloat64LiteralOutOfRange(const char *literal) {
  bool isOutOfRange;
  parseLiteral(APFloat::IEEEdouble(), literal, &isOutOfRange);
  return isOutOfRange;
}

////////////////////////////////////////////////////////////////////////////////

int CTFloat::sprint(char *str, char fmt, real_t x) {
  assert(fmt == 'g' || fmt == 'a' || fmt == 'A');
  const bool uppercase = fmt == 'A';

  // We try to keep close to C printf and handle a few divergences of the LLVM
  // to-string utility functions.

  if (isNaN(x)) {
    int length = 0;
    if (copysign(one, x) != one) {
      str[0] = '-';
      ++length;
    }
    memcpy(str + length, uppercase ? "NAN" : "nan", 3);
    length += 3;
    str[length] = 0;
    return length;
  }

  if (isInfinity(x)) { // incl. -inf
    int length = 0;
    if (x < 0) {
      str[0] = '-';
      ++length;
    }
    memcpy(str + length, uppercase ? "INF" : "inf", 3);
    length += 3;
    str[length] = 0;
    return length;
  }

  // Use LLVM for printing hex strings.
  if (fmt == 'a' || fmt == 'A') {
    APFloat ap(0.0);
    toAPFloat(x, ap);

    int length =
        ap.convertToHexString(str, 0, uppercase, APFloat::rmNearestTiesToEven);

    // insert a '+' prefix for non-negative exponents (incl. 0) as visual aid
    const char p = uppercase ? 'P' : 'p';
    for (int i = length - 2; i >= 0; --i) {
      if (str[i] == p) {
        if (str[i + 1] != '-' && str[i + 1] != '+') {
          for (int j = length - 1; j > i; --j)
            str[j + 1] = str[j];
          str[i + 1] = '+';
          ++length;
        }

        break;
      }
    }

    str[length] = 0;
    return length;
  }

  assert(fmt == 'g');

  // Use the host C runtime for printing decimal strings;
  // llvm::APFloat::toString() seems not to round correctly, e.g., with LLVM 10:
  // * powl(2.5L, 2.5L) = 9.882117688... => `9.88211` (not 9.88212)
  // * 1e-300L => `9.99999e-301`
#ifdef _MSC_VER
  int length = sprintf(str, "%g", static_cast<double>(x));
#else
  int length = sprintf(str, "%Lg", x);
#endif

  // 1 => 1.0 to distinguish from integers
  bool needsFPSuffix = true;
  for (int i = 0; i < length; ++i) {
    if (str[i] != '-' && !isdigit(str[i])) {
      needsFPSuffix = false;
      break;
    }
  }
  if (needsFPSuffix) {
    memcpy(str + length, ".0", 2);
    length += 2;
  }

  str[length] = 0;
  return length;
}