numr 0.5.2

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
//! AVX-512 mathematical function implementations
//!
//! Provides vectorized transcendental functions using 512-bit registers.
//! All algorithms and coefficients are documented in `common.rs`.
//!
//! # Supported Functions
//!
//! | Function | f32 | f64 | Relative Error |
//! |----------|-----|-----|----------------|
//! | exp      | ✓   | ✓   | < 1e-6 / 1e-12 |
//! | tanh     | ✓   | ✓   | < 1e-6 / 1e-12 |
//! | log      | ✓   | ✓   | < 1e-6 / 1e-12 |
//! | sin      | ✓   | ✓   | < 1e-6 / 1e-10 |
//! | cos      | ✓   | ✓   | < 1e-6 / 1e-10 |
//! | tan      | ✓   | ✓   | < 2e-4 / 1e-4  |
//! | atan     | ✓   | ✓   | < 1e-6 / 1e-12 |
//!
//! # Safety
//!
//! All functions require AVX-512F CPU feature.
//!
//! # Advantages over AVX2
//!
//! - Native 64-bit integer conversion (`_mm512_cvtpd_epi64`, `_mm512_cvtepi64_pd`)
//! - Masked operations for branchless conditionals
//! - Twice the throughput (16 f32 or 8 f64 per operation)

#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

use super::common::{
    atan_coefficients, exp_coefficients, log_coefficients, tan_coefficients, trig_coefficients,
};

// ============================================================================
// Exponential function: exp(x)
// ============================================================================

/// Fast SIMD exp approximation for f32 using AVX-512
///
/// See `common::_EXP_ALGORITHM_DOC` for algorithm details.
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn exp_f32(x: __m512) -> __m512 {
    use exp_coefficients::*;

    let log2e = _mm512_set1_ps(std::f32::consts::LOG2_E);
    let ln2 = _mm512_set1_ps(std::f32::consts::LN_2);

    let c0 = _mm512_set1_ps(C0_F32);
    let c1 = _mm512_set1_ps(C1_F32);
    let c2 = _mm512_set1_ps(C2_F32);
    let c3 = _mm512_set1_ps(C3_F32);
    let c4 = _mm512_set1_ps(C4_F32);
    let c5 = _mm512_set1_ps(C5_F32);
    let c6 = _mm512_set1_ps(C6_F32);

    // Clamp input to avoid overflow/underflow
    let x = _mm512_max_ps(x, _mm512_set1_ps(MIN_F32));
    let x = _mm512_min_ps(x, _mm512_set1_ps(MAX_F32));

    // y = x * log2(e)
    let y = _mm512_mul_ps(x, log2e);

    // n = round(y)
    let n = _mm512_roundscale_ps::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(y);

    // f = y - n (fractional part)
    let f = _mm512_sub_ps(y, n);

    // r = f * ln(2)
    let r = _mm512_mul_ps(f, ln2);

    // Polynomial approximation
    let r2 = _mm512_mul_ps(r, r);
    let r3 = _mm512_mul_ps(r2, r);
    let r4 = _mm512_mul_ps(r2, r2);
    let r5 = _mm512_mul_ps(r4, r);
    let r6 = _mm512_mul_ps(r4, r2);

    let mut poly = c0;
    poly = _mm512_fmadd_ps(c1, r, poly);
    poly = _mm512_fmadd_ps(c2, r2, poly);
    poly = _mm512_fmadd_ps(c3, r3, poly);
    poly = _mm512_fmadd_ps(c4, r4, poly);
    poly = _mm512_fmadd_ps(c5, r5, poly);
    poly = _mm512_fmadd_ps(c6, r6, poly);

    // Compute 2^n using IEEE 754 bit manipulation
    let n_i32 = _mm512_cvtps_epi32(n);
    let bias = _mm512_set1_epi32(127);
    let exp_bits = _mm512_slli_epi32::<23>(_mm512_add_epi32(n_i32, bias));
    let pow2n = _mm512_castsi512_ps(exp_bits);

    _mm512_mul_ps(pow2n, poly)
}

/// Fast SIMD exp approximation for f64 using AVX-512
///
/// See `common::_EXP_ALGORITHM_DOC` for algorithm details.
///
/// # Note
/// Unlike AVX2, AVX-512 has native 64-bit integer conversion, so this
/// implementation is fully vectorized with no scalar operations.
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn exp_f64(x: __m512d) -> __m512d {
    use exp_coefficients::*;

    let log2e = _mm512_set1_pd(std::f64::consts::LOG2_E);
    let ln2 = _mm512_set1_pd(std::f64::consts::LN_2);

    let c0 = _mm512_set1_pd(C0_F64);
    let c1 = _mm512_set1_pd(C1_F64);
    let c2 = _mm512_set1_pd(C2_F64);
    let c3 = _mm512_set1_pd(C3_F64);
    let c4 = _mm512_set1_pd(C4_F64);
    let c5 = _mm512_set1_pd(C5_F64);
    let c6 = _mm512_set1_pd(C6_F64);

    // Clamp input
    let x = _mm512_max_pd(x, _mm512_set1_pd(MIN_F64));
    let x = _mm512_min_pd(x, _mm512_set1_pd(MAX_F64));

    let y = _mm512_mul_pd(x, log2e);
    let n = _mm512_roundscale_pd::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(y);
    let f = _mm512_sub_pd(y, n);
    let r = _mm512_mul_pd(f, ln2);

    let r2 = _mm512_mul_pd(r, r);
    let r3 = _mm512_mul_pd(r2, r);
    let r4 = _mm512_mul_pd(r2, r2);
    let r5 = _mm512_mul_pd(r4, r);
    let r6 = _mm512_mul_pd(r4, r2);

    let mut poly = c0;
    poly = _mm512_fmadd_pd(c1, r, poly);
    poly = _mm512_fmadd_pd(c2, r2, poly);
    poly = _mm512_fmadd_pd(c3, r3, poly);
    poly = _mm512_fmadd_pd(c4, r4, poly);
    poly = _mm512_fmadd_pd(c5, r5, poly);
    poly = _mm512_fmadd_pd(c6, r6, poly);

    // AVX-512 has native 64-bit integer conversion
    let n_i64 = _mm512_cvtpd_epi64(n);
    let bias = _mm512_set1_epi64(1023);
    let exp_bits = _mm512_slli_epi64::<52>(_mm512_add_epi64(n_i64, bias));
    let pow2n = _mm512_castsi512_pd(exp_bits);

    _mm512_mul_pd(pow2n, poly)
}

// ============================================================================
// Hyperbolic tangent: tanh(x)
// ============================================================================

/// Fast SIMD tanh approximation for f32 using AVX-512
///
/// Algorithm: tanh(x) = (exp(2x) - 1) / (exp(2x) + 1)
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn tanh_f32(x: __m512) -> __m512 {
    let two = _mm512_set1_ps(2.0);
    let one = _mm512_set1_ps(1.0);

    let exp2x = exp_f32(_mm512_mul_ps(two, x));
    let num = _mm512_sub_ps(exp2x, one);
    let den = _mm512_add_ps(exp2x, one);

    _mm512_div_ps(num, den)
}

/// Fast SIMD tanh approximation for f64 using AVX-512
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn tanh_f64(x: __m512d) -> __m512d {
    let two = _mm512_set1_pd(2.0);
    let one = _mm512_set1_pd(1.0);

    let exp2x = exp_f64(_mm512_mul_pd(two, x));
    let num = _mm512_sub_pd(exp2x, one);
    let den = _mm512_add_pd(exp2x, one);

    _mm512_div_pd(num, den)
}

// ============================================================================
// Natural logarithm: log(x)
// ============================================================================

/// Fast SIMD log approximation for f32 using AVX-512
///
/// See `common::_LOG_ALGORITHM_DOC` for algorithm details.
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn log_f32(x: __m512) -> __m512 {
    use log_coefficients::*;

    let one = _mm512_set1_ps(1.0);
    let ln2 = _mm512_set1_ps(std::f32::consts::LN_2);
    let sqrt2 = _mm512_set1_ps(std::f32::consts::SQRT_2);
    let half = _mm512_set1_ps(0.5);

    let c1 = _mm512_set1_ps(C1_F32);
    let c2 = _mm512_set1_ps(C2_F32);
    let c3 = _mm512_set1_ps(C3_F32);
    let c4 = _mm512_set1_ps(C4_F32);
    let c5 = _mm512_set1_ps(C5_F32);
    let c6 = _mm512_set1_ps(C6_F32);
    let c7 = _mm512_set1_ps(C7_F32);

    // Extract exponent
    let x_bits = _mm512_castps_si512(x);
    let exp_raw = _mm512_srli_epi32::<23>(x_bits);
    let exp_unbiased = _mm512_sub_epi32(exp_raw, _mm512_set1_epi32(EXP_BIAS_F32));
    let mut n = _mm512_cvtepi32_ps(exp_unbiased);

    // Extract mantissa in [1, 2)
    let mantissa_mask = _mm512_set1_epi32(MANTISSA_MASK_F32);
    let exp_zero = _mm512_set1_epi32(EXP_ZERO_F32);
    let m_bits = _mm512_or_si512(_mm512_and_si512(x_bits, mantissa_mask), exp_zero);
    let mut m = _mm512_castsi512_ps(m_bits);

    // Normalize: if m > sqrt(2), divide by 2 and increment exponent
    // AVX-512 masked operations are branchless
    let need_adjust = _mm512_cmp_ps_mask::<_CMP_GT_OQ>(m, sqrt2);
    m = _mm512_mask_mul_ps(m, need_adjust, m, half);
    n = _mm512_mask_add_ps(n, need_adjust, n, one);

    let f = _mm512_sub_ps(m, one);

    // Horner's method
    let mut poly = c7;
    poly = _mm512_fmadd_ps(poly, f, c6);
    poly = _mm512_fmadd_ps(poly, f, c5);
    poly = _mm512_fmadd_ps(poly, f, c4);
    poly = _mm512_fmadd_ps(poly, f, c3);
    poly = _mm512_fmadd_ps(poly, f, c2);
    poly = _mm512_fmadd_ps(poly, f, c1);
    poly = _mm512_mul_ps(poly, f);

    _mm512_fmadd_ps(n, ln2, poly)
}

/// Fast SIMD log approximation for f64 using AVX-512
///
/// See `common::_LOG_ALGORITHM_DOC` for algorithm details.
///
/// # Note
/// AVX-512 has native 64-bit integer operations, so this implementation
/// is fully vectorized with no scalar operations.
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn log_f64(x: __m512d) -> __m512d {
    use log_coefficients::*;

    let one = _mm512_set1_pd(1.0);
    let ln2 = _mm512_set1_pd(std::f64::consts::LN_2);
    let sqrt2 = _mm512_set1_pd(std::f64::consts::SQRT_2);
    let half = _mm512_set1_pd(0.5);

    let c1 = _mm512_set1_pd(C1_F64);
    let c2 = _mm512_set1_pd(C2_F64);
    let c3 = _mm512_set1_pd(C3_F64);
    let c4 = _mm512_set1_pd(C4_F64);
    let c5 = _mm512_set1_pd(C5_F64);
    let c6 = _mm512_set1_pd(C6_F64);
    let c7 = _mm512_set1_pd(C7_F64);
    let c8 = _mm512_set1_pd(C8_F64);
    let c9 = _mm512_set1_pd(C9_F64);

    // Extract exponent using AVX-512 native 64-bit ops
    let x_bits = _mm512_castpd_si512(x);
    let exp_raw = _mm512_srli_epi64::<52>(x_bits);
    let exp_unbiased = _mm512_sub_epi64(exp_raw, _mm512_set1_epi64(EXP_BIAS_F64));
    let mut n = _mm512_cvtepi64_pd(exp_unbiased);

    // Extract mantissa in [1, 2)
    let mantissa_mask = _mm512_set1_epi64(MANTISSA_MASK_F64 as i64);
    let exp_zero = _mm512_set1_epi64(EXP_ZERO_F64 as i64);
    let m_bits = _mm512_or_si512(_mm512_and_si512(x_bits, mantissa_mask), exp_zero);
    let mut m = _mm512_castsi512_pd(m_bits);

    // Normalize: if m > sqrt(2), divide by 2 and increment exponent
    let need_adjust = _mm512_cmp_pd_mask::<_CMP_GT_OQ>(m, sqrt2);
    m = _mm512_mask_mul_pd(m, need_adjust, m, half);
    n = _mm512_mask_add_pd(n, need_adjust, n, one);

    let f = _mm512_sub_pd(m, one);

    // Horner's method
    let mut poly = c9;
    poly = _mm512_fmadd_pd(poly, f, c8);
    poly = _mm512_fmadd_pd(poly, f, c7);
    poly = _mm512_fmadd_pd(poly, f, c6);
    poly = _mm512_fmadd_pd(poly, f, c5);
    poly = _mm512_fmadd_pd(poly, f, c4);
    poly = _mm512_fmadd_pd(poly, f, c3);
    poly = _mm512_fmadd_pd(poly, f, c2);
    poly = _mm512_fmadd_pd(poly, f, c1);
    poly = _mm512_mul_pd(poly, f);

    _mm512_fmadd_pd(n, ln2, poly)
}

// ============================================================================
// Trigonometric functions: sin, cos, tan
// ============================================================================

/// Fast SIMD sin approximation for f32 using AVX-512
///
/// See `common::_TRIG_ALGORITHM_DOC` for algorithm details.
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn sin_f32(x: __m512) -> __m512 {
    use trig_coefficients::*;

    let two_over_pi = _mm512_set1_ps(std::f32::consts::FRAC_2_PI);
    let pi_over_2 = _mm512_set1_ps(std::f32::consts::FRAC_PI_2);

    let s1 = _mm512_set1_ps(S1_F32);
    let s3 = _mm512_set1_ps(S3_F32);
    let s5 = _mm512_set1_ps(S5_F32);
    let s7 = _mm512_set1_ps(S7_F32);

    let c0 = _mm512_set1_ps(C0_F32);
    let c2 = _mm512_set1_ps(C2_F32);
    let c4 = _mm512_set1_ps(C4_F32);
    let c6 = _mm512_set1_ps(C6_F32);

    // Range reduction
    let j = _mm512_roundscale_ps::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(
        _mm512_mul_ps(x, two_over_pi),
    );
    let j_int = _mm512_cvtps_epi32(j);

    let y = _mm512_fnmadd_ps(j, pi_over_2, x);

    let y2 = _mm512_mul_ps(y, y);
    let y3 = _mm512_mul_ps(y2, y);
    let y4 = _mm512_mul_ps(y2, y2);
    let y5 = _mm512_mul_ps(y4, y);
    let y6 = _mm512_mul_ps(y4, y2);
    let y7 = _mm512_mul_ps(y4, y3);

    // sin(y) polynomial
    let sin_y = _mm512_fmadd_ps(
        s7,
        y7,
        _mm512_fmadd_ps(s5, y5, _mm512_fmadd_ps(s3, y3, _mm512_mul_ps(s1, y))),
    );

    // cos(y) polynomial
    let cos_y = _mm512_fmadd_ps(c6, y6, _mm512_fmadd_ps(c4, y4, _mm512_fmadd_ps(c2, y2, c0)));

    // Select sin or cos based on quadrant using AVX-512 masks
    let j_mod_4 = _mm512_and_si512(j_int, _mm512_set1_epi32(3));

    // Use cos when j mod 4 is 1 or 3
    let use_cos_mask = _mm512_cmpeq_epi32_mask(
        _mm512_and_si512(j_mod_4, _mm512_set1_epi32(1)),
        _mm512_set1_epi32(1),
    );

    // Negate when j mod 4 is 2 or 3
    let negate_mask = _mm512_cmpeq_epi32_mask(
        _mm512_and_si512(j_mod_4, _mm512_set1_epi32(2)),
        _mm512_set1_epi32(2),
    );

    let result = _mm512_mask_blend_ps(use_cos_mask, sin_y, cos_y);
    let negated = _mm512_sub_ps(_mm512_setzero_ps(), result);
    _mm512_mask_blend_ps(negate_mask, result, negated)
}

/// Fast SIMD sin approximation for f64 using AVX-512
///
/// See `common::_TRIG_ALGORITHM_DOC` for algorithm details.
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn sin_f64(x: __m512d) -> __m512d {
    use trig_coefficients::*;

    let two_over_pi = _mm512_set1_pd(std::f64::consts::FRAC_2_PI);
    let pi_over_2 = _mm512_set1_pd(std::f64::consts::FRAC_PI_2);

    let s1 = _mm512_set1_pd(S1_F64);
    let s3 = _mm512_set1_pd(S3_F64);
    let s5 = _mm512_set1_pd(S5_F64);
    let s7 = _mm512_set1_pd(S7_F64);
    let s9 = _mm512_set1_pd(S9_F64);

    let c0 = _mm512_set1_pd(C0_F64);
    let c2 = _mm512_set1_pd(C2_F64);
    let c4 = _mm512_set1_pd(C4_F64);
    let c6 = _mm512_set1_pd(C6_F64);
    let c8 = _mm512_set1_pd(C8_F64);

    let j = _mm512_roundscale_pd::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(
        _mm512_mul_pd(x, two_over_pi),
    );
    let j_i64 = _mm512_cvtpd_epi64(j);

    let y = _mm512_fnmadd_pd(j, pi_over_2, x);

    let y2 = _mm512_mul_pd(y, y);
    let y3 = _mm512_mul_pd(y2, y);
    let y4 = _mm512_mul_pd(y2, y2);
    let y5 = _mm512_mul_pd(y4, y);
    let y6 = _mm512_mul_pd(y4, y2);
    let y7 = _mm512_mul_pd(y4, y3);
    let y8 = _mm512_mul_pd(y4, y4);
    let y9 = _mm512_mul_pd(y8, y);

    // sin(y) polynomial
    let mut sin_y = _mm512_mul_pd(s1, y);
    sin_y = _mm512_fmadd_pd(s3, y3, sin_y);
    sin_y = _mm512_fmadd_pd(s5, y5, sin_y);
    sin_y = _mm512_fmadd_pd(s7, y7, sin_y);
    sin_y = _mm512_fmadd_pd(s9, y9, sin_y);

    // cos(y) polynomial
    let mut cos_y = c0;
    cos_y = _mm512_fmadd_pd(c2, y2, cos_y);
    cos_y = _mm512_fmadd_pd(c4, y4, cos_y);
    cos_y = _mm512_fmadd_pd(c6, y6, cos_y);
    cos_y = _mm512_fmadd_pd(c8, y8, cos_y);

    // Quadrant selection using AVX-512 masks
    let j_mod_4 = _mm512_and_si512(j_i64, _mm512_set1_epi64(3));

    let use_cos_mask = _mm512_cmpeq_epi64_mask(
        _mm512_and_si512(j_mod_4, _mm512_set1_epi64(1)),
        _mm512_set1_epi64(1),
    );

    let negate_mask = _mm512_cmpeq_epi64_mask(
        _mm512_and_si512(j_mod_4, _mm512_set1_epi64(2)),
        _mm512_set1_epi64(2),
    );

    let result = _mm512_mask_blend_pd(use_cos_mask, sin_y, cos_y);
    let negated = _mm512_sub_pd(_mm512_setzero_pd(), result);
    _mm512_mask_blend_pd(negate_mask, result, negated)
}

/// Fast SIMD cos approximation for f32 using AVX-512
///
/// Implemented as: cos(x) = sin(x + π/2)
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn cos_f32(x: __m512) -> __m512 {
    let pi_over_2 = _mm512_set1_ps(std::f32::consts::FRAC_PI_2);
    sin_f32(_mm512_add_ps(x, pi_over_2))
}

/// Fast SIMD cos approximation for f64 using AVX-512
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn cos_f64(x: __m512d) -> __m512d {
    let pi_over_2 = _mm512_set1_pd(std::f64::consts::FRAC_PI_2);
    sin_f64(_mm512_add_pd(x, pi_over_2))
}

/// Fast SIMD tan approximation for f32 using AVX-512
///
/// See `common::_TAN_ALGORITHM_DOC` for algorithm details.
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn tan_f32(x: __m512) -> __m512 {
    use tan_coefficients::*;

    let two_over_pi = _mm512_set1_ps(std::f32::consts::FRAC_2_PI);
    let pi_over_2 = _mm512_set1_ps(std::f32::consts::FRAC_PI_2);

    let j = _mm512_roundscale_ps::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(
        _mm512_mul_ps(x, two_over_pi),
    );
    let y = _mm512_fnmadd_ps(j, pi_over_2, x);

    let t1 = _mm512_set1_ps(T1_F32);
    let t3 = _mm512_set1_ps(T3_F32);
    let t5 = _mm512_set1_ps(T5_F32);
    let t7 = _mm512_set1_ps(T7_F32);
    let t9 = _mm512_set1_ps(T9_F32);
    let t11 = _mm512_set1_ps(T11_F32);

    let y2 = _mm512_mul_ps(y, y);

    // Horner's method
    let mut poly = t11;
    poly = _mm512_fmadd_ps(poly, y2, t9);
    poly = _mm512_fmadd_ps(poly, y2, t7);
    poly = _mm512_fmadd_ps(poly, y2, t5);
    poly = _mm512_fmadd_ps(poly, y2, t3);
    poly = _mm512_fmadd_ps(poly, y2, t1);
    let tan_y = _mm512_mul_ps(y, poly);

    // For odd quadrants, use -cot(y)
    let j_int = _mm512_cvtps_epi32(j);
    let use_cot_mask = _mm512_cmpeq_epi32_mask(
        _mm512_and_si512(j_int, _mm512_set1_epi32(1)),
        _mm512_set1_epi32(1),
    );

    let neg_one = _mm512_set1_ps(-1.0);
    let cot_y = _mm512_div_ps(neg_one, tan_y);

    _mm512_mask_blend_ps(use_cot_mask, tan_y, cot_y)
}

/// Fast SIMD tan approximation for f64 using AVX-512
///
/// See `common::_TAN_ALGORITHM_DOC` for algorithm details.
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn tan_f64(x: __m512d) -> __m512d {
    use tan_coefficients::*;

    let two_over_pi = _mm512_set1_pd(std::f64::consts::FRAC_2_PI);
    let pi_over_2 = _mm512_set1_pd(std::f64::consts::FRAC_PI_2);

    let j = _mm512_roundscale_pd::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(
        _mm512_mul_pd(x, two_over_pi),
    );
    let y = _mm512_fnmadd_pd(j, pi_over_2, x);

    let t1 = _mm512_set1_pd(T1_F64);
    let t3 = _mm512_set1_pd(T3_F64);
    let t5 = _mm512_set1_pd(T5_F64);
    let t7 = _mm512_set1_pd(T7_F64);
    let t9 = _mm512_set1_pd(T9_F64);
    let t11 = _mm512_set1_pd(T11_F64);
    let t13 = _mm512_set1_pd(T13_F64);

    let y2 = _mm512_mul_pd(y, y);

    // Horner's method
    let mut poly = t13;
    poly = _mm512_fmadd_pd(poly, y2, t11);
    poly = _mm512_fmadd_pd(poly, y2, t9);
    poly = _mm512_fmadd_pd(poly, y2, t7);
    poly = _mm512_fmadd_pd(poly, y2, t5);
    poly = _mm512_fmadd_pd(poly, y2, t3);
    poly = _mm512_fmadd_pd(poly, y2, t1);
    let tan_y = _mm512_mul_pd(y, poly);

    let j_i64 = _mm512_cvtpd_epi64(j);
    let use_cot_mask = _mm512_cmpeq_epi64_mask(
        _mm512_and_si512(j_i64, _mm512_set1_epi64(1)),
        _mm512_set1_epi64(1),
    );

    let neg_one = _mm512_set1_pd(-1.0);
    let cot_y = _mm512_div_pd(neg_one, tan_y);

    _mm512_mask_blend_pd(use_cot_mask, tan_y, cot_y)
}

// ============================================================================
// Inverse tangent function: atan(x)
// ============================================================================

/// Fast SIMD atan approximation for f32 using AVX-512
///
/// See `common::_ATAN_ALGORITHM_DOC` for algorithm details.
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn atan_f32(x: __m512) -> __m512 {
    use atan_coefficients::*;

    let one = _mm512_set1_ps(1.0);
    let pi_over_2 = _mm512_set1_ps(std::f32::consts::FRAC_PI_2);
    let zero = _mm512_setzero_ps();

    // Save sign and work with absolute value
    let abs_x = _mm512_abs_ps(x);
    let neg_mask = _mm512_cmp_ps_mask::<_CMP_LT_OQ>(x, zero);

    // Range reduction: for |x| > 1, compute atan(1/x) then adjust
    let need_recip_mask = _mm512_cmp_ps_mask::<_CMP_GT_OQ>(abs_x, one);
    let recip_x = _mm512_div_ps(one, abs_x);
    let y = _mm512_mask_blend_ps(need_recip_mask, abs_x, recip_x);

    // Polynomial approximation for atan(y) where y in [0, 1]
    let a0 = _mm512_set1_ps(A0_F32);
    let a2 = _mm512_set1_ps(A2_F32);
    let a4 = _mm512_set1_ps(A4_F32);
    let a6 = _mm512_set1_ps(A6_F32);
    let a8 = _mm512_set1_ps(A8_F32);
    let a10 = _mm512_set1_ps(A10_F32);
    let a12 = _mm512_set1_ps(A12_F32);

    let y2 = _mm512_mul_ps(y, y);

    // Horner's method
    let mut poly = a12;
    poly = _mm512_fmadd_ps(poly, y2, a10);
    poly = _mm512_fmadd_ps(poly, y2, a8);
    poly = _mm512_fmadd_ps(poly, y2, a6);
    poly = _mm512_fmadd_ps(poly, y2, a4);
    poly = _mm512_fmadd_ps(poly, y2, a2);
    poly = _mm512_fmadd_ps(poly, y2, a0);
    let atan_y = _mm512_mul_ps(y, poly);

    // Apply range reduction inverse: if |x| > 1, result = π/2 - atan(1/x)
    let adjusted = _mm512_sub_ps(pi_over_2, atan_y);
    let result = _mm512_mask_blend_ps(need_recip_mask, atan_y, adjusted);

    // Restore sign: negate result if x was negative
    let neg_result = _mm512_sub_ps(zero, result);
    _mm512_mask_blend_ps(neg_mask, result, neg_result)
}

/// Fast SIMD atan approximation for f64 using AVX-512
///
/// See `common::_ATAN_ALGORITHM_DOC` for algorithm details.
///
/// # Safety
/// Requires AVX-512F CPU feature.
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn atan_f64(x: __m512d) -> __m512d {
    use atan_coefficients::*;

    let one = _mm512_set1_pd(1.0);
    let pi_over_2 = _mm512_set1_pd(std::f64::consts::FRAC_PI_2);
    let zero = _mm512_setzero_pd();

    // Save sign and work with absolute value
    let abs_x = _mm512_abs_pd(x);
    let neg_mask = _mm512_cmp_pd_mask::<_CMP_LT_OQ>(x, zero);

    // Range reduction: for |x| > 1, compute atan(1/x) then adjust
    let need_recip_mask = _mm512_cmp_pd_mask::<_CMP_GT_OQ>(abs_x, one);
    let recip_x = _mm512_div_pd(one, abs_x);
    let y = _mm512_mask_blend_pd(need_recip_mask, abs_x, recip_x);

    // Polynomial approximation for atan(y) where y in [0, 1]
    let a0 = _mm512_set1_pd(A0_F64);
    let a2 = _mm512_set1_pd(A2_F64);
    let a4 = _mm512_set1_pd(A4_F64);
    let a6 = _mm512_set1_pd(A6_F64);
    let a8 = _mm512_set1_pd(A8_F64);
    let a10 = _mm512_set1_pd(A10_F64);
    let a12 = _mm512_set1_pd(A12_F64);
    let a14 = _mm512_set1_pd(A14_F64);
    let a16 = _mm512_set1_pd(A16_F64);
    let a18 = _mm512_set1_pd(A18_F64);
    let a20 = _mm512_set1_pd(A20_F64);

    let y2 = _mm512_mul_pd(y, y);

    // Horner's method with 11 terms for higher precision
    let mut poly = a20;
    poly = _mm512_fmadd_pd(poly, y2, a18);
    poly = _mm512_fmadd_pd(poly, y2, a16);
    poly = _mm512_fmadd_pd(poly, y2, a14);
    poly = _mm512_fmadd_pd(poly, y2, a12);
    poly = _mm512_fmadd_pd(poly, y2, a10);
    poly = _mm512_fmadd_pd(poly, y2, a8);
    poly = _mm512_fmadd_pd(poly, y2, a6);
    poly = _mm512_fmadd_pd(poly, y2, a4);
    poly = _mm512_fmadd_pd(poly, y2, a2);
    poly = _mm512_fmadd_pd(poly, y2, a0);
    let atan_y = _mm512_mul_pd(y, poly);

    // Apply range reduction inverse: if |x| > 1, result = π/2 - atan(1/x)
    let adjusted = _mm512_sub_pd(pi_over_2, atan_y);
    let result = _mm512_mask_blend_pd(need_recip_mask, atan_y, adjusted);

    // Restore sign: negate result if x was negative
    let neg_result = _mm512_sub_pd(zero, result);
    _mm512_mask_blend_pd(neg_mask, result, neg_result)
}

// ============================================================================
// Additional transcendental functions
// ============================================================================

/// Fast SIMD rsqrt (1/sqrt(x)) for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn rsqrt_f32(x: __m512) -> __m512 {
    // AVX-512 has 14-bit precision rsqrt, refine with Newton-Raphson
    let approx = _mm512_rsqrt14_ps(x);
    let half = _mm512_set1_ps(0.5);
    let three = _mm512_set1_ps(3.0);
    let x_approx2 = _mm512_mul_ps(x, _mm512_mul_ps(approx, approx));
    let factor = _mm512_sub_ps(three, x_approx2);
    _mm512_mul_ps(half, _mm512_mul_ps(approx, factor))
}

/// Fast SIMD rsqrt (1/sqrt(x)) for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn rsqrt_f64(x: __m512d) -> __m512d {
    let sqrt_x = _mm512_sqrt_pd(x);
    _mm512_div_pd(_mm512_set1_pd(1.0), sqrt_x)
}

/// Fast SIMD exp2 (2^x) for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn exp2_f32(x: __m512) -> __m512 {
    let ln2 = _mm512_set1_ps(std::f32::consts::LN_2);
    exp_f32(_mm512_mul_ps(x, ln2))
}

/// Fast SIMD exp2 (2^x) for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn exp2_f64(x: __m512d) -> __m512d {
    let ln2 = _mm512_set1_pd(std::f64::consts::LN_2);
    exp_f64(_mm512_mul_pd(x, ln2))
}

/// Fast SIMD expm1 (e^x - 1) for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn expm1_f32(x: __m512) -> __m512 {
    let one = _mm512_set1_ps(1.0);
    let half = _mm512_set1_ps(0.5);
    let abs_x = _mm512_abs_ps(x);

    // Taylor series for small |x|
    let x2 = _mm512_mul_ps(x, x);
    let x3 = _mm512_mul_ps(x2, x);
    let x4 = _mm512_mul_ps(x2, x2);
    let c2 = _mm512_set1_ps(0.5);
    let c3 = _mm512_set1_ps(1.0 / 6.0);
    let c4 = _mm512_set1_ps(1.0 / 24.0);
    let taylor = _mm512_fmadd_ps(c4, x4, _mm512_fmadd_ps(c3, x3, _mm512_fmadd_ps(c2, x2, x)));

    let exp_result = _mm512_sub_ps(exp_f32(x), one);
    let mask = _mm512_cmp_ps_mask::<_CMP_GT_OQ>(abs_x, half);
    _mm512_mask_blend_ps(mask, taylor, exp_result)
}

/// Fast SIMD expm1 (e^x - 1) for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn expm1_f64(x: __m512d) -> __m512d {
    let one = _mm512_set1_pd(1.0);
    let half = _mm512_set1_pd(0.5);
    let abs_x = _mm512_abs_pd(x);

    let x2 = _mm512_mul_pd(x, x);
    let x3 = _mm512_mul_pd(x2, x);
    let x4 = _mm512_mul_pd(x2, x2);
    let c2 = _mm512_set1_pd(0.5);
    let c3 = _mm512_set1_pd(1.0 / 6.0);
    let c4 = _mm512_set1_pd(1.0 / 24.0);
    let taylor = _mm512_fmadd_pd(c4, x4, _mm512_fmadd_pd(c3, x3, _mm512_fmadd_pd(c2, x2, x)));

    let exp_result = _mm512_sub_pd(exp_f64(x), one);
    let mask = _mm512_cmp_pd_mask::<_CMP_GT_OQ>(abs_x, half);
    _mm512_mask_blend_pd(mask, taylor, exp_result)
}

/// Fast SIMD log2 for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn log2_f32(x: __m512) -> __m512 {
    let log2e = _mm512_set1_ps(std::f32::consts::LOG2_E);
    _mm512_mul_ps(log_f32(x), log2e)
}

/// Fast SIMD log2 for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn log2_f64(x: __m512d) -> __m512d {
    let log2e = _mm512_set1_pd(std::f64::consts::LOG2_E);
    _mm512_mul_pd(log_f64(x), log2e)
}

/// Fast SIMD log10 for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn log10_f32(x: __m512) -> __m512 {
    let log10e = _mm512_set1_ps(std::f32::consts::LOG10_E);
    _mm512_mul_ps(log_f32(x), log10e)
}

/// Fast SIMD log10 for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn log10_f64(x: __m512d) -> __m512d {
    let log10e = _mm512_set1_pd(std::f64::consts::LOG10_E);
    _mm512_mul_pd(log_f64(x), log10e)
}

/// Fast SIMD log1p (log(1+x)) for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn log1p_f32(x: __m512) -> __m512 {
    let one = _mm512_set1_ps(1.0);
    let half = _mm512_set1_ps(0.5);
    let abs_x = _mm512_abs_ps(x);

    let x2 = _mm512_mul_ps(x, x);
    let x3 = _mm512_mul_ps(x2, x);
    let x4 = _mm512_mul_ps(x2, x2);
    let c2 = _mm512_set1_ps(-0.5);
    let c3 = _mm512_set1_ps(1.0 / 3.0);
    let c4 = _mm512_set1_ps(-0.25);
    let taylor = _mm512_fmadd_ps(c4, x4, _mm512_fmadd_ps(c3, x3, _mm512_fmadd_ps(c2, x2, x)));

    let log_result = log_f32(_mm512_add_ps(one, x));
    let mask = _mm512_cmp_ps_mask::<_CMP_GT_OQ>(abs_x, half);
    _mm512_mask_blend_ps(mask, taylor, log_result)
}

/// Fast SIMD log1p (log(1+x)) for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn log1p_f64(x: __m512d) -> __m512d {
    let one = _mm512_set1_pd(1.0);
    let half = _mm512_set1_pd(0.5);
    let abs_x = _mm512_abs_pd(x);

    let x2 = _mm512_mul_pd(x, x);
    let x3 = _mm512_mul_pd(x2, x);
    let x4 = _mm512_mul_pd(x2, x2);
    let c2 = _mm512_set1_pd(-0.5);
    let c3 = _mm512_set1_pd(1.0 / 3.0);
    let c4 = _mm512_set1_pd(-0.25);
    let taylor = _mm512_fmadd_pd(c4, x4, _mm512_fmadd_pd(c3, x3, _mm512_fmadd_pd(c2, x2, x)));

    let log_result = log_f64(_mm512_add_pd(one, x));
    let mask = _mm512_cmp_pd_mask::<_CMP_GT_OQ>(abs_x, half);
    _mm512_mask_blend_pd(mask, taylor, log_result)
}

/// Fast SIMD sinh for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn sinh_f32(x: __m512) -> __m512 {
    let half = _mm512_set1_ps(0.5);
    let exp_x = exp_f32(x);
    let exp_neg_x = exp_f32(_mm512_sub_ps(_mm512_setzero_ps(), x));
    _mm512_mul_ps(half, _mm512_sub_ps(exp_x, exp_neg_x))
}

/// Fast SIMD sinh for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn sinh_f64(x: __m512d) -> __m512d {
    let half = _mm512_set1_pd(0.5);
    let exp_x = exp_f64(x);
    let exp_neg_x = exp_f64(_mm512_sub_pd(_mm512_setzero_pd(), x));
    _mm512_mul_pd(half, _mm512_sub_pd(exp_x, exp_neg_x))
}

/// Fast SIMD cosh for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn cosh_f32(x: __m512) -> __m512 {
    let half = _mm512_set1_ps(0.5);
    let exp_x = exp_f32(x);
    let exp_neg_x = exp_f32(_mm512_sub_ps(_mm512_setzero_ps(), x));
    _mm512_mul_ps(half, _mm512_add_ps(exp_x, exp_neg_x))
}

/// Fast SIMD cosh for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn cosh_f64(x: __m512d) -> __m512d {
    let half = _mm512_set1_pd(0.5);
    let exp_x = exp_f64(x);
    let exp_neg_x = exp_f64(_mm512_sub_pd(_mm512_setzero_pd(), x));
    _mm512_mul_pd(half, _mm512_add_pd(exp_x, exp_neg_x))
}

/// Fast SIMD asinh for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn asinh_f32(x: __m512) -> __m512 {
    let one = _mm512_set1_ps(1.0);
    let x2 = _mm512_mul_ps(x, x);
    let sqrt_term = _mm512_sqrt_ps(_mm512_add_ps(x2, one));
    log_f32(_mm512_add_ps(x, sqrt_term))
}

/// Fast SIMD asinh for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn asinh_f64(x: __m512d) -> __m512d {
    let one = _mm512_set1_pd(1.0);
    let x2 = _mm512_mul_pd(x, x);
    let sqrt_term = _mm512_sqrt_pd(_mm512_add_pd(x2, one));
    log_f64(_mm512_add_pd(x, sqrt_term))
}

/// Fast SIMD acosh for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn acosh_f32(x: __m512) -> __m512 {
    let one = _mm512_set1_ps(1.0);
    let x2 = _mm512_mul_ps(x, x);
    let sqrt_term = _mm512_sqrt_ps(_mm512_sub_ps(x2, one));
    log_f32(_mm512_add_ps(x, sqrt_term))
}

/// Fast SIMD acosh for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn acosh_f64(x: __m512d) -> __m512d {
    let one = _mm512_set1_pd(1.0);
    let x2 = _mm512_mul_pd(x, x);
    let sqrt_term = _mm512_sqrt_pd(_mm512_sub_pd(x2, one));
    log_f64(_mm512_add_pd(x, sqrt_term))
}

/// Fast SIMD atanh for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn atanh_f32(x: __m512) -> __m512 {
    let half = _mm512_set1_ps(0.5);
    let one = _mm512_set1_ps(1.0);
    let one_plus_x = _mm512_add_ps(one, x);
    let one_minus_x = _mm512_sub_ps(one, x);
    let ratio = _mm512_div_ps(one_plus_x, one_minus_x);
    _mm512_mul_ps(half, log_f32(ratio))
}

/// Fast SIMD atanh for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn atanh_f64(x: __m512d) -> __m512d {
    let half = _mm512_set1_pd(0.5);
    let one = _mm512_set1_pd(1.0);
    let one_plus_x = _mm512_add_pd(one, x);
    let one_minus_x = _mm512_sub_pd(one, x);
    let ratio = _mm512_div_pd(one_plus_x, one_minus_x);
    _mm512_mul_pd(half, log_f64(ratio))
}

/// Fast SIMD asin for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn asin_f32(x: __m512) -> __m512 {
    let one = _mm512_set1_ps(1.0);
    let x2 = _mm512_mul_ps(x, x);
    let sqrt_term = _mm512_sqrt_ps(_mm512_sub_ps(one, x2));
    let ratio = _mm512_div_ps(x, sqrt_term);
    atan_f32(ratio)
}

/// Fast SIMD asin for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn asin_f64(x: __m512d) -> __m512d {
    let one = _mm512_set1_pd(1.0);
    let x2 = _mm512_mul_pd(x, x);
    let sqrt_term = _mm512_sqrt_pd(_mm512_sub_pd(one, x2));
    let ratio = _mm512_div_pd(x, sqrt_term);
    atan_f64(ratio)
}

/// Fast SIMD acos for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn acos_f32(x: __m512) -> __m512 {
    let pi_half = _mm512_set1_ps(std::f32::consts::FRAC_PI_2);
    _mm512_sub_ps(pi_half, asin_f32(x))
}

/// Fast SIMD acos for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn acos_f64(x: __m512d) -> __m512d {
    let pi_half = _mm512_set1_pd(std::f64::consts::FRAC_PI_2);
    _mm512_sub_pd(pi_half, asin_f64(x))
}

/// Fast SIMD cbrt (cube root) for f32 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn cbrt_f32(x: __m512) -> __m512 {
    let sign_bit = _mm512_set1_ps(-0.0);
    let sign = _mm512_and_ps(x, sign_bit);
    let abs_x = _mm512_andnot_ps(sign_bit, x);

    let one_third = _mm512_set1_ps(1.0 / 3.0);
    let log_x = log_f32(abs_x);
    let guess = exp_f32(_mm512_mul_ps(log_x, one_third));

    let two = _mm512_set1_ps(2.0);
    let three = _mm512_set1_ps(3.0);

    let y = guess;
    let y2 = _mm512_mul_ps(y, y);
    let y_new = _mm512_div_ps(_mm512_fmadd_ps(two, y, _mm512_div_ps(abs_x, y2)), three);

    let y2 = _mm512_mul_ps(y_new, y_new);
    let result = _mm512_div_ps(_mm512_fmadd_ps(two, y_new, _mm512_div_ps(abs_x, y2)), three);

    _mm512_or_ps(result, sign)
}

/// Fast SIMD cbrt (cube root) for f64 using AVX-512
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn cbrt_f64(x: __m512d) -> __m512d {
    let sign_bit = _mm512_set1_pd(-0.0);
    let sign = _mm512_and_pd(x, sign_bit);
    let abs_x = _mm512_andnot_pd(sign_bit, x);

    let one_third = _mm512_set1_pd(1.0 / 3.0);
    let log_x = log_f64(abs_x);
    let guess = exp_f64(_mm512_mul_pd(log_x, one_third));

    let two = _mm512_set1_pd(2.0);
    let three = _mm512_set1_pd(3.0);

    let y = guess;
    let y2 = _mm512_mul_pd(y, y);
    let y_new = _mm512_div_pd(_mm512_fmadd_pd(two, y, _mm512_div_pd(abs_x, y2)), three);

    let y2 = _mm512_mul_pd(y_new, y_new);
    let result = _mm512_div_pd(_mm512_fmadd_pd(two, y_new, _mm512_div_pd(abs_x, y2)), three);

    _mm512_or_pd(result, sign)
}