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
use necsim_core::cogs::MathsCore;

#[derive(Clone, Debug)]
#[allow(clippy::module_name_repetitions)]
pub enum NvptxMathsCore {}

impl MathsCore for NvptxMathsCore {
    #[inline]
    fn floor(x: f64) -> f64 {
        // IEEE-compliant implementation on CPU and GPU
        unsafe { core::intrinsics::floorf64(x) }
    }

    #[inline]
    fn ceil(x: f64) -> f64 {
        // IEEE-compliant implementation on CPU and GPU
        unsafe { core::intrinsics::ceilf64(x) }
    }

    #[inline]
    fn ln(x: f64) -> f64 {
        // Guard against usage on the CPU as results will NOT match

        #[cfg(target_os = "cuda")]
        unsafe {
            const FRAC_1_LOG2_E: f64 = 1.0_f64 / core::f64::consts::LOG2_E;

            #[allow(clippy::cast_possible_truncation)]
            let x: f32 = x as f32;
            let f: f32;

            core::arch::asm!("lg2.approx.f32 {}, {};", out(reg32) f, in(reg32) x, options(pure, nomem, nostack));

            // f / log_2(e)
            f64::from(f) * FRAC_1_LOG2_E
        }
        #[cfg(not(target_os = "cuda"))]
        {
            // extern "C" {
            //     fn nvptx_maths_core_ln_on_cpu(_x: f64) -> !;
            // }

            // unsafe { nvptx_maths_core_ln_on_cpu(x) }

            // TODO: disallow using NvptxMathsCore::ln on CPU
            unsafe { core::intrinsics::logf64(x) }
        }
    }

    #[inline]
    fn exp(x: f64) -> f64 {
        // Guard against usage on the CPU as results will NOT match

        #[cfg(target_os = "cuda")]
        unsafe {
            #[allow(clippy::cast_possible_truncation)]
            let x: f32 = (x * core::f64::consts::LOG2_E) as f32;
            let f: f32;

            core::arch::asm!("ex2.approx.f32 {}, {};", out(reg32) f, in(reg32) x, options(pure, nomem, nostack));

            f64::from(f)
        }
        #[cfg(not(target_os = "cuda"))]
        {
            extern "C" {
                fn nvptx_maths_core_exp_on_cpu(_x: f64) -> !;
            }

            unsafe { nvptx_maths_core_exp_on_cpu(x) }
        }
    }

    #[inline]
    fn sqrt(x: f64) -> f64 {
        // IEEE-compliant implementation on CPU and GPU
        unsafe { core::intrinsics::sqrtf64(x) }
    }

    #[inline]
    fn pow(x: f64, exp: f64) -> f64 {
        // Guard against usage on the CPU as results will NOT match

        #[cfg(target_os = "cuda")]
        unsafe {
            // Compute x ^ exp = 2 ^ (exp * log2(x))
            // https://stackoverflow.com/a/54273307
            // by https://stackoverflow.com/users/2341466/andars
            // Licensed under CC BY-SA 4.0
            #[allow(clippy::cast_possible_truncation)]
            let x: f32 = x as f32;
            #[allow(clippy::cast_possible_truncation)]
            let exp: f32 = exp as f32;

            let log2_x: f32;
            core::arch::asm!("lg2.approx.f32 {}, {};", out(reg32) log2_x, in(reg32) x, options(pure, nomem, nostack));

            let exp_log2_x = exp * log2_x;

            let f: f32;
            core::arch::asm!("ex2.approx.f32 {}, {};", out(reg32) f, in(reg32) exp_log2_x, options(pure, nomem, nostack));

            f64::from(f)
        }
        #[cfg(not(target_os = "cuda"))]
        {
            extern "C" {
                fn nvptx_maths_core_pow_on_cpu(_x: f64, _exp: f64) -> !;
            }

            unsafe { nvptx_maths_core_pow_on_cpu(x, exp) }
        }
    }

    #[inline]
    fn sin(x: f64) -> f64 {
        // Guard against usage on the CPU as results will NOT match

        #[cfg(target_os = "cuda")]
        unsafe {
            #[allow(clippy::cast_possible_truncation)]
            let x: f32 = x as f32;
            let f: f32;

            core::arch::asm!("sin.approx.f32 {}, {};", out(reg32) f, in(reg32) x, options(pure, nomem, nostack));

            f64::from(f)
        }
        #[cfg(not(target_os = "cuda"))]
        {
            extern "C" {
                fn nvptx_maths_core_sin_on_cpu(_x: f64) -> !;
            }

            unsafe { nvptx_maths_core_sin_on_cpu(x) }
        }
    }

    #[inline]
    fn cos(x: f64) -> f64 {
        // Guard against usage on the CPU as results will NOT match

        #[cfg(target_os = "cuda")]
        unsafe {
            #[allow(clippy::cast_possible_truncation)]
            let x: f32 = x as f32;
            let f: f32;

            core::arch::asm!("cos.approx.f32 {}, {};", out(reg32) f, in(reg32) x, options(pure, nomem, nostack));

            f64::from(f)
        }
        #[cfg(not(target_os = "cuda"))]
        {
            extern "C" {
                fn nvptx_maths_core_cos_on_cpu(_x: f64) -> !;
            }

            unsafe { nvptx_maths_core_cos_on_cpu(x) }
        }
    }

    #[inline]
    fn round(x: f64) -> f64 {
        // Implementation based on IEEE-compliant f64::trunc() on CPU and GPU
        // Logic adapted from libm (Apache 2.0 / MIT dual-licensed):
        // https://github.com/rust-lang/libm/blob/1f7b8/src/math/round.rs#L6-L8

        const ROUND_TRUNC_OFFSET: f64 = 0.5_f64 - 0.25_f64 * f64::EPSILON;

        let offset: f64;

        #[cfg(target_os = "cuda")]
        unsafe {
            core::arch::asm!("copysign.f64 {}, {}, {};", out(reg64) offset, in(reg64) x, in(reg64) ROUND_TRUNC_OFFSET, options(pure, nomem, nostack));
        }
        #[cfg(not(target_os = "cuda"))]
        unsafe {
            offset = core::intrinsics::copysignf64(ROUND_TRUNC_OFFSET, x);
        }

        unsafe { core::intrinsics::truncf64(x + offset) }
    }
}