forked from urbit/vere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieve.h
538 lines (471 loc) · 12.7 KB
/
retrieve.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
/// @file
#ifndef U3_RETRIEVE_H
#define U3_RETRIEVE_H
#include "c3.h"
#include "allocate.h"
#include "error.h"
#include "gmp.h"
#include "types.h"
/** u3r_*: read without ever crashing.
**/
/* u3r_cell(): factor (a) as a cell (b c).
*/
inline c3_o
u3r_cell(u3_noun a, u3_noun* b, u3_noun* c)
{
u3a_cell* cel_u;
u3_assert(u3_none != a);
if ( c3y == u3a_is_cell(a) ) {
cel_u = u3a_to_ptr(a);
if ( b ) *b = cel_u->hed;
if ( c ) *c = cel_u->tel;
return c3y;
}
else {
return c3n;
}
}
/* u3r_trel(): factor (a) as a trel (b c d).
*/
inline c3_o
u3r_trel(u3_noun a, u3_noun *b, u3_noun *c, u3_noun *d)
{
u3_noun guf;
if ( (c3y == u3r_cell(a, b, &guf)) &&
(c3y == u3r_cell(guf, c, d)) ) {
return c3y;
}
else {
return c3n;
}
}
/* u3r_qual(): factor (a) as a qual (b c d e).
*/
inline c3_o
u3r_qual(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e)
{
u3_noun guf;
if ( (c3y == u3r_cell(a, b, &guf)) &&
(c3y == u3r_trel(guf, c, d, e)) ) {
return c3y;
}
else return c3n;
}
/* u3r_quil(): factor (a) as a quil (b c d e f).
*/
inline c3_o
u3r_quil(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e,
u3_noun* f)
{
u3_noun guf;
if ( (c3y == u3r_cell(a, b, &guf)) &&
(c3y == u3r_qual(guf, c, d, e, f)) ) {
return c3y;
}
else return c3n;
}
/* u3r_hext(): factor (a) as a hext (b c d e f g)
*/
inline c3_o
u3r_hext(u3_noun a,
u3_noun* b,
u3_noun* c,
u3_noun* d,
u3_noun* e,
u3_noun* f,
u3_noun* g)
{
u3_noun guf;
if ( (c3y == u3r_cell(a, b, &guf)) &&
(c3y == u3r_quil(guf, c, d, e, f, g)) ) {
return c3y;
}
else return c3n;
}
/* u3r_at(): fragment `a` of `b`, or u3_none.
*/
u3_weak
u3r_at(u3_atom a, u3_weak b);
/* u3r_mean():
**
** Attempt to deconstruct `a` by axis, noun pairs; 0 terminates.
** Axes must be sorted in tree order.
*/
c3_o
u3r_vmean(u3_noun a, va_list ap);
c3_o
u3r_mean(u3_noun a, ...);
/* u3r_mug_both(): Join two mugs.
*/
c3_l
u3r_mug_both(c3_w lef_w, c3_w rit_w);
/* u3r_mug_bytes(): Compute the mug of `buf`, `len`, LSW first.
*/
c3_l
u3r_mug_bytes(const c3_y *buf_y,
c3_w len_w);
/* u3r_mug_c(): Compute the mug of `a`, LSB first.
*/
c3_l
u3r_mug_c(const c3_c *a_c);
/* u3r_mug_cell(): Compute the mug of the cell `[hed tel]`.
*/
c3_l
u3r_mug_cell(u3_noun hed,
u3_noun tel);
/* u3r_mug_chub(): Compute the mug of `num`, LSW first.
*/
c3_l
u3r_mug_chub(c3_d num_d);
/* u3r_mug_words(): 31-bit nonzero MurmurHash3 on raw words.
*/
c3_l
u3r_mug_words(const c3_w* key_w, c3_w len_w);
/* u3r_mug(): statefully mug a noun with 31-bit murmur3.
*/
c3_l
u3r_mug(u3_noun veb);
/* u3r_fing():
**
** Yes iff (a) and (b) are the same copy of the same noun.
** (Ie, by pointer equality - u3r_sing with false negatives.)
*/
c3_o
u3r_fing(u3_noun a,
u3_noun b);
/* u3r_fing_cell():
**
** Yes iff `[p q]` and `b` are the same copy of the same noun.
*/
c3_o
u3r_fing_cell(u3_noun p,
u3_noun q,
u3_noun b);
/* u3r_fing_mixt():
**
** Yes iff `[p q]` and `b` are the same copy of the same noun.
*/
c3_o
u3r_fing_mixt(const c3_c* p_c,
u3_noun q,
u3_noun b);
/* u3r_fing_trel():
**
** Yes iff `[p q r]` and `b` are the same copy of the same noun.
*/
c3_o
u3r_fing_trel(u3_noun p,
u3_noun q,
u3_noun r,
u3_noun b);
/* u3r_fing_qual():
**
** Yes iff `[p q r s]` and `b` are the same copy of the same noun.
*/
c3_o
u3r_fing_qual(u3_noun p,
u3_noun q,
u3_noun r,
u3_noun s,
u3_noun b);
/* u3r_sing(): noun value equality.
**
** Unifies noun pointers on inner roads.
*/
c3_o
u3r_sing(u3_noun a, u3_noun b);
/* u3r_sing_c(): cord/C-string value equivalence.
*/
c3_o
u3r_sing_c(const c3_c* a_c,
u3_noun b);
/* u3r_sing_cell():
**
** Yes iff `[p q]` and `b` are the same noun.
*/
c3_o
u3r_sing_cell(u3_noun p,
u3_noun q,
u3_noun b);
/* u3r_sing_mixt():
**
** Yes iff `[p q]` and `b` are the same noun.
*/
c3_o
u3r_sing_mixt(const c3_c* p_c,
u3_noun q,
u3_noun b);
/* u3r_sing_trel():
**
** Yes iff `[p q r]` and `b` are the same noun.
*/
c3_o
u3r_sing_trel(u3_noun p,
u3_noun q,
u3_noun r,
u3_noun b);
/* u3r_sing_qual():
**
** Yes iff `[p q r s]` and `b` are the same noun.
*/
c3_o
u3r_sing_qual(u3_noun p,
u3_noun q,
u3_noun r,
u3_noun s,
u3_noun b);
/* u3r_nord():
**
** Return 0, 1 or 2 if `a` is below, equal to, or above `b`.
*/
u3_atom
u3r_nord(u3_noun a,
u3_noun b);
/* u3r_mold():
**
** Divide `a` as a mold `[b.[p q] c]`.
*/
c3_o
u3r_mold(u3_noun a,
u3_noun* b,
u3_noun* c);
/* u3r_bite(): retrieve/default $bloq and $step from $bite.
*/
c3_o
u3r_bite(u3_noun bite, u3_atom* bloq, u3_atom *step);
/* u3r_p():
**
** & [0] if [a] is of the form [b *c].
*/
c3_o
u3r_p(u3_noun a,
u3_noun b,
u3_noun* c);
/* u3r_bush():
**
** Factor [a] as a bush [b.[p q] c].
*/
c3_o
u3r_bush(u3_noun a,
u3_noun* b,
u3_noun* c);
/* u3r_pq():
**
** & [0] if [a] is of the form [b *c d].
*/
c3_o
u3r_pq(u3_noun a,
u3_noun b,
u3_noun* c,
u3_noun* d);
/* u3r_pqr():
**
** & [0] if [a] is of the form [b *c *d *e].
*/
c3_o
u3r_pqr(u3_noun a,
u3_noun b,
u3_noun* c,
u3_noun* d,
u3_noun* e);
/* u3r_pqrs():
**
** & [0] if [a] is of the form [b *c *d *e *f].
*/
c3_o
u3r_pqrs(u3_noun a,
u3_noun b,
u3_noun* c,
u3_noun* d,
u3_noun* e,
u3_noun* f);
/* u3r_met():
**
** Return the size of (b) in bits, rounded up to
** (1 << a_y).
**
** For example, (a_y == 3) returns the size in bytes.
** NB: (a_y) must be < 37.
*/
c3_w
u3r_met(c3_y a_y,
u3_atom b);
/* u3r_bit():
**
** Return bit (a_w) of (b).
*/
c3_b
u3r_bit(c3_w a_w,
u3_atom b);
/* u3r_byte():
**
** Return byte (a_w) of (b).
*/
c3_y
u3r_byte(c3_w a_w,
u3_atom b);
/* u3r_bytes():
**
** Copy bytes (a_w) through (a_w + b_w - 1) from (d) to (c).
*/
void
u3r_bytes(c3_w a_w,
c3_w b_w,
c3_y* c_y,
u3_atom d);
/* u3r_bytes_fit():
**
** Copy (len_w) bytes of (a) into (buf_y) if it fits, returning overage.
*/
c3_w
u3r_bytes_fit(c3_w len_w,
c3_y* buf_y,
u3_atom a);
/* u3r_bytes_alloc():
**
** Copy (len_w) bytes starting at (a_w) from (b) into a fresh allocation.
*/
c3_y*
u3r_bytes_alloc(c3_w a_w,
c3_w len_w,
u3_atom b);
/* u3r_bytes_all():
**
** Allocate and return a new byte array with all the bytes of (a),
** storing the length in (len_w).
*/
c3_y*
u3r_bytes_all(c3_w* len_w,
u3_atom a);
/* u3r_chop_bits():
**
** XOR `wid_d` bits from`src_w` at `bif_g` to `dst_w` at `bif_g`
**
** NB: [dst_w] must have space for [bit_g + wid_d] bits
*/
void
u3r_chop_bits(c3_g bif_g,
c3_d wid_d,
c3_g bit_g,
c3_w* dst_w,
const c3_w* src_w);
/* u3r_chop_words():
**
** Into the bloq space of `met`, from position `fum` for a
** span of `wid`, to position `tou`, XOR from `src_w`
** into `dst_w`.
**
** NB: [dst_w] must have space for [tou_w + wid_w] bloqs
*/
void
u3r_chop_words(c3_g met_g,
c3_w fum_w,
c3_w wid_w,
c3_w tou_w,
c3_w* dst_w,
c3_w len_w,
const c3_w* src_w);
/* u3r_chop():
**
** Into the bloq space of `met`, from position `fum` for a
** span of `wid`, to position `tou`, XOR from atom `src`
** into `dst_w`.
**
** NB: [dst_w] must have space for [tou_w + wid_w] bloqs
*/
void
u3r_chop(c3_g met_g,
c3_w fum_w,
c3_w wid_w,
c3_w tou_w,
c3_w* dst_w,
u3_atom src);
/* u3r_mp():
**
** Copy (b) into (a_mp).
*/
void
u3r_mp(mpz_t a_mp,
u3_atom b);
/* u3r_short():
**
** Return short (a_w) of (b).
*/
c3_s
u3r_short(c3_w a_w,
u3_atom b);
/* u3r_word():
**
** Return word (a_w) of (b).
*/
c3_w
u3r_word(c3_w a_w,
u3_atom b);
/* u3r_word_fit():
**
** Fill (out_w) with (a) if it fits, returning success.
*/
c3_t
u3r_word_fit(c3_w* out_w,
u3_atom a);
/* u3r_chub():
**
** Return double-word (a_w) of (b).
*/
c3_d
u3r_chub(c3_w a_w,
u3_atom b);
/* u3r_words():
**
** Copy words (a_w) through (a_w + b_w - 1) from (d) to (c).
*/
void
u3r_words(c3_w a_w,
c3_w b_w,
c3_w* c_w,
u3_atom d);
/* u3r_chubs():
**
** Copy double-words (a_w) through (a_w + b_w - 1) from (d) to (c).
*/
void
u3r_chubs(c3_w a_w,
c3_w b_w,
c3_d* c_d,
u3_atom d);
/* u3r_safe_byte(): validate and retrieve byte.
*/
c3_o
u3r_safe_byte(u3_noun dat, c3_y* out_y);
/* u3r_safe_word(): validate and retrieve word.
*/
c3_o
u3r_safe_word(u3_noun dat, c3_w* out_w);
/* u3r_safe_chub(): validate and retrieve chub.
*/
c3_o
u3r_safe_chub(u3_noun dat, c3_d* out_d);
/* u3r_string(): `a`, a text atom, as malloced C string.
*/
c3_c*
u3r_string(u3_atom a);
/* u3r_tape(): `a`, a list of bytes, as malloced C string.
*/
c3_y*
u3r_tape(u3_noun a);
/* u3r_skip():
**
** Extract a constant from a formula, ignoring
** safe/static hints, doing no computation.
*/
u3_weak
u3r_skip(u3_noun fol);
#endif /* ifndef U3_RETRIEVE_H */