1 /* include/c/defs.h
2 **
3 ** This file is in the public domain.
4 */
5   /** Loobeans - inverse booleans to match nock.
6   **/
7 #     define c3y      0
8 #     define c3n      1
9 
10 #     define _(x)        (c3y == (x))
11 #     define __(x)       ((x) ? c3y : c3n)
12 #     define c3a(x, y)   __(_(x) && _(y))
13 #     define c3o(x, y)   __(_(x) || _(y))
14 
15 
16   /** Random useful C macros.
17   **/
18     /* Assert.  Good to capture.
19     */
20 // #     define c3_assert(x)   assert(x)
21 #     define c3_assert(x)  ( (x) ? 0 : c3_cooked(), assert(x) )
22 
23     /* Stub.
24     */
25 #     define c3_stub       (assert(!"stub"), 0)
26 
27     /* Size in words.
28     */
29 #     define c3_wiseof(x)  (((sizeof (x)) + 3) >> 2)
30 
31     /* Bit counting.
32     */
33 #     define c3_bits_word(w) ((w) ? (32 - __builtin_clz(w)) : 0)
34 
35     /* Min and max.
36     */
37 #     define c3_max(x, y) ( ((x) > (y)) ? (x) : (y) )
38 #     define c3_min(x, y) ( ((x) < (y)) ? (x) : (y) )
39 
40     /* Rotate.
41     */
42 #     define c3_rotw(r, x)  ( ((x) << (r)) | ((x) >> (32 - (r))) )
43 
44     /* Emergency stdio fix.
45     */
46       int
47       c3_cooked();
48 
49     /* Short integers.
50     */
51 #     define c3_s1(a)          ( (a) )
52 #     define c3_s2(a, b)       ( ((b) << 8) | c3_s1(a) )
53 #     define c3_s3(a, b, c)    ( ((c) << 16) | c3_s2(a, b) )
54 #     define c3_s4(a, b, c, d) ( ((d) << 24) | c3_s3(a, b, c) )
55 
56 #     define c3_s5(a, b, c, d, e) \
57         ( ((uint64_t)c3_s1(e) << 32ULL) | c3_s4(a, b, c, d) )
58 #     define c3_s6(a, b, c, d, e, f) \
59         ( ((uint64_t)c3_s2(e, f) << 32ULL) | c3_s4(a, b, c, d) )
60 #     define c3_s7(a, b, c, d, e, f, g) \
61         ( ((uint64_t)c3_s3(e, f, g) << 32ULL) | c3_s4(a, b, c, d) )
62 #     define c3_s8(a, b, c, d, e, f, g, h) \
63         ( ((uint64_t)c3_s4(e, f, g, h) << 32ULL) | c3_s4(a, b, c, d) )
64 
65     /* Byte-order twiddling.
66     */
67 #     define c3_flip32(w) \
68         ( (((w) >> 24) & 0xff) \
69         | (((w) >> 16) & 0xff) << 8 \
70         | (((w) >>  8) & 0xff) << 16 \
71         | ( (w)        & 0xff) << 24 )
72 
73     /* Logging shorthand.
74     */
75 #     define c3_log_every(n, args...) \
76         do {                          \
77           static c3_w cnt_w = 0;      \
78                                       \
79           if ( 0 == cnt_w % (n) ) {   \
80             uL(fprintf(uH, args));    \
81           }                           \
82           cnt_w = (cnt_w + 1) % (n);  \
83         } while (0)
84 
85 /* c3_malloc(): asserting malloc
86  */
87 #define c3_malloc(s) ({                         \
88       void* rut = malloc(s);                    \
89       if ( 0 == rut ) {                         \
90         c3_assert(!"memory lost");              \
91       }                                         \
92       rut;})
93 
94 /* c3_calloc(): asserting calloc
95  */
96 #define c3_calloc(s) ({                         \
97       void* rut = c3_malloc(s);                 \
98       memset(rut, 0, s);                        \
99       rut;})
100