stb_sprintf.h
This file
Path
stb_sprintf.h
1,879 lines · 56.0 KiB · C
Last changed by
update version numbersSean Barrettbefore adoption37b9b20
History of this fileTake it
The walk stopped after 200 commits: lines older than f1417ef point at it.
update version numbersSean Barrettbefore adoption37b9b20
line 1why, one step back1// stb_sprintf - v1.08 - public domain snprintf() implementationupdate READMESean Barrettbefore adoptionf1417ef
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//update READMESean Barrettbefore adoptionf1417ef
line 5why, one step back5// allowed types: sc uidBboXx p AaGgEef nMerge branch 'working' into work2Sean Barrettbefore adoptionca1b581
line 6why, one step back6// lengths : hh 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//update READMESean Barrettbefore adoptionf1417ef
lines 11–13why, one step back11// Contributors (bugfixes):12// github:d2643513// github:trex78stb_sprintf: creditsSean Barrettbefore adoptionf11f1e8
line 14why, one step back14// github:account-loginupdate READMESean Barrettbefore adoptionf1417ef
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 'fix806' of https://github.com/kolbma/stb into work2Sean Barrettbefore adoptionacd1c66
line 22why, one step back22// Markus KolbMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 23why, one step back23//update READMESean Barrettbefore adoptionf1417ef
line 24why, one step back24// LICENSE:Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 25why, one step back25//update READMESean Barrettbefore adoptionf1417ef
line 26why, one step back26// See end of file for license information.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 27why, one step back27update READMESean Barrettbefore adoptionf1417ef
lines 28–29why, one step back28#ifndef STB_SPRINTF_H_INCLUDE29#define STB_SPRINTF_H_INCLUDEMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 30–31why, one step back3031/*update READMESean Barrettbefore adoptionf1417ef
line 32why, one step back32Single file sprintf replacement.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 33–34why, one step back3334Originally written by Jeff Roberts at RAD Game Tools - 2015/10/20.update READMESean Barrettbefore adoptionf1417ef
line 35why, one step back35Hereby placed in public domain.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 36why, one step back36update READMESean Barrettbefore adoptionf1417ef
lines 37–39why, one step back37This is a full sprintf replacement that supports everything that38the C runtime sprintfs support, including float/double, 64-bit integers,39hex floats, field parameters (%*.*d stuff), length reads backs, etc.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 40why, one step back40update READMESean Barrettbefore adoptionf1417ef
lines 41–42why, one step back41Why would you need this if sprintf already exists? Well, first off,42it's *much* faster (see below). It's also much smaller than the CRTMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 43why, one step back43versions code-space-wise. We've also added some simple improvementsupdate READMESean Barrettbefore adoptionf1417ef
line 44why, one step back44that are super handy (commas in thousands, callbacks at buffer full,Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 45–46why, one step back45for example). Finally, the format strings for MSVC and GCC differ46for 64-bit integers (among other small things), so this lets you useupdate READMESean Barrettbefore adoptionf1417ef
line 47why, one step back47the same format strings in cross platform code.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 48why, one step back48update READMESean Barrettbefore adoptionf1417ef
line 49why, one step back49It uses the standard single file trick of being both the header fileMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 50why, one step back50and the source itself. If you just include it normally, you just getupdate READMESean Barrettbefore adoptionf1417ef
lines 51–52why, one step back51the header file function definitions. To get the code, you include52it from a C or C++ file and define STB_SPRINTF_IMPLEMENTATION first.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 53why, one step back53update READMESean Barrettbefore adoptionf1417ef
line 54why, one step back54It only uses va_args macros from the C runtime to do it's work. ItMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 55why, one step back55does cast doubles to S64s and shifts and divides U64s, which doesupdate READMESean Barrettbefore adoptionf1417ef
line 56why, one step back56drag in CRT code on most platforms.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 57why, one step back57update READMESean Barrettbefore adoptionf1417ef
lines 58–60why, one step back58It compiles to roughly 8K with float support, and 4K without.59As a comparison, when using MSVC static libs, calling sprintf drags60in 16K.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 61why, one step back61update READMESean Barrettbefore adoptionf1417ef
lines 62–66why, one step back62API:63====64int stbsp_sprintf( char * buf, char const * fmt, ... )65int stbsp_snprintf( char * buf, int count, char const * fmt, ... )66 Convert an arg list into a buffer. stbsp_snprintf always returnsMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 67–68why, one step back67 a zero-terminated string (unlike regular snprintf).68update READMESean Barrettbefore adoptionf1417ef
lines 69–71why, one step back69int stbsp_vsprintf( char * buf, char const * fmt, va_list va )70int stbsp_vsnprintf( char * buf, int count, char const * fmt, va_list va )71 Convert a va_list arg list into a buffer. stbsp_vsnprintf always returnsMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 72–73why, one step back72 a zero-terminated string (unlike regular snprintf).73update READMESean Barrettbefore adoptionf1417ef
lines 74–80why, one step back74int stbsp_vsprintfcb( STBSP_SPRINTFCB * callback, void * user, char * buf, char const * fmt, va_list va )75 typedef char * STBSP_SPRINTFCB( char const * buf, void * user, int len );76 Convert into a buffer, calling back every STB_SPRINTF_MIN chars.77 Your callback can then copy the chars out, print them or whatever.78 This function is actually the workhorse for everything else.79 The buffer you pass in must hold at least STB_SPRINTF_MIN characters.80 // you return the next buffer to use or 0 to stop convertingMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 81why, one step back81update READMESean Barrettbefore adoptionf1417ef
lines 82–83why, one step back82void stbsp_set_separators( char comma, char period )83 Set the comma and period characters to use.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 84why, one step back84update READMESean Barrettbefore adoptionf1417ef
lines 85–90why, one step back85FLOATS/DOUBLES:86===============87This code uses a internal float->ascii conversion method that uses88doubles with error correction (double-doubles, for ~105 bits of89precision). This conversion is round-trip perfect - that is, an atof90of the values output here will give you the bit-exact double back.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 91–94why, one step back9192One difference is that our insignificant digits will be different than93with MSVC or GCC (but they don't match each other either). We also94don't attempt to find the minimum length matching float (pre-MSVC15update READMESean Barrettbefore adoptionf1417ef
line 95why, one step back95doesn't either).Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 96why, one step back96update READMESean Barrettbefore adoptionf1417ef
lines 97–98why, one step back97If you don't need float or doubles at all, define STB_SPRINTF_NOFLOAT98and you'll save 4K of code space.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 99why, one step back99update READMESean Barrettbefore adoptionf1417ef
lines 100–104why, one step back10064-BIT INTS:101============102This library also supports 64-bit integers and you can use MSVC style or103GCC style indicators (%I64d or %lld). It supports the C99 specifiers104for size_t and ptr_diff_t (%jd %zd) as well.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 105why, one step back105update READMESean Barrettbefore adoptionf1417ef
lines 106–108why, one step back106EXTRAS:107=======108Like some GCCs, for integers and floats, you can use a ' (single quote)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 109why, one step back109specifier and commas will be inserted on the thousands: "%'d" on 12345update READMESean Barrettbefore adoptionf1417ef
line 110why, one step back110would print 12,345.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 111–112why, one step back111112For integers and floats, you can use a "$" specifier and the numberupdate READMESean Barrettbefore adoptionf1417ef
line 113why, one step back113will be converted to float and then divided to get kilo, mega, giga orMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 114–115why, one step back114tera and then printed, so "%$d" 1000 is "1.0 k", "%$.2d" 2536000 is115"2.53 M", etc. For byte values, use two $:s, like "%$$d" to turnupdate READMESean Barrettbefore adoptionf1417ef
lines 116–118why, one step back1162536000 to "2.42 Mi". If you prefer JEDEC suffixes to SI ones, use three117$:s: "%$$$d" -> "2.42 M". To remove the space between the number and the118suffix, add "_" specifier: "%_$d" -> "2.53M".Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 119–120why, one step back119120In addition to octal and hexadecimal conversions, you can printupdate READMESean Barrettbefore adoptionf1417ef
line 121why, one step back121integers in binary: "%b" for 256 would print 100.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 122why, one step back122update READMESean Barrettbefore adoptionf1417ef
lines 123–142why, one step back123PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC):124===================================================================125"%d" across all 32-bit ints (4.8x/4.0x faster than 32-/64-bit MSVC)126"%24d" across all 32-bit ints (4.5x/4.2x faster)127"%x" across all 32-bit ints (4.5x/3.8x faster)128"%08x" across all 32-bit ints (4.3x/3.8x faster)129"%f" across e-10 to e+10 floats (7.3x/6.0x faster)130"%e" across e-10 to e+10 floats (8.1x/6.0x faster)131"%g" across e-10 to e+10 floats (10.0x/7.1x faster)132"%f" for values near e-300 (7.9x/6.5x faster)133"%f" for values near e+300 (10.0x/9.1x faster)134"%e" for values near e-300 (10.1x/7.0x faster)135"%e" for values near e+300 (9.2x/6.0x faster)136"%.320f" for values near e-300 (12.6x/11.2x faster)137"%a" for random values (8.6x/4.3x faster)138"%I64d" for 64-bits with 32-bit values (4.8x/3.4x faster)139"%I64d" for 64-bits > 32-bit values (4.9x/5.5x faster)140"%s%s%s" for 64 char strings (7.1x/7.3x faster)141"...512 char string..." ( 35.0x/32.5x faster!)142*/Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 143why, one step back143stb_sprintf: redo ASAN fixes after problematic mergeSean Barrettbefore adoption3f5db89
lines 144–152why, one step back144#if defined(__clang__)145 #if defined(__has_feature) && defined(__has_attribute)146 #if __has_feature(address_sanitizer)147 #if __has_attribute(__no_sanitize__)148 #define STBSP__ASAN __attribute__((__no_sanitize__("address")))149 #elif __has_attribute(__no_sanitize_address__)150 #define STBSP__ASAN __attribute__((__no_sanitize_address__))151 #elif __has_attribute(__no_address_safety_analysis__)152 #define STBSP__ASAN __attribute__((__no_address_safety_analysis__))Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 153why, one step back153 #endifstb_sprintf: redo ASAN fixes after problematic mergeSean Barrettbefore adoption3f5db89
lines 154–159why, one step back154 #endif155 #endif156#elif __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)157 #if __SANITIZE_ADDRESS__158 #define STBSP__ASAN __attribute__((__no_sanitize_address__))159 #endifMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 160why, one step back160#endifstb_sprintf: redo ASAN fixes after problematic mergeSean Barrettbefore adoption3f5db89
lines 161–163why, one step back161162#ifndef STBSP__ASAN163#define STBSP__ASANMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 164–165why, one step back164#endif165update READMESean Barrettbefore adoptionf1417ef
lines 166–167why, one step back166#ifdef STB_SPRINTF_STATIC167#define STBSP__PUBLICDEC staticstb_sprintf: redo ASAN fixes after problematic mergeSean Barrettbefore adoption3f5db89
line 168why, one step back168#define STBSP__PUBLICDEF static STBSP__ASANMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 169why, one step back169#elseupdate READMESean Barrettbefore adoptionf1417ef
lines 170–171why, one step back170#ifdef __cplusplus171#define STBSP__PUBLICDEC extern "C"stb_sprintf: redo ASAN fixes after problematic mergeSean Barrettbefore adoption3f5db89
line 172why, one step back172#define STBSP__PUBLICDEF extern "C" STBSP__ASANMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 173why, one step back173#elseupdate READMESean Barrettbefore adoptionf1417ef
line 174why, one step back174#define STBSP__PUBLICDEC externstb_sprintf: redo ASAN fixes after problematic mergeSean Barrettbefore adoption3f5db89
line 175why, one step back175#define STBSP__PUBLICDEF STBSP__ASANMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 176–179why, one step back176#endif177#endif178179#include <stdarg.h> // for va_list()stb_sprintf: fix 32 vs 64-bit sizes for format widts j/z/tSean Barrettbefore adoption619cdb6
line 180why, one step back180#include <stddef.h> // size_t, ptrdiff_tMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 181why, one step back181update READMESean Barrettbefore adoptionf1417ef
lines 182–183why, one step back182#ifndef STB_SPRINTF_MIN183#define STB_SPRINTF_MIN 512 // how many characters per callbackMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 184why, one step back184#endifsprintf warningsSean Barrettbefore adoptionf06f586
line 185why, one step back185typedef char *STBSP_SPRINTFCB(const char *buf, void *user, int len);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 186why, one step back186update READMESean Barrettbefore adoptionf1417ef
line 187why, one step back187#ifndef STB_SPRINTF_DECORATEMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 188–198why, one step back188#define STB_SPRINTF_DECORATE(name) stbsp_##name // define this before including if you want to change the names189#endif190191STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va);192STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsnprintf)(char *buf, int count, char const *fmt, va_list va);193STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...);194STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...);195196STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va);197STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char comma, char period);198update READMESean Barrettbefore adoptionf1417ef
line 199why, one step back199#endif // STB_SPRINTF_H_INCLUDEMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 200why, one step back200update READMESean Barrettbefore adoptionf1417ef
line 201why, one step back201#ifdef STB_SPRINTF_IMPLEMENTATIONMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 202–204why, one step back202203#include <stdlib.h> // for va_arg()204update READMESean Barrettbefore adoptionf1417ef
lines 205–206why, one step back205#define stbsp__uint32 unsigned int206#define stbsp__int32 signed intMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 207why, one step back207update READMESean Barrettbefore adoptionf1417ef
lines 208–210why, one step back208#ifdef _MSC_VER209#define stbsp__uint64 unsigned __int64210#define stbsp__int64 signed __int64Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 211why, one step back211#elseupdate READMESean Barrettbefore adoptionf1417ef
lines 212–213why, one step back212#define stbsp__uint64 unsigned long long213#define stbsp__int64 signed long longMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 214why, one step back214#endifupdate READMESean Barrettbefore adoptionf1417ef
line 215why, one step back215#define stbsp__uint16 unsigned shortMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 216–217why, one step back216217#ifndef stbsp__uintptrMerge branch 'fix806' of https://github.com/kolbma/stb into work2Sean Barrettbefore adoptionacd1c66
line 218why, one step back218#if defined(__ppc64__) || defined(__powerpc64__) || defined(__aarch64__) || defined(_M_X64) || defined(__x86_64__) || defined(__x86_64)update READMESean Barrettbefore adoptionf1417ef
line 219why, one step back219#define stbsp__uintptr stbsp__uint64Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 220why, one step back220#elseupdate READMESean Barrettbefore adoptionf1417ef
line 221why, one step back221#define stbsp__uintptr stbsp__uint32Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 222–226why, one step back222#endif223#endif224225#ifndef STB_SPRINTF_MSVC_MODE // used for MSVC2013 and earlier (MSVC2015 matches GCC)226#if defined(_MSC_VER) && (_MSC_VER < 1900)update READMESean Barrettbefore adoptionf1417ef
line 227why, one step back227#define STB_SPRINTF_MSVC_MODEMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 228–231why, one step back228#endif229#endif230231#ifdef STB_SPRINTF_NOUNALIGNED // define this before inclusion to force stbsp_sprintf to always use aligned accessesupdate READMESean Barrettbefore adoptionf1417ef
line 232why, one step back232#define STBSP__UNALIGNED(code)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 233why, one step back233#elseupdate READMESean Barrettbefore adoptionf1417ef
line 234why, one step back234#define STBSP__UNALIGNED(code) codeMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 235–237why, one step back235#endif236237#ifndef STB_SPRINTF_NOFLOATupdate READMESean Barrettbefore adoptionf1417ef
line 238why, one step back238// internal float utility functionsMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 239–240why, one step back239static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits);240static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value);update READMESean Barrettbefore adoptionf1417ef
line 241why, one step back241#define STBSP__SPECIAL 0x7000Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 242–245why, one step back242#endif243244static char stbsp__period = '.';245static char stbsp__comma = ',';Merge branch 'working'Sean Barrettbefore adoption9092c04
lines 246–252why, one step back246static struct247{248 short temp; // force next field to be 2-byte aligned249 char pair[201];250} stbsp__digitpair =251{252 0,stb_sprintf: minor reformattingSean Barrettbefore adoption9f1ef73
lines 253–255why, one step back253 "00010203040506070809101112131415161718192021222324"254 "25262728293031323334353637383940414243444546474849"255 "50515253545556575859606162636465666768697071727374"Merge branch 'working'Sean Barrettbefore adoption9092c04
lines 256–257why, one step back256 "75767778798081828384858687888990919293949596979899"257};Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 258–264why, one step back258259STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char pcomma, char pperiod)260{261 stbsp__period = pperiod;262 stbsp__comma = pcomma;263}264update READMESean Barrettbefore adoptionf1417ef
lines 265–277why, one step back265#define STBSP__LEFTJUST 1266#define STBSP__LEADINGPLUS 2267#define STBSP__LEADINGSPACE 4268#define STBSP__LEADING_0X 8269#define STBSP__LEADINGZERO 16270#define STBSP__INTMAX 32271#define STBSP__TRIPLET_COMMA 64272#define STBSP__NEGATIVE 128273#define STBSP__METRIC_SUFFIX 256274#define STBSP__HALFWIDTH 512275#define STBSP__METRIC_NOSPACE 1024276#define STBSP__METRIC_1024 2048277#define STBSP__METRIC_JEDEC 4096Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 278why, one step back278update READMESean Barrettbefore adoptionf1417ef
line 279why, one step back279static void stbsp__lead_sign(stbsp__uint32 fl, char *sign)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 280–362why, one step back280{281 sign[0] = 0;282 if (fl & STBSP__NEGATIVE) {283 sign[0] = 1;284 sign[1] = '-';285 } else if (fl & STBSP__LEADINGSPACE) {286 sign[0] = 1;287 sign[1] = ' ';288 } else if (fl & STBSP__LEADINGPLUS) {289 sign[0] = 1;290 sign[1] = '+';291 }292}293294STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va)295{296 static char hex[] = "0123456789abcdefxp";297 static char hexu[] = "0123456789ABCDEFXP";298 char *bf;299 char const *f;300 int tlen = 0;301302 bf = buf;303 f = fmt;304 for (;;) {305 stbsp__int32 fw, pr, tz;306 stbsp__uint32 fl;307308 // macros for the callback buffer stuff309 #define stbsp__chk_cb_bufL(bytes) \310 { \311 int len = (int)(bf - buf); \312 if ((len + (bytes)) >= STB_SPRINTF_MIN) { \313 tlen += len; \314 if (0 == (bf = buf = callback(buf, user, len))) \315 goto done; \316 } \317 }318 #define stbsp__chk_cb_buf(bytes) \319 { \320 if (callback) { \321 stbsp__chk_cb_bufL(bytes); \322 } \323 }324 #define stbsp__flush_cb() \325 { \326 stbsp__chk_cb_bufL(STB_SPRINTF_MIN - 1); \327 } // flush if there is even one byte in the buffer328 #define stbsp__cb_buf_clamp(cl, v) \329 cl = v; \330 if (callback) { \331 int lg = STB_SPRINTF_MIN - (int)(bf - buf); \332 if (cl > lg) \333 cl = lg; \334 }335336 // fast copy everything up to the next % (or end of string)337 for (;;) {338 while (((stbsp__uintptr)f) & 3) {339 schk1:340 if (f[0] == '%')341 goto scandd;342 schk2:343 if (f[0] == 0)344 goto endfmt;345 stbsp__chk_cb_buf(1);346 *bf++ = f[0];347 ++f;348 }349 for (;;) {350 // Check if the next 4 bytes contain %(0x25) or end of string.351 // Using the 'hasless' trick:352 // https://graphics.stanford.edu/~seander/bithacks.html#HasLessInWord353 stbsp__uint32 v, c;354 v = *(stbsp__uint32 *)f;355 c = (~v) & 0x80808080;356 if (((v ^ 0x25252525) - 0x01010101) & c)357 goto schk1;358 if ((v - 0x01010101) & c)359 goto schk2;360 if (callback)361 if ((STB_SPRINTF_MIN - (int)(bf - buf)) < 4)362 goto schk1;Merge branch 'fix-unaligned' of https://github.com/stefano-zanotti-88/stb into workingSean Barrettbefore adoption6e7e5c5
lines 363–373why, one step back363 #ifdef STB_SPRINTF_NOUNALIGNED364 if(((stbsp__uintptr)bf) & 3) {365 bf[0] = f[0];366 bf[1] = f[1];367 bf[2] = f[2];368 bf[3] = f[3];369 } else370 #endif371 {372 *(stbsp__uint32 *)bf = v;373 }Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 374–470why, one step back374 bf += 4;375 f += 4;376 }377 }378 scandd:379380 ++f;381382 // ok, we have a percent, read the modifiers first383 fw = 0;384 pr = -1;385 fl = 0;386 tz = 0;387388 // flags389 for (;;) {390 switch (f[0]) {391 // if we have left justify392 case '-':393 fl |= STBSP__LEFTJUST;394 ++f;395 continue;396 // if we have leading plus397 case '+':398 fl |= STBSP__LEADINGPLUS;399 ++f;400 continue;401 // if we have leading space402 case ' ':403 fl |= STBSP__LEADINGSPACE;404 ++f;405 continue;406 // if we have leading 0x407 case '#':408 fl |= STBSP__LEADING_0X;409 ++f;410 continue;411 // if we have thousand commas412 case '\'':413 fl |= STBSP__TRIPLET_COMMA;414 ++f;415 continue;416 // if we have kilo marker (none->kilo->kibi->jedec)417 case '$':418 if (fl & STBSP__METRIC_SUFFIX) {419 if (fl & STBSP__METRIC_1024) {420 fl |= STBSP__METRIC_JEDEC;421 } else {422 fl |= STBSP__METRIC_1024;423 }424 } else {425 fl |= STBSP__METRIC_SUFFIX;426 }427 ++f;428 continue;429 // if we don't want space between metric suffix and number430 case '_':431 fl |= STBSP__METRIC_NOSPACE;432 ++f;433 continue;434 // if we have leading zero435 case '0':436 fl |= STBSP__LEADINGZERO;437 ++f;438 goto flags_done;439 default: goto flags_done;440 }441 }442 flags_done:443444 // get the field width445 if (f[0] == '*') {446 fw = va_arg(va, stbsp__uint32);447 ++f;448 } else {449 while ((f[0] >= '0') && (f[0] <= '9')) {450 fw = fw * 10 + f[0] - '0';451 f++;452 }453 }454 // get the precision455 if (f[0] == '.') {456 ++f;457 if (f[0] == '*') {458 pr = va_arg(va, stbsp__uint32);459 ++f;460 } else {461 pr = 0;462 while ((f[0] >= '0') && (f[0] <= '9')) {463 pr = pr * 10 + f[0] - '0';464 f++;465 }466 }467 }468469 // handle integer size overrides470 switch (f[0]) {update READMESean Barrettbefore adoptionf1417ef
line 471why, one step back471 // are we halfwidth?Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 472–474why, one step back472 case 'h':473 fl |= STBSP__HALFWIDTH;474 ++f;Merge branch 'working' into work2Sean Barrettbefore adoptionca1b581
lines 475–476why, one step back475 if (f[0] == 'h')476 ++f; // QUARTERWIDTHMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 477why, one step back477 break;update READMESean Barrettbefore adoptionf1417ef
line 478why, one step back478 // are we 64-bit (unix style)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 479why, one step back479 case 'l':"long int" => "long"Sean Barrettbefore adoptiona23f466
line 480why, one step back480 fl |= ((sizeof(long) == 8) ? STBSP__INTMAX : 0);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 481–486why, one step back481 ++f;482 if (f[0] == 'l') {483 fl |= STBSP__INTMAX;484 ++f;485 }486 break;update READMESean Barrettbefore adoptionf1417ef
line 487why, one step back487 // are we 64-bit on intmax? (c99)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 488why, one step back488 case 'j':stb_sprintf: fix 32 vs 64-bit sizes for format widts j/z/tSean Barrettbefore adoption619cdb6
line 489why, one step back489 fl |= (sizeof(size_t) == 8) ? STBSP__INTMAX : 0;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 490–491why, one step back490 ++f;491 break;update READMESean Barrettbefore adoptionf1417ef
line 492why, one step back492 // are we 64-bit on size_t or ptrdiff_t? (c99)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 493why, one step back493 case 'z':stb_sprintf: fix 32 vs 64-bit sizes for format widts j/z/tSean Barrettbefore adoption619cdb6
lines 494–496why, one step back494 fl |= (sizeof(ptrdiff_t) == 8) ? STBSP__INTMAX : 0;495 ++f;496 break;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 497why, one step back497 case 't':stb_sprintf: fix 32 vs 64-bit sizes for format widts j/z/tSean Barrettbefore adoption619cdb6
line 498why, one step back498 fl |= (sizeof(ptrdiff_t) == 8) ? STBSP__INTMAX : 0;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 499–500why, one step back499 ++f;500 break;update READMESean Barrettbefore adoptionf1417ef
line 501why, one step back501 // are we 64-bit (msft style)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 502–512why, one step back502 case 'I':503 if ((f[1] == '6') && (f[2] == '4')) {504 fl |= STBSP__INTMAX;505 f += 3;506 } else if ((f[1] == '3') && (f[2] == '2')) {507 f += 3;508 } else {509 fl |= ((sizeof(void *) == 8) ? STBSP__INTMAX : 0);510 ++f;511 }512 break;update READMESean Barrettbefore adoptionf1417ef
line 513why, one step back513 default: break;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 514–531why, one step back514 }515516 // handle each replacement517 switch (f[0]) {518 #define STBSP__NUMSZ 512 // big enough for e308 (with commas) or e-307519 char num[STBSP__NUMSZ];520 char lead[8];521 char tail[8];522 char *s;523 char const *h;524 stbsp__uint32 l, n, cs;525 stbsp__uint64 n64;526#ifndef STB_SPRINTF_NOFLOAT527 double fv;528#endif529 stbsp__int32 dp;530 char const *sn;531update READMESean Barrettbefore adoptionf1417ef
line 532why, one step back532 case 's':Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 533–541why, one step back533 // get the string534 s = va_arg(va, char *);535 if (s == 0)536 s = (char *)"null";537 // get the length538 sn = s;539 for (;;) {540 if ((((stbsp__uintptr)sn) & 3) == 0)541 break;update READMESean Barrettbefore adoptionf1417ef
line 542why, one step back542 lchk:Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 543–575why, one step back543 if (sn[0] == 0)544 goto ld;545 ++sn;546 }547 n = 0xffffffff;548 if (pr >= 0) {549 n = (stbsp__uint32)(sn - s);550 if (n >= (stbsp__uint32)pr)551 goto ld;552 n = ((stbsp__uint32)(pr - n)) >> 2;553 }554 while (n) {555 stbsp__uint32 v = *(stbsp__uint32 *)sn;556 if ((v - 0x01010101) & (~v) & 0x80808080UL)557 goto lchk;558 sn += 4;559 --n;560 }561 goto lchk;562 ld:563564 l = (stbsp__uint32)(sn - s);565 // clamp to precision566 if (l > (stbsp__uint32)pr)567 l = pr;568 lead[0] = 0;569 tail[0] = 0;570 pr = 0;571 dp = 0;572 cs = 0;573 // copy the string in574 goto scopy;575update READMESean Barrettbefore adoptionf1417ef
line 576why, one step back576 case 'c': // charMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 577–587why, one step back577 // get the character578 s = num + STBSP__NUMSZ - 1;579 *s = (char)va_arg(va, int);580 l = 1;581 lead[0] = 0;582 tail[0] = 0;583 pr = 0;584 dp = 0;585 cs = 0;586 goto scopy;587update READMESean Barrettbefore adoptionf1417ef
line 588why, one step back588 case 'n': // weird write-bytes specifierMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 589–593why, one step back589 {590 int *d = va_arg(va, int *);591 *d = tlen + (int)(bf - buf);592 } break;593update READMESean Barrettbefore adoptionf1417ef
line 594why, one step back594#ifdef STB_SPRINTF_NOFLOATMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 595–712why, one step back595 case 'A': // float596 case 'a': // hex float597 case 'G': // float598 case 'g': // float599 case 'E': // float600 case 'e': // float601 case 'f': // float602 va_arg(va, double); // eat it603 s = (char *)"No float";604 l = 8;605 lead[0] = 0;606 tail[0] = 0;607 pr = 0;608 dp = 0;609 cs = 0;610 goto scopy;611#else612 case 'A': // hex float613 case 'a': // hex float614 h = (f[0] == 'A') ? hexu : hex;615 fv = va_arg(va, double);616 if (pr == -1)617 pr = 6; // default is 6618 // read the double into a string619 if (stbsp__real_to_parts((stbsp__int64 *)&n64, &dp, fv))620 fl |= STBSP__NEGATIVE;621622 s = num + 64;623624 stbsp__lead_sign(fl, lead);625626 if (dp == -1023)627 dp = (n64) ? -1022 : 0;628 else629 n64 |= (((stbsp__uint64)1) << 52);630 n64 <<= (64 - 56);631 if (pr < 15)632 n64 += ((((stbsp__uint64)8) << 56) >> (pr * 4));633// add leading chars634635#ifdef STB_SPRINTF_MSVC_MODE636 *s++ = '0';637 *s++ = 'x';638#else639 lead[1 + lead[0]] = '0';640 lead[2 + lead[0]] = 'x';641 lead[0] += 2;642#endif643 *s++ = h[(n64 >> 60) & 15];644 n64 <<= 4;645 if (pr)646 *s++ = stbsp__period;647 sn = s;648649 // print the bits650 n = pr;651 if (n > 13)652 n = 13;653 if (pr > (stbsp__int32)n)654 tz = pr - n;655 pr = 0;656 while (n--) {657 *s++ = h[(n64 >> 60) & 15];658 n64 <<= 4;659 }660661 // print the expo662 tail[1] = h[17];663 if (dp < 0) {664 tail[2] = '-';665 dp = -dp;666 } else667 tail[2] = '+';668 n = (dp >= 1000) ? 6 : ((dp >= 100) ? 5 : ((dp >= 10) ? 4 : 3));669 tail[0] = (char)n;670 for (;;) {671 tail[n] = '0' + dp % 10;672 if (n <= 3)673 break;674 --n;675 dp /= 10;676 }677678 dp = (int)(s - sn);679 l = (int)(s - (num + 64));680 s = num + 64;681 cs = 1 + (3 << 24);682 goto scopy;683684 case 'G': // float685 case 'g': // float686 h = (f[0] == 'G') ? hexu : hex;687 fv = va_arg(va, double);688 if (pr == -1)689 pr = 6;690 else if (pr == 0)691 pr = 1; // default is 6692 // read the double into a string693 if (stbsp__real_to_str(&sn, &l, num, &dp, fv, (pr - 1) | 0x80000000))694 fl |= STBSP__NEGATIVE;695696 // clamp the precision and delete extra zeros after clamp697 n = pr;698 if (l > (stbsp__uint32)pr)699 l = pr;700 while ((l > 1) && (pr) && (sn[l - 1] == '0')) {701 --pr;702 --l;703 }704705 // should we use %e706 if ((dp <= -4) || (dp > (stbsp__int32)n)) {707 if (pr > (stbsp__int32)l)708 pr = l - 1;709 else if (pr)710 --pr; // when using %e, there is one digit before the decimal711 goto doexpfromg;712 }Merge branch 'misc-typos' of https://github.com/luzpaz/stb into workingSean Barrettbefore adoption3005bcf
line 713why, one step back713 // this is the insane action to get the pr to match %g semantics for %fMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 714–716why, one step back714 if (dp > 0) {715 pr = (dp < (stbsp__int32)l) ? l - dp : 0;716 } else {Merge branch 'working'Sean Barrettbefore adoption9092c04
line 717why, one step back717 pr = -dp + ((pr > (stbsp__int32)l) ? (stbsp__int32) l : pr);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 718–782why, one step back718 }719 goto dofloatfromg;720721 case 'E': // float722 case 'e': // float723 h = (f[0] == 'E') ? hexu : hex;724 fv = va_arg(va, double);725 if (pr == -1)726 pr = 6; // default is 6727 // read the double into a string728 if (stbsp__real_to_str(&sn, &l, num, &dp, fv, pr | 0x80000000))729 fl |= STBSP__NEGATIVE;730 doexpfromg:731 tail[0] = 0;732 stbsp__lead_sign(fl, lead);733 if (dp == STBSP__SPECIAL) {734 s = (char *)sn;735 cs = 0;736 pr = 0;737 goto scopy;738 }739 s = num + 64;740 // handle leading chars741 *s++ = sn[0];742743 if (pr)744 *s++ = stbsp__period;745746 // handle after decimal747 if ((l - 1) > (stbsp__uint32)pr)748 l = pr + 1;749 for (n = 1; n < l; n++)750 *s++ = sn[n];751 // trailing zeros752 tz = pr - (l - 1);753 pr = 0;754 // dump expo755 tail[1] = h[0xe];756 dp -= 1;757 if (dp < 0) {758 tail[2] = '-';759 dp = -dp;760 } else761 tail[2] = '+';762#ifdef STB_SPRINTF_MSVC_MODE763 n = 5;764#else765 n = (dp >= 100) ? 5 : 4;766#endif767 tail[0] = (char)n;768 for (;;) {769 tail[n] = '0' + dp % 10;770 if (n <= 3)771 break;772 --n;773 dp /= 10;774 }775 cs = 1 + (3 << 24); // how many tens776 goto flt_lead;777778 case 'f': // float779 fv = va_arg(va, double);780 doafloat:781 // do kilos782 if (fl & STBSP__METRIC_SUFFIX) {update READMESean Barrettbefore adoptionf1417ef
line 783why, one step back783 double divisor;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 784–920why, one step back784 divisor = 1000.0f;785 if (fl & STBSP__METRIC_1024)786 divisor = 1024.0;787 while (fl < 0x4000000) {788 if ((fv < divisor) && (fv > -divisor))789 break;790 fv /= divisor;791 fl += 0x1000000;792 }793 }794 if (pr == -1)795 pr = 6; // default is 6796 // read the double into a string797 if (stbsp__real_to_str(&sn, &l, num, &dp, fv, pr))798 fl |= STBSP__NEGATIVE;799 dofloatfromg:800 tail[0] = 0;801 stbsp__lead_sign(fl, lead);802 if (dp == STBSP__SPECIAL) {803 s = (char *)sn;804 cs = 0;805 pr = 0;806 goto scopy;807 }808 s = num + 64;809810 // handle the three decimal varieties811 if (dp <= 0) {812 stbsp__int32 i;813 // handle 0.000*000xxxx814 *s++ = '0';815 if (pr)816 *s++ = stbsp__period;817 n = -dp;818 if ((stbsp__int32)n > pr)819 n = pr;820 i = n;821 while (i) {822 if ((((stbsp__uintptr)s) & 3) == 0)823 break;824 *s++ = '0';825 --i;826 }827 while (i >= 4) {828 *(stbsp__uint32 *)s = 0x30303030;829 s += 4;830 i -= 4;831 }832 while (i) {833 *s++ = '0';834 --i;835 }836 if ((stbsp__int32)(l + n) > pr)837 l = pr - n;838 i = l;839 while (i) {840 *s++ = *sn++;841 --i;842 }843 tz = pr - (n + l);844 cs = 1 + (3 << 24); // how many tens did we write (for commas below)845 } else {846 cs = (fl & STBSP__TRIPLET_COMMA) ? ((600 - (stbsp__uint32)dp) % 3) : 0;847 if ((stbsp__uint32)dp >= l) {848 // handle xxxx000*000.0849 n = 0;850 for (;;) {851 if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {852 cs = 0;853 *s++ = stbsp__comma;854 } else {855 *s++ = sn[n];856 ++n;857 if (n >= l)858 break;859 }860 }861 if (n < (stbsp__uint32)dp) {862 n = dp - n;863 if ((fl & STBSP__TRIPLET_COMMA) == 0) {864 while (n) {865 if ((((stbsp__uintptr)s) & 3) == 0)866 break;867 *s++ = '0';868 --n;869 }870 while (n >= 4) {871 *(stbsp__uint32 *)s = 0x30303030;872 s += 4;873 n -= 4;874 }875 }876 while (n) {877 if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {878 cs = 0;879 *s++ = stbsp__comma;880 } else {881 *s++ = '0';882 --n;883 }884 }885 }886 cs = (int)(s - (num + 64)) + (3 << 24); // cs is how many tens887 if (pr) {888 *s++ = stbsp__period;889 tz = pr;890 }891 } else {892 // handle xxxxx.xxxx000*000893 n = 0;894 for (;;) {895 if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {896 cs = 0;897 *s++ = stbsp__comma;898 } else {899 *s++ = sn[n];900 ++n;901 if (n >= (stbsp__uint32)dp)902 break;903 }904 }905 cs = (int)(s - (num + 64)) + (3 << 24); // cs is how many tens906 if (pr)907 *s++ = stbsp__period;908 if ((l - dp) > (stbsp__uint32)pr)909 l = pr + dp;910 while (n < l) {911 *s++ = sn[n];912 ++n;913 }914 tz = pr - (l - dp);915 }916 }917 pr = 0;918919 // handle k,m,g,t920 if (fl & STBSP__METRIC_SUFFIX) {update READMESean Barrettbefore adoptionf1417ef
line 921why, one step back921 char idx;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 922–950why, one step back922 idx = 1;923 if (fl & STBSP__METRIC_NOSPACE)924 idx = 0;925 tail[0] = idx;926 tail[1] = ' ';927 {928 if (fl >> 24) { // SI kilo is 'k', JEDEC and SI kibits are 'K'.929 if (fl & STBSP__METRIC_1024)930 tail[idx + 1] = "_KMGT"[fl >> 24];931 else932 tail[idx + 1] = "_kMGT"[fl >> 24];933 idx++;934 // If printing kibits and not in jedec, add the 'i'.935 if (fl & STBSP__METRIC_1024 && !(fl & STBSP__METRIC_JEDEC)) {936 tail[idx + 1] = 'i';937 idx++;938 }939 tail[0] = idx;940 }941 }942 };943944 flt_lead:945 // get the length that we copied946 l = (stbsp__uint32)(s - (num + 64));947 s = num + 64;948 goto scopy;949#endif950update READMESean Barrettbefore adoptionf1417ef
lines 951–952why, one step back951 case 'B': // upper binary952 case 'b': // lower binaryMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 953–962why, one step back953 h = (f[0] == 'B') ? hexu : hex;954 lead[0] = 0;955 if (fl & STBSP__LEADING_0X) {956 lead[0] = 2;957 lead[1] = '0';958 lead[2] = h[0xb];959 }960 l = (8 << 4) | (1 << 8);961 goto radixnum;962update READMESean Barrettbefore adoptionf1417ef
line 963why, one step back963 case 'o': // octalMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 964–972why, one step back964 h = hexu;965 lead[0] = 0;966 if (fl & STBSP__LEADING_0X) {967 lead[0] = 1;968 lead[1] = '0';969 }970 l = (3 << 4) | (3 << 8);971 goto radixnum;972update READMESean Barrettbefore adoptionf1417ef
line 973why, one step back973 case 'p': // pointerMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 974–1028why, one step back974 fl |= (sizeof(void *) == 8) ? STBSP__INTMAX : 0;975 pr = sizeof(void *) * 2;976 fl &= ~STBSP__LEADINGZERO; // 'p' only prints the pointer with zeros977 // fall through - to X978979 case 'X': // upper hex980 case 'x': // lower hex981 h = (f[0] == 'X') ? hexu : hex;982 l = (4 << 4) | (4 << 8);983 lead[0] = 0;984 if (fl & STBSP__LEADING_0X) {985 lead[0] = 2;986 lead[1] = '0';987 lead[2] = h[16];988 }989 radixnum:990 // get the number991 if (fl & STBSP__INTMAX)992 n64 = va_arg(va, stbsp__uint64);993 else994 n64 = va_arg(va, stbsp__uint32);995996 s = num + STBSP__NUMSZ;997 dp = 0;998 // clear tail, and clear leading if value is zero999 tail[0] = 0;1000 if (n64 == 0) {1001 lead[0] = 0;1002 if (pr == 0) {1003 l = 0;1004 cs = (((l >> 4) & 15)) << 24;1005 goto scopy;1006 }1007 }1008 // convert to string1009 for (;;) {1010 *--s = h[n64 & ((1 << (l >> 8)) - 1)];1011 n64 >>= (l >> 8);1012 if (!((n64) || ((stbsp__int32)((num + STBSP__NUMSZ) - s) < pr)))1013 break;1014 if (fl & STBSP__TRIPLET_COMMA) {1015 ++l;1016 if ((l & 15) == ((l >> 4) & 15)) {1017 l &= ~15;1018 *--s = stbsp__comma;1019 }1020 }1021 };1022 // get the tens and the comma pos1023 cs = (stbsp__uint32)((num + STBSP__NUMSZ) - s) + ((((l >> 4) & 15)) << 24);1024 // get the length that we copied1025 l = (stbsp__uint32)((num + STBSP__NUMSZ) - s);1026 // copy it1027 goto scopy;1028update READMESean Barrettbefore adoptionf1417ef
lines 1029–1031why, one step back1029 case 'u': // unsigned1030 case 'i':1031 case 'd': // integerMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1032–1076why, one step back1032 // get the integer and abs it1033 if (fl & STBSP__INTMAX) {1034 stbsp__int64 i64 = va_arg(va, stbsp__int64);1035 n64 = (stbsp__uint64)i64;1036 if ((f[0] != 'u') && (i64 < 0)) {1037 n64 = (stbsp__uint64)-i64;1038 fl |= STBSP__NEGATIVE;1039 }1040 } else {1041 stbsp__int32 i = va_arg(va, stbsp__int32);1042 n64 = (stbsp__uint32)i;1043 if ((f[0] != 'u') && (i < 0)) {1044 n64 = (stbsp__uint32)-i;1045 fl |= STBSP__NEGATIVE;1046 }1047 }10481049#ifndef STB_SPRINTF_NOFLOAT1050 if (fl & STBSP__METRIC_SUFFIX) {1051 if (n64 < 1024)1052 pr = 0;1053 else if (pr == -1)1054 pr = 1;1055 fv = (double)(stbsp__int64)n64;1056 goto doafloat;1057 }1058#endif10591060 // convert to string1061 s = num + STBSP__NUMSZ;1062 l = 0;10631064 for (;;) {1065 // do in 32-bit chunks (avoid lots of 64-bit divides even with constant denominators)1066 char *o = s - 8;1067 if (n64 >= 100000000) {1068 n = (stbsp__uint32)(n64 % 100000000);1069 n64 /= 100000000;1070 } else {1071 n = (stbsp__uint32)n64;1072 n64 = 0;1073 }1074 if ((fl & STBSP__TRIPLET_COMMA) == 0) {1075 do {1076 s -= 2;Merge branch 'working'Sean Barrettbefore adoption9092c04
line 1077why, one step back1077 *(stbsp__uint16 *)s = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2];Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1078–1304why, one step back1078 n /= 100;1079 } while (n);1080 }1081 while (n) {1082 if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) {1083 l = 0;1084 *--s = stbsp__comma;1085 --o;1086 } else {1087 *--s = (char)(n % 10) + '0';1088 n /= 10;1089 }1090 }1091 if (n64 == 0) {1092 if ((s[0] == '0') && (s != (num + STBSP__NUMSZ)))1093 ++s;1094 break;1095 }1096 while (s != o)1097 if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) {1098 l = 0;1099 *--s = stbsp__comma;1100 --o;1101 } else {1102 *--s = '0';1103 }1104 }11051106 tail[0] = 0;1107 stbsp__lead_sign(fl, lead);11081109 // get the length that we copied1110 l = (stbsp__uint32)((num + STBSP__NUMSZ) - s);1111 if (l == 0) {1112 *--s = '0';1113 l = 1;1114 }1115 cs = l + (3 << 24);1116 if (pr < 0)1117 pr = 0;11181119 scopy:1120 // get fw=leading/trailing space, pr=leading zeros1121 if (pr < (stbsp__int32)l)1122 pr = l;1123 n = pr + lead[0] + tail[0] + tz;1124 if (fw < (stbsp__int32)n)1125 fw = n;1126 fw -= n;1127 pr -= l;11281129 // handle right justify and leading zeros1130 if ((fl & STBSP__LEFTJUST) == 0) {1131 if (fl & STBSP__LEADINGZERO) // if leading zeros, everything is in pr1132 {1133 pr = (fw > pr) ? fw : pr;1134 fw = 0;1135 } else {1136 fl &= ~STBSP__TRIPLET_COMMA; // if no leading zeros, then no commas1137 }1138 }11391140 // copy the spaces and/or zeros1141 if (fw + pr) {1142 stbsp__int32 i;1143 stbsp__uint32 c;11441145 // copy leading spaces (or when doing %8.4d stuff)1146 if ((fl & STBSP__LEFTJUST) == 0)1147 while (fw > 0) {1148 stbsp__cb_buf_clamp(i, fw);1149 fw -= i;1150 while (i) {1151 if ((((stbsp__uintptr)bf) & 3) == 0)1152 break;1153 *bf++ = ' ';1154 --i;1155 }1156 while (i >= 4) {1157 *(stbsp__uint32 *)bf = 0x20202020;1158 bf += 4;1159 i -= 4;1160 }1161 while (i) {1162 *bf++ = ' ';1163 --i;1164 }1165 stbsp__chk_cb_buf(1);1166 }11671168 // copy leader1169 sn = lead + 1;1170 while (lead[0]) {1171 stbsp__cb_buf_clamp(i, lead[0]);1172 lead[0] -= (char)i;1173 while (i) {1174 *bf++ = *sn++;1175 --i;1176 }1177 stbsp__chk_cb_buf(1);1178 }11791180 // copy leading zeros1181 c = cs >> 24;1182 cs &= 0xffffff;1183 cs = (fl & STBSP__TRIPLET_COMMA) ? ((stbsp__uint32)(c - ((pr + cs) % (c + 1)))) : 0;1184 while (pr > 0) {1185 stbsp__cb_buf_clamp(i, pr);1186 pr -= i;1187 if ((fl & STBSP__TRIPLET_COMMA) == 0) {1188 while (i) {1189 if ((((stbsp__uintptr)bf) & 3) == 0)1190 break;1191 *bf++ = '0';1192 --i;1193 }1194 while (i >= 4) {1195 *(stbsp__uint32 *)bf = 0x30303030;1196 bf += 4;1197 i -= 4;1198 }1199 }1200 while (i) {1201 if ((fl & STBSP__TRIPLET_COMMA) && (cs++ == c)) {1202 cs = 0;1203 *bf++ = stbsp__comma;1204 } else1205 *bf++ = '0';1206 --i;1207 }1208 stbsp__chk_cb_buf(1);1209 }1210 }12111212 // copy leader if there is still one1213 sn = lead + 1;1214 while (lead[0]) {1215 stbsp__int32 i;1216 stbsp__cb_buf_clamp(i, lead[0]);1217 lead[0] -= (char)i;1218 while (i) {1219 *bf++ = *sn++;1220 --i;1221 }1222 stbsp__chk_cb_buf(1);1223 }12241225 // copy the string1226 n = l;1227 while (n) {1228 stbsp__int32 i;1229 stbsp__cb_buf_clamp(i, n);1230 n -= i;1231 STBSP__UNALIGNED(while (i >= 4) {1232 *(stbsp__uint32 *)bf = *(stbsp__uint32 *)s;1233 bf += 4;1234 s += 4;1235 i -= 4;1236 })1237 while (i) {1238 *bf++ = *s++;1239 --i;1240 }1241 stbsp__chk_cb_buf(1);1242 }12431244 // copy trailing zeros1245 while (tz) {1246 stbsp__int32 i;1247 stbsp__cb_buf_clamp(i, tz);1248 tz -= i;1249 while (i) {1250 if ((((stbsp__uintptr)bf) & 3) == 0)1251 break;1252 *bf++ = '0';1253 --i;1254 }1255 while (i >= 4) {1256 *(stbsp__uint32 *)bf = 0x30303030;1257 bf += 4;1258 i -= 4;1259 }1260 while (i) {1261 *bf++ = '0';1262 --i;1263 }1264 stbsp__chk_cb_buf(1);1265 }12661267 // copy tail if there is one1268 sn = tail + 1;1269 while (tail[0]) {1270 stbsp__int32 i;1271 stbsp__cb_buf_clamp(i, tail[0]);1272 tail[0] -= (char)i;1273 while (i) {1274 *bf++ = *sn++;1275 --i;1276 }1277 stbsp__chk_cb_buf(1);1278 }12791280 // handle the left justify1281 if (fl & STBSP__LEFTJUST)1282 if (fw > 0) {1283 while (fw) {1284 stbsp__int32 i;1285 stbsp__cb_buf_clamp(i, fw);1286 fw -= i;1287 while (i) {1288 if ((((stbsp__uintptr)bf) & 3) == 0)1289 break;1290 *bf++ = ' ';1291 --i;1292 }1293 while (i >= 4) {1294 *(stbsp__uint32 *)bf = 0x20202020;1295 bf += 4;1296 i -= 4;1297 }1298 while (i--)1299 *bf++ = ' ';1300 stbsp__chk_cb_buf(1);1301 }1302 }1303 break;1304update READMESean Barrettbefore adoptionf1417ef
line 1305why, one step back1305 default: // unknown, just copy codeMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1306–1329why, one step back1306 s = num + STBSP__NUMSZ - 1;1307 *s = f[0];1308 l = 1;1309 fw = fl = 0;1310 lead[0] = 0;1311 tail[0] = 0;1312 pr = 0;1313 dp = 0;1314 cs = 0;1315 goto scopy;1316 }1317 ++f;1318 }1319endfmt:13201321 if (!callback)1322 *bf = 0;1323 else1324 stbsp__flush_cb();13251326done:1327 return tlen + (int)(bf - buf);1328}1329update READMESean Barrettbefore adoptionf1417ef
lines 1330–1344why, one step back1330// cleanup1331#undef STBSP__LEFTJUST1332#undef STBSP__LEADINGPLUS1333#undef STBSP__LEADINGSPACE1334#undef STBSP__LEADING_0X1335#undef STBSP__LEADINGZERO1336#undef STBSP__INTMAX1337#undef STBSP__TRIPLET_COMMA1338#undef STBSP__NEGATIVE1339#undef STBSP__METRIC_SUFFIX1340#undef STBSP__NUMSZ1341#undef stbsp__chk_cb_bufL1342#undef stbsp__chk_cb_buf1343#undef stbsp__flush_cb1344#undef stbsp__cb_buf_clampMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1345why, one step back1345update READMESean Barrettbefore adoptionf1417ef
lines 1346–1347why, one step back1346// ============================================================================1347// wrapper functionsMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1348–1361why, one step back13481349STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...)1350{1351 int result;1352 va_list va;1353 va_start(va, fmt);1354 result = STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va);1355 va_end(va);1356 return result;1357}13581359typedef struct stbsp__context {1360 char *buf;1361 int count;stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1362why, one step back1362 int length;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1363why, one step back1363 char tmp[STB_SPRINTF_MIN];update READMESean Barrettbefore adoptionf1417ef
line 1364why, one step back1364} stbsp__context;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1365why, one step back1365sprintf: warning fixesSean Barrettbefore adoptioncb9d4e9
line 1366why, one step back1366static char *stbsp__clamp_callback(const char *buf, void *user, int len)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1367–1368why, one step back1367{1368 stbsp__context *c = (stbsp__context *)user;stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1369why, one step back1369 c->length += len;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1370–1375why, one step back13701371 if (len > c->count)1372 len = c->count;13731374 if (len) {1375 if (buf != c->buf) {sprintf: warning fixesSean Barrettbefore adoptioncb9d4e9
lines 1376–1377why, one step back1376 const char *s, *se;1377 char *d;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1378–1389why, one step back1378 d = c->buf;1379 s = buf;1380 se = buf + len;1381 do {1382 *d++ = *s++;1383 } while (s < se);1384 }1385 c->buf += len;1386 c->count -= len;1387 }13881389 if (c->count <= 0)stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1390why, one step back1390 return c->tmp;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1391–1393why, one step back1391 return (c->count >= STB_SPRINTF_MIN) ? c->buf : c->tmp; // go direct into buffer if you can1392}1393sprintf: warning fixesSean Barrettbefore adoptioncb9d4e9
line 1394why, one step back1394static char * stbsp__count_clamp_callback( const char * buf, void * user, int len )Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1395why, one step back1395{stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
line 1396why, one step back1396 stbsp__context * c = (stbsp__context*)user;sprintf: warning fixesSean Barrettbefore adoptioncb9d4e9
line 1397why, one step back1397 (void) sizeof(buf);stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
line 1398why, one step back1398stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1399why, one step back1399 c->length += len;stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
lines 1400–1404why, one step back1400 return c->tmp; // go direct into buffer if you can1401}14021403STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va )1404{Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1405–1406why, one step back1405 stbsp__context c;1406stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
lines 1407–1408why, one step back1407 if ( (count == 0) && !buf )1408 {stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1409why, one step back1409 c.length = 0;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1410why, one step back1410stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
lines 1411–1414why, one step back1411 STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__count_clamp_callback, &c, c.tmp, fmt, va );1412 }1413 else1414 {stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1415why, one step back1415 int l;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1416why, one step back1416stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
lines 1417–1418why, one step back1417 c.buf = buf;1418 c.count = count;stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1419why, one step back1419 c.length = 0;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1420why, one step back1420stb_sprintf: fix size-only snprintf querySean Barrettbefore adoption01daa3a
lines 1421–1428why, one step back1421 STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__clamp_callback, &c, stbsp__clamp_callback(0,&c,0), fmt, va );14221423 // zero-terminate1424 l = (int)( c.buf - buf );1425 if ( l >= count ) // should never be greater, only equal (or less) than count1426 l = count - 1;1427 buf[l] = 0;1428 }Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1429why, one step back1429stb_sprintf: PR 613Sean Barrettbefore adoptione802821
line 1430why, one step back1430 return c.length;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1431–1449why, one step back1431}14321433STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...)1434{1435 int result;1436 va_list va;1437 va_start(va, fmt);14381439 result = STB_SPRINTF_DECORATE(vsnprintf)(buf, count, fmt, va);1440 va_end(va);14411442 return result;1443}14441445STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va)1446{1447 return STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va);1448}1449update READMESean Barrettbefore adoptionf1417ef
lines 1450–1451why, one step back1450// =======================================================================1451// low level float utility functionsMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1452–1454why, one step back14521453#ifndef STB_SPRINTF_NOFLOAT1454update READMESean Barrettbefore adoptionf1417ef
line 1455why, one step back1455// copies d to bits w/ strict aliasing (this compiles to nothing on /Ox)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1456–1462why, one step back1456#define STBSP__COPYFP(dest, src) \1457 { \1458 int cn; \1459 for (cn = 0; cn < 8; cn++) \1460 ((char *)&dest)[cn] = ((char *)&src)[cn]; \1461 }1462update READMESean Barrettbefore adoptionf1417ef
line 1463why, one step back1463// get float infoMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1464–1476why, one step back1464static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value)1465{1466 double d;1467 stbsp__int64 b = 0;14681469 // load value and round at the frac_digits1470 d = value;14711472 STBSP__COPYFP(b, d);14731474 *bits = b & ((((stbsp__uint64)1) << 52) - 1);1475 *expo = (stbsp__int32)(((b >> 52) & 2047) - 1023);1476Merge branch 'working'Sean Barrettbefore adoption9092c04
line 1477why, one step back1477 return (stbsp__int32)((stbsp__uint64) b >> 63);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1478–1544why, one step back1478}14791480static double const stbsp__bot[23] = {1481 1e+000, 1e+001, 1e+002, 1e+003, 1e+004, 1e+005, 1e+006, 1e+007, 1e+008, 1e+009, 1e+010, 1e+011,1482 1e+012, 1e+013, 1e+014, 1e+015, 1e+016, 1e+017, 1e+018, 1e+019, 1e+020, 1e+021, 1e+0221483};1484static double const stbsp__negbot[22] = {1485 1e-001, 1e-002, 1e-003, 1e-004, 1e-005, 1e-006, 1e-007, 1e-008, 1e-009, 1e-010, 1e-011,1486 1e-012, 1e-013, 1e-014, 1e-015, 1e-016, 1e-017, 1e-018, 1e-019, 1e-020, 1e-021, 1e-0221487};1488static double const stbsp__negboterr[22] = {1489 -5.551115123125783e-018, -2.0816681711721684e-019, -2.0816681711721686e-020, -4.7921736023859299e-021, -8.1803053914031305e-022, 4.5251888174113741e-023,1490 4.5251888174113739e-024, -2.0922560830128471e-025, -6.2281591457779853e-026, -3.6432197315497743e-027, 6.0503030718060191e-028, 2.0113352370744385e-029,1491 -3.0373745563400371e-030, 1.1806906454401013e-032, -7.7705399876661076e-032, 2.0902213275965398e-033, -7.1542424054621921e-034, -7.1542424054621926e-035,1492 2.4754073164739869e-036, 5.4846728545790429e-037, 9.2462547772103625e-038, -4.8596774326570872e-0391493};1494static double const stbsp__top[13] = {1495 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+2991496};1497static double const stbsp__negtop[13] = {1498 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-2991499};1500static double const stbsp__toperr[13] = {1501 8388608,1502 6.8601809640529717e+028,1503 -7.253143638152921e+052,1504 -4.3377296974619174e+075,1505 -1.5559416129466825e+098,1506 -3.2841562489204913e+121,1507 -3.7745893248228135e+144,1508 -1.7356668416969134e+167,1509 -3.8893577551088374e+190,1510 -9.9566444326005119e+213,1511 6.3641293062232429e+236,1512 -5.2069140800249813e+259,1513 -5.2504760255204387e+2821514};1515static double const stbsp__negtoperr[13] = {1516 3.9565301985100693e-040, -2.299904345391321e-063, 3.6506201437945798e-086, 1.1875228833981544e-109,1517 -5.0644902316928607e-132, -6.7156837247865426e-155, -2.812077463003139e-178, -5.7778912386589953e-201,1518 7.4997100559334532e-224, -4.6439668915134491e-247, -6.3691100762962136e-270, -9.436808465446358e-293,1519 8.0970921678014997e-3171520};15211522#if defined(_MSC_VER) && (_MSC_VER <= 1200)1523static stbsp__uint64 const stbsp__powten[20] = {1524 1,1525 10,1526 100,1527 1000,1528 10000,1529 100000,1530 1000000,1531 10000000,1532 100000000,1533 1000000000,1534 10000000000,1535 100000000000,1536 1000000000000,1537 10000000000000,1538 100000000000000,1539 1000000000000000,1540 10000000000000000,1541 100000000000000000,1542 1000000000000000000,1543 10000000000000000000U1544};update READMESean Barrettbefore adoptionf1417ef
line 1545why, one step back1545#define stbsp__tento19th ((stbsp__uint64)1000000000000000000)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1546–1568why, one step back1546#else1547static stbsp__uint64 const stbsp__powten[20] = {1548 1,1549 10,1550 100,1551 1000,1552 10000,1553 100000,1554 1000000,1555 10000000,1556 100000000,1557 1000000000,1558 10000000000ULL,1559 100000000000ULL,1560 1000000000000ULL,1561 10000000000000ULL,1562 100000000000000ULL,1563 1000000000000000ULL,1564 10000000000000000ULL,1565 100000000000000000ULL,1566 1000000000000000000ULL,1567 10000000000000000000ULL1568};update READMESean Barrettbefore adoptionf1417ef
line 1569why, one step back1569#define stbsp__tento19th (1000000000000000000ULL)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1570–1673why, one step back1570#endif15711572#define stbsp__ddmulthi(oh, ol, xh, yh) \1573 { \1574 double ahi = 0, alo, bhi = 0, blo; \1575 stbsp__int64 bt; \1576 oh = xh * yh; \1577 STBSP__COPYFP(bt, xh); \1578 bt &= ((~(stbsp__uint64)0) << 27); \1579 STBSP__COPYFP(ahi, bt); \1580 alo = xh - ahi; \1581 STBSP__COPYFP(bt, yh); \1582 bt &= ((~(stbsp__uint64)0) << 27); \1583 STBSP__COPYFP(bhi, bt); \1584 blo = yh - bhi; \1585 ol = ((ahi * bhi - oh) + ahi * blo + alo * bhi) + alo * blo; \1586 }15871588#define stbsp__ddtoS64(ob, xh, xl) \1589 { \1590 double ahi = 0, alo, vh, t; \1591 ob = (stbsp__int64)ph; \1592 vh = (double)ob; \1593 ahi = (xh - vh); \1594 t = (ahi - xh); \1595 alo = (xh - (ahi - t)) - (vh + t); \1596 ob += (stbsp__int64)(ahi + alo + xl); \1597 }15981599#define stbsp__ddrenorm(oh, ol) \1600 { \1601 double s; \1602 s = oh + ol; \1603 ol = ol - (s - oh); \1604 oh = s; \1605 }16061607#define stbsp__ddmultlo(oh, ol, xh, xl, yh, yl) ol = ol + (xh * yl + xl * yh);16081609#define stbsp__ddmultlos(oh, ol, xh, yl) ol = ol + (xh * yl);16101611static void stbsp__raise_to_power10(double *ohi, double *olo, double d, stbsp__int32 power) // power can be -323 to +3501612{1613 double ph, pl;1614 if ((power >= 0) && (power <= 22)) {1615 stbsp__ddmulthi(ph, pl, d, stbsp__bot[power]);1616 } else {1617 stbsp__int32 e, et, eb;1618 double p2h, p2l;16191620 e = power;1621 if (power < 0)1622 e = -e;1623 et = (e * 0x2c9) >> 14; /* %23 */1624 if (et > 13)1625 et = 13;1626 eb = e - (et * 23);16271628 ph = d;1629 pl = 0.0;1630 if (power < 0) {1631 if (eb) {1632 --eb;1633 stbsp__ddmulthi(ph, pl, d, stbsp__negbot[eb]);1634 stbsp__ddmultlos(ph, pl, d, stbsp__negboterr[eb]);1635 }1636 if (et) {1637 stbsp__ddrenorm(ph, pl);1638 --et;1639 stbsp__ddmulthi(p2h, p2l, ph, stbsp__negtop[et]);1640 stbsp__ddmultlo(p2h, p2l, ph, pl, stbsp__negtop[et], stbsp__negtoperr[et]);1641 ph = p2h;1642 pl = p2l;1643 }1644 } else {1645 if (eb) {1646 e = eb;1647 if (eb > 22)1648 eb = 22;1649 e -= eb;1650 stbsp__ddmulthi(ph, pl, d, stbsp__bot[eb]);1651 if (e) {1652 stbsp__ddrenorm(ph, pl);1653 stbsp__ddmulthi(p2h, p2l, ph, stbsp__bot[e]);1654 stbsp__ddmultlos(p2h, p2l, stbsp__bot[e], pl);1655 ph = p2h;1656 pl = p2l;1657 }1658 }1659 if (et) {1660 stbsp__ddrenorm(ph, pl);1661 --et;1662 stbsp__ddmulthi(p2h, p2l, ph, stbsp__top[et]);1663 stbsp__ddmultlo(p2h, p2l, ph, pl, stbsp__top[et], stbsp__toperr[et]);1664 ph = p2h;1665 pl = p2l;1666 }1667 }1668 }1669 stbsp__ddrenorm(ph, pl);1670 *ohi = ph;1671 *olo = pl;1672}1673update READMESean Barrettbefore adoptionf1417ef
lines 1674–1677why, one step back1674// given a float value, returns the significant bits in bits, and the position of the1675// decimal point in decimal_pos. +/-INF and NAN are specified by special values1676// returned in the decimal_pos parameter.1677// 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 1678–1686why, one step back1678static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits)1679{1680 double d;1681 stbsp__int64 bits = 0;1682 stbsp__int32 expo, e, ng, tens;16831684 d = value;1685 STBSP__COPYFP(bits, d);1686 expo = (stbsp__int32)((bits >> 52) & 2047);Merge branch 'working'Sean Barrettbefore adoption9092c04
line 1687why, one step back1687 ng = (stbsp__int32)((stbsp__uint64) bits >> 63);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1688–1695why, one step back1688 if (ng)1689 d = -d;16901691 if (expo == 2047) // is nan or inf?1692 {1693 *start = (bits & ((((stbsp__uint64)1) << 52) - 1)) ? "NaN" : "Inf";1694 *decimal_pos = STBSP__SPECIAL;1695 *len = 3;update READMESean Barrettbefore adoptionf1417ef
line 1696why, one step back1696 return ng;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1697–1700why, one step back1697 }16981699 if (expo == 0) // is zero or denormal1700 {Merge branch 'fix_ub_shift' of https://github.com/wojdyr/stb into workingSean Barrettbefore adoption2805fe3
line 1701why, one step back1701 if (((stbsp__uint64) bits << 1) == 0) // do zeroMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1702–1796why, one step back1702 {1703 *decimal_pos = 1;1704 *start = out;1705 out[0] = '0';1706 *len = 1;1707 return ng;1708 }1709 // find the right expo for denormals1710 {1711 stbsp__int64 v = ((stbsp__uint64)1) << 51;1712 while ((bits & v) == 0) {1713 --expo;1714 v >>= 1;1715 }1716 }1717 }17181719 // find the decimal exponent as well as the decimal bits of the value1720 {1721 double ph, pl;17221723 // log10 estimate - very specifically tweaked to hit or undershoot by no more than 1 of log10 of all expos 1..20461724 tens = expo - 1023;1725 tens = (tens < 0) ? ((tens * 617) / 2048) : (((tens * 1233) / 4096) + 1);17261727 // move the significant bits into position and stick them into an int1728 stbsp__raise_to_power10(&ph, &pl, d, 18 - tens);17291730 // get full as much precision from double-double as possible1731 stbsp__ddtoS64(bits, ph, pl);17321733 // check if we undershot1734 if (((stbsp__uint64)bits) >= stbsp__tento19th)1735 ++tens;1736 }17371738 // now do the rounding in integer land1739 frac_digits = (frac_digits & 0x80000000) ? ((frac_digits & 0x7ffffff) + 1) : (tens + frac_digits);1740 if ((frac_digits < 24)) {1741 stbsp__uint32 dg = 1;1742 if ((stbsp__uint64)bits >= stbsp__powten[9])1743 dg = 10;1744 while ((stbsp__uint64)bits >= stbsp__powten[dg]) {1745 ++dg;1746 if (dg == 20)1747 goto noround;1748 }1749 if (frac_digits < dg) {1750 stbsp__uint64 r;1751 // add 0.5 at the right position and round1752 e = dg - frac_digits;1753 if ((stbsp__uint32)e >= 24)1754 goto noround;1755 r = stbsp__powten[e];1756 bits = bits + (r / 2);1757 if ((stbsp__uint64)bits >= stbsp__powten[dg])1758 ++tens;1759 bits /= r;1760 }1761 noround:;1762 }17631764 // kill long trailing runs of zeros1765 if (bits) {1766 stbsp__uint32 n;1767 for (;;) {1768 if (bits <= 0xffffffff)1769 break;1770 if (bits % 1000)1771 goto donez;1772 bits /= 1000;1773 }1774 n = (stbsp__uint32)bits;1775 while ((n % 1000) == 0)1776 n /= 1000;1777 bits = n;1778 donez:;1779 }17801781 // convert to string1782 out += 64;1783 e = 0;1784 for (;;) {1785 stbsp__uint32 n;1786 char *o = out - 8;1787 // do the conversion in chunks of U32s (avoid most 64-bit divides, worth it, constant denomiators be damned)1788 if (bits >= 100000000) {1789 n = (stbsp__uint32)(bits % 100000000);1790 bits /= 100000000;1791 } else {1792 n = (stbsp__uint32)bits;1793 bits = 0;1794 }1795 while (n) {1796 out -= 2;Merge branch 'working'Sean Barrettbefore adoption9092c04
line 1797why, one step back1797 *(stbsp__uint16 *)out = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2];Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1798–1819why, one step back1798 n /= 100;1799 e += 2;1800 }1801 if (bits == 0) {1802 if ((e) && (out[0] == '0')) {1803 ++out;1804 --e;1805 }1806 break;1807 }1808 while (out != o) {1809 *--out = '0';1810 ++e;1811 }1812 }18131814 *decimal_pos = tens;1815 *start = out;1816 *len = e;1817 return ng;1818}1819update READMESean Barrettbefore adoptionf1417ef
lines 1820–1823why, one step back1820#undef stbsp__ddmulthi1821#undef stbsp__ddrenorm1822#undef stbsp__ddmultlo1823#undef stbsp__ddmultlosMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1824why, one step back1824#undef STBSP__SPECIALupdate READMESean Barrettbefore adoptionf1417ef
line 1825why, one step back1825#undef STBSP__COPYFPMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1826why, one step back1826update READMESean Barrettbefore adoptionf1417ef
line 1827why, one step back1827#endif // STB_SPRINTF_NOFLOATMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1828why, one step back1828update READMESean Barrettbefore adoptionf1417ef
lines 1829–1830why, one step back1829// clean up1830#undef stbsp__uint16Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1831–1832why, one step back1831#undef stbsp__uint321832#undef stbsp__int32update READMESean Barrettbefore adoptionf1417ef
lines 1833–1835why, one step back1833#undef stbsp__uint641834#undef stbsp__int641835#undef STBSP__UNALIGNEDMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1836why, one step back1836update READMESean Barrettbefore adoptionf1417ef
line 1837why, one step back1837#endif // STB_SPRINTF_IMPLEMENTATIONMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1838–1840why, one step back18381839/*1840------------------------------------------------------------------------------update READMESean Barrettbefore adoptionf1417ef
line 1841why, one step back1841This software is available under 2 licenses -- choose whichever you prefer.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1842why, one step back1842------------------------------------------------------------------------------update READMESean Barrettbefore adoptionf1417ef
lines 1843–1844why, one step back1843ALTERNATIVE A - MIT License1844Copyright (c) 2017 Sean BarrettMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1845–1849why, one step back1845Permission is hereby granted, free of charge, to any person obtaining a copy of1846this software and associated documentation files (the "Software"), to deal in1847the Software without restriction, including without limitation the rights to1848use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies1849of the Software, and to permit persons to whom the Software is furnished to doupdate READMESean Barrettbefore adoptionf1417ef
line 1850why, one step back1850so, subject to the following conditions:Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1851why, one step back1851The above copyright notice and this permission notice shall be included in allupdate READMESean Barrettbefore adoptionf1417ef
line 1852why, one step back1852copies or substantial portions of the Software.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1853–1858why, one step back1853THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR1854IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,1855FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE1856AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER1857LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,1858OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THEupdate READMESean Barrettbefore adoptionf1417ef
line 1859why, one step back1859SOFTWARE.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1860why, one step back1860------------------------------------------------------------------------------update READMESean Barrettbefore adoptionf1417ef
lines 1861–1862why, one step back1861ALTERNATIVE B - Public Domain (www.unlicense.org)1862This is free and unencumbered software released into the public domain.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1863–1864why, one step back1863Anyone is free to copy, modify, publish, use, compile, sell, or distribute this1864software, either in source code form or as a compiled binary, for any purpose,update READMESean Barrettbefore adoptionf1417ef
line 1865why, one step back1865commercial or non-commercial, and by any means.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1866–1870why, one step back1866In jurisdictions that recognize copyright laws, the author or authors of this1867software dedicate any and all copyright interest in the software to the public1868domain. We make this dedication for the benefit of the public at large and to1869the detriment of our heirs and successors. We intend this dedication to be an1870overt act of relinquishment in perpetuity of all present and future rights toupdate READMESean Barrettbefore adoptionf1417ef
line 1871why, one step back1871this software under copyright law.Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1872–1876why, one step back1872THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR1873IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,1874FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE1875AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN1876ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTIONupdate READMESean Barrettbefore adoptionf1417ef
lines 1877–1879why, one step back1877WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.1878------------------------------------------------------------------------------1879*/