libc:Add macro restrictions to code that uses floating point numbers

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao 2024-05-15 10:39:58 +08:00 committed by Xiang Xiao
parent bed1845333
commit 3fdff878bc
11 changed files with 46 additions and 17 deletions

View File

@ -217,7 +217,7 @@ time_t time(FAR time_t *timep);
#ifdef CONFIG_HAVE_DOUBLE #ifdef CONFIG_HAVE_DOUBLE
double difftime(time_t time1, time_t time0); double difftime(time_t time1, time_t time0);
#else #elif defined(CONFIG_HAVE_FLOAT)
float difftime(time_t time1, time_t time0); float difftime(time_t time1, time_t time0);
#endif #endif

View File

@ -202,7 +202,9 @@ FAR wchar_t *wcsstr(FAR const wchar_t *, FAR const wchar_t *);
#ifdef CONFIG_HAVE_DOUBLE #ifdef CONFIG_HAVE_DOUBLE
double wcstod(FAR const wchar_t *, FAR wchar_t **); double wcstod(FAR const wchar_t *, FAR wchar_t **);
#endif #endif
#ifdef CONFIG_HAVE_FLOAT
float wcstof(FAR const wchar_t *, FAR wchar_t **); float wcstof(FAR const wchar_t *, FAR wchar_t **);
#endif
FAR wchar_t *wcstok(FAR wchar_t *, FAR const wchar_t *, FAR wchar_t **); FAR wchar_t *wcstok(FAR wchar_t *, FAR const wchar_t *, FAR wchar_t **);
long int wcstol(FAR const wchar_t *, FAR wchar_t **, int); long int wcstol(FAR const wchar_t *, FAR wchar_t **, int);
#ifdef CONFIG_HAVE_LONG_DOUBLE #ifdef CONFIG_HAVE_LONG_DOUBLE

View File

@ -174,7 +174,10 @@ doswitch:
} }
doexit: doexit:
if (v) /* Default => accept */
/* Default => accept */
if (v)
{ {
for (i = 0; i < 32; i++) /* Invert all */ for (i = 0; i < 32; i++) /* Invert all */
{ {
@ -950,7 +953,10 @@ int lib_vscanf(FAR struct lib_instream_s *stream, FAR int *lastc,
#ifdef CONFIG_HAVE_DOUBLE #ifdef CONFIG_HAVE_DOUBLE
FAR double *pd = NULL; FAR double *pd = NULL;
#endif #endif
#ifdef CONFIG_HAVE_FLOAT
FAR float *pf = NULL; FAR float *pf = NULL;
#endif
linfo("Performing floating point conversion\n"); linfo("Performing floating point conversion\n");
@ -973,10 +979,12 @@ int lib_vscanf(FAR struct lib_instream_s *stream, FAR int *lastc,
} }
else else
#endif #endif
#ifdef CONFIG_HAVE_FLOAT
{ {
pf = va_arg(ap, FAR float *); pf = va_arg(ap, FAR float *);
*pf = 0.0; *pf = 0.0;
} }
#endif
} }
#ifdef CONFIG_LIBC_FLOATINGPOINT #ifdef CONFIG_LIBC_FLOATINGPOINT
@ -994,7 +1002,9 @@ int lib_vscanf(FAR struct lib_instream_s *stream, FAR int *lastc,
if (c > 0) if (c > 0)
{ {
# if defined(CONFIG_HAVE_DOUBLE) || defined(CONFIG_HAVE_FLOAT)
FAR char *endptr; FAR char *endptr;
# endif
bool expnt; bool expnt;
bool dot; bool dot;
bool sign; bool sign;
@ -1003,8 +1013,9 @@ int lib_vscanf(FAR struct lib_instream_s *stream, FAR int *lastc,
# ifdef CONFIG_HAVE_DOUBLE # ifdef CONFIG_HAVE_DOUBLE
double dvalue; double dvalue;
# endif # endif
# ifdef CONFIG_HAVE_FLOAT
float fvalue; float fvalue;
# endif
/* Was a fieldwidth specified? */ /* Was a fieldwidth specified? */
if (!width || width > sizeof(tmp) - 1) if (!width || width > sizeof(tmp) - 1)
@ -1093,17 +1104,21 @@ int lib_vscanf(FAR struct lib_instream_s *stream, FAR int *lastc,
} }
else else
# endif # endif
# ifdef CONFIG_HAVE_FLOAT
{ {
fvalue = strtof(tmp, &endptr); fvalue = strtof(tmp, &endptr);
} }
# endif
/* Check if the number was successfully converted */ /* Check if the number was successfully converted */
# if defined(CONFIG_HAVE_DOUBLE) || defined(CONFIG_HAVE_FLOAT)
if (tmp == endptr || get_errno() == ERANGE) if (tmp == endptr || get_errno() == ERANGE)
{ {
*lastc = c; *lastc = c;
return assigncount; return assigncount;
} }
#endif
set_errno(errsave); set_errno(errsave);
@ -1126,8 +1141,10 @@ int lib_vscanf(FAR struct lib_instream_s *stream, FAR int *lastc,
{ {
/* Return the float value */ /* Return the float value */
# ifdef CONFIG_HAVE_FLOAT
linfo("Return %f to %p\n", (double)fvalue, pf); linfo("Return %f to %p\n", (double)fvalue, pf);
*pf = fvalue; *pf = fvalue;
# endif
} }
assigncount++; assigncount++;

View File

@ -131,7 +131,9 @@ struct arg_s
#ifdef CONFIG_HAVE_LONG_LONG #ifdef CONFIG_HAVE_LONG_LONG
unsigned long long ull; unsigned long long ull;
#endif #endif
#ifdef CONFIG_HAVE_DOUBLE
double d; double d;
#endif
FAR char *cp; FAR char *cp;
} value; } value;
}; };
@ -1350,9 +1352,11 @@ int lib_vsprintf(FAR struct lib_outstream_s *stream,
arglist[i].value.u = va_arg(ap, unsigned int); arglist[i].value.u = va_arg(ap, unsigned int);
break; break;
#ifdef CONFIG_HAVE_DOUBLE
case TYPE_DOUBLE: case TYPE_DOUBLE:
arglist[i].value.d = va_arg(ap, double); arglist[i].value.d = va_arg(ap, double);
break; break;
#endif
case TYPE_CHAR_POINTER: case TYPE_CHAR_POINTER:
arglist[i].value.cp = va_arg(ap, FAR char *); arglist[i].value.cp = va_arg(ap, FAR char *);

View File

@ -8,10 +8,11 @@ menu "stdlib Options"
config LIBC_RAND_ORDER config LIBC_RAND_ORDER
int "Order of the random number generate" int "Order of the random number generate"
default 1 default 1
range 1 3 range 0 3
---help--- ---help---
The order of the random number generator. 1=fast but very bad random The order of the random number generator. 0=fast but very bad random
numbers, 3=slow but very good random numbers. numbers, 3=slow but very good random numbers.
0 just use integer generation, 1-3 use floating point generation
config LIBC_HOMEDIR config LIBC_HOMEDIR
string "Home directory" string "Home directory"

View File

@ -218,6 +218,7 @@ static float_t frand3(void)
static unsigned long nrand_r(unsigned long limit, static unsigned long nrand_r(unsigned long limit,
FAR unsigned long *seed) FAR unsigned long *seed)
{ {
#if CONFIG_LIBC_RAND_ORDER > 0
unsigned long result; unsigned long result;
float_t ratio; float_t ratio;
@ -231,7 +232,7 @@ static unsigned long nrand_r(unsigned long limit,
ratio = frand1(seed); ratio = frand1(seed);
# elif (CONFIG_LIBC_RAND_ORDER == 2) # elif (CONFIG_LIBC_RAND_ORDER == 2)
ratio = frand2(); ratio = frand2();
#else /* if (CONFIG_LIBC_RAND_ORDER > 2) */ # elif (CONFIG_LIBC_RAND_ORDER > 2)
ratio = frand3(); ratio = frand3();
#endif #endif
@ -246,6 +247,9 @@ static unsigned long nrand_r(unsigned long limit,
while (result >= limit); while (result >= limit);
return result; return result;
#else
return fgenerate1(seed) % limit;
#endif
} }
/**************************************************************************** /****************************************************************************

View File

@ -94,6 +94,8 @@
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_HAVE_LONG_DOUBLE
/**************************************************************************** /****************************************************************************
* Name: scanexp * Name: scanexp
* *
@ -769,15 +771,11 @@ float strtof(FAR const char *str, FAR char **endptr)
* *
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_HAVE_DOUBLE
double strtod(FAR const char *str, FAR char **endptr) double strtod(FAR const char *str, FAR char **endptr)
{ {
return strtox(str, endptr, 2); return strtox(str, endptr, 2);
} }
#endif /* CONFIG_HAVE_DOUBLE */
/**************************************************************************** /****************************************************************************
* Name: strtold * Name: strtold
* *
@ -793,11 +791,8 @@ double strtod(FAR const char *str, FAR char **endptr)
* *
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_HAVE_LONG_DOUBLE
long double strtold(FAR const char *str, FAR char **endptr) long double strtold(FAR const char *str, FAR char **endptr)
{ {
return strtox(str, endptr, 3); return strtox(str, endptr, 3);
} }
#endif /* CONFIG_HAVE_LONG_DOUBLE */ #endif /* CONFIG_HAVE_LONG_DOUBLE */

View File

@ -48,7 +48,7 @@ double difftime(time_t time1, time_t time0)
{ {
return (double)time1 - (double)time0; return (double)time1 - (double)time0;
} }
#else #elif defined(CONFIG_HAVE_FLOAT)
float difftime(time_t time1, time_t time0) float difftime(time_t time1, time_t time0)
{ {
if (time1 >= time0) if (time1 >= time0)

View File

@ -48,7 +48,9 @@
* *
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_HAVE_DOUBLE
double wcstod(FAR const wchar_t *nptr, FAR wchar_t **endptr) double wcstod(FAR const wchar_t *nptr, FAR wchar_t **endptr)
{ {
return strtod((FAR const char *)nptr, (FAR char **)endptr); return strtod((FAR const char *)nptr, (FAR char **)endptr);
} }
#endif

View File

@ -48,7 +48,9 @@
* *
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_HAVE_FLOAT
float wcstof(FAR const wchar_t *nptr, FAR wchar_t **endptr) float wcstof(FAR const wchar_t *nptr, FAR wchar_t **endptr)
{ {
return strtof((FAR const char *)nptr, (FAR char **)endptr); return strtof((FAR const char *)nptr, (FAR char **)endptr);
} }
#endif

View File

@ -48,7 +48,9 @@
* *
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_HAVE_LONG_DOUBLE
long double wcstold(FAR const wchar_t *nptr, FAR wchar_t **endptr) long double wcstold(FAR const wchar_t *nptr, FAR wchar_t **endptr)
{ {
return strtold((FAR const char *)nptr, (FAR char **)endptr); return strtold((FAR const char *)nptr, (FAR char **)endptr);
} }
#endif