tomoxide 0.14.0

GPU/CPU tomographic reconstruction toolkit (tomopy + tomocupy, in Rust)
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
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
//! C-ABI bindings to the CUDA shim (`cuda/shim.cpp`).
//!
//! Minimal CUDA-runtime helpers (device probe + linear device memory) plus
//! tomocupy's `cfunc_linerec` parallel-beam back-projection, wrapped as
//! `extern "C"` so Rust links them without SWIG. Only compiled under the `cuda`
//! feature; the symbols resolve against the static library `build.rs` produces
//! with `nvcc`.
//!
//! Device pointers are opaque `*mut c_void`; `stream` is a `cudaStream_t`
//! (null = default stream). `theta`/`f`/`g` are device addresses.
//!
//! A kernel launcher returns 0 once its work is queued, else the
//! `cudaError_t` the launch raised, or -2 when cuFFT refused a transform.

use std::os::raw::c_void;

unsafe extern "C" {
    // --- device runtime helpers ---
    /// Number of CUDA devices (0 if none / driver missing).
    #[must_use]
    pub fn tomoxide_cuda_device_count() -> i32;
    /// Bind the calling host thread to a device (multi-GPU pools); 0 on success.
    #[must_use]
    pub fn tomoxide_cuda_set_device(dev: i32) -> i32;
    /// Name of the current device, written NUL-terminated into `buf` (capacity
    /// `len` bytes); 0 on success, non-zero `cudaError_t` otherwise.
    #[must_use]
    pub fn tomoxide_cuda_device_name(buf: *mut std::os::raw::c_char, len: usize) -> i32;
    /// `cudaMalloc` — returns a device pointer or null on failure.
    pub fn tomoxide_cuda_malloc(bytes: usize) -> *mut c_void;
    /// `cudaFree`.
    pub fn tomoxide_cuda_free(p: *mut c_void);
    /// `cudaMemcpy` host→device; returns 0 on success.
    #[must_use]
    pub fn tomoxide_cuda_memcpy_h2d(dst: *mut c_void, src: *const c_void, bytes: usize) -> i32;
    /// `cudaMemcpy` device→host; returns 0 on success.
    #[must_use]
    pub fn tomoxide_cuda_memcpy_d2h(dst: *mut c_void, src: *const c_void, bytes: usize) -> i32;
    /// `cudaMemcpyAsync` device→device on `stream` (null = per-thread default); 0 ok.
    #[must_use]
    pub fn tomoxide_cuda_memcpy_d2d_async(
        dst: *mut c_void,
        src: *const c_void,
        bytes: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `cudaMemset`; returns 0 on success.
    #[must_use]
    pub fn tomoxide_cuda_memset(p: *mut c_void, value: i32, bytes: usize) -> i32;
    /// `cudaDeviceSynchronize`; returns 0 on success.
    #[must_use]
    pub fn tomoxide_cuda_sync() -> i32;
    /// `cudaMemGetInfo` — free / total bytes on the current device; 0 on success.
    #[must_use]
    pub fn tomoxide_cuda_mem_info(free_bytes: *mut usize, total_bytes: *mut usize) -> i32;

    // --- async pipeline: streams, pinned host memory, async copies ---
    /// `cudaStreamCreate` — returns an opaque `cudaStream_t` or null on failure.
    pub fn tomoxide_cuda_stream_create() -> *mut c_void;
    /// `cudaStreamDestroy`.
    pub fn tomoxide_cuda_stream_destroy(stream: *mut c_void);
    /// `cudaStreamSynchronize` — block until the stream's work completes; 0 on ok.
    #[must_use]
    pub fn tomoxide_cuda_stream_sync(stream: *mut c_void) -> i32;
    /// `cudaHostAlloc` (page-locked) — returns a pinned host pointer or null.
    pub fn tomoxide_cuda_host_alloc(bytes: usize) -> *mut c_void;
    /// `cudaFreeHost`.
    pub fn tomoxide_cuda_host_free(p: *mut c_void);
    /// `cudaMemcpyAsync` host→device on `stream`; 0 on success.
    #[must_use]
    pub fn tomoxide_cuda_memcpy_h2d_async(
        dst: *mut c_void,
        src: *const c_void,
        bytes: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `cudaMemcpyAsync` device→host on `stream`; 0 on success.
    #[must_use]
    pub fn tomoxide_cuda_memcpy_d2h_async(
        dst: *mut c_void,
        src: *const c_void,
        bytes: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `cudaMemsetAsync` on `stream`; 0 on success.
    #[must_use]
    pub fn tomoxide_cuda_memset_async(
        p: *mut c_void,
        value: i32,
        bytes: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `cudaEventCreateWithFlags(cudaEventDisableTiming)` — opaque event or null.
    pub fn tomoxide_cuda_event_create() -> *mut c_void;
    /// `cudaEventDestroy`.
    pub fn tomoxide_cuda_event_destroy(e: *mut c_void);
    /// `cudaEventRecord(e, stream)` (null stream = per-thread compute stream); 0 ok.
    #[must_use]
    pub fn tomoxide_cuda_event_record(e: *mut c_void, stream: *mut c_void) -> i32;
    /// `cudaStreamWaitEvent(stream, e, 0)` — `stream` defers until `e` fires; 0 ok.
    #[must_use]
    pub fn tomoxide_cuda_stream_wait_event(stream: *mut c_void, e: *mut c_void) -> i32;
    /// `cudaEventSynchronize(e)` — block the host until `e` fires; 0 ok.
    #[must_use]
    pub fn tomoxide_cuda_event_sync(e: *mut c_void) -> i32;

    // --- linerec (cfunc_linerec) ---
    /// `cfunc_linerec(nproj, nz, n, ncproj, ncz)`.
    pub fn tomoxide_linerec_new(
        nproj: usize,
        nz: usize,
        n: usize,
        ncproj: usize,
        ncz: usize,
    ) -> *mut c_void;
    /// `backprojection(f, g, theta, phi, axis, gain, sz, rh, stream)` (phi = π/2
    /// for parallel beam). `axis` is the detector column voxel column `n/2`
    /// reads, as in [`tomoxide_forwardproject`]. `gain` is the caller's
    /// angular quadrature weight: π/nproj for the analytic FBP paths (the dθ
    /// weight), 1.0 for the iterative solvers (pure adjoint of
    /// `tomoxide_forwardproject`).
    /// `rh` is the FULL reconstruction height in slices (`nz` for parallel
    /// beam, [`super::lamino_recon_height`] for laminography): the volume z
    /// `tz + sz` is centred on `rh/2` — the volume's own midpoint — while the
    /// detector row stays centred on `nz/2`, matching the Fourier (USFFT)
    /// laminography reconstruction. For parallel beam `rh == nz` reproduces
    /// the old expression exactly.
    #[must_use]
    pub fn tomoxide_linerec_backproject(
        handle: *mut c_void,
        f: *mut c_void,
        g: *const c_void,
        theta: *const f32,
        phi: f32,
        axis: f32,
        gain: f32,
        sz: i32,
        rh: i32,
        stream: *mut c_void,
    ) -> i32;
    /// Rotation-centre probe: back-project the single detector row `sz` once per
    /// entry of `sh` (device `[nshift]`, detector columns) in one launch, writing
    /// `f` as `[nshift, n, n]` — one candidate slice per shift. `phi` is the
    /// scalar tilt (`π/2` = untilted), so this serves parallel-beam and
    /// laminography alike. The handle must be built with `ncz == nshift`; `g` is
    /// the filtered sinogram `[nz, nproj, n]`; `f` must be pre-zeroed.
    ///
    /// The shift is applied to the back-projection sampling coordinate
    /// (`u -= sh[k]`) with **linear interpolation**, whereas a full
    /// reconstruction applies its centre as a Fourier linear phase in the filter.
    /// The two are therefore not bit-identical away from integer shifts — see
    /// [`super::center_probe`], which owns the shift↔centre mapping and documents
    /// the consequence.
    ///
    /// `sz` is a volume z (centred on `rh/2`, the full reconstruction height —
    /// `nz` for parallel beam), not a detector row; see
    /// [`tomoxide_linerec_backproject`].
    #[must_use]
    pub fn tomoxide_linerec_backproject_try(
        handle: *mut c_void,
        f: *mut c_void,
        g: *const c_void,
        theta: *const f32,
        sh: *const f32,
        phi: f32,
        sz: i32,
        rh: i32,
        stream: *mut c_void,
    ) -> i32;
    /// Laminography tilt probe: back-project the single detector row `sz` at
    /// every tilt in `phi` (device `[ntilt]`, radians, `π/2` = untilted) in one
    /// launch, writing `f` as `[ntilt, n, n]` — one candidate slice per tilt.
    /// The handle must be built with `ncz == ntilt`; `g` is the filtered
    /// sinogram `[nz, nproj, n]`; `f` must be pre-zeroed (the kernel `+=`s).
    /// Carries the FBP dθ gain, so a slice matches the corresponding
    /// `Algorithm::Linerec` laminography reconstruction, not merely its shape.
    /// `rh` is a device array `[ntilt]` of per-slot reconstruction heights
    /// ([`super::lamino_recon_height`] of each tilt — the height depends on the
    /// tilt): slot k's slice `sz` is a volume z centred on `rh[k]/2`.
    #[must_use]
    pub fn tomoxide_linerec_backproject_try_lamino(
        handle: *mut c_void,
        f: *mut c_void,
        g: *const c_void,
        theta: *const f32,
        phi: *const f32,
        sz: i32,
        rh: *const i32,
        stream: *mut c_void,
    ) -> i32;

    pub fn tomoxide_linerec_free(handle: *mut c_void);

    // --- forward projection (adjoint of cfunc_linerec back-projection) ---
    /// Parallel-beam forward projection (Radon), the exact discrete transpose of
    /// `tomoxide_linerec_backproject`. `g` (output sinogram `[nz, nproj, n]`)
    /// must be pre-zeroed; the kernel only `atomicAdd`s into it. `f` is the input
    /// volume `[nz, n, n]`, `phi = π/2` for parallel beam. `rh` = full
    /// reconstruction height the volume z is centred on (see
    /// [`tomoxide_linerec_backproject`]); must match the back-projection's `rh`
    /// so the pair stays an exact adjoint, and so must `axis`, the detector
    /// column voxel column `n/2` lands on (the rotation centre).
    #[must_use]
    pub fn tomoxide_forwardproject(
        g: *mut c_void,
        f: *const c_void,
        theta: *const f32,
        phi: f32,
        axis: f32,
        sz: i32,
        ncz: i32,
        nz: i32,
        n: i32,
        rh: i32,
        nproj: i32,
        stream: *mut c_void,
    ) -> i32;
    /// `d` `[2, nframes, rows, n]`: each frame of `m` `[nframes, rows, n]`
    /// differentiated along its columns, then along its rows, as numpy's
    /// `gradient`.
    #[must_use]
    pub fn tomoxide_frame_derivatives(
        m: *const c_void,
        d: *mut c_void,
        nframes: i64,
        rows: i32,
        n: i32,
        stream: *mut c_void,
    ) -> i32;
    /// `out` `[nproj, 2]` (pre-zeroed f64) += the inner product of
    /// [`tomoxide_forwardproject`]'s frame of `f` at each angle with that
    /// angle's frame of `hx`, and of `hy` (`[nproj, nz, n]` each), without
    /// forming the projection. Geometry arguments as `tomoxide_forwardproject`.
    #[must_use]
    pub fn tomoxide_framedot(
        out: *mut c_void,
        f: *const c_void,
        hx: *const c_void,
        hy: *const c_void,
        theta: *const f32,
        phi: f32,
        axis: f32,
        sz: i32,
        ncz: i32,
        nz: i32,
        n: i32,
        rh: i32,
        nproj: i32,
        stream: *mut c_void,
    ) -> i32;

    // --- band focus (bandfocus.cu) ---
    /// `m` `[rows, alen, n]`: frames `a0 .. a0 + alen` of the stack whose
    /// spline coefficients `coef` holds (`[nproj, rows + 24, n + 24]`), moved
    /// by `dy`/`dx` (device, `[nproj]`) as `PreppedStack::moved` moves them at
    /// roll 0. With `dmx`/`dmy` non-null, also `m`'s derivative over `dx` and
    /// over `dy`.
    #[must_use]
    pub fn tomoxide_bf_move(
        m: *mut c_void,
        dmx: *mut c_void,
        dmy: *mut c_void,
        coef: *const c_void,
        dy: *const f32,
        dx: *const f32,
        a0: i32,
        c0: i32,
        alen: i32,
        rows: i32,
        n: i32,
        stream: *mut c_void,
    ) -> i32;
    /// `prof` `[nb]` (pre-zeroed f64) += each slice of `v` `[nb, n, n]`'s
    /// `slice_focus` sum over the pixels `mask` `[n, n]` (u8) keeps.
    #[must_use]
    pub fn tomoxide_bf_focus(
        prof: *mut c_void,
        v: *const c_void,
        mask: *const c_void,
        nb: i32,
        n: i32,
        stream: *mut c_void,
    ) -> i32;
    /// `r` `[nb, n, n]` = `scale` × the derivative of those sums over `v`.
    #[must_use]
    pub fn tomoxide_bf_focus_grad(
        r: *mut c_void,
        v: *const c_void,
        mask: *const c_void,
        scale: f32,
        nb: i32,
        n: i32,
        stream: *mut c_void,
    ) -> i32;
    /// `out` `[nproj, 3]` (pre-zeroed f64) += `r` `[ncz, n, n]` dotted with
    /// the back-projection of each angle of `hx`, `hy` and `hc` (`[nz, nproj,
    /// n]` each) at unit gain. Geometry arguments as
    /// [`tomoxide_linerec_backproject`].
    #[must_use]
    pub fn tomoxide_bf_framedot3(
        out: *mut c_void,
        r: *const c_void,
        hx: *const c_void,
        hy: *const c_void,
        hc: *const c_void,
        theta: *const f32,
        phi: f32,
        axis: f32,
        sz: i32,
        ncz: i32,
        nz: i32,
        n: i32,
        rh: i32,
        nproj: i32,
        stream: *mut c_void,
    ) -> i32;

    // --- device-resident iterative solver elementwise ops (iterative.cu) ---
    /// `ax[i] = (b[i] - ax[i]) * rw[i]` over `n` elements (in-place into `ax`).
    #[must_use]
    pub fn tomoxide_iter_residual(
        ax: *mut c_void,
        b: *const c_void,
        rw: *const c_void,
        n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `vol[i] += cw[i] * corr[i]` over `n` elements.
    #[must_use]
    pub fn tomoxide_iter_update(
        vol: *mut c_void,
        cw: *const c_void,
        corr: *const c_void,
        n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `out[i] = |x[i]| > thr ? 1/x[i] : 0` over `n` elements (in-place ok).
    #[must_use]
    pub fn tomoxide_iter_recip_thresh(
        out: *mut c_void,
        x: *const c_void,
        thr: f32,
        n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `ax[i] = |ax[i]| > 1e-6 ? b[i]/ax[i] : 0` (EM ratio, in-place into `ax`).
    #[must_use]
    pub fn tomoxide_iter_em_ratio(
        ax: *mut c_void,
        b: *const c_void,
        n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `vol[i] = |sens[i]| > 1e-6 ? vol[i]*corr[i]/sens[i] : vol[i]` (EM update).
    #[must_use]
    pub fn tomoxide_iter_em_update(
        vol: *mut c_void,
        corr: *const c_void,
        sens: *const c_void,
        n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// De Pierro penalized-ML pixel update over the snapshot `old` → `vol`
    /// (3-D grid `[nz,n,n]`). `delta` used only when `has_delta != 0` (hybrid
    /// prior); `reg = 0` reduces to the OSEM step.
    #[must_use]
    pub fn tomoxide_iter_pml_update(
        vol: *mut c_void,
        old: *const c_void,
        corr: *const c_void,
        sens: *const c_void,
        reg: f32,
        delta: f32,
        has_delta: i32,
        n: usize,
        nz: usize,
        stream: *mut c_void,
    ) -> i32;
    /// TV data dual proximal `pd = (pd + c·r·ax − c·b)/(1+c)` (in-place into `pd`).
    #[must_use]
    pub fn tomoxide_iter_tv_datadual(
        pd: *mut c_void,
        ax: *const c_void,
        b: *const c_void,
        c: f32,
        r: f32,
        n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// TV dual ascent on `xbar` + λ-ball projection (interior stencil; last
    /// row/col of `p0x`/`p0y` untouched). 3-D grid `[nz,n,n]`.
    #[must_use]
    pub fn tomoxide_iter_tv_dual(
        p0x: *mut c_void,
        p0y: *mut c_void,
        xbar: *const c_void,
        c: f32,
        lambda: f32,
        n: usize,
        nz: usize,
        stream: *mut c_void,
    ) -> i32;
    /// TV primal step + over-relaxation: `x ← x − c·r·Rᵀ(pd) + c·div(pᵀᵛ)`,
    /// `xbar ← 2x − x_old`. 3-D grid `[nz,n,n]`.
    #[must_use]
    pub fn tomoxide_iter_tv_primal(
        x: *mut c_void,
        xbar: *mut c_void,
        bpv: *const c_void,
        p0x: *const c_void,
        p0y: *const c_void,
        c: f32,
        r: f32,
        n: usize,
        nz: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `ax[i] = ax[i]*r - b[i]` (GD data proximal, in-place into `ax`).
    #[must_use]
    pub fn tomoxide_iter_grad_prox(
        ax: *mut c_void,
        b: *const c_void,
        r: f32,
        n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `grad[i] = coef * bpv[i]` (GD data gradient, fresh write).
    #[must_use]
    pub fn tomoxide_iter_grad_assemble(
        grad: *mut c_void,
        bpv: *const c_void,
        coef: f32,
        n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `grad[i] += two_reg1 * (vol[i] - prior[i])` (Tikhonov gradient).
    #[must_use]
    pub fn tomoxide_iter_grad_tikh(
        grad: *mut c_void,
        vol: *const c_void,
        prior: *const c_void,
        two_reg1: f32,
        n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `x[i] *= s` over `n` elements.
    #[must_use]
    pub fn tomoxide_iter_scale_inplace(
        x: *mut c_void,
        s: f32,
        n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `vol[i] -= lambda[i/slice_len] * grad[i]` (per-slice gradient step).
    #[must_use]
    pub fn tomoxide_iter_axpy_neg_slice(
        vol: *mut c_void,
        grad: *const c_void,
        lambda: *const c_void,
        slice_len: usize,
        total_n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Per-slice BB reductions `num[z]=Σ(x−x0)(g−g0)`, `den[z]=Σ(g−g0)²` (one block
    /// per slice). `num`/`den` are device `[nz]`.
    #[must_use]
    pub fn tomoxide_iter_bb_reduce(
        num: *mut c_void,
        den: *mut c_void,
        x: *const c_void,
        x0: *const c_void,
        g: *const c_void,
        g0: *const c_void,
        slice_len: usize,
        nz: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `lambda[z] = fixed_step≥0 ? fixed_step : (is_first ? 1e-3 : den≠0 ? num/den : 1e-3)`.
    #[must_use]
    pub fn tomoxide_iter_bb_lambda(
        lambda: *mut c_void,
        num: *const c_void,
        den: *const c_void,
        fixed_step: f32,
        is_first: i32,
        nz: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Per-slice dot product `out[z] = Σ_i a[z,i]·b[z,i]` (one block per slice).
    /// `out` is device `[nz]`; `slice_len` is the per-slice element count.
    #[must_use]
    pub fn tomoxide_iter_slice_dot(
        out: *mut c_void,
        a: *const c_void,
        b: *const c_void,
        slice_len: usize,
        nz: usize,
        stream: *mut c_void,
    ) -> i32;
    /// CGLS direction recurrence `p[i] = z[i] + beta[s]·p[i]`, `s = i/slice_len`.
    #[must_use]
    pub fn tomoxide_iter_xpby_slice(
        p: *mut c_void,
        zv: *const c_void,
        beta: *const c_void,
        slice_len: usize,
        total_n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// CGLS step `alpha[z] = wdot>0 ? gamma/wdot : 0`, `neg_alpha[z] = −alpha[z]`.
    #[must_use]
    pub fn tomoxide_iter_cgls_alpha(
        alpha: *mut c_void,
        neg_alpha: *mut c_void,
        gamma: *const c_void,
        wdot: *const c_void,
        nz: usize,
        stream: *mut c_void,
    ) -> i32;
    /// CGLS `beta[z] = gamma>0 ? gnew/gamma : 0`, then advance `gamma[z] = gnew[z]`.
    #[must_use]
    pub fn tomoxide_iter_cgls_beta(
        beta: *mut c_void,
        gamma: *mut c_void,
        gnew: *const c_void,
        nz: usize,
        stream: *mut c_void,
    ) -> i32;

    // --- fourierrec (cfunc_fourierrec) ---
    /// `cfunc_fourierrec(nproj, nz, n, theta_ptr)` — `nz` is the number of
    /// complex slice-pairs (real input slices / 2); `theta` is a device pointer.
    pub fn tomoxide_fourierrec_new(
        nproj: usize,
        nz: usize,
        n: usize,
        theta: *const f32,
    ) -> *mut c_void;
    /// `backprojection(f, g, stream)` — `g` = packed complex filtered sinogram,
    /// `f` = packed complex output volume (both device pointers).
    #[must_use]
    pub fn tomoxide_fourierrec_backproject(
        handle: *mut c_void,
        f: *mut c_void,
        g: *const c_void,
        stream: *mut c_void,
    ) -> i32;
    pub fn tomoxide_fourierrec_free(handle: *mut c_void);

    // --- FBP filter (cfunc_filter) ---
    /// `cfunc_filter(nproj, nz, n)` — `n` is the padded width `ne`.
    pub fn tomoxide_filter_new(nproj: usize, nz: usize, n: usize) -> *mut c_void;
    /// `filter(g, w, stream)` — in-place R2C → ×w → C2R on the padded real
    /// sinogram `g` `[nz, nproj, ne]`; `w` is complex `[nz, ne/2+1]` (device).
    #[must_use]
    pub fn tomoxide_filter_apply(
        handle: *mut c_void,
        g: *mut c_void,
        w: *const c_void,
        stream: *mut c_void,
    ) -> i32;
    pub fn tomoxide_filter_free(handle: *mut c_void);

    // --- elementwise preprocessing ---
    /// `(data − dark2d) / denom` over a `[nproj, nz, nx]` projection volume;
    /// `dark2d`/`denom` are device `[nz, nx]`. Returns 0 on success.
    #[must_use]
    pub fn tomoxide_darkflat(
        data: *mut c_void,
        dark2d: *const c_void,
        denom: *const c_void,
        nproj: usize,
        nz: usize,
        nx: usize,
        stream: *mut c_void,
    ) -> i32;
    /// In-place `−ln(max(x, 1e-6))` (non-finite → 0) over `n` elements.
    #[must_use]
    pub fn tomoxide_minuslog(data: *mut c_void, n: usize, stream: *mut c_void) -> i32;
    /// Projection→sinogram transpose: `[nproj, nz, ncols]` → `[nz, nproj, ncols]`
    /// (swap the two outer axes). Pure reorder; matches the host `to_layout`
    /// permute bit-for-bit. Returns 0 on success.
    #[must_use]
    pub fn tomoxide_transpose(
        src: *const c_void,
        dst: *mut c_void,
        nproj: usize,
        nz: usize,
        ncols: usize,
        stream: *mut c_void,
    ) -> i32;

    /// Titarenko (`remove_stripe_ti`, nblock=0) on the f32 sinogram
    /// `[nz, nproj, ncol]`, in place. `scratch` is `nz * 7 * ncol` doubles.
    /// One block per slice runs the conjugate-gradient solve in f64; held to
    /// correlation parity with the CPU golden, not bit-exactness. Returns 0 ok.
    #[must_use]
    pub fn tomoxide_stripe_ti(
        sino: *mut c_void,
        nz: usize,
        nproj: usize,
        ncol: usize,
        beta: f32,
        scratch: *mut c_void,
        stream: *mut c_void,
    ) -> i32;

    // --- Fourier-Wavelet (`remove_stripe_fw`) building blocks, orchestrated
    // from `CudaFbpStream::stripe_on_device`. All f64 except the damping FFT
    // (f32 cuFFT via `tomoxide_fft_1d`); batched over the leading `nz`. ---
    /// Pad f32 sino `[nz,nproj,ncol]` → f64 approx `[nz,nx,ncol]` at row `xshift`.
    #[must_use]
    pub fn tomoxide_fw_pad(
        in_: *const c_void,
        approx: *mut c_void,
        nz: usize,
        nproj: usize,
        ncol: usize,
        nx: usize,
        xshift: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Crop f64 `sli [nz,sliR,sliC]` rows `[xshift, xshift+nproj)` → f32 sino.
    #[must_use]
    pub fn tomoxide_fw_final(
        sli: *const c_void,
        out: *mut c_void,
        nz: usize,
        nproj: usize,
        ncol: usize,
        sli_r: usize,
        sli_c: usize,
        xshift: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Crop the top-left `[oR,oC]` block of `[nz,inR,inC]` (f64).
    #[must_use]
    pub fn tomoxide_fw_crop(
        in_: *const c_void,
        out: *mut c_void,
        nz: usize,
        in_r: usize,
        in_c: usize,
        o_r: usize,
        o_c: usize,
        stream: *mut c_void,
    ) -> i32;
    /// In-place f32 round-trip of `n` f64 elements (tomopy band quantization).
    #[must_use]
    pub fn tomoxide_fw_round(a: *mut c_void, n: usize, stream: *mut c_void) -> i32;
    /// Forward db5 DWT along the last axis: `[nz,R,C]` → `lo,hi [nz,R,(C+9)/2]`.
    #[must_use]
    pub fn tomoxide_fw_dwt_rows(
        in_: *const c_void,
        lo: *mut c_void,
        hi: *mut c_void,
        nz: usize,
        r: usize,
        c: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Forward db5 DWT along the middle axis: `[nz,R,C]` → `lo,hi [nz,(R+9)/2,C]`.
    #[must_use]
    pub fn tomoxide_fw_dwt_cols(
        in_: *const c_void,
        lo: *mut c_void,
        hi: *mut c_void,
        nz: usize,
        r: usize,
        c: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Inverse db5 DWT along the middle axis: `lo,hi [nz,L0,C]` → `[nz,2L0+2-10,C]`.
    #[must_use]
    pub fn tomoxide_fw_idwt_cols(
        lo: *const c_void,
        hi: *const c_void,
        out: *mut c_void,
        nz: usize,
        l0: usize,
        c: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Inverse db5 DWT along the last axis: `lo,hi [nz,R,L1]` → `[nz,R,2L1+2-10]`.
    #[must_use]
    pub fn tomoxide_fw_idwt_rows(
        lo: *const c_void,
        hi: *const c_void,
        out: *mut c_void,
        nz: usize,
        r: usize,
        l1: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Gather cv `[nz,my,mx]` (f64) → interleaved f32 complex `[nz*mx][my]`.
    #[must_use]
    pub fn tomoxide_fw_damp_gather(
        cv: *const c_void,
        cplx: *mut c_void,
        nz: usize,
        my: usize,
        mx: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Multiply each length-`my` spectrum by the damping vector `d[my]`.
    #[must_use]
    pub fn tomoxide_fw_damp_apply(
        cplx: *mut c_void,
        d: *const c_void,
        nz: usize,
        my: usize,
        mx: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Scatter the real part of `[nz*mx][my]` complex back into cv `[nz,my,mx]`.
    #[must_use]
    pub fn tomoxide_fw_damp_scatter(
        cplx: *const c_void,
        cv: *mut c_void,
        nz: usize,
        my: usize,
        mx: usize,
        stream: *mut c_void,
    ) -> i32;

    // --- Vo all-stripe (`remove_all_stripe`) building blocks, orchestrated
    // from `CudaFbpStream::vo_on_device`. Operate on the f32 sinogram
    // [nz, nproj, ncol] and per-column [nz, ncol] vectors; batched over nz. ---
    /// uniform_filter1d along axis 0 (projection), mode='reflect'.
    #[must_use]
    pub fn tomoxide_vo_uniform_axis0(
        sino: *const c_void,
        out: *mut c_void,
        nz: usize,
        nrow: usize,
        nc: usize,
        size: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `listdiff[z,c] = sum_r |sino - smooth|`.
    #[must_use]
    pub fn tomoxide_vo_absdiff_colsum(
        sino: *const c_void,
        smooth: *const c_void,
        listdiff: *mut c_void,
        nz: usize,
        nrow: usize,
        nc: usize,
        stream: *mut c_void,
    ) -> i32;
    /// 1-D median filter along the last axis of `[nz,R,nc]`, reflect, `size<=256`.
    #[must_use]
    pub fn tomoxide_vo_median_axis1(
        in_: *const c_void,
        out: *mut c_void,
        nz: usize,
        r: usize,
        nc: usize,
        size: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Elementwise `out = num/den` (or 1 where `den==0`).
    #[must_use]
    pub fn tomoxide_vo_ratio(
        num: *const c_void,
        den: *const c_void,
        out: *mut c_void,
        n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Sort each column of `sino[nz,nrow,nc]` → `sorted[z,rank,c]` (+ optional
    /// `perm` rows). Composite (value,row) key = Rust stable order.
    #[must_use]
    pub fn tomoxide_vo_colsort(
        sino: *const c_void,
        sorted: *mut c_void,
        perm: *mut c_void,
        nz: usize,
        nrow: usize,
        nc: usize,
        ascending: i32,
        stream: *mut c_void,
    ) -> i32;
    /// Sort each slice row of `in[nz,n]` → `sorted[z,rank]`.
    #[must_use]
    pub fn tomoxide_vo_slicesort(
        in_: *const c_void,
        sorted: *mut c_void,
        nz: usize,
        n: usize,
        ascending: i32,
        stream: *mut c_void,
    ) -> i32;
    /// `_detect_stripe` raw mask from `listfact` and its descending sort.
    #[must_use]
    pub fn tomoxide_vo_detect_rawmask(
        listfact: *const c_void,
        listsorted: *const c_void,
        rawmask: *mut c_void,
        nz: usize,
        nc: usize,
        snr: f32,
        stream: *mut c_void,
    ) -> i32;
    /// `binary_dilation` (3-element SE); `border_zero != 0` zeroes 2 cols/side.
    #[must_use]
    pub fn tomoxide_vo_dilate(
        rawmask: *const c_void,
        mask: *mut c_void,
        nz: usize,
        nc: usize,
        border_zero: i32,
        stream: *mut c_void,
    ) -> i32;
    /// Compact good (`mask<1`) columns per slice → `goodx[z,:]` + `goodcount[z]`.
    #[must_use]
    pub fn tomoxide_vo_build_goodx(
        mask: *const c_void,
        goodx: *mut c_void,
        goodcount: *mut c_void,
        nz: usize,
        nc: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Per-row linear fill of dead columns from bracketing good columns.
    #[must_use]
    pub fn tomoxide_vo_interp_fill(
        sino: *const c_void,
        work: *mut c_void,
        mask: *const c_void,
        goodx: *const c_void,
        goodcount: *const c_void,
        nz: usize,
        nrow: usize,
        nc: usize,
        stream: *mut c_void,
    ) -> i32;
    /// `_rs_large` per-column intensity factor (f64 + f32 copies).
    #[must_use]
    pub fn tomoxide_vo_rs_large_listfact(
        sinosort: *const c_void,
        sinosmooth: *const c_void,
        lf64: *mut c_void,
        lf32: *mut c_void,
        nz: usize,
        nrow: usize,
        nc: usize,
        ndrop: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Normalise each column by `1/listfact` (f64 divide).
    #[must_use]
    pub fn tomoxide_vo_normalize(
        s: *const c_void,
        lf64: *const c_void,
        out: *mut c_void,
        nz: usize,
        nrow: usize,
        nc: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Scatter `sinosmooth` back through `perm` for masked columns (`out`
    /// pre-seeded with the normalised working copy).
    #[must_use]
    pub fn tomoxide_vo_scatter_masked(
        perm: *const c_void,
        sinosmooth: *const c_void,
        mask: *const c_void,
        out: *mut c_void,
        nz: usize,
        nrow: usize,
        nc: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Scatter `smoothed` back through `perm` for every column (`out` fully
    /// overwritten) — the `_rs_sort` unsort.
    #[must_use]
    pub fn tomoxide_vo_unsort_scatter(
        perm: *const c_void,
        smoothed: *const c_void,
        out: *mut c_void,
        nz: usize,
        nrow: usize,
        nc: usize,
        stream: *mut c_void,
    ) -> i32;

    // --- device-resident analytic pipeline helpers ---
    /// Edge-replicate pad `[nz,nproj,ncols]` → `[nz,nproj,ne]` (centred at `pad_side`).
    #[must_use]
    pub fn tomoxide_pad(
        src: *const c_void,
        dst: *mut c_void,
        nz: usize,
        nproj: usize,
        ncols: usize,
        ne: usize,
        pad_side: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Crop centred window `[nz,nproj,ne]` → `[nz,nproj,ncols]`.
    #[must_use]
    pub fn tomoxide_crop(
        src: *const c_void,
        dst: *mut c_void,
        nz: usize,
        nproj: usize,
        ncols: usize,
        ne: usize,
        pad_side: usize,
        stream: *mut c_void,
    ) -> i32;
    /// Pack slice pairs `(s, s+nz/2)` → complex `[nz/2,nproj,ncols]` (interleaved).
    #[must_use]
    pub fn tomoxide_pack_pairs(
        src: *const c_void,
        dst: *mut c_void,
        nz: usize,
        nproj: usize,
        ncols: usize,
        stream: *mut c_void,
    ) -> i32;
    /// De-interleave complex `[nz/2,n,n]` → `[nz,n,n]`.
    #[must_use]
    pub fn tomoxide_unpack_pairs(
        src: *const c_void,
        dst: *mut c_void,
        nz: usize,
        n: usize,
        stream: *mut c_void,
    ) -> i32;

    // --- Log-polar (lprec) runtime kernels (cuda/lprec.cu) ---
    // Device-resident port of `recon/lprec.rs::process_row`. Buffers are batched
    // over `nz` slices: `g` [nz,nproj,n] real (also the in-place spline-coeff
    // buffer), `flc` [nz,nrho,ntheta] complex (float2), `f` [nz,n,n] real. The
    // geometry grids (`kfull`, per-span coords, target index sets) are uploaded
    // from the host `build_grids` output.
    /// Cubic-B-spline prefilter along the detector axis (one line per angle).
    #[must_use]
    pub fn tomoxide_lprec_prefilter_rows(
        g: *mut c_void,
        nz: i32,
        nproj: i32,
        n: i32,
        stream: *mut c_void,
    ) -> i32;
    /// Cubic-B-spline prefilter along the angle axis (one strided column per detector).
    #[must_use]
    pub fn tomoxide_lprec_prefilter_cols(
        g: *mut c_void,
        nz: i32,
        nproj: i32,
        n: i32,
        stream: *mut c_void,
    ) -> i32;
    /// Gather polar → log-polar: cubic interp of `g` accumulated into the padded
    /// real work buffer `flc` at `targets`. `xs` = detector coord (width n),
    /// `ys` = angle coord (height nproj). `ntheta_pad = 2*(ntheta/2+1)` is the
    /// padded row width (per-slice stride `nrho*ntheta_pad`); `targets` are the
    /// padded within-slice indices.
    #[allow(clippy::too_many_arguments)]
    #[must_use]
    pub fn tomoxide_lprec_gather(
        g: *const c_void,
        flc: *mut c_void,
        targets: *const c_void,
        xs: *const c_void,
        ys: *const c_void,
        npts: i32,
        nz: i32,
        nproj: i32,
        n: i32,
        nrho: i32,
        ntheta_pad: i32,
        stream: *mut c_void,
    ) -> i32;
    /// Broadcast complex multiply `flc[s,i] *= kfull[i]` over the half-complex
    /// `[nrho, ntheta_c]` grid (`ntheta_c = ntheta/2+1`).
    #[must_use]
    pub fn tomoxide_lprec_cmul(
        flc: *mut c_void,
        kfull: *const c_void,
        nz: i32,
        nrho: i32,
        ntheta_c: i32,
        stream: *mut c_void,
    ) -> i32;
    /// Scatter log-polar → Cartesian disk: cubic interp of the padded real `flc`
    /// (×2) summed into `f` at `targets`. `xs` = theta coord (logical width
    /// ntheta), `ys` = rho coord (height nrho); `ntheta_pad` is the padded row
    /// stride.
    #[allow(clippy::too_many_arguments)]
    #[must_use]
    pub fn tomoxide_lprec_scatter(
        flc: *const c_void,
        f: *mut c_void,
        targets: *const c_void,
        xs: *const c_void,
        ys: *const c_void,
        npts: i32,
        nz: i32,
        n: i32,
        nrho: i32,
        ntheta: i32,
        ntheta_pad: i32,
        stream: *mut c_void,
    ) -> i32;

    // --- FP16 (half-precision) variants (cuda/shim_fp16.cu) ---
    // Same semantics as the f32 entry points above, but the device buffers hold
    // `half` (`real = half`) — 2 bytes/element — and the filter runs a
    // half-precision cuFFT (requires power-of-two `ne`). theta/shift stay f32.
    /// `cfunc_filter` compiled with `-DHALF`.
    pub fn tomoxide_filter_fp16_new(nproj: usize, nz: usize, n: usize) -> *mut c_void;
    #[must_use]
    pub fn tomoxide_filter_fp16_apply(
        handle: *mut c_void,
        g: *mut c_void,
        w: *const c_void,
        stream: *mut c_void,
    ) -> i32;
    pub fn tomoxide_filter_fp16_free(handle: *mut c_void);
    /// `cfunc_linerec` compiled with `-DHALF`.
    pub fn tomoxide_linerec_fp16_new(
        nproj: usize,
        nz: usize,
        n: usize,
        ncproj: usize,
        ncz: usize,
    ) -> *mut c_void;
    #[must_use]
    pub fn tomoxide_linerec_fp16_backproject(
        handle: *mut c_void,
        f: *mut c_void,
        g: *const c_void,
        theta: *const f32,
        phi: f32,
        axis: f32,
        gain: f32,
        sz: i32,
        rh: i32,
        stream: *mut c_void,
    ) -> i32;
    pub fn tomoxide_linerec_fp16_free(handle: *mut c_void);
    /// `cfunc_fourierrec` compiled with `-DHALF`.
    pub fn tomoxide_fourierrec_fp16_new(
        nproj: usize,
        nz: usize,
        n: usize,
        theta: *const f32,
    ) -> *mut c_void;
    #[must_use]
    pub fn tomoxide_fourierrec_fp16_backproject(
        handle: *mut c_void,
        f: *mut c_void,
        g: *const c_void,
        stream: *mut c_void,
    ) -> i32;
    pub fn tomoxide_fourierrec_fp16_free(handle: *mut c_void);
    /// Half-precision pad/crop/pack/unpack (pure `__half` data moves).
    #[must_use]
    pub fn tomoxide_pad_fp16(
        src: *const c_void,
        dst: *mut c_void,
        nz: usize,
        nproj: usize,
        ncols: usize,
        ne: usize,
        pad_side: usize,
        stream: *mut c_void,
    ) -> i32;
    #[must_use]
    pub fn tomoxide_crop_fp16(
        src: *const c_void,
        dst: *mut c_void,
        nz: usize,
        nproj: usize,
        ncols: usize,
        ne: usize,
        pad_side: usize,
        stream: *mut c_void,
    ) -> i32;
    #[must_use]
    pub fn tomoxide_pack_pairs_fp16(
        src: *const c_void,
        dst: *mut c_void,
        nz: usize,
        nproj: usize,
        ncols: usize,
        stream: *mut c_void,
    ) -> i32;
    #[must_use]
    pub fn tomoxide_unpack_pairs_fp16(
        src: *const c_void,
        dst: *mut c_void,
        nz: usize,
        n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// On-device cast of `n` contiguous `f32` elements to `f16`, on `stream`.
    #[must_use]
    pub fn tomoxide_cast_f32_to_f16(
        src: *const c_void,
        dst: *mut c_void,
        n: usize,
        stream: *mut c_void,
    ) -> i32;
    /// On-device cast of `n` contiguous `f16` elements to `f32`, on `stream`.
    #[must_use]
    pub fn tomoxide_cast_f16_to_f32(
        src: *const c_void,
        dst: *mut c_void,
        n: usize,
        stream: *mut c_void,
    ) -> i32;

    // --- batched C2C FFT (cuFFT) ---
    /// In-place batched 1-D C2C FFT (`data` = device interleaved float2, length
    /// `n*batch`); inverse is normalized by `1/n`. Returns 0 on success.
    #[must_use]
    pub fn tomoxide_fft_1d(data: *mut c_void, n: usize, batch: usize, inverse: i32) -> i32;
    /// In-place batched 2-D C2C FFT (`rows*cols*batch`); inverse normalized by
    /// `1/(rows*cols)`.
    #[must_use]
    pub fn tomoxide_fft_2d(
        data: *mut c_void,
        rows: usize,
        cols: usize,
        batch: usize,
        inverse: i32,
    ) -> i32;
    /// In-place batched 2-D **R2C** FFT (forward, unnormalized). `data` is a
    /// row-padded real buffer `[rows, 2*(cols/2+1)]` per image (so it overlays
    /// the half-complex output `[rows, cols/2+1]`); on return it holds the
    /// spectrum. Returns 0 on success.
    #[must_use]
    pub fn tomoxide_fft_2d_r2c(data: *mut c_void, rows: usize, cols: usize, batch: usize) -> i32;
    /// In-place batched 2-D **C2R** FFT (inverse, normalized by `1/(rows*cols)`).
    /// Consumes the half-complex spectrum `[rows, cols/2+1]` and writes the
    /// row-padded real image `[rows, 2*(cols/2+1)]` per image in place.
    #[must_use]
    pub fn tomoxide_fft_2d_c2r(data: *mut c_void, rows: usize, cols: usize, batch: usize) -> i32;

    /// Async (non-syncing) C2C variants of [`tomoxide_fft_1d`]/[`tomoxide_fft_2d`]:
    /// they enqueue the transform (+ inverse `1/n` scale) on `cudaStreamPerThread`
    /// and return without a host sync, so the caller's host thread can run CPU work
    /// concurrently. Used only by the host laminography conveyor, whose stage
    /// kernels also run on the null (per-thread) stream and so stay serialized with
    /// the FFT on the device. Returns 0 on success.
    #[must_use]
    pub fn tomoxide_fft_1d_async(data: *mut c_void, n: usize, batch: usize, inverse: i32) -> i32;
    #[must_use]
    pub fn tomoxide_fft_2d_async(
        data: *mut c_void,
        rows: usize,
        cols: usize,
        batch: usize,
        inverse: i32,
    ) -> i32;

    /// The sub-batch the `tomoxide_fft_*` entry points above would run
    /// `(rank, n0, n1, batch, type)` in: `batch` itself when cuFFT's scratch for
    /// the whole batch already fits the per-plan cap, and a smaller divisor when
    /// it must be split so the scratch stays bounded (see `cuda/fft.cu`).
    /// `type`: 0 = C2C, 1 = R2C, 2 = C2R. Creates nothing — a pure query.
    #[must_use]
    pub fn tomoxide_fft_plan_split(rank: i32, n0: i32, n1: i32, batch: i32, r#type: i32) -> i32;

    // --- Fourier/USFFT laminography (LamFourierRec) gridding + modulation ---
    // Device-resident, full-complex mirror of the CPU host loops in
    // `recon/lamino.rs`; the Rust orchestrator drives `tomoxide_fft_1d/2d`
    // between these stages. All complex buffers are interleaved float2.
    /// ramp_filter_detw: edge-replicate real `proj [nlines, detw]` into complex
    /// lines `[nlines, ne]` (`ne`, `pad` from `recon::lamino::projection_filter`).
    #[must_use]
    pub fn tomoxide_lam_ramp_pad(
        proj: *const c_void,
        buf: *mut c_void,
        nlines: i64,
        detw: i32,
        ne: i32,
        pad: i32,
        stream: *mut c_void,
    ) -> i32;
    /// Multiply complex lines `[nlines, ne]` by the per-bin filter weights
    /// `filt [ne]` (ramp folded with the FBP apodisation window) and the
    /// rotation-center phase.
    #[must_use]
    pub fn tomoxide_lam_ramp_mul(
        buf: *mut c_void,
        filt: *const f32,
        nlines: i64,
        ne: i32,
        shift: f32,
        stream: *mut c_void,
    ) -> i32;
    /// Crop complex lines `[nlines, ne]` back to real `proj [nlines, detw]`.
    #[must_use]
    pub fn tomoxide_lam_ramp_crop(
        buf: *const c_void,
        proj: *mut c_void,
        nlines: i64,
        detw: i32,
        ne: i32,
        pad: i32,
        stream: *mut c_void,
    ) -> i32;
    /// fft2d_fwd pre-modulation: real `proj [ntheta,deth,detw]` -> complex
    /// `out * sign(tx)*sign(ty)`.
    #[must_use]
    pub fn tomoxide_lam_fft2d_pre(
        proj: *const c_void,
        out: *mut c_void,
        ntheta: i64,
        deth: i32,
        detw: i32,
        stream: *mut c_void,
    ) -> i32;
    /// fft2d_fwd post-modulation: complex `*= sign(tx)*sign(ty)/(deth*detw)`.
    #[must_use]
    pub fn tomoxide_lam_fft2d_post(
        out: *mut c_void,
        ntheta: i64,
        deth: i32,
        detw: i32,
        stream: *mut c_void,
    ) -> i32;
    /// usfft2d take_x: in-plane frequency sample positions `xs,ys [ntheta,nky,detw]`.
    #[must_use]
    pub fn tomoxide_lam_takexy2d(
        theta: *const c_void,
        xs: *mut c_void,
        ys: *mut c_void,
        ntheta: i64,
        nky: i32,
        detw: i32,
        phi: f32,
        ky0: i32,
        deth: i32,
        stream: *mut c_void,
    ) -> i32;
    /// usfft2d gather (adj): scatter `g [ntheta,nky,detw]` into `fdee [nky,gy,gx]`.
    #[allow(clippy::too_many_arguments)]
    #[must_use]
    pub fn tomoxide_lam_gather2d_adj(
        g: *const c_void,
        xs: *const c_void,
        ys: *const c_void,
        fdee: *mut c_void,
        ntheta: i64,
        nky: i32,
        detw: i32,
        n0: i32,
        n1: i32,
        m0: i32,
        m1: i32,
        mu0: f32,
        mu1: f32,
        gdeth: i32,
        ky0: i32,
        stream: *mut c_void,
    ) -> i32;
    /// usfft2d wrap (adj): fold m-wide borders into the interior in x and y.
    #[must_use]
    pub fn tomoxide_lam_wrap2d_adj(
        fdee: *mut c_void,
        nky: i32,
        n0: i32,
        n1: i32,
        m0: i32,
        m1: i32,
        stream: *mut c_void,
    ) -> i32;
    /// usfft2d centered inverse FFT window extract: `fdee [nky,gy,gx]` -> `win [nky,2n1,2n0]`.
    #[must_use]
    pub fn tomoxide_lam_win2d_extract(
        fdee: *const c_void,
        win: *mut c_void,
        nky: i32,
        n0: i32,
        n1: i32,
        m0: i32,
        m1: i32,
        stream: *mut c_void,
    ) -> i32;
    /// usfft2d centered inverse FFT window scatter (applies `wx*wy` renorm).
    #[must_use]
    pub fn tomoxide_lam_win2d_scatter(
        fdee: *mut c_void,
        win: *const c_void,
        nky: i32,
        n0: i32,
        n1: i32,
        m0: i32,
        m1: i32,
        stream: *mut c_void,
    ) -> i32;
    /// usfft2d divker (adj): deapodize -> `f [n1,nky,n0]` complex.
    #[allow(clippy::too_many_arguments)]
    #[must_use]
    pub fn tomoxide_lam_divker2d_adj(
        fdee: *const c_void,
        f: *mut c_void,
        n1: i32,
        nky: i32,
        n0: i32,
        m0: i32,
        m1: i32,
        mu0: f32,
        mu1: f32,
        fdeth: i32,
        ky0: i32,
        stream: *mut c_void,
    ) -> i32;
    /// usfft1d take_x: depth sample positions `z [deth]`.
    #[must_use]
    pub fn tomoxide_lam_takez1d(z: *mut c_void, deth: i32, phi: f32, stream: *mut c_void) -> i32;
    /// usfft1d gather (adj): scatter `g [n1,deth,n0]` into `fdee [ng,n1,n0]`.
    #[allow(clippy::too_many_arguments)]
    #[must_use]
    pub fn tomoxide_lam_gather1d_adj(
        g: *const c_void,
        z: *const c_void,
        fdee: *mut c_void,
        n1: i32,
        deth: i32,
        n0: i32,
        n2: i32,
        m2: i32,
        mu2: f32,
        stream: *mut c_void,
    ) -> i32;
    /// usfft1d wrap (adj): fold m2-wide borders into the interior along the depth grid.
    #[must_use]
    pub fn tomoxide_lam_wrap1d_adj(
        fdee: *mut c_void,
        n0: i32,
        n1: i32,
        n2: i32,
        m2: i32,
        stream: *mut c_void,
    ) -> i32;
    /// usfft1d centered inverse FFT window extract: `fdee [ng,batch]` -> `lines [batch,2n2]`.
    #[must_use]
    pub fn tomoxide_lam_win1d_extract(
        fdee: *const c_void,
        lines: *mut c_void,
        batch: i64,
        n2: i32,
        m2: i32,
        stream: *mut c_void,
    ) -> i32;
    /// usfft1d centered inverse FFT window scatter (applies `2n2` renorm).
    #[must_use]
    pub fn tomoxide_lam_win1d_scatter(
        fdee: *mut c_void,
        lines: *const c_void,
        batch: i64,
        n2: i32,
        m2: i32,
        stream: *mut c_void,
    ) -> i32;
    /// usfft1d divker (adj): deapodize, take real part -> `f [n1,n2,n0]` real.
    #[must_use]
    pub fn tomoxide_lam_divker1d_adj(
        fdee: *const c_void,
        f: *mut c_void,
        n1: i32,
        n2: i32,
        n0: i32,
        m2: i32,
        mu2: f32,
        stream: *mut c_void,
    ) -> i32;
    /// copyTransposed: `p00 [n1,rh,n2]` -> `vol [rh,n1,n2]`.
    #[must_use]
    pub fn tomoxide_lam_transpose(
        p00: *const c_void,
        vol: *mut c_void,
        n1: i32,
        rh: i32,
        n2: i32,
        stream: *mut c_void,
    ) -> i32;
}