Skip to content

Commit 4878c32

Browse files
committed
Add builtins for f16/f128 float conversions
1 parent 7240849 commit 4878c32

File tree

6 files changed

+119
-41
lines changed

6 files changed

+119
-41
lines changed

README.md

+32-27
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ features = ["c"]
142142
- [x] divmodsi4.c
143143
- [x] divsf3.c
144144
- [x] divsi3.c
145-
- [ ] extendhfsf2.c
146145
- [x] extendsfdf2.c
147146
- [x] fixdfdi.c
148147
- [x] fixdfsi.c
@@ -181,9 +180,7 @@ features = ["c"]
181180
- [x] powisf2.c
182181
- [x] subdf3.c
183182
- [x] subsf3.c
184-
- [ ] truncdfhf2.c
185183
- [x] truncdfsf2.c
186-
- [ ] truncsfhf2.c
187184
- [x] udivdi3.c
188185
- [x] udivmoddi4.c
189186
- [x] udivmodsi4.c
@@ -213,60 +210,68 @@ These builtins are needed to support 128-bit integers, which are in the process
213210
- [x] udivti3.c
214211
- [x] umodti3.c
215212

213+
These builtins are needed to support `f16` and `f128`, which are in the process of being added to Rust.
214+
215+
- [ ] addtf3.c
216+
- [ ] comparetf2.c
217+
- [ ] divtf3.c
218+
- [x] extenddftf2.c
219+
- [x] extendhfsf2.c
220+
- [x] extendhftf2.c
221+
- [x] extendsftf2.c
222+
- [ ] fixtfdi.c
223+
- [ ] fixtfsi.c
224+
- [ ] fixtfti.c
225+
- [ ] fixunstfdi.c
226+
- [ ] fixunstfsi.c
227+
- [ ] fixunstfti.c
228+
- [ ] floatditf.c
229+
- [ ] floatsitf.c
230+
- [ ] floatunditf.c
231+
- [ ] floatunsitf.c
232+
- [ ] multf3.c
233+
- [ ] powitf2.c
234+
- [ ] ppc/fixtfdi.c
235+
- [ ] ppc/fixunstfdi.c
236+
- [ ] ppc/floatditf.c
237+
- [ ] ppc/floatunditf.c
238+
- [ ] subtf3.c
239+
- [x] truncdfhf2.c
240+
- [x] truncsfhf2.c
241+
- [x] trunctfdf2.c
242+
- [x] trunctfhf2.c
243+
- [x] trunctfsf2.c
244+
216245
## Unimplemented functions
217246

218247
These builtins involve floating-point types ("`f128`", "`f80`" and complex numbers) that are not supported by Rust.
219248

220-
- ~~addtf3.c~~
221-
- ~~comparetf2.c~~
222249
- ~~divdc3.c~~
223250
- ~~divsc3.c~~
224251
- ~~divtc3.c~~
225-
- ~~divtf3.c~~
226252
- ~~divxc3.c~~
227-
- ~~extenddftf2.c~~
228-
- ~~extendsftf2.c~~
229-
- ~~fixtfdi.c~~
230-
- ~~fixtfsi.c~~
231-
- ~~fixtfti.c~~
232-
- ~~fixunstfdi.c~~
233-
- ~~fixunstfsi.c~~
234-
- ~~fixunstfti.c~~
235253
- ~~fixunsxfdi.c~~
236254
- ~~fixunsxfsi.c~~
237255
- ~~fixunsxfti.c~~
238256
- ~~fixxfdi.c~~
239257
- ~~fixxfti.c~~
240-
- ~~floatditf.c~~
241258
- ~~floatdixf.c~~
242-
- ~~floatsitf.c~~
243259
- ~~floattixf.c~~
244-
- ~~floatunditf.c~~
245260
- ~~floatundixf.c~~
246-
- ~~floatunsitf.c~~
247261
- ~~floatuntixf.c~~
248262
- ~~i386/floatdixf.S~~
249263
- ~~i386/floatundixf.S~~
250264
- ~~muldc3.c~~
251265
- ~~mulsc3.c~~
252266
- ~~multc3.c~~
253-
- ~~multf3.c~~
254267
- ~~mulxc3.c~~
255-
- ~~powitf2.c~~
256268
- ~~powixf2.c~~
257269
- ~~ppc/divtc3.c~~
258-
- ~~ppc/fixtfdi.c~~
259-
- ~~ppc/fixunstfdi.c~~
260-
- ~~ppc/floatditf.c~~
261-
- ~~ppc/floatunditf.c~~
262270
- ~~ppc/gcc_qadd.c~~
263271
- ~~ppc/gcc_qdiv.c~~
264272
- ~~ppc/gcc_qmul.c~~
265273
- ~~ppc/gcc_qsub.c~~
266274
- ~~ppc/multc3.c~~
267-
- ~~subtf3.c~~
268-
- ~~trunctfdf2.c~~
269-
- ~~trunctfsf2.c~~
270275
- ~~x86_64/floatdixf.c~~
271276
- ~~x86_64/floatundixf.S~~
272277

build.rs

-13
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,10 @@ mod c {
288288
sources.extend(&[
289289
("__divdc3", "divdc3.c"),
290290
("__divsc3", "divsc3.c"),
291-
("__extendhfsf2", "extendhfsf2.c"),
292291
("__muldc3", "muldc3.c"),
293292
("__mulsc3", "mulsc3.c"),
294293
("__negdf2", "negdf2.c"),
295294
("__negsf2", "negsf2.c"),
296-
("__truncdfhf2", "truncdfhf2.c"),
297-
("__truncsfhf2", "truncsfhf2.c"),
298295
]);
299296
}
300297

@@ -464,8 +461,6 @@ mod c {
464461
if (target_arch == "aarch64" || target_arch == "arm64ec") && consider_float_intrinsics {
465462
sources.extend(&[
466463
("__comparetf2", "comparetf2.c"),
467-
("__extenddftf2", "extenddftf2.c"),
468-
("__extendsftf2", "extendsftf2.c"),
469464
("__fixtfdi", "fixtfdi.c"),
470465
("__fixtfsi", "fixtfsi.c"),
471466
("__fixtfti", "fixtfti.c"),
@@ -476,8 +471,6 @@ mod c {
476471
("__floatsitf", "floatsitf.c"),
477472
("__floatunditf", "floatunditf.c"),
478473
("__floatunsitf", "floatunsitf.c"),
479-
("__trunctfdf2", "trunctfdf2.c"),
480-
("__trunctfsf2", "trunctfsf2.c"),
481474
("__addtf3", "addtf3.c"),
482475
("__multf3", "multf3.c"),
483476
("__subtf3", "subtf3.c"),
@@ -498,7 +491,6 @@ mod c {
498491

499492
if target_arch == "mips64" {
500493
sources.extend(&[
501-
("__extenddftf2", "extenddftf2.c"),
502494
("__netf2", "comparetf2.c"),
503495
("__addtf3", "addtf3.c"),
504496
("__multf3", "multf3.c"),
@@ -509,14 +501,11 @@ mod c {
509501
("__floatunsitf", "floatunsitf.c"),
510502
("__fe_getround", "fp_mode.c"),
511503
("__divtf3", "divtf3.c"),
512-
("__trunctfdf2", "trunctfdf2.c"),
513-
("__trunctfsf2", "trunctfsf2.c"),
514504
]);
515505
}
516506

517507
if target_arch == "loongarch64" {
518508
sources.extend(&[
519-
("__extenddftf2", "extenddftf2.c"),
520509
("__netf2", "comparetf2.c"),
521510
("__addtf3", "addtf3.c"),
522511
("__multf3", "multf3.c"),
@@ -527,8 +516,6 @@ mod c {
527516
("__floatunsitf", "floatunsitf.c"),
528517
("__fe_getround", "fp_mode.c"),
529518
("__divtf3", "divtf3.c"),
530-
("__trunctfdf2", "trunctfdf2.c"),
531-
("__trunctfsf2", "trunctfsf2.c"),
532519
]);
533520
}
534521

src/float/extend.rs

+31
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,35 @@ intrinsics! {
8181
pub extern "C" fn __extendsfdf2vfp(a: f32) -> f64 {
8282
a as f64 // LLVM generate 'fcvtds'
8383
}
84+
85+
#[avr_skip]
86+
#[aapcs_on_arm]
87+
#[arm_aeabi_alias = __aeabi_h2f]
88+
pub extern "C" fn __extendhfsf2(a: f16) -> f32 {
89+
extend(a)
90+
}
91+
92+
#[avr_skip]
93+
#[aapcs_on_arm]
94+
pub extern "C" fn __gnu_h2f_ieee(a: f16) -> f32 {
95+
extend(a)
96+
}
97+
98+
#[avr_skip]
99+
#[aapcs_on_arm]
100+
pub extern "C" fn __extendhftf2(a: f16) -> f128 {
101+
extend(a)
102+
}
103+
104+
#[avr_skip]
105+
#[aapcs_on_arm]
106+
pub extern "C" fn __extendsftf2(a: f32) -> f128 {
107+
extend(a)
108+
}
109+
110+
#[avr_skip]
111+
#[aapcs_on_arm]
112+
pub extern "C" fn __extenddftf2(a: f64) -> f128 {
113+
extend(a)
114+
}
84115
}

src/float/mod.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,20 @@ macro_rules! float_impl {
127127
self.to_bits() as Self::SignedInt
128128
}
129129
fn eq_repr(self, rhs: Self) -> bool {
130-
if self.is_nan() && rhs.is_nan() {
130+
#[cfg(feature = "mangled-names")]
131+
fn is_nan(x: $ty) -> bool {
132+
// When using mangled-names, the "real" compiler-builtins might not have the
133+
// necessary builtin (__unordtf2) to test whether `f128` is NaN.
134+
// FIXME: Remove once the nightly toolchain has the __unordtf2 builtin
135+
// x is NaN if all the bits of the exponent are set and the significand is non-0
136+
x.repr() & $ty::EXPONENT_MASK == $ty::EXPONENT_MASK
137+
&& x.repr() & $ty::SIGNIFICAND_MASK != 0
138+
}
139+
#[cfg(not(feature = "mangled-names"))]
140+
fn is_nan(x: $ty) -> bool {
141+
x.is_nan()
142+
}
143+
if is_nan(self) && is_nan(rhs) {
131144
true
132145
} else {
133146
self.repr() == rhs.repr()
@@ -171,5 +184,7 @@ macro_rules! float_impl {
171184
};
172185
}
173186

187+
float_impl!(f16, u16, i16, i8, 16, 10);
174188
float_impl!(f32, u32, i32, i16, 32, 23);
175189
float_impl!(f64, u64, i64, i16, 64, 52);
190+
float_impl!(f128, u128, i128, i16, 128, 112);

src/float/trunc.rs

+38
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,42 @@ intrinsics! {
123123
pub extern "C" fn __truncdfsf2vfp(a: f64) -> f32 {
124124
a as f32
125125
}
126+
127+
#[avr_skip]
128+
#[aapcs_on_arm]
129+
#[arm_aeabi_alias = __aeabi_f2h]
130+
pub extern "C" fn __truncsfhf2(a: f32) -> f16 {
131+
trunc(a)
132+
}
133+
134+
#[avr_skip]
135+
#[aapcs_on_arm]
136+
pub extern "C" fn __gnu_f2h_ieee(a: f32) -> f16 {
137+
trunc(a)
138+
}
139+
140+
#[avr_skip]
141+
#[aapcs_on_arm]
142+
#[arm_aeabi_alias = __aeabi_d2h]
143+
pub extern "C" fn __truncdfhf2(a: f32) -> f16 {
144+
trunc(a)
145+
}
146+
147+
#[avr_skip]
148+
#[aapcs_on_arm]
149+
pub extern "C" fn __trunctfhf2(a: f128) -> f16 {
150+
trunc(a)
151+
}
152+
153+
#[avr_skip]
154+
#[aapcs_on_arm]
155+
pub extern "C" fn __trunctfsf2(a: f128) -> f32 {
156+
trunc(a)
157+
}
158+
159+
#[avr_skip]
160+
#[aapcs_on_arm]
161+
pub extern "C" fn __trunctfdf2(a: f128) -> f64 {
162+
trunc(a)
163+
}
126164
}

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#![feature(naked_functions)]
1414
#![feature(repr_simd)]
1515
#![feature(c_unwind)]
16+
#![feature(f16)]
17+
#![feature(f128)]
1618
#![no_builtins]
1719
#![no_std]
1820
#![allow(unused_features)]

0 commit comments

Comments
 (0)