Skip to content

Commit 2d8b9de

Browse files
Cynerdxiaoxiang781216
authored andcommitted
float.h: improve long double related definitions
This addresses "FIX ME" definitions for the long double. These might not be all long double format supported by NuttX. The support was also added only for GCC alike compilers. Other compilers that have CONFIG_HAVE_LONG_DOUBLE defined will fail with error that mantisage digits have to be defined for that compiler. Signed-off-by: Karel Kočí <kkoci@elektroline.cz>
1 parent 961fd8f commit 2d8b9de

2 files changed

Lines changed: 84 additions & 10 deletions

File tree

include/nuttx/compiler.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,12 @@
621621

622622
# endif
623623

624+
/* Use compiler definition to define long double mantisa digits */
625+
626+
# ifdef CONFIG_HAVE_LONG_DOUBLE
627+
# define LDBL_MANT_DIG __LDBL_MANT_DIG__
628+
# endif
629+
624630
/* Indicate that a local variable is not used */
625631

626632
# ifndef UNUSED
@@ -1201,6 +1207,10 @@
12011207

12021208
# define memory_barrier()
12031209

1210+
/* long double is suppose to be same as double on MSVC. */
1211+
1212+
# define LDBL_MANT_DIG 53
1213+
12041214
/* TASKING (Infineon AURIX C/C++)-specific definitions **********************/
12051215

12061216
#elif defined(__TASKING__)

include/nuttx/lib/float.h

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
#ifndef LDBL_MANT_DIG /* May be defined in a toolchain header */
6666
# ifdef CONFIG_HAVE_LONG_DOUBLE
67-
# define LDBL_MANT_DIG DBL_MANT_DIG /* FIX ME */
67+
# error "LDBL_MANT_DIG has to be defined in compiler.h"
6868
# else
6969
# define LDBL_MANT_DIG DBL_MANT_DIG
7070
# endif
@@ -96,8 +96,16 @@
9696
#endif
9797

9898
#ifndef LDBL_DIG /* May be defined in a toolchain header */
99-
# ifdef CONFIG_HAVE_LONG_DOUBLE
100-
# define LDBL_DIG DBL_DIG /* FIX ME */
99+
# ifdef CONFIG_HAVE_LONG_DOUBLE
100+
# if LDBL_MANT_DIG == 53 /* IEEE binary64 */
101+
# define LDBL_DIG DBL_DIG
102+
# elif LDBL_MANT_DIG == 64 /* x87 80-bit */
103+
# define LDBL_DIG 18
104+
# elif LDBL_MANT_DIG == 113 /* IEEE binary128 */
105+
# define LDBL_DIG 33
106+
# else
107+
# error "Unknown long double format"
108+
# endif
101109
# else
102110
# define LDBL_DIG DBL_DIG
103111
# endif
@@ -121,7 +129,15 @@
121129

122130
#ifndef LDBL_MIN_EXP /* May be defined in a toolchain header */
123131
# ifdef CONFIG_HAVE_LONG_DOUBLE
124-
# define LDBL_MIN_EXP DBL_MIN_EXP /* FIX ME */
132+
# if LDBL_MANT_DIG == 53 /* IEEE binary64 */
133+
# define LDBL_MIN_EXP DBL_MIN_EXP
134+
# elif LDBL_MANT_DIG == 64 /* x87 80-bit */
135+
# define LDBL_MIN_EXP (-16381)
136+
# elif LDBL_MANT_DIG == 113 /* IEEE binary128 */
137+
# define LDBL_MIN_EXP (-16381)
138+
# else
139+
# error "Unknown long double format"
140+
# endif
125141
# else
126142
# define LDBL_MIN_EXP DBL_MIN_EXP
127143
# endif
@@ -145,7 +161,15 @@
145161

146162
#ifndef LDBL_MIN_10_EXP /* May be defined in a toolchain header */
147163
# ifdef CONFIG_HAVE_LONG_DOUBLE
148-
# define LDBL_MIN_10_EXP DBL_MIN_10_EXP /* FIX ME */
164+
# if LDBL_MANT_DIG == 53 /* IEEE binary64 */
165+
# define LDBL_MIN_10_EXP DBL_MIN_10_EXP
166+
# elif LDBL_MANT_DIG == 64 /* x87 80-bit */
167+
# define LDBL_MIN_10_EXP (-4931)
168+
# elif LDBL_MANT_DIG == 113 /* IEEE binary128 */
169+
# define LDBL_MIN_10_EXP (-4931)
170+
# else
171+
# error "Unknown long double format"
172+
# endif
149173
# else
150174
# define LDBL_MIN_10_EXP DBL_MIN_10_EXP
151175
# endif
@@ -169,7 +193,15 @@
169193

170194
#ifndef LDBL_MAX_EXP /* May be defined in a toolchain header */
171195
# ifdef CONFIG_HAVE_LONG_DOUBLE
172-
# define LDBL_MAX_EXP DBL_MAX_EXP /* FIX ME */
196+
# if LDBL_MANT_DIG == 53 /* IEEE binary64 */
197+
# define LDBL_MAX_EXP DBL_MAX_EXP
198+
# elif LDBL_MANT_DIG == 64 /* x87 80-bit */
199+
# define LDBL_MAX_EXP 16384
200+
# elif LDBL_MANT_DIG == 113 /* IEEE binary128 */
201+
# define LDBL_MAX_EXP 16384
202+
# else
203+
# error "Unknown long double format"
204+
# endif
173205
# else
174206
# define LDBL_MAX_EXP DBL_MAX_EXP
175207
# endif
@@ -193,7 +225,15 @@
193225

194226
#ifndef LDBL_MAX_10_EXP /* May be defined in toolchain header */
195227
# ifdef CONFIG_HAVE_LONG_DOUBLE
196-
# define LDBL_MAX_10_EXP DBL_MAX_10_EXP /* FIX ME */
228+
# if LDBL_MANT_DIG == 53 /* IEEE binary64 */
229+
# define LDBL_MAX_10_EXP DBL_MAX_10_EXP
230+
# elif LDBL_MANT_DIG == 64 /* x87 80-bit */
231+
# define LDBL_MAX_10_EXP 4932
232+
# elif LDBL_MANT_DIG == 113 /* IEEE binary128 */
233+
# define LDBL_MAX_10_EXP 4932
234+
# else
235+
# error "Unknown long double format"
236+
# endif
197237
# else
198238
# define LDBL_MAX_10_EXP DBL_MAX_10_EXP
199239
# endif
@@ -215,7 +255,15 @@
215255

216256
#ifndef LDBL_MAX /* May be defined in toolchain header */
217257
# ifdef CONFIG_HAVE_LONG_DOUBLE
218-
# define LDBL_MAX DBL_MAX /* FIX ME */
258+
# if LDBL_MANT_DIG == 53 /* IEEE binary64 */
259+
# define LDBL_MAX DBL_MAX
260+
# elif LDBL_MANT_DIG == 64 /* x87 80-bit */
261+
# define LDBL_MAX 1.18973149535723176502126385303097021e+4932L
262+
# elif LDBL_MANT_DIG == 113 /* IEEE binary128 */
263+
# define LDBL_MAX 1.18973149535723176508575932662800702e+4932L
264+
# else
265+
# error "Unknown long double format"
266+
# endif
219267
# else
220268
# define LDBL_MAX DBL_MAX
221269
# endif
@@ -239,7 +287,15 @@
239287

240288
#ifndef LDBL_EPSILON /* May be defined in toolchain header */
241289
# ifdef CONFIG_HAVE_LONG_DOUBLE
242-
# define LDBL_EPSILON DBL_EPSILON /* FIX ME */
290+
# if LDBL_MANT_DIG == 53 /* IEEE binary64 */
291+
# define LDBL_EPSILON DBL_EPSILON
292+
# elif LDBL_MANT_DIG == 64 /* x87 80-bit */
293+
# define LDBL_EPSILON 1.08420217248550443400745280086994171e-19L
294+
# elif LDBL_MANT_DIG == 113 /* IEEE binary128 */
295+
# define LDBL_EPSILON 1.92592994438723585305597794258492732e-34L
296+
# else
297+
# error "Unknown long double format"
298+
# endif
243299
# else
244300
# define LDBL_EPSILON DBL_EPSILON
245301
# endif
@@ -261,7 +317,15 @@
261317

262318
#ifndef LDBL_MIN /* May be defined in toolchain header */
263319
# ifdef CONFIG_HAVE_LONG_DOUBLE
264-
# define LDBL_MIN DBL_MIN /* FIX ME */
320+
# if LDBL_MANT_DIG == 53 /* IEEE binary64 */
321+
# define LDBL_MIN DBL_MIN
322+
# elif LDBL_MANT_DIG == 64 /* x87 80-bit */
323+
# define LDBL_MIN 3.36210314311209350626267781732175260e-4932L
324+
# elif LDBL_MANT_DIG == 113 /* IEEE binary128 */
325+
# define LDBL_MIN 3.36210314311209350626267781732175260e-4932L
326+
# else
327+
# error "Unknown long double format"
328+
# endif
265329
# else
266330
# define LDBL_MIN DBL_MIN
267331
# endif

0 commit comments

Comments
 (0)