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
|
#include "flang/Decimal/decimal.h"
#include "llvm/Support/raw_ostream.h"
#include <cinttypes>
#include <cstdio>
#include <cstring>
using namespace Fortran::decimal;
static int tests{0};
static int fails{0};
union u {
float x;
std::uint32_t u;
};
llvm::raw_ostream &failed(float x) {
++fails;
union u u;
u.x = x;
llvm::outs() << "FAIL: 0x";
return llvm::outs().write_hex(u.u);
}
void testDirect(float x, const char *expect, int expectExpo, int flags = 0) {
char buffer[1024];
++tests;
auto result{ConvertFloatToDecimal(buffer, sizeof buffer,
static_cast<enum DecimalConversionFlags>(flags), 1024, RoundNearest, x)};
if (result.str == nullptr) {
failed(x) << ' ' << flags << ": no result str\n";
} else if (std::strcmp(result.str, expect) != 0 ||
result.decimalExponent != expectExpo) {
failed(x) << ' ' << flags << ": expect '." << expect << 'e' << expectExpo
<< "', got '." << result.str << 'e' << result.decimalExponent
<< "'\n";
}
}
void testReadback(float x, int flags) {
char buffer[1024];
++tests;
auto result{ConvertFloatToDecimal(buffer, sizeof buffer,
static_cast<enum DecimalConversionFlags>(flags), 1024, RoundNearest, x)};
if (result.str == nullptr) {
failed(x) << ' ' << flags << ": no result str\n";
} else {
float y{0};
char *q{const_cast<char *>(result.str)};
int expo{result.decimalExponent};
expo -= result.length;
if (*q == '-' || *q == '+') {
++expo;
}
if (q >= buffer && q < buffer + sizeof buffer) {
std::snprintf(q + result.length,
buffer + sizeof buffer - (q + result.length), "e%d", expo);
}
const char *p{q};
auto rflags{ConvertDecimalToFloat(&p, &y, RoundNearest)};
union u u;
if (!(x == x)) {
if (y == y || *p != '\0' || (rflags & Invalid)) {
u.x = y;
(failed(x) << " (NaN) " << flags << ": -> '" << result.str << "' -> 0x")
.write_hex(u.u)
<< " '" << p << "' " << rflags << '\n';
}
} else if (x != y || *p != '\0' || (rflags & Invalid)) {
u.x = x;
(failed(x) << ' ' << flags << ": -> '" << result.str << "' -> 0x")
.write_hex(u.u)
<< " '" << p << "' " << rflags << '\n';
}
}
}
int main() {
union u u;
testDirect(-1.0, "-1", 1);
testDirect(0.0, "0", 0);
testDirect(0.0, "+0", 0, AlwaysSign);
testDirect(1.0, "1", 1);
testDirect(2.0, "2", 1);
testDirect(-1.0, "-1", 1);
testDirect(314159, "314159", 6);
testDirect(0.0625, "625", -1);
u.u = 0x80000000;
testDirect(u.x, "-0", 0);
u.u = 0x7f800000;
testDirect(u.x, "Inf", 0);
testDirect(u.x, "+Inf", 0, AlwaysSign);
u.u = 0xff800000;
testDirect(u.x, "-Inf", 0);
u.u = 0xffffffff;
testDirect(u.x, "NaN", 0);
testDirect(u.x, "NaN", 0, AlwaysSign);
u.u = 1;
testDirect(u.x,
"140129846432481707092372958328991613128026194187651577175706828388979108"
"268586060148663818836212158203125",
-44, 0);
testDirect(u.x, "1", -44, Minimize);
u.u = 0x7f777777;
testDirect(u.x, "3289396118917826996438159226753253376", 39, 0);
testDirect(u.x, "32893961", 39, Minimize);
for (u.u = 0; u.u < 16; ++u.u) {
testReadback(u.x, 0);
testReadback(-u.x, 0);
testReadback(u.x, Minimize);
testReadback(-u.x, Minimize);
}
for (u.u = 1; u.u < 0x7f800000; u.u *= 2) {
testReadback(u.x, 0);
testReadback(-u.x, 0);
testReadback(u.x, Minimize);
testReadback(-u.x, Minimize);
}
for (u.u = 0x7f7ffff0; u.u < 0x7f800010; ++u.u) {
testReadback(u.x, 0);
testReadback(-u.x, 0);
testReadback(u.x, Minimize);
testReadback(-u.x, Minimize);
}
for (u.u = 0; u.u < 0x7f800000; u.u += 65536) {
testReadback(u.x, 0);
testReadback(-u.x, 0);
testReadback(u.x, Minimize);
testReadback(-u.x, Minimize);
}
for (u.u = 0; u.u < 0x7f800000; u.u += 99999) {
testReadback(u.x, 0);
testReadback(-u.x, 0);
testReadback(u.x, Minimize);
testReadback(-u.x, Minimize);
}
for (u.u = 0; u.u < 0x7f800000; u.u += 32767) {
testReadback(u.x, 0);
testReadback(-u.x, 0);
testReadback(u.x, Minimize);
testReadback(-u.x, Minimize);
}
llvm::outs() << tests << " tests run, " << fails << " tests failed\n";
return fails > 0;
}
|