1010#include <stdarg.h>
1111#include <math.h>
1212#include "nanoprintf.h"
13+ #include "nanoprintf_cfg.h"
1314#include <float.h>
1415
1516/* Define default macro to access the format string using a pointer. */
@@ -251,63 +252,48 @@ static char *format_float(double number, flt_width_t ndigits, flt_width_t width,
251252#endif
252253 }
253254#else
255+ /* Normalise using a binary search, making the largest possible
256+ * adjustment first and getting progressively smaller. This gets
257+ * to the answer in the fastest time, with the minimum number of
258+ * operations to introduce rounding errors.
259+ */
260+ // First make small numbers bigger.
254261 flt_width_t power10 = MAX_POWER ;
255262 decpt = 1 ;
256-
257- if (number == DBL_MAX )
263+ i = 0 ;
264+ while (number < 1.0 )
258265 {
259- number = 1.7976931348623157 ;
260- decpt = 309 ;
266+ while (number < smalltable [i + 1 ])
267+ {
268+ number /= smalltable [i ];
269+ decpt -= power10 ;
270+ }
271+ power10 >>= 1 ;
272+ i ++ ;
261273 }
262- else if (number == DBL_MIN )
263- {
264- number = -1.7976931348623157 ;
265- decpt = -309 ;
266- }
267- else
274+ // Then make big numbers smaller.
275+ power10 = MAX_POWER ;
276+ i = 0 ;
277+ while (number >= 10.0 )
268278 {
269- /* Normalise using a binary search, making the largest possible
270- * adjustment first and getting progressively smaller. This gets
271- * to the answer in the fastest time, with the minimum number of
272- * operations to introduce rounding errors.
273- */
274- // First make small numbers bigger.
275-
276- i = 0 ;
277- while (number < 1.0 )
279+ while (number >= largetable [i ])
278280 {
279- while (number < smalltable [i + 1 ])
281+ number /= largetable [i ];
282+ decpt += power10 ;
283+ #ifdef NO_ISNAN_ISINF
284+ // Avoid this loop hanging on infinity.
285+ if (decpt > DP_LIMIT )
280286 {
281- number /= smalltable [i ];
282- decpt -= power10 ;
287+ buf [0 ] = 'I' ;
288+ buf [1 ] = 'n' ;
289+ buf [2 ] = 'f' ;
290+ buf [3 ] = '\0' ;
291+ return buf ;
283292 }
284- power10 >>= 1 ;
285- i ++ ;
286- }
287- // Then make big numbers smaller.
288- power10 = MAX_POWER ;
289- i = 0 ;
290- while (number >= 10.0 )
291- {
292- while (number >= largetable [i ])
293- {
294- number /= largetable [i ];
295- decpt += power10 ;
296- #ifdef NO_ISNAN_ISINF
297- // Avoid this loop hanging on infinity.
298- if (decpt > DP_LIMIT )
299- {
300- buf [0 ] = 'I' ;
301- buf [1 ] = 'n' ;
302- buf [2 ] = 'f' ;
303- buf [3 ] = '\0' ;
304- return buf ;
305- }
306293#endif
307- }
308- power10 >>= 1 ;
309- i ++ ;
310294 }
295+ power10 >>= 1 ;
296+ i ++ ;
311297 }
312298#endif
313299 }
@@ -1128,15 +1114,29 @@ The context is normally required, but is not used at present. It could be
11281114extended to include streams or to avoid output mixing in multi-threaded use.
11291115If using BASIC_PRINTF, context is not supported.
11301116--------------------------------------------------------------------------- */
1131- #ifdef BASIC_PRINTF_ONLY
1132- static void putout (char c )
1133- {
1134- #else
1135- static void putout (char c , void * context )
1117+ // [NF_CHANGE]
1118+ // #ifdef BASIC_PRINTF_ONLY
1119+ // static void putout(char c)
1120+ // {
1121+ // #else
1122+ // static void putout(char c, void *context)
1123+ // {
1124+ // (void) context; // Suppress compiler warning about unused argument.
1125+ // #endif
1126+ // PUTCHAR_FUNC(c);
1127+ // }
1128+ // [END_NF_CHANGE]
1129+ /* ---------------------------------------------------------------------------
1130+ Function: putbuf()
1131+ This is the output function used for sprintf.
1132+ Here the context is a pointer to a pointer to the buffer.
1133+ Double indirection allows the function to increment the buffer pointer.
1134+ --------------------------------------------------------------------------- */
1135+ static void putbuf (char c , void * context )
11361136{
1137- ( void ) context ; // Suppress compiler warning about unused argument.
1138- #endif
1139- PUTCHAR_FUNC ( c ) ;
1137+ char * buf = * (( char * * ) context );
1138+ * buf ++ = c ;
1139+ * (( char * * ) context ) = buf ;
11401140}
11411141
11421142/* ---------------------------------------------------------------------------
@@ -1145,7 +1145,7 @@ Replacement for library printf - writes to output (normally serial)
11451145It uses the output function putout() to update the serial output.
11461146If PRINTF_T is defined then the number of characters generated is returned.
11471147--------------------------------------------------------------------------- */
1148- printf_t printf_ (const char * fmt , ...)
1148+ printf_t _prntf (const char * fmt , ...)
11491149{
11501150 va_list ap ;
11511151#ifdef PRINTF_T
@@ -1157,7 +1157,7 @@ printf_t printf_(const char *fmt, ...)
11571157 #ifdef BASIC_PRINTF_ONLY
11581158 Count = doprnt (putout , fmt , ap );
11591159 #else
1160- Count = doprnt ((void * )0 , putout , BUFMAX , fmt , ap );
1160+ Count = doprnt ((void * )0 , putbuf , BUFMAX , fmt , ap );
11611161 #endif
11621162#else
11631163 #ifdef BASIC_PRINTF_ONLY
@@ -1174,18 +1174,6 @@ printf_t printf_(const char *fmt, ...)
11741174}
11751175
11761176#ifndef BASIC_PRINTF_ONLY
1177- /* ---------------------------------------------------------------------------
1178- Function: putbuf()
1179- This is the output function used for sprintf.
1180- Here the context is a pointer to a pointer to the buffer.
1181- Double indirection allows the function to increment the buffer pointer.
1182- --------------------------------------------------------------------------- */
1183- static void putbuf (char c , void * context )
1184- {
1185- char * buf = * ((char * * ) context );
1186- * buf ++ = c ;
1187- * ((char * * ) context ) = buf ;
1188- }
11891177
11901178/* ---------------------------------------------------------------------------
11911179Function: sprintf()
@@ -1194,7 +1182,7 @@ Normally it uses the output function putout() to update the buffer.
11941182sprintf is not supported when using BASIC_PRINTF
11951183If PRINTF_T is defined then the number of characters generated is returned.
11961184--------------------------------------------------------------------------- */
1197- printf_t sprintf_ (char * buf , const char * fmt , ... )
1185+ printf_t sprintf_ (char * buffer , const char * fmt , ... )
11981186{
11991187 va_list ap ;
12001188#ifdef PRINTF_T
@@ -1203,13 +1191,21 @@ printf_t sprintf_(char *buf, const char *fmt, ... )
12031191
12041192 va_start (ap , fmt );
12051193#ifdef PRINTF_T
1206- Count = doprnt (& buf , putbuf , BUFMAX , fmt , ap );
1194+ #ifdef BASIC_PRINTF_ONLY
1195+ Count = doprnt (putout , fmt , ap );
1196+ #else
1197+ Count = doprnt (& buffer , putbuf , BUFMAX , fmt , ap );
1198+ #endif
12071199#else
1208- doprnt (& buf , putbuf , fmt , ap );
1200+ #ifdef BASIC_PRINTF_ONLY
1201+ doprnt (putout , fmt , ap );
1202+ #else
1203+ doprnt (& buffer , putout , fmt , ap );
1204+ #endif
12091205#endif
12101206 va_end (ap );
12111207 // Append null terminator.
1212- * buf = '\0' ;
1208+ * buffer = '\0' ;
12131209
12141210#ifdef PRINTF_T
12151211 return Count ;
@@ -1218,16 +1214,28 @@ printf_t sprintf_(char *buf, const char *fmt, ... )
12181214#endif
12191215
12201216// [NF_CHANGE]
1221- printf_t snprintf_ (char * buf , size_t n ,const char * fmt , ... )
1217+ printf_t snprintf_ (char * buffer , size_t n , const char * fmt , ... )
12221218{
12231219 va_list ap ;
12241220 int Count ;
12251221
12261222 va_start (ap , fmt );
1227- Count = doprnt (& buf , putbuf , n , fmt , ap );
1223+ #ifdef PRINTF_T
1224+ #ifdef BASIC_PRINTF_ONLY
1225+ Count = doprnt (putout , fmt , ap );
1226+ #else
1227+ Count = doprnt (& buffer , putbuf , n , fmt , ap );
1228+ #endif
1229+ #else
1230+ #ifdef BASIC_PRINTF_ONLY
1231+ doprnt (putout , fmt , ap );
1232+ #else
1233+ doprnt (& buffer , putout , fmt , ap );
1234+ #endif
1235+ #endif
12281236 va_end (ap );
12291237 // Append null terminator.
1230- * buf = '\0' ;
1238+ * buffer = '\0' ;
12311239
12321240 return Count ;
12331241}
0 commit comments