-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmsh_std.h
1446 lines (1250 loc) · 42.6 KB
/
msh_std.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
==============================================================================
Licensing information can be found at the end of the file.
==============================================================================
MSH_STD.H v0.6
A single header library for extending the C standard library.
This file is partially written by myself, but also includes a lot
of code copied / adapted from other libraries. Please see credits for details.
To use the library you simply add:
#define MSH_STD_IMPLEMENTATION
#include "msh_std.h"
The define should only include once in your source.
==============================================================================
DEPENDENCIES
This file depends on a number c stdlib header (see below).
By default, these are not included by this library. If you'd like these to
be included, define:
#define MSH_STD_INCLUDE_LIBC_HEADERS
==============================================================================
AUTHORS:
Maciej Halber
ADDITIONAL CREDITS:
Sean T. Barrett, Per Vognsen, Mattias Gustavsson, Randy Gaul, Ginger Bill,
Бранимир Караџић
Please see particular sections for exact source of code / inspiration
==============================================================================
TODOs:
[ ] Memory allocation / alternative allocators
[ ] Tracking memory allocs
[ ] Custom prints (stb_sprintf)
[ ] Random generation: modify Alias method for frequent weights updates (?)
*/
#ifndef MSH_STD
#define MSH_STD
#ifdef __cplusplus
extern "C" {
#endif
////////////////////////////////////////////////////////////////////////////////
// Miscellaneous
//
// Credits:
// Ginger Bill: System and architecture detection from gb.h and bgfx
// Бранимир Караџић: Platform detection macros
////////////////////////////////////////////////////////////////////////////////
// c standard library headers
// Note(maciej): Need to double check how many of those are actually needed
#ifdef MSH_STD_INCLUDE_LIBC_HEADERS
#include <assert.h>
#include <math.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <ctype.h>
#include <stddef.h>
#endif
#define msh_count_of(x) ((sizeof(x) / sizeof(*x)))
#ifdef MSH_STD_STATIC
#define MSH_STD_DEF static
#else
#define MSH_STD_DEF extern
#endif
#define msh_persistent static // Local variables with persisting values
#define msh_global static // Global variables
#define msh_internal static // Internal linkage
#define MSH_STRINGIFY(_x) MSH_STRINGIFY_(_x)
#define MSH_STRINGIFY_(_x) #_x
#define MSH_PLATFORM_WINDOWS 0
#define MSH_PLATFORM_LINUX 0
#define MSH_PLATFORM_MACOS 0
#if defined(_WIN32) || defined(_WIN64)
#undef MSH_PLATFORM_WINDOWS
#define MSH_PLATFORM_WINDOWS 1
#elif defined(__linux__)
#undef MSH_PLATFORM_LINUX
#define MSH_PLATFORM_LINUX 1
#elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
#undef MSH_PLATFORM_MACOS
#define MSH_PLATFORM_MACOS 1
#define GL_SILENCE_DEPRECATION
#else
#error "Platform not recognized!"
#endif
#define MSH_PLATFORM_POSIX (0 || MSH_PLATFORM_MACOS || MSH_PLATFORM_LINUX)
// PLATFORM NAME
#if MSH_PLATFORM_EMSCRIPTEN
#define MSH_PLATFORM_NAME \
"asm.js " MSH_STRINGIFY(__EMSCRIPTEN_major__) "." MSH_STRINGIFY( \
__EMSCRIPTEN_minor__) "." MSH_STRINGIFY(__EMSCRIPTEN_tiny__)
#elif MSH_PLATFORM_LINUX
#define MSH_PLATFORM_NAME "Linux"
#elif MSH_PLATFORM_MACOS
#define MSH_PLATFORM_NAME "OSX"
#elif MSH_PLATFORM_WINDOWS
#define MSH_PLATFORM_NAME "Windows"
#else
#error "Unknown platform!"
#endif
// Architecture
#define MSH_ARCH_64BIT 0
#define MSH_ARCH_32BIT 0
#if defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || \
defined(__64BIT__) || defined(__powerpc64__) || defined(__ppc64__)
#undef MSH_ARCH_64BIT
#define MSH_ARCH_64BIT 1
#else
#undef MSH_ARCH_32BIT
#define MSH_ARCH_32BIT 1
#endif
// CPU
#define MSH_CPU_ARM 0
#define MSH_CPU_PPC 0
#define MSH_CPU_X86 0
#if defined(__arm__) || defined(__aarch64__) || defined(_M_ARM)
#undef MSH_CPU_ARM
#define MSH_CPU_ARM 1
#define MSH_CACHE_LINE_SIZE 64
#elif defined(_M_PPC) || defined(__powerpc__) || defined(__powerpc64__)
#undef MSH_CPU_PPC
#define MSH_CPU_PPC 1
#define MSH_CACHE_LINE_SIZE 128
#elif defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || \
defined(__x86_64__)
#undef MSH_CPU_X86
#define MSH_CPU_X86 1
#define MSH_CACHE_LINE_SIZE 64
#endif
// Endianness
#define MSH_CPU_ENDIAN_LITTLE 0
#define MSH_CPU_ENDIAN_BIG 0
#if MSH_CPU_PPC
//_LITTLE_ENDIAN exists on ppc64le.
#if _LITTLE_ENDIAN
#undef MSH_CPU_ENDIAN_LITTLE
#define MSH_CPU_ENDIAN_LITTLE 1
#else
#undef MSH_CPU_ENDIAN_BIG
#define MSH_CPU_ENDIAN_BIG 1
#endif
#else
#undef MSH_CPU_ENDIAN_LITTLE
#define MSH_CPU_ENDIAN_LITTLE 1
#endif
// Compiler detection
#define MSH_COMPILER_CLANG 0
#define MSH_COMPILER_GCC 0
#define MSH_COMPILER_MSVC 0
#define MSH_COMPLIER_TCC 0
#if defined(__clang__)
#undef MSH_COMPILER_CLANG
#define MSH_COMPILER_CLANG \
(__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
#elif defined(_MSC_VER)
#undef MSH_COMPILER_MSVC
#define MSH_COMPILER_MSVC _MSC_VER
#elif defined(__GNUC__)
#undef MSH_COMPILER_GCC
#define MSH_COMPILER_GCC \
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#elif defined(__TINYC__)
#undef MSH_COMPILER_TCC
#define MSH_COMPILER_TCC 1
#else
#error "MSH_COMPILER_* is not defined!"
#endif
// Compiler name
#if MSH_COMPILER_GCC
#define MSH_COMPILER_NAME \
"GCC " MSH_STRINGIFY(__GNUC__) "." MSH_STRINGIFY( \
__GNUC_MINOR__) "." MSH_STRINGIFY(__GNUC_PATCHLEVEL__)
#elif MSH_COMPILER_CLANG
#define MSH_COMPILER_NAME \
"Clang " MSH_STRINGIFY(__clang_major__) "." MSH_STRINGIFY( \
__clang_minor__) "." MSH_STRINGIFY(__clang_patchlevel__)
#elif MSH_COMPILER_MSVC
#if MSH_COMPILER_MSVC >= 1920 // Visual Studio 2019
#define MSH_COMPILER_NAME "MSVC 16.0"
#elif MSH_COMPILER_MSVC >= 1910 // Visual Studio 2017
#define MSH_COMPILER_NAME "MSVC 15.0"
#elif MSH_COMPILER_MSVC >= 1900 // Visual Studio 2015
#define MSH_COMPILER_NAME "MSVC 14.0"
#elif MSH_COMPILER_MSVC >= 1800 // Visual Studio 2013
#define MSH_COMPILER_NAME "MSVC 12.0"
#elif MSH_COMPILER_MSVC >= 1700 // Visual Studio 2012
#define MSH_COMPILER_NAME "MSVC 11.0"
#elif MSH_COMPILER_MSVC >= 1600 // Visual Studio 2010
#define MSH_COMPILER_NAME "MSVC 10.0"
#elif MSH_COMPILER_MSVC >= 1500 // Visual Studio 2008
#define MSH_COMPILER_NAME "MSVC 9.0"
#else
#define MSH_COMPILER_NAME "MSVC"
#endif
#elif MSH_COMPILER_TCC
#define MSH_COMPILER_NAME "TCC"
#endif
// C runtime
#define MSH_CRT_MINGW 0
#if defined(__MINGW32__) || defined(__MINGW64__)
#undef MSH_CRT_MINGW
#define MSH_CRT_MINGW 1
#endif
// System specific headers
#if MSH_PLATFORM_LINUX
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#endif
#if MSH_PLATFORM_WINDOWS
#if MSH_COMPILER_MSVC
#ifndef NOMINMAX
#define NOMINMAX
#endif
#define VC_EXTRALEAN
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif
#include <windows.h>
#include <direct.h>
// Thanks Windows!
#ifdef NO_ERROR
#undef NO_ERROR
#endif
#endif
#if MSH_PLATFORM_MACOS
#include <mach/mach_time.h>
#endif
////////////////////////////////////////////////////////////////////////////////
// Debug / Time
//
// Credits:
// Randy Gaul: cute_time.h
////////////////////////////////////////////////////////////////////////////////
enum msh__time_units
{
MSHT_SEC,
MSHT_MS,
MSHT_US,
MSHT_NS
};
MSH_STD_DEF void msh_sleep(uint64_t ms);
MSH_STD_DEF int32_t msh_time_rdtsc();
MSH_STD_DEF int32_t msh_time_rdtscp();
MSH_STD_DEF uint64_t msh_time_now();
MSH_STD_DEF double
msh_time_diff(int32_t unit, uint64_t new_time, uint64_t old_time);
MSH_STD_DEF double msh_time_diff_sec(uint64_t new_time, uint64_t old_time);
MSH_STD_DEF double msh_time_diff_ms(uint64_t new_time, uint64_t old_time);
MSH_STD_DEF double msh_time_diff_us(uint64_t new_time, uint64_t old_time);
MSH_STD_DEF double msh_time_diff_ns(uint64_t new_time, uint64_t old_time);
////////////////////////////////////////////////////////////////////////////////
// Printing Helpers
////////////////////////////////////////////////////////////////////////////////
#define msh_cprintf(cond, fmt, ...) \
do { \
if (cond) { printf(fmt, ##__VA_ARGS__); } \
} while (0)
#define msh_eprintf(fmt, ...) \
do { \
fprintf(stderr, fmt, ##__VA_ARGS__); \
} while (0)
#define msh_ceprintf(cond, fmt, ...) \
do { \
if (cond) { fprintf(stderr, fmt, ##__VA_ARGS__); } \
} while (0)
#define msh_panic_eprintf(fmt, ...) \
do { \
fprintf(stderr, fmt, ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
#define msh_panic_ceprintf(cond, fmt, ...) \
do { \
if (cond) \
{ \
fprintf(stderr, fmt, ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} \
} while (0)
MSH_STD_DEF void msh_print_progress_bar(char* prefix,
char* suffix,
uint64_t iter,
uint64_t total,
int32_t len);
////////////////////////////////////////////////////////////////////////////////
// Memory allocation + tracking
////////////////////////////////////////////////////////////////////////////////
// TODO
////////////////////////////////////////////////////////////////////////////////
// String and path manipulation
// TODO: This needs a complete rework
////////////////////////////////////////////////////////////////////////////////
MSH_STD_DEF char* msh_strdup(const char* src);
MSH_STD_DEF char* msh_strndup(const char* src, size_t len);
MSH_STD_DEF void msh_str_rstrip(const char* path);
MSH_STD_DEF size_t msh_strncpy(char* dst, const char* src, size_t len);
MSH_STD_DEF size_t msh_strcpy_range(char* dst,
const char* src,
size_t start,
size_t len);
#if MSH_PLATFORM_WINDOWS && !MSH_CRT_MINGW
#define MSH_FILE_SEPARATOR '\\'
#else
#define MSH_FILE_SEPARATOR '/'
#endif
MSH_STD_DEF int32_t msh_path_join(char* buf, size_t size, int32_t n, ...);
MSH_STD_DEF const char* msh_path_basename(const char* path);
MSH_STD_DEF void msh_path_normalize(char* path);
MSH_STD_DEF const char* msh_path_get_ext(const char* src);
////////////////////////////////////////////////////////////////////////////////
// Directory traversal
//
// Credits:
// Randy Gaul cute_files.h: https://github.com/RandyGaul/cute_headers/blob/master/cute_files.h
////////////////////////////////////////////////////////////////////////////////
struct msh_dir;
struct msh_finfo;
typedef struct msh_dir msh_dir_t;
typedef struct msh_finfo msh_finfo_t;
#define MSH_PATH_MAX_LEN 1024
#define MSH_FILENAME_MAX_LEN 128
#define MSH_FILEEXT_MAX_LEN 16
MSH_STD_DEF int32_t msh_dir_open(msh_dir_t* dir, const char* path);
MSH_STD_DEF void msh_dir_close(msh_dir_t* dir);
MSH_STD_DEF int32_t msh_file_peek(msh_dir_t*, msh_finfo_t* file);
MSH_STD_DEF void msh_dir_next(msh_dir_t* dir);
MSH_STD_DEF int32_t msh_file_exists(const char* path);
MSH_STD_DEF int32_t msh_make_dir(const char* src);
////////////////////////////////////////////////////////////////////////////////
// PCG-based random number generation
//
// Credits:
// Mattias Gustavsson: internals of pcg seed generator
// Jonatan Hedborg: unsigned int to normalized float conversion
////////////////////////////////////////////////////////////////////////////////
typedef struct msh_rand_ctx
{
uint64_t state[2];
} msh_rand_ctx_t;
MSH_STD_DEF void msh_rand_init(msh_rand_ctx_t* pcg, uint32_t seed);
MSH_STD_DEF uint32_t msh_rand_next(msh_rand_ctx_t* pcg);
MSH_STD_DEF float msh_rand_nextf(msh_rand_ctx_t* pcg);
MSH_STD_DEF int msh_rand_range(msh_rand_ctx_t* pcg, int min, int max);
////////////////////////////////////////////////////////////////////////////////
// Maths & stats helpers
//
// Credit:
// Ginger Bill: Limits from gb.h
////////////////////////////////////////////////////////////////////////////////
#ifndef MSH_U8_MIN
#define MSH_U8_MIN 0u
#define MSH_U8_MAX 0xffu
#define MSH_I8_MIN (-0x7f - 1)
#define MSH_I8_MAX 0x7f
#define MSH_U16_MIN 0u
#define MSH_U16_MAX 0xffffu
#define MSH_I16_MIN (-0x7fff - 1)
#define MSH_I16_MAX 0x7fff
#define MSH_U32_MIN 0u
#define MSH_U32_MAX 0xffffffffu
#define MSH_I32_MIN (-0x7fffffff - 1)
#define MSH_I32_MAX 0x7fffffff
#define MSH_U64_MIN 0ull
#define MSH_U64_MAX 0xffffffffffffffffull
#define MSH_I64_MIN (-0x7fffffffffffffffll - 1)
#define MSH_I64_MAX 0x7fffffffffffffffll
#define MSH_F32_MIN 1.17549435e-38f
#define MSH_F32_MAX 1.70141183e+38f
#define MSH_F64_MIN 2.2250738585072014e-308
#define MSH_F64_MAX 1.7976931348623157e+308
#endif
#define MSH_PI \
3.14159265358979323846264338327950288419716939937510582097494459231
#define MSH_TWO_PI \
6.28318530717958647692528676655900576839433879875021164194988918462
#define MSH_INV_PI \
0.31830988618379067153776752674502872406891929148091289749533468812
#define MSH_PI_OVER_TWO \
1.57079632679489661923132169163975144209858469968755291048747229615
#define msh_isnan(x) ((x) != (x))
#define msh_rad2deg(x) ((x)*180.0 * MSH_INV_PI)
#define msh_deg2rad(x) ((x)*0.005555555556 * MSH_PI)
#define msh_max(a, b) ((a) > (b) ? (a) : (b))
#define msh_min(a, b) ((a) < (b) ? (a) : (b))
#define msh_max3(a, b, c) msh_max(msh_max((a), (b)), (c))
#define msh_min3(a, b, c) msh_min(msh_min((a), (b)), (c))
#define msh_clamp(x, lower, upper) msh_min(msh_max((x), (lower)), (upper))
#define msh_clamp01(x) msh_clamp((x), 0, 1)
#define msh_is_within(x, lower, upper) (((x) >= (lower)) && ((x) <= (upper)))
#define msh_abs(x) ((x) < 0 ? -(x) : (x))
#if !MSH_COMPILER_TCC
// clang-format off
#define msh_sq(x) \
_Generic((x), int32_t : msh_sqi32, \
int64_t : msh_sqi64, \
float : msh_sqf, \
double : msh_sqd, \
default: msh_sqf)(x)
#define msh_accumulate(x, n) \
_Generic((x), int32_t : msh_accumulatei, \
float : msh_accumulatef, \
double : msh_accumulated, \
default : msh_accumulatef)(x, n)
// clang-format off
#endif
MSH_STD_DEF int32_t msh_sqi32(int32_t a);
MSH_STD_DEF int64_t msh_sqi64(int64_t a);
MSH_STD_DEF float msh_sqf(float a);
MSH_STD_DEF double msh_sqd(double a);
MSH_STD_DEF int32_t msh_accumulatei(const int32_t* vals, const size_t n_vals);
MSH_STD_DEF float msh_accumulatef(const float* vals, const size_t n_vals);
MSH_STD_DEF float msh_accumulated(const double* vals, const size_t n_vals);
MSH_STD_DEF float msh_inner_product(const float* vals, const int n_vals);
MSH_STD_DEF float msh_compute_mean(const float* vals, const int n_vals);
MSH_STD_DEF float msh_compute_stddev(float mean, float* vals, int n_vals);
MSH_STD_DEF float msh_gauss_1d(float x, float mu, float sigma);
MSH_STD_DEF float msh_gausspdf_1d(float x, float mu, float sigma);
MSH_STD_DEF void
msh_distrib2pdf(const double* dist, double* pdf, int32_t n_vals);
MSH_STD_DEF void msh_pdf2cdf(const double* pdf, double* cdf, int32_t n_vals);
MSH_STD_DEF void msh_invert_cdf(const double* cdf,
int32_t n_vals,
double* invcdf,
int32_t n_invcdf_bins);
MSH_STD_DEF int
msh_pdfsample_linear(const double* pdf, double prob, int32_t n_vals);
MSH_STD_DEF int
msh_pdfsample_invcdf(const double* pdf, double prob, int32_t n_vals);
typedef struct msh_discrete_distrib_t
{
double* prob;
int32_t* alias;
int32_t n_weights;
msh_rand_ctx_t rand_gen;
} msh_discrete_distrib_t;
MSH_STD_DEF void msh_discrete_distribution_init(msh_discrete_distrib_t* ctx,
double* weights,
int32_t n_weights,
uint32_t seed);
MSH_STD_DEF void msh_discrete_distribution_update(msh_discrete_distrib_t* ctx,
double* weights,
size_t n_weights);
MSH_STD_DEF void msh_discrete_distribution_free(msh_discrete_distrib_t* ctx);
MSH_STD_DEF int msh_discrete_distribution_sample(msh_discrete_distrib_t* ctx);
#ifdef __cplusplus
}
#endif
#endif /* MSH_STD */
#ifdef MSH_STD_IMPLEMENTATION
////////////////////////////////////////////////////////////////////////////////
// Printing Helpers
// TODOs: Helpers
/// ////////////////////////////////////////////////////////////////////////////
void
msh_print_progress_bar(char* prefix,
char* suffix,
uint64_t iter,
uint64_t total,
int32_t len)
{
unsigned char fill_chr = 219;
unsigned char empty_chr = 176;
float percent_complete = (float)(iter) / (float)(total - 1);
int32_t filled_len = (int32_t)(percent_complete * (int32_t)len);
unsigned char bar[1024] = {0};
for (int32_t i = 0; i < filled_len; ++i) { bar[i] = fill_chr; }
for (int32_t i = filled_len; i < len; ++i) { bar[i] = empty_chr; }
printf("\r%s%c%s%c %5.2f%% %s",
prefix ? prefix : "",
179,
bar,
195,
100.0f * percent_complete,
suffix ? suffix : "");
if (iter >= total - 1) { printf("\n"); }
}
////////////////////////////////////////////////////////////////////////////////
// String and path manipulation
////////////////////////////////////////////////////////////////////////////////
MSH_STD_DEF char*
msh_strndup(const char* src, size_t len)
{
char* cpy = (char*)malloc(len + 1);
memcpy(cpy, src, len);
cpy[len] = '\0';
return cpy;
}
MSH_STD_DEF char*
msh_strdup(const char* src)
{
size_t len = strlen(src);
return msh_strndup(src, len);
}
MSH_STD_DEF void
msh_path_rstrip(char* str)
{
char* end_ptr_1 = strrchr(str, '\r');
if (end_ptr_1)
{
*end_ptr_1 = 0;
return;
}
char* end_ptr_2 = strrchr(str, '\n');
if (end_ptr_2)
{
*end_ptr_2 = 0;
return;
}
}
MSH_STD_DEF size_t
msh_strcpy_range(char* dst, const char* src, size_t start, size_t len)
{
assert(dst != NULL && src != NULL);
size_t i = start;
size_t max = start + len;
while (i < max && *src != 0)
{
dst[i] = *src++;
i++;
}
dst[i] = 0;
return i;
}
MSH_STD_DEF size_t
msh_strncpy(char* dst, const char* src, size_t n)
{
return msh_strcpy_range(dst, src, 0, n);
}
MSH_STD_DEF const char*
msh_path_get_ext(const char* name)
{
const char* period = NULL;
while (*name++)
{
if (*name == '.') { period = name; }
}
if (period && strlen(period) > 1) { return period + 1; }
return period;
}
MSH_STD_DEF int32_t
msh_path_join(char* buf, size_t size, int32_t n, ...)
{
va_list args;
va_start(args, n);
size_t len = 0;
char separator[2] = {MSH_FILE_SEPARATOR, 0};
for (int32_t i = 0; i < n; i++)
{
const char* str = va_arg(args, const char*);
len = msh_strcpy_range(buf, str, len, size);
if (i < n - 1) { len = msh_strcpy_range(buf, separator, len, size); }
}
va_end(args);
return (int32_t)len;
}
MSH_STD_DEF const char*
msh_path_basename(const char* path)
{
const char* sep_ptr = strrchr(path, MSH_FILE_SEPARATOR);
if (sep_ptr && strlen(sep_ptr) > 1) { return sep_ptr + 1; }
return path;
}
MSH_STD_DEF void
msh_path_normalize(char* path)
{
int32_t last_idx = (int32_t)strlen(path) - 1;
if (path[last_idx] == '\\' || path[last_idx] == '/') { path[last_idx] = 0; }
}
////////////////////////////////////////////////////////////////////////////////
// Directory Traversal
////////////////////////////////////////////////////////////////////////////////
#if MSH_PLATFORM_WINDOWS
struct msh_dir
{
char path[MSH_PATH_MAX_LEN];
int32_t has_next;
HANDLE handle;
WIN32_FIND_DATAA file_data;
};
struct msh_finfo
{
char name[MSH_FILENAME_MAX_LEN];
char ext[MSH_FILEEXT_MAX_LEN];
msh_dir_t* parent_dir;
int32_t is_dir;
int32_t is_reg;
size_t size;
};
int32_t
msh_file_peek(msh_dir_t* dir, msh_finfo_t* file)
{
assert(dir->handle != INVALID_HANDLE_VALUE);
char* file_name = dir->file_data.cFileName;
const char* ext = msh_path_get_ext(file_name);
if (ext) { msh_strcpy_range(file->ext, ext, 0, MSH_FILEEXT_MAX_LEN); }
msh_strcpy_range(file->name, file_name, 0, MSH_FILENAME_MAX_LEN);
size_t max_dword = MAXDWORD;
file->size = ((size_t)dir->file_data.nFileSizeHigh * (max_dword + 1)) +
(size_t)dir->file_data.nFileSizeLow;
file->is_dir = !!(dir->file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
file->is_reg = !!(dir->file_data.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) ||
!(dir->file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
file->parent_dir = dir;
return 1;
}
MSH_STD_DEF void
msh_dir_next(msh_dir_t* dir)
{
assert(dir->has_next);
if (!FindNextFileA(dir->handle, &dir->file_data))
{
dir->has_next = 0;
DWORD err = GetLastError();
assert(err == ERROR_SUCCESS || err == ERROR_NO_MORE_FILES);
}
}
MSH_STD_DEF int32_t
msh_dir_open(msh_dir_t* dir, const char* path)
{
size_t n = msh_strcpy_range(dir->path, path, 0, MSH_PATH_MAX_LEN);
if (dir->path[n - 1] == '/' ||
dir->path[n - 1] == '\\') // MSYS uses unix slashes
{
dir->path[n - 1] = 0;
n--;
}
n = msh_strcpy_range(dir->path, "\\*", n, MSH_PATH_MAX_LEN);
dir->handle = FindFirstFileA(dir->path, &dir->file_data);
dir->path[n - 2] = 0;
// NOTE(maciej): I think I'd rather return an error value?
if (dir->handle == INVALID_HANDLE_VALUE)
{
LPVOID err_buf;
DWORD err = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&err_buf,
0,
NULL);
printf("ERROR: Failed to open directory (%s): %s", path, (char*)err_buf);
msh_dir_close(dir);
LocalFree(err_buf);
return 1;
}
dir->has_next = 1;
return 0;
}
MSH_STD_DEF void
msh_dir_close(msh_dir_t* dir)
{
dir->path[0] = 0;
dir->has_next = 0;
if (dir->handle != INVALID_HANDLE_VALUE) { FindClose(dir->handle); }
}
MSH_STD_DEF int32_t
msh_file_exists(const char* path)
{
WIN32_FILE_ATTRIBUTE_DATA unused;
return GetFileAttributesExA(path, GetFileExInfoStandard, &unused);
}
#endif
// TODO(maciej): Needs better error handling...
MSH_STD_DEF int32_t
msh__mkdir(const char* folder)
{
#if MSH_PLATFORM_WINDOWS
return _mkdir(folder);
#else
return mkdir(folder, 0777);
#endif
}
MSH_STD_DEF int32_t
msh_create_directory(const char* folder)
{
char* start = msh_strdup(folder);
char* end = start;
int counter = 0;
while (1)
{
while (end[0] != MSH_FILE_SEPARATOR && end[0] != 0) { end++; }
char tmp = end[0];
end[0] = 0;
msh__mkdir(start);
end[0] = tmp;
if (end[0] == 0) { break; }
end++;
counter++;
}
free(start);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// TIME
////////////////////////////////////////////////////////////////////////////////
MSH_STD_DEF void
msh_sleep(uint64_t ms)
{
#if MSH_PLATFORM_WINDOWS
Sleep((DWORD)ms);
#elif MSH_PLATFORM_POSIX
usleep(1000 * ms);
#endif
}
// NOTE(maciej): http://codearcana.com/posts/2013/05/15/a-cross-platform-monotonic-timer.html
MSH_STD_DEF uint64_t
msh_rdtsc()
{
#if MSH_PLATFORM_WINDOWS
return __rdtsc();
#elif MSH_PLATFORM_POSIX
/* From:
https://stackoverflow.com/questions/9887839/how-to-count-clock-cycles-with-rdtsc-in-gcc-x86
*/
uint32_t hi, lo;
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
return (((uint64_t)lo) | (((uint64_t)hi) << 32));
#endif
}
// GCC Does not deal with inline well...
MSH_STD_DEF double
msh_time_nano_to(int32_t unit, uint64_t time)
{
switch (unit)
{
case MSHT_SEC:
return (double)(time * 1e-9);
case MSHT_MS:
return (double)(time * 1e-6);
case MSHT_US:
return (double)(time * 1e-3);
case MSHT_NS:
return (double)(time);
}
return (double)(time);
}
MSH_STD_DEF double
msh_time_diff(int32_t unit, uint64_t new_time, uint64_t old_time)
{
uint64_t diff = new_time - old_time;
return msh_time_nano_to(unit, diff);
}
MSH_STD_DEF double
msh_time_diff_sec(uint64_t t2, uint64_t t1)
{
return msh_time_diff(MSHT_SEC, t2, t1);
}
MSH_STD_DEF double
msh_time_diff_ms(uint64_t t2, uint64_t t1)
{
return msh_time_diff(MSHT_MS, t2, t1);
}
MSH_STD_DEF double
msh_time_diff_us(uint64_t t2, uint64_t t1)
{
return msh_time_diff(MSHT_US, t2, t1);
}
MSH_STD_DEF double
msh_time_diff_ns(uint64_t t2, uint64_t t1)
{
return msh_time_diff(MSHT_NS, t2, t1);
}
/* prevent 64-bit overflow when computing relative timestamp
see https://gist.github.com/jspohr/3dc4f00033d79ec5bdaf67bc46c813e3
*/
msh_internal int64_t
msh__int64_muldiv(int64_t value, int64_t numer, int64_t denom)
{
int64_t q = value / denom;
int64_t r = value % denom;
return q * numer + r * numer / denom;
}
#if MSH_PLATFORM_WINDOWS
MSH_STD_DEF uint64_t
msh_time_now()
{
msh_persistent int first = 1;
msh_persistent LARGE_INTEGER freq;
msh_persistent LARGE_INTEGER start;
if (first)
{
first = 0;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
}
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
return msh__int64_muldiv(now.QuadPart - start.QuadPart,
1000000000,
freq.QuadPart);
}
#elif MSH_PLATFORM_LINUX
MSH_STD_DEF uint64_t
msh_time_now()
{
msh_persistent int first = 1;
msh_persistent uint64_t start;
struct timespec now;
if (first)
{
first = 0;
clock_gettime(CLOCK_MONOTONIC, &now);
start = (((uint64_t)now.tv_sec * 1000000000) + (uint64_t)now.tv_nsec);
}
clock_gettime(CLOCK_MONOTONIC, &now);
uint64_t time_now =
(((uint64_t)now.tv_sec * 1000000000) + (uint64_t)now.tv_nsec) - start;
return time_now;
}
#elif MSH_PLATFORM_MACOS
MSH_STD_DEF uint64_t
msh_time_now()
{
msh_persistent int first = 1;
msh_persistent uint64_t start;
msh_persistent mach_timebase_info_data_t info;
if (first)
{
first = 0;
mach_timebase_info(&info);
start = mach_absolute_time();
}
const uint64_t now = mach_absolute_time() - start;
return msh__int64_muldiv(now, info.numer, info.denom);
}
#endif
////////////////////////////////////////////////////////////////////////////////
// TIME
////////////////////////////////////////////////////////////////////////////////
// /* TODOs(maciej):
// [ ] Add naming of the blocks for better reporting
// [ ] Add wall clock time version?
// [ ] Add automatic counter / id gen (use map ??)
// [ ] Ensure that it works within for loops.
// */
// /*
// NOTES(maciej): String interning for comapring the strings?
// Current issues:
// - If function is hit multipe times, how to store timing? -- Just like handmade hero, record events linearly, and store to which record they hark back, then print record information after pairing the events.
// - How to pair start and end...? -- with stack variable?
// */
// typedef enum msh_debug_event_type
// {
// MSH_DEBUG_EVENT_START,
// MSH_DEBUG_EVENT_END,
// MSH_DEBUG_EVENT_PROCESSED,
// MSH_DEBUG_EVENT_COUNT
// } msh_debug_event_type_t;
// typedef struct msh_debug_event
// {
// uint32_t uid;
// uint32_t hit_count;
// uint8_t type;
// uint64_t clock;
// char* filename;
// char* function_name;
// uint16_t line_number;
// } msh_debug_event_t;
// #if 0
// typedef struct msh_debug_event_table