Skip to content

Commit

Permalink
NLWriter: CMake option NLW2_LIB_DMGAY_DTOA #30
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Feb 17, 2025
1 parent e1ccbde commit 2303c9c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
29 changes: 26 additions & 3 deletions nl-writer2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
cmake_minimum_required(VERSION 3.10)


# Minimum MacOS target to enable C++17 std::filesystem
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15)
option(NLW2_LIB_DMGAY_DTOA
"NLW2_LIB: use David M. Gay's dtoa() implementation" OFF)
if (NLW2_LIB_DMGAY_DTOA)
message(STATUS
" NLW2_LIB: using David M. Gay's dtoa() implementation."
)
# Minimum MacOS target to enable C++17 std::filesystem
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15)
else()
message(STATUS
" NLW2_LIB: using std::to_chars().\n"
" On MacOS, requires OS 13.3 or higher."
)
set(CMAKE_OSX_DEPLOYMENT_TARGET 13.3)
endif()


## project() after OSX_DEPLOYMENT_TARGET
project(NLWriter2)


Expand All @@ -25,9 +39,14 @@ set(NLW2_EXAMPLE_PATH ${NLW2_DIR}/examples)
set(NLW2_LIB_FILES
${NLW2_SRC_PATH}/nl-writer2.cc
${NLW2_SRC_PATH}/nl-utils.cc
# ${NLW2_SRC_PATH}/dtoa.cc
${NLW2_SRC_PATH}/nl-solver.cc
)
if (NLW2_LIB_DMGAY_DTOA)
set(NLW2_LIB_FILES
${NLW2_LIB_FILES}
${NLW2_SRC_PATH}/dtoa.cc
)
endif()
set(NLW2_INC_FILES
${NLW2_INCLUDE_PATH}/mp/nl-solver-basics-c.h
${NLW2_INCLUDE_PATH}/mp/nl-model.h
Expand Down Expand Up @@ -56,6 +75,10 @@ if (NLW2_LIB_ORIG_HDR)
target_compile_definitions(
${NLW2_LIB_NAME} PUBLIC NL_LIB2_ORIG_HDR)
endif()
if (NLW2_LIB_DMGAY_DTOA)
target_compile_definitions(
${NLW2_LIB_NAME} PUBLIC NLW2_LIB_DMGAY_DTOA)
endif()

# C API sources
set(NLW2_C_API_LIB_FILES
Expand Down
17 changes: 11 additions & 6 deletions nl-writer2/src/nl-writer2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ int TextFormatter::apr(File& f, const char *fmt, ...)
while (*fmt++ != 'g');
case 'g':
x = va_arg(ap, double);
#define NL_LIB_USE_TO_CHARS
#ifdef NL_LIB_USE_TO_CHARS
#ifndef NLW2_LIB_DMGAY_DTOA
#ifndef NL_LIB_USE_OWN_GFMT
std::to_chars_result res;
if (output_prec <= 0) // shortest representation
res = std::to_chars(s = buf, buf+sizeof(buf)-1, x);
Expand All @@ -168,9 +168,10 @@ int TextFormatter::apr(File& f, const char *fmt, ...)
else
Utils().myexit("aprintf / to_chars bug: " +
std::make_error_code(res.ec).message());
#elif NL_LIB_USE_OWN_GFMT
#else // NL_LIB_USE_OWN_GFMT
NL_LIB_GFMT::gfmt(s = buf, sizeof(buf), x, output_prec);
#else
#endif // NL_LIB_USE_OWN_GFMT
#else // NLW2_LIB_DMGAY_DTOA
s = DAVID_GAY_GFMT::gfmt(x, output_prec);
#endif
goto have_s;
Expand Down Expand Up @@ -319,7 +320,7 @@ apr(File& f, const char *fmt, ...)
} // namespace mp


#ifndef NL_LIB_USE_TO_CHARS
#ifdef NLW2_LIB_DMGAY_DTOA

extern "C" {
char *
Expand Down Expand Up @@ -417,6 +418,10 @@ gfmt(double x, int prec)

} // namespace DAVID_GAY_GFMT

#endif // NLW2_LIB_DMGAY_DTOA


#ifdef NL_LIB_USE_OWN_GFMT

namespace NL_LIB_GFMT {

Expand Down Expand Up @@ -475,4 +480,4 @@ void gfmt(char *b, size_t sz, double x, int prec) {

} // namespace NL_LIB_GFMT

#endif // NL_LIB_USE_TO_CHARS
#endif // NL_LIB_USE_OWN_GFMT

0 comments on commit 2303c9c

Please sign in to comment.