rrsprintf.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. #ifndef RR_SPRINTF_H_INCLUDE
  2. #define RR_SPRINTF_H_INCLUDE
  3. /*
  4. Single file sprintf replacement.
  5. Originally written by Jeff Roberts at RAD Game Tools - 2015/10/20.
  6. Hereby placed in public domain.
  7. This is a full sprintf replacement that supports everything that
  8. the C runtime sprintfs support, including float/double, 64-bit integers,
  9. hex floats, field parameters (%*.*d stuff), length reads backs, etc.
  10. Why would you need this if sprintf already exists? Well, first off,
  11. it's *much* faster (see below). It's also much smaller than the CRT
  12. versions code-space-wise. We've also added some simple improvements
  13. that are super handy (commas in thousands, callbacks at buffer full,
  14. for example). Finally, the format strings for MSVC and GCC differ
  15. for 64-bit integers (among other small things), so this lets you use
  16. the same format strings in cross platform code.
  17. It uses the standard single file trick of being both the header file
  18. and the source itself. If you just include it normally, you just get
  19. the header file function definitions. To get the code, you include
  20. it from a C or C++ file and define RR_SPRINTF_IMPLEMENTATION first.
  21. It only uses va_args macros from the C runtime to do it's work. It
  22. does cast doubles to S64s and shifts and divides U64s, which does
  23. drag in CRT code on most platforms.
  24. It compiles to roughly 8K with float support, and 4K without.
  25. As a comparison, when using MSVC static libs, calling sprintf drags
  26. in 16K.
  27. API:
  28. ====
  29. int rrsprintf( char * buf, char const * fmt, ... )
  30. int rrsnprintf( char * buf, int count, char const * fmt, ... )
  31. Convert an arg list into a buffer. rrsnprintf always returns
  32. a zero-terminated string (unlike regular snprintf).
  33. int rrvsprintf( char * buf, char const * fmt, va_list va )
  34. int rrvsnprintf( char * buf, int count, char const * fmt, va_list va )
  35. Convert a va_list arg list into a buffer. rrvsnprintf always returns
  36. a zero-terminated string (unlike regular snprintf).
  37. int rrvsprintfcb( RRSPRINTFCB * callback, void * user, char * buf, char const * fmt, va_list va )
  38. typedef char * RRSPRINTFCB( char const * buf, void * user, int len );
  39. Convert into a buffer, calling back every RR_SPRINTF_MIN chars.
  40. Your callback can then copy the chars out, print them or whatever.
  41. This function is actually the workhorse for everything else.
  42. The buffer you pass in must hold at least RR_SPRINTF_MIN characters.
  43. // you return the next buffer to use or 0 to stop converting
  44. void rrsetseparators( char comma, char period )
  45. Set the comma and period characters to use.
  46. FLOATS/DOUBLES:
  47. ===============
  48. This code uses a internal float->ascii conversion method that uses
  49. doubles with error correction (double-doubles, for ~105 bits of
  50. precision). This conversion is round-trip perfect - that is, an atof
  51. of the values output here will give you the bit-exact double back.
  52. One difference is that our insignificant digits will be different than
  53. with MSVC or GCC (but they don't match each other either). We also
  54. don't attempt to find the minimum length matching float (pre-MSVC15
  55. doesn't either).
  56. If you don't need float or doubles at all, define RR_SPRINTF_NOFLOAT
  57. and you'll save 4K of code space.
  58. 64-BIT INTS:
  59. ============
  60. This library also supports 64-bit integers and you can use MSVC style or
  61. GCC style indicators (%I64d or %lld). It supports the C99 specifiers
  62. for size_t and ptr_diff_t (%jd %zd) as well.
  63. EXTRAS:
  64. =======
  65. Like some GCCs, for integers and floats, you can use a ' (single quote)
  66. specifier and commas will be inserted on the thousands: "%'d" on 12345
  67. would print 12,345.
  68. For integers and floats, you can use a "$" specifier and the number
  69. will be converted to float and then divided to get kilo, mega, giga or
  70. tera and then printed, so "%$d" 1024 is "1.0 k", "%$.2d" 2536000 is
  71. "2.42 m", etc.
  72. In addition to octal and hexadecimal conversions, you can print
  73. integers in binary: "%b" for 256 would print 100.
  74. PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC):
  75. ===================================================================
  76. "%d" across all 32-bit ints (4.8x/4.0x faster than 32-/64-bit MSVC)
  77. "%24d" across all 32-bit ints (4.5x/4.2x faster)
  78. "%x" across all 32-bit ints (4.5x/3.8x faster)
  79. "%08x" across all 32-bit ints (4.3x/3.8x faster)
  80. "%f" across e-10 to e+10 floats (7.3x/6.0x faster)
  81. "%e" across e-10 to e+10 floats (8.1x/6.0x faster)
  82. "%g" across e-10 to e+10 floats (10.0x/7.1x faster)
  83. "%f" for values near e-300 (7.9x/6.5x faster)
  84. "%f" for values near e+300 (10.0x/9.1x faster)
  85. "%e" for values near e-300 (10.1x/7.0x faster)
  86. "%e" for values near e+300 (9.2x/6.0x faster)
  87. "%.320f" for values near e-300 (12.6x/11.2x faster)
  88. "%a" for random values (8.6x/4.3x faster)
  89. "%I64d" for 64-bits with 32-bit values (4.8x/3.4x faster)
  90. "%I64d" for 64-bits > 32-bit values (4.9x/5.5x faster)
  91. "%s%s%s" for 64 char strings (7.1x/7.3x faster)
  92. "...512 char string..." ( 35.0x/32.5x faster!)
  93. */
  94. #ifdef RR_SPRINTF_STATIC
  95. #define RRPUBLIC_DEC static
  96. #define RRPUBLIC_DEF static
  97. #else
  98. #ifdef __cplusplus
  99. #define RRPUBLIC_DEC extern "C"
  100. #define RRPUBLIC_DEF extern "C"
  101. #else
  102. #define RRPUBLIC_DEC extern
  103. #define RRPUBLIC_DEF
  104. #endif
  105. #endif
  106. #include <stdarg.h> // for va_list()
  107. #ifndef RR_SPRINTF_MIN
  108. #define RR_SPRINTF_MIN 512 // how many characters per callback
  109. #endif
  110. typedef char * RRSPRINTFCB( char * buf, void * user, int len );
  111. #ifndef RR_SPRINTF_DECORATE
  112. #define RR_SPRINTF_DECORATE(name) rr##name // define this before including if you want to change the names
  113. #endif
  114. #ifndef RR_SPRINTF_IMPLEMENTATION
  115. RRPUBLIC_DEF int RR_SPRINTF_DECORATE( vsprintf )( char * buf, char const * fmt, va_list va );
  116. RRPUBLIC_DEF int RR_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va );
  117. RRPUBLIC_DEF int RR_SPRINTF_DECORATE( sprintf ) ( char * buf, char const * fmt, ... );
  118. RRPUBLIC_DEF int RR_SPRINTF_DECORATE( snprintf )( char * buf, int count, char const * fmt, ... );
  119. RRPUBLIC_DEF int RR_SPRINTF_DECORATE( vsprintfcb )( RRSPRINTFCB * callback, void * user, char * buf, char const * fmt, va_list va );
  120. RRPUBLIC_DEF void RR_SPRINTF_DECORATE( setseparators )( char comma, char period );
  121. #else
  122. #include <stdlib.h> // for va_arg()
  123. #define rU32 unsigned int
  124. #define rS32 signed int
  125. #ifdef _MSC_VER
  126. #define rU64 unsigned __int64
  127. #define rS64 signed __int64
  128. #else
  129. #define rU64 unsigned long long
  130. #define rS64 signed long long
  131. #endif
  132. #define rU16 unsigned short
  133. #ifndef rUINTa
  134. #if defined(__ppc64__) || defined(__aarch64__) || defined(_M_X64) || defined(__x86_64__) || defined(__x86_64)
  135. #define rUINTa rU64
  136. #else
  137. #define rUINTa rU32
  138. #endif
  139. #endif
  140. #ifndef RR_SPRINTF_MSVC_MODE // used for MSVC2013 and earlier (MSVC2015 matches GCC)
  141. #if defined(_MSC_VER) && (_MSC_VER<1900)
  142. #define RR_SPRINTF_MSVC_MODE
  143. #endif
  144. #endif
  145. #ifdef RR_SPRINTF_NOUNALIGNED // define this before inclusion to force rrsprint to always use aligned accesses
  146. #define RR_UNALIGNED(code)
  147. #else
  148. #define RR_UNALIGNED(code) code
  149. #endif
  150. #ifndef RR_SPRINTF_NOFLOAT
  151. // internal float utility functions
  152. static rS32 rrreal_to_str( char const * * start, rU32 * len, char *out, rS32 * decimal_pos, double value, rU32 frac_digits );
  153. static rS32 rrreal_to_parts( rS64 * bits, rS32 * expo, double value );
  154. #define RRSPECIAL 0x7000
  155. #endif
  156. static char RRperiod='.';
  157. static char RRcomma=',';
  158. static char rrdiglookup[201]="00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899";
  159. RRPUBLIC_DEF void RR_SPRINTF_DECORATE( setseparators )( char pcomma, char pperiod )
  160. {
  161. RRperiod=pperiod;
  162. RRcomma=pcomma;
  163. }
  164. RRPUBLIC_DEF int RR_SPRINTF_DECORATE( vsprintfcb )( RRSPRINTFCB * callback, void * user, char * buf, char const * fmt, va_list va )
  165. {
  166. static char hex[]="0123456789abcdefxp";
  167. static char hexu[]="0123456789ABCDEFXP";
  168. char * bf;
  169. char const * f;
  170. int tlen = 0;
  171. bf = buf;
  172. f = fmt;
  173. for(;;)
  174. {
  175. rS32 fw,pr,tz; rU32 fl;
  176. #define LJ 1
  177. #define LP 2
  178. #define LS 4
  179. #define LX 8
  180. #define LZ 16
  181. #define BI 32
  182. #define CS 64
  183. #define NG 128
  184. #define KI 256
  185. #define HW 512
  186. // macros for the callback buffer stuff
  187. #define chk_cb_bufL(bytes) { int len = (int)(bf-buf); if ((len+(bytes))>=RR_SPRINTF_MIN) { tlen+=len; if (0==(bf=buf=callback(buf,user,len))) goto done; } }
  188. #define chk_cb_buf(bytes) { if ( callback ) { chk_cb_bufL(bytes); } }
  189. #define flush_cb() { chk_cb_bufL(RR_SPRINTF_MIN-1); } //flush if there is even one byte in the buffer
  190. #define cb_buf_clamp(cl,v) cl = v; if ( callback ) { int lg = RR_SPRINTF_MIN-(int)(bf-buf); if (cl>lg) cl=lg; }
  191. // fast copy everything up to the next % (or end of string)
  192. for(;;)
  193. {
  194. while (((rUINTa)f)&3)
  195. {
  196. schk1: if (f[0]=='%') goto scandd;
  197. schk2: if (f[0]==0) goto endfmt;
  198. chk_cb_buf(1); *bf++=f[0]; ++f;
  199. }
  200. for(;;)
  201. {
  202. rU32 v,c;
  203. v=*(rU32*)f; c=(~v)&0x80808080;
  204. if ((v-0x26262626)&c) goto schk1;
  205. if ((v-0x01010101)&c) goto schk2;
  206. if (callback) if ((RR_SPRINTF_MIN-(int)(bf-buf))<4) goto schk1;
  207. *(rU32*)bf=v; bf+=4; f+=4;
  208. }
  209. } scandd:
  210. ++f;
  211. // ok, we have a percent, read the modifiers first
  212. fw = 0; pr = -1; fl = 0; tz = 0;
  213. // flags
  214. for(;;)
  215. {
  216. switch(f[0])
  217. {
  218. // if we have left just
  219. case '-': fl|=LJ; ++f; continue;
  220. // if we have leading plus
  221. case '+': fl|=LP; ++f; continue;
  222. // if we have leading space
  223. case ' ': fl|=LS; ++f; continue;
  224. // if we have leading 0x
  225. case '#': fl|=LX; ++f; continue;
  226. // if we have thousand commas
  227. case '\'': fl|=CS; ++f; continue;
  228. // if we have kilo marker
  229. case '$': fl|=KI; ++f; continue;
  230. // if we have leading zero
  231. case '0': fl|=LZ; ++f; goto flags_done;
  232. default: goto flags_done;
  233. }
  234. }
  235. flags_done:
  236. // get the field width
  237. if ( f[0] == '*' ) {fw = va_arg(va,rU32); ++f;} else { while (( f[0] >= '0' ) && ( f[0] <= '9' )) { fw = fw * 10 + f[0] - '0'; f++; } }
  238. // get the precision
  239. if ( f[0]=='.' ) { ++f; if ( f[0] == '*' ) {pr = va_arg(va,rU32); ++f;} else { pr = 0; while (( f[0] >= '0' ) && ( f[0] <= '9' )) { pr = pr * 10 + f[0] - '0'; f++; } } }
  240. // handle integer size overrides
  241. switch(f[0])
  242. {
  243. // are we halfwidth?
  244. case 'h': fl|=HW; ++f; break;
  245. // are we 64-bit (unix style)
  246. case 'l': ++f; if ( f[0]=='l') { fl|=BI; ++f; } break;
  247. // are we 64-bit on intmax? (c99)
  248. case 'j': fl|=BI; ++f; break;
  249. // are we 64-bit on size_t or ptrdiff_t? (c99)
  250. case 'z': case 't': fl|=((sizeof(char*)==8)?BI:0); ++f; break;
  251. // are we 64-bit (msft style)
  252. case 'I': if ( ( f[1]=='6') && ( f[2]=='4') ) { fl|=BI; f+=3; } else if ( ( f[1]=='3') && ( f[2]=='2') ) { f+=3; } else { fl|=((sizeof(void*)==8)?BI:0); ++f; } break;
  253. default: break;
  254. }
  255. // handle each replacement
  256. switch( f[0] )
  257. {
  258. #define NUMSZ 512 // big enough for e308 (with commas) or e-307
  259. char num[NUMSZ];
  260. char lead[8];
  261. char tail[8];
  262. char *s;
  263. char const *h;
  264. rU32 l,n,cs;
  265. rU64 n64;
  266. #ifndef RR_SPRINTF_NOFLOAT
  267. double fv;
  268. #endif
  269. rS32 dp; char const * sn;
  270. case 's':
  271. // get the string
  272. s = va_arg(va,char*); if (s==0) s = (char*)"null";
  273. // get the length
  274. sn = s;
  275. for(;;)
  276. {
  277. if ((((rUINTa)sn)&3)==0) break;
  278. lchk:
  279. if (sn[0]==0) goto ld;
  280. ++sn;
  281. }
  282. n = 0xffffffff;
  283. if (pr>=0) { n=(rU32)(sn-s); if (n>=(rU32)pr) goto ld; n=((rU32)(pr-n))>>2; }
  284. while(n)
  285. {
  286. rU32 v=*(rU32*)sn;
  287. if ((v-0x01010101)&(~v)&0x80808080UL) goto lchk;
  288. sn+=4;
  289. --n;
  290. }
  291. goto lchk;
  292. ld:
  293. l = (rU32) ( sn - s );
  294. // clamp to precision
  295. if ( l > (rU32)pr ) l = pr;
  296. lead[0]=0; tail[0]=0; pr = 0; dp = 0; cs = 0;
  297. // copy the string in
  298. goto scopy;
  299. case 'c': // char
  300. // get the character
  301. s = num + NUMSZ -1; *s = (char)va_arg(va,int);
  302. l = 1;
  303. lead[0]=0; tail[0]=0; pr = 0; dp = 0; cs = 0;
  304. goto scopy;
  305. case 'n': // weird write-bytes specifier
  306. { int * d = va_arg(va,int*);
  307. *d = tlen + (int)( bf - buf ); }
  308. break;
  309. #ifdef RR_SPRINTF_NOFLOAT
  310. case 'A': // float
  311. case 'a': // hex float
  312. case 'G': // float
  313. case 'g': // float
  314. case 'E': // float
  315. case 'e': // float
  316. case 'f': // float
  317. va_arg(va,double); // eat it
  318. s = (char*)"No float";
  319. l = 8;
  320. lead[0]=0; tail[0]=0; pr = 0; dp = 0; cs = 0;
  321. goto scopy;
  322. #else
  323. case 'A': // float
  324. h=hexu;
  325. goto hexfloat;
  326. case 'a': // hex float
  327. h=hex;
  328. hexfloat:
  329. fv = va_arg(va,double);
  330. if (pr==-1) pr=6; // default is 6
  331. // read the double into a string
  332. if ( rrreal_to_parts( (rS64*)&n64, &dp, fv ) )
  333. fl |= NG;
  334. s = num+64;
  335. // sign
  336. lead[0]=0; if (fl&NG) { lead[0]=1; lead[1]='-'; } else if (fl&LS) { lead[0]=1; lead[1]=' '; } else if (fl&LP) { lead[0]=1; lead[1]='+'; };
  337. if (dp==-1023) dp=(n64)?-1022:0; else n64|=(((rU64)1)<<52);
  338. n64<<=(64-56);
  339. if (pr<15) n64+=((((rU64)8)<<56)>>(pr*4));
  340. // add leading chars
  341. #ifdef RR_SPRINTF_MSVC_MODE
  342. *s++='0';*s++='x';
  343. #else
  344. lead[1+lead[0]]='0'; lead[2+lead[0]]='x'; lead[0]+=2;
  345. #endif
  346. *s++=h[(n64>>60)&15]; n64<<=4;
  347. if ( pr ) *s++=RRperiod;
  348. sn = s;
  349. // print the bits
  350. n = pr; if (n>13) n = 13; if (pr>(rS32)n) tz=pr-n; pr = 0;
  351. while(n--) { *s++=h[(n64>>60)&15]; n64<<=4; }
  352. // print the expo
  353. tail[1]=h[17];
  354. if (dp<0) { tail[2]='-'; dp=-dp;} else tail[2]='+';
  355. n = (dp>=1000)?6:((dp>=100)?5:((dp>=10)?4:3));
  356. tail[0]=(char)n;
  357. for(;;) { tail[n]='0'+dp%10; if (n<=3) break; --n; dp/=10; }
  358. dp = (int)(s-sn);
  359. l = (int)(s-(num+64));
  360. s = num+64;
  361. cs = 1 + (3<<24);
  362. goto scopy;
  363. case 'G': // float
  364. h=hexu;
  365. goto dosmallfloat;
  366. case 'g': // float
  367. h=hex;
  368. dosmallfloat:
  369. fv = va_arg(va,double);
  370. if (pr==-1) pr=6; else if (pr==0) pr = 1; // default is 6
  371. // read the double into a string
  372. if ( rrreal_to_str( &sn, &l, num, &dp, fv, (pr-1)|0x80000000 ) )
  373. fl |= NG;
  374. // clamp the precision and delete extra zeros after clamp
  375. n = pr;
  376. if ( l > (rU32)pr ) l = pr; while ((l>1)&&(pr)&&(sn[l-1]=='0')) { --pr; --l; }
  377. // should we use %e
  378. if ((dp<=-4)||(dp>(rS32)n))
  379. {
  380. if ( pr > (rS32)l ) pr = l-1; else if ( pr ) --pr; // when using %e, there is one digit before the decimal
  381. goto doexpfromg;
  382. }
  383. // this is the insane action to get the pr to match %g sematics for %f
  384. if(dp>0) { pr=(dp<(rS32)l)?l-dp:0; } else { pr = -dp+((pr>(rS32)l)?l:pr); }
  385. goto dofloatfromg;
  386. case 'E': // float
  387. h=hexu;
  388. goto doexp;
  389. case 'e': // float
  390. h=hex;
  391. doexp:
  392. fv = va_arg(va,double);
  393. if (pr==-1) pr=6; // default is 6
  394. // read the double into a string
  395. if ( rrreal_to_str( &sn, &l, num, &dp, fv, pr|0x80000000 ) )
  396. fl |= NG;
  397. doexpfromg:
  398. tail[0]=0;
  399. lead[0]=0; if (fl&NG) { lead[0]=1; lead[1]='-'; } else if (fl&LS) { lead[0]=1; lead[1]=' '; } else if (fl&LP) { lead[0]=1; lead[1]='+'; };
  400. if ( dp == RRSPECIAL ) { s=(char*)sn; cs=0; pr=0; goto scopy; }
  401. s=num+64;
  402. // handle leading chars
  403. *s++=sn[0];
  404. if (pr) *s++=RRperiod;
  405. // handle after decimal
  406. if ((l-1)>(rU32)pr) l=pr+1;
  407. for(n=1;n<l;n++) *s++=sn[n];
  408. // trailing zeros
  409. tz = pr-(l-1); pr=0;
  410. // dump expo
  411. tail[1]=h[0xe];
  412. dp -= 1;
  413. if (dp<0) { tail[2]='-'; dp=-dp;} else tail[2]='+';
  414. #ifdef RR_SPRINTF_MSVC_MODE
  415. n = 5;
  416. #else
  417. n = (dp>=100)?5:4;
  418. #endif
  419. tail[0]=(char)n;
  420. for(;;) { tail[n]='0'+dp%10; if (n<=3) break; --n; dp/=10; }
  421. cs = 1 + (3<<24); // how many tens
  422. goto flt_lead;
  423. case 'f': // float
  424. fv = va_arg(va,double);
  425. doafloat:
  426. // do kilos
  427. if (fl&KI) {while(fl<0x4000000) { if ((fv<1024.0) && (fv>-1024.0)) break; fv/=1024.0; fl+=0x1000000; }}
  428. if (pr==-1) pr=6; // default is 6
  429. // read the double into a string
  430. if ( rrreal_to_str( &sn, &l, num, &dp, fv, pr ) )
  431. fl |= NG;
  432. dofloatfromg:
  433. tail[0]=0;
  434. // sign
  435. lead[0]=0; if (fl&NG) { lead[0]=1; lead[1]='-'; } else if (fl&LS) { lead[0]=1; lead[1]=' '; } else if (fl&LP) { lead[0]=1; lead[1]='+'; };
  436. if ( dp == RRSPECIAL ) { s=(char*)sn; cs=0; pr=0; goto scopy; }
  437. s=num+64;
  438. // handle the three decimal varieties
  439. if (dp<=0)
  440. {
  441. rS32 i;
  442. // handle 0.000*000xxxx
  443. *s++='0'; if (pr) *s++=RRperiod;
  444. n=-dp; if((rS32)n>pr) n=pr; i=n; while(i) { if ((((rUINTa)s)&3)==0) break; *s++='0'; --i; } while(i>=4) { *(rU32*)s=0x30303030; s+=4; i-=4; } while(i) { *s++='0'; --i; }
  445. if ((rS32)(l+n)>pr) l=pr-n; i=l; while(i) { *s++=*sn++; --i; }
  446. tz = pr-(n+l);
  447. cs = 1 + (3<<24); // how many tens did we write (for commas below)
  448. }
  449. else
  450. {
  451. cs = (fl&CS)?((600-(rU32)dp)%3):0;
  452. if ((rU32)dp>=l)
  453. {
  454. // handle xxxx000*000.0
  455. n=0; for(;;) { if ((fl&CS) && (++cs==4)) { cs = 0; *s++=RRcomma; } else { *s++=sn[n]; ++n; if (n>=l) break; } }
  456. if (n<(rU32)dp)
  457. {
  458. n = dp - n;
  459. if ((fl&CS)==0) { while(n) { if ((((rUINTa)s)&3)==0) break; *s++='0'; --n; } while(n>=4) { *(rU32*)s=0x30303030; s+=4; n-=4; } }
  460. while(n) { if ((fl&CS) && (++cs==4)) { cs = 0; *s++=RRcomma; } else { *s++='0'; --n; } }
  461. }
  462. cs = (int)(s-(num+64)) + (3<<24); // cs is how many tens
  463. if (pr) { *s++=RRperiod; tz=pr;}
  464. }
  465. else
  466. {
  467. // handle xxxxx.xxxx000*000
  468. n=0; for(;;) { if ((fl&CS) && (++cs==4)) { cs = 0; *s++=RRcomma; } else { *s++=sn[n]; ++n; if (n>=(rU32)dp) break; } }
  469. cs = (int)(s-(num+64)) + (3<<24); // cs is how many tens
  470. if (pr) *s++=RRperiod;
  471. if ((l-dp)>(rU32)pr) l=pr+dp;
  472. while(n<l) { *s++=sn[n]; ++n; }
  473. tz = pr-(l-dp);
  474. }
  475. }
  476. pr = 0;
  477. // handle k,m,g,t
  478. if (fl&KI) { tail[0]=1; tail[1]=' '; { if (fl>>24) { tail[2]="_kmgt"[fl>>24]; tail[0]=2; } } };
  479. flt_lead:
  480. // get the length that we copied
  481. l = (rU32) ( s-(num+64) );
  482. s=num+64;
  483. goto scopy;
  484. #endif
  485. case 'B': // upper binary
  486. h = hexu;
  487. goto binary;
  488. case 'b': // lower binary
  489. h = hex;
  490. binary:
  491. lead[0]=0;
  492. if (fl&LX) { lead[0]=2;lead[1]='0';lead[2]=h[0xb]; }
  493. l=(8<<4)|(1<<8);
  494. goto radixnum;
  495. case 'o': // octal
  496. h = hexu;
  497. lead[0]=0;
  498. if (fl&LX) { lead[0]=1;lead[1]='0'; }
  499. l=(3<<4)|(3<<8);
  500. goto radixnum;
  501. case 'p': // pointer
  502. fl |= (sizeof(void*)==8)?BI:0;
  503. pr = sizeof(void*)*2;
  504. fl &= ~LZ; // 'p' only prints the pointer with zeros
  505. // drop through to X
  506. case 'X': // upper binary
  507. h = hexu;
  508. goto dohexb;
  509. case 'x': // lower binary
  510. h = hex; dohexb:
  511. l=(4<<4)|(4<<8);
  512. lead[0]=0;
  513. if (fl&LX) { lead[0]=2;lead[1]='0';lead[2]=h[16]; }
  514. radixnum:
  515. // get the number
  516. if ( fl&BI )
  517. n64 = va_arg(va,rU64);
  518. else
  519. n64 = va_arg(va,rU32);
  520. s = num + NUMSZ; dp = 0;
  521. // clear tail, and clear leading if value is zero
  522. tail[0]=0; if (n64==0) { lead[0]=0; if (pr==0) { l=0; cs = ( ((l>>4)&15)) << 24; goto scopy; } }
  523. // convert to string
  524. for(;;) { *--s = h[n64&((1<<(l>>8))-1)]; n64>>=(l>>8); if ( ! ( (n64) || ((rS32) ( (num+NUMSZ) - s ) < pr ) ) ) break; if ( fl&CS) { ++l; if ((l&15)==((l>>4)&15)) { l&=~15; *--s=RRcomma; } } };
  525. // get the tens and the comma pos
  526. cs = (rU32) ( (num+NUMSZ) - s ) + ( ( ((l>>4)&15)) << 24 );
  527. // get the length that we copied
  528. l = (rU32) ( (num+NUMSZ) - s );
  529. // copy it
  530. goto scopy;
  531. case 'u': // unsigned
  532. case 'i':
  533. case 'd': // integer
  534. // get the integer and abs it
  535. if ( fl&BI )
  536. {
  537. rS64 i64 = va_arg(va,rS64); n64 = (rU64)i64; if ((f[0]!='u') && (i64<0)) { n64=(rU64)-i64; fl|=NG; }
  538. }
  539. else
  540. {
  541. rS32 i = va_arg(va,rS32); n64 = (rU32)i; if ((f[0]!='u') && (i<0)) { n64=(rU32)-i; fl|=NG; }
  542. }
  543. #ifndef RR_SPRINTF_NOFLOAT
  544. if (fl&KI) { if (n64<1024) pr=0; else if (pr==-1) pr=1; fv=(double)(rS64)n64; goto doafloat; }
  545. #endif
  546. // convert to string
  547. s = num+NUMSZ; l=0;
  548. for(;;)
  549. {
  550. // do in 32-bit chunks (avoid lots of 64-bit divides even with constant denominators)
  551. char * o=s-8;
  552. if (n64>=100000000) { n = (rU32)( n64 % 100000000); n64 /= 100000000; } else {n = (rU32)n64; n64 = 0; }
  553. if((fl&CS)==0) { while(n) { s-=2; *(rU16*)s=*(rU16*)&rrdiglookup[(n%100)*2]; n/=100; } }
  554. while (n) { if ( ( fl&CS) && (l++==3) ) { l=0; *--s=RRcomma; --o; } else { *--s=(char)(n%10)+'0'; n/=10; } }
  555. if (n64==0) { if ((s[0]=='0') && (s!=(num+NUMSZ))) ++s; break; }
  556. while (s!=o) if ( ( fl&CS) && (l++==3) ) { l=0; *--s=RRcomma; --o; } else { *--s='0'; }
  557. }
  558. tail[0]=0;
  559. // sign
  560. lead[0]=0; if (fl&NG) { lead[0]=1; lead[1]='-'; } else if (fl&LS) { lead[0]=1; lead[1]=' '; } else if (fl&LP) { lead[0]=1; lead[1]='+'; };
  561. // get the length that we copied
  562. l = (rU32) ( (num+NUMSZ) - s ); if ( l == 0 ) { *--s='0'; l = 1; }
  563. cs = l + (3<<24);
  564. if (pr<0) pr = 0;
  565. scopy:
  566. // get fw=leading/trailing space, pr=leading zeros
  567. if (pr<(rS32)l) pr = l;
  568. n = pr + lead[0] + tail[0] + tz;
  569. if (fw<(rS32)n) fw = n;
  570. fw -= n;
  571. pr -= l;
  572. // handle right justify and leading zeros
  573. if ( (fl&LJ)==0 )
  574. {
  575. if (fl&LZ) // if leading zeros, everything is in pr
  576. {
  577. pr = (fw>pr)?fw:pr;
  578. fw = 0;
  579. }
  580. else
  581. {
  582. fl &= ~CS; // if no leading zeros, then no commas
  583. }
  584. }
  585. // copy the spaces and/or zeros
  586. if (fw+pr)
  587. {
  588. rS32 i; rU32 c;
  589. // copy leading spaces (or when doing %8.4d stuff)
  590. if ( (fl&LJ)==0 ) while(fw>0) { cb_buf_clamp(i,fw); fw -= i; while(i) { if ((((rUINTa)bf)&3)==0) break; *bf++=' '; --i; } while(i>=4) { *(rU32*)bf=0x20202020; bf+=4; i-=4; } while (i) {*bf++=' '; --i;} chk_cb_buf(1); }
  591. // copy leader
  592. sn=lead+1; while(lead[0]) { cb_buf_clamp(i,lead[0]); lead[0] -= (char)i; while (i) {*bf++=*sn++; --i;} chk_cb_buf(1); }
  593. // copy leading zeros
  594. c = cs >> 24; cs &= 0xffffff;
  595. cs = (fl&CS)?((rU32)(c-((pr+cs)%(c+1)))):0;
  596. while(pr>0) { cb_buf_clamp(i,pr); pr -= i; if((fl&CS)==0) { while(i) { if ((((rUINTa)bf)&3)==0) break; *bf++='0'; --i; } while(i>=4) { *(rU32*)bf=0x30303030; bf+=4; i-=4; } } while (i) { if((fl&CS) && (cs++==c)) { cs = 0; *bf++=RRcomma; } else *bf++='0'; --i; } chk_cb_buf(1); }
  597. }
  598. // copy leader if there is still one
  599. sn=lead+1; while(lead[0]) { rS32 i; cb_buf_clamp(i,lead[0]); lead[0] -= (char)i; while (i) {*bf++=*sn++; --i;} chk_cb_buf(1); }
  600. // copy the string
  601. n = l; while (n) { rS32 i; cb_buf_clamp(i,n); n-=i; RR_UNALIGNED( while(i>=4) { *(rU32*)bf=*(rU32*)s; bf+=4; s+=4; i-=4; } ) while (i) {*bf++=*s++; --i;} chk_cb_buf(1); }
  602. // copy trailing zeros
  603. while(tz) { rS32 i; cb_buf_clamp(i,tz); tz -= i; while(i) { if ((((rUINTa)bf)&3)==0) break; *bf++='0'; --i; } while(i>=4) { *(rU32*)bf=0x30303030; bf+=4; i-=4; } while (i) {*bf++='0'; --i;} chk_cb_buf(1); }
  604. // copy tail if there is one
  605. sn=tail+1; while(tail[0]) { rS32 i; cb_buf_clamp(i,tail[0]); tail[0] -= (char)i; while (i) {*bf++=*sn++; --i;} chk_cb_buf(1); }
  606. // handle the left justify
  607. if (fl&LJ) if (fw>0) { while (fw) { rS32 i; cb_buf_clamp(i,fw); fw-=i; while(i) { if ((((rUINTa)bf)&3)==0) break; *bf++=' '; --i; } while(i>=4) { *(rU32*)bf=0x20202020; bf+=4; i-=4; } while (i--) *bf++=' '; chk_cb_buf(1); } }
  608. break;
  609. default: // unknown, just copy code
  610. s = num + NUMSZ -1; *s = f[0];
  611. l = 1;
  612. fw=pr=fl=0;
  613. lead[0]=0; tail[0]=0; pr = 0; dp = 0; cs = 0;
  614. goto scopy;
  615. }
  616. ++f;
  617. }
  618. endfmt:
  619. if (!callback)
  620. *bf = 0;
  621. else
  622. flush_cb();
  623. done:
  624. return tlen + (int)(bf-buf);
  625. }
  626. // cleanup
  627. #undef LJ
  628. #undef LP
  629. #undef LS
  630. #undef LX
  631. #undef LZ
  632. #undef BI
  633. #undef CS
  634. #undef NG
  635. #undef KI
  636. #undef NUMSZ
  637. #undef chk_cb_bufL
  638. #undef chk_cb_buf
  639. #undef flush_cb
  640. #undef cb_buf_clamp
  641. // ============================================================================
  642. // wrapper functions
  643. RRPUBLIC_DEF int RR_SPRINTF_DECORATE( sprintf )( char * buf, char const * fmt, ... )
  644. {
  645. va_list va;
  646. va_start( va, fmt );
  647. return RR_SPRINTF_DECORATE( vsprintfcb )( 0, 0, buf, fmt, va );
  648. }
  649. typedef struct RRCCS
  650. {
  651. char * buf;
  652. int count;
  653. char tmp[ RR_SPRINTF_MIN ];
  654. } RRCCS;
  655. static char * rrclampcallback( char * buf, void * user, int len )
  656. {
  657. RRCCS * c = (RRCCS*)user;
  658. if ( len > c->count ) len = c->count;
  659. if (len)
  660. {
  661. if ( buf != c->buf )
  662. {
  663. char * s, * d, * se;
  664. d = c->buf; s = buf; se = buf+len;
  665. do{ *d++ = *s++; } while (s<se);
  666. }
  667. c->buf += len;
  668. c->count -= len;
  669. }
  670. if ( c->count <= 0 ) return 0;
  671. return ( c->count >= RR_SPRINTF_MIN ) ? c->buf : c->tmp; // go direct into buffer if you can
  672. }
  673. RRPUBLIC_DEF int RR_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va )
  674. {
  675. RRCCS c;
  676. int l;
  677. if ( count == 0 )
  678. return 0;
  679. c.buf = buf;
  680. c.count = count;
  681. RR_SPRINTF_DECORATE( vsprintfcb )( rrclampcallback, &c, rrclampcallback(0,&c,0), fmt, va );
  682. // zero-terminate
  683. l = (int)( c.buf - buf );
  684. if ( l >= count ) // should never be greater, only equal (or less) than count
  685. l = count - 1;
  686. buf[l] = 0;
  687. return l;
  688. }
  689. RRPUBLIC_DEF int RR_SPRINTF_DECORATE( snprintf )( char * buf, int count, char const * fmt, ... )
  690. {
  691. va_list va;
  692. va_start( va, fmt );
  693. return RR_SPRINTF_DECORATE( vsnprintf )( buf, count, fmt, va );
  694. }
  695. RRPUBLIC_DEF int RR_SPRINTF_DECORATE( vsprintf )( char * buf, char const * fmt, va_list va )
  696. {
  697. return RR_SPRINTF_DECORATE( vsprintfcb )( 0, 0, buf, fmt, va );
  698. }
  699. // =======================================================================
  700. // low level float utility functions
  701. #ifndef RR_SPRINTF_NOFLOAT
  702. // copies d to bits w/ strict aliasing (this compiles to nothing on /Ox)
  703. #define RRCOPYFP(dest,src) { int cn; for(cn=0;cn<8;cn++) ((char*)&dest)[cn]=((char*)&src)[cn]; }
  704. // get float info
  705. static rS32 rrreal_to_parts( rS64 * bits, rS32 * expo, double value )
  706. {
  707. double d;
  708. rS64 b = 0;
  709. // load value and round at the frac_digits
  710. d = value;
  711. RRCOPYFP( b, d );
  712. *bits = b & ((((rU64)1)<<52)-1);
  713. *expo = ((b >> 52) & 2047)-1023;
  714. return (rS32)(b >> 63);
  715. }
  716. static double const rrbot[23]={1e+000,1e+001,1e+002,1e+003,1e+004,1e+005,1e+006,1e+007,1e+008,1e+009,1e+010,1e+011,1e+012,1e+013,1e+014,1e+015,1e+016,1e+017,1e+018,1e+019,1e+020,1e+021,1e+022};
  717. static double const rrnegbot[22]={1e-001,1e-002,1e-003,1e-004,1e-005,1e-006,1e-007,1e-008,1e-009,1e-010,1e-011,1e-012,1e-013,1e-014,1e-015,1e-016,1e-017,1e-018,1e-019,1e-020,1e-021,1e-022};
  718. static double const rrnegboterr[22]={-5.551115123125783e-018,-2.0816681711721684e-019,-2.0816681711721686e-020,-4.7921736023859299e-021,-8.1803053914031305e-022,4.5251888174113741e-023,4.5251888174113739e-024,-2.0922560830128471e-025,-6.2281591457779853e-026,-3.6432197315497743e-027,6.0503030718060191e-028,2.0113352370744385e-029,-3.0373745563400371e-030,1.1806906454401013e-032,-7.7705399876661076e-032,2.0902213275965398e-033,-7.1542424054621921e-034,-7.1542424054621926e-035,2.4754073164739869e-036,5.4846728545790429e-037,9.2462547772103625e-038,-4.8596774326570872e-039};
  719. static double const rrtop[13]={1e+023,1e+046,1e+069,1e+092,1e+115,1e+138,1e+161,1e+184,1e+207,1e+230,1e+253,1e+276,1e+299};
  720. static double const rrnegtop[13]={1e-023,1e-046,1e-069,1e-092,1e-115,1e-138,1e-161,1e-184,1e-207,1e-230,1e-253,1e-276,1e-299};
  721. static double const rrtoperr[13]={8388608,6.8601809640529717e+028,-7.253143638152921e+052,-4.3377296974619174e+075,-1.5559416129466825e+098,-3.2841562489204913e+121,-3.7745893248228135e+144,-1.7356668416969134e+167,-3.8893577551088374e+190,-9.9566444326005119e+213,6.3641293062232429e+236,-5.2069140800249813e+259,-5.2504760255204387e+282};
  722. static double const rrnegtoperr[13]={3.9565301985100693e-040,-2.299904345391321e-063,3.6506201437945798e-086,1.1875228833981544e-109,-5.0644902316928607e-132,-6.7156837247865426e-155,-2.812077463003139e-178,-5.7778912386589953e-201,7.4997100559334532e-224,-4.6439668915134491e-247,-6.3691100762962136e-270,-9.436808465446358e-293,8.0970921678014997e-317};
  723. #if defined(_MSC_VER) && (_MSC_VER<=1200)
  724. static rU64 const rrpot[20]={1,10,100,1000, 10000,100000,1000000,10000000, 100000000,1000000000,10000000000,100000000000, 1000000000000,10000000000000,100000000000000,1000000000000000, 10000000000000000,100000000000000000,1000000000000000000,10000000000000000000U };
  725. #define rrtento19th ((rU64)1000000000000000000)
  726. #else
  727. static rU64 const rrpot[20]={1,10,100,1000, 10000,100000,1000000,10000000, 100000000,1000000000,10000000000ULL,100000000000ULL, 1000000000000ULL,10000000000000ULL,100000000000000ULL,1000000000000000ULL, 10000000000000000ULL,100000000000000000ULL,1000000000000000000ULL,10000000000000000000ULL };
  728. #define rrtento19th (1000000000000000000ULL)
  729. #endif
  730. #define rrddmulthi(oh,ol,xh,yh) \
  731. { \
  732. double ahi=0,alo,bhi=0,blo; \
  733. rS64 bt; \
  734. oh = xh * yh; \
  735. RRCOPYFP(bt,xh); bt&=((~(rU64)0)<<27); RRCOPYFP(ahi,bt); alo = xh-ahi; \
  736. RRCOPYFP(bt,yh); bt&=((~(rU64)0)<<27); RRCOPYFP(bhi,bt); blo = yh-bhi; \
  737. ol = ((ahi*bhi-oh)+ahi*blo+alo*bhi)+alo*blo; \
  738. }
  739. #define rrddtoS64(ob,xh,xl) \
  740. { \
  741. double ahi=0,alo,vh,t;\
  742. ob = (rS64)ph;\
  743. vh=(double)ob;\
  744. ahi = ( xh - vh );\
  745. t = ( ahi - xh );\
  746. alo = (xh-(ahi-t))-(vh+t);\
  747. ob += (rS64)(ahi+alo+xl);\
  748. }
  749. #define rrddrenorm(oh,ol) { double s; s=oh+ol; ol=ol-(s-oh); oh=s; }
  750. #define rrddmultlo(oh,ol,xh,xl,yh,yl) \
  751. ol = ol + ( xh*yl + xl*yh ); \
  752. #define rrddmultlos(oh,ol,xh,yl) \
  753. ol = ol + ( xh*yl ); \
  754. static void rrraise_to_power10( double *ohi, double *olo, double d, rS32 power ) // power can be -323 to +350
  755. {
  756. double ph, pl;
  757. if ((power>=0) && (power<=22))
  758. {
  759. rrddmulthi(ph,pl,d,rrbot[power]);
  760. }
  761. else
  762. {
  763. rS32 e,et,eb;
  764. double p2h,p2l;
  765. e=power; if (power<0) e=-e;
  766. et = (e*0x2c9)>>14;/* %23 */ if (et>13) et=13; eb = e-(et*23);
  767. ph = d; pl = 0.0;
  768. if (power<0)
  769. {
  770. if (eb) { --eb; rrddmulthi(ph,pl,d,rrnegbot[eb]); rrddmultlos(ph,pl,d,rrnegboterr[eb]); }
  771. if (et)
  772. {
  773. rrddrenorm(ph,pl);
  774. --et; rrddmulthi(p2h,p2l,ph,rrnegtop[et]); rrddmultlo(p2h,p2l,ph,pl,rrnegtop[et],rrnegtoperr[et]); ph=p2h;pl=p2l;
  775. }
  776. }
  777. else
  778. {
  779. if (eb)
  780. {
  781. e = eb; if (eb>22) eb=22; e -= eb;
  782. rrddmulthi(ph,pl,d,rrbot[eb]);
  783. if ( e ) { rrddrenorm(ph,pl); rrddmulthi(p2h,p2l,ph,rrbot[e]); rrddmultlos(p2h,p2l,rrbot[e],pl); ph=p2h;pl=p2l; }
  784. }
  785. if (et)
  786. {
  787. rrddrenorm(ph,pl);
  788. --et; rrddmulthi(p2h,p2l,ph,rrtop[et]); rrddmultlo(p2h,p2l,ph,pl,rrtop[et],rrtoperr[et]); ph=p2h;pl=p2l;
  789. }
  790. }
  791. }
  792. rrddrenorm(ph,pl);
  793. *ohi = ph; *olo = pl;
  794. }
  795. // given a float value, returns the significant bits in bits, and the position of the
  796. // decimal point in decimal_pos. +/-INF and NAN are specified by special values
  797. // returned in the decimal_pos parameter.
  798. // frac_digits is absolute normally, but if you want from first significant digits (got %g and %e), or in 0x80000000
  799. static rS32 rrreal_to_str( char const * * start, rU32 * len, char *out, rS32 * decimal_pos, double value, rU32 frac_digits )
  800. {
  801. double d;
  802. rS64 bits = 0;
  803. rS32 expo, e, ng, tens;
  804. d = value;
  805. RRCOPYFP(bits,d);
  806. expo = (bits >> 52) & 2047;
  807. ng = (rS32)(bits >> 63);
  808. if (ng) d=-d;
  809. if ( expo == 2047 ) // is nan or inf?
  810. {
  811. *start = (bits&((((rU64)1)<<52)-1)) ? "NaN" : "Inf";
  812. *decimal_pos = RRSPECIAL;
  813. *len = 3;
  814. return ng;
  815. }
  816. if ( expo == 0 ) // is zero or denormal
  817. {
  818. if ((bits<<1)==0) // do zero
  819. {
  820. *decimal_pos = 1;
  821. *start = out;
  822. out[0] = '0'; *len = 1;
  823. return ng;
  824. }
  825. // find the right expo for denormals
  826. {
  827. rS64 v = ((rU64)1)<<51;
  828. while ((bits&v)==0) { --expo; v >>= 1; }
  829. }
  830. }
  831. // find the decimal exponent as well as the decimal bits of the value
  832. {
  833. double ph,pl;
  834. // log10 estimate - very specifically tweaked to hit or undershoot by no more than 1 of log10 of all expos 1..2046
  835. tens=expo-1023; tens = (tens<0)?((tens*617)/2048):(((tens*1233)/4096)+1);
  836. // move the significant bits into position and stick them into an int
  837. rrraise_to_power10( &ph, &pl, d, 18-tens );
  838. // get full as much precision from double-double as possible
  839. rrddtoS64( bits, ph,pl );
  840. // check if we undershot
  841. if ( ((rU64)bits) >= rrtento19th ) ++tens;
  842. }
  843. // now do the rounding in integer land
  844. frac_digits = ( frac_digits & 0x80000000 ) ? ( (frac_digits&0x7ffffff) + 1 ) : ( tens + frac_digits );
  845. if ( ( frac_digits < 24 ) )
  846. {
  847. rU32 dg = 1; if ((rU64)bits >= rrpot[9] ) dg=10; while( (rU64)bits >= rrpot[dg] ) { ++dg; if (dg==20) goto noround; }
  848. if ( frac_digits < dg )
  849. {
  850. rU64 r;
  851. // add 0.5 at the right position and round
  852. e = dg - frac_digits;
  853. if ( (rU32)e >= 24 ) goto noround;
  854. r = rrpot[e];
  855. bits = bits + (r/2);
  856. if ( (rU64)bits >= rrpot[dg] ) ++tens;
  857. bits /= r;
  858. }
  859. noround:;
  860. }
  861. // kill long trailing runs of zeros
  862. if ( bits )
  863. {
  864. rU32 n; for(;;) { if ( bits<=0xffffffff ) break; if (bits%1000) goto donez; bits/=1000; } n = (rU32)bits; while ((n%1000)==0) n/=1000; bits=n; donez:;
  865. }
  866. // convert to string
  867. out += 64;
  868. e = 0;
  869. for(;;)
  870. {
  871. rU32 n;
  872. char * o = out-8;
  873. // do the conversion in chunks of U32s (avoid most 64-bit divides, worth it, constant denomiators be damned)
  874. if (bits>=100000000) { n = (rU32)( bits % 100000000); bits /= 100000000; } else {n = (rU32)bits; bits = 0; }
  875. while(n) { out-=2; *(rU16*)out=*(rU16*)&rrdiglookup[(n%100)*2]; n/=100; e+=2; }
  876. if (bits==0) { if ((e) && (out[0]=='0')) { ++out; --e; } break; }
  877. while( out!=o ) { *--out ='0'; ++e; }
  878. }
  879. *decimal_pos = tens;
  880. *start = out;
  881. *len = e;
  882. return ng;
  883. }
  884. #undef rrddmulthi
  885. #undef rrddrenorm
  886. #undef rrddmultlo
  887. #undef rrddmultlos
  888. #undef RRSPECIAL
  889. #undef RRCOPYFP
  890. #endif
  891. // clean up
  892. #undef rU16
  893. #undef rU32
  894. #undef rS32
  895. #undef rU64
  896. #undef rS64
  897. #undef RRPUBLIC_DEC
  898. #undef RRPUBLIC_DEF
  899. #undef RR_SPRINTF_DECORATE
  900. #undef RR_UNALIGNED
  901. #endif
  902. #endif