Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/faudio/src/stb.h
4389 views
1
/* stb.h - v2.32 - Sean's Tool Box -- public domain -- http://nothings.org/stb.h
2
no warranty is offered or implied; use this code at your own risk
3
4
This is a single header file with a bunch of useful utilities
5
for getting stuff done in C/C++.
6
7
Documentation: http://nothings.org/stb/stb_h.html
8
Unit tests: http://nothings.org/stb/stb.c
9
10
11
============================================================================
12
You MUST
13
14
#define STB_DEFINE
15
16
in EXACTLY _one_ C or C++ file that includes this header, BEFORE the
17
include, like this:
18
19
#define STB_DEFINE
20
#include "stb.h"
21
22
All other files should just #include "stb.h" without the #define.
23
============================================================================
24
25
26
Version History
27
28
2.32 stb_intcmprev, stb_uidict, fix random numbers on Linux
29
2.31 stb_ucharcmp
30
2.30 MinGW fix
31
2.29 attempt to fix use of swprintf()
32
2.28 various new functionality
33
2.27 test _WIN32 not WIN32 in STB_THREADS
34
2.26 various warning & bugfixes
35
2.25 various warning & bugfixes
36
2.24 various warning & bugfixes
37
2.23 fix 2.22
38
2.22 64-bit fixes from '!='; fix stb_sdict_copy() to have preferred name
39
2.21 utf-8 decoder rejects "overlong" encodings; attempted 64-bit improvements
40
2.20 fix to hash "copy" function--reported by someone with handle "!="
41
2.19 ???
42
2.18 stb_readdir_subdirs_mask
43
2.17 stb_cfg_dir
44
2.16 fix stb_bgio_, add stb_bgio_stat(); begin a streaming wrapper
45
2.15 upgraded hash table template to allow:
46
- aggregate keys (explicit comparison func for EMPTY and DEL keys)
47
- "static" implementations (so they can be culled if unused)
48
2.14 stb_mprintf
49
2.13 reduce identifiable strings in STB_NO_STB_STRINGS
50
2.12 fix STB_ONLY -- lots of uint32s, TRUE/FALSE things had crept in
51
2.11 fix bug in stb_dirtree_get() which caused "c://path" sorts of stuff
52
2.10 STB_F(), STB_I() inline constants (also KI,KU,KF,KD)
53
2.09 stb_box_face_vertex_axis_side
54
2.08 bugfix stb_trimwhite()
55
2.07 colored printing in windows (why are we in 1985?)
56
2.06 comparison functions are now functions-that-return-functions and
57
accept a struct-offset as a parameter (not thread-safe)
58
2.05 compile and pass tests under Linux (but no threads); thread cleanup
59
2.04 stb_cubic_bezier_1d, smoothstep, avoid dependency on registry
60
2.03 ?
61
2.02 remove integrated documentation
62
2.01 integrate various fixes; stb_force_uniprocessor
63
2.00 revised stb_dupe to use multiple hashes
64
1.99 stb_charcmp
65
1.98 stb_arr_deleten, stb_arr_insertn
66
1.97 fix stb_newell_normal()
67
1.96 stb_hash_number()
68
1.95 hack stb__rec_max; clean up recursion code to use new functions
69
1.94 stb_dirtree; rename stb_extra to stb_ptrmap
70
1.93 stb_sem_new() API cleanup (no blockflag-starts blocked; use 'extra')
71
1.92 stb_threadqueue--multi reader/writer queue, fixed size or resizeable
72
1.91 stb_bgio_* for reading disk asynchronously
73
1.90 stb_mutex uses CRITICAL_REGION; new stb_sync primitive for thread
74
joining; workqueue supports stb_sync instead of stb_semaphore
75
1.89 support ';' in constant-string wildcards; stb_mutex wrapper (can
76
implement with EnterCriticalRegion eventually)
77
1.88 portable threading API (only for win32 so far); worker thread queue
78
1.87 fix wildcard handling in stb_readdir_recursive
79
1.86 support ';' in wildcards
80
1.85 make stb_regex work with non-constant strings;
81
beginnings of stb_introspect()
82
1.84 (forgot to make notes)
83
1.83 whoops, stb_keep_if_different wasn't deleting the temp file
84
1.82 bring back stb_compress from stb_file.h for cmirror
85
1.81 various bugfixes, STB_FASTMALLOC_INIT inits FASTMALLOC in release
86
1.80 stb_readdir returns utf8; write own utf8-utf16 because lib was wrong
87
1.79 stb_write
88
1.78 calloc() support for malloc wrapper, STB_FASTMALLOC
89
1.77 STB_FASTMALLOC
90
1.76 STB_STUA - Lua-like language; (stb_image, stb_csample, stb_bilinear)
91
1.75 alloc/free array of blocks; stb_hheap bug; a few stb_ps_ funcs;
92
hash*getkey, hash*copy; stb_bitset; stb_strnicmp; bugfix stb_bst
93
1.74 stb_replaceinplace; use stdlib C function to convert utf8 to UTF-16
94
1.73 fix performance bug & leak in stb_ischar (C++ port lost a 'static')
95
1.72 remove stb_block, stb_block_manager, stb_decompress (to stb_file.h)
96
1.71 stb_trimwhite, stb_tokens_nested, etc.
97
1.70 back out 1.69 because it might problemize mixed builds; stb_filec()
98
1.69 (stb_file returns 'char *' in C++)
99
1.68 add a special 'tree root' data type for stb_bst; stb_arr_end
100
1.67 full C++ port. (stb_block_manager)
101
1.66 stb_newell_normal
102
1.65 stb_lex_item_wild -- allow wildcard items which MUST match entirely
103
1.64 stb_data
104
1.63 stb_log_name
105
1.62 stb_define_sort; C++ cleanup
106
1.61 stb_hash_fast -- Paul Hsieh's hash function (beats Bob Jenkins'?)
107
1.60 stb_delete_directory_recursive
108
1.59 stb_readdir_recursive
109
1.58 stb_bst variant with parent pointer for O(1) iteration, not O(log N)
110
1.57 replace LCG random with Mersenne Twister (found a public domain one)
111
1.56 stb_perfect_hash, stb_ischar, stb_regex
112
1.55 new stb_bst API allows multiple BSTs per node (e.g. secondary keys)
113
1.54 bugfix: stb_define_hash, stb_wildmatch, regexp
114
1.53 stb_define_hash; recoded stb_extra, stb_sdict use it
115
1.52 stb_rand_define, stb_bst, stb_reverse
116
1.51 fix 'stb_arr_setlen(NULL, 0)'
117
1.50 stb_wordwrap
118
1.49 minor improvements to enable the scripting language
119
1.48 better approach for stb_arr using stb_malloc; more invasive, clearer
120
1.47 stb_lex (lexes stb.h at 1.5ML/s on 3Ghz P4; 60/70% of optimal/flex)
121
1.46 stb_wrapper_*, STB_MALLOC_WRAPPER
122
1.45 lightly tested DFA acceleration of regexp searching
123
1.44 wildcard matching & searching; regexp matching & searching
124
1.43 stb_temp
125
1.42 allow stb_arr to use stb_malloc/realloc; note this is global
126
1.41 make it compile in C++; (disable stb_arr in C++)
127
1.40 stb_dupe tweak; stb_swap; stb_substr
128
1.39 stb_dupe; improve stb_file_max to be less stupid
129
1.38 stb_sha1_file: generate sha1 for file, even > 4GB
130
1.37 stb_file_max; partial support for utf8 filenames in Windows
131
1.36 remove STB__NO_PREFIX - poor interaction with IDE, not worth it
132
streamline stb_arr to make it separately publishable
133
1.35 bugfixes for stb_sdict, stb_malloc(0), stristr
134
1.34 (streaming interfaces for stb_compress)
135
1.33 stb_alloc; bug in stb_getopt; remove stb_overflow
136
1.32 (stb_compress returns, smaller&faster; encode window & 64-bit len)
137
1.31 stb_prefix_count
138
1.30 (STB__NO_PREFIX - remove stb_ prefixes for personal projects)
139
1.29 stb_fput_varlen64, etc.
140
1.28 stb_sha1
141
1.27 ?
142
1.26 stb_extra
143
1.25 ?
144
1.24 stb_copyfile
145
1.23 stb_readdir
146
1.22 ?
147
1.21 ?
148
1.20 ?
149
1.19 ?
150
1.18 ?
151
1.17 ?
152
1.16 ?
153
1.15 stb_fixpath, stb_splitpath, stb_strchr2
154
1.14 stb_arr
155
1.13 ?stb, stb_log, stb_fatal
156
1.12 ?stb_hash2
157
1.11 miniML
158
1.10 stb_crc32, stb_adler32
159
1.09 stb_sdict
160
1.08 stb_bitreverse, stb_ispow2, stb_big32
161
stb_fopen, stb_fput_varlen, stb_fput_ranged
162
stb_fcmp, stb_feq
163
1.07 (stb_encompress)
164
1.06 stb_compress
165
1.05 stb_tokens, (stb_hheap)
166
1.04 stb_rand
167
1.03 ?(s-strings)
168
1.02 ?stb_filelen, stb_tokens
169
1.01 stb_tolower
170
1.00 stb_hash, stb_intcmp
171
stb_file, stb_stringfile, stb_fgets
172
stb_prefix, stb_strlower, stb_strtok
173
stb_image
174
(stb_array), (stb_arena)
175
176
Parenthesized items have since been removed.
177
178
LICENSE
179
180
See end of file for license information.
181
182
CREDITS
183
184
Written by Sean Barrett.
185
186
Fixes:
187
Philipp Wiesemann
188
Robert Nix
189
r-lyeh
190
blackpawn
191
github:Mojofreem
192
Ryan Whitworth
193
Vincent Isambart
194
Mike Sartain
195
Eugene Opalev
196
Tim Sjostrand
197
github:infatum
198
Dave Butler (Croepha)
199
*/
200
201
#ifndef STB__INCLUDE_STB_H
202
#define STB__INCLUDE_STB_H
203
204
#define STB_VERSION 1
205
206
/* In addition to trimming out all the stuff FAudio does not use, we are also
207
* binding various stdlib functions stb.h uses to FAudio's stdlib.
208
* -flibit
209
*/
210
#ifdef STB_MEMCPY_OVERRIDE
211
#ifdef memcpy /* Thanks Apple! */
212
#undef memcpy
213
#endif
214
#define memcpy FAudio_memcpy
215
#endif
216
217
//////////////////////////////////////////////////////////////////////////////
218
//
219
// Miscellany
220
//
221
222
STB_EXTERN void stb_swap(void *p, void *q, size_t sz);
223
224
#ifdef STB_DEFINE
225
typedef struct { char d[4]; } stb__4;
226
typedef struct { char d[8]; } stb__8;
227
228
// optimize the small cases, though you shouldn't be calling this for those!
229
void stb_swap(void *p, void *q, size_t sz)
230
{
231
char buffer[256];
232
if (p == q) return;
233
if (sz == 4) {
234
stb__4 temp = * ( stb__4 *) p;
235
* (stb__4 *) p = * ( stb__4 *) q;
236
* (stb__4 *) q = temp;
237
return;
238
} else if (sz == 8) {
239
stb__8 temp = * ( stb__8 *) p;
240
* (stb__8 *) p = * ( stb__8 *) q;
241
* (stb__8 *) q = temp;
242
return;
243
}
244
245
while (sz > sizeof(buffer)) {
246
stb_swap(p, q, sizeof(buffer));
247
p = (char *) p + sizeof(buffer);
248
q = (char *) q + sizeof(buffer);
249
sz -= sizeof(buffer);
250
}
251
252
memcpy(buffer, p , sz);
253
memcpy(p , q , sz);
254
memcpy(q , buffer, sz);
255
}
256
#endif
257
258
//////////////////////////////////////////////////////////////////////////////
259
//
260
// Random Numbers via Meresenne Twister or LCG
261
//
262
263
STB_EXTERN unsigned int stb_srandLCG(unsigned int seed);
264
STB_EXTERN unsigned int stb_randLCG(void);
265
266
STB_EXTERN void stb_srand(unsigned int seed);
267
STB_EXTERN unsigned int stb_rand(void);
268
STB_EXTERN double stb_frand(void);
269
270
#define stb_rand_define(x,y) \
271
\
272
unsigned int x(void) \
273
{ \
274
static unsigned int stb__rand = y; \
275
stb__rand = stb__rand * 2147001325 + 715136305; /* BCPL */ \
276
return 0x31415926 ^ ((stb__rand >> 16) + (stb__rand << 16)); \
277
}
278
279
#ifdef STB_DEFINE
280
static unsigned int stb__rand_seed=0;
281
282
unsigned int stb_srandLCG(unsigned int seed)
283
{
284
unsigned int previous = stb__rand_seed;
285
stb__rand_seed = seed;
286
return previous;
287
}
288
289
unsigned int stb_randLCG(void)
290
{
291
stb__rand_seed = stb__rand_seed * 2147001325 + 715136305; // BCPL generator
292
// shuffle non-random bits to the middle, and xor to decorrelate with seed
293
return 0x31415926 ^ ((stb__rand_seed >> 16) + (stb__rand_seed << 16));
294
}
295
296
// public domain Mersenne Twister by Michael Brundage
297
#define STB__MT_LEN 624
298
299
int stb__mt_index = STB__MT_LEN*sizeof(int)+1;
300
unsigned int stb__mt_buffer[STB__MT_LEN];
301
302
void stb_srand(unsigned int seed)
303
{
304
int i;
305
unsigned int old = stb_srandLCG(seed);
306
for (i = 0; i < STB__MT_LEN; i++)
307
stb__mt_buffer[i] = stb_randLCG();
308
stb_srandLCG(old);
309
stb__mt_index = STB__MT_LEN*sizeof(unsigned int);
310
}
311
312
#define STB__MT_IA 397
313
#define STB__MT_IB (STB__MT_LEN - STB__MT_IA)
314
#define STB__UPPER_MASK 0x80000000
315
#define STB__LOWER_MASK 0x7FFFFFFF
316
#define STB__MATRIX_A 0x9908B0DF
317
#define STB__TWIST(b,i,j) ((b)[i] & STB__UPPER_MASK) | ((b)[j] & STB__LOWER_MASK)
318
#define STB__MAGIC(s) (((s)&1)*STB__MATRIX_A)
319
320
unsigned int stb_rand()
321
{
322
unsigned int * b = stb__mt_buffer;
323
int idx = stb__mt_index;
324
unsigned int s,r;
325
int i;
326
327
if (idx >= STB__MT_LEN*sizeof(unsigned int)) {
328
if (idx > STB__MT_LEN*sizeof(unsigned int))
329
stb_srand(0);
330
idx = 0;
331
i = 0;
332
for (; i < STB__MT_IB; i++) {
333
s = STB__TWIST(b, i, i+1);
334
b[i] = b[i + STB__MT_IA] ^ (s >> 1) ^ STB__MAGIC(s);
335
}
336
for (; i < STB__MT_LEN-1; i++) {
337
s = STB__TWIST(b, i, i+1);
338
b[i] = b[i - STB__MT_IB] ^ (s >> 1) ^ STB__MAGIC(s);
339
}
340
341
s = STB__TWIST(b, STB__MT_LEN-1, 0);
342
b[STB__MT_LEN-1] = b[STB__MT_IA-1] ^ (s >> 1) ^ STB__MAGIC(s);
343
}
344
stb__mt_index = idx + sizeof(unsigned int);
345
346
r = *(unsigned int *)((unsigned char *)b + idx);
347
348
r ^= (r >> 11);
349
r ^= (r << 7) & 0x9D2C5680;
350
r ^= (r << 15) & 0xEFC60000;
351
r ^= (r >> 18);
352
353
return r;
354
}
355
356
double stb_frand(void)
357
{
358
return stb_rand() / ((double) (1 << 16) * (1 << 16));
359
}
360
361
#endif
362
363
#undef STB_EXTERN
364
#endif // STB_INCLUDE_STB_H
365
366
/*
367
------------------------------------------------------------------------------
368
This software is available under 2 licenses -- choose whichever you prefer.
369
------------------------------------------------------------------------------
370
ALTERNATIVE A - MIT License
371
Copyright (c) 2017 Sean Barrett
372
Permission is hereby granted, free of charge, to any person obtaining a copy of
373
this software and associated documentation files (the "Software"), to deal in
374
the Software without restriction, including without limitation the rights to
375
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
376
of the Software, and to permit persons to whom the Software is furnished to do
377
so, subject to the following conditions:
378
The above copyright notice and this permission notice shall be included in all
379
copies or substantial portions of the Software.
380
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
381
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
382
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
383
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
384
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
385
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
386
SOFTWARE.
387
------------------------------------------------------------------------------
388
ALTERNATIVE B - Public Domain (www.unlicense.org)
389
This is free and unencumbered software released into the public domain.
390
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
391
software, either in source code form or as a compiled binary, for any purpose,
392
commercial or non-commercial, and by any means.
393
In jurisdictions that recognize copyright laws, the author or authors of this
394
software dedicate any and all copyright interest in the software to the public
395
domain. We make this dedication for the benefit of the public at large and to
396
the detriment of our heirs and successors. We intend this dedication to be an
397
overt act of relinquishment in perpetuity of all present and future rights to
398
this software under copyright law.
399
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
400
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
401
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
402
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
403
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
404
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
405
------------------------------------------------------------------------------
406
*/
407
408