Debian Codegraph @

ulib/TYPE.h

raw
1 #ifndef ULIB__TYPE_H
2 #define ULIB__TYPE_H
3
4
5 /* related from SO for gcc:
6 * https://stackoverflow.com/questions/1188939/representing-128-bit-numbers-in-c
7 *******************************************************************************
8 typedef unsigned int uint128_t __attribute__((mode(TI)));
9
10 uint64_t x = 0xABCDEF01234568;
11 uint64_t y = ~x;
12
13 uint128_t result = ((uint128_t) x * y);
14
15 printf("%016llX * %016llX -> ", x, y);
16
17 uint64_t r1 = (result >> 64);
18 uint64_t r2 = result;
19
20 printf("%016llX %016llX\n", r1, r2);
21 */
22
23
24 /* Quad_t/U64 first appear in 5.00563 (8175356b44).
25 #ifdef Quad_t
26 typedef I64TYPE I64;
27 typedef U64TYPE U64;
28 #endif
29 * U64TYPE also first appears in 8175356b44.
30 * HAS_QUAD also appears in 5.00563 (de1c261475),
31 * but did not take over the typedefs until 6b8eaf9322,
32 * (also 5.00563) where U64 type was restricted to core.
33 * QUADKIND also first appears in 6b8eaf9322.
34 *
35 * U64 made available outside core in 5.27.7.
36 */
37 #ifndef U64
38 # ifdef U64TYPE
39 typedef U64TYPE U64;
40 # else
41 typedef uint64_t U64;
42 # endif
43 #endif
44
45 typedef union {
46 struct {
47 /* keep this struct first for namespace init */
48 U32 time_low;
49 U16 time_mid;
50 U16 time_high_and_version;
51 U16 clock_seq_and_variant;
52 U8 node[6];
53 } v1;
54 struct {
55 U64 low;
56 U64 high;
57 } v0;
58 struct {
59 U32 md5_high32;
60 U16 md5_high16;
61 U16 md5_mid_and_version;
62 U32 md5_low_and_variant;
63 U32 md5_low;
64 } v3;
65 struct {
66 U32 rand_a;
67 U32 rand_b_and_version;
68 U32 rand_c_and_variant;
69 U32 rand_d;
70 } v4;
71 struct {
72 U32 sha1_high32;
73 U16 sha1_high16;
74 U16 sha1_mid_and_version;
75 U32 sha1_low_and_variant;
76 U32 sha1_low;
77 } v5;
78 struct {
79 U32 time_high;
80 U16 time_mid;
81 U16 time_low_and_version;
82 U16 clock_seq_and_variant;
83 U8 node[6];
84 } v6;
85 struct {
86 U32 time_high;
87 U16 time_low;
88 U16 rand_a_and_version;
89 U64 rand_b_and_variant;
90 } v7;
91 U64 __align;
92 } struct_uu_t;
93
94 typedef unsigned char UCHAR;
95
96 #define CC_STATESZ 16 /* words: 4 constant, 8 key, 2 counter, 2 nonce */
97 #define CC_KEYSZ 40 /* bytes of user supplied key+nonce */
98 #define CC_CORESZ 64 /* bytes output by core */
99 #define CC_BUFSZ 16*CC_CORESZ /* bytes we get at a time (1024) */
100 #define CC_ROUNDS 20
101
102 typedef struct {
103 U32 state[CC_STATESZ];
104 UCHAR buf[CC_BUFSZ];
105 U16 have;
106 U64 __align;
107 } chacha_t;
108
109 /* not including trailing null */
110 #define MAX_PERSIST_LEN 512 - sizeof(UV) - 1
111
112 typedef struct {
113 STRLEN len; /* not including trailing null, so <= MAX_PERSIST_LEN */
114 UCHAR path[MAX_PERSIST_LEN];
115 } persist_t;
116
117 #if defined(HAVE_SRWLOCK)
118 typedef SRWLOCK uu_lock_t;
119 #elif defined(USE_WIN32_ALIEN) || defined(USE_WIN32_NATIVE)
120 typedef CRITICAL_SECTION uu_lock_t;
121 #elif defined(__OpenBSD__)
122 typedef uint32_t uu_lock_t;
123 #elif defined(__APPLE__) && defined(HAVE_DISPATCH_DISPATCH_H)
124 typedef dispatch_semaphore_t uu_lock_t;
125 #else
126 typedef sem_t uu_lock_t;
127 #endif
128
129 typedef struct {
130 uu_lock_t LOCK; /* ?? bytes (on Linux) */
131 U8 __pad0[64 - sizeof(uu_lock_t)];
132 /* -- 64 bytes -- */
133 struct timeval clock_last; /* 16 bytes (8 on win32) */
134 U64 clock_prev_reg; /* 8 bytes */
135 U64 clock_defer_100ns; /* 8 bytes */
136 I16 clock_adj; /* 2 bytes */
137 U16 clock_seq; /* 2 bytes */
138 U8 __pad1[64 - 20 - sizeof(struct timeval)];
139 /* -- 64 bytes -- */
140 U64 gen_epoch; /* 8 bytes */
141 U8 gen_node[6]; /* 6 bytes */ /* need 64bit align */
142 U16 gen_has_real_node; /* 2 bytes */
143 U8 gen_real_node[6]; /* 6 bytes */ /* need 64bit align */
144 U16 gen_use_unique; /* 2 bytes */
145 U8 __pad2[64 - 24];
146 /* -- 64 bytes -- */
147 chacha_t cc; /* 1104 bytes */ /* aligned 64bit */
148 U64 xo_s[4]; /* 32 bytes */
149 U64 sm_x; /* 8 bytes */
150 U8 __pad3[1152 - 40 - sizeof(chacha_t)];
151 /* -- 18 * 64 bytes -- */
152 persist_t clock_persist; /* 512 bytes */
153 /* -- 8 * 64 bytes -- */
154 } shared_mem_t;
155
156 /* this should be aligned at least 4 bytes, better yet 16 */
157 typedef U8 uu_t[16];
158
159 typedef struct {
160 shared_mem_t *shared; /* 8 bytes */
161 IV shared_len; /* 8 bytes */
162 FILE *clock_state_f; /* 8 bytes */
163 I32 clock_state_fd; /* 4 bytes */
164 U8 __pad5[64 - 4 - sizeof(shared_mem_t*) - sizeof(IV) - sizeof(FILE*)];
165 /* -- 64 bytes -- */
166 persist_t clock_persist; /* 512 bytes */
167 /* -- 8 * 64 bytes -- */
168 } my_cxt_t;
169 /*} __attribute__ ((aligned (64))) my_cxt_t;
170 *
171 * Align 64 bytes vs 8.
172 *
173 * It's a toss-up as to whether this helps -- faster some places,
174 * slower others.
175 */
176
177 /* no *UCXT_ variants, just use a comma */
178 #define pUCXT pTHX_ my_cxt_t *my_cxtp
179 #define aUCXT aTHX_ my_cxtp
180 #define UCXT (*my_cxtp)
181
182 #define SMEM UCXT.shared
183
184 #endif
185 /* ex:set ts=2 sw=2 itab=spaces: */