core-math-sys 1.4.0

Generate bindings to CORE-MATH
Documentation
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
/* Correctly-rounded cosine function for the IEEE-754 binary128 format

Copyright (c) 2026 Chen-Pang He <jdh863@gmail.com>

This file is part of the CORE-MATH project
(https://core-math.gitlabpages.inria.fr/).

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

/* This file is sinq.c with the entry point switched to the cosine: keep
   the two in sync.

   This is a C port of the binary128 sine/cosine of metallic-rs
   (https://github.com/jdh8/metallic-rs, src/f128_/trig.rs), extended from
   round-to-nearest to the four IEEE rounding modes. AI agents assisted with
   the port and subsequent revisions. Methodology and related agent skills:
   https://jdh8.org/how-to-program-math-functions/
   https://github.com/jdh8/metallic-rs/blob/main/.claude/skills/program-math-functions/SKILL.md
   https://github.com/jdh8/metallic/blob/main/.claude/skills/program-math-functions/SKILL.md
   Review: https://gitlab.inria.fr/core-math/core-math/-/work_items/47
   Fast-path error argument: ../sin/sinq_error.md.

   The sine is odd and the
   cosine even, so both run one pipeline on |x| in unsigned fixed point:

   1. Reduce.  x*2/pi is formed as the exact product of the 113-bit
      significand with a window of 2/pi cut from 64-bit limbs at the exponent
      (Payne-Hanek): six limbs on the fast path, ten on the accurate one.
      Bits of 2/pi above the window contribute multiples of 4 to x*2/pi and
      drop, bits below it stay under the last bit kept; what is left is the
      quadrant and a fraction, 192 bits wide on the fast path and 448 on the
      accurate one.
   2. Split.  Quadrant and fraction round to n = round(256*x/pi) mod 512: the
      quadrant n>>7 and a breakpoint j = n & 127, leaving a signed residual g
      with |g| <= 1/256, so theta = g*pi/2 stays below pi/512 < 2^-7.34.  |g|
      is normalized into a floating fraction at its own exponent, so the
      residual keeps full relative precision however deep the cancellation;
      the fast path only insists on 136 of its 192 bits (MAX_LZ) and hands
      anything closer to a multiple of pi/2 over.  No binary128 comes within
      2^-124 of a multiple of pi/2 (sin.sage), so the 448-bit residual keeps
      over 300 bits.
   3. Evaluate.  sin(theta) = theta*(1 - u*(A - u*B)) and 1 - cos(theta) =
      u*(1/2 - u*(E - u*O)) for u = theta^2, with A, B, E, O the even and odd
      halves of polynomials in v = u^2: a degree-four minimax for sine and
      six Taylor terms for cosine at the fast width, eighteen Taylor
      coefficients each at the accurate width, summed in three tiers (128,
      256 and 384 bits) since the high-order terms ride powers of u that
      bury a narrower width's slack; the sums are within 2^-296 of exact.
   4. Recombine.  With A = j*pi/256 + theta, sin|x| is +-sin A or +-cos A by
      quadrant, and sin A = S_j*cos(theta) + C_j*sin(theta), cos A =
      C_j*cos(theta) - S_j*sin(theta) from tables of sin(j*pi/256) and
      cos(j*pi/256), in a frame at 2^-256 (fast) or 2^-384 (accurate).  j = 0
      with the sine wanted keeps the floating form of sin(theta) all the way.
   5. Round.  The fast path keeps a 15-bit guard below the 113-bit
      significand; its error is below ZIV_GATE units of that guard
      (see ../sin/sinq_error.md and sinq_tables.py), so it decides
      whenever the guard is farther than the gate from the rounding boundary:
      the midpoint for round-to-nearest, the grid point for directed modes.
      Everything else goes to the 384-bit path, which rounds outright.

   Below |x| = 2^-8 (DIRECT) there is nothing to reduce: theta = x exactly.
   Below 2^-57 (TINY) the cubic and quadratic terms sit under half an ulp, so
   sin(x) = x and cos(x) = 1 in round-to-nearest, and one ulp toward zero in
   the directed modes that round toward zero.

   Constants (limbs of 2/pi and pi/2, table values, Taylor terms) and the
   independent minimax SIN_FAST are generated by ../sin/sinq_tables.py;
   regenerate rather than edit. */

#define _GNU_SOURCE /* to define ...f128 functions */

#ifdef CORE_MATH_SUPPORT_ERRNO
#include <errno.h>
#endif
#include <fenv.h> // for FE_INEXACT, FE_INVALID, FE_UNDERFLOW
#include <stdint.h>
#include <math.h>
#ifdef __x86_64__
#include <x86intrin.h>
#endif

// Warning: clang also defines __GNUC__
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#endif

#pragma STDC FENV_ACCESS ON

typedef uint64_t u64;
typedef int64_t i64;
typedef unsigned __int128 u128;
typedef union {
  u128 a;
  u64 b[2];
  __float128 f;
} b128u128_u;
typedef struct { u128 l[2]; } u256; // little-endian 128-bit limbs
typedef struct { u128 l[3]; } u384;

/* Rounding mode and exception flags.  On x86-64 read MXCSR once, accumulate
   the flags and write it back only when something changed, as the other
   binary128 functions do; elsewhere fall back to fenv.h. */
#ifdef __x86_64__
#define RM_NEAREST _MM_ROUND_NEAREST
#define RM_UP _MM_ROUND_UP
#define RM_DOWN _MM_ROUND_DOWN
#define FENV_GET(rm, flags) do { flags = _mm_getcsr(); rm = flags & _MM_ROUND_MASK; } while (0)
#define FENV_RAISE(oflags, flags) do { if (__builtin_expect(oflags != flags, 0)) _mm_setcsr(flags); } while (0)
#else
#define RM_NEAREST FE_TONEAREST
#define RM_UP FE_UPWARD
#define RM_DOWN FE_DOWNWARD
#define FENV_GET(rm, flags) do { flags = 0; rm = fegetround(); } while (0)
#define FENV_RAISE(oflags, flags) do { if (__builtin_expect(oflags != flags, 0)) feraiseexcept(flags); } while (0)
#endif

// In a directed mode, whether the result of this sign rounds away from zero.
static inline int round_away(unsigned rm, u128 sign) {
  return sign ? rm == RM_DOWN : rm == RM_UP;
}

#define SIGN_MASK ((u128)1 << 127)
#define EXP_MASK ((u128)0x7fff << 112)
#define MANTISSA_MASK (((u128)1 << 112) - 1)
#define IMPLICIT_BIT ((u128)1 << 112)
#define QUIET_BIT ((u128)1 << 111)
#define BIAS 0x3fff
#define ONE ((u128)BIAS << 112)

/* Below 2^-57 the sine rounds to x and the cosine to 1: x^3/6 < 2^-173.5 sits
   under the half ulp 2^-171 of x, and x^2/2 < 2^-115 under the half ulp
   2^-114 below 1. */
#define TINY ((u128)(BIAS - 57) << 112)

// Below 2^-8 the argument is its own reduced angle: no window, no table.
#define DIRECT (-8)

/* Leading zeros of the fast path's 192-bit residual past which it gives up:
   136 bits are left at the limit, against a relative need of about 130. */
#define MAX_LZ 56

// Discarded bits below the fast path's 113-bit significand within its top limb.
#define GUARD 15
#define GUARD_MASK ((1u << GUARD) - 1)
#define GUARD_HALF (1u << (GUARD - 1))

/* Half-width of the window around a rounding boundary the fast path refuses
   to decide, in units of 2^(e2-128), or 2^-15 ulp in the returned frame.
   The analytic bound is < 15.8 units, including reduction, evaluation and
   recombination; see ../sin/sinq_error.md. This covers midpoint boundaries
   in round-to-nearest and grid boundaries in directed modes. Sampled MPFR
   error measurements are additional checks, not a proof of the bound. */
#define ZIV_GATE 16u

static inline __float128 asf128(u128 t) {
#if defined(__SSE4_1__) && !defined(__clang__)
  // put u128 into xmm register
  __m128i m = {0, 0};
  m = _mm_insert_epi64 (m, t, 0);
  m = _mm_insert_epi64 (m, t>>64, 1);
  __float128 r;
  asm("": "=x"(r): "0"(m));
  return r;
#else
  b128u128_u u = {.a = t};
  return u.f;
#endif
}

static inline u128 asu128(__float128 z) {
#if defined(__SSE4_1__) && !defined(__clang__)
  __m128i t;
  asm("" : "=x" (t) :"0" (z));
  u64 h = _mm_extract_epi64(t, 1);
  u64 l = _mm_extract_epi64(t, 0);
  return ((u128)h<<64)|l;
#else
  b128u128_u u = {.f = z};
  return u.a;
#endif
}

/* ---------- tables (../sin/sinq_tables.py) ---------- */

#define U128(hi, lo) (((u128)(hi) << 64) | (u64)(lo))
// 2/pi as 64-bit limbs: FRAC_2_PI[i] holds bits 64i+1 ..= 64i+64 after the binary point,
// most significant limb first (264 limbs: a window of 10 limbs must reach exponent 16383).
static const u64 FRAC_2_PI[264] = {
  0xa2f9836e4e441529, 0xfc2757d1f534ddc0, 0xdb6295993c439041, 0xfe5163abdebbc561,
  0xb7246e3a424dd2e0, 0x06492eea09d1921c, 0xfe1deb1cb129a73e, 0xe88235f52ebb4484,
  0xe99c7026b45f7e41, 0x3991d639835339f4, 0x9c845f8bbdf9283b, 0x1ff897ffde05980f,
  0xef2f118b5a0a6d1f, 0x6d367ecf27cb09b7, 0x4f463f669e5fea2d, 0x7527bac7ebe5f17b,
  0x3d0739f78a5292ea, 0x6bfb5fb11f8d5d08, 0x56033046fc7b6bab, 0xf0cfbc209af4361d,
  0xa9e391615ee61b08, 0x6599855f14a06840, 0x8dffd8804d732731, 0x06061556ca73a8c9,
  0x60e27bc08c6b47c4, 0x19c367cddce8092a, 0x8359c4768b961ca6, 0xddaf44d15719053e,
  0xa5ff07053f7e33e8, 0x32c2de4f98327dbb, 0xc33d26ef6b1e5ef8, 0x9f3a1f35caf27f1d,
  0x87f121907c7c246a, 0xfa6ed5772d30433b, 0x15c614b59d19c3c2, 0xc4ad414d2c5d000c,
  0x467d862d71e39ac6, 0x9b0062337cd2b497, 0xa7b4d55537f63ed7, 0x1810a3fc764d2a9d,
  0x64abd770f87c6357, 0xb07ae715175649c0, 0xd9d63b3884a7cb23, 0x24778ad623545ab9,
  0x1f001b0af1dfce19, 0xff319f6a1e666157, 0x9947fbacd87f7eb7, 0x652289e83260bfe6,
  0xcdc4ef09366cd43f, 0x5dd7de16de3b5892, 0x9bde2822d2e88628, 0x4d58e232cac616e3,
  0x08cb7de050c017a7, 0x1df35be01834132e, 0x6212830148835b8e, 0xf57fb0adf2e91e43,
  0x4a48d36710d8ddaa, 0x425faece616aa428, 0x0ab499d3f2a6067f, 0x775c83c2a3883c61,
  0x78738a5a8cafbdd7, 0x6f63a62dcbbff4ef, 0x818d67c12645ca55, 0x36d9cad2a8288d61,
  0xc277c9121426049b, 0x4612c459c444c5c8, 0x91b24df31700ad43, 0xd4e5492910d5fdfc,
  0xbe00cc941eeece70, 0xf53e1380f1ecc3e7, 0xb328f8c79405933e, 0x71c1b3092ef3450b,
  0x9c12887b20ab9fb5, 0x2ec292472f327b6d, 0x550c90a7721fe76b, 0x96cb314a1679e279,
  0x4189dff49794e884, 0xe6e29731996bed88, 0x365f5f0efdbbb49a, 0x486ca46742727132,
  0x5d8db8159f09e5bc, 0x25318d3974f71c05, 0x30010c0d68084b58, 0xee2c90aa4702e774,
  0x24d6bda67df77248, 0x6eef169fa6948ef6, 0x91b45153d1f20acf, 0x3398207e4bf56863,
  0xb25f3edd035d407f, 0x8985295255c06437, 0x10d86d324832754c, 0x5bd4714e6e5445c1,
  0x090b69f52ad56614, 0x9d072750045ddb3b, 0xb4c576ea17f9877d, 0x6b49ba271d296996,
  0xacccc65414ad6ae2, 0x9089d98850722cbe, 0xa4049407777030f3, 0x27fc00a871ea49c2,
  0x663de06483dd9797, 0x3fa3fd94438c860d, 0xde41319d39928c70, 0xdde7b7173bdf082b,
  0x3715a0805c93805a, 0x921110d8e80faf80, 0x6c4bffdb0f903876, 0x185915a562bbcb61,
  0xb989c7bd401004f2, 0xd2277549f6b6ebbb, 0x22dbaa140a2f2689, 0x768364333b091a94,
  0x0eaa3a51c2a31dae, 0xedaf12265c4dc26d, 0x9c7a2d9756c0833f, 0x03f6f0098c402b99,
  0x316d07b43915200c, 0x5bc3d8c492f54bad, 0xc6a5ca4ecd37a736, 0xa9e69492ab6842dd,
  0xde6319ef8c76528b, 0x6837dbfcaba1ae31, 0x15dfa1ae00dafb0c, 0x664d64b705ed3065,
  0x29bf56573aff47b9, 0xf96af3be75df9328, 0x3080abf68c6615cb, 0x040622fa1de4d9a4,
  0xb33d8f1b5709cd36, 0xe9424ea4be13b523, 0x331aaaf0a8654fa5, 0xc1d20f3f0bcd785b,
  0x76f923048b7b7217, 0x8953a6c6e26e6f00, 0xebef584a9bb7dac4, 0xba66aacfcf761d02,
  0xd12df1b1c1998c77, 0xadc3da4886a05df7, 0xf480c62ff0ac9aec, 0xddbc5c3f6dded01f,
  0xc790b6db2a3a25a3, 0x9aaf009353ad0457, 0xb6b42d297e804ba7, 0x07da0eaa76a1597b,
  0x2a12162db7dcfde5, 0xfafedb89fdbe896c, 0x76e4fca90670803e, 0x156e85ff87fd073e,
  0x2833676186182aea, 0xbd4dafe7b36e6d8f, 0x3967955bbf3148d7, 0x8416df30432dc735,
  0x6125ce70c9b8cb30, 0xfd6cbfa200a4e46c, 0x05a0dd5a476f21d2, 0x1262845cb9496170,
  0xe0566b0152993755, 0x50b7d51ec4f1335f, 0x6e13e4305da92e85, 0xc3b21d3632a1a4b7,
  0x08d4b1ea21f716e4, 0x698f77ff2780030c, 0x2d408da0cd4f99a5, 0x20d3a2b30a5d2f42,
  0xf9b4cbda11d0be7d, 0xc1db9bbd17ab81a2, 0xca5c6a0817552e55, 0x0027f0147f8607e1,
  0x640b148d4196debe, 0x872afddab6256b34, 0x897bfef3059ebfb9, 0x4f6a68a82a4a5ac4,
  0x4fbcf82d985ad795, 0xc7f48d4d0da63a20, 0x5f57a4b13f149538, 0x800120cc86dd71b6,
  0xdec9f560bf11654d, 0x6b0701acb08cd0c0, 0xb24855510efb1ec3, 0x72953b06a33540c0,
  0x7bdc06cc45e0fa29, 0x4ec8cad641f3e8de, 0x647cd8649b31bed9, 0xc397a4d45877c5e3,
  0x6913daf03c3aba46, 0x18465f7555f5bdd2, 0xc6926e5d2eaced44, 0x0e423e1c87c461e9,
  0xfd29f3d6e7ca7c22, 0x35916fc5e0088dd7, 0xffe26a6ec6fdb0c1, 0x0893745d7cb2ad6b,
  0x9d6ecd7b723e6a11, 0xc6a9cff7df7329ba, 0xc9b55100b70db2e2, 0x24ba74607de58ad8,
  0x742c150d0c188194, 0x667e162901767a9f, 0xbefdfdef4556367e, 0xd913d9ecb9ba8bfc,
  0x97c427a831c36ef1, 0x36c59456a8d8b5a8, 0xb40ecccf2d891234, 0x576f89562ce3ce99,
  0xb920d6aa5e6b9c2a, 0x3ecc5f114a0bfdfb, 0xf4e16d3b8e2c86e2, 0x84d4e9a9b4fcd1ee,
  0xefc9352e61392f44, 0x2138c8d91b0afc81, 0x6a4afbd81c2f84b4, 0x538c994ecc2254dc,
  0x552ad6c6c096190b, 0xb8701a649569605a, 0x26ee523f0f117f11, 0xb5f4f5cbfc2dbc34,
  0xeebc34cc5de8605e, 0xdd9b8e67ef3392b8, 0x17c99b5861bc57e1, 0xc68351103ed84871,
  0xdddd1c2da118af46, 0x2c21d7f359987ad9, 0xc0549efa864ffc06, 0x56ae79e536228922,
  0xad38dc9367aae855, 0x3826829be7caa40d, 0x51b133990ed7a948, 0x0569f0b265a7887f,
  0x974c8836d1f9b392, 0x214a827b21cf98dc, 0x9f405547dc3a74e1, 0x42eb67df9dfe5fd4,
  0x5ea4677b7aacbaa2, 0xf65523882b55ba41, 0x086e59862a218347, 0x39e6e389d49ee540,
  0xfb49e956ffca0f1c, 0x8a59c52bfa94c5c1, 0xd3cfc50fae5adb86, 0xc5476243853b8621,
  0x94792c8761107b4c, 0x2a1a2c8012bf4390, 0x2688893c78e4c4a8, 0x7bdbe5c23ac4eaf4,
  0x268a67f7bf920d2b, 0xa365b1933d0b7cbd, 0xdc51a463dd27dde1, 0x6919949a9529a828,
  0xce68b4ed09209f44, 0xca984e638270237c, 0x7e32b90f8ef5a7e7, 0x561408f1212a9db5,
  0x4d7e6f5119a5abf9, 0xb5d6df8261dd9602, 0x36169f3ac4a1a283, 0x6ded727a8d39a9b8,
  0x825c326b5b2746ed, 0x34007700d255f4fc, 0x4d59018071e0e13f, 0x89b295f364a8f1ae,
  0xa74b38fc4ceab2bb, 0x47270babc3a734ba, 0x6052dd34f8563aeb, 0x7e8a31bb365895b7,
};
// pi/2 * 2^127, rounded to nearest.
static const u128 PIO2_128 = U128(0xc90fdaa22168c234, 0xc4c6628b80dc1cd1);
// pi/2 * 2^383, rounded to nearest, little-endian 128-bit limbs.
static const u128 PIO2_384[3] = {U128(0x514a08798e3404dd, 0xef9519b3cd3a431b), U128(0x29024e088a67cc74, 0x020bbea63b139b22), U128(0xc90fdaa22168c234, 0xc4c6628b80dc1cd1)};
// [sin(j*pi/256), cos(j*pi/256)] * 2^384, rounded to nearest, little-endian 128-bit limbs;
// cos(0) saturates to 2^384 - 1.
static const u128 SINCOS[128][2][3] = {
  {{U128(0x0000000000000000, 0x0000000000000000), U128(0x0000000000000000, 0x0000000000000000), U128(0x0000000000000000, 0x0000000000000000)}, {U128(0xffffffffffffffff, 0xffffffffffffffff), U128(0xffffffffffffffff, 0xffffffffffffffff), U128(0xffffffffffffffff, 0xffffffffffffffff)}},
  {{U128(0xe36885f0fd7b869a, 0x35b064282559db48), U128(0xf0257406fa2c2f25, 0x06e03a898ebf83db), U128(0x03243a3f9bd8f08c, 0xc3c75f41b6ce7aa7)}, {U128(0xc7e9f8e9e7f57263, 0xfc29714c032279b1), U128(0xff8d89566a2181db, 0xab255bf76857a287), U128(0xfffb10b4dc96dabb, 0xb47903f7a19f8ee1)}},
  {{U128(0xa8390bc33658b7d6, 0x4cf0323d7cbf0eb5), U128(0x62652f9e1aa508ac, 0x0cf31ac036de81c6), U128(0x0648557de8d99f7e, 0x4e29cf6e5fed0679)}, {U128(0xab5dc1a419785764, 0xb707f21199fe9209), U128(0x568fb69282182f32, 0xef6f30da8fe307e8), U128(0xffec4304266865d9, 0x5657552366961732)}},
  {{U128(0x02881e6ab9834899, 0xfc79c4beab7508d1), U128(0x036d93dfef35cbd6, 0xe4df8597c03c05d2), U128(0x096c32baca2ae68b, 0x437b2dd49d5fca3c)}, {U128(0x831dcf0e30781c87, 0xde3ad81725b27622), U128(0x30bd6fbe3cdfb585, 0x2fb15c7502f6de78), U128(0xffd3977ff7bae4e9, 0x664649b4d541b9c5)}},
  {{U128(0xa8902b24fdd1fb46, 0x72e72bf5af6010c4), U128(0x1216769fcda0d0eb, 0xf36fc97b1fef1fde), U128(0x0c8fb2f886ec09f3, 0x76a17954b2b7c517)}, {U128(0x1bf58f6a90d520d5, 0xa94ff9046c34d4e4), U128(0xcf53e4baa403250b, 0x5750700a6df26524), U128(0xffb10f1bcb6bef1d, 0x421e8edaaf59453d)}},
  {{U128(0x9684b763f409ea33, 0x3d124a454239f979), U128(0xfb3691daa4fc8f5c, 0xec1cd0d9c7c3b699), U128(0x0fb2b73cfc106ff6, 0x8f0a0e36a000c734)}, {U128(0xa7ba18398767930a, 0x1018736805f7bf87), U128(0x3cb04da7e6b0bbc3, 0x369ca2e0608be070), U128(0xff84ab2c738d6a03, 0x519c314973ccae6b)}},
  {{U128(0x83d904a423e340df, 0xfd367461499ea9fe), U128(0x825261edb7445e11, 0x5b93892877537a40), U128(0x12d52092ce19f5cc, 0xbeeeae8129a786b8)}, {U128(0x3b7c57bc5c735193, 0xf1419bcc0ff7192e), U128(0x91cf862e7fad71ee, 0x964aad96d95ae9fb), U128(0xff4e6d680c41d0a9, 0x0f668633f1ab8589)}},
  {{U128(0x46f9278d36fb08fe, 0xdbff81d194ca39fa), U128(0xa360bea372e455ef, 0x2eafd08a7f7fc6c4), U128(0x15f6d00a9aa418c1, 0x7fba5f8126cb2985)}, {U128(0x7fa801bea5d4c3cb, 0xbd57ce6223df6540), U128(0x5f063781804a271a, 0x287f3f198f7cd53c), U128(0xff0e57e5ead848d1, 0x1f1901544271c3f8)}},
  {{U128(0x01af208bbd6f4012, 0xa801974079e03914), U128(0x21030810a8ddd093, 0xd9abe105fe0fbea3), U128(0x1917a6bc29b42be1, 0xd8e72d912977ee71)}, {U128(0xa0e9ee7a85258e45, 0x485224a982a7c85a), U128(0x5a2c976d196a3567, 0xb2681b468efba361), U128(0xfec46d1e89292cf0, 0x41390efdc726e9ef)}},
  {{U128(0xd07230f388861771, 0x4b3f495dc82a7a68), U128(0x39773c54cb0e9276, 0x209abd55453a49b8), U128(0x1c3785c79ec2d4f5, 0x86310766026f8dcc)}, {U128(0x12e11c21cf9985c6, 0x3ac4eca2fbd267bb), U128(0xff4bd7de3aae3a28, 0x7faf2efdd7ae4acc), U128(0xfe70afeb6d33d6a2, 0x2907cf2b3f6feac1)}},
  {{U128(0xdafc53bdb0f3ad5e, 0x98b1eed416377b10), U128(0xe2427477a6ecf31e, 0xf0c8a6bf13a12290), U128(0x1f564e56a9730e34, 0x4e08e535cadaf146)}, {U128(0x300e707e9a9a66e5, 0xf65d64ce2ea940ee), U128(0xcc9d5d787e81f867, 0x92ae1e5d881f153b), U128(0xfe1323870cfe9a3d, 0x90cd1d959db674ee)}},
  {{U128(0xe0692c1d4d6a90c2, 0xdd411df178eb7d1e), U128(0x04e60cacc98164d7, 0xcfe841fd3b113802), U128(0x2273e19db5eaed56, 0xe5e597261830d391)}, {U128(0xd3bd6558940206cb, 0xd94accf74de23b6d), U128(0x9569c48a4106edf0, 0xc35ed177651ddbb9), U128(0xfdabcb8caeba091b, 0xfac7397cc07a646f)}},
  {{U128(0x8e8799e6913803e0, 0x37dd74aaaeb56c5a), U128(0x9fe14048be786e10, 0x3c6f72706b194b88), U128(0x259020dd1cc27444, 0xc002a2684781f07f)}, {U128(0x673171e6a6554f35, 0xb393be74c8381589), U128(0xa19702d6d140d729, 0x62b817621ff0f760), U128(0xfd3aabf84528b50b, 0xeae6bd951c1dabbd)}},
  {{U128(0x26ace06db55ecfbd, 0x063c572324d01a61), U128(0xfcd3974462ee60e3, 0xd0dcfa34d1aa337f), U128(0x28aaed62527cb3b5, 0xe976fb98213b8497)}, {U128(0xeab2d4b6c97209b7, 0xcf685093ab467d00), U128(0x39dbeb5c2d2cbf8e, 0x8b70e81ea4598ee8), U128(0xfcbfc926484cd43a, 0xa3e22b4d38917e73)}},
  {{U128(0x712beb01d32241b1, 0x1b51a3cdad65a191), U128(0x23d5a2b8dc1e0341, 0xdc32fb8507a753b5), U128(0x2bc42889167f8ca9, 0x8ffbbceed62c7c43)}, {U128(0x184b1d6fccfc856e, 0x9c1991b686a00c7d), U128(0x246ddc4e63f59390, 0xb859505a0b301225), U128(0xfc3b27d38a5d49ab, 0x256778ffcb5c1769)}},
  {{U128(0x767dfafab5ba9092, 0x655e186472be5455), U128(0xabcc11744430db5e, 0x8767e77e76440759), U128(0x2edbb3bca17e628e, 0xaf776280d975ac74)}, {U128(0xe1d49c2b973c884c, 0x1046bcdcf4fccd05), U128(0xf68925b421c48b09, 0xa1971c56ebc10d90), U128(0xfbaccd1d0903bb09, 0xe63ae8632b84473b)}},
  {{U128(0xa28fbdbe7eb1987e, 0x24859793181c3718), U128(0xf394e58d12972f1d, 0x39438767895414c1), U128(0x31f17078d34c156c, 0x9732300393f33613)}, {U128(0x947a5373353c45a4, 0x8d5b15eaa58368ef), U128(0xc5f40e3fd8f18ae1, 0xb1997321b48e8b1b), U128(0xfb14be7fbae58156, 0x2172a361fd2a722e)}},
  {{U128(0xd0fcf9cf13165ffe, 0xf465ff2dcbe8ac4b), U128(0x8c47e96b350b7c3f, 0x6e7693d88171a2b4), U128(0x3505404b6008a13c, 0x37d0017bda992a80)}, {U128(0x8df64f3218965d3c, 0xee268c7681608ebc), U128(0x7865a49bdcd7f074, 0x45770c9f804efeec), U128(0xfa7301d859796671, 0x1fe196a53fb5b237)}},
  {{U128(0x276234f6f3e1e1eb, 0xa623a2a80c4cb131), U128(0x2c9b801c691e71bb, 0x813b716e71055e67), U128(0x381704d4fc9ec5f9, 0x43af186b79b2a0f3)}, {U128(0xa87cf32ce084af6a, 0x5af910ded9deeb17), U128(0x79b567a67cde8b61, 0x61e63b20928c47af), U128(0xf9c79d63272c4628, 0x4504ae08d19b2980)}},
  {{U128(0x8f7f5e200d41a580, 0x8dee643bbbd353d6), U128(0xc138ad021532194d, 0x04eaaaab0d4998ea), U128(0x3b269fca8a8622ba, 0xdc913b883d64660e)}, {U128(0x09c93255bc853ced, 0x705a76d3418f2b4f), U128(0x2e09716dff309c62, 0x61e131e94ae140be), U128(0xf91297bbb1d6cdbe, 0x68fc6e4d6a920bd2)}},
  {{U128(0xa9bf1fae55e97289, 0x4ffaeb22d748d6d4), U128(0x5580085f9c2247b3, 0x76ceee5dee6d2d54), U128(0x3e33f2f642be355e, 0x90887712e9dc9663)}, {U128(0x18da934f3fc63793, 0x9958f29a37efd515), U128(0x95a1acb343363fb2, 0x6d1932845f3813dd), U128(0xf853f7dc9186b952, 0xc7adc6b4988891ba)}},
  {{U128(0xa3808a70a1da1d66, 0x0fa8580dc2529179), U128(0xcb01c0a806a3322e, 0xce36ba75ab4ffe02), U128(0x413ee038dff6b7fd, 0x7da6491b4f867803)}, {U128(0x600449cd14f8b1ec, 0x52bfd4ada717a31c), U128(0x4f10d75812d26a6d, 0x901915a71344b708), U128(0xf78bc51f239e12c6, 0x214cffcee9dd33ca)}},
  {{U128(0x2098bb3f6852d2b1, 0xfd197afa03c1844e), U128(0xf735312b47773c26, 0x6a6e3a60fdae388e), U128(0x4447498ac7d9dd82, 0x4c20ab7aa99a2182)}, {U128(0xafdc40732a1b03f6, 0x0f46e91acb4eeab8), U128(0xb06854a7d3139b87, 0xf2f68677a56efb23), U128(0xf6ba073b424b19e8, 0x2c791f59cc1ffc22)}},
  {{U128(0xa75ab2ef3590737e, 0x5257cf0b424aab42), U128(0x2b110165bc3ba0b2, 0xe1a97db3c5d0fc6c), U128(0x474d10fd336cf746, 0xf95f1f657313c4ea)}, {U128(0xd727e908a1cc5001, 0xd24303f2f370d960), U128(0xcb773db17e36deb8, 0xa64033468f47f60f), U128(0xf5dec646f85ba1c6, 0xc8c615e72768d6b4)}},
  {{U128(0x96e8a93742139e47, 0x76c746094bca11de), U128(0xc70534171bf68a81, 0x1c561ea3e50849c2), U128(0x4a5018bb567c16a2, 0xd725d3b9ed35fba9)}, {U128(0xd5fd8a1cafb2cce5, 0x2a53096f0c1278a8), U128(0x5ee4b09989a11728, 0x8e77ee63670fa090), U128(0xf4fa0ab6316ed2ec, 0x163c5c7f03b718c5)}},
  {{U128(0xda3ec2ec26e111aa, 0x1ce81f6a7938f852), U128(0xe9b81d82d31b3987, 0x29694eddb5afbcc9), U128(0x4d50430b860546c3, 0x4ffd0691f9e13630)}, {U128(0x5fd41175e0c39c34, 0xb336c99ce723d817), U128(0xd861a7ef2e855f75, 0xe59aad1cf16cdb2b), U128(0xf40bdd5a6688662f, 0x5019794a1f5896e4)}},
  {{U128(0x16af5c057a216a8c, 0x08371289cb79dce7), U128(0x7b0a0a42f45c41bd, 0x64895285f2b7e950), U128(0x504d72505d98050c, 0x97c4afa25181e605)}, {U128(0xc3d4240d5d9a890a, 0xe285567b61bd57d7), U128(0x6cc90b7d06c506d8, 0x511aacdcd3b8eb99), U128(0xf314476247088f74, 0xa5486bdc455d56a2)}},
  {{U128(0x9dea4afc821a9730, 0xf90d930343d33002), U128(0xddf9e1806cdc80ef, 0x18e2bc14c8ed3c36), U128(0x53478909e39da892, 0x0b90c3c942bfa62c)}, {U128(0xd7166cd3d924d607, 0xb942b718144c51cb), U128(0x83ef188365e5681d, 0x40e0ba89720e6c81), U128(0xf21352595e0bf350, 0xe7112e89103cc0c6)}},
  {{U128(0x7d9d0af68fc06e7f, 0x3b27dffbfc02ab2f), U128(0x1a157ced9b54e454, 0xe1e5b755b8291558), U128(0x563e69d6ac7f73f8, 0x408fca9cc277fc1f)}, {U128(0xd2cfecddd6b7ba8a, 0xb39accc3025a1b44), U128(0x89dab2e0fe03a229, 0x894c0e9aac77bfe4), U128(0xf1090827b43725fd, 0x67127db35b287315)}},
  {{U128(0x70648748c0e93f2f, 0xa093129e5604b25c), U128(0xa162f122c751ffbe, 0xe448924574962b91), U128(0x5931f774fc9f1844, 0x34ad29990485d84d)}, {U128(0x878499cdabe3cdfd, 0xb477fbf5eadcd513), U128(0xf01bb72d7bcdcefb, 0x3be1d154823575ea), U128(0xeff573116df1555d, 0x62aef7b55319d1d3)}},
  {{U128(0xe83c1ffda3ec476f, 0x39bf1307dd6481f6), U128(0xef75477f5dbb812b, 0x2b7d0ea00521b3d9), U128(0x5c2214c3e9167abb, 0x4e61f79b3a36f1db)}, {U128(0x0e0ea289950add57, 0xa8acc772534bbb89), U128(0xfc4facdfd3d64e3f, 0xb6a4dec498e3fbc3), U128(0xeed89db66611e307, 0x86f8c20fb664b01a)}},
  {{U128(0xf46e8417ae1b7ac7, 0x023ec85e7e723382), U128(0x4633259b16954dad, 0xd07cf3484aacca02), U128(0x5f0ea4c477339c06, 0x68fc87bcfa363f00)}, {U128(0x6479184fd64fdd03, 0xfff5ac40eec5dd32), U128(0x6f340fabaa13780e, 0xee2ea72f6796aab4), U128(0xedb29311c504d652, 0x11815196b9fbf5df)}},
  {{U128(0x6d12d59bba9c4881, 0x7b8a103264edaf26), U128(0xbdf1f5b4ab3de24c, 0x3a3c159062718f71), U128(0x61f78a9abaa58b46, 0x98916152cf7eee1b)}, {U128(0xa6340cb65c4b11d5, 0xb1ec03e9739998c9), U128(0xf0a83d3cd0dae9b5, 0xdb897c2384083746), U128(0xec835e79946a3145, 0x7e610231ac1d6180)}},
  {{U128(0x2a51bb803173c218, 0x09e7f29df29795a8), U128(0xce241f5bddec213c, 0x823518ea88a51741), U128(0x64dca98ef24f5cb4, 0x218cf2d6ad86e5c1)}, {U128(0x15a1ef23b6fcbe7c, 0x5d2c60a38e3f1513), U128(0xef30f42455269f35, 0xd67b03c2e02269d4), U128(0xeb4b0b9e4f345617, 0x39e39c6c2ab3655c)}},
  {{U128(0x128cfed9c6a525ac, 0x154b74a5668d058d), U128(0x16fec586e008e2c9, 0x76c8c1d21cfb8d44), U128(0x67bde50ea3b628b6, 0xd409485edd56b172)}, {U128(0x23cb86a3c7b29248, 0xe4f7620cff6de0c8), U128(0x3d5705f10a9a7f6f, 0x2397ea24d5049427), U128(0xea09a68a6e49cd62, 0x15ad45b4a1b5e823)}},
  {{U128(0xad30468299d12a94, 0xd31ec3e562d38d68), U128(0x8680b929c2af992e, 0xb6224cac9d2fde72), U128(0x6a9b20adb4ff262a, 0x0ef911a3308e35a5)}, {U128(0x1ac1087622ce6519, 0x9f45710178447ede), U128(0x18801dd8eb6d0389, 0xe02cade99a642003), U128(0xe8bf3ba1f1aedfbb, 0xf8972affb3d98e1f)}},
  {{U128(0x5e7a1201472731cd, 0x28a927836f80a344), U128(0x041e72879f2cc28a, 0x9b2affa69c693d13), U128(0x6d744027857300ad, 0x9b165cba0c171818)}, {U128(0xc924411252c09e83, 0xc0a76c669f14305b), U128(0x2f1bb3282663e451, 0x09de14efea575873), U128(0xe76bd7a1e63b9786, 0x125129529d48a92f)}},
  {{U128(0xfdd30e2b653cf05e, 0x5197dc55b37b71a1), U128(0x26d02c91483d2b98, 0x2f03ab4c7a03bca6), U128(0x70492760047b9a7e, 0xc5544aeadf9f4275)}, {U128(0x760a98a3a5e7070a, 0x2f1f5397d9b3bd20), U128(0x83dad966df1f6a5b, 0xac14cdda015a2273), U128(0xe60f879fe7e2e1e5, 0x7613b68f6ab0312f)}},
  {{U128(0x6f830a878ee71525, 0xa4aaec3e753e8319), U128(0x5a5c9453fa24a534, 0x4bf6e0e57669d638), U128(0x7319ba64c711785a, 0x1439670dfe3d68e6)}, {U128(0xb858fec82022732f, 0x4995e362e423e0b2), U128(0x8743f3fe219a6a8f, 0x27902aa36084a51b), U128(0xe4aa5909a08fa7b4, 0x122785ae67f5515c)}},
  {{U128(0xdeb7639b7fda6317, 0x504841dbc1bc33cb), U128(0xd6a30f214c6f5661, 0x94211e282444d411), U128(0x75e5dd6e1b8e2555, 0x0ec87bc0574a889f)}, {U128(0x00c457882d61331a, 0xb59d28224cf60b9a), U128(0xda3dac309974fa5b, 0xcd6367a773e9c0fc), U128(0xe33c59a4439cd8ec, 0x36563e2ffad83519)}},
  {{U128(0x1ac961f0b0786c8e, 0x974624fbb7dd5944), U128(0xd68aad829daca0d0, 0xca819ced385cb98a), U128(0x78ad74e01bd8ec78, 0x362474f1a105878e)}, {U128(0xf775e41ec0015d69, 0xc80f46beaf1a70b6), U128(0xfdea3ff0dc053327, 0x82c4fe35f8f29cea), U128(0xe1c5978c05ed8691, 0xf4e8a8372f8c580f)}},
  {{U128(0x7be9be180f7ad019, 0x7c972f8498cc190a), U128(0x2cc10e25c2a714b5, 0xa2b8a0351e3a7882), U128(0x7b70654bbde35622, 0xf083cc12119ff7a3)}, {U128(0xc22923054710b10b, 0xf38130f5b4f7d5a8), U128(0xc9985ae9491098ca, 0xb0d96a3a40821766), U128(0xe046213392aa486c, 0x55ff6038a5197366)}},
  {{U128(0xa8b8ac3a1aad3732, 0x3d2d1ef3b7418052), U128(0xb55ae104d717a04b, 0xd37a87fa45ce838b), U128(0x7e2e936fe26ae7ed, 0x13e03e4889485c68)}, {U128(0xa01d809beb9cbef8, 0xacd64cce066f8326), U128(0xd4a257966e116379, 0x86a3422b18230d41), U128(0xdebe05637ca94cfb, 0x4b19aa71fec3ae6c)}},
  {{U128(0x9d3488db15ba9684, 0xea356b71f2fa5f76), U128(0xbabcfdf74f528666, 0xedeb672dfdaf228c), U128(0x80e7e43a61f5b6cb, 0x5ca183dc973abc21)}, {U128(0xb1b75f774b427ef6, 0x7c175bc3672f2209), U128(0x7866f911e19bddc7, 0x6a0a4b9a9f65b5ec), U128(0xdd2d5339ac8692fd, 0x49c6e0ea76cbcaac)}},
  {{U128(0x718fb60d736dce7c, 0xc4e8dac740710c18), U128(0xd6bc7fe57653aec3, 0x2d3bfe70d9ab409a), U128(0x839c3cc917ff6cb4, 0xbfd79717f2880abe)}, {U128(0xa71857fd6a653db4, 0xcba1e4ca9b20ef6b), U128(0xb67292cbdf4cb882, 0xf8ae9c38d38a7789), U128(0xdb941a28cb71ec87, 0x2c19b63253da43fb)}},
  {{U128(0x68298f5e656cddcf, 0xaee3fde7196372b6), U128(0xa4087bb61a5a1571, 0x43fdc5fb3261bde9), U128(0x864b826aec4c74e5, 0x85043222c9bdd18c)}, {U128(0x31cd467b273c3b6b, 0xcfa21aafbc4af64e), U128(0x768df376f0edc304, 0xf2abc04130b0a03c), U128(0xd9f269f7aab88c29, 0x28e81dcb6dab91ac)}},
  {{U128(0xdd549bb352a247b8, 0x275c1d521f91ca92), U128(0x1ddc902c4a28d838, 0x5c00d180c7655a29), U128(0x88f59aa0da591421, 0xb892ca8361d8c84c)}, {U128(0xf20994c8b2e8f9f4, 0x2220cf60307904fb), U128(0xdd2c2b52901fe500, 0x17b15cb0a701425b), U128(0xd84852c0a80ffcdb, 0x24b9fe00663574a3)}},
  {{U128(0x7634f0497dec9f3e, 0x3b1787b920908215), U128(0xd10e572017ce8920, 0x2b0f5e038c54e982), U128(0x8b9a6b1ef6da4502, 0x21a6675f51580bc3)}, {U128(0x23cdeac1b5fa0870, 0xdbc56a93033d7336), U128(0xfea24fa6ea109da0, 0x25ac706da7d56ce0), U128(0xd695e4f10ea88570, 0x083f082b570611d6)}},
  {{U128(0x7f9152cba72ae50c, 0xdf93eac8ec561d41), U128(0x7ca7d749adfba33e, 0xca996068c296fd79), U128(0x8e39d9cd73464364, 0xbba4cfecbff54867)}, {U128(0x3f46de4fbafdc08b, 0xc63df891efb87d5c), U128(0xb4691d2f99ec9eaa, 0xac08e58a7cd39544), U128(0xd4db3148750d1819, 0xf630e8b6dac83e68)}},
  {{U128(0xcd26e7a833103d93, 0x0b752332c193a8c6), U128(0x0e3328f9a15a9849, 0x3074ee8cf7cef350), U128(0x90d3ccc99f5ac58b, 0x09d1072e09b72292)}, {U128(0x724b33af23a1acea, 0x15b92eb32a15e942), U128(0x725b3942ea886124, 0xad809b6779bb56e3), U128(0xd31848d817d70e16, 0xeeeaddb72f00e0dd)}},
  {{U128(0x4f95ec5747e09033, 0x085d722683513df8), U128(0x44b562fe29a53a66, 0x7ad1253f5ec14ff0), U128(0x93682a66e896f544, 0xb17821911e71c16e)}, {U128(0x18e858d225187bd4, 0x5b311ee0c5a1e4f3), U128(0xe386cbda4f76d559, 0x39d956bcfe9a5d9a), U128(0xd14d3d02313c0eed, 0x744fea20e8abef91)}},
  {{U128(0x783a89bea38a8e02, 0x462b8681663dbe77), U128(0x567fb901bd502162, 0xa01118815237a72a), U128(0x95f6d92fd79f4fba, 0xd9f8e1a446e973b9)}, {U128(0x7a32f1837ac516a6, 0xfbf6cc6456873c0b), U128(0xd6f34524a4c3da3d, 0x8a787c40acca14e4), U128(0xcf7a1f794d7ca1b1, 0xdfcb60445c1bf972)}},
  {{U128(0x01acd947a1647870, 0x51694c2422e6a3fe), U128(0x6b714df5a5e72d4f, 0x1d9ce3a2743a1804), U128(0x987fbfe70b81a708, 0x19cec845ac87a5c6)}, {U128(0x6c2162f37c431f7f, 0xa9aac44fb4486325), U128(0x9d02f0e6caa17d3d, 0xc881a55027476af7), U128(0xcd9f023f9c3a059e, 0x23af31db7179a4a9)}},
  {{U128(0xc3599ef0b50ef59a, 0xe1fc7518e80713ca), U128(0x6ced068130b67332, 0x76dcd42f6c39e4db), U128(0x9b02c58832cf95c0, 0x698b94f50326a043)}, {U128(0x8cd967bccea9addc, 0xc609e5033a767a21), U128(0x9cf6385be759c799, 0x5bd241c2de422690), U128(0xcbbbf7a63eba0dd5, 0x70cbb7f3343451bd)}},
  {{U128(0xc54b364f1553b6cc, 0x81cb56de48333065), U128(0x87424855daf5d9f3, 0x21f5f9a3921b151e), U128(0x9d7fd1490285c9e3, 0xe25e39549638ae67)}, {U128(0xa0fa0954d3d040bb, 0x7c863e3c5fb24298), U128(0x4bfc4d9abfeb5f9f, 0x3e33f4230b848c8a), U128(0xc9d1124c931fda7a, 0x8335241be1693225)}},
  {{U128(0xa0ceb403f2c524af, 0xa3eb8e405ca8a3df), U128(0xe77e3c6814fd92a5, 0x4562bb5fb2e2154d), U128(0x9ff6ca9a2ab6a26d, 0x22cc118a0c118a9f)}, {U128(0x1dce62094d77050c, 0x5738dbfa159676c9), U128(0x5ccea1083f804b74, 0x005c4b0e953e4fff), U128(0xc7de651f7ca06749, 0x02b31bc86877fd2c)}},
  {{U128(0xa7de0615a7501006, 0x15370b1dff515e33), U128(0xc381700bdf757600, 0x04c7b378b215b01d), U128(0xa267992848eeb0c0, 0x3b5167ee359a234d)}, {U128(0x810c85a6661d3baf, 0x0e2f6071f0d41621), U128(0xd47437d3710b81d7, 0x9bcdaef02b673f05), U128(0xc5e40358a8ba05a7, 0x43da25d99267326a)}},
  {{U128(0xa9cf80d2cce37a8d, 0x31444caabc2bc91e), U128(0xbec67a81b0f50a90, 0x0b73385ac3b53a36), U128(0xa4d224dcd849c5b0, 0x23d251cc8d7975cb)}, {U128(0xece93ff45353cd2a, 0x547e3a0451e4bef2), U128(0x9bb528d632279b92, 0x173f3b2935cb89e7), U128(0xc3e2007dd175f5a4, 0xa87e78136665cdb1)}},
  {{U128(0xe237eeaa1b32dac2, 0xa8aa22ab31e036a8), U128(0x9aab00f488eea6ad, 0x3d517247a54a9ea8), U128(0xa73655df1f2f489e, 0x149f6e75993468a2)}, {U128(0x4203b9bff4935afd, 0x6c34a47eddd9d480), U128(0xd785c0cfc7c2e7a2, 0x164c59c4fc989f35), U128(0xc1d8705ffcbb6e90, 0xbdf0715cb8b20bd6)}},
  {{U128(0x36880f67db05c705, 0x2aff44eacb27bb10), U128(0xa78b957f2ae93736, 0xbbbdccbb12ab1287), U128(0xa99414951aacae5e, 0xd147625fda929af7)}, {U128(0x4a009b490f31716f, 0xf0e5064107bb6bf2), U128(0xa4b71762aa6a622d, 0xd3810198147748ad), U128(0xbfc7671ab8bb84c6, 0xe4e62d86dd136e77)}},
  {{U128(0x3c2ea3be7e7a3226, 0x307f343eb7973bb4), U128(0x2fd0f3859abecff4, 0x64742ca428c80c16), U128(0xabeb49a46764fd15, 0x1becda8089c1a94c)}, {U128(0x6a8e73990ff51f79, 0xe9fd235a467a6348), U128(0x9c677149abc2caaf, 0xe0bd35686bed05f0), U128(0xbdaef913557d76f0, 0xac85320f528d6d5c)}},
  {{U128(0xca706e5b8e5b2095, 0xc1f13d3b7843aa10), U128(0xd900f1364fee42ca, 0x4f62ddb564e462a3), U128(0xae3bddf3280c620d, 0x3d53817865422564)}, {U128(0xe8b420ef309aa397, 0x8b55149872e53d35), U128(0x7ddf4124fc561794, 0xf7ae3fc5d25b394f), U128(0xbb8f3af81b93095c, 0xfce8d84068e825b6)}},
  {{U128(0xfebc57d474cf8ba5, 0xdc1da8030635a8b3), U128(0xa642438dce55e433, 0xc0570800fb967a6b), U128(0xb085baa8e966f6da, 0xe4cad00d5c94bcd1)}, {U128(0xf052b35906eac2ee, 0xc7b22e5647b309c5), U128(0x43b1297eff842a9f, 0xd1ab4f851f6104b5), U128(0xb96841bf7ffcb21a, 0x9de1e3b22b8bf4db)}},
  {{U128(0xa1306e034b29ec45, 0xb909acbac4dd556f), U128(0xa29d2dcf89c9bd9c, 0x6d245d9bdb619aca), U128(0xb2c8c92f83c1eb87, 0xac9f7ebbc469ef58)}, {U128(0x54443ef10e6e2695, 0xba6be5f86b9c3528), U128(0xe4069d20fb3c443d, 0x084ba203841cbb20), U128(0xb73a22a755457448, 0x7f86f63bb23f4969)}},
  {{U128(0x4afc83043ab8a2c3, 0xa8b1fe6fdc83db39), U128(0x1d6f60ba893ba84c, 0xed17ac8583339915), U128(0xb504f333f9de6484, 0x597d89b3754abe9f)}, {U128(0x4afc83043ab8a2c3, 0xa8b1fe6fdc83db39), U128(0x1d6f60ba893ba84c, 0xed17ac8583339915), U128(0xb504f333f9de6484, 0x597d89b3754abe9f)}},
  {{U128(0x54443ef10e6e2695, 0xba6be5f86b9c3528), U128(0xe4069d20fb3c443d, 0x084ba203841cbb20), U128(0xb73a22a755457448, 0x7f86f63bb23f4969)}, {U128(0xa1306e034b29ec45, 0xb909acbac4dd556f), U128(0xa29d2dcf89c9bd9c, 0x6d245d9bdb619aca), U128(0xb2c8c92f83c1eb87, 0xac9f7ebbc469ef58)}},
  {{U128(0xf052b35906eac2ee, 0xc7b22e5647b309c5), U128(0x43b1297eff842a9f, 0xd1ab4f851f6104b5), U128(0xb96841bf7ffcb21a, 0x9de1e3b22b8bf4db)}, {U128(0xfebc57d474cf8ba5, 0xdc1da8030635a8b3), U128(0xa642438dce55e433, 0xc0570800fb967a6b), U128(0xb085baa8e966f6da, 0xe4cad00d5c94bcd1)}},
  {{U128(0xe8b420ef309aa397, 0x8b55149872e53d35), U128(0x7ddf4124fc561794, 0xf7ae3fc5d25b394f), U128(0xbb8f3af81b93095c, 0xfce8d84068e825b6)}, {U128(0xca706e5b8e5b2095, 0xc1f13d3b7843aa10), U128(0xd900f1364fee42ca, 0x4f62ddb564e462a3), U128(0xae3bddf3280c620d, 0x3d53817865422564)}},
  {{U128(0x6a8e73990ff51f79, 0xe9fd235a467a6348), U128(0x9c677149abc2caaf, 0xe0bd35686bed05f0), U128(0xbdaef913557d76f0, 0xac85320f528d6d5c)}, {U128(0x3c2ea3be7e7a3226, 0x307f343eb7973bb4), U128(0x2fd0f3859abecff4, 0x64742ca428c80c16), U128(0xabeb49a46764fd15, 0x1becda8089c1a94c)}},
  {{U128(0x4a009b490f31716f, 0xf0e5064107bb6bf2), U128(0xa4b71762aa6a622d, 0xd3810198147748ad), U128(0xbfc7671ab8bb84c6, 0xe4e62d86dd136e77)}, {U128(0x36880f67db05c705, 0x2aff44eacb27bb10), U128(0xa78b957f2ae93736, 0xbbbdccbb12ab1287), U128(0xa99414951aacae5e, 0xd147625fda929af7)}},
  {{U128(0x4203b9bff4935afd, 0x6c34a47eddd9d480), U128(0xd785c0cfc7c2e7a2, 0x164c59c4fc989f35), U128(0xc1d8705ffcbb6e90, 0xbdf0715cb8b20bd6)}, {U128(0xe237eeaa1b32dac2, 0xa8aa22ab31e036a8), U128(0x9aab00f488eea6ad, 0x3d517247a54a9ea8), U128(0xa73655df1f2f489e, 0x149f6e75993468a2)}},
  {{U128(0xece93ff45353cd2a, 0x547e3a0451e4bef2), U128(0x9bb528d632279b92, 0x173f3b2935cb89e7), U128(0xc3e2007dd175f5a4, 0xa87e78136665cdb1)}, {U128(0xa9cf80d2cce37a8d, 0x31444caabc2bc91e), U128(0xbec67a81b0f50a90, 0x0b73385ac3b53a36), U128(0xa4d224dcd849c5b0, 0x23d251cc8d7975cb)}},
  {{U128(0x810c85a6661d3baf, 0x0e2f6071f0d41621), U128(0xd47437d3710b81d7, 0x9bcdaef02b673f05), U128(0xc5e40358a8ba05a7, 0x43da25d99267326a)}, {U128(0xa7de0615a7501006, 0x15370b1dff515e33), U128(0xc381700bdf757600, 0x04c7b378b215b01d), U128(0xa267992848eeb0c0, 0x3b5167ee359a234d)}},
  {{U128(0x1dce62094d77050c, 0x5738dbfa159676c9), U128(0x5ccea1083f804b74, 0x005c4b0e953e4fff), U128(0xc7de651f7ca06749, 0x02b31bc86877fd2c)}, {U128(0xa0ceb403f2c524af, 0xa3eb8e405ca8a3df), U128(0xe77e3c6814fd92a5, 0x4562bb5fb2e2154d), U128(0x9ff6ca9a2ab6a26d, 0x22cc118a0c118a9f)}},
  {{U128(0xa0fa0954d3d040bb, 0x7c863e3c5fb24298), U128(0x4bfc4d9abfeb5f9f, 0x3e33f4230b848c8a), U128(0xc9d1124c931fda7a, 0x8335241be1693225)}, {U128(0xc54b364f1553b6cc, 0x81cb56de48333065), U128(0x87424855daf5d9f3, 0x21f5f9a3921b151e), U128(0x9d7fd1490285c9e3, 0xe25e39549638ae67)}},
  {{U128(0x8cd967bccea9addc, 0xc609e5033a767a21), U128(0x9cf6385be759c799, 0x5bd241c2de422690), U128(0xcbbbf7a63eba0dd5, 0x70cbb7f3343451bd)}, {U128(0xc3599ef0b50ef59a, 0xe1fc7518e80713ca), U128(0x6ced068130b67332, 0x76dcd42f6c39e4db), U128(0x9b02c58832cf95c0, 0x698b94f50326a043)}},
  {{U128(0x6c2162f37c431f7f, 0xa9aac44fb4486325), U128(0x9d02f0e6caa17d3d, 0xc881a55027476af7), U128(0xcd9f023f9c3a059e, 0x23af31db7179a4a9)}, {U128(0x01acd947a1647870, 0x51694c2422e6a3fe), U128(0x6b714df5a5e72d4f, 0x1d9ce3a2743a1804), U128(0x987fbfe70b81a708, 0x19cec845ac87a5c6)}},
  {{U128(0x7a32f1837ac516a6, 0xfbf6cc6456873c0b), U128(0xd6f34524a4c3da3d, 0x8a787c40acca14e4), U128(0xcf7a1f794d7ca1b1, 0xdfcb60445c1bf972)}, {U128(0x783a89bea38a8e02, 0x462b8681663dbe77), U128(0x567fb901bd502162, 0xa01118815237a72a), U128(0x95f6d92fd79f4fba, 0xd9f8e1a446e973b9)}},
  {{U128(0x18e858d225187bd4, 0x5b311ee0c5a1e4f3), U128(0xe386cbda4f76d559, 0x39d956bcfe9a5d9a), U128(0xd14d3d02313c0eed, 0x744fea20e8abef91)}, {U128(0x4f95ec5747e09033, 0x085d722683513df8), U128(0x44b562fe29a53a66, 0x7ad1253f5ec14ff0), U128(0x93682a66e896f544, 0xb17821911e71c16e)}},
  {{U128(0x724b33af23a1acea, 0x15b92eb32a15e942), U128(0x725b3942ea886124, 0xad809b6779bb56e3), U128(0xd31848d817d70e16, 0xeeeaddb72f00e0dd)}, {U128(0xcd26e7a833103d93, 0x0b752332c193a8c6), U128(0x0e3328f9a15a9849, 0x3074ee8cf7cef350), U128(0x90d3ccc99f5ac58b, 0x09d1072e09b72292)}},
  {{U128(0x3f46de4fbafdc08b, 0xc63df891efb87d5c), U128(0xb4691d2f99ec9eaa, 0xac08e58a7cd39544), U128(0xd4db3148750d1819, 0xf630e8b6dac83e68)}, {U128(0x7f9152cba72ae50c, 0xdf93eac8ec561d41), U128(0x7ca7d749adfba33e, 0xca996068c296fd79), U128(0x8e39d9cd73464364, 0xbba4cfecbff54867)}},
  {{U128(0x23cdeac1b5fa0870, 0xdbc56a93033d7336), U128(0xfea24fa6ea109da0, 0x25ac706da7d56ce0), U128(0xd695e4f10ea88570, 0x083f082b570611d6)}, {U128(0x7634f0497dec9f3e, 0x3b1787b920908215), U128(0xd10e572017ce8920, 0x2b0f5e038c54e982), U128(0x8b9a6b1ef6da4502, 0x21a6675f51580bc3)}},
  {{U128(0xf20994c8b2e8f9f4, 0x2220cf60307904fb), U128(0xdd2c2b52901fe500, 0x17b15cb0a701425b), U128(0xd84852c0a80ffcdb, 0x24b9fe00663574a3)}, {U128(0xdd549bb352a247b8, 0x275c1d521f91ca92), U128(0x1ddc902c4a28d838, 0x5c00d180c7655a29), U128(0x88f59aa0da591421, 0xb892ca8361d8c84c)}},
  {{U128(0x31cd467b273c3b6b, 0xcfa21aafbc4af64e), U128(0x768df376f0edc304, 0xf2abc04130b0a03c), U128(0xd9f269f7aab88c29, 0x28e81dcb6dab91ac)}, {U128(0x68298f5e656cddcf, 0xaee3fde7196372b6), U128(0xa4087bb61a5a1571, 0x43fdc5fb3261bde9), U128(0x864b826aec4c74e5, 0x85043222c9bdd18c)}},
  {{U128(0xa71857fd6a653db4, 0xcba1e4ca9b20ef6b), U128(0xb67292cbdf4cb882, 0xf8ae9c38d38a7789), U128(0xdb941a28cb71ec87, 0x2c19b63253da43fb)}, {U128(0x718fb60d736dce7c, 0xc4e8dac740710c18), U128(0xd6bc7fe57653aec3, 0x2d3bfe70d9ab409a), U128(0x839c3cc917ff6cb4, 0xbfd79717f2880abe)}},
  {{U128(0xb1b75f774b427ef6, 0x7c175bc3672f2209), U128(0x7866f911e19bddc7, 0x6a0a4b9a9f65b5ec), U128(0xdd2d5339ac8692fd, 0x49c6e0ea76cbcaac)}, {U128(0x9d3488db15ba9684, 0xea356b71f2fa5f76), U128(0xbabcfdf74f528666, 0xedeb672dfdaf228c), U128(0x80e7e43a61f5b6cb, 0x5ca183dc973abc21)}},
  {{U128(0xa01d809beb9cbef8, 0xacd64cce066f8326), U128(0xd4a257966e116379, 0x86a3422b18230d41), U128(0xdebe05637ca94cfb, 0x4b19aa71fec3ae6c)}, {U128(0xa8b8ac3a1aad3732, 0x3d2d1ef3b7418052), U128(0xb55ae104d717a04b, 0xd37a87fa45ce838b), U128(0x7e2e936fe26ae7ed, 0x13e03e4889485c68)}},
  {{U128(0xc22923054710b10b, 0xf38130f5b4f7d5a8), U128(0xc9985ae9491098ca, 0xb0d96a3a40821766), U128(0xe046213392aa486c, 0x55ff6038a5197366)}, {U128(0x7be9be180f7ad019, 0x7c972f8498cc190a), U128(0x2cc10e25c2a714b5, 0xa2b8a0351e3a7882), U128(0x7b70654bbde35622, 0xf083cc12119ff7a3)}},
  {{U128(0xf775e41ec0015d69, 0xc80f46beaf1a70b6), U128(0xfdea3ff0dc053327, 0x82c4fe35f8f29cea), U128(0xe1c5978c05ed8691, 0xf4e8a8372f8c580f)}, {U128(0x1ac961f0b0786c8e, 0x974624fbb7dd5944), U128(0xd68aad829daca0d0, 0xca819ced385cb98a), U128(0x78ad74e01bd8ec78, 0x362474f1a105878e)}},
  {{U128(0x00c457882d61331a, 0xb59d28224cf60b9a), U128(0xda3dac309974fa5b, 0xcd6367a773e9c0fc), U128(0xe33c59a4439cd8ec, 0x36563e2ffad83519)}, {U128(0xdeb7639b7fda6317, 0x504841dbc1bc33cb), U128(0xd6a30f214c6f5661, 0x94211e282444d411), U128(0x75e5dd6e1b8e2555, 0x0ec87bc0574a889f)}},
  {{U128(0xb858fec82022732f, 0x4995e362e423e0b2), U128(0x8743f3fe219a6a8f, 0x27902aa36084a51b), U128(0xe4aa5909a08fa7b4, 0x122785ae67f5515c)}, {U128(0x6f830a878ee71525, 0xa4aaec3e753e8319), U128(0x5a5c9453fa24a534, 0x4bf6e0e57669d638), U128(0x7319ba64c711785a, 0x1439670dfe3d68e6)}},
  {{U128(0x760a98a3a5e7070a, 0x2f1f5397d9b3bd20), U128(0x83dad966df1f6a5b, 0xac14cdda015a2273), U128(0xe60f879fe7e2e1e5, 0x7613b68f6ab0312f)}, {U128(0xfdd30e2b653cf05e, 0x5197dc55b37b71a1), U128(0x26d02c91483d2b98, 0x2f03ab4c7a03bca6), U128(0x70492760047b9a7e, 0xc5544aeadf9f4275)}},
  {{U128(0xc924411252c09e83, 0xc0a76c669f14305b), U128(0x2f1bb3282663e451, 0x09de14efea575873), U128(0xe76bd7a1e63b9786, 0x125129529d48a92f)}, {U128(0x5e7a1201472731cd, 0x28a927836f80a344), U128(0x041e72879f2cc28a, 0x9b2affa69c693d13), U128(0x6d744027857300ad, 0x9b165cba0c171818)}},
  {{U128(0x1ac1087622ce6519, 0x9f45710178447ede), U128(0x18801dd8eb6d0389, 0xe02cade99a642003), U128(0xe8bf3ba1f1aedfbb, 0xf8972affb3d98e1f)}, {U128(0xad30468299d12a94, 0xd31ec3e562d38d68), U128(0x8680b929c2af992e, 0xb6224cac9d2fde72), U128(0x6a9b20adb4ff262a, 0x0ef911a3308e35a5)}},
  {{U128(0x23cb86a3c7b29248, 0xe4f7620cff6de0c8), U128(0x3d5705f10a9a7f6f, 0x2397ea24d5049427), U128(0xea09a68a6e49cd62, 0x15ad45b4a1b5e823)}, {U128(0x128cfed9c6a525ac, 0x154b74a5668d058d), U128(0x16fec586e008e2c9, 0x76c8c1d21cfb8d44), U128(0x67bde50ea3b628b6, 0xd409485edd56b172)}},
  {{U128(0x15a1ef23b6fcbe7c, 0x5d2c60a38e3f1513), U128(0xef30f42455269f35, 0xd67b03c2e02269d4), U128(0xeb4b0b9e4f345617, 0x39e39c6c2ab3655c)}, {U128(0x2a51bb803173c218, 0x09e7f29df29795a8), U128(0xce241f5bddec213c, 0x823518ea88a51741), U128(0x64dca98ef24f5cb4, 0x218cf2d6ad86e5c1)}},
  {{U128(0xa6340cb65c4b11d5, 0xb1ec03e9739998c9), U128(0xf0a83d3cd0dae9b5, 0xdb897c2384083746), U128(0xec835e79946a3145, 0x7e610231ac1d6180)}, {U128(0x6d12d59bba9c4881, 0x7b8a103264edaf26), U128(0xbdf1f5b4ab3de24c, 0x3a3c159062718f71), U128(0x61f78a9abaa58b46, 0x98916152cf7eee1b)}},
  {{U128(0x6479184fd64fdd03, 0xfff5ac40eec5dd32), U128(0x6f340fabaa13780e, 0xee2ea72f6796aab4), U128(0xedb29311c504d652, 0x11815196b9fbf5df)}, {U128(0xf46e8417ae1b7ac7, 0x023ec85e7e723382), U128(0x4633259b16954dad, 0xd07cf3484aacca02), U128(0x5f0ea4c477339c06, 0x68fc87bcfa363f00)}},
  {{U128(0x0e0ea289950add57, 0xa8acc772534bbb89), U128(0xfc4facdfd3d64e3f, 0xb6a4dec498e3fbc3), U128(0xeed89db66611e307, 0x86f8c20fb664b01a)}, {U128(0xe83c1ffda3ec476f, 0x39bf1307dd6481f6), U128(0xef75477f5dbb812b, 0x2b7d0ea00521b3d9), U128(0x5c2214c3e9167abb, 0x4e61f79b3a36f1db)}},
  {{U128(0x878499cdabe3cdfd, 0xb477fbf5eadcd513), U128(0xf01bb72d7bcdcefb, 0x3be1d154823575ea), U128(0xeff573116df1555d, 0x62aef7b55319d1d3)}, {U128(0x70648748c0e93f2f, 0xa093129e5604b25c), U128(0xa162f122c751ffbe, 0xe448924574962b91), U128(0x5931f774fc9f1844, 0x34ad29990485d84d)}},
  {{U128(0xd2cfecddd6b7ba8a, 0xb39accc3025a1b44), U128(0x89dab2e0fe03a229, 0x894c0e9aac77bfe4), U128(0xf1090827b43725fd, 0x67127db35b287315)}, {U128(0x7d9d0af68fc06e7f, 0x3b27dffbfc02ab2f), U128(0x1a157ced9b54e454, 0xe1e5b755b8291558), U128(0x563e69d6ac7f73f8, 0x408fca9cc277fc1f)}},
  {{U128(0xd7166cd3d924d607, 0xb942b718144c51cb), U128(0x83ef188365e5681d, 0x40e0ba89720e6c81), U128(0xf21352595e0bf350, 0xe7112e89103cc0c6)}, {U128(0x9dea4afc821a9730, 0xf90d930343d33002), U128(0xddf9e1806cdc80ef, 0x18e2bc14c8ed3c36), U128(0x53478909e39da892, 0x0b90c3c942bfa62c)}},
  {{U128(0xc3d4240d5d9a890a, 0xe285567b61bd57d7), U128(0x6cc90b7d06c506d8, 0x511aacdcd3b8eb99), U128(0xf314476247088f74, 0xa5486bdc455d56a2)}, {U128(0x16af5c057a216a8c, 0x08371289cb79dce7), U128(0x7b0a0a42f45c41bd, 0x64895285f2b7e950), U128(0x504d72505d98050c, 0x97c4afa25181e605)}},
  {{U128(0x5fd41175e0c39c34, 0xb336c99ce723d817), U128(0xd861a7ef2e855f75, 0xe59aad1cf16cdb2b), U128(0xf40bdd5a6688662f, 0x5019794a1f5896e4)}, {U128(0xda3ec2ec26e111aa, 0x1ce81f6a7938f852), U128(0xe9b81d82d31b3987, 0x29694eddb5afbcc9), U128(0x4d50430b860546c3, 0x4ffd0691f9e13630)}},
  {{U128(0xd5fd8a1cafb2cce5, 0x2a53096f0c1278a8), U128(0x5ee4b09989a11728, 0x8e77ee63670fa090), U128(0xf4fa0ab6316ed2ec, 0x163c5c7f03b718c5)}, {U128(0x96e8a93742139e47, 0x76c746094bca11de), U128(0xc70534171bf68a81, 0x1c561ea3e50849c2), U128(0x4a5018bb567c16a2, 0xd725d3b9ed35fba9)}},
  {{U128(0xd727e908a1cc5001, 0xd24303f2f370d960), U128(0xcb773db17e36deb8, 0xa64033468f47f60f), U128(0xf5dec646f85ba1c6, 0xc8c615e72768d6b4)}, {U128(0xa75ab2ef3590737e, 0x5257cf0b424aab42), U128(0x2b110165bc3ba0b2, 0xe1a97db3c5d0fc6c), U128(0x474d10fd336cf746, 0xf95f1f657313c4ea)}},
  {{U128(0xafdc40732a1b03f6, 0x0f46e91acb4eeab8), U128(0xb06854a7d3139b87, 0xf2f68677a56efb23), U128(0xf6ba073b424b19e8, 0x2c791f59cc1ffc22)}, {U128(0x2098bb3f6852d2b1, 0xfd197afa03c1844e), U128(0xf735312b47773c26, 0x6a6e3a60fdae388e), U128(0x4447498ac7d9dd82, 0x4c20ab7aa99a2182)}},
  {{U128(0x600449cd14f8b1ec, 0x52bfd4ada717a31c), U128(0x4f10d75812d26a6d, 0x901915a71344b708), U128(0xf78bc51f239e12c6, 0x214cffcee9dd33ca)}, {U128(0xa3808a70a1da1d66, 0x0fa8580dc2529179), U128(0xcb01c0a806a3322e, 0xce36ba75ab4ffe02), U128(0x413ee038dff6b7fd, 0x7da6491b4f867803)}},
  {{U128(0x18da934f3fc63793, 0x9958f29a37efd515), U128(0x95a1acb343363fb2, 0x6d1932845f3813dd), U128(0xf853f7dc9186b952, 0xc7adc6b4988891ba)}, {U128(0xa9bf1fae55e97289, 0x4ffaeb22d748d6d4), U128(0x5580085f9c2247b3, 0x76ceee5dee6d2d54), U128(0x3e33f2f642be355e, 0x90887712e9dc9663)}},
  {{U128(0x09c93255bc853ced, 0x705a76d3418f2b4f), U128(0x2e09716dff309c62, 0x61e131e94ae140be), U128(0xf91297bbb1d6cdbe, 0x68fc6e4d6a920bd2)}, {U128(0x8f7f5e200d41a580, 0x8dee643bbbd353d6), U128(0xc138ad021532194d, 0x04eaaaab0d4998ea), U128(0x3b269fca8a8622ba, 0xdc913b883d64660e)}},
  {{U128(0xa87cf32ce084af6a, 0x5af910ded9deeb17), U128(0x79b567a67cde8b61, 0x61e63b20928c47af), U128(0xf9c79d63272c4628, 0x4504ae08d19b2980)}, {U128(0x276234f6f3e1e1eb, 0xa623a2a80c4cb131), U128(0x2c9b801c691e71bb, 0x813b716e71055e67), U128(0x381704d4fc9ec5f9, 0x43af186b79b2a0f3)}},
  {{U128(0x8df64f3218965d3c, 0xee268c7681608ebc), U128(0x7865a49bdcd7f074, 0x45770c9f804efeec), U128(0xfa7301d859796671, 0x1fe196a53fb5b237)}, {U128(0xd0fcf9cf13165ffe, 0xf465ff2dcbe8ac4b), U128(0x8c47e96b350b7c3f, 0x6e7693d88171a2b4), U128(0x3505404b6008a13c, 0x37d0017bda992a80)}},
  {{U128(0x947a5373353c45a4, 0x8d5b15eaa58368ef), U128(0xc5f40e3fd8f18ae1, 0xb1997321b48e8b1b), U128(0xfb14be7fbae58156, 0x2172a361fd2a722e)}, {U128(0xa28fbdbe7eb1987e, 0x24859793181c3718), U128(0xf394e58d12972f1d, 0x39438767895414c1), U128(0x31f17078d34c156c, 0x9732300393f33613)}},
  {{U128(0xe1d49c2b973c884c, 0x1046bcdcf4fccd05), U128(0xf68925b421c48b09, 0xa1971c56ebc10d90), U128(0xfbaccd1d0903bb09, 0xe63ae8632b84473b)}, {U128(0x767dfafab5ba9092, 0x655e186472be5455), U128(0xabcc11744430db5e, 0x8767e77e76440759), U128(0x2edbb3bca17e628e, 0xaf776280d975ac74)}},
  {{U128(0x184b1d6fccfc856e, 0x9c1991b686a00c7d), U128(0x246ddc4e63f59390, 0xb859505a0b301225), U128(0xfc3b27d38a5d49ab, 0x256778ffcb5c1769)}, {U128(0x712beb01d32241b1, 0x1b51a3cdad65a191), U128(0x23d5a2b8dc1e0341, 0xdc32fb8507a753b5), U128(0x2bc42889167f8ca9, 0x8ffbbceed62c7c43)}},
  {{U128(0xeab2d4b6c97209b7, 0xcf685093ab467d00), U128(0x39dbeb5c2d2cbf8e, 0x8b70e81ea4598ee8), U128(0xfcbfc926484cd43a, 0xa3e22b4d38917e73)}, {U128(0x26ace06db55ecfbd, 0x063c572324d01a61), U128(0xfcd3974462ee60e3, 0xd0dcfa34d1aa337f), U128(0x28aaed62527cb3b5, 0xe976fb98213b8497)}},
  {{U128(0x673171e6a6554f35, 0xb393be74c8381589), U128(0xa19702d6d140d729, 0x62b817621ff0f760), U128(0xfd3aabf84528b50b, 0xeae6bd951c1dabbd)}, {U128(0x8e8799e6913803e0, 0x37dd74aaaeb56c5a), U128(0x9fe14048be786e10, 0x3c6f72706b194b88), U128(0x259020dd1cc27444, 0xc002a2684781f07f)}},
  {{U128(0xd3bd6558940206cb, 0xd94accf74de23b6d), U128(0x9569c48a4106edf0, 0xc35ed177651ddbb9), U128(0xfdabcb8caeba091b, 0xfac7397cc07a646f)}, {U128(0xe0692c1d4d6a90c2, 0xdd411df178eb7d1e), U128(0x04e60cacc98164d7, 0xcfe841fd3b113802), U128(0x2273e19db5eaed56, 0xe5e597261830d391)}},
  {{U128(0x300e707e9a9a66e5, 0xf65d64ce2ea940ee), U128(0xcc9d5d787e81f867, 0x92ae1e5d881f153b), U128(0xfe1323870cfe9a3d, 0x90cd1d959db674ee)}, {U128(0xdafc53bdb0f3ad5e, 0x98b1eed416377b10), U128(0xe2427477a6ecf31e, 0xf0c8a6bf13a12290), U128(0x1f564e56a9730e34, 0x4e08e535cadaf146)}},
  {{U128(0x12e11c21cf9985c6, 0x3ac4eca2fbd267bb), U128(0xff4bd7de3aae3a28, 0x7faf2efdd7ae4acc), U128(0xfe70afeb6d33d6a2, 0x2907cf2b3f6feac1)}, {U128(0xd07230f388861771, 0x4b3f495dc82a7a68), U128(0x39773c54cb0e9276, 0x209abd55453a49b8), U128(0x1c3785c79ec2d4f5, 0x86310766026f8dcc)}},
  {{U128(0xa0e9ee7a85258e45, 0x485224a982a7c85a), U128(0x5a2c976d196a3567, 0xb2681b468efba361), U128(0xfec46d1e89292cf0, 0x41390efdc726e9ef)}, {U128(0x01af208bbd6f4012, 0xa801974079e03914), U128(0x21030810a8ddd093, 0xd9abe105fe0fbea3), U128(0x1917a6bc29b42be1, 0xd8e72d912977ee71)}},
  {{U128(0x7fa801bea5d4c3cb, 0xbd57ce6223df6540), U128(0x5f063781804a271a, 0x287f3f198f7cd53c), U128(0xff0e57e5ead848d1, 0x1f1901544271c3f8)}, {U128(0x46f9278d36fb08fe, 0xdbff81d194ca39fa), U128(0xa360bea372e455ef, 0x2eafd08a7f7fc6c4), U128(0x15f6d00a9aa418c1, 0x7fba5f8126cb2985)}},
  {{U128(0x3b7c57bc5c735193, 0xf1419bcc0ff7192e), U128(0x91cf862e7fad71ee, 0x964aad96d95ae9fb), U128(0xff4e6d680c41d0a9, 0x0f668633f1ab8589)}, {U128(0x83d904a423e340df, 0xfd367461499ea9fe), U128(0x825261edb7445e11, 0x5b93892877537a40), U128(0x12d52092ce19f5cc, 0xbeeeae8129a786b8)}},
  {{U128(0xa7ba18398767930a, 0x1018736805f7bf87), U128(0x3cb04da7e6b0bbc3, 0x369ca2e0608be070), U128(0xff84ab2c738d6a03, 0x519c314973ccae6b)}, {U128(0x9684b763f409ea33, 0x3d124a454239f979), U128(0xfb3691daa4fc8f5c, 0xec1cd0d9c7c3b699), U128(0x0fb2b73cfc106ff6, 0x8f0a0e36a000c734)}},
  {{U128(0x1bf58f6a90d520d5, 0xa94ff9046c34d4e4), U128(0xcf53e4baa403250b, 0x5750700a6df26524), U128(0xffb10f1bcb6bef1d, 0x421e8edaaf59453d)}, {U128(0xa8902b24fdd1fb46, 0x72e72bf5af6010c4), U128(0x1216769fcda0d0eb, 0xf36fc97b1fef1fde), U128(0x0c8fb2f886ec09f3, 0x76a17954b2b7c517)}},
  {{U128(0x831dcf0e30781c87, 0xde3ad81725b27622), U128(0x30bd6fbe3cdfb585, 0x2fb15c7502f6de78), U128(0xffd3977ff7bae4e9, 0x664649b4d541b9c5)}, {U128(0x02881e6ab9834899, 0xfc79c4beab7508d1), U128(0x036d93dfef35cbd6, 0xe4df8597c03c05d2), U128(0x096c32baca2ae68b, 0x437b2dd49d5fca3c)}},
  {{U128(0xab5dc1a419785764, 0xb707f21199fe9209), U128(0x568fb69282182f32, 0xef6f30da8fe307e8), U128(0xffec4304266865d9, 0x5657552366961732)}, {U128(0xa8390bc33658b7d6, 0x4cf0323d7cbf0eb5), U128(0x62652f9e1aa508ac, 0x0cf31ac036de81c6), U128(0x0648557de8d99f7e, 0x4e29cf6e5fed0679)}},
  {{U128(0xc7e9f8e9e7f57263, 0xfc29714c032279b1), U128(0xff8d89566a2181db, 0xab255bf76857a287), U128(0xfffb10b4dc96dabb, 0xb47903f7a19f8ee1)}, {U128(0xe36885f0fd7b869a, 0x35b064282559db48), U128(0xf0257406fa2c2f25, 0x06e03a898ebf83db), U128(0x03243a3f9bd8f08c, 0xc3c75f41b6ce7aa7)}},
};
// Magnitudes of the degree-4 minimax for (1-sin(sqrt(u))/sqrt(u))/u, Q128, alternating signs.
// ratapprox --function="(1-sin(sqrt(x))/sqrt(x))/x" --dom="[8.7e-19,3.765e-5]" --type=[4,0] --numF="[140]" --prec=400 --dispCoeff=hex
// Certified Q128 error < 2^-114 on [0, 3.765e-5], containing (pi/512)^2.
static const u128 SIN_FAST[5] = {
  U128(0x2aaaaaaaaaaaaaaa, 0xaaaaaaaaaaaa8b20),
  U128(0x0222222222222222, 0x2222221fa2fd1243),
  U128(0x000d00d00d00d00d, 0x00c7f4be42e1a641),
  U128(0x00002e3bc74aad85, 0x378e3e9d9c3058a3),
  U128(0x0000006b99115ea3, 0xfd77eea5743ff7be),
};
// 1/(2k+3)! * 2^384 for k = 0..17: sin(t)/t = 1 - sum_k (-1)^k SIN_COEF[k] (t^2)^(k+1).
static const u128 SIN_COEF[18][3] = {
  {U128(0xaaaaaaaaaaaaaaaa, 0xaaaaaaaaaaaaaaab), U128(0xaaaaaaaaaaaaaaaa, 0xaaaaaaaaaaaaaaaa), U128(0x2aaaaaaaaaaaaaaa, 0xaaaaaaaaaaaaaaaa)},
  {U128(0x2222222222222222, 0x2222222222222222), U128(0x2222222222222222, 0x2222222222222222), U128(0x0222222222222222, 0x2222222222222222)},
  {U128(0x00d00d00d00d00d0, 0x0d00d00d00d00d01), U128(0x0d00d00d00d00d00, 0xd00d00d00d00d00d), U128(0x000d00d00d00d00d, 0x00d00d00d00d00d0)},
  {U128(0x4aad8e671f558391, 0x1ca002e3bc74aad9), U128(0xe3bc74aad8e671f5, 0x583911ca002e3bc7), U128(0x00002e3bc74aad8e, 0x671f5583911ca002)},
  {U128(0x8ea47ca3d8b8a1c7, 0x232b58766f185556), U128(0x71c7880adcbc46da, 0xaab1643c04a7fbe3), U128(0x0000006b99159fd5, 0x138e3f9d1f92e0df)},
  {U128(0x12f7354fd1ccabb4, 0x25f8129e4c33071c), U128(0xd7b4269d9babdfa2, 0x38e3994206980d1a), U128(0x00000000b092309d, 0x43684be51c198e91)},
  {U128(0xc5937680614dc64d, 0xa868cce37f24d07e), U128(0x774657f48f5eaf63, 0x83ed943c0c38ccdc), U128(0x0000000000d73f9f, 0x399dc0f88ec32b58)},
  {U128(0x938cc7061e79b29c, 0x855335758ad20488), U128(0xcbbb8d7ff53ba468, 0xd621d08b83cf4484), U128(0x000000000000ca96, 0x3b81856a53593028)},
  {U128(0x88aa546b0f0f539c, 0x2750563cf9ab47df), U128(0xf61dbdcb3a5abf5b, 0xa0d03143c6bf7bb5), U128(0x0000000000000097, 0xa4da340a0ab92650)},
  {U128(0x7866ce3854c2797e, 0x8b10a5feed16f5cf), U128(0xbc51bf3b9b914861, 0x21e81d5fdb4ad15c), U128(0x0000000000000000, 0x5c6e3bdb73d5c62f)},
  {U128(0xcb9f1098e6defe37, 0x6575ea11330fbaab), U128(0xb2f70e09bafec4f3, 0x09adfe08ca4c8b09), U128(0x0000000000000000, 0x002ec368262c7033)},
  {U128(0x4bdbff99dad68eee, 0x2c1da2f33a89d6ed), U128(0xa8d4e44a419776f1, 0x0b893fff294c1301), U128(0x0000000000000000, 0x000013f3ccdd165f)},
  {U128(0xf8732f68269a3287, 0x449f0a1409edf676), U128(0x33a8c82a6863c575, 0x09dcd281d2798b54), U128(0x0000000000000000, 0x0000000746ac70b7)},
  {U128(0x93da4f30fec9552f, 0x750cce83ced4786c), U128(0x686b15af57c61cee, 0xcde2523accdebb20), U128(0x0000000000000000, 0x00000000024b3f31)},
  {U128(0x6db749db7122598d, 0xd0811d668aaebfb7), U128(0x973c1fade2170f72, 0x37d35fe1c89db179), U128(0x0000000000000000, 0x000000000000a1a6)},
  {U128(0x824924ad0e690091, 0xd4cdb2b1cb48686d), U128(0x3024a9ba1aa36a70, 0x59bff52e844faa1b), U128(0x0000000000000000, 0x0000000000000027)},
  {U128(0x7ed1981ffbecba15, 0xa29957b4daf43a9a), U128(0x086e2ce38b6c8f94, 0x19e3fad3f0311d9d), U128(0x0000000000000000, 0x0000000000000000)},
  {U128(0xf3fcb2a927344305, 0xc831b36193c49a91), U128(0x00019ec8d1c94e85, 0xaf4c78b15c3d89d2), U128(0x0000000000000000, 0x0000000000000000)},
};
// 1/(2k+2)! * 2^384 for k = 0..17: 1 - cos(t) = sum_k (-1)^k COS_COEF[k] (t^2)^(k+1).
static const u128 COS_COEF[18][3] = {
  {U128(0x0000000000000000, 0x0000000000000000), U128(0x0000000000000000, 0x0000000000000000), U128(0x8000000000000000, 0x0000000000000000)},
  {U128(0xaaaaaaaaaaaaaaaa, 0xaaaaaaaaaaaaaaab), U128(0xaaaaaaaaaaaaaaaa, 0xaaaaaaaaaaaaaaaa), U128(0x0aaaaaaaaaaaaaaa, 0xaaaaaaaaaaaaaaaa)},
  {U128(0x05b05b05b05b05b0, 0x5b05b05b05b05b06), U128(0x5b05b05b05b05b05, 0xb05b05b05b05b05b), U128(0x005b05b05b05b05b, 0x05b05b05b05b05b0)},
  {U128(0xa01a01a01a01a01a, 0x01a01a01a01a01a0), U128(0x01a01a01a01a01a0, 0x1a01a01a01a01a01), U128(0x0001a01a01a01a01, 0xa01a01a01a01a01a)},
  {U128(0x21115b0a4feef38e, 0x82dccd16c60baaaf), U128(0xe392d8777c170b65, 0x559f4e943337d2c7), U128(0x0000049f93edde27, 0xd71cbbc05b4fa999)},
  {U128(0xf68db50da764b825, 0xed98f209de975c72), U128(0xf425f600e7ba5b3c, 0xe38ec85a55b8aa52), U128(0x00000008f76c77fc, 0x6c4bdaa26d4c3d67)},
  {U128(0x93a3f185b38e9e8c, 0xde2401547328375d), U128(0xfd1f2754668c46d4, 0xbaebaf84b75400ef), U128(0x000000000c9cba54, 0x603e4e905d6f8a2e)},
  {U128(0xcc5937680614dc64, 0xda868cce37f24d08), U128(0x8774657f48f5eaf6, 0x383ed943c0c38ccd), U128(0x00000000000d73f9, 0xf399dc0f88ec32b5)},
  {U128(0x24a443f21e233496, 0xeaf6668687b65596), U128(0x4435161554bc33cc, 0xef73a807c0362e79), U128(0x0000000000000b41, 0x3c31dcbecbbdd802)},
  {U128(0xe06eea9ef3f3f761, 0x685d9de972e229fe), U128(0x72b4afe3c2eaeff7, 0xc80a68dcfd232c95), U128(0x0000000000000007, 0x950ae900808941ea)},
  {U128(0x4b4a7dbcbe08d6fa, 0x1d98078b9669c55b), U128(0x143242dfcce3b1d5, 0xdea1d2ca2ce07de1), U128(0x0000000000000000, 0x04338e5b6dfe14a5)},
  {U128(0x687bf6065ef3f542, 0x4ee4e9c0b775fd1c), U128(0x7cca4b4067ca9d8a, 0x20673feb086ddb20), U128(0x0000000000000000, 0x0001f2cf01972f57)},
  {U128(0x3425fffc12435444, 0x3cc6101d0c18fe6c), U128(0x72cd1c790285d358, 0x0a4a33b132d1b1f6), U128(0x0000000000000000, 0x000000c4742fe352)},
  {U128(0xbfbaf88cdccea660, 0x427364ee6e11a43b), U128(0xd42174dcf171470d, 0x52a350a9353b32b0), U128(0x0000000000000000, 0x0000000042862898)},
  {U128(0x4931f192b328d82c, 0x3fa28f6acb293737), U128(0x5047d60e60caded4, 0xc2989c574b187db4), U128(0x0000000000000000, 0x000000000013932c)},
  {U128(0xcb6dba4edb8912cc, 0x6e8408eb345575fe), U128(0x34b9e0fd6f10b87b, 0x91be9aff0e44ed8b), U128(0x0000000000000000, 0x000000000000050d)},
  {U128(0x56a7cc5f715d70f5, 0x3af6fdb9ef640312), U128(0x2710231c0fd7a13f, 0x8a2b4af9d6b70c88), U128(0x0000000000000000, 0x0000000000000001)},
  {U128(0x4385d272aa8dafd5, 0xef2eed1a5b6a56f6), U128(0x003bf30652185952, 0x560d71a254e4eb7d), U128(0x0000000000000000, 0x0000000000000000)},
};

/* ---------- wide unsigned arithmetic ---------- */

// leading zeros of a 128-bit value, 128 when it is zero
static inline int clz128(u128 x) {
  u64 h = x >> 64, l = x;
  if (h) return __builtin_clzll(h);
  return l ? 64 + __builtin_clzll(l) : 128;
}

// the low 64 bits of (high:low) >> shift, for shift < 64
static inline u64 funnel_down(u64 low, u64 high, unsigned shift) {
  return (low >> shift) | (high << 1 << (63 - shift));
}

// full 128x128-bit product
static inline void wmul(u128 x, u128 y, u128 *high, u128 *low) {
  u64 xl = x, xh = x >> 64, yl = y, yh = y >> 64;
  u128 ll = (u128)xl * yl, lh = (u128)xl * yh, hl = (u128)xh * yl, hh = (u128)xh * yh;
  u128 mid = (ll >> 64) + (u64)lh + (u64)hl;
  *low = (mid << 64) | (u64)ll;
  *high = hh + (lh >> 64) + (hl >> 64) + (mid >> 64);
}

/* approximate high half of a 128x128-bit product from three 64-bit products:
   up to two units short of the exact high half, never over */
static inline u128 mhi_approx(u128 x, u128 y) {
  u128 xh = x >> 64, xl = (u64)x, yh = y >> 64, yl = (u64)y;
  return xh * yh + ((xh * yl) >> 64) + ((xl * yh) >> 64);
}

static inline u256 add_256(u256 a, u256 b) {
  u256 r;
  int c = __builtin_add_overflow(a.l[0], b.l[0], &r.l[0]);
  r.l[1] = a.l[1] + b.l[1] + c;
  return r;
}

static inline u256 sub_256(u256 a, u256 b) {
  u256 r;
  int c = __builtin_sub_overflow(a.l[0], b.l[0], &r.l[0]);
  r.l[1] = a.l[1] - b.l[1] - c;
  return r;
}

// a + b or a - b without a data-dependent branch
static inline u256 add_signed_256(u256 a, u256 b, int negative) {
  u128 mask = -(u128)negative;
  u256 nb = {{b.l[0] ^ mask, b.l[1] ^ mask}}, one = {{(u128)negative, 0}};
  return add_256(a, add_256(nb, one));
}

// f << shift in the 256-bit frame, for shift < 128
static inline u256 place_256(u128 f, unsigned shift) {
  u256 r = {{f << shift, (f >> 1) >> (127 - shift)}};
  return r;
}

// the top 128 bits of s << shift, for shift < 64
static inline u128 top_256(u256 s, unsigned shift) {
  return (s.l[1] << shift) | ((s.l[0] >> 1) >> (127 - shift));
}

// x >> shift with the bit below it folded into the last bit, 0 < shift < 128
static inline u128 shr_round(u128 x, unsigned shift) {
  return (x >> shift) | ((x >> (shift - 1)) & 1);
}

static inline u384 add_384(u384 a, u384 b) {
  u384 r;
  int c0 = __builtin_add_overflow(a.l[0], b.l[0], &r.l[0]);
  int c1 = __builtin_add_overflow(a.l[1], b.l[1], &r.l[1]);
  int c2 = __builtin_add_overflow(r.l[1], (u128)c0, &r.l[1]);
  r.l[2] = a.l[2] + b.l[2] + c1 + c2;
  return r;
}

static inline u384 sub_384(u384 a, u384 b) {
  u384 r;
  int c0 = __builtin_sub_overflow(a.l[0], b.l[0], &r.l[0]);
  int c1 = __builtin_sub_overflow(a.l[1], b.l[1], &r.l[1]);
  int c2 = __builtin_sub_overflow(r.l[1], (u128)c0, &r.l[1]);
  r.l[2] = a.l[2] - b.l[2] - c1 - c2;
  return r;
}

// x << shift, discarding whatever leaves the window, for shift < 384
static inline u384 shl_384(u384 x, unsigned shift) {
  unsigned word = shift / 128, bits = shift % 128;
  u384 r = {{0, 0, 0}};
  for (unsigned i = word; i < 3; i++) {
    u128 carry = (bits == 0 || i == word) ? 0 : x.l[i - word - 1] >> (128 - bits);
    r.l[i] = (x.l[i - word] << bits) | carry;
  }
  return r;
}

// x >> shift, saturating to zero past the window
static inline u384 shr_384_sat(u384 x, unsigned shift) {
  u384 r = {{0, 0, 0}};
  if (shift >= 384) return r;
  unsigned word = shift / 128, bits = shift % 128;
  for (unsigned i = word; i < 3; i++) {
    u128 carry = (i + 1 < 3 && bits != 0) ? x.l[i + 1] << (128 - bits) : 0;
    r.l[i - word] = (x.l[i] >> bits) | carry;
  }
  return r;
}

// leading zeros of a 384-bit value, 384 when it is zero
static inline int lz_384(u384 x) {
  if (x.l[2]) return clz128(x.l[2]);
  if (x.l[1]) return 128 + clz128(x.l[1]);
  return 256 + clz128(x.l[0]);
}

/* high 384 bits of a 384x384-bit product: the dropped tail leaves it up to
   four units short of the exact value, never over */
static inline u384 mul_hi_384(u384 a, u384 b) {
  u128 h02, l02, h11, l11, h20, l20, h12, l12, h21, l21, h22, l22, sum, diagonal;
  wmul(a.l[0], b.l[2], &h02, &l02);
  wmul(a.l[1], b.l[1], &h11, &l11);
  wmul(a.l[2], b.l[0], &h20, &l20);
  wmul(a.l[1], b.l[2], &h12, &l12);
  wmul(a.l[2], b.l[1], &h21, &l21);
  wmul(a.l[2], b.l[2], &h22, &l22);
  /* The diagonal one limb below the window contributes its high halves plus
     the carry of its low halves; everything further down is the slack above. */
  int carry_low = __builtin_add_overflow(l02, l11, &sum);
  int carry_high = __builtin_add_overflow(sum, l20, &sum);
  int first = __builtin_add_overflow(h02, h11, &diagonal);
  int second = __builtin_add_overflow(diagonal, h20, &diagonal);
  int third = __builtin_add_overflow(diagonal, (u128)carry_low + carry_high, &diagonal);
  u384 t1 = {{l12, h12, 0}}, t2 = {{l21, h21, 0}}, t3 = {{0, l22, h22}};
  u384 t4 = {{diagonal, (u128)first + second + third, 0}};
  return add_384(add_384(t1, t2), add_384(t3, t4));
}

/* high 256 bits of a 256x256-bit product: the dropped tail leaves it up to
   one unit short of the exact value, never over */
static inline u256 mul_hi_256(u256 a, u256 b) {
  u128 high, low, cross_high, cross_low, other_high, other_low, sum, middle;
  wmul(a.l[1], b.l[1], &high, &low);
  wmul(a.l[1], b.l[0], &cross_high, &cross_low);
  wmul(a.l[0], b.l[1], &other_high, &other_low);
  /* The two cross terms and the top of a[0]*b[0] all land one limb below the
     window, so only their carry into it survives. */
  int carry = __builtin_add_overflow(cross_low, other_low, &sum);
  int tail_carry = __builtin_add_overflow(sum, mhi_approx(a.l[0], b.l[0]), &sum);
  int overflow = __builtin_add_overflow(cross_high, other_high, &middle);
  int spill = __builtin_add_overflow(middle, (u128)carry + tail_carry, &middle);
  int final_carry = __builtin_add_overflow(low, middle, &low);
  u256 r = {{low, high + overflow + spill + final_carry}};
  return r;
}

/* ---------- reduction ---------- */

/* m*win as W+3 little-endian 64-bit limbs, for a window win of W limbs most
   significant first: the product fills W+2 of them and the spare top limb
   stays zero so the funnels' last read is in range. */
static inline void product(u128 m, const u64 *win, int W, u64 *p) {
  u128 low = (u64)m, high = m >> 64, carry = 0;
  for (int c = 0; c < W + 3; c++) {
    u128 cur = c < W ? win[W - 1 - c] : 0;
    u128 prev = (c >= 1 && c <= W) ? win[W - c] : 0;
    /* high < 2^49 keeps the first sum under 2^114; only the second can
       spill, and its bit goes straight into the carry. */
    u128 acc = high * prev + carry, t;
    int spill = __builtin_add_overflow(acc, low * cur, &t);
    p[c] = (u64)t;
    carry = (t >> 64) | ((u128)spill << 64);
  }
}

/* The 2/pi window and the product's alignment for x = m*2^(e-112): bits of
   2/pi above position e-114 contribute multiples of 4 to x*2/pi, so the
   window starts at the limb holding bit e-113; within it, *shift says how
   far the units bit of x*2/pi sits below the product's bit 64*W-2 --
   negative below |x| = 2^114, where the window is the top of 2/pi. */
static inline const u64 *window(int e, int *shift) {
  int big = e - 114;
  if (big >= 0) {
    *shift = big % 64;
    return FRAC_2_PI + big / 64;
  }
  *shift = big;
  return FRAC_2_PI;
}

// the reduced angle theta = |g|*pi/2 as t1*2^(et-128), and its breakpoint
typedef struct {
  int n;        // round(256*x/pi) mod 512: quadrant n>>7, breakpoint n&127
  int negative; // g < 0: the angle sits below its breakpoint
  u128 t1;
  int et;
} residual;

/* Payne-Hanek on six limbs of 2/pi; 0 when the residual keeps fewer than
   136 of the fraction's 192 bits.  Five limbs would leave the tail of 2/pi
   below the window at 2^(shift-205) of the units bit, up to 2^-142, which
   the sine near a multiple of pi cannot afford; six put it under 2^-206. */
static inline int reduce(u128 m, int e, residual *r) {
  int shift;
  const u64 *win = window(e, &shift);
  u64 p[9];
  product(m, win, 6, p);
  /* The fraction is the 192 bits below the units bit, the quadrant the two
     above: four funnels at one offset, from limb i <= 4. */
  unsigned base = 190 - shift; // 127 <= base <= 312
  unsigned i = (base / 64) & 7, bits = base % 64;
  u64 f0 = funnel_down(p[i], p[i + 1], bits);
  u64 f1 = funnel_down(p[i + 1], p[i + 2], bits);
  u64 f2 = funnel_down(p[i + 2], p[i + 3], bits);
  u64 quadrant = funnel_down(p[i + 3], p[i + 4], bits) & 3;

  // n = round(256*x/pi); the residual's top limb becomes signed
  u128 top = ((u128)quadrant << 64) | f2;
  int n = (int)(((top + ((u128)1 << 56)) >> 57) & 511);
  f2 -= (u64)(n & 127) << 57;
  int negative = (i64)f2 < 0;
  u64 mask = -(u64)negative, g0, g1, g2;
  int c0 = __builtin_add_overflow(f0 ^ mask, (u64)negative, &g0);
  int c1 = __builtin_add_overflow(f1 ^ mask, (u64)c0, &g1);
  g2 = (f2 ^ mask) + c1;
  if (g2 == 0) return 0;
  int lz = __builtin_clzll(g2);
  if (lz > MAX_LZ) return 0;
  /* |g| <= 1/256 leaves at least seven leading zeros, so every shift below
     is in range; |g| = G*2^(-128-lz) for the top 128 bits G. */
  u64 high = (g2 << lz) | (g1 >> (64 - lz));
  u64 low = (g1 << lz) | (g0 >> (64 - lz));
  u128 g = ((u128)high << 64) | low, ph, pl;
  wmul(g, PIO2_128, &ph, &pl);
  int lzt = clz128(ph);
  u256 t = {{pl, ph}};
  r->n = n;
  r->negative = negative;
  r->t1 = top_256(t, lzt);
  r->et = 1 - (lz + lzt);
  return 1;
}

/* theta^2 rounded into a 2^-128 word (u), theta^4 likewise (v), and the
   unshifted theta^2 = u1*2^(2*et-128) the cosine's correction rides on */
static inline void squares(u128 t1, int et, u128 *u, u128 *v, u128 *u1) {
  *u1 = mhi_approx(t1, t1);
  *u = shr_round(*u1, (unsigned)(-2 * et));
  *v = mhi_approx(*u, *u);
}

/* sin(theta) in theta's own floating form: t1*(1 - u*(A - u*B)) with the
   even and odd halves of the degree-four minimax SIN_FAST in v.
   Its contribution to the ratio is below u*2^-114 < 2^-128.69. */
static inline u128 sin_frac(u128 t1, u128 u, u128 v) {
#define S(k) SIN_FAST[k]
  u128 a = S(0) + mhi_approx(v, S(2) + mhi_approx(v, S(4)));
  u128 b = S(1) + mhi_approx(v, S(3));
#undef S
  return t1 - mhi_approx(mhi_approx(t1, u), a - mhi_approx(u, b));
}

/* 1 - cos(theta) as the floating fraction c1*2^(2*et-128): theta^2*(1/2 -
   u*W) with W the even and odd halves of sum (-1)^k u^k/(2k+4)! in v. Six
   terms: the omitted seventh contributes less than 2^-139 to cos(theta). */
static inline u128 cos_corr(u128 u1, u128 u, u128 v) {
#define C(k) COS_COEF[k][2]
  u128 e = C(1) + mhi_approx(v, C(3) + mhi_approx(v, C(5)));
  u128 o = C(2) + mhi_approx(v, C(4));
  return mhi_approx(u1, C(0) - mhi_approx(u, e - mhi_approx(u, o)));
#undef C
}

/* The fast path: the magnitude as a floating 128-bit fraction frac*2^(e2-128)
   with the sign it contributes in *flip, or 0 when the reduction cannot
   vouch for it. */
static inline int fast(u128 m, int e, int cosine, u128 *frac, int *e2, u128 *flip) {
  u128 u, v, u1;
  if (e < DIRECT) {
    u128 t1 = m << 15;
    int et = e + 1;
    squares(t1, et, &u, &v, &u1);
    if (cosine) {
      u256 all = {{~(u128)0, ~(u128)0}};
      *frac = sub_256(all, place_256(cos_corr(u1, u, v), 2 * et + 128)).l[1];
      *e2 = 0;
    } else {
      // sin(theta) < theta drops a leading bit when t1 starts a binade
      u128 s1 = sin_frac(t1, u, v);
      int lz = clz128(s1);
      *frac = s1 << lz;
      *e2 = et - lz;
    }
    *flip = 0;
    return 1;
  }
  residual r;
  if (!reduce(m, e, &r)) return 0;
  squares(r.t1, r.et, &u, &v, &u1);
  u128 s1 = sin_frac(r.t1, u, v);
  /* The cosine is the sine a quadrant on; the top bit of the quadrant is the
     sign, the low bit which of sin A and cos A is wanted. */
  int k = (r.n >> 7) + cosine, want_cos = k & 1, j = r.n & 127;
  *flip = (k & 2) ? SIGN_MASK : 0;
  if (j == 0 && !want_cos) {
    int lz = clz128(s1);
    *flip ^= r.negative ? SIGN_MASK : 0;
    *frac = s1 << lz;
    *e2 = r.et - lz;
    return 1;
  }
  u128 c1 = cos_corr(u1, u, v);
  const u128 *first = SINCOS[j][want_cos], *second = SINCOS[j][!want_cos];
  u256 base = {{first[1], first[2]}};
  u256 c = place_256(mhi_approx(first[2], c1), 2 * r.et + 128);
  u256 s = place_256(mhi_approx(second[2], s1), r.et + 128);
  u256 f = add_signed_256(sub_256(base, c), s, r.negative != want_cos);
  int lz = clz128(f.l[1]);
  *frac = top_256(f, lz);
  *e2 = -lz;
  return 1;
}

/* Round the fast frame on its fixed 15-bit guard; 0 hands the window around
   the rounding boundary to the accurate path.  The result is never subnormal
   here: |x| >= 2^-57 keeps it above 2^-124. */
static inline int round_fast(u128 frac, int e2, u128 sign, unsigned rm, u128 *out) {
  unsigned rest = frac & GUARD_MASK;
  // the boundary is the midpoint in round-to-nearest, the grid point otherwise
  unsigned boundary = (rm == RM_NEAREST) << (GUARD - 1);
  if (((rest + ZIV_GATE - boundary) & GUARD_MASK) <= 2 * ZIV_GATE) return 0;
  /* The gate has already ruled out the boundary: the guard's top bit decides
     in round-to-nearest, and the discarded bits cannot flip it; a directed
     mode rounds the truncated frame by its direction alone. */
  int up = rm == RM_NEAREST ? rest > GUARD_HALF : round_away(rm, sign);
  *out = sign | (((u128)(e2 + 16382) << 112) + (frac >> GUARD) - IMPLICIT_BIT + up);
  return 1;
}

/* ---------- the 384-bit path ---------- */

// reduce() on ten limbs of 2/pi and a 448-bit fraction, the residual as a
// normalized 384-bit fraction t*2^(et-384)
static void reduce_wide(u128 m, int e, int *n, int *negative, u384 *t, int *et) {
  int shift;
  const u64 *win = window(e, &shift);
  u64 p[13];
  product(m, win, 10, p);
  unsigned base = 190 - shift; // 127 <= base <= 312
  unsigned i = (base / 64) & 7, bits = base % 64;
  u64 f[7];
  for (int k = 0; k < 7; k++) f[k] = funnel_down(p[i + k], p[i + k + 1], bits);
  u64 quadrant = funnel_down(p[i + 7], p[i + 8], bits) & 3;
  u128 top = ((u128)quadrant << 64) | f[6];
  int nn = (int)(((top + ((u128)1 << 56)) >> 57) & 511);
  f[6] -= (u64)(nn & 127) << 57;
  int neg = (i64)f[6] < 0;
  u64 mask = -(u64)neg;
  int carry = neg;
  for (int k = 0; k < 7; k++) carry = __builtin_add_overflow(f[k] ^ mask, (u64)carry, &f[k]);
  int lz = 448;
  for (int k = 6; k >= 0; k--)
    if (f[k]) { lz = 64 * (6 - k) + __builtin_clzll(f[k]); break; }
  /* No binary128 comes within 2^-124 of a multiple of pi/2, so the residual
     keeps over 300 of its 448 bits (lz < 140); ten limbs leave the tail of
     2/pi under 2^-462 of the units bit, so all 448 are meaningful. */
  unsigned word = lz / 64, wbits = lz % 64;
  u64 s[7] = {0, 0, 0, 0, 0, 0, 0};
  for (unsigned k = word; k < 7; k++) {
    u64 c = (wbits == 0 || k == word) ? 0 : f[k - word - 1] >> (64 - wbits);
    s[k] = (f[k - word] << wbits) | c;
  }
  u384 g = {{(u128)s[1] | (u128)s[2] << 64, (u128)s[3] | (u128)s[4] << 64, (u128)s[5] | (u128)s[6] << 64}};
  u384 pio2 = {{PIO2_384[0], PIO2_384[1], PIO2_384[2]}};
  u384 tt = mul_hi_384(g, pio2);
  int lzt = clz128(tt.l[2]);
  *n = nn;
  *negative = neg;
  *t = shl_384(tt, lzt);
  *et = 1 - (lz + lzt);
}

/* sum_{k>=0} (-1)^k coef[k]*u^k, eighteen terms by Horner in three tiers:
   terms 11..17 at 128 bits, 2..10 at 256, 0..1 at 384.  Every step stays
   positive because u*coef[k+1] < coef[k].  Each tier's slack -- under 2^-126
   for the narrow one, 2^-253 for the middle -- rides u^11 < 2^-161 or
   u^2 < 2^-29 (u < 2^-14.68) into the sum, so the result is within 2^-282 of
   the exact value, and the caller's multiply by u puts that below 2^-296.
   Mirrors correction_384 in metallic-rs's atan2.rs. */
static u384 alternating(u384 u, const u128 coef[][3]) {
  u128 narrow = coef[17][2];
  for (int k = 16; k >= 11; k--)
    narrow = coef[k][2] - mhi_approx(u.l[2], narrow);
  u256 middle = {{0, narrow}}, u2 = {{u.l[1], u.l[2]}};
  for (int k = 10; k >= 2; k--) {
    u256 c = {{coef[k][1], coef[k][2]}};
    middle = sub_256(c, mul_hi_256(u2, middle));
  }
  u384 q = {{0, middle.l[0], middle.l[1]}};
  for (int k = 1; k >= 0; k--) {
    u384 c = {{coef[k][0], coef[k][1], coef[k][2]}};
    q = sub_384(c, mul_hi_384(u, q));
  }
  return q;
}

/* Round a normalized 384-bit fraction frac*2^(e2-384) to binary128 in the
   current mode.  The result is never subnormal here (see round_fast). */
static inline u128 round_384(u384 frac, int e2, u128 sign, unsigned rm) {
  u128 mantissa = frac.l[2] >> GUARD;
  int round_bit = (frac.l[2] >> (GUARD - 1)) & 1;
  int sticky = ((frac.l[2] & (GUARD_MASK >> 1)) | frac.l[1] | frac.l[0]) != 0;
  int up = rm == RM_NEAREST ? round_bit && (sticky || (mantissa & 1))
                            : round_away(rm, sign) && (round_bit || sticky);
  return sign | (((u128)(e2 + 16382) << 112) + (mantissa - IMPLICIT_BIT) + up);
}

// the 384-bit path: everything the fast path would not decide
static u128 __attribute__((noinline, cold)) accurate(u128 m, int e, int cosine, u128 sign, unsigned rm) {
  int n, negative, et;
  u384 t;
  if (e < DIRECT) {
    n = 0;
    negative = 0;
    t.l[0] = 0;
    t.l[1] = 0;
    t.l[2] = m << 15;
    et = e + 1;
  } else
    reduce_wide(m, e, &n, &negative, &t, &et);
  u384 u = shr_384_sat(mul_hi_384(t, t), (unsigned)(-2 * et));
  int k = (n >> 7) + cosine, want_cos = k & 1, j = n & 127;
  sign ^= (k & 2) ? SIGN_MASK : 0;

  // sin(theta) = theta*(1 - u*Q) keeps theta's floating form, at most one bit short
  u384 s = sub_384(t, mul_hi_384(t, mul_hi_384(u, alternating(u, SIN_COEF))));
  int lzs = lz_384(s);
  s = shl_384(s, lzs);
  int es = et - lzs;
  if (j == 0 && !want_cos)
    return round_384(s, es, sign ^ (negative ? SIGN_MASK : 0), rm);

  // 1 - cos(theta) = u*Q, so T*cos(theta) = T - T*(u*Q) never overflows the frame
  u384 c = mul_hi_384(u, alternating(u, COS_COEF));
  u384 first = {{SINCOS[j][want_cos][0], SINCOS[j][want_cos][1], SINCOS[j][want_cos][2]}};
  u384 second = {{SINCOS[j][!want_cos][0], SINCOS[j][!want_cos][1], SINCOS[j][!want_cos][2]}};
  u384 base = sub_384(first, mul_hi_384(first, c));
  u384 term = shr_384_sat(mul_hi_384(second, s), (unsigned)(-es));
  u384 f = (negative != want_cos) ? sub_384(base, term) : add_384(base, term);
  int lz = lz_384(f);
  return round_384(shl_384(f, lz), -lz, sign, rm);
}

/* ---------- entry ---------- */

static inline __float128 __attribute__((always_inline)) trig(__float128 x, int cosine) {
  unsigned rm, flags, oflags;
  FENV_GET(rm, flags);
  oflags = flags;
  u128 bits = asu128(x), ax = bits & ~SIGN_MASK, res;
  if (__builtin_expect(ax >= EXP_MASK, 0)) { // Inf or NaN
    if (ax == EXP_MASK) { // +-Inf: domain error
#ifdef CORE_MATH_SUPPORT_ERRNO
      errno = EDOM;
#endif
      flags |= FE_INVALID;
      FENV_RAISE(oflags, flags);
      return asf128(EXP_MASK | QUIET_BIT);
    }
    if (!(ax & QUIET_BIT)) { // signaling NaN
      flags |= FE_INVALID;
      FENV_RAISE(oflags, flags);
    }
    return asf128(bits | QUIET_BIT);
  }
  if (__builtin_expect(ax < TINY, 0)) {
    if (ax == 0) return cosine ? asf128(ONE) : x; // exact
    // sin(x) sits below x in magnitude and cos(x) below 1
    if (cosine)
      res = ONE - (rm != RM_NEAREST && !round_away(rm, 0));
    else {
      res = ax - (rm != RM_NEAREST && !round_away(rm, bits & SIGN_MASK));
      if (res < IMPLICIT_BIT) { // subnormal or zero after rounding
#ifdef CORE_MATH_SUPPORT_ERRNO
        errno = ERANGE;
#endif
        flags |= FE_UNDERFLOW;
      }
      res |= bits & SIGN_MASK;
    }
    flags |= FE_INEXACT;
    FENV_RAISE(oflags, flags);
    return asf128(res);
  }
  u128 m = (ax & MANTISSA_MASK) | IMPLICIT_BIT, sign = cosine ? 0 : bits & SIGN_MASK;
  int e = (int)(ax >> 112) - BIAS;
  u128 frac, flip;
  int e2;
  if (!(fast(m, e, cosine, &frac, &e2, &flip) && round_fast(frac, e2, sign ^ flip, rm, &res)))
    res = accurate(m, e, cosine, sign, rm);
  flags |= FE_INEXACT;
  FENV_RAISE(oflags, flags);
  return asf128(res);
}

__float128 cr_cosq(__float128 x) {
  return trig(x, 1);
}

#ifndef __APPLE__
// somewhat we need to include that for icx and the Intel math library
extern __float128 __cosq (__float128);

// cosq is called cosf128 in GNU libc, and __cosq in the Intel math library
__float128 cosq(__float128 x) {
#ifdef __INTEL_CLANG_COMPILER
  return __cosq (x);
#else
  return cosf128 (x);
#endif
}
#endif