-
Notifications
You must be signed in to change notification settings - Fork 3
/
hebimath.h.in
685 lines (612 loc) · 21 KB
/
hebimath.h.in
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
/*
* hebimath - arbitrary precision arithmetic library
* See LICENSE file for copyright and license details
*/
#ifndef HEBIMATH_H__
#define HEBIMATH_H__
#include <setjmp.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __GNUC__
#define HEBI_IMPORT
#define HEBI_EXPORT __attribute__((__visibility__("default")))
#else
#define HEBI_IMPORT
#define HEBI_EXPORT
#endif
#ifdef HEBI_EXPORT_SYMBOLS
#define HEBI_API HEBI_EXPORT
#else
#define HEBI_API HEBI_IMPORT
#endif
#if defined __GNUC__
#define HEBI_INLINE __extension__ __inline__ HEBI_API
#elif defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
#define HEBI_INLINE inline HEBI_API
#elif defined __cplusplus
#define HEBI_INLINE inline HEBI_API
#else
#define HEBI_INLINE static
#endif
#if defined __GNUC__
#define HEBI_RESTRICT __restrict__
#elif defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
#define HEBI_RESTRICT restrict
#else
#define HEBI_RESTRICT
#endif
#if defined __GNUC__
#define HEBI_ALIGNAS(A) __attribute__((__aligned__(A)))
#define HEBI_NORETURN __attribute__((__noreturn__))
#elif defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L
#define HEBI_ALIGNAS(A) _Alignas(A)
#define HEBI_NORETURN _Noreturn
#else
#define HEBI_ALIGNAS(A)
#define HEBI_NORETURN
#endif
#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
#define HEBI_VLA(T,X,N) T X[N]
#else
#define HEBI_VLA(T,X,N) T *X
#endif
#if defined __GNUC__
#define HEBI_CONST __attribute__((__const__))
#define HEBI_ALLOC __attribute__((__malloc__))
#define HEBI_NOTHROW __attribute__((__nothrow__))
#define HEBI_WARNUNUSED __attribute__((__warn_unused_result__))
#define HEBI_LIKELY(E) __builtin_expect(!!(E),1)
#define HEBI_UNLIKELY(E) __builtin_expect(!!(E),0)
#else
#define HEBI_CONST
#define HEBI_ALLOC
#define HEBI_NOTHROW
#define HEBI_WARNUNUSED
#define HEBI_LIKELY(E) (E)
#define HEBI_UNLIKELY(E) (E)
#endif
>>> dispatch_dynamic
#define HEBI_MULTI_VERSIONING
#define HEBI_MVFUNC(R,N,P,A) \
extern HEBI_API R (* N##_ptr__) P; \
HEBI_INLINE R N P { return (*N##_ptr__) A; }
#define HEBI_MVFUNC_VOID(N,P,A) \
extern HEBI_API void (* N##_ptr__) P; \
HEBI_INLINE void N P { (*N##_ptr__) A; }
<<< dispatch_dynamic
enum hebi_errdom {
HEBI_ERRDOM_HEBI,
HEBI_ERRDOM_ERRNO,
HEBI_ERRDOM_THRD
};
enum {
HEBI_ENONE,
HEBI_EDIVZERO,
HEBI_EZERODIVZERO,
HEBI_EZEROPOWZERO,
HEBI_EASSERTION,
HEBI_EBADLENGTH,
HEBI_EBADSTATE,
HEBI_EBADSYNTAX,
HEBI_EBADVALUE,
HEBI_ENOHWCAPS,
HEBI_ENOMEM,
HEBI_ENOSLOTS
};
struct hebi_error {
enum hebi_errdom he_domain;
int he_code;
};
typedef void (* hebi_errhandler)(void *, const struct hebi_error *);
struct hebi_errstate {
hebi_errhandler hebi_handler__;
void *hebi_arg__;
int hebi_longjmp__;
int hebi_val__;
jmp_buf hebi_env__;
};
HEBI_API void hebi_error_handler(hebi_errhandler handler__, void *arg__);
HEBI_API void hebi_error_jmp(jmp_buf env__, int retval__);
HEBI_API void hebi_error_save(struct hebi_errstate *state__);
HEBI_API void hebi_error_restore(const struct hebi_errstate *state__);
HEBI_API HEBI_NOTHROW int hebi_error_last(struct hebi_error *err__);
HEBI_API HEBI_NORETURN void hebi_error_raise(enum hebi_errdom domain__, int code__);
enum {
HEBI_ALLOC_DEFAULT = 0,
HEBI_ALLOC_SCRATCH = -1,
HEBI_ALLOC_STDLIB = -2,
HEBI_ALLOC_FIXED = -3,
HEBI_ALLOC_INVALID = -4
};
typedef int hebi_allocid;
struct hebi_allocfnptrs {
void *(* ha_alloc)(void *, size_t, size_t);
void (* ha_free)(void *, void *, size_t);
void *ha_arg;
};
HEBI_API HEBI_ALLOC HEBI_WARNUNUSED void *hebi_alloc(hebi_allocid i__, size_t a__, size_t s__);
HEBI_API void hebi_free(hebi_allocid i__, void *p__, size_t s__);
HEBI_API HEBI_ALLOC HEBI_WARNUNUSED void *hebi_allocfp(const struct hebi_allocfnptrs *f__, size_t a__, size_t s__);
HEBI_API void hebi_freefp(const struct hebi_allocfnptrs *f__, void *p__, size_t s__);
HEBI_API HEBI_NOTHROW int hebi_alloc_valid(hebi_allocid i__);
HEBI_API const struct hebi_allocfnptrs *hebi_alloc_query(hebi_allocid *p__, hebi_allocid i__);
HEBI_API hebi_allocid hebi_alloc_add(const struct hebi_allocfnptrs *f__);
HEBI_API void hebi_alloc_remove(hebi_allocid i__);
HEBI_API hebi_allocid hebi_alloc_get_default(void);
HEBI_API hebi_allocid hebi_alloc_set_default(hebi_allocid i__);
HEBI_API hebi_allocid hebi_alloc_get_scratch(void);
HEBI_API hebi_allocid hebi_alloc_set_scratch(hebi_allocid i__);
HEBI_API void hebi_free_scratch(void);
enum {
HEBI_STR_BASE36 = 0x00,
HEBI_STR_BASE36_UPPER = 0x01,
HEBI_STR_BASE36_LOWER = 0x02,
HEBI_STR_BASE62 = 0x03,
HEBI_STR_RFC4648_BASE32 = 0x04,
HEBI_STR_RFC4648_BASE64 = 0x05,
HEBI_STR_ALPHABET_COUNT = 0x06,
HEBI_STR_ALPHABET_MASK = 0x0F,
HEBI_STR_PAD = 0x10,
HEBI_STR_RADIX = 0x20,
HEBI_STR_SIGN = 0x40,
HEBI_STR_TRIM = 0x80
};
struct hebi_psetstrstate
{
const char* hm_str;
size_t hm_len;
size_t hm_start;
size_t hm_end;
size_t hm_cur;
unsigned int hm_alphabet;
unsigned int hm_radix;
int hm_sign;
int hm_errcode;
};
struct hebi_kiss {
uint64_t hebi_xorshift__;
uint64_t hebi_congruential__;
uint64_t hebi_carry__;
uint64_t hebi_single__[1];
uint64_t *hebi_multi__;
size_t hebi_length__;
size_t hebi_index__;
hebi_allocid hebi_allocator__;
};
#define HEBI_KISS_INIT { \
UINT64_C(362436069362436069), \
UINT64_C(123456789987654321), \
0, { UINT64_C(4261283300534326642) }, \
NULL, 1, 0, HEBI_ALLOC_INVALID }
#define HEBI_PACKET_ALIGNMENT 16
#define HEBI_PACKET_BIT 128
#define HEBI_PACKET_LIMBS32 4
#define HEBI_PACKET_LIMBS64 2
#define HEBI_PACKET_MAXLEN (SIZE_MAX / HEBI_PACKET_BIT)
typedef union hebi_packet {
HEBI_ALIGNAS(HEBI_PACKET_ALIGNMENT)
uint32_t hp_limbs32[HEBI_PACKET_LIMBS32];
uint64_t hp_limbs64[HEBI_PACKET_LIMBS64];
} hebi_packet;
>>> dispatch_dynamic
HEBI_MVFUNC_VOID(hebi_pcopy, (hebi_packet *HEBI_RESTRICT r__, const hebi_packet *HEBI_RESTRICT a__, size_t n__), (r__,a__,n__))
HEBI_MVFUNC_VOID(hebi_pmove, (hebi_packet *r__, const hebi_packet *a__, size_t n__), (r__,a__,n__))
HEBI_MVFUNC_VOID(hebi_pzero, (hebi_packet *r__, size_t n__), (r__,n__))
HEBI_MVFUNC(int, hebi_pcmp, (const hebi_packet *a__, const hebi_packet *b__, size_t n__), (a__,b__,n__))
HEBI_MVFUNC(size_t, hebi_pnorm, (const hebi_packet *a__, size_t n__), (a__,n__))
HEBI_MVFUNC(size_t, hebi_pclz, (const hebi_packet *a__, size_t n__), (a__,n__))
HEBI_MVFUNC(size_t, hebi_pctz, (const hebi_packet *a__, size_t n__), (a__,n__))
HEBI_MVFUNC(size_t, hebi_pshl, (hebi_packet *r__, const hebi_packet *a__, size_t bits__, size_t n__), (r__,a__,bits__,n__))
HEBI_MVFUNC(size_t, hebi_pshr, (hebi_packet *r__, const hebi_packet *a__, size_t bits__, size_t n__), (r__,a__,bits__,n__))
HEBI_MVFUNC_VOID(hebi_pnot, (hebi_packet *r__, const hebi_packet *a__, size_t n__), (r__,a__,n__))
HEBI_MVFUNC_VOID(hebi_pand, (hebi_packet *r__, const hebi_packet *a__, const hebi_packet *b__, size_t n__), (r__,a__,b__,n__))
HEBI_MVFUNC_VOID(hebi_por, (hebi_packet *r__, const hebi_packet *a__, const hebi_packet *b__, size_t n__), (r__,a__,b__,n__))
HEBI_MVFUNC_VOID(hebi_pxor, (hebi_packet *r__, const hebi_packet *a__, const hebi_packet *b__, size_t n__), (r__,a__,b__,n__))
HEBI_MVFUNC(uint64_t, hebi_pmulu, (hebi_packet *r__, const hebi_packet *a__, uint64_t b__, size_t n__), (r__,a__,b__,n__))
HEBI_MVFUNC(uint64_t, hebi_pdivremu, (hebi_packet *r__, const hebi_packet *a__, uint64_t b__, size_t n__), (r__,a__,b__,n__))
HEBI_MVFUNC_VOID(hebi_pmul, (hebi_packet *HEBI_RESTRICT r__, const hebi_packet *a__, const hebi_packet *b__, size_t m__, size_t n__), (r__,a__,b__,m__,n__))
HEBI_MVFUNC_VOID(hebi_psqr, (hebi_packet *HEBI_RESTRICT r__, const hebi_packet *HEBI_RESTRICT a__, size_t n__), (r__,a__,n__))
<<< dispatch_dynamic
>>> dispatch_static
HEBI_API void hebi_pcopy(hebi_packet *HEBI_RESTRICT r__, const hebi_packet *HEBI_RESTRICT a__, size_t n__);
HEBI_API void hebi_pmove(hebi_packet *r__, const hebi_packet *a__, size_t n__);
HEBI_API void hebi_pzero(hebi_packet *r__, size_t n__);
HEBI_API int hebi_pcmp(const hebi_packet *a__, const hebi_packet *b__, size_t n__);
HEBI_API size_t hebi_pnorm(const hebi_packet *a__, size_t n__);
HEBI_API size_t hebi_pclz(const hebi_packet *a__, size_t n__);
HEBI_API size_t hebi_pctz(const hebi_packet *a__, size_t n__);
HEBI_API size_t hebi_pshl(hebi_packet *r__, const hebi_packet *a__, size_t bits__, size_t n__);
HEBI_API size_t hebi_pshr(hebi_packet *r__, const hebi_packet *a__, size_t bits__, size_t n__);
HEBI_API void hebi_pnot(hebi_packet *r__, const hebi_packet *a__, size_t n__);
HEBI_API void hebi_pand(hebi_packet *r__, const hebi_packet *a__, const hebi_packet *b__, size_t n__);
HEBI_API void hebi_por(hebi_packet *r__, const hebi_packet *a__, const hebi_packet *b__, size_t n__);
HEBI_API void hebi_pxor(hebi_packet *r__, const hebi_packet *a__, const hebi_packet *b__, size_t n__);
HEBI_API uint64_t hebi_pmulu(hebi_packet *r__, const hebi_packet *a__, uint64_t b__, size_t n__);
HEBI_API uint64_t hebi_pdivremu(hebi_packet *r__, const hebi_packet *a__, uint64_t b__, size_t n__);
HEBI_API void hebi_pmul(hebi_packet *HEBI_RESTRICT r__, const hebi_packet *a__, const hebi_packet *b__, size_t m__, size_t n__);
HEBI_API void hebi_psqr(hebi_packet *HEBI_RESTRICT r__, const hebi_packet *HEBI_RESTRICT a__, size_t n__);
<<< dispatch_static
HEBI_API HEBI_ALLOC HEBI_WARNUNUSED hebi_packet *hebi_palloc(hebi_allocid i__, size_t n__);
HEBI_API void hebi_pfree(hebi_allocid i__, hebi_packet *p__, size_t n__);
HEBI_API HEBI_ALLOC HEBI_WARNUNUSED hebi_packet *hebi_pallocfp(const struct hebi_allocfnptrs *f__, size_t n__);
HEBI_API void hebi_pfreefp(const struct hebi_allocfnptrs *f__, hebi_packet *p__, size_t n__);
HEBI_API uint64_t hebi_pneg(hebi_packet *r__, const hebi_packet *a__, size_t n__);
HEBI_API uint64_t hebi_padd(hebi_packet *r__, const hebi_packet *a__, const hebi_packet *b__, size_t m__, size_t n__);
HEBI_API uint64_t hebi_psub(hebi_packet *r__, const hebi_packet *a__, const hebi_packet *b__, size_t m__, size_t n__);
HEBI_API uint64_t hebi_padda(hebi_packet *r__, const hebi_packet *a__, size_t m__, size_t n__);
HEBI_API uint64_t hebi_psuba(hebi_packet *r__, const hebi_packet *a__, size_t m__, size_t n__);
HEBI_API uint64_t hebi_paddu(hebi_packet *r__, const hebi_packet *a__, uint64_t b__, size_t n__);
HEBI_API uint64_t hebi_psubu(hebi_packet *r__, const hebi_packet *a__, uint64_t b__, size_t n__);
HEBI_API size_t hebi_pdivrem(hebi_packet *HEBI_RESTRICT q__, hebi_packet *HEBI_RESTRICT r__, hebi_packet *HEBI_RESTRICT w__, size_t *HEBI_RESTRICT rn__, const hebi_packet *a__, const hebi_packet *b__, size_t m__, size_t n__);
HEBI_API void hebi_pmul_karatsuba(hebi_packet *HEBI_RESTRICT r__, hebi_packet *HEBI_RESTRICT w__, const hebi_packet *a__, const hebi_packet *b__, size_t m__, size_t n__);
HEBI_API HEBI_CONST size_t hebi_pmul_karatsuba_space(size_t m__, size_t n__);
HEBI_API void hebi_psqr_karatsuba(hebi_packet *HEBI_RESTRICT r__, hebi_packet *HEBI_RESTRICT w__, const hebi_packet *a__, size_t n__);
HEBI_API HEBI_CONST size_t hebi_psqr_karatsuba_space(size_t n__);
HEBI_API size_t hebi_pgetstr(char *HEBI_RESTRICT str__, size_t len__, hebi_packet *HEBI_RESTRICT w__, size_t n__, unsigned int base__, unsigned int flags__);
HEBI_API size_t hebi_pgetstrlen(const hebi_packet *a__, size_t n__, unsigned int base__, unsigned int flags__);
HEBI_API size_t hebi_psetstr(hebi_packet *HEBI_RESTRICT r__, size_t n__, struct hebi_psetstrstate *HEBI_RESTRICT state__);
HEBI_API size_t hebi_psetstrprepare(struct hebi_psetstrstate* HEBI_RESTRICT state__, const char *HEBI_RESTRICT str__, size_t len__, unsigned int base__, unsigned int flags__);
HEBI_API void hebi_prand_kiss(hebi_packet *HEBI_RESTRICT r__, size_t n__, size_t bits__, struct hebi_kiss *HEBI_RESTRICT k__);
HEBI_INLINE
void
hebi_psetu(hebi_packet *r__, uint64_t a__)
{
r__->hp_limbs64[0] = a__;
r__->hp_limbs64[1] = 0;
}
HEBI_INLINE
void
hebi_psetu2(hebi_packet *r__, uint64_t a__, uint64_t b__)
{
r__->hp_limbs64[0] = a__;
r__->hp_limbs64[1] = b__;
}
HEBI_INLINE
void
hebi_psetzero(hebi_packet *r__)
{
r__->hp_limbs64[0] = 0;
r__->hp_limbs64[1] = 0;
}
struct hebi_integer {
hebi_packet *hebi_packs__;
size_t hebi_capacity__;
size_t hebi_used__;
#if SIZE_MAX > UINT32_MAX
int hebi_sign__;
int hebi_allocator__;
#else
int hebi_sign__:8;
int hebi_allocator__:24;
#endif
};
typedef struct hebi_integer hebi_z[1];
typedef struct hebi_integer *hebi_zptr;
typedef const struct hebi_integer *hebi_zsrcptr;
#define HEBI_Z_INIT {{NULL,0,0,0,HEBI_ALLOC_DEFAULT}}
HEBI_API void hebi_zinit_fixed(hebi_zptr HEBI_RESTRICT r__, void *HEBI_RESTRICT p__, size_t n__);
HEBI_API void hebi_zinit_reserve(hebi_zptr r__, size_t n__, hebi_allocid i__);
HEBI_API void hebi_zinit_copy(hebi_zptr HEBI_RESTRICT r__, hebi_zsrcptr HEBI_RESTRICT a__);
HEBI_API void hebi_zinit_copy_fixed(hebi_zptr HEBI_RESTRICT r__, hebi_zsrcptr HEBI_RESTRICT a__, void *HEBI_RESTRICT p__, size_t n__);
HEBI_API void hebi_zinit_copy_reserve(hebi_zptr HEBI_RESTRICT r__, hebi_zsrcptr HEBI_RESTRICT a__, size_t n__, hebi_allocid i__);
HEBI_API void hebi_zinitn(size_t count__, HEBI_VLA(struct hebi_integer, r__, count__));
HEBI_API void hebi_zinitv(size_t count__, HEBI_VLA(const hebi_zptr, r__, count__));
HEBI_API void hebi_zdestroy(hebi_zptr r__);
HEBI_API void hebi_zdestroyn(size_t count__, HEBI_VLA(struct hebi_integer, r__, count__));
HEBI_API void hebi_zdestroyv(size_t count__, HEBI_VLA(const hebi_zptr, r__, count__));
HEBI_API void hebi_zrealloc(hebi_zptr r__, size_t n__);
HEBI_API void hebi_zrealloczero(hebi_zptr r__, size_t n__);
HEBI_API int64_t hebi_zgetsi(hebi_zsrcptr a__);
HEBI_API uint64_t hebi_zgetsu(hebi_zsrcptr a__);
HEBI_API size_t hebi_zgetstr(char *HEBI_RESTRICT str__, size_t len__, hebi_zsrcptr HEBI_RESTRICT a__, unsigned int base__, unsigned int flags__);
HEBI_API size_t hebi_zgetstrlen(hebi_zsrcptr a__, unsigned int base__, unsigned int flags__);
HEBI_API int hebi_zsetstr(hebi_zptr HEBI_RESTRICT r__, const char *HEBI_RESTRICT str__, char **HEBI_RESTRICT strptr__, unsigned int base__, unsigned int flags__);
HEBI_API int hebi_zcmp(hebi_zsrcptr a__, hebi_zsrcptr b__);
HEBI_API int hebi_zcmpmag(hebi_zsrcptr a__, hebi_zsrcptr b__);
HEBI_API int hebi_zcmpi(hebi_zsrcptr a__, int64_t b__);
HEBI_API int hebi_zcmpu(hebi_zsrcptr a__, uint64_t b__);
HEBI_API void hebi_zshl(hebi_zptr r__, hebi_zsrcptr a__, size_t bits__);
HEBI_API void hebi_zshr(hebi_zptr r__, hebi_zsrcptr a__, size_t bits__);
HEBI_API void hebi_ztrunc(hebi_zptr r__, hebi_zsrcptr a__, size_t bits__);
HEBI_API void hebi_znot(hebi_zptr r__, hebi_zsrcptr a__);
HEBI_API void hebi_zand(hebi_zptr r__, hebi_zsrcptr a__, hebi_zsrcptr b__);
HEBI_API void hebi_zor(hebi_zptr r__, hebi_zsrcptr a__, hebi_zsrcptr b__);
HEBI_API void hebi_zxor(hebi_zptr r__, hebi_zsrcptr a__, hebi_zsrcptr b__);
HEBI_API void hebi_zaddu(hebi_zptr r__, hebi_zsrcptr a__, uint64_t b__);
HEBI_API void hebi_zsubu(hebi_zptr r__, hebi_zsrcptr a__, uint64_t b__);
HEBI_API void hebi_zmulu(hebi_zptr r__, hebi_zsrcptr a__, uint64_t b__);
HEBI_API int64_t hebi_zdivremi(hebi_zptr r__, hebi_zsrcptr a__, int64_t b__);
HEBI_API uint64_t hebi_zdivremu(hebi_zptr r__, hebi_zsrcptr a__, uint64_t b__);
HEBI_API void hebi_zadd(hebi_zptr r__, hebi_zsrcptr a__, hebi_zsrcptr b__);
HEBI_API void hebi_zaddmag(hebi_zptr r__, hebi_zsrcptr a__, hebi_zsrcptr b__);
HEBI_API void hebi_zsub(hebi_zptr r__, hebi_zsrcptr a__, hebi_zsrcptr b__);
HEBI_API void hebi_zsubmag(hebi_zptr r__, hebi_zsrcptr a__, hebi_zsrcptr b__);
HEBI_API void hebi_zmul(hebi_zptr r__, hebi_zsrcptr a__, hebi_zsrcptr b__);
HEBI_API void hebi_zsqr(hebi_zptr r__, hebi_zsrcptr a__);
HEBI_API void hebi_zdivrem(hebi_zptr q__, hebi_zptr r__, hebi_zsrcptr a__, hebi_zsrcptr b__);
HEBI_API void hebi_zrand_kiss(hebi_zptr HEBI_RESTRICT r__, size_t n__, struct hebi_kiss *HEBI_RESTRICT k__);
HEBI_INLINE
void
hebi_zinit_allocator(hebi_zptr r__, hebi_allocid i__)
{
r__->hebi_packs__ = NULL;
r__->hebi_capacity__ = 0;
r__->hebi_used__ = 0;
r__->hebi_sign__ = 0;
r__->hebi_allocator__ = i__;
}
HEBI_INLINE
void
hebi_zinit(hebi_zptr r__)
{
hebi_zinit_allocator(r__, HEBI_ALLOC_DEFAULT);
}
HEBI_INLINE
void
hebi_zinit_move(hebi_zptr HEBI_RESTRICT r__, hebi_zptr HEBI_RESTRICT s__)
{
*r__ = *s__;
hebi_zinit(s__);
}
HEBI_INLINE
void
hebi_zreserve(hebi_zptr r__, size_t n__)
{
if (n__ > r__->hebi_capacity__)
hebi_zrealloc(r__, n__);
}
HEBI_INLINE
void
hebi_zshrink(hebi_zptr r__)
{
if (r__->hebi_sign__ && r__->hebi_used__ < r__->hebi_capacity__)
hebi_zrealloc(r__, r__->hebi_used__);
}
HEBI_INLINE
void
hebi_zswap(hebi_zptr r__, hebi_zptr s__)
{
struct hebi_integer t__;
t__.hebi_packs__ = r__->hebi_packs__;
t__.hebi_capacity__ = r__->hebi_capacity__;
t__.hebi_used__ = r__->hebi_used__;
t__.hebi_sign__ = r__->hebi_sign__;
t__.hebi_allocator__ = r__->hebi_allocator__;
r__->hebi_packs__ = s__->hebi_packs__;
r__->hebi_capacity__ = s__->hebi_capacity__;
r__->hebi_used__ = s__->hebi_used__;
r__->hebi_sign__ = s__->hebi_sign__;
r__->hebi_allocator__ = s__->hebi_allocator__;
s__->hebi_packs__ = t__.hebi_packs__;
s__->hebi_capacity__ = t__.hebi_capacity__;
s__->hebi_used__ = t__.hebi_used__;
s__->hebi_sign__ = t__.hebi_sign__;
s__->hebi_allocator__ = t__.hebi_allocator__;
}
HEBI_INLINE
hebi_allocid
hebi_zallocator(hebi_zsrcptr a__)
{
return a__->hebi_allocator__;
}
HEBI_INLINE
size_t
hebi_zcapacity(hebi_zsrcptr a__)
{
return a__->hebi_capacity__;
}
HEBI_INLINE
size_t
hebi_zused(hebi_zsrcptr a__)
{
return a__->hebi_sign__ ? a__->hebi_used__ : 0;
}
HEBI_INLINE
int
hebi_zsign(hebi_zsrcptr a__)
{
return a__->hebi_sign__;
}
HEBI_INLINE
int
hebi_zzero(hebi_zsrcptr a__)
{
return !a__->hebi_sign__;
}
HEBI_INLINE
int
hebi_zeven(hebi_zsrcptr a__)
{
return !a__->hebi_sign__ || (~a__->hebi_packs__->hp_limbs64[0] & 1);
}
HEBI_INLINE
int
hebi_zodd(hebi_zsrcptr a__)
{
return a__->hebi_sign__ && (a__->hebi_packs__->hp_limbs64[0] & 1);
}
HEBI_INLINE
size_t
hebi_zbits(hebi_zsrcptr a__)
{
size_t b__;
if (HEBI_UNLIKELY(!a__->hebi_sign__))
return 0;
b__ = HEBI_PACKET_BIT * a__->hebi_used__;
return b__ - hebi_pclz(a__->hebi_packs__, a__->hebi_used__);
}
HEBI_INLINE
size_t
hebi_zlsb(hebi_zsrcptr a__)
{
if (HEBI_UNLIKELY(!a__->hebi_sign__))
return 0;
return hebi_pctz(a__->hebi_packs__, a__->hebi_used__);
}
HEBI_INLINE
void
hebi_zset_copy(hebi_zptr HEBI_RESTRICT r__, hebi_zsrcptr HEBI_RESTRICT a__)
{
size_t n__;
if (a__->hebi_sign__) {
if ((n__ = a__->hebi_used__) > r__->hebi_capacity__)
hebi_zrealloczero(r__, n__);
hebi_pcopy(r__->hebi_packs__, a__->hebi_packs__, n__);
r__->hebi_used__ = n__;
r__->hebi_sign__ = a__->hebi_sign__;
} else {
r__->hebi_sign__ = 0;
}
}
HEBI_INLINE
void
hebi_zset_move(hebi_zptr HEBI_RESTRICT r__, hebi_zptr HEBI_RESTRICT s__)
{
if (r__->hebi_packs__)
hebi_zdestroy(s__);
*r__ = *s__;
hebi_zinit(s__);
}
HEBI_INLINE
void hebi_zset(hebi_zptr r__, hebi_zsrcptr a__)
{
if (r__ != a__)
hebi_zset_copy(r__, a__);
}
HEBI_INLINE
void hebi_zseti(hebi_zptr r__, int64_t a__)
{
if (HEBI_LIKELY(a__)) {
if (HEBI_UNLIKELY(!r__->hebi_capacity__))
hebi_zrealloczero(r__, 1);
hebi_psetu(r__->hebi_packs__,
(uint64_t)(a__ < 0 ? -a__ : a__));
r__->hebi_used__ = 1;
r__->hebi_sign__ = a__ < 0 ? -1 : 1;
} else {
r__->hebi_sign__ = 0;
}
}
HEBI_INLINE
void hebi_zsetu(hebi_zptr r__, uint64_t a__)
{
if (HEBI_LIKELY(a__)) {
if (HEBI_UNLIKELY(!r__->hebi_capacity__))
hebi_zrealloczero(r__, 1);
hebi_psetu(r__->hebi_packs__, a__);
r__->hebi_used__ = 1;
r__->hebi_sign__ = 1;
} else {
r__->hebi_sign__ = 0;
}
}
HEBI_INLINE
void
hebi_zsetzero(hebi_zptr r__)
{
r__->hebi_sign__ = 0;
}
HEBI_INLINE
int64_t
hebi_zgeti(hebi_zsrcptr a__)
{
int64_t r__;
int s__;
if (HEBI_UNLIKELY(!(s__ = a__->hebi_sign__)))
return 0;
r__ = (int64_t)a__->hebi_packs__->hp_limbs64[0];
return s__ < 0 ? -r__ : r__;
}
HEBI_INLINE
uint64_t
hebi_zgetu(hebi_zsrcptr a__)
{
if (HEBI_UNLIKELY(!a__->hebi_sign__))
return 0;
return a__->hebi_packs__->hp_limbs64[0];
}
HEBI_INLINE
void
hebi_zabs(hebi_zptr r__, hebi_zsrcptr a__)
{
if (r__ != a__)
hebi_zset_copy(r__, a__);
r__->hebi_sign__ &= 1;
}
HEBI_INLINE
void
hebi_zneg(hebi_zptr r__, hebi_zsrcptr a__)
{
if (r__ != a__)
hebi_zset_copy(r__, a__);
r__->hebi_sign__ = -r__->hebi_sign__;
}
HEBI_INLINE
void
hebi_zaddi(hebi_zptr r__, hebi_zsrcptr a__, int64_t b__)
{
if (b__ < 0)
hebi_zsubu(r__, a__, (uint64_t)(-b__));
else
hebi_zaddu(r__, a__, (uint64_t)b__);
}
HEBI_INLINE
void
hebi_zsubi(hebi_zptr r__, hebi_zsrcptr a__, int64_t b__)
{
if (b__ < 0)
hebi_zaddu(r__, a__, (uint64_t)(-b__));
else
hebi_zsubu(r__, a__, (uint64_t)b__);
}
HEBI_INLINE
void
hebi_zmuli(hebi_zptr r__, hebi_zsrcptr a__, int64_t b__)
{
hebi_zmulu(r__, a__, (uint64_t)(b__ < 0 ? -b__ : b__));
if (b__ < 0)
r__->hebi_sign__ = -r__->hebi_sign__;
}
HEBI_INLINE
void
hebi_zdivi(hebi_zptr q__, hebi_zsrcptr a__, int64_t b__)
{
(void)hebi_zdivremi(q__, a__, b__);
}
HEBI_INLINE
void
hebi_zdivu(hebi_zptr q__, hebi_zsrcptr a__, uint64_t b__)
{
(void)hebi_zdivremu(q__, a__, b__);
}
HEBI_INLINE
int64_t
hebi_zremi(hebi_zsrcptr a__, int64_t b__)
{
return hebi_zdivremi(NULL, a__, b__);
}
HEBI_INLINE
uint64_t
hebi_zremu(hebi_zsrcptr a__, uint64_t b__)
{
return hebi_zdivremu(NULL, a__, b__);
}
HEBI_INLINE
void
hebi_zdiv(hebi_zptr q__, hebi_zsrcptr a__, hebi_zsrcptr b__)
{
return hebi_zdivrem(q__, NULL, a__, b__);
}
HEBI_INLINE
void
hebi_zrem(hebi_zptr r__, hebi_zsrcptr a__, hebi_zsrcptr b__)
{
return hebi_zdivrem(NULL, r__, a__, b__);
}
#ifdef __cplusplus
}
#endif
#endif