Merge branch 'feature/nano_libc_support_printf_float' into 'master'

newlib: nano mode libc supports print float type data

See merge request sdk/ESP8266_RTOS_SDK!1056
This commit is contained in:
Dong Heng
2019-08-15 10:44:23 +08:00
7 changed files with 36 additions and 69 deletions

View File

@ -4,7 +4,3 @@ set(COMPONENT_ADD_INCLUDEDIRS cJSON)
set(COMPONENT_REQUIRES newlib)
register_component()
if(CONFIG_NEWLIB_LIBRARY_LEVEL_NORMAL)
target_compile_definitions(${COMPONENT_LIB} PRIVATE -DCJSON_SPRINTF_FLOAT=1)
endif()

View File

@ -56,14 +56,6 @@
#pragma GCC visibility pop
#endif
#ifndef CJSON_SPRINTF_FLOAT
#define CJSON_SPRINTF_FLOAT 0
#endif
#if !CJSON_SPRINTF_FLOAT
#include <assert.h>
#endif
#include "cJSON.h"
/* define our own boolean type */
@ -501,7 +493,6 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
}
else
{
#if CJSON_SPRINTF_FLOAT
double test;
/* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
@ -513,51 +504,6 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
/* If not, print with 17 decimal places of precision */
length = sprintf((char*)number_buffer, "%1.17g", d);
}
#else
long long d64 = (long long)d;
long d32 = (long)d;
/**
* Check if 32-bit type data is overflow.
*/
assert(d32 == d64 && "Library newlib of nano mode does not support double or long-long format, please enable newlib normal mode.");
length = sprintf((char*)number_buffer, "%ld", d32);
if ((double)d32 != d) {
size_t precision = 14;
unsigned char *pbuf = number_buffer;
if (d < 0.0) {
d = (double)d32 - d + 0.00000000000001;
} else {
d = d - (double)d32;
}
pbuf = &number_buffer[length];
*pbuf++ = '.';
length++;
while (d > 0.0 && precision--) {
d *= 10.0;
unsigned char tmp = (unsigned char)d;
*pbuf++ = tmp + '0';
length++;
d -= (double)tmp;
}
pbuf = &number_buffer[length - 1];
while (*pbuf == '0') {
pbuf--;
length--;
}
*++pbuf = 0;
}
#endif
}
/* sprintf failed or buffer overrun occured */

View File

@ -4,7 +4,3 @@
COMPONENT_ADD_INCLUDEDIRS += cJSON
COMPONENT_SRCDIRS := cJSON
ifdef CONFIG_NEWLIB_LIBRARY_LEVEL_NORMAL
CFLAGS += -DCJSON_SPRINTF_FLOAT=1
endif

View File

@ -2,10 +2,12 @@ set(COMPONENT_SRCDIRS newlib/port)
set(COMPONENT_ADD_INCLUDEDIRS newlib/include newlib/port/include)
if(CONFIG_NEWLIB_NANO_FORMAT)
if(CONFIG_NEWLIB_LIBRARY_LEVEL_NANO)
set(LIBC c_nano)
else()
elseif(CONFIG_NEWLIB_LIBRARY_LEVEL_NORMAL)
set(LIBC c)
elseif(CONFIG_NEWLIB_LIBRARY_LEVEL_FLOAT_NANO)
set(LIBC c_fnano)
endif()
set(LIBM m)
@ -14,6 +16,10 @@ set(COMPONENT_PRIV_REQUIRES "vfs" "lwip") # for sys/ioctl.h
register_component()
if(CONFIG_NEWLIB_LIBRARY_LEVEL_FLOAT_NANO)
target_link_libraries(${COMPONENT_LIB} PUBLIC "-u _printf_float" "-u _scanf_float")
endif()
target_compile_definitions(${COMPONENT_LIB} PUBLIC
-D_CLOCKS_PER_SEC_=CONFIG_FREERTOS_HZ -D_POSIX_THREADS=1 -D_UNIX98_THREAD_MUTEX_ATTRIBUTES=1
)

View File

@ -11,7 +11,7 @@ config NEWLIB_ENABLE
choice NEWLIB_LIBRARY_LEVEL
prompt "newlib level"
default NEWLIB_LIBRARY_LEVEL_NANO
default NEWLIB_LIBRARY_LEVEL_FLOAT_NANO
depends on NEWLIB_ENABLE
help
Choose newlib library level.
@ -26,15 +26,29 @@ config NEWLIB_LIBRARY_LEVEL_NANO
bool "nano"
help
The newlib library which has been compiled with so-called "nano"
formatting option. This option doesn't support 64-bit integer formats and C99
features, such as positional arguments.
formatting option. This option doesn't support 64-bit integer formats, C99
features and float formats, such as positional arguments.
For more details about "nano" formatting option, please see newlib readme file,
search for '--enable-newlib-nano-formatted-io':
https://sourceware.org/newlib/README
If you do not need 64-bit integer formatting support or C99 features, select this
option.
If you do not need 64-bit integer formatting support, C99 features and float,
select this option.
config NEWLIB_LIBRARY_LEVEL_FLOAT_NANO
bool "float nano"
help
The newlib library which has been compiled with so-called "float nano"
formatting option. This option doesn't support 64-bit integer formats and C99
features, but support float formats, such as positional arguments.
For more details about "nano" formatting option, please see newlib readme file,
search for '--enable-newlib-nano-formatted-io':
https://sourceware.org/newlib/README
If you do not need 64-bit integer formatting support and C99 features, but need float formats,
select this option.
endchoice

View File

@ -16,6 +16,12 @@ ifdef CONFIG_NEWLIB_LIBRARY_LEVEL_NANO
LIBC_PATH := $(COMPONENT_PATH)/newlib/lib/libc_nano.a
LIBM_PATH := $(COMPONENT_PATH)/newlib/lib/libm.a
ADD_NEW_NEWLIB := 1
else
ifdef CONFIG_NEWLIB_LIBRARY_LEVEL_FLOAT_NANO
LIBC_PATH := $(COMPONENT_PATH)/newlib/lib/libc_fnano.a
LIBM_PATH := $(COMPONENT_PATH)/newlib/lib/libm.a
ADD_NEW_NEWLIB := 1
endif
endif
endif
@ -23,6 +29,9 @@ ifeq ($(ADD_NEW_NEWLIB),1)
COMPONENT_ADD_INCLUDEDIRS += newlib/include newlib/port/include
COMPONENT_SRCDIRS += newlib/port
COMPONENT_ADD_LDFLAGS := $(LIBC_PATH) $(LIBM_PATH) -lnewlib
ifdef CONFIG_NEWLIB_LIBRARY_LEVEL_FLOAT_NANO
COMPONENT_ADD_LDFLAGS := $(COMPONENT_ADD_LDFLAGS) -u _printf_float -u _scanf_float
endif
COMPONENT_ADD_LINKER_DEPS := $(LIBC_PATH) $(LIBM_PATH)
endif

Binary file not shown.