float literals now parse using musl's 128 bit float code

fixes float literals not having 128 bit precision
master
Andrew Kelley 2019-03-22 14:56:03 -04:00
parent 127bb124a0
commit 4615ed5ea0
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
9 changed files with 1090 additions and 25 deletions

View File

@ -313,6 +313,7 @@ set(EMBEDDED_SOFTFLOAT_SOURCES
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/f32_to_f128M.c" "${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/f32_to_f128M.c"
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/f64_to_f128M.c" "${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/f64_to_f128M.c"
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/f64_to_f16.c" "${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/f64_to_f16.c"
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/i32_to_f128M.c"
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/s_add256M.c" "${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/s_add256M.c"
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/s_addCarryM.c" "${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/s_addCarryM.c"
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/s_addComplCarryM.c" "${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/s_addComplCarryM.c"
@ -427,11 +428,12 @@ set(ZIG_SOURCES
"${CMAKE_SOURCE_DIR}/src/range_set.cpp" "${CMAKE_SOURCE_DIR}/src/range_set.cpp"
"${CMAKE_SOURCE_DIR}/src/target.cpp" "${CMAKE_SOURCE_DIR}/src/target.cpp"
"${CMAKE_SOURCE_DIR}/src/tokenizer.cpp" "${CMAKE_SOURCE_DIR}/src/tokenizer.cpp"
"${CMAKE_SOURCE_DIR}/src/util.cpp"
"${CMAKE_SOURCE_DIR}/src/translate_c.cpp" "${CMAKE_SOURCE_DIR}/src/translate_c.cpp"
"${CMAKE_SOURCE_DIR}/src/util.cpp"
) )
set(BLAKE_SOURCES set(OPTIMIZED_C_SOURCES
"${CMAKE_SOURCE_DIR}/src/blake2b.c" "${CMAKE_SOURCE_DIR}/src/blake2b.c"
"${CMAKE_SOURCE_DIR}/src/parse_f128.c"
) )
set(ZIG_CPP_SOURCES set(ZIG_CPP_SOURCES
"${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp" "${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"
@ -6600,7 +6602,7 @@ else()
endif() endif()
endif() endif()
set(BLAKE_CFLAGS "-std=c99") set(OPTIMIZED_C_FLAGS "-std=c99 -O3")
set(EXE_LDFLAGS " ") set(EXE_LDFLAGS " ")
if(MINGW) if(MINGW)
@ -6626,9 +6628,9 @@ set_target_properties(zig_cpp PROPERTIES
COMPILE_FLAGS ${EXE_CFLAGS} COMPILE_FLAGS ${EXE_CFLAGS}
) )
add_library(embedded_blake STATIC ${BLAKE_SOURCES}) add_library(opt_c_util STATIC ${OPTIMIZED_C_SOURCES})
set_target_properties(embedded_blake PROPERTIES set_target_properties(opt_c_util PROPERTIES
COMPILE_FLAGS "${BLAKE_CFLAGS} -O3" COMPILE_FLAGS "${OPTIMIZED_C_FLAGS}"
) )
add_executable(zig ${ZIG_SOURCES}) add_executable(zig ${ZIG_SOURCES})
@ -6639,7 +6641,7 @@ set_target_properties(zig PROPERTIES
target_link_libraries(zig LINK_PUBLIC target_link_libraries(zig LINK_PUBLIC
zig_cpp zig_cpp
embedded_blake opt_c_util
${SOFTFLOAT_LIBRARIES} ${SOFTFLOAT_LIBRARIES}
${CLANG_LIBRARIES} ${CLANG_LIBRARIES}
${LLD_LIBRARIES} ${LLD_LIBRARIES}

View File

@ -9,6 +9,7 @@
#include "bigint.hpp" #include "bigint.hpp"
#include "buffer.hpp" #include "buffer.hpp"
#include "softfloat.hpp" #include "softfloat.hpp"
#include "parse_f128.h"
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include <errno.h> #include <errno.h>
@ -65,22 +66,18 @@ void bigfloat_init_bigint(BigFloat *dest, const BigInt *op) {
} }
} }
int bigfloat_init_buf_base10(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_len) { Error bigfloat_init_buf(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_len) {
char *str_begin = (char *)buf_ptr; char *str_begin = (char *)buf_ptr;
char *str_end; char *str_end;
errno = 0; errno = 0;
double value = strtod(str_begin, &str_end); // TODO actual f128 parsing dest->value = parse_f128(str_begin, &str_end);
if (errno) { if (errno) {
return ErrorOverflow; return ErrorOverflow;
} }
float64_t value_f64;
memcpy(&value_f64, &value, sizeof(double));
f64_to_f128M(value_f64, &dest->value);
assert(str_end <= ((char*)buf_ptr) + buf_len); assert(str_end <= ((char*)buf_ptr) + buf_len);
return 0; return ErrorNone;
} }
void bigfloat_add(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) { void bigfloat_add(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {

View File

@ -28,7 +28,7 @@ void bigfloat_init_64(BigFloat *dest, double x);
void bigfloat_init_128(BigFloat *dest, float128_t x); void bigfloat_init_128(BigFloat *dest, float128_t x);
void bigfloat_init_bigfloat(BigFloat *dest, const BigFloat *x); void bigfloat_init_bigfloat(BigFloat *dest, const BigFloat *x);
void bigfloat_init_bigint(BigFloat *dest, const BigInt *op); void bigfloat_init_bigint(BigFloat *dest, const BigInt *op);
int bigfloat_init_buf_base10(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_len); Error bigfloat_init_buf(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_len);
float16_t bigfloat_to_f16(const BigFloat *bigfloat); float16_t bigfloat_to_f16(const BigFloat *bigfloat);
float bigfloat_to_f32(const BigFloat *bigfloat); float bigfloat_to_f32(const BigFloat *bigfloat);

1038
src/parse_f128.c Normal file

File diff suppressed because it is too large Load Diff

23
src/parse_f128.h Normal file
View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2015 Andrew Kelley
*
* This file is part of zig, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#ifndef ZIG_PARSE_F128_H
#define ZIG_PARSE_F128_H
#include "softfloat_types.h"
#ifdef __cplusplus
#define ZIG_EXTERN_C extern "C"
#define ZIG_RESTRICT
#else
#define ZIG_EXTERN_C
#define ZIG_RESTRICT restrict
#endif
ZIG_EXTERN_C float128_t parse_f128(const char *ZIG_RESTRICT s, char **ZIG_RESTRICT p);
#endif

View File

@ -293,10 +293,10 @@ static void cancel_token(Tokenize *t) {
} }
static void end_float_token(Tokenize *t) { static void end_float_token(Tokenize *t) {
if (t->radix == 10) { if (t->radix == 10 || t->radix == 16) {
uint8_t *ptr_buf = (uint8_t*)buf_ptr(t->buf) + t->cur_tok->start_pos; uint8_t *ptr_buf = (uint8_t*)buf_ptr(t->buf) + t->cur_tok->start_pos;
size_t buf_len = t->cur_tok->end_pos - t->cur_tok->start_pos; size_t buf_len = t->cur_tok->end_pos - t->cur_tok->start_pos;
if (bigfloat_init_buf_base10(&t->cur_tok->data.float_lit.bigfloat, ptr_buf, buf_len)) { if (bigfloat_init_buf(&t->cur_tok->data.float_lit.bigfloat, ptr_buf, buf_len)) {
t->cur_tok->data.float_lit.overflow = true; t->cur_tok->data.float_lit.overflow = true;
} }
return; return;

View File

@ -4774,7 +4774,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add( cases.add(
"float literal too large error", "float literal too large error",
\\comptime { \\comptime {
\\ const a = 0x1.0p16384; \\ const a = 0x1.0p18495;
\\} \\}
, ,
"tmp.zig:2:15: error: float literal out of range of any type", "tmp.zig:2:15: error: float literal out of range of any type",
@ -4783,7 +4783,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add( cases.add(
"float literal too small error (denormal)", "float literal too small error (denormal)",
\\comptime { \\comptime {
\\ const a = 0x1.0p-16384; \\ const a = 0x1.0p-19000;
\\} \\}
, ,
"tmp.zig:2:15: error: float literal out of range of any type", "tmp.zig:2:15: error: float literal out of range of any type",

View File

@ -385,10 +385,10 @@ test "@setEvalBranchQuota" {
} }
} }
// TODO test "float literal at compile time not lossy" { test "float literal at compile time not lossy" {
// TODO expect(16777216.0 + 1.0 == 16777217.0); expect(16777216.0 + 1.0 == 16777217.0);
// TODO expect(9007199254740992.0 + 1.0 == 9007199254740993.0); expect(9007199254740992.0 + 1.0 == 9007199254740993.0);
// TODO } }
test "f32 at compile time is lossy" { test "f32 at compile time is lossy" {
expect(f32(1 << 24) + 1 == 1 << 24); expect(f32(1 << 24) + 1 == 1 << 24);

View File

@ -324,11 +324,11 @@ test "quad hex float literal parsing accurate" {
} }
{ {
var f: f128 = 0x1.353e45674d89abacc3a2ebf3ff4ffp-50; var f: f128 = 0x1.353e45674d89abacc3a2ebf3ff4ffp-50;
expect(@bitCast(u128, f) == 0x3fcd353e45674d89abacc3a2ebf3ff4f); expect(@bitCast(u128, f) == 0x3fcd353e45674d89abacc3a2ebf3ff50);
} }
{ {
var f: f128 = 0x1.ed8764648369535adf4be3214567fp-9; var f: f128 = 0x1.ed8764648369535adf4be3214567fp-9;
expect(@bitCast(u128, f) == 0x3ff6ed8764648369535adf4be3214567); expect(@bitCast(u128, f) == 0x3ff6ed8764648369535adf4be3214568);
} }
const exp2ft = []f64{ const exp2ft = []f64{
0x1.6a09e667f3bcdp-1, 0x1.6a09e667f3bcdp-1,
@ -597,3 +597,8 @@ test "vector integer addition" {
S.doTheTest(); S.doTheTest();
comptime S.doTheTest(); comptime S.doTheTest();
} }
test "binary and octal float literals" {
expect(0b10100.00010e0 == 0x1.4100000000000p+4);
expect(0o10700.00010e0 == 0x1.1c00010000000p+12);
}