Skip to content

Implement i128 <-> float conversion functions #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ name = "compiler_builtins"
version = "0.1.0"

[build-dependencies]
cast = { version = "0.2.0", optional = true }
cast = { version = "0.2.2", features = ["x128"], optional = true }
rand = { version = "0.3.15", optional = true }

[build-dependencies.gcc]
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -141,22 +141,22 @@ features = ["c"]
- [x] divsi3.c
- [ ] extendhfsf2.c
- [ ] extendsfdf2.c
- [ ] fixdfdi.c
- [ ] fixdfsi.c
- [ ] fixsfdi.c
- [ ] fixsfsi.c
- [ ] fixunsdfdi.c
- [ ] fixunsdfsi.c
- [ ] fixunssfdi.c
- [ ] fixunssfsi.c
- [ ] floatdidf.c
- [x] fixdfdi.c
- [x] fixdfsi.c
- [x] fixsfdi.c
- [x] fixsfsi.c
- [x] fixunsdfdi.c
- [x] fixunsdfsi.c
- [x] fixunssfdi.c
- [x] fixunssfsi.c
- [x] floatdidf.c
- [ ] floatdisf.c
- [ ] floatsidf.c
- [ ] floatsisf.c
- [ ] floatundidf.c
- [x] floatsidf.c
- [x] floatsisf.c
- [x] floatundidf.c
- [ ] floatundisf.c
- [ ] floatunsidf.c
- [ ] floatunsisf.c
- [x] floatunsidf.c
- [x] floatunsisf.c
- [ ] i386/ashldi3.S
- [ ] i386/ashrdi3.S
- [ ] i386/chkstk.S
@@ -196,14 +196,14 @@ These builtins are needed to support 128-bit integers, which are in the process
- [x] ashlti3.c
- [x] ashrti3.c
- [x] divti3.c
- [ ] fixdfti.c
- [ ] fixsfti.c
- [ ] fixunsdfti.c
- [ ] fixunssfti.c
- [ ] floattidf.c
- [ ] floattisf.c
- [ ] floatuntidf.c
- [ ] floatuntisf.c
- [x] fixdfti.c
- [x] fixsfti.c
- [x] fixunsdfti.c
- [x] fixunssfti.c
- [x] floattidf.c
- [x] floattisf.c
- [x] floatuntidf.c
- [x] floatuntisf.c
- [x] lshrti3.c
- [x] modti3.c
- [x] muloti4.c
529 changes: 526 additions & 3 deletions build.rs

Large diffs are not rendered by default.

68 changes: 50 additions & 18 deletions src/float/conv.rs
Original file line number Diff line number Diff line change
@@ -12,10 +12,13 @@ macro_rules! fp_overflow {
}
}

macro_rules! fp_convert {
macro_rules! int_to_float {
($intrinsic:ident: $ity:ty, $fty:ty) => {
int_to_float!($intrinsic: $ity, $fty, "C");
};
($intrinsic:ident: $ity:ty, $fty:ty, $abi:tt) => {

pub extern "C" fn $intrinsic(i: $ity) -> $fty {
pub extern $abi fn $intrinsic(i: $ity) -> $fty {
if i == 0 {
return 0.0
}
@@ -82,22 +85,38 @@ macro_rules! fp_convert {
}
}

fp_convert!(__floatsisf: i32, f32);
fp_convert!(__floatsidf: i32, f64);
fp_convert!(__floatdidf: i64, f64);
fp_convert!(__floatunsisf: u32, f32);
fp_convert!(__floatunsidf: u32, f64);
fp_convert!(__floatundidf: u64, f64);
macro_rules! int_to_float_unadj_on_win {
($intrinsic:ident: $ity:ty, $fty:ty) => {
#[cfg(all(windows, target_pointer_width="64"))]
int_to_float!($intrinsic: $ity, $fty, "unadjusted");
#[cfg(not(all(windows, target_pointer_width="64")))]
int_to_float!($intrinsic: $ity, $fty, "C");
};
}

int_to_float!(__floatsisf: i32, f32);
int_to_float!(__floatsidf: i32, f64);
int_to_float!(__floatdidf: i64, f64);
int_to_float_unadj_on_win!(__floattisf: i128, f32);
int_to_float_unadj_on_win!(__floattidf: i128, f64);
int_to_float!(__floatunsisf: u32, f32);
int_to_float!(__floatunsidf: u32, f64);
int_to_float!(__floatundidf: u64, f64);
int_to_float_unadj_on_win!(__floatuntisf: u128, f32);
int_to_float_unadj_on_win!(__floatuntidf: u128, f64);

#[derive(PartialEq, Debug)]
enum Sign {
Positive,
Negative
}

macro_rules! fp_fix {
macro_rules! float_to_int {
($intrinsic:ident: $fty:ty, $ity:ty) => {
pub extern "C" fn $intrinsic(f: $fty) -> $ity {
float_to_int!($intrinsic: $fty, $ity, "C");
};
($intrinsic:ident: $fty:ty, $ity:ty, $abi:tt) => {
pub extern $abi fn $intrinsic(f: $fty) -> $ity {
let fixint_min = <$ity>::min_value();
let fixint_max = <$ity>::max_value();
let fixint_bits = <$ity>::bits() as usize;
@@ -147,12 +166,25 @@ macro_rules! fp_fix {
}
}

fp_fix!(__fixsfsi: f32, i32);
fp_fix!(__fixsfdi: f32, i64);
fp_fix!(__fixdfsi: f64, i32);
fp_fix!(__fixdfdi: f64, i64);
macro_rules! float_to_int_unadj_on_win {
($intrinsic:ident: $fty:ty, $ity:ty) => {
#[cfg(all(windows, target_pointer_width="64"))]
float_to_int!($intrinsic: $fty, $ity, "unadjusted");
#[cfg(not(all(windows, target_pointer_width="64")))]
float_to_int!($intrinsic: $fty, $ity, "C");
};
}

fp_fix!(__fixunssfsi: f32, u32);
fp_fix!(__fixunssfdi: f32, u64);
fp_fix!(__fixunsdfsi: f64, u32);
fp_fix!(__fixunsdfdi: f64, u64);
float_to_int!(__fixsfsi: f32, i32);
float_to_int!(__fixsfdi: f32, i64);
float_to_int_unadj_on_win!(__fixsfti: f32, i128);
float_to_int!(__fixdfsi: f64, i32);
float_to_int!(__fixdfdi: f64, i64);
float_to_int_unadj_on_win!(__fixdfti: f64, i128);

float_to_int!(__fixunssfsi: f32, u32);
float_to_int!(__fixunssfdi: f32, u64);
float_to_int_unadj_on_win!(__fixunssfti: f32, u128);
float_to_int!(__fixunsdfsi: f64, u32);
float_to_int!(__fixunsdfdi: f64, u64);
float_to_int_unadj_on_win!(__fixunsdfti: f64, u128);
2 changes: 1 addition & 1 deletion src/int/mod.rs
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ macro_rules! int_impl {

fn extract_sign(self) -> (bool, $uty) {
if self < 0 {
(true, !(self as $uty) + 1)
(true, (!(self as $uty)).wrapping_add(1))
} else {
(false, self as $uty)
}
8 changes: 8 additions & 0 deletions tests/fixdfti.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/fixdfti.rs"));
8 changes: 8 additions & 0 deletions tests/fixsfti.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/fixsfti.rs"));
8 changes: 8 additions & 0 deletions tests/fixunsdfti.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/fixunsdfti.rs"));
8 changes: 8 additions & 0 deletions tests/fixunssfti.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/fixunssfti.rs"));
8 changes: 8 additions & 0 deletions tests/floattidf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/floattidf.rs"));
8 changes: 8 additions & 0 deletions tests/floattisf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/floattisf.rs"));
8 changes: 8 additions & 0 deletions tests/floatuntidf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/floatuntidf.rs"));
8 changes: 8 additions & 0 deletions tests/floatuntisf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/floatuntisf.rs"));