Commit 5c7fa44e authored by Andrey Zgarbul's avatar Andrey Zgarbul

i32

parent d8b30d21
use super::{P16E1, Q16E1};
use crate::WithSign;
use core::convert::From;
use core::f64;
use core::{f32, f64};
impl From<P16E1> for Q16E1 {
#[inline]
......@@ -534,8 +534,25 @@ impl From<f64> for P16E1 {
impl From<P16E1> for f32 {
#[inline]
fn from(a: P16E1) -> Self {
f64::from(a) as f32
fn from(p_a: P16E1) -> Self {
let mut ui_a = p_a.to_bits();
if p_a.is_zero() {
0.
} else if p_a.is_nar() {
f32::NAN
} else {
let sign_a = ui_a & P16E1::SIGN_MASK;
if sign_a != 0 {
ui_a = ui_a.wrapping_neg();
}
let (k_a, tmp) = P16E1::separate_bits_tmp(ui_a);
let frac_a = ((tmp << 2) as u32) << 7;
let exp_a = (((k_a as u32) << 1) + ((tmp >> 14) as u32)).wrapping_add(127) << 23;
f32::from_bits(exp_a + frac_a + ((sign_a as u32) << 16))
}
}
}
......@@ -549,8 +566,8 @@ impl From<P16E1> for f64 {
} else if p_a.is_nar() {
f64::NAN
} else {
let sign_a = P16E1::sign_ui(ui_a);
if sign_a {
let sign_a = ui_a & P16E1::SIGN_MASK;
if sign_a != 0 {
ui_a = ui_a.wrapping_neg();
}
let (k_a, tmp) = P16E1::separate_bits_tmp(ui_a);
......@@ -558,7 +575,7 @@ impl From<P16E1> for f64 {
let frac_a = ((tmp << 2) as u64) << 36;
let exp_a = (((k_a as u64) << 1) + ((tmp >> 14) as u64)).wrapping_add(1023) << 52;
f64::from_bits(exp_a + frac_a + (((sign_a as u64) & 0x1) << 63))
f64::from_bits(exp_a + frac_a + ((sign_a as u64) << 48))
}
}
}
......@@ -671,3 +688,12 @@ fn convert_p16_f64() {
assert_eq!(p, P16E1::from(f));
}
}
#[test]
fn convert_p16_f32() {
for n in -0x_8000_i16..0x_7fff {
let p = P16E1::new(n);
let f = f32::from(p);
assert_eq!(p, P16E1::from(f));
}
}
......@@ -192,8 +192,8 @@ impl From<P32E2> for f64 {
} else if p_a.is_nar() {
f64::NAN
} else {
let sign_a = P32E2::sign_ui(ui_a);
if sign_a {
let sign_a = ui_a & P32E2::SIGN_MASK;
if sign_a != 0 {
ui_a = ui_a.wrapping_neg();
}
let (k_a, tmp) = P32E2::separate_bits_tmp(ui_a);
......@@ -201,7 +201,7 @@ impl From<P32E2> for f64 {
let frac_a = ((tmp << 3) as u64) << 20;
let exp_a = (((k_a as u64) << 2) + ((tmp >> 29) as u64)).wrapping_add(1023) << 52;
f64::from_bits(exp_a + frac_a + (((sign_a as u64) & 0x1) << 63))
f64::from_bits(exp_a + frac_a + ((sign_a as u64) << 32))
}
}
}
......
use super::{P8E0, Q8E0};
use crate::WithSign;
use core::convert::From;
use core::f64;
use core::{f32, f64};
impl From<P8E0> for Q8E0 {
#[inline]
......@@ -194,8 +194,25 @@ impl From<f64> for P8E0 {
impl From<P8E0> for f32 {
#[inline]
fn from(a: P8E0) -> Self {
f64::from(a) as f32
fn from(p_a: P8E0) -> Self {
let mut ui_a = p_a.to_bits();
if p_a.is_zero() {
0.
} else if p_a.is_nar() {
f32::NAN
} else {
let sign_a = ui_a & P8E0::SIGN_MASK;
if sign_a != 0 {
ui_a = ui_a.wrapping_neg();
}
let (k_a, tmp) = P8E0::separate_bits_tmp(ui_a);
let frac_a = ((tmp << 1) as u32) << 15;
let exp_a = (k_a as u32).wrapping_add(127) << 23;
f32::from_bits(exp_a + frac_a + ((sign_a as u32) << 24))
}
}
}
......@@ -209,8 +226,8 @@ impl From<P8E0> for f64 {
} else if p_a.is_nar() {
f64::NAN
} else {
let sign_a = P8E0::sign_ui(ui_a);
if sign_a {
let sign_a = ui_a & P8E0::SIGN_MASK;
if sign_a != 0 {
ui_a = ui_a.wrapping_neg();
}
let (k_a, tmp) = P8E0::separate_bits_tmp(ui_a);
......@@ -218,7 +235,7 @@ impl From<P8E0> for f64 {
let frac_a = ((tmp << 1) as u64) << 44;
let exp_a = (k_a as u64).wrapping_add(1023) << 52;
f64::from_bits(exp_a + frac_a + (((sign_a as u64) & 0x1) << 63))
f64::from_bits(exp_a + frac_a + ((sign_a as u64) << 56))
}
}
}
......@@ -598,3 +615,12 @@ fn convert_p8_f64() {
assert_eq!(p, P8E0::from(f));
}
}
#[test]
fn convert_p8_f32() {
for n in -0x_80_i8..0x_7f {
let p = P8E0::new(n);
let f = f32::from(p);
assert_eq!(p, P8E0::from(f));
}
}
use core::mem;
//mod convert;
mod convert;
mod math;
mod ops;
......
This diff is collapsed.
......@@ -20,8 +20,8 @@ impl<const N: u32> From<PxE2<{ N }>> for f64 {
} else if p_a.is_nar() {
f64::NAN
} else {
let sign_a = PxE2::<{ N }>::sign_ui(ui_a);
if sign_a {
let sign_a = ui_a & 0x_8000_0000;
if sign_a != 0 {
ui_a = ui_a.wrapping_neg();
}
let (k_a, tmp) = PxE2::<{ N }>::separate_bits_tmp(ui_a);
......@@ -29,7 +29,7 @@ impl<const N: u32> From<PxE2<{ N }>> for f64 {
let frac_a = ((tmp << 3) as u64) << 20;
let exp_a = (((k_a as u64) << 2) + ((tmp >> 29) as u64)).wrapping_add(1023) << 52;
f64::from_bits(exp_a + frac_a + (((sign_a as u64) & 0x1) << 63))
f64::from_bits(exp_a + frac_a + ((sign_a as u64) << 32))
}
}
}
......@@ -47,13 +47,7 @@ impl<const N: u32> From<f64> for PxE2<{ N }> {
return Self::ZERO;
} else if !float.is_finite() {
return Self::NAR;
} /* else if float >= 1.329_227_995_784_916_e36 {
//maxpos
return Self::MAX;
} else if float <= -1.329_227_995_784_916_e36 {
// -maxpos
return Self::MIN;
}*/
}
let sign = float < 0.;
......@@ -61,12 +55,6 @@ impl<const N: u32> From<f64> for PxE2<{ N }> {
0x4000_0000
} else if float == -1. {
0xC000_0000
/*} else if (float <= 7.523_163_845_262_64_e-37) && !sign {
//minpos
0x1
} else if (float >= -7.523_163_845_262_64_e-37) && sign {
//-minpos
0xFFFF_FFFF*/
} else if (float > 1.) || (float < -1.) {
if sign {
//Make negative numbers positive for easier computation
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment