stb_sprintf.h
This file
Path
stb_sprintf.h
1,874 lines · 55.8 KiB · C
Last changed by
stb_sprintf: redo ASAN fixes after problematic mergeSean Barrettbefore adoption3f5db89
History of this fileTake it
The walk stopped after 200 commits: lines older than e895f6d point at it.
update version numbersSean Barrettbefore adoption63b59b4
line 1why, one step back1// stb_sprintf - v1.06 - public domain snprintf() implementationtweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 2–3why, one step back2// originally by Jeff Roberts / RAD Game Tools, 2015/10/203// http://github.com/nothings/stbMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 4why, one step back4//tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 5–6why, one step back5// allowed types: sc uidBboXx p AaGgEef n6// lengths : h ll j z t I64 I32 IMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 7–10why, one step back7//8// Contributors:9// Fabian "ryg" Giesen (reformatting)10//tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 11–13why, one step back11// Contributors (bugfixes):12// github:d2643513// github:trex78stb_sprintf: creditsSean Barrettbefore adoptionf11f1e8
line 14why, one step back14// github:account-logintweak previous PR mergeSean Barrettbefore adoptione895f6d
line 15why, one step back15// Jari Komppa (SI suffixes)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 16–17why, one step back16// Rohit Nirmal17// Marcin Wojdyrstb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
line 18why, one step back18// Leonard Ritterstb_sprintf: fix 32 vs 64-bit sizes for format widts j/z/tSean Barrettbefore adoption619cdb6
line 19why, one step back19// Stefano ZanottiMerge branch 'working'Sean Barrettbefore adoption9092c04
line 20why, one step back20// Adam Allisonstb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 21why, one step back21// Arvid GerstmannMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 22why, one step back22//tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 23why, one step back23// LICENSE:Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 24why, one step back24//tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 25why, one step back25// See end of file for license information.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 26why, one step back26tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 27–28why, one step back27#ifndef STB_SPRINTF_H_INCLUDE28#define STB_SPRINTF_H_INCLUDEMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 29–30why, one step back2930/*tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 31why, one step back31Single file sprintf replacement.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 32–33why, one step back3233Originally written by Jeff Roberts at RAD Game Tools - 2015/10/20.tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 34why, one step back34Hereby placed in public domain.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 35why, one step back35tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 36–38why, one step back36This is a full sprintf replacement that supports everything that37the C runtime sprintfs support, including float/double, 64-bit integers,38hex floats, field parameters (%*.*d stuff), length reads backs, etc.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 39why, one step back39tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 40–41why, one step back40Why would you need this if sprintf already exists? Well, first off,41it's *much* faster (see below). It's also much smaller than the CRTMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 42why, one step back42versions code-space-wise. We've also added some simple improvementstweak previous PR mergeSean Barrettbefore adoptione895f6d
line 43why, one step back43that are super handy (commas in thousands, callbacks at buffer full,Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 44–45why, one step back44for example). Finally, the format strings for MSVC and GCC differ45for 64-bit integers (among other small things), so this lets you usetweak previous PR mergeSean Barrettbefore adoptione895f6d
line 46why, one step back46the same format strings in cross platform code.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 47why, one step back47tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 48why, one step back48It uses the standard single file trick of being both the header fileMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 49why, one step back49and the source itself. If you just include it normally, you just gettweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 50–51why, one step back50the header file function definitions. To get the code, you include51it from a C or C++ file and define STB_SPRINTF_IMPLEMENTATION first.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 52why, one step back52tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 53why, one step back53It only uses va_args macros from the C runtime to do it's work. ItMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 54why, one step back54does cast doubles to S64s and shifts and divides U64s, which doestweak previous PR mergeSean Barrettbefore adoptione895f6d
line 55why, one step back55drag in CRT code on most platforms.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 56why, one step back56tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 57–59why, one step back57It compiles to roughly 8K with float support, and 4K without.58As a comparison, when using MSVC static libs, calling sprintf drags59in 16K.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 60why, one step back60tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 61–65why, one step back61API:62====63int stbsp_sprintf( char * buf, char const * fmt, ... )64int stbsp_snprintf( char * buf, int count, char const * fmt, ... )65 Convert an arg list into a buffer. stbsp_snprintf always returnsMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 66–67why, one step back66 a zero-terminated string (unlike regular snprintf).67tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 68–70why, one step back68int stbsp_vsprintf( char * buf, char const * fmt, va_list va )69int stbsp_vsnprintf( char * buf, int count, char const * fmt, va_list va )70 Convert a va_list arg list into a buffer. stbsp_vsnprintf always returnsMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 71–72why, one step back71 a zero-terminated string (unlike regular snprintf).72tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 73–79why, one step back73int stbsp_vsprintfcb( STBSP_SPRINTFCB * callback, void * user, char * buf, char const * fmt, va_list va )74 typedef char * STBSP_SPRINTFCB( char const * buf, void * user, int len );75 Convert into a buffer, calling back every STB_SPRINTF_MIN chars.76 Your callback can then copy the chars out, print them or whatever.77 This function is actually the workhorse for everything else.78 The buffer you pass in must hold at least STB_SPRINTF_MIN characters.79 // you return the next buffer to use or 0 to stop convertingMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 80why, one step back80tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 81–82why, one step back81void stbsp_set_separators( char comma, char period )82 Set the comma and period characters to use.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 83why, one step back83tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 84–89why, one step back84FLOATS/DOUBLES:85===============86This code uses a internal float->ascii conversion method that uses87doubles with error correction (double-doubles, for ~105 bits of88precision). This conversion is round-trip perfect - that is, an atof89of the values output here will give you the bit-exact double back.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 90–93why, one step back9091One difference is that our insignificant digits will be different than92with MSVC or GCC (but they don't match each other either). We also93don't attempt to find the minimum length matching float (pre-MSVC15tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 94why, one step back94doesn't either).Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 95why, one step back95tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 96–97why, one step back96If you don't need float or doubles at all, define STB_SPRINTF_NOFLOAT97and you'll save 4K of code space.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 98why, one step back98tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 99–103why, one step back9964-BIT INTS:100============101This library also supports 64-bit integers and you can use MSVC style or102GCC style indicators (%I64d or %lld). It supports the C99 specifiers103for size_t and ptr_diff_t (%jd %zd) as well.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 104why, one step back104tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 105–107why, one step back105EXTRAS:106=======107Like some GCCs, for integers and floats, you can use a ' (single quote)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 108why, one step back108specifier and commas will be inserted on the thousands: "%'d" on 12345tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 109why, one step back109would print 12,345.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 110–111why, one step back110111For integers and floats, you can use a "$" specifier and the numbertweak previous PR mergeSean Barrettbefore adoptione895f6d
line 112why, one step back112will be converted to float and then divided to get kilo, mega, giga orMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 113–114why, one step back113tera and then printed, so "%$d" 1000 is "1.0 k", "%$.2d" 2536000 is114"2.53 M", etc. For byte values, use two $:s, like "%$$d" to turntweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 115–117why, one step back1152536000 to "2.42 Mi". If you prefer JEDEC suffixes to SI ones, use three116$:s: "%$$$d" -> "2.42 M". To remove the space between the number and the117suffix, add "_" specifier: "%_$d" -> "2.53M".Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 118–119why, one step back118119In addition to octal and hexadecimal conversions, you can printtweak previous PR mergeSean Barrettbefore adoptione895f6d
line 120why, one step back120integers in binary: "%b" for 256 would print 100.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 121why, one step back121tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 122–141why, one step back122PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC):123===================================================================124"%d" across all 32-bit ints (4.8x/4.0x faster than 32-/64-bit MSVC)125"%24d" across all 32-bit ints (4.5x/4.2x faster)126"%x" across all 32-bit ints (4.5x/3.8x faster)127"%08x" across all 32-bit ints (4.3x/3.8x faster)128"%f" across e-10 to e+10 floats (7.3x/6.0x faster)129"%e" across e-10 to e+10 floats (8.1x/6.0x faster)130"%g" across e-10 to e+10 floats (10.0x/7.1x faster)131"%f" for values near e-300 (7.9x/6.5x faster)132"%f" for values near e+300 (10.0x/9.1x faster)133"%e" for values near e-300 (10.1x/7.0x faster)134"%e" for values near e+300 (9.2x/6.0x faster)135"%.320f" for values near e-300 (12.6x/11.2x faster)136"%a" for random values (8.6x/4.3x faster)137"%I64d" for 64-bits with 32-bit values (4.8x/3.4x faster)138"%I64d" for 64-bits > 32-bit values (4.9x/5.5x faster)139"%s%s%s" for 64 char strings (7.1x/7.3x faster)140"...512 char string..." ( 35.0x/32.5x faster!)141*/Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 142why, one step back142stb_sprintf: redo ASAN fixes after problematic mergeSean Barrettbefore adoption3f5db89
lines 143–151why, one step back143#if defined(__clang__)144 #if defined(__has_feature) && defined(__has_attribute)145 #if __has_feature(address_sanitizer)146 #if __has_attribute(__no_sanitize__)147 #define STBSP__ASAN __attribute__((__no_sanitize__("address")))148 #elif __has_attribute(__no_sanitize_address__)149 #define STBSP__ASAN __attribute__((__no_sanitize_address__))150 #elif __has_attribute(__no_address_safety_analysis__)151 #define STBSP__ASAN __attribute__((__no_address_safety_analysis__))Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 152why, one step back152 #endifstb_sprintf: redo ASAN fixes after problematic mergeSean Barrettbefore adoption3f5db89
lines 153–158why, one step back153 #endif154 #endif155#elif __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)156 #if __SANITIZE_ADDRESS__157 #define STBSP__ASAN __attribute__((__no_sanitize_address__))158 #endifMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 159why, one step back159#endifstb_sprintf: redo ASAN fixes after problematic mergeSean Barrettbefore adoption3f5db89
lines 160–162why, one step back160161#ifndef STBSP__ASAN162#define STBSP__ASANMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 163–164why, one step back163#endif164tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 165–166why, one step back165#ifdef STB_SPRINTF_STATIC166#define STBSP__PUBLICDEC staticstb_sprintf: redo ASAN fixes after problematic mergeSean Barrettbefore adoption3f5db89
line 167why, one step back167#define STBSP__PUBLICDEF static STBSP__ASANMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 168why, one step back168#elsetweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 169–170why, one step back169#ifdef __cplusplus170#define STBSP__PUBLICDEC extern "C"stb_sprintf: redo ASAN fixes after problematic mergeSean Barrettbefore adoption3f5db89
line 171why, one step back171#define STBSP__PUBLICDEF extern "C" STBSP__ASANMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 172why, one step back172#elsetweak previous PR mergeSean Barrettbefore adoptione895f6d
line 173why, one step back173#define STBSP__PUBLICDEC externstb_sprintf: redo ASAN fixes after problematic mergeSean Barrettbefore adoption3f5db89
line 174why, one step back174#define STBSP__PUBLICDEF STBSP__ASANMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 175–178why, one step back175#endif176#endif177178#include <stdarg.h> // for va_list()stb_sprintf: fix 32 vs 64-bit sizes for format widts j/z/tSean Barrettbefore adoption619cdb6
line 179why, one step back179#include <stddef.h> // size_t, ptrdiff_tMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 180why, one step back180tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 181–182why, one step back181#ifndef STB_SPRINTF_MIN182#define STB_SPRINTF_MIN 512 // how many characters per callbackMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 183–185why, one step back183#endif184typedef char *STBSP_SPRINTFCB(char *buf, void *user, int len);185tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 186why, one step back186#ifndef STB_SPRINTF_DECORATEMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 187–197why, one step back187#define STB_SPRINTF_DECORATE(name) stbsp_##name // define this before including if you want to change the names188#endif189190STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va);191STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsnprintf)(char *buf, int count, char const *fmt, va_list va);192STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...);193STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...);194195STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va);196STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char comma, char period);197tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 198why, one step back198#endif // STB_SPRINTF_H_INCLUDEMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 199why, one step back199tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 200why, one step back200#ifdef STB_SPRINTF_IMPLEMENTATIONMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 201–203why, one step back201202#include <stdlib.h> // for va_arg()203tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 204–205why, one step back204#define stbsp__uint32 unsigned int205#define stbsp__int32 signed intMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 206why, one step back206tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 207–209why, one step back207#ifdef _MSC_VER208#define stbsp__uint64 unsigned __int64209#define stbsp__int64 signed __int64Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 210why, one step back210#elsetweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 211–212why, one step back211#define stbsp__uint64 unsigned long long212#define stbsp__int64 signed long longMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 213why, one step back213#endiftweak previous PR mergeSean Barrettbefore adoptione895f6d
line 214why, one step back214#define stbsp__uint16 unsigned shortMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 215–216why, one step back215216#ifndef stbsp__uintptrtweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 217–218why, one step back217#if defined(__ppc64__) || defined(__aarch64__) || defined(_M_X64) || defined(__x86_64__) || defined(__x86_64)218#define stbsp__uintptr stbsp__uint64Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 219why, one step back219#elsetweak previous PR mergeSean Barrettbefore adoptione895f6d
line 220why, one step back220#define stbsp__uintptr stbsp__uint32Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 221–225why, one step back221#endif222#endif223224#ifndef STB_SPRINTF_MSVC_MODE // used for MSVC2013 and earlier (MSVC2015 matches GCC)225#if defined(_MSC_VER) && (_MSC_VER < 1900)tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 226why, one step back226#define STB_SPRINTF_MSVC_MODEMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 227–230why, one step back227#endif228#endif229230#ifdef STB_SPRINTF_NOUNALIGNED // define this before inclusion to force stbsp_sprintf to always use aligned accessestweak previous PR mergeSean Barrettbefore adoptione895f6d
line 231why, one step back231#define STBSP__UNALIGNED(code)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 232why, one step back232#elsetweak previous PR mergeSean Barrettbefore adoptione895f6d
line 233why, one step back233#define STBSP__UNALIGNED(code) codeMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 234–236why, one step back234#endif235236#ifndef STB_SPRINTF_NOFLOATtweak previous PR mergeSean Barrettbefore adoptione895f6d
line 237why, one step back237// internal float utility functionsMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 238–239why, one step back238static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits);239static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value);tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 240why, one step back240#define STBSP__SPECIAL 0x7000Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 241–244why, one step back241#endif242243static char stbsp__period = '.';244static char stbsp__comma = ',';Merge branch 'working'Sean Barrettbefore adoption9092c04
lines 245–251why, one step back245static struct246{247 short temp; // force next field to be 2-byte aligned248 char pair[201];249} stbsp__digitpair =250{251 0,stb_sprintf: minor reformattingSean Barrettbefore adoption9f1ef73
lines 252–254why, one step back252 "00010203040506070809101112131415161718192021222324"253 "25262728293031323334353637383940414243444546474849"254 "50515253545556575859606162636465666768697071727374"Merge branch 'working'Sean Barrettbefore adoption9092c04
lines 255–256why, one step back255 "75767778798081828384858687888990919293949596979899"256};Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 257–263why, one step back257258STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char pcomma, char pperiod)259{260 stbsp__period = pperiod;261 stbsp__comma = pcomma;262}263tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 264–276why, one step back264#define STBSP__LEFTJUST 1265#define STBSP__LEADINGPLUS 2266#define STBSP__LEADINGSPACE 4267#define STBSP__LEADING_0X 8268#define STBSP__LEADINGZERO 16269#define STBSP__INTMAX 32270#define STBSP__TRIPLET_COMMA 64271#define STBSP__NEGATIVE 128272#define STBSP__METRIC_SUFFIX 256273#define STBSP__HALFWIDTH 512274#define STBSP__METRIC_NOSPACE 1024275#define STBSP__METRIC_1024 2048276#define STBSP__METRIC_JEDEC 4096Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 277why, one step back277tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 278why, one step back278static void stbsp__lead_sign(stbsp__uint32 fl, char *sign)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 279–361why, one step back279{280 sign[0] = 0;281 if (fl & STBSP__NEGATIVE) {282 sign[0] = 1;283 sign[1] = '-';284 } else if (fl & STBSP__LEADINGSPACE) {285 sign[0] = 1;286 sign[1] = ' ';287 } else if (fl & STBSP__LEADINGPLUS) {288 sign[0] = 1;289 sign[1] = '+';290 }291}292293STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va)294{295 static char hex[] = "0123456789abcdefxp";296 static char hexu[] = "0123456789ABCDEFXP";297 char *bf;298 char const *f;299 int tlen = 0;300301 bf = buf;302 f = fmt;303 for (;;) {304 stbsp__int32 fw, pr, tz;305 stbsp__uint32 fl;306307 // macros for the callback buffer stuff308 #define stbsp__chk_cb_bufL(bytes) \309 { \310 int len = (int)(bf - buf); \311 if ((len + (bytes)) >= STB_SPRINTF_MIN) { \312 tlen += len; \313 if (0 == (bf = buf = callback(buf, user, len))) \314 goto done; \315 } \316 }317 #define stbsp__chk_cb_buf(bytes) \318 { \319 if (callback) { \320 stbsp__chk_cb_bufL(bytes); \321 } \322 }323 #define stbsp__flush_cb() \324 { \325 stbsp__chk_cb_bufL(STB_SPRINTF_MIN - 1); \326 } // flush if there is even one byte in the buffer327 #define stbsp__cb_buf_clamp(cl, v) \328 cl = v; \329 if (callback) { \330 int lg = STB_SPRINTF_MIN - (int)(bf - buf); \331 if (cl > lg) \332 cl = lg; \333 }334335 // fast copy everything up to the next % (or end of string)336 for (;;) {337 while (((stbsp__uintptr)f) & 3) {338 schk1:339 if (f[0] == '%')340 goto scandd;341 schk2:342 if (f[0] == 0)343 goto endfmt;344 stbsp__chk_cb_buf(1);345 *bf++ = f[0];346 ++f;347 }348 for (;;) {349 // Check if the next 4 bytes contain %(0x25) or end of string.350 // Using the 'hasless' trick:351 // https://graphics.stanford.edu/~seander/bithacks.html#HasLessInWord352 stbsp__uint32 v, c;353 v = *(stbsp__uint32 *)f;354 c = (~v) & 0x80808080;355 if (((v ^ 0x25252525) - 0x01010101) & c)356 goto schk1;357 if ((v - 0x01010101) & c)358 goto schk2;359 if (callback)360 if ((STB_SPRINTF_MIN - (int)(bf - buf)) < 4)361 goto schk1;Merge branch 'fix-unaligned' of https://github.com/stefano-zanotti-88/stb into workingSean Barrettbefore adoption6e7e5c5
lines 362–372why, one step back362 #ifdef STB_SPRINTF_NOUNALIGNED363 if(((stbsp__uintptr)bf) & 3) {364 bf[0] = f[0];365 bf[1] = f[1];366 bf[2] = f[2];367 bf[3] = f[3];368 } else369 #endif370 {371 *(stbsp__uint32 *)bf = v;372 }Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 373–469why, one step back373 bf += 4;374 f += 4;375 }376 }377 scandd:378379 ++f;380381 // ok, we have a percent, read the modifiers first382 fw = 0;383 pr = -1;384 fl = 0;385 tz = 0;386387 // flags388 for (;;) {389 switch (f[0]) {390 // if we have left justify391 case '-':392 fl |= STBSP__LEFTJUST;393 ++f;394 continue;395 // if we have leading plus396 case '+':397 fl |= STBSP__LEADINGPLUS;398 ++f;399 continue;400 // if we have leading space401 case ' ':402 fl |= STBSP__LEADINGSPACE;403 ++f;404 continue;405 // if we have leading 0x406 case '#':407 fl |= STBSP__LEADING_0X;408 ++f;409 continue;410 // if we have thousand commas411 case '\'':412 fl |= STBSP__TRIPLET_COMMA;413 ++f;414 continue;415 // if we have kilo marker (none->kilo->kibi->jedec)416 case '$':417 if (fl & STBSP__METRIC_SUFFIX) {418 if (fl & STBSP__METRIC_1024) {419 fl |= STBSP__METRIC_JEDEC;420 } else {421 fl |= STBSP__METRIC_1024;422 }423 } else {424 fl |= STBSP__METRIC_SUFFIX;425 }426 ++f;427 continue;428 // if we don't want space between metric suffix and number429 case '_':430 fl |= STBSP__METRIC_NOSPACE;431 ++f;432 continue;433 // if we have leading zero434 case '0':435 fl |= STBSP__LEADINGZERO;436 ++f;437 goto flags_done;438 default: goto flags_done;439 }440 }441 flags_done:442443 // get the field width444 if (f[0] == '*') {445 fw = va_arg(va, stbsp__uint32);446 ++f;447 } else {448 while ((f[0] >= '0') && (f[0] <= '9')) {449 fw = fw * 10 + f[0] - '0';450 f++;451 }452 }453 // get the precision454 if (f[0] == '.') {455 ++f;456 if (f[0] == '*') {457 pr = va_arg(va, stbsp__uint32);458 ++f;459 } else {460 pr = 0;461 while ((f[0] >= '0') && (f[0] <= '9')) {462 pr = pr * 10 + f[0] - '0';463 f++;464 }465 }466 }467468 // handle integer size overrides469 switch (f[0]) {tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 470why, one step back470 // are we halfwidth?Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 471–474why, one step back471 case 'h':472 fl |= STBSP__HALFWIDTH;473 ++f;474 break;tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 475why, one step back475 // are we 64-bit (unix style)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 476why, one step back476 case 'l':"long int" => "long"Sean Barrettbefore adoptiona23f466
line 477why, one step back477 fl |= ((sizeof(long) == 8) ? STBSP__INTMAX : 0);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 478–483why, one step back478 ++f;479 if (f[0] == 'l') {480 fl |= STBSP__INTMAX;481 ++f;482 }483 break;tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 484why, one step back484 // are we 64-bit on intmax? (c99)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 485why, one step back485 case 'j':stb_sprintf: fix 32 vs 64-bit sizes for format widts j/z/tSean Barrettbefore adoption619cdb6
line 486why, one step back486 fl |= (sizeof(size_t) == 8) ? STBSP__INTMAX : 0;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 487–488why, one step back487 ++f;488 break;tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 489why, one step back489 // are we 64-bit on size_t or ptrdiff_t? (c99)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 490why, one step back490 case 'z':stb_sprintf: fix 32 vs 64-bit sizes for format widts j/z/tSean Barrettbefore adoption619cdb6
lines 491–493why, one step back491 fl |= (sizeof(ptrdiff_t) == 8) ? STBSP__INTMAX : 0;492 ++f;493 break;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 494why, one step back494 case 't':stb_sprintf: fix 32 vs 64-bit sizes for format widts j/z/tSean Barrettbefore adoption619cdb6
line 495why, one step back495 fl |= (sizeof(ptrdiff_t) == 8) ? STBSP__INTMAX : 0;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 496–497why, one step back496 ++f;497 break;tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 498why, one step back498 // are we 64-bit (msft style)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 499–509why, one step back499 case 'I':500 if ((f[1] == '6') && (f[2] == '4')) {501 fl |= STBSP__INTMAX;502 f += 3;503 } else if ((f[1] == '3') && (f[2] == '2')) {504 f += 3;505 } else {506 fl |= ((sizeof(void *) == 8) ? STBSP__INTMAX : 0);507 ++f;508 }509 break;tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 510why, one step back510 default: break;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 511–528why, one step back511 }512513 // handle each replacement514 switch (f[0]) {515 #define STBSP__NUMSZ 512 // big enough for e308 (with commas) or e-307516 char num[STBSP__NUMSZ];517 char lead[8];518 char tail[8];519 char *s;520 char const *h;521 stbsp__uint32 l, n, cs;522 stbsp__uint64 n64;523#ifndef STB_SPRINTF_NOFLOAT524 double fv;525#endif526 stbsp__int32 dp;527 char const *sn;528tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 529why, one step back529 case 's':Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 530–538why, one step back530 // get the string531 s = va_arg(va, char *);532 if (s == 0)533 s = (char *)"null";534 // get the length535 sn = s;536 for (;;) {537 if ((((stbsp__uintptr)sn) & 3) == 0)538 break;tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 539why, one step back539 lchk:Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 540–572why, one step back540 if (sn[0] == 0)541 goto ld;542 ++sn;543 }544 n = 0xffffffff;545 if (pr >= 0) {546 n = (stbsp__uint32)(sn - s);547 if (n >= (stbsp__uint32)pr)548 goto ld;549 n = ((stbsp__uint32)(pr - n)) >> 2;550 }551 while (n) {552 stbsp__uint32 v = *(stbsp__uint32 *)sn;553 if ((v - 0x01010101) & (~v) & 0x80808080UL)554 goto lchk;555 sn += 4;556 --n;557 }558 goto lchk;559 ld:560561 l = (stbsp__uint32)(sn - s);562 // clamp to precision563 if (l > (stbsp__uint32)pr)564 l = pr;565 lead[0] = 0;566 tail[0] = 0;567 pr = 0;568 dp = 0;569 cs = 0;570 // copy the string in571 goto scopy;572tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 573why, one step back573 case 'c': // charMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 574–584why, one step back574 // get the character575 s = num + STBSP__NUMSZ - 1;576 *s = (char)va_arg(va, int);577 l = 1;578 lead[0] = 0;579 tail[0] = 0;580 pr = 0;581 dp = 0;582 cs = 0;583 goto scopy;584tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 585why, one step back585 case 'n': // weird write-bytes specifierMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 586–590why, one step back586 {587 int *d = va_arg(va, int *);588 *d = tlen + (int)(bf - buf);589 } break;590tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 591why, one step back591#ifdef STB_SPRINTF_NOFLOATMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 592–709why, one step back592 case 'A': // float593 case 'a': // hex float594 case 'G': // float595 case 'g': // float596 case 'E': // float597 case 'e': // float598 case 'f': // float599 va_arg(va, double); // eat it600 s = (char *)"No float";601 l = 8;602 lead[0] = 0;603 tail[0] = 0;604 pr = 0;605 dp = 0;606 cs = 0;607 goto scopy;608#else609 case 'A': // hex float610 case 'a': // hex float611 h = (f[0] == 'A') ? hexu : hex;612 fv = va_arg(va, double);613 if (pr == -1)614 pr = 6; // default is 6615 // read the double into a string616 if (stbsp__real_to_parts((stbsp__int64 *)&n64, &dp, fv))617 fl |= STBSP__NEGATIVE;618619 s = num + 64;620621 stbsp__lead_sign(fl, lead);622623 if (dp == -1023)624 dp = (n64) ? -1022 : 0;625 else626 n64 |= (((stbsp__uint64)1) << 52);627 n64 <<= (64 - 56);628 if (pr < 15)629 n64 += ((((stbsp__uint64)8) << 56) >> (pr * 4));630// add leading chars631632#ifdef STB_SPRINTF_MSVC_MODE633 *s++ = '0';634 *s++ = 'x';635#else636 lead[1 + lead[0]] = '0';637 lead[2 + lead[0]] = 'x';638 lead[0] += 2;639#endif640 *s++ = h[(n64 >> 60) & 15];641 n64 <<= 4;642 if (pr)643 *s++ = stbsp__period;644 sn = s;645646 // print the bits647 n = pr;648 if (n > 13)649 n = 13;650 if (pr > (stbsp__int32)n)651 tz = pr - n;652 pr = 0;653 while (n--) {654 *s++ = h[(n64 >> 60) & 15];655 n64 <<= 4;656 }657658 // print the expo659 tail[1] = h[17];660 if (dp < 0) {661 tail[2] = '-';662 dp = -dp;663 } else664 tail[2] = '+';665 n = (dp >= 1000) ? 6 : ((dp >= 100) ? 5 : ((dp >= 10) ? 4 : 3));666 tail[0] = (char)n;667 for (;;) {668 tail[n] = '0' + dp % 10;669 if (n <= 3)670 break;671 --n;672 dp /= 10;673 }674675 dp = (int)(s - sn);676 l = (int)(s - (num + 64));677 s = num + 64;678 cs = 1 + (3 << 24);679 goto scopy;680681 case 'G': // float682 case 'g': // float683 h = (f[0] == 'G') ? hexu : hex;684 fv = va_arg(va, double);685 if (pr == -1)686 pr = 6;687 else if (pr == 0)688 pr = 1; // default is 6689 // read the double into a string690 if (stbsp__real_to_str(&sn, &l, num, &dp, fv, (pr - 1) | 0x80000000))691 fl |= STBSP__NEGATIVE;692693 // clamp the precision and delete extra zeros after clamp694 n = pr;695 if (l > (stbsp__uint32)pr)696 l = pr;697 while ((l > 1) && (pr) && (sn[l - 1] == '0')) {698 --pr;699 --l;700 }701702 // should we use %e703 if ((dp <= -4) || (dp > (stbsp__int32)n)) {704 if (pr > (stbsp__int32)l)705 pr = l - 1;706 else if (pr)707 --pr; // when using %e, there is one digit before the decimal708 goto doexpfromg;709 }Merge branch 'misc-typos' of https://github.com/luzpaz/stb into workingSean Barrettbefore adoption3005bcf
line 710why, one step back710 // this is the insane action to get the pr to match %g semantics for %fMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 711–713why, one step back711 if (dp > 0) {712 pr = (dp < (stbsp__int32)l) ? l - dp : 0;713 } else {Merge branch 'working'Sean Barrettbefore adoption9092c04
line 714why, one step back714 pr = -dp + ((pr > (stbsp__int32)l) ? (stbsp__int32) l : pr);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 715–779why, one step back715 }716 goto dofloatfromg;717718 case 'E': // float719 case 'e': // float720 h = (f[0] == 'E') ? hexu : hex;721 fv = va_arg(va, double);722 if (pr == -1)723 pr = 6; // default is 6724 // read the double into a string725 if (stbsp__real_to_str(&sn, &l, num, &dp, fv, pr | 0x80000000))726 fl |= STBSP__NEGATIVE;727 doexpfromg:728 tail[0] = 0;729 stbsp__lead_sign(fl, lead);730 if (dp == STBSP__SPECIAL) {731 s = (char *)sn;732 cs = 0;733 pr = 0;734 goto scopy;735 }736 s = num + 64;737 // handle leading chars738 *s++ = sn[0];739740 if (pr)741 *s++ = stbsp__period;742743 // handle after decimal744 if ((l - 1) > (stbsp__uint32)pr)745 l = pr + 1;746 for (n = 1; n < l; n++)747 *s++ = sn[n];748 // trailing zeros749 tz = pr - (l - 1);750 pr = 0;751 // dump expo752 tail[1] = h[0xe];753 dp -= 1;754 if (dp < 0) {755 tail[2] = '-';756 dp = -dp;757 } else758 tail[2] = '+';759#ifdef STB_SPRINTF_MSVC_MODE760 n = 5;761#else762 n = (dp >= 100) ? 5 : 4;763#endif764 tail[0] = (char)n;765 for (;;) {766 tail[n] = '0' + dp % 10;767 if (n <= 3)768 break;769 --n;770 dp /= 10;771 }772 cs = 1 + (3 << 24); // how many tens773 goto flt_lead;774775 case 'f': // float776 fv = va_arg(va, double);777 doafloat:778 // do kilos779 if (fl & STBSP__METRIC_SUFFIX) {tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 780why, one step back780 double divisor;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 781–917why, one step back781 divisor = 1000.0f;782 if (fl & STBSP__METRIC_1024)783 divisor = 1024.0;784 while (fl < 0x4000000) {785 if ((fv < divisor) && (fv > -divisor))786 break;787 fv /= divisor;788 fl += 0x1000000;789 }790 }791 if (pr == -1)792 pr = 6; // default is 6793 // read the double into a string794 if (stbsp__real_to_str(&sn, &l, num, &dp, fv, pr))795 fl |= STBSP__NEGATIVE;796 dofloatfromg:797 tail[0] = 0;798 stbsp__lead_sign(fl, lead);799 if (dp == STBSP__SPECIAL) {800 s = (char *)sn;801 cs = 0;802 pr = 0;803 goto scopy;804 }805 s = num + 64;806807 // handle the three decimal varieties808 if (dp <= 0) {809 stbsp__int32 i;810 // handle 0.000*000xxxx811 *s++ = '0';812 if (pr)813 *s++ = stbsp__period;814 n = -dp;815 if ((stbsp__int32)n > pr)816 n = pr;817 i = n;818 while (i) {819 if ((((stbsp__uintptr)s) & 3) == 0)820 break;821 *s++ = '0';822 --i;823 }824 while (i >= 4) {825 *(stbsp__uint32 *)s = 0x30303030;826 s += 4;827 i -= 4;828 }829 while (i) {830 *s++ = '0';831 --i;832 }833 if ((stbsp__int32)(l + n) > pr)834 l = pr - n;835 i = l;836 while (i) {837 *s++ = *sn++;838 --i;839 }840 tz = pr - (n + l);841 cs = 1 + (3 << 24); // how many tens did we write (for commas below)842 } else {843 cs = (fl & STBSP__TRIPLET_COMMA) ? ((600 - (stbsp__uint32)dp) % 3) : 0;844 if ((stbsp__uint32)dp >= l) {845 // handle xxxx000*000.0846 n = 0;847 for (;;) {848 if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {849 cs = 0;850 *s++ = stbsp__comma;851 } else {852 *s++ = sn[n];853 ++n;854 if (n >= l)855 break;856 }857 }858 if (n < (stbsp__uint32)dp) {859 n = dp - n;860 if ((fl & STBSP__TRIPLET_COMMA) == 0) {861 while (n) {862 if ((((stbsp__uintptr)s) & 3) == 0)863 break;864 *s++ = '0';865 --n;866 }867 while (n >= 4) {868 *(stbsp__uint32 *)s = 0x30303030;869 s += 4;870 n -= 4;871 }872 }873 while (n) {874 if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {875 cs = 0;876 *s++ = stbsp__comma;877 } else {878 *s++ = '0';879 --n;880 }881 }882 }883 cs = (int)(s - (num + 64)) + (3 << 24); // cs is how many tens884 if (pr) {885 *s++ = stbsp__period;886 tz = pr;887 }888 } else {889 // handle xxxxx.xxxx000*000890 n = 0;891 for (;;) {892 if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {893 cs = 0;894 *s++ = stbsp__comma;895 } else {896 *s++ = sn[n];897 ++n;898 if (n >= (stbsp__uint32)dp)899 break;900 }901 }902 cs = (int)(s - (num + 64)) + (3 << 24); // cs is how many tens903 if (pr)904 *s++ = stbsp__period;905 if ((l - dp) > (stbsp__uint32)pr)906 l = pr + dp;907 while (n < l) {908 *s++ = sn[n];909 ++n;910 }911 tz = pr - (l - dp);912 }913 }914 pr = 0;915916 // handle k,m,g,t917 if (fl & STBSP__METRIC_SUFFIX) {tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 918why, one step back918 char idx;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 919–947why, one step back919 idx = 1;920 if (fl & STBSP__METRIC_NOSPACE)921 idx = 0;922 tail[0] = idx;923 tail[1] = ' ';924 {925 if (fl >> 24) { // SI kilo is 'k', JEDEC and SI kibits are 'K'.926 if (fl & STBSP__METRIC_1024)927 tail[idx + 1] = "_KMGT"[fl >> 24];928 else929 tail[idx + 1] = "_kMGT"[fl >> 24];930 idx++;931 // If printing kibits and not in jedec, add the 'i'.932 if (fl & STBSP__METRIC_1024 && !(fl & STBSP__METRIC_JEDEC)) {933 tail[idx + 1] = 'i';934 idx++;935 }936 tail[0] = idx;937 }938 }939 };940941 flt_lead:942 // get the length that we copied943 l = (stbsp__uint32)(s - (num + 64));944 s = num + 64;945 goto scopy;946#endif947tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 948–949why, one step back948 case 'B': // upper binary949 case 'b': // lower binaryMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 950–959why, one step back950 h = (f[0] == 'B') ? hexu : hex;951 lead[0] = 0;952 if (fl & STBSP__LEADING_0X) {953 lead[0] = 2;954 lead[1] = '0';955 lead[2] = h[0xb];956 }957 l = (8 << 4) | (1 << 8);958 goto radixnum;959tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 960why, one step back960 case 'o': // octalMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 961–969why, one step back961 h = hexu;962 lead[0] = 0;963 if (fl & STBSP__LEADING_0X) {964 lead[0] = 1;965 lead[1] = '0';966 }967 l = (3 << 4) | (3 << 8);968 goto radixnum;969tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 970why, one step back970 case 'p': // pointerMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 971–1025why, one step back971 fl |= (sizeof(void *) == 8) ? STBSP__INTMAX : 0;972 pr = sizeof(void *) * 2;973 fl &= ~STBSP__LEADINGZERO; // 'p' only prints the pointer with zeros974 // fall through - to X975976 case 'X': // upper hex977 case 'x': // lower hex978 h = (f[0] == 'X') ? hexu : hex;979 l = (4 << 4) | (4 << 8);980 lead[0] = 0;981 if (fl & STBSP__LEADING_0X) {982 lead[0] = 2;983 lead[1] = '0';984 lead[2] = h[16];985 }986 radixnum:987 // get the number988 if (fl & STBSP__INTMAX)989 n64 = va_arg(va, stbsp__uint64);990 else991 n64 = va_arg(va, stbsp__uint32);992993 s = num + STBSP__NUMSZ;994 dp = 0;995 // clear tail, and clear leading if value is zero996 tail[0] = 0;997 if (n64 == 0) {998 lead[0] = 0;999 if (pr == 0) {1000 l = 0;1001 cs = (((l >> 4) & 15)) << 24;1002 goto scopy;1003 }1004 }1005 // convert to string1006 for (;;) {1007 *--s = h[n64 & ((1 << (l >> 8)) - 1)];1008 n64 >>= (l >> 8);1009 if (!((n64) || ((stbsp__int32)((num + STBSP__NUMSZ) - s) < pr)))1010 break;1011 if (fl & STBSP__TRIPLET_COMMA) {1012 ++l;1013 if ((l & 15) == ((l >> 4) & 15)) {1014 l &= ~15;1015 *--s = stbsp__comma;1016 }1017 }1018 };1019 // get the tens and the comma pos1020 cs = (stbsp__uint32)((num + STBSP__NUMSZ) - s) + ((((l >> 4) & 15)) << 24);1021 // get the length that we copied1022 l = (stbsp__uint32)((num + STBSP__NUMSZ) - s);1023 // copy it1024 goto scopy;1025tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 1026–1028why, one step back1026 case 'u': // unsigned1027 case 'i':1028 case 'd': // integerMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1029–1073why, one step back1029 // get the integer and abs it1030 if (fl & STBSP__INTMAX) {1031 stbsp__int64 i64 = va_arg(va, stbsp__int64);1032 n64 = (stbsp__uint64)i64;1033 if ((f[0] != 'u') && (i64 < 0)) {1034 n64 = (stbsp__uint64)-i64;1035 fl |= STBSP__NEGATIVE;1036 }1037 } else {1038 stbsp__int32 i = va_arg(va, stbsp__int32);1039 n64 = (stbsp__uint32)i;1040 if ((f[0] != 'u') && (i < 0)) {1041 n64 = (stbsp__uint32)-i;1042 fl |= STBSP__NEGATIVE;1043 }1044 }10451046#ifndef STB_SPRINTF_NOFLOAT1047 if (fl & STBSP__METRIC_SUFFIX) {1048 if (n64 < 1024)1049 pr = 0;1050 else if (pr == -1)1051 pr = 1;1052 fv = (double)(stbsp__int64)n64;1053 goto doafloat;1054 }1055#endif10561057 // convert to string1058 s = num + STBSP__NUMSZ;1059 l = 0;10601061 for (;;) {1062 // do in 32-bit chunks (avoid lots of 64-bit divides even with constant denominators)1063 char *o = s - 8;1064 if (n64 >= 100000000) {1065 n = (stbsp__uint32)(n64 % 100000000);1066 n64 /= 100000000;1067 } else {1068 n = (stbsp__uint32)n64;1069 n64 = 0;1070 }1071 if ((fl & STBSP__TRIPLET_COMMA) == 0) {1072 do {1073 s -= 2;Merge branch 'working'Sean Barrettbefore adoption9092c04
line 1074why, one step back1074 *(stbsp__uint16 *)s = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2];Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1075–1301why, one step back1075 n /= 100;1076 } while (n);1077 }1078 while (n) {1079 if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) {1080 l = 0;1081 *--s = stbsp__comma;1082 --o;1083 } else {1084 *--s = (char)(n % 10) + '0';1085 n /= 10;1086 }1087 }1088 if (n64 == 0) {1089 if ((s[0] == '0') && (s != (num + STBSP__NUMSZ)))1090 ++s;1091 break;1092 }1093 while (s != o)1094 if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) {1095 l = 0;1096 *--s = stbsp__comma;1097 --o;1098 } else {1099 *--s = '0';1100 }1101 }11021103 tail[0] = 0;1104 stbsp__lead_sign(fl, lead);11051106 // get the length that we copied1107 l = (stbsp__uint32)((num + STBSP__NUMSZ) - s);1108 if (l == 0) {1109 *--s = '0';1110 l = 1;1111 }1112 cs = l + (3 << 24);1113 if (pr < 0)1114 pr = 0;11151116 scopy:1117 // get fw=leading/trailing space, pr=leading zeros1118 if (pr < (stbsp__int32)l)1119 pr = l;1120 n = pr + lead[0] + tail[0] + tz;1121 if (fw < (stbsp__int32)n)1122 fw = n;1123 fw -= n;1124 pr -= l;11251126 // handle right justify and leading zeros1127 if ((fl & STBSP__LEFTJUST) == 0) {1128 if (fl & STBSP__LEADINGZERO) // if leading zeros, everything is in pr1129 {1130 pr = (fw > pr) ? fw : pr;1131 fw = 0;1132 } else {1133 fl &= ~STBSP__TRIPLET_COMMA; // if no leading zeros, then no commas1134 }1135 }11361137 // copy the spaces and/or zeros1138 if (fw + pr) {1139 stbsp__int32 i;1140 stbsp__uint32 c;11411142 // copy leading spaces (or when doing %8.4d stuff)1143 if ((fl & STBSP__LEFTJUST) == 0)1144 while (fw > 0) {1145 stbsp__cb_buf_clamp(i, fw);1146 fw -= i;1147 while (i) {1148 if ((((stbsp__uintptr)bf) & 3) == 0)1149 break;1150 *bf++ = ' ';1151 --i;1152 }1153 while (i >= 4) {1154 *(stbsp__uint32 *)bf = 0x20202020;1155 bf += 4;1156 i -= 4;1157 }1158 while (i) {1159 *bf++ = ' ';1160 --i;1161 }1162 stbsp__chk_cb_buf(1);1163 }11641165 // copy leader1166 sn = lead + 1;1167 while (lead[0]) {1168 stbsp__cb_buf_clamp(i, lead[0]);1169 lead[0] -= (char)i;1170 while (i) {1171 *bf++ = *sn++;1172 --i;1173 }1174 stbsp__chk_cb_buf(1);1175 }11761177 // copy leading zeros1178 c = cs >> 24;1179 cs &= 0xffffff;1180 cs = (fl & STBSP__TRIPLET_COMMA) ? ((stbsp__uint32)(c - ((pr + cs) % (c + 1)))) : 0;1181 while (pr > 0) {1182 stbsp__cb_buf_clamp(i, pr);1183 pr -= i;1184 if ((fl & STBSP__TRIPLET_COMMA) == 0) {1185 while (i) {1186 if ((((stbsp__uintptr)bf) & 3) == 0)1187 break;1188 *bf++ = '0';1189 --i;1190 }1191 while (i >= 4) {1192 *(stbsp__uint32 *)bf = 0x30303030;1193 bf += 4;1194 i -= 4;1195 }1196 }1197 while (i) {1198 if ((fl & STBSP__TRIPLET_COMMA) && (cs++ == c)) {1199 cs = 0;1200 *bf++ = stbsp__comma;1201 } else1202 *bf++ = '0';1203 --i;1204 }1205 stbsp__chk_cb_buf(1);1206 }1207 }12081209 // copy leader if there is still one1210 sn = lead + 1;1211 while (lead[0]) {1212 stbsp__int32 i;1213 stbsp__cb_buf_clamp(i, lead[0]);1214 lead[0] -= (char)i;1215 while (i) {1216 *bf++ = *sn++;1217 --i;1218 }1219 stbsp__chk_cb_buf(1);1220 }12211222 // copy the string1223 n = l;1224 while (n) {1225 stbsp__int32 i;1226 stbsp__cb_buf_clamp(i, n);1227 n -= i;1228 STBSP__UNALIGNED(while (i >= 4) {1229 *(stbsp__uint32 *)bf = *(stbsp__uint32 *)s;1230 bf += 4;1231 s += 4;1232 i -= 4;1233 })1234 while (i) {1235 *bf++ = *s++;1236 --i;1237 }1238 stbsp__chk_cb_buf(1);1239 }12401241 // copy trailing zeros1242 while (tz) {1243 stbsp__int32 i;1244 stbsp__cb_buf_clamp(i, tz);1245 tz -= i;1246 while (i) {1247 if ((((stbsp__uintptr)bf) & 3) == 0)1248 break;1249 *bf++ = '0';1250 --i;1251 }1252 while (i >= 4) {1253 *(stbsp__uint32 *)bf = 0x30303030;1254 bf += 4;1255 i -= 4;1256 }1257 while (i) {1258 *bf++ = '0';1259 --i;1260 }1261 stbsp__chk_cb_buf(1);1262 }12631264 // copy tail if there is one1265 sn = tail + 1;1266 while (tail[0]) {1267 stbsp__int32 i;1268 stbsp__cb_buf_clamp(i, tail[0]);1269 tail[0] -= (char)i;1270 while (i) {1271 *bf++ = *sn++;1272 --i;1273 }1274 stbsp__chk_cb_buf(1);1275 }12761277 // handle the left justify1278 if (fl & STBSP__LEFTJUST)1279 if (fw > 0) {1280 while (fw) {1281 stbsp__int32 i;1282 stbsp__cb_buf_clamp(i, fw);1283 fw -= i;1284 while (i) {1285 if ((((stbsp__uintptr)bf) & 3) == 0)1286 break;1287 *bf++ = ' ';1288 --i;1289 }1290 while (i >= 4) {1291 *(stbsp__uint32 *)bf = 0x20202020;1292 bf += 4;1293 i -= 4;1294 }1295 while (i--)1296 *bf++ = ' ';1297 stbsp__chk_cb_buf(1);1298 }1299 }1300 break;1301tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1302why, one step back1302 default: // unknown, just copy codeMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1303–1326why, one step back1303 s = num + STBSP__NUMSZ - 1;1304 *s = f[0];1305 l = 1;1306 fw = fl = 0;1307 lead[0] = 0;1308 tail[0] = 0;1309 pr = 0;1310 dp = 0;1311 cs = 0;1312 goto scopy;1313 }1314 ++f;1315 }1316endfmt:13171318 if (!callback)1319 *bf = 0;1320 else1321 stbsp__flush_cb();13221323done:1324 return tlen + (int)(bf - buf);1325}1326tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 1327–1341why, one step back1327// cleanup1328#undef STBSP__LEFTJUST1329#undef STBSP__LEADINGPLUS1330#undef STBSP__LEADINGSPACE1331#undef STBSP__LEADING_0X1332#undef STBSP__LEADINGZERO1333#undef STBSP__INTMAX1334#undef STBSP__TRIPLET_COMMA1335#undef STBSP__NEGATIVE1336#undef STBSP__METRIC_SUFFIX1337#undef STBSP__NUMSZ1338#undef stbsp__chk_cb_bufL1339#undef stbsp__chk_cb_buf1340#undef stbsp__flush_cb1341#undef stbsp__cb_buf_clampMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1342why, one step back1342tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 1343–1344why, one step back1343// ============================================================================1344// wrapper functionsMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1345–1358why, one step back13451346STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...)1347{1348 int result;1349 va_list va;1350 va_start(va, fmt);1351 result = STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va);1352 va_end(va);1353 return result;1354}13551356typedef struct stbsp__context {1357 char *buf;1358 int count;stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1359why, one step back1359 int length;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1360why, one step back1360 char tmp[STB_SPRINTF_MIN];tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1361why, one step back1361} stbsp__context;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1362–1365why, one step back13621363static char *stbsp__clamp_callback(char *buf, void *user, int len)1364{1365 stbsp__context *c = (stbsp__context *)user;stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1366why, one step back1366 c->length += len;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1367–1385why, one step back13671368 if (len > c->count)1369 len = c->count;13701371 if (len) {1372 if (buf != c->buf) {1373 char *s, *d, *se;1374 d = c->buf;1375 s = buf;1376 se = buf + len;1377 do {1378 *d++ = *s++;1379 } while (s < se);1380 }1381 c->buf += len;1382 c->count -= len;1383 }13841385 if (c->count <= 0)stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1386why, one step back1386 return c->tmp;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1387–1389why, one step back1387 return (c->count >= STB_SPRINTF_MIN) ? c->buf : c->tmp; // go direct into buffer if you can1388}1389stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
line 1390why, one step back1390static char * stbsp__count_clamp_callback( char * buf, void * user, int len )Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1391why, one step back1391{stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
lines 1392–1393why, one step back1392 stbsp__context * c = (stbsp__context*)user;1393stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1394why, one step back1394 c->length += len;stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
lines 1395–1399why, one step back1395 return c->tmp; // go direct into buffer if you can1396}13971398STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va )1399{Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1400–1401why, one step back1400 stbsp__context c;1401stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
lines 1402–1403why, one step back1402 if ( (count == 0) && !buf )1403 {stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1404why, one step back1404 c.length = 0;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1405why, one step back1405stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
lines 1406–1409why, one step back1406 STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__count_clamp_callback, &c, c.tmp, fmt, va );1407 }1408 else1409 {stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1410why, one step back1410 int l;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1411why, one step back1411stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
lines 1412–1413why, one step back1412 c.buf = buf;1413 c.count = count;stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1414why, one step back1414 c.length = 0;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1415why, one step back1415stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
lines 1416–1423why, one step back1416 STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__clamp_callback, &c, stbsp__clamp_callback(0,&c,0), fmt, va );14171418 // zero-terminate1419 l = (int)( c.buf - buf );1420 if ( l >= count ) // should never be greater, only equal (or less) than count1421 l = count - 1;1422 buf[l] = 0;1423 }Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1424why, one step back1424stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1425why, one step back1425 return c.length;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1426–1444why, one step back1426}14271428STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...)1429{1430 int result;1431 va_list va;1432 va_start(va, fmt);14331434 result = STB_SPRINTF_DECORATE(vsnprintf)(buf, count, fmt, va);1435 va_end(va);14361437 return result;1438}14391440STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va)1441{1442 return STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va);1443}1444tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 1445–1446why, one step back1445// =======================================================================1446// low level float utility functionsMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1447–1449why, one step back14471448#ifndef STB_SPRINTF_NOFLOAT1449tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1450why, one step back1450// copies d to bits w/ strict aliasing (this compiles to nothing on /Ox)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1451–1457why, one step back1451#define STBSP__COPYFP(dest, src) \1452 { \1453 int cn; \1454 for (cn = 0; cn < 8; cn++) \1455 ((char *)&dest)[cn] = ((char *)&src)[cn]; \1456 }1457tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1458why, one step back1458// get float infoMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1459–1471why, one step back1459static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value)1460{1461 double d;1462 stbsp__int64 b = 0;14631464 // load value and round at the frac_digits1465 d = value;14661467 STBSP__COPYFP(b, d);14681469 *bits = b & ((((stbsp__uint64)1) << 52) - 1);1470 *expo = (stbsp__int32)(((b >> 52) & 2047) - 1023);1471Merge branch 'working'Sean Barrettbefore adoption9092c04
line 1472why, one step back1472 return (stbsp__int32)((stbsp__uint64) b >> 63);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1473–1539why, one step back1473}14741475static double const stbsp__bot[23] = {1476 1e+000, 1e+001, 1e+002, 1e+003, 1e+004, 1e+005, 1e+006, 1e+007, 1e+008, 1e+009, 1e+010, 1e+011,1477 1e+012, 1e+013, 1e+014, 1e+015, 1e+016, 1e+017, 1e+018, 1e+019, 1e+020, 1e+021, 1e+0221478};1479static double const stbsp__negbot[22] = {1480 1e-001, 1e-002, 1e-003, 1e-004, 1e-005, 1e-006, 1e-007, 1e-008, 1e-009, 1e-010, 1e-011,1481 1e-012, 1e-013, 1e-014, 1e-015, 1e-016, 1e-017, 1e-018, 1e-019, 1e-020, 1e-021, 1e-0221482};1483static double const stbsp__negboterr[22] = {1484 -5.551115123125783e-018, -2.0816681711721684e-019, -2.0816681711721686e-020, -4.7921736023859299e-021, -8.1803053914031305e-022, 4.5251888174113741e-023,1485 4.5251888174113739e-024, -2.0922560830128471e-025, -6.2281591457779853e-026, -3.6432197315497743e-027, 6.0503030718060191e-028, 2.0113352370744385e-029,1486 -3.0373745563400371e-030, 1.1806906454401013e-032, -7.7705399876661076e-032, 2.0902213275965398e-033, -7.1542424054621921e-034, -7.1542424054621926e-035,1487 2.4754073164739869e-036, 5.4846728545790429e-037, 9.2462547772103625e-038, -4.8596774326570872e-0391488};1489static double const stbsp__top[13] = {1490 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+2991491};1492static double const stbsp__negtop[13] = {1493 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-2991494};1495static double const stbsp__toperr[13] = {1496 8388608,1497 6.8601809640529717e+028,1498 -7.253143638152921e+052,1499 -4.3377296974619174e+075,1500 -1.5559416129466825e+098,1501 -3.2841562489204913e+121,1502 -3.7745893248228135e+144,1503 -1.7356668416969134e+167,1504 -3.8893577551088374e+190,1505 -9.9566444326005119e+213,1506 6.3641293062232429e+236,1507 -5.2069140800249813e+259,1508 -5.2504760255204387e+2821509};1510static double const stbsp__negtoperr[13] = {1511 3.9565301985100693e-040, -2.299904345391321e-063, 3.6506201437945798e-086, 1.1875228833981544e-109,1512 -5.0644902316928607e-132, -6.7156837247865426e-155, -2.812077463003139e-178, -5.7778912386589953e-201,1513 7.4997100559334532e-224, -4.6439668915134491e-247, -6.3691100762962136e-270, -9.436808465446358e-293,1514 8.0970921678014997e-3171515};15161517#if defined(_MSC_VER) && (_MSC_VER <= 1200)1518static stbsp__uint64 const stbsp__powten[20] = {1519 1,1520 10,1521 100,1522 1000,1523 10000,1524 100000,1525 1000000,1526 10000000,1527 100000000,1528 1000000000,1529 10000000000,1530 100000000000,1531 1000000000000,1532 10000000000000,1533 100000000000000,1534 1000000000000000,1535 10000000000000000,1536 100000000000000000,1537 1000000000000000000,1538 10000000000000000000U1539};tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1540why, one step back1540#define stbsp__tento19th ((stbsp__uint64)1000000000000000000)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1541–1563why, one step back1541#else1542static stbsp__uint64 const stbsp__powten[20] = {1543 1,1544 10,1545 100,1546 1000,1547 10000,1548 100000,1549 1000000,1550 10000000,1551 100000000,1552 1000000000,1553 10000000000ULL,1554 100000000000ULL,1555 1000000000000ULL,1556 10000000000000ULL,1557 100000000000000ULL,1558 1000000000000000ULL,1559 10000000000000000ULL,1560 100000000000000000ULL,1561 1000000000000000000ULL,1562 10000000000000000000ULL1563};tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1564why, one step back1564#define stbsp__tento19th (1000000000000000000ULL)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1565–1668why, one step back1565#endif15661567#define stbsp__ddmulthi(oh, ol, xh, yh) \1568 { \1569 double ahi = 0, alo, bhi = 0, blo; \1570 stbsp__int64 bt; \1571 oh = xh * yh; \1572 STBSP__COPYFP(bt, xh); \1573 bt &= ((~(stbsp__uint64)0) << 27); \1574 STBSP__COPYFP(ahi, bt); \1575 alo = xh - ahi; \1576 STBSP__COPYFP(bt, yh); \1577 bt &= ((~(stbsp__uint64)0) << 27); \1578 STBSP__COPYFP(bhi, bt); \1579 blo = yh - bhi; \1580 ol = ((ahi * bhi - oh) + ahi * blo + alo * bhi) + alo * blo; \1581 }15821583#define stbsp__ddtoS64(ob, xh, xl) \1584 { \1585 double ahi = 0, alo, vh, t; \1586 ob = (stbsp__int64)ph; \1587 vh = (double)ob; \1588 ahi = (xh - vh); \1589 t = (ahi - xh); \1590 alo = (xh - (ahi - t)) - (vh + t); \1591 ob += (stbsp__int64)(ahi + alo + xl); \1592 }15931594#define stbsp__ddrenorm(oh, ol) \1595 { \1596 double s; \1597 s = oh + ol; \1598 ol = ol - (s - oh); \1599 oh = s; \1600 }16011602#define stbsp__ddmultlo(oh, ol, xh, xl, yh, yl) ol = ol + (xh * yl + xl * yh);16031604#define stbsp__ddmultlos(oh, ol, xh, yl) ol = ol + (xh * yl);16051606static void stbsp__raise_to_power10(double *ohi, double *olo, double d, stbsp__int32 power) // power can be -323 to +3501607{1608 double ph, pl;1609 if ((power >= 0) && (power <= 22)) {1610 stbsp__ddmulthi(ph, pl, d, stbsp__bot[power]);1611 } else {1612 stbsp__int32 e, et, eb;1613 double p2h, p2l;16141615 e = power;1616 if (power < 0)1617 e = -e;1618 et = (e * 0x2c9) >> 14; /* %23 */1619 if (et > 13)1620 et = 13;1621 eb = e - (et * 23);16221623 ph = d;1624 pl = 0.0;1625 if (power < 0) {1626 if (eb) {1627 --eb;1628 stbsp__ddmulthi(ph, pl, d, stbsp__negbot[eb]);1629 stbsp__ddmultlos(ph, pl, d, stbsp__negboterr[eb]);1630 }1631 if (et) {1632 stbsp__ddrenorm(ph, pl);1633 --et;1634 stbsp__ddmulthi(p2h, p2l, ph, stbsp__negtop[et]);1635 stbsp__ddmultlo(p2h, p2l, ph, pl, stbsp__negtop[et], stbsp__negtoperr[et]);1636 ph = p2h;1637 pl = p2l;1638 }1639 } else {1640 if (eb) {1641 e = eb;1642 if (eb > 22)1643 eb = 22;1644 e -= eb;1645 stbsp__ddmulthi(ph, pl, d, stbsp__bot[eb]);1646 if (e) {1647 stbsp__ddrenorm(ph, pl);1648 stbsp__ddmulthi(p2h, p2l, ph, stbsp__bot[e]);1649 stbsp__ddmultlos(p2h, p2l, stbsp__bot[e], pl);1650 ph = p2h;1651 pl = p2l;1652 }1653 }1654 if (et) {1655 stbsp__ddrenorm(ph, pl);1656 --et;1657 stbsp__ddmulthi(p2h, p2l, ph, stbsp__top[et]);1658 stbsp__ddmultlo(p2h, p2l, ph, pl, stbsp__top[et], stbsp__toperr[et]);1659 ph = p2h;1660 pl = p2l;1661 }1662 }1663 }1664 stbsp__ddrenorm(ph, pl);1665 *ohi = ph;1666 *olo = pl;1667}1668tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 1669–1672why, one step back1669// given a float value, returns the significant bits in bits, and the position of the1670// decimal point in decimal_pos. +/-INF and NAN are specified by special values1671// returned in the decimal_pos parameter.1672// frac_digits is absolute normally, but if you want from first significant digits (got %g and %e), or in 0x80000000Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1673–1681why, one step back1673static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits)1674{1675 double d;1676 stbsp__int64 bits = 0;1677 stbsp__int32 expo, e, ng, tens;16781679 d = value;1680 STBSP__COPYFP(bits, d);1681 expo = (stbsp__int32)((bits >> 52) & 2047);Merge branch 'working'Sean Barrettbefore adoption9092c04
line 1682why, one step back1682 ng = (stbsp__int32)((stbsp__uint64) bits >> 63);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1683–1690why, one step back1683 if (ng)1684 d = -d;16851686 if (expo == 2047) // is nan or inf?1687 {1688 *start = (bits & ((((stbsp__uint64)1) << 52) - 1)) ? "NaN" : "Inf";1689 *decimal_pos = STBSP__SPECIAL;1690 *len = 3;tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1691why, one step back1691 return ng;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1692–1791why, one step back1692 }16931694 if (expo == 0) // is zero or denormal1695 {1696 if ((bits << 1) == 0) // do zero1697 {1698 *decimal_pos = 1;1699 *start = out;1700 out[0] = '0';1701 *len = 1;1702 return ng;1703 }1704 // find the right expo for denormals1705 {1706 stbsp__int64 v = ((stbsp__uint64)1) << 51;1707 while ((bits & v) == 0) {1708 --expo;1709 v >>= 1;1710 }1711 }1712 }17131714 // find the decimal exponent as well as the decimal bits of the value1715 {1716 double ph, pl;17171718 // log10 estimate - very specifically tweaked to hit or undershoot by no more than 1 of log10 of all expos 1..20461719 tens = expo - 1023;1720 tens = (tens < 0) ? ((tens * 617) / 2048) : (((tens * 1233) / 4096) + 1);17211722 // move the significant bits into position and stick them into an int1723 stbsp__raise_to_power10(&ph, &pl, d, 18 - tens);17241725 // get full as much precision from double-double as possible1726 stbsp__ddtoS64(bits, ph, pl);17271728 // check if we undershot1729 if (((stbsp__uint64)bits) >= stbsp__tento19th)1730 ++tens;1731 }17321733 // now do the rounding in integer land1734 frac_digits = (frac_digits & 0x80000000) ? ((frac_digits & 0x7ffffff) + 1) : (tens + frac_digits);1735 if ((frac_digits < 24)) {1736 stbsp__uint32 dg = 1;1737 if ((stbsp__uint64)bits >= stbsp__powten[9])1738 dg = 10;1739 while ((stbsp__uint64)bits >= stbsp__powten[dg]) {1740 ++dg;1741 if (dg == 20)1742 goto noround;1743 }1744 if (frac_digits < dg) {1745 stbsp__uint64 r;1746 // add 0.5 at the right position and round1747 e = dg - frac_digits;1748 if ((stbsp__uint32)e >= 24)1749 goto noround;1750 r = stbsp__powten[e];1751 bits = bits + (r / 2);1752 if ((stbsp__uint64)bits >= stbsp__powten[dg])1753 ++tens;1754 bits /= r;1755 }1756 noround:;1757 }17581759 // kill long trailing runs of zeros1760 if (bits) {1761 stbsp__uint32 n;1762 for (;;) {1763 if (bits <= 0xffffffff)1764 break;1765 if (bits % 1000)1766 goto donez;1767 bits /= 1000;1768 }1769 n = (stbsp__uint32)bits;1770 while ((n % 1000) == 0)1771 n /= 1000;1772 bits = n;1773 donez:;1774 }17751776 // convert to string1777 out += 64;1778 e = 0;1779 for (;;) {1780 stbsp__uint32 n;1781 char *o = out - 8;1782 // do the conversion in chunks of U32s (avoid most 64-bit divides, worth it, constant denomiators be damned)1783 if (bits >= 100000000) {1784 n = (stbsp__uint32)(bits % 100000000);1785 bits /= 100000000;1786 } else {1787 n = (stbsp__uint32)bits;1788 bits = 0;1789 }1790 while (n) {1791 out -= 2;Merge branch 'working'Sean Barrettbefore adoption9092c04
line 1792why, one step back1792 *(stbsp__uint16 *)out = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2];Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1793–1814why, one step back1793 n /= 100;1794 e += 2;1795 }1796 if (bits == 0) {1797 if ((e) && (out[0] == '0')) {1798 ++out;1799 --e;1800 }1801 break;1802 }1803 while (out != o) {1804 *--out = '0';1805 ++e;1806 }1807 }18081809 *decimal_pos = tens;1810 *start = out;1811 *len = e;1812 return ng;1813}1814tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 1815–1818why, one step back1815#undef stbsp__ddmulthi1816#undef stbsp__ddrenorm1817#undef stbsp__ddmultlo1818#undef stbsp__ddmultlosMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1819why, one step back1819#undef STBSP__SPECIALtweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1820why, one step back1820#undef STBSP__COPYFPMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1821why, one step back1821tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1822why, one step back1822#endif // STB_SPRINTF_NOFLOATMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1823why, one step back1823tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 1824–1825why, one step back1824// clean up1825#undef stbsp__uint16Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1826–1827why, one step back1826#undef stbsp__uint321827#undef stbsp__int32tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 1828–1830why, one step back1828#undef stbsp__uint641829#undef stbsp__int641830#undef STBSP__UNALIGNEDMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1831why, one step back1831tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1832why, one step back1832#endif // STB_SPRINTF_IMPLEMENTATIONMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1833–1835why, one step back18331834/*1835------------------------------------------------------------------------------tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1836why, one step back1836This software is available under 2 licenses -- choose whichever you prefer.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1837why, one step back1837------------------------------------------------------------------------------tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 1838–1839why, one step back1838ALTERNATIVE A - MIT License1839Copyright (c) 2017 Sean BarrettMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1840–1844why, one step back1840Permission is hereby granted, free of charge, to any person obtaining a copy of1841this software and associated documentation files (the "Software"), to deal in1842the Software without restriction, including without limitation the rights to1843use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies1844of the Software, and to permit persons to whom the Software is furnished to dotweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1845why, one step back1845so, subject to the following conditions:Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1846why, one step back1846The above copyright notice and this permission notice shall be included in alltweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1847why, one step back1847copies or substantial portions of the Software.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1848–1853why, one step back1848THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR1849IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,1850FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE1851AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER1852LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,1853OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THEtweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1854why, one step back1854SOFTWARE.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1855why, one step back1855------------------------------------------------------------------------------tweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 1856–1857why, one step back1856ALTERNATIVE B - Public Domain (www.unlicense.org)1857This is free and unencumbered software released into the public domain.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1858–1859why, one step back1858Anyone is free to copy, modify, publish, use, compile, sell, or distribute this1859software, either in source code form or as a compiled binary, for any purpose,tweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1860why, one step back1860commercial or non-commercial, and by any means.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1861–1865why, one step back1861In jurisdictions that recognize copyright laws, the author or authors of this1862software dedicate any and all copyright interest in the software to the public1863domain. We make this dedication for the benefit of the public at large and to1864the detriment of our heirs and successors. We intend this dedication to be an1865overt act of relinquishment in perpetuity of all present and future rights totweak previous PR mergeSean Barrettbefore adoptione895f6d
line 1866why, one step back1866this software under copyright law.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1867–1871why, one step back1867THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR1868IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,1869FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE1870AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN1871ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTIONtweak previous PR mergeSean Barrettbefore adoptione895f6d
lines 1872–1874why, one step back1872WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.1873------------------------------------------------------------------------------1874*/