Brijesh-Thakkar commented on code in PR #21537:
URL: https://github.com/apache/nuttx/pull/21537#discussion_r4505945306
##########
libs/libc/stream/lib_libvscanf.c:
##########
@@ -1101,7 +1111,36 @@ static int vscanf_internal(FAR struct lib_instream_s
*stream, FAR int *lastc,
/* Return the float value */
# ifdef CONFIG_HAVE_FLOAT
- *pf = fvalue;
+# ifdef CONFIG_LIBC_BFORMAT_B16
+ if (modifier == H_MOD && !varg)
+ {
+ /* Binary format: %hf is a b16_t, rounded to
+ * the nearest value and saturated.
+ */
+
+ FAR b16_t *pb16 = (FAR b16_t *)pf;
+
+ if (fvalue >= 32768.0f)
+ {
+ *pb16 = b16MAX;
+ }
+ else if (fvalue <= -32768.0f)
+ {
+ *pb16 = b16MIN;
+ }
+ else
+ {
+ float r = fvalue * 65536.0f;
Review Comment:
strtof() had already rounded to 24 bits before the multiply, so double math
afterwards wouldn't have helped. With CONFIG_HAVE_DOUBLE, a b16_t %hf is now
parsed with strtod() and rounded in double, in a small helper that also
saturates. I compared it against exact rational rounding on 100016 host values
(random, near 300, near the limits): 0 mismatches. On sim, 300.0001 now stores
0x012c0007 (it was 0x012c0006) and 32767.99 stores 0x7ffffd71. Without
CONFIG_HAVE_DOUBLE it still converts in float and is only exact up to 256;
I've said that in the commit message and the PR body.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]