Skip to content

Commit

Permalink
Merge pull request #680 from akheron/dtoa
Browse files Browse the repository at this point in the history
Use `dtoa()` for optimal encoding of reals
  • Loading branch information
akheron authored Mar 31, 2024
2 parents 8660da0 + c780171 commit 50953fb
Show file tree
Hide file tree
Showing 20 changed files with 6,489 additions and 29 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ jobs:
matrix:
os: ["ubuntu-latest", "macos-latest"]
cc: ["gcc", "clang"]
dtoa: ["yes", "no"]

runs-on: ${{matrix.os}}
runs-on: ${{ matrix.os }}

steps:
- if: ${{runner.os == 'macOS'}}
run: brew install autoconf automake libtool
- uses: actions/checkout@v4
- run: autoreconf -fi
- env:
CC: ${{matrix.cc}}
CC: ${{ matrix.cc }}
CFLAGS: -Werror
run: ./configure
run: ./configure --enable-dtoa=${{ matrix.dtoa }}
- run: make check

cmake:
Expand Down
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Work in progress
is used to switch locales inside the threads (#674, #675, #677. Thanks to
Bruno Haible the report and help with fixing.)

- Use David M. Gay's `dtoa()` algorithm to avoid misprinting issues of real
numbers that are not exactly representable as a `double` (#680).

If this is not desirable, use `./configure --disable-dtoa` or `cmake
-DUSE_DTOA=OFF .`

* Build:

- Make test output nicer in CMake based builds (#683)
Expand Down
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ project(jansson C)
option(JANSSON_BUILD_SHARED_LIBS "Build shared libraries." OFF)
option(USE_URANDOM "Use /dev/urandom to seed the hash function." ON)
option(USE_WINDOWS_CRYPTOAPI "Use CryptGenRandom to seed the hash function." ON)
option(USE_DTOA "Use dtoa for optimal floating-point to string conversions." ON)

if (MSVC)
# This option must match the settings used in your program, in particular if you
Expand Down Expand Up @@ -93,6 +94,9 @@ check_function_exists (sched_yield HAVE_SCHED_YIELD)
# Check for the int-type includes
check_include_files (stdint.h HAVE_STDINT_H)

include (TestBigEndian)
TEST_BIG_ENDIAN(WORDS_BIGENDIAN)

# Check our 64 bit integer sizes
check_type_size (__int64 __INT64)
check_type_size (int64_t INT64_T)
Expand Down Expand Up @@ -193,6 +197,8 @@ endif ()
# detect what to use for the 64 bit type.
# Note: I will prefer long long if I can get it, as that is what the automake system aimed for.
if (NOT DEFINED JSON_INT_T)
set (JSON_INTEGER_IS_LONG_LONG 1)

if (HAVE_LONG_LONG_INT AND (LONG_LONG_INT EQUAL 8))
set (JSON_INT_T "long long")
elseif (HAVE_INT64_T)
Expand Down Expand Up @@ -274,6 +280,9 @@ include_directories (${CMAKE_CURRENT_BINARY_DIR}/private_include)

# Add the lib sources.
file(GLOB JANSSON_SRC src/*.c)
if (NOT USE_DTOA)
list(FILTER JANSSON_SRC EXCLUDE REGEX ".*dtoa\\.c$")
endif()

set(JANSSON_HDR_PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/hashtable.h
Expand Down Expand Up @@ -526,6 +535,11 @@ if (NOT JANSSON_WITHOUT_TESTS)
if (IS_DIRECTORY ${TESTDIR})
get_filename_component(TNAME ${TESTDIR} NAME)

if ((USE_DTOA AND EXISTS ${TESTDIR}/skip_if_dtoa) OR
(NOT USE_DTOA AND EXISTS ${TESTDIR}/skip_unless_dtoa))
continue()
endif()

if (JANSSON_TEST_WITH_VALGRIND)
add_test(memcheck__${SUITE}__${TNAME}
${MEMCHECK_COMMAND} ${SUITE_TEST_CMD} ${TESTDIR})
Expand Down
26 changes: 25 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
Copyright (c) 2009-2020 Petri Lehtinen <[email protected]>
# License

This project is licensed under the MIT license, except where otherwise noted.
The full text of the MIT license is included below.

## MIT License

Copyright (c) 2009-2024 Petri Lehtinen <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -17,3 +24,20 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

## Exceptions

### `src/dtoa.c`

Copyright (c) 1991, 2000, 2001 by Lucent Technologies.

Permission to use, copy, modify, and distribute this software for any
purpose without fee is hereby granted, provided that this entire notice
is included in all copies of any software which is or includes a copy
or modification of this software and in all copies of the supporting
documentation for such software.

THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
7 changes: 4 additions & 3 deletions cmake/jansson_config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
#define JANSSON_USING_CMAKE
#endif

/* Note: when using cmake, JSON_INTEGER_IS_LONG_LONG is not defined nor used,
* as we will also check for __int64 etc types.
* (the definition was used in the automake system) */
/* If your compiler supports the `long long` type and the strtoll()
library function, JSON_INTEGER_IS_LONG_LONG is defined to 1,
otherwise to 0. */
#cmakedefine JSON_INTEGER_IS_LONG_LONG 1

/* Bring in the cmake-detected defines */
#cmakedefine HAVE_STDINT_H 1
Expand Down
9 changes: 9 additions & 0 deletions cmake/jansson_private_config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#cmakedefine HAVE_LOCALE_H 1
#cmakedefine HAVE_SETLOCALE 1

#cmakedefine WORDS_BIGENDIAN 1

#cmakedefine HAVE_INT32_T 1
#ifndef HAVE_INT32_T
# define int32_t @JSON_INT32@
Expand Down Expand Up @@ -50,4 +52,11 @@
#cmakedefine USE_URANDOM 1
#cmakedefine USE_WINDOWS_CRYPTOAPI 1

#cmakedefine USE_DTOA 1
#if USE_DTOA
# define DTOA_ENABLED 1
#else
# define DTOA_ENABLED 0
#endif

#define INITIAL_HASHTABLE_ORDER @JANSSON_INITIAL_HASHTABLE_ORDER@
15 changes: 15 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ AC_TYPE_UINT16_T
AC_TYPE_UINT8_T
AC_TYPE_LONG_LONG_INT

AC_C_BIGENDIAN

AC_C_INLINE
case $ac_cv_c_inline in
yes) json_inline=inline;;
Expand Down Expand Up @@ -136,6 +138,19 @@ JSON_SYMVER_LDFLAGS=
AC_CHECK_DECL([__GLIBC__], [JSON_SYMVER_LDFLAGS=-Wl,--default-symver])
AC_SUBST([JSON_SYMVER_LDFLAGS])

AC_ARG_ENABLE([dtoa],
[AS_HELP_STRING([--enable-dtoa], [Use dtoa for optimal floating point to string conversion])],
[case "$enableval" in
yes) dtoa=yes ;;
no) dtoa=no ;;
*) AC_MSG_ERROR([bad value ${enableval} for --enable-dtoa]) ;;
esac], [dtoa=yes])
if test "$dtoa" = "yes"; then
AC_DEFINE([DTOA_ENABLED], [1],
[Define to 1 to use dtoa to convert floating points to strings])
fi
AM_CONDITIONAL([DTOA_ENABLED], [test "$dtoa" = "yes"])

AC_ARG_ENABLE([ossfuzzers],
[AS_HELP_STRING([--enable-ossfuzzers],
[Whether to generate the fuzzers for OSS-Fuzz])],
Expand Down
7 changes: 6 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
EXTRA_DIST = jansson.def
EXTRA_DIST = jansson.def dtoa.c

include_HEADERS = jansson.h
nodist_include_HEADERS = jansson_config.h
Expand All @@ -22,6 +22,11 @@ libjansson_la_SOURCES = \
utf.h \
value.c \
version.c

if DTOA_ENABLED
libjansson_la_SOURCES += dtoa.c
endif

libjansson_la_LDFLAGS = \
-no-undefined \
-export-symbols-regex '^json_|^jansson_' \
Expand Down
Loading

0 comments on commit 50953fb

Please sign in to comment.