mirror of
https://github.com/apache/nuttx.git
synced 2026-06-05 15:58:59 +08:00
sscanf: Use strtof() instead of strtod() if a short floating point value was requested. The should help performance with MCUs with 32-bit FPU support
This commit is contained in:
+31
-12
@@ -511,7 +511,7 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap)
|
|||||||
else if (strchr("aAfFeEgG", *fmt) != NULL)
|
else if (strchr("aAfFeEgG", *fmt) != NULL)
|
||||||
{
|
{
|
||||||
#ifdef CONFIG_HAVE_DOUBLE
|
#ifdef CONFIG_HAVE_DOUBLE
|
||||||
FAR double_t *pd = NULL;
|
FAR double *pd = NULL;
|
||||||
#endif
|
#endif
|
||||||
FAR float *pf = NULL;
|
FAR float *pf = NULL;
|
||||||
|
|
||||||
@@ -531,7 +531,7 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap)
|
|||||||
#ifdef CONFIG_HAVE_DOUBLE
|
#ifdef CONFIG_HAVE_DOUBLE
|
||||||
if (lflag)
|
if (lflag)
|
||||||
{
|
{
|
||||||
pd = va_arg(ap, FAR double_t *);
|
pd = va_arg(ap, FAR double *);
|
||||||
*pd = 0.0;
|
*pd = 0.0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -581,13 +581,22 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap)
|
|||||||
|
|
||||||
FAR char *endptr;
|
FAR char *endptr;
|
||||||
int errsave;
|
int errsave;
|
||||||
double_t dvalue;
|
|
||||||
|
|
||||||
/* Preserve the errno value */
|
/* Preserve the errno value */
|
||||||
|
|
||||||
errsave = get_errno();
|
errsave = get_errno();
|
||||||
set_errno(0);
|
set_errno(0);
|
||||||
dvalue = strtod(tmp, &endptr);
|
|
||||||
|
/* We have to check whether we need to return a float
|
||||||
|
* or a double.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef CONFIG_HAVE_DOUBLE
|
||||||
|
if (lflag)
|
||||||
|
{
|
||||||
|
/* Get the converted double value */
|
||||||
|
|
||||||
|
double dvalue = strtod(tmp, &endptr);
|
||||||
|
|
||||||
/* Check if the number was successfully converted */
|
/* Check if the number was successfully converted */
|
||||||
|
|
||||||
@@ -598,21 +607,31 @@ int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap)
|
|||||||
|
|
||||||
set_errno(errsave);
|
set_errno(errsave);
|
||||||
|
|
||||||
/* We have to check whether we need to return a float
|
/* Return the double value */
|
||||||
* or a double.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef CONFIG_HAVE_DOUBLE
|
|
||||||
if (lflag)
|
|
||||||
{
|
|
||||||
linfo("vsscanf: Return %f to %p\n", dvalue, pd);
|
linfo("vsscanf: Return %f to %p\n", dvalue, pd);
|
||||||
*pd = dvalue;
|
*pd = dvalue;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
linfo("vsscanf: Return %f to %p\n", dvalue, pf);
|
/* Get the converted float value */
|
||||||
*pf = (float)dvalue;
|
|
||||||
|
float fvalue = strtof(tmp, &endptr);
|
||||||
|
|
||||||
|
/* Check if the number was successfully converted */
|
||||||
|
|
||||||
|
if (tmp == endptr || get_errno() == ERANGE)
|
||||||
|
{
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
set_errno(errsave);
|
||||||
|
|
||||||
|
/* Return the float value */
|
||||||
|
|
||||||
|
linfo("vsscanf: Return %f to %p\n", (double)fvalue, pf);
|
||||||
|
*pf = fvalue;
|
||||||
}
|
}
|
||||||
|
|
||||||
count++;
|
count++;
|
||||||
|
|||||||
Reference in New Issue
Block a user