aboutsummaryrefslogtreecommitdiff
path: root/embassy-rp/src/float/cmp.rs
blob: f917eb9b34680f0f6135403c9886fb88d0f59461 (plain)
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
// Credit: taken from `rp-hal` (also licensed Apache+MIT)
// https://github.com/rp-rs/rp-hal/blob/main/rp2040-hal/src/float/cmp.rs

use super::Float;
use crate::rom_data;

trait ROMCmp {
    fn rom_cmp(self, b: Self) -> i32;
}

impl ROMCmp for f32 {
    fn rom_cmp(self, b: Self) -> i32 {
        rom_data::float_funcs::fcmp(self, b)
    }
}

impl ROMCmp for f64 {
    fn rom_cmp(self, b: Self) -> i32 {
        rom_data::double_funcs::dcmp(self, b)
    }
}

fn le_abi<F: Float + ROMCmp>(a: F, b: F) -> i32 {
    if a.is_nan() || b.is_nan() { 1 } else { a.rom_cmp(b) }
}

fn ge_abi<F: Float + ROMCmp>(a: F, b: F) -> i32 {
    if a.is_nan() || b.is_nan() { -1 } else { a.rom_cmp(b) }
}

intrinsics! {
    #[slower_than_default]
    #[bootrom_v2]
    #[alias = __eqsf2, __ltsf2, __nesf2]
    extern "C" fn __lesf2(a: f32, b: f32) -> i32 {
        le_abi(a, b)
    }

    #[slower_than_default]
    #[bootrom_v2]
    #[alias = __eqdf2, __ltdf2, __nedf2]
    extern "C" fn __ledf2(a: f64, b: f64) -> i32 {
        le_abi(a, b)
    }

    #[slower_than_default]
    #[bootrom_v2]
    #[alias = __gtsf2]
    extern "C" fn __gesf2(a: f32, b: f32) -> i32 {
        ge_abi(a, b)
    }

    #[slower_than_default]
    #[bootrom_v2]
    #[alias = __gtdf2]
    extern "C" fn __gedf2(a: f64, b: f64) -> i32 {
        ge_abi(a, b)
    }


    #[slower_than_default]
    #[bootrom_v2]
    extern "aapcs" fn __aeabi_fcmple(a: f32, b: f32) -> i32 {
        (le_abi(a, b) <= 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "aapcs" fn __aeabi_fcmpge(a: f32, b: f32) -> i32 {
        (ge_abi(a, b) >= 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "aapcs" fn __aeabi_fcmpeq(a: f32, b: f32) -> i32 {
        (le_abi(a, b) == 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "aapcs" fn __aeabi_fcmplt(a: f32, b: f32) -> i32 {
        (le_abi(a, b) < 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "aapcs" fn __aeabi_fcmpgt(a: f32, b: f32) -> i32 {
        (ge_abi(a, b) > 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "aapcs" fn __aeabi_dcmple(a: f64, b: f64) -> i32 {
        (le_abi(a, b) <= 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "aapcs" fn __aeabi_dcmpge(a: f64, b: f64) -> i32 {
        (ge_abi(a, b) >= 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "aapcs" fn __aeabi_dcmpeq(a: f64, b: f64) -> i32 {
        (le_abi(a, b) == 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "aapcs" fn __aeabi_dcmplt(a: f64, b: f64) -> i32 {
        (le_abi(a, b) < 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "aapcs" fn __aeabi_dcmpgt(a: f64, b: f64) -> i32 {
        (ge_abi(a, b) > 0) as i32
    }


    #[slower_than_default]
    #[bootrom_v2]
    extern "C" fn __gesf2vfp(a: f32, b: f32) -> i32 {
        (ge_abi(a, b) >= 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "C" fn __gedf2vfp(a: f64, b: f64) -> i32 {
        (ge_abi(a, b) >= 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "C" fn __gtsf2vfp(a: f32, b: f32) -> i32 {
        (ge_abi(a, b) > 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "C" fn __gtdf2vfp(a: f64, b: f64) -> i32 {
        (ge_abi(a, b) > 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "C" fn __ltsf2vfp(a: f32, b: f32) -> i32 {
        (le_abi(a, b) < 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "C" fn __ltdf2vfp(a: f64, b: f64) -> i32 {
        (le_abi(a, b) < 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "C" fn __lesf2vfp(a: f32, b: f32) -> i32 {
        (le_abi(a, b) <= 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "C" fn __ledf2vfp(a: f64, b: f64) -> i32 {
        (le_abi(a, b) <= 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "C" fn __nesf2vfp(a: f32, b: f32) -> i32 {
        (le_abi(a, b) != 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "C" fn __nedf2vfp(a: f64, b: f64) -> i32 {
        (le_abi(a, b) != 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "C" fn __eqsf2vfp(a: f32, b: f32) -> i32 {
        (le_abi(a, b) == 0) as i32
    }

    #[slower_than_default]
    #[bootrom_v2]
    extern "C" fn __eqdf2vfp(a: f64, b: f64) -> i32 {
        (le_abi(a, b) == 0) as i32
    }
}