Skip to content

Commit 3d2857a

Browse files
committed
Add integer to f128 conversions
1 parent 857ecab commit 3d2857a

File tree

6 files changed

+300
-26
lines changed

6 files changed

+300
-26
lines changed

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,12 @@ These builtins are needed to support `f16` and `f128`, which are in the process
245245
- [x] fixunstfdi.c
246246
- [x] fixunstfsi.c
247247
- [x] fixunstfti.c
248-
- [ ] floatditf.c
249-
- [ ] floatsitf.c
250-
- [ ] floatunditf.c
251-
- [ ] floatunsitf.c
248+
- [x] floatditf.c
249+
- [x] floatsitf.c
250+
- [x] floattitf.c
251+
- [x] floatunditf.c
252+
- [x] floatunsitf.c
253+
- [x] floatuntitf.c
252254
- [x] multf3.c
253255
- [ ] powitf2.c
254256
- [ ] ppc/fixtfdi.c

build.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,6 @@ mod c {
524524
if (target_arch == "aarch64" || target_arch == "arm64ec") && consider_float_intrinsics {
525525
sources.extend(&[
526526
("__comparetf2", "comparetf2.c"),
527-
("__floatditf", "floatditf.c"),
528-
("__floatsitf", "floatsitf.c"),
529-
("__floatunditf", "floatunditf.c"),
530-
("__floatunsitf", "floatunsitf.c"),
531527
("__divtf3", "divtf3.c"),
532528
("__powitf2", "powitf2.c"),
533529
("__fe_getround", "fp_mode.c"),
@@ -544,21 +540,11 @@ mod c {
544540
}
545541

546542
if target_arch == "mips64" {
547-
sources.extend(&[
548-
("__netf2", "comparetf2.c"),
549-
("__floatsitf", "floatsitf.c"),
550-
("__floatunsitf", "floatunsitf.c"),
551-
("__fe_getround", "fp_mode.c"),
552-
]);
543+
sources.extend(&[("__netf2", "comparetf2.c"), ("__fe_getround", "fp_mode.c")]);
553544
}
554545

555546
if target_arch == "loongarch64" {
556-
sources.extend(&[
557-
("__netf2", "comparetf2.c"),
558-
("__floatsitf", "floatsitf.c"),
559-
("__floatunsitf", "floatunsitf.c"),
560-
("__fe_getround", "fp_mode.c"),
561-
]);
547+
sources.extend(&[("__netf2", "comparetf2.c"), ("__fe_getround", "fp_mode.c")]);
562548
}
563549

564550
// Remove the assembly implementations that won't compile for the target

examples/intrinsics.rs

+56-6
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,18 @@ mod intrinsics {
262262

263263
/* i32 operations */
264264

265+
// floatsisf
266+
pub fn aeabi_i2f(x: i32) -> f32 {
267+
x as f32
268+
}
269+
265270
// floatsidf
266271
pub fn aeabi_i2d(x: i32) -> f64 {
267272
x as f64
268273
}
269274

270-
// floatsisf
271-
pub fn aeabi_i2f(x: i32) -> f32 {
272-
x as f32
275+
pub fn floatsitf(x: i32) -> f128 {
276+
x as f128
273277
}
274278

275279
pub fn aeabi_idiv(a: i32, b: i32) -> i32 {
@@ -292,6 +296,10 @@ mod intrinsics {
292296
x as f64
293297
}
294298

299+
pub fn floatditf(x: i64) -> f128 {
300+
x as f128
301+
}
302+
295303
pub fn mulodi4(a: i64, b: i64) -> i64 {
296304
a * b
297305
}
@@ -312,6 +320,18 @@ mod intrinsics {
312320

313321
/* i128 operations */
314322

323+
pub fn floattisf(x: i128) -> f32 {
324+
x as f32
325+
}
326+
327+
pub fn floattidf(x: i128) -> f64 {
328+
x as f64
329+
}
330+
331+
pub fn floattitf(x: i128) -> f128 {
332+
x as f128
333+
}
334+
315335
pub fn lshrti3(a: i128, b: usize) -> i128 {
316336
a >> b
317337
}
@@ -326,14 +346,18 @@ mod intrinsics {
326346

327347
/* u32 operations */
328348

349+
// floatunsisf
350+
pub fn aeabi_ui2f(x: u32) -> f32 {
351+
x as f32
352+
}
353+
329354
// floatunsidf
330355
pub fn aeabi_ui2d(x: u32) -> f64 {
331356
x as f64
332357
}
333358

334-
// floatunsisf
335-
pub fn aeabi_ui2f(x: u32) -> f32 {
336-
x as f32
359+
pub fn floatunsitf(x: u32) -> f128 {
360+
x as f128
337361
}
338362

339363
pub fn aeabi_uidiv(a: u32, b: u32) -> u32 {
@@ -356,6 +380,10 @@ mod intrinsics {
356380
x as f64
357381
}
358382

383+
pub fn floatunditf(x: u64) -> f128 {
384+
x as f128
385+
}
386+
359387
// udivdi3
360388
pub fn aeabi_uldivmod(a: u64, b: u64) -> u64 {
361389
a * b
@@ -367,6 +395,18 @@ mod intrinsics {
367395

368396
/* u128 operations */
369397

398+
pub fn floatuntisf(x: u128) -> f32 {
399+
x as f32
400+
}
401+
402+
pub fn floatuntidf(x: u128) -> f64 {
403+
x as f64
404+
}
405+
406+
pub fn floatuntitf(x: u128) -> f128 {
407+
x as f128
408+
}
409+
370410
pub fn muloti4(a: u128, b: u128) -> Option<u128> {
371411
a.checked_mul(b)
372412
}
@@ -463,6 +503,16 @@ fn run() {
463503
bb(fixunstfsi(bb(2.)));
464504
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
465505
bb(fixunstfti(bb(2.)));
506+
bb(floatditf(bb(2)));
507+
bb(floatsitf(bb(2)));
508+
bb(floattidf(bb(2)));
509+
bb(floattisf(bb(2)));
510+
bb(floattitf(bb(2)));
511+
bb(floatunditf(bb(2)));
512+
bb(floatunsitf(bb(2)));
513+
bb(floatuntidf(bb(2)));
514+
bb(floatuntisf(bb(2)));
515+
bb(floatuntitf(bb(2)));
466516
bb(gttf(bb(2.), bb(2.)));
467517
bb(lshrti3(bb(2), bb(2)));
468518
bb(lttf(bb(2.), bb(2.)));

src/float/conv.rs

+76
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ mod int_to_float {
9191
repr::<f64>(e, m)
9292
}
9393

94+
#[cfg(not(feature = "no-f16-f128"))]
95+
pub fn u32_to_f128_bits(i: u32) -> u128 {
96+
if i == 0 {
97+
return 0;
98+
}
99+
let n = i.leading_zeros();
100+
101+
// High 64 significant bits, with bit 113 still in tact.
102+
let m = (i as u64) << (f128::SIGNIFICAND_BITS - u32::BITS + 1 - 64 + n);
103+
let e = (u32::BITS + f128::EXPONENT_BIAS - 2 - n) as u64;
104+
// High 64 bits of f128 representation.
105+
let h = (e << (f128::SIGNIFICAND_BITS - 64)) + m;
106+
107+
// Result was calculated as a u64 to avoid 128-bit operations (lower bits are always 0).
108+
(h as u128) << 64
109+
}
110+
94111
pub fn u64_to_f32_bits(i: u64) -> u32 {
95112
let n = i.leading_zeros();
96113
let i_m = i.wrapping_shl(n); // Mantissa, shifted so the first bit is nonzero
@@ -114,6 +131,17 @@ mod int_to_float {
114131
repr::<f64>(e, m)
115132
}
116133

134+
#[cfg(not(feature = "no-f16-f128"))]
135+
pub fn u64_to_f128_bits(i: u64) -> u128 {
136+
if i == 0 {
137+
return 0;
138+
}
139+
let n = i.leading_zeros();
140+
let m = m_f_gt_i::<_, f128>(i, n);
141+
let e = exp::<u64, f128>(n);
142+
repr::<f128>(e, m)
143+
}
144+
117145
pub fn u128_to_f32_bits(i: u128) -> u32 {
118146
let n = i.leading_zeros();
119147
let i_m = i.wrapping_shl(n); // Mantissa, shifted so the first bit is nonzero
@@ -144,6 +172,18 @@ mod int_to_float {
144172
let e = if i == 0 { 0 } else { exp::<u128, f64>(n) };
145173
repr::<f64>(e, m)
146174
}
175+
176+
#[cfg(not(feature = "no-f16-f128"))]
177+
pub fn u128_to_f128_bits(i: u128) -> u128 {
178+
if i == 0 {
179+
return 0;
180+
}
181+
let n = i.leading_zeros();
182+
let (m_base, adj) = m_f_eq_i::<u128, f128>(i, n);
183+
let m = m_adj::<f128>(m_base, adj);
184+
let e = exp::<u128, f128>(n);
185+
repr::<f128>(e, m)
186+
}
147187
}
148188

149189
// Conversions from unsigned integers to floats.
@@ -177,6 +217,24 @@ intrinsics! {
177217
pub extern "C" fn __floatuntidf(i: u128) -> f64 {
178218
f64::from_bits(int_to_float::u128_to_f64_bits(i))
179219
}
220+
221+
#[ppc_alias = __floatunsikf]
222+
#[cfg(not(feature = "no-f16-f128"))]
223+
pub extern "C" fn __floatunsitf(i: u32) -> f128 {
224+
f128::from_bits(int_to_float::u32_to_f128_bits(i))
225+
}
226+
227+
#[ppc_alias = __floatundikf]
228+
#[cfg(not(feature = "no-f16-f128"))]
229+
pub extern "C" fn __floatunditf(i: u64) -> f128 {
230+
f128::from_bits(int_to_float::u64_to_f128_bits(i))
231+
}
232+
233+
#[ppc_alias = __floatuntikf]
234+
#[cfg(not(feature = "no-f16-f128"))]
235+
pub extern "C" fn __floatuntitf(i: u128) -> f128 {
236+
f128::from_bits(int_to_float::u128_to_f128_bits(i))
237+
}
180238
}
181239

182240
// Conversions from signed integers to floats.
@@ -210,6 +268,24 @@ intrinsics! {
210268
pub extern "C" fn __floattidf(i: i128) -> f64 {
211269
int_to_float::signed(i, int_to_float::u128_to_f64_bits)
212270
}
271+
272+
#[ppc_alias = __floatsikf]
273+
#[cfg(not(feature = "no-f16-f128"))]
274+
pub extern "C" fn __floatsitf(i: i32) -> f128 {
275+
int_to_float::signed(i, int_to_float::u32_to_f128_bits)
276+
}
277+
278+
#[ppc_alias = __floatdikf]
279+
#[cfg(not(feature = "no-f16-f128"))]
280+
pub extern "C" fn __floatditf(i: i64) -> f128 {
281+
int_to_float::signed(i, int_to_float::u64_to_f128_bits)
282+
}
283+
284+
#[ppc_alias = __floattikf]
285+
#[cfg(not(feature = "no-f16-f128"))]
286+
pub extern "C" fn __floattitf(i: i128) -> f128 {
287+
int_to_float::signed(i, int_to_float::u128_to_f128_bits)
288+
}
213289
}
214290

215291
/// Generic float to unsigned int conversions.

0 commit comments

Comments
 (0)