Skip to content

Commit

Permalink
float literals now parse using musl's 128 bit float code
Browse files Browse the repository at this point in the history
fixes float literals not having 128 bit precision
  • Loading branch information
andrewrk committed Mar 22, 2019
1 parent 127bb12 commit 4615ed5
Show file tree
Hide file tree
Showing 9 changed files with 1,090 additions and 25 deletions.
16 changes: 9 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/f64_to_f128M.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_addCarryM.c"
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/s_addComplCarryM.c"
Expand Down Expand Up @@ -427,11 +428,12 @@ set(ZIG_SOURCES
"${CMAKE_SOURCE_DIR}/src/range_set.cpp"
"${CMAKE_SOURCE_DIR}/src/target.cpp"
"${CMAKE_SOURCE_DIR}/src/tokenizer.cpp"
"${CMAKE_SOURCE_DIR}/src/util.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/parse_f128.c"
)
set(ZIG_CPP_SOURCES
"${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"
Expand Down Expand Up @@ -6600,7 +6602,7 @@ else()
endif()
endif()

set(BLAKE_CFLAGS "-std=c99")
set(OPTIMIZED_C_FLAGS "-std=c99 -O3")

set(EXE_LDFLAGS " ")
if(MINGW)
Expand All @@ -6626,9 +6628,9 @@ set_target_properties(zig_cpp PROPERTIES
COMPILE_FLAGS ${EXE_CFLAGS}
)

add_library(embedded_blake STATIC ${BLAKE_SOURCES})
set_target_properties(embedded_blake PROPERTIES
COMPILE_FLAGS "${BLAKE_CFLAGS} -O3"
add_library(opt_c_util STATIC ${OPTIMIZED_C_SOURCES})
set_target_properties(opt_c_util PROPERTIES
COMPILE_FLAGS "${OPTIMIZED_C_FLAGS}"
)

add_executable(zig ${ZIG_SOURCES})
Expand All @@ -6639,7 +6641,7 @@ set_target_properties(zig PROPERTIES

target_link_libraries(zig LINK_PUBLIC
zig_cpp
embedded_blake
opt_c_util
${SOFTFLOAT_LIBRARIES}
${CLANG_LIBRARIES}
${LLD_LIBRARIES}
Expand Down
11 changes: 4 additions & 7 deletions src/bigfloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "bigint.hpp"
#include "buffer.hpp"
#include "softfloat.hpp"
#include "parse_f128.h"
#include <stdio.h>
#include <math.h>
#include <errno.h>
Expand Down Expand Up @@ -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_end;

errno = 0;
double value = strtod(str_begin, &str_end); // TODO actual f128 parsing
dest->value = parse_f128(str_begin, &str_end);
if (errno) {
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);
return 0;
return ErrorNone;
}

void bigfloat_add(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
Expand Down
2 changes: 1 addition & 1 deletion src/bigfloat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void bigfloat_init_64(BigFloat *dest, double x);
void bigfloat_init_128(BigFloat *dest, float128_t x);
void bigfloat_init_bigfloat(BigFloat *dest, const BigFloat *x);
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);
float bigfloat_to_f32(const BigFloat *bigfloat);
Expand Down

0 comments on commit 4615ed5

Please sign in to comment.