Skip to content

Commit 449643f

Browse files
authored
Merge pull request #606 from tgross35/f16-f128-intrinsics-min
Add addition, subtraction, multiplication, and compare operations for `f128`
2 parents 7f33201 + 6a847ab commit 449643f

25 files changed

+923
-226
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ These builtins are needed to support 128-bit integers.
232232

233233
These builtins are needed to support `f16` and `f128`, which are in the process of being added to Rust.
234234

235-
- [ ] addtf3.c
236-
- [ ] comparetf2.c
235+
- [x] addtf3.c
236+
- [x] comparetf2.c
237237
- [ ] divtf3.c
238238
- [x] extenddftf2.c
239239
- [x] extendhfsf2.c
@@ -249,13 +249,13 @@ These builtins are needed to support `f16` and `f128`, which are in the process
249249
- [ ] floatsitf.c
250250
- [ ] floatunditf.c
251251
- [ ] floatunsitf.c
252-
- [ ] multf3.c
252+
- [x] multf3.c
253253
- [ ] powitf2.c
254254
- [ ] ppc/fixtfdi.c
255255
- [ ] ppc/fixunstfdi.c
256256
- [ ] ppc/floatditf.c
257257
- [ ] ppc/floatunditf.c
258-
- [ ] subtf3.c
258+
- [x] subtf3.c
259259
- [x] truncdfhf2.c
260260
- [x] truncsfhf2.c
261261
- [x] trunctfdf2.c

build.rs

-11
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,6 @@ mod c {
543543
("__floatsitf", "floatsitf.c"),
544544
("__floatunditf", "floatunditf.c"),
545545
("__floatunsitf", "floatunsitf.c"),
546-
("__addtf3", "addtf3.c"),
547-
("__multf3", "multf3.c"),
548-
("__subtf3", "subtf3.c"),
549546
("__divtf3", "divtf3.c"),
550547
("__powitf2", "powitf2.c"),
551548
("__fe_getround", "fp_mode.c"),
@@ -564,30 +561,22 @@ mod c {
564561
if target_arch == "mips64" {
565562
sources.extend(&[
566563
("__netf2", "comparetf2.c"),
567-
("__addtf3", "addtf3.c"),
568-
("__multf3", "multf3.c"),
569-
("__subtf3", "subtf3.c"),
570564
("__fixtfsi", "fixtfsi.c"),
571565
("__floatsitf", "floatsitf.c"),
572566
("__fixunstfsi", "fixunstfsi.c"),
573567
("__floatunsitf", "floatunsitf.c"),
574568
("__fe_getround", "fp_mode.c"),
575-
("__divtf3", "divtf3.c"),
576569
]);
577570
}
578571

579572
if target_arch == "loongarch64" {
580573
sources.extend(&[
581574
("__netf2", "comparetf2.c"),
582-
("__addtf3", "addtf3.c"),
583-
("__multf3", "multf3.c"),
584-
("__subtf3", "subtf3.c"),
585575
("__fixtfsi", "fixtfsi.c"),
586576
("__floatsitf", "floatsitf.c"),
587577
("__fixunstfsi", "fixunstfsi.c"),
588578
("__floatunsitf", "floatunsitf.c"),
589579
("__fe_getround", "fp_mode.c"),
590-
("__divtf3", "divtf3.c"),
591580
]);
592581
}
593582

ci/run.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fi
2121
if [ "${NO_STD:-}" = "1" ]; then
2222
echo "nothing to do for no_std"
2323
else
24-
run="cargo test --manifest-path testcrate/Cargo.toml --target $target"
24+
run="cargo test --manifest-path testcrate/Cargo.toml --no-fail-fast --target $target"
2525
$run
2626
$run --release
2727
$run --features c

src/float/add.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::float::Float;
2-
use crate::int::{CastInto, Int};
2+
use crate::int::{CastInto, Int, MinInt};
33

44
/// Returns `a + b`
55
fn add<F: Float>(a: F, b: F) -> F
@@ -57,17 +57,17 @@ where
5757
}
5858

5959
// zero + anything = anything
60-
if a_abs == Int::ZERO {
60+
if a_abs == MinInt::ZERO {
6161
// but we need to get the sign right for zero + zero
62-
if b_abs == Int::ZERO {
62+
if b_abs == MinInt::ZERO {
6363
return F::from_repr(a.repr() & b.repr());
6464
} else {
6565
return b;
6666
}
6767
}
6868

6969
// anything + zero = anything
70-
if b_abs == Int::ZERO {
70+
if b_abs == MinInt::ZERO {
7171
return a;
7272
}
7373
}
@@ -113,10 +113,10 @@ where
113113
// Shift the significand of b by the difference in exponents, with a sticky
114114
// bottom bit to get rounding correct.
115115
let align = a_exponent.wrapping_sub(b_exponent).cast();
116-
if align != Int::ZERO {
116+
if align != MinInt::ZERO {
117117
if align < bits {
118118
let sticky =
119-
F::Int::from_bool(b_significand << bits.wrapping_sub(align).cast() != Int::ZERO);
119+
F::Int::from_bool(b_significand << bits.wrapping_sub(align).cast() != MinInt::ZERO);
120120
b_significand = (b_significand >> align.cast()) | sticky;
121121
} else {
122122
b_significand = one; // sticky; b is known to be non-zero.
@@ -125,8 +125,8 @@ where
125125
if subtraction {
126126
a_significand = a_significand.wrapping_sub(b_significand);
127127
// If a == -b, return +zero.
128-
if a_significand == Int::ZERO {
129-
return F::from_repr(Int::ZERO);
128+
if a_significand == MinInt::ZERO {
129+
return F::from_repr(MinInt::ZERO);
130130
}
131131

132132
// If partial cancellation occured, we need to left-shift the result
@@ -143,8 +143,8 @@ where
143143

144144
// If the addition carried up, we need to right-shift the result and
145145
// adjust the exponent:
146-
if a_significand & implicit_bit << 4 != Int::ZERO {
147-
let sticky = F::Int::from_bool(a_significand & one != Int::ZERO);
146+
if a_significand & implicit_bit << 4 != MinInt::ZERO {
147+
let sticky = F::Int::from_bool(a_significand & one != MinInt::ZERO);
148148
a_significand = a_significand >> 1 | sticky;
149149
a_exponent += 1;
150150
}
@@ -160,7 +160,7 @@ where
160160
// need to shift the significand.
161161
let shift = (1 - a_exponent).cast();
162162
let sticky =
163-
F::Int::from_bool((a_significand << bits.wrapping_sub(shift).cast()) != Int::ZERO);
163+
F::Int::from_bool((a_significand << bits.wrapping_sub(shift).cast()) != MinInt::ZERO);
164164
a_significand = a_significand >> shift.cast() | sticky;
165165
a_exponent = 0;
166166
}
@@ -203,6 +203,16 @@ intrinsics! {
203203
add(a, b)
204204
}
205205

206+
#[cfg(not(any(feature = "no-f16-f128", target_arch = "powerpc", target_arch = "powerpc64")))]
207+
pub extern "C" fn __addtf3(a: f128, b: f128) -> f128 {
208+
add(a, b)
209+
}
210+
211+
#[cfg(all(not(feature = "no-f16-f128"), any(target_arch = "powerpc", target_arch = "powerpc64")))]
212+
pub extern "C" fn __addkf3(a: f128, b: f128) -> f128 {
213+
add(a, b)
214+
}
215+
206216
#[cfg(target_arch = "arm")]
207217
pub extern "C" fn __addsf3vfp(a: f32, b: f32) -> f32 {
208218
a + b

src/float/cmp.rs

+84-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(unreachable_code)]
22

33
use crate::float::Float;
4-
use crate::int::Int;
4+
use crate::int::MinInt;
55

66
#[derive(Clone, Copy)]
77
enum Result {
@@ -172,6 +172,89 @@ intrinsics! {
172172
}
173173
}
174174

175+
#[cfg(not(any(
176+
feature = "no-f16-f128",
177+
target_arch = "powerpc",
178+
target_arch = "powerpc64"
179+
)))]
180+
intrinsics! {
181+
#[avr_skip]
182+
pub extern "C" fn __letf2(a: f128, b: f128) -> i32 {
183+
cmp(a, b).to_le_abi()
184+
}
185+
186+
#[avr_skip]
187+
pub extern "C" fn __getf2(a: f128, b: f128) -> i32 {
188+
cmp(a, b).to_ge_abi()
189+
}
190+
191+
#[avr_skip]
192+
pub extern "C" fn __unordtf2(a: f128, b: f128) -> i32 {
193+
unord(a, b) as i32
194+
}
195+
196+
#[avr_skip]
197+
pub extern "C" fn __eqtf2(a: f128, b: f128) -> i32 {
198+
cmp(a, b).to_le_abi()
199+
}
200+
201+
#[avr_skip]
202+
pub extern "C" fn __lttf2(a: f128, b: f128) -> i32 {
203+
cmp(a, b).to_le_abi()
204+
}
205+
206+
#[avr_skip]
207+
pub extern "C" fn __netf2(a: f128, b: f128) -> i32 {
208+
cmp(a, b).to_le_abi()
209+
}
210+
211+
#[avr_skip]
212+
pub extern "C" fn __gttf2(a: f128, b: f128) -> i32 {
213+
cmp(a, b).to_ge_abi()
214+
}
215+
}
216+
217+
#[cfg(all(
218+
not(feature = "no-f16-f128"),
219+
any(target_arch = "powerpc", target_arch = "powerpc64")
220+
))]
221+
intrinsics! {
222+
#[avr_skip]
223+
pub extern "C" fn __lekf2(a: f128, b: f128) -> i32 {
224+
cmp(a, b).to_le_abi()
225+
}
226+
227+
#[avr_skip]
228+
pub extern "C" fn __gekf2(a: f128, b: f128) -> i32 {
229+
cmp(a, b).to_ge_abi()
230+
}
231+
232+
#[avr_skip]
233+
pub extern "C" fn __unordkf2(a: f128, b: f128) -> i32 {
234+
unord(a, b) as i32
235+
}
236+
237+
#[avr_skip]
238+
pub extern "C" fn __eqkf2(a: f128, b: f128) -> i32 {
239+
cmp(a, b).to_le_abi()
240+
}
241+
242+
#[avr_skip]
243+
pub extern "C" fn __ltkf2(a: f128, b: f128) -> i32 {
244+
cmp(a, b).to_le_abi()
245+
}
246+
247+
#[avr_skip]
248+
pub extern "C" fn __nekf2(a: f128, b: f128) -> i32 {
249+
cmp(a, b).to_le_abi()
250+
}
251+
252+
#[avr_skip]
253+
pub extern "C" fn __gtkf2(a: f128, b: f128) -> i32 {
254+
cmp(a, b).to_ge_abi()
255+
}
256+
}
257+
175258
#[cfg(target_arch = "arm")]
176259
intrinsics! {
177260
pub extern "aapcs" fn __aeabi_fcmple(a: f32, b: f32) -> i32 {

0 commit comments

Comments
 (0)