Skip to content

Commit 53637f7

Browse files
committed
apfloat: make convertor function names for posit compatible with int converters
1 parent 47f6ae5 commit 53637f7

File tree

1 file changed

+86
-37
lines changed

1 file changed

+86
-37
lines changed

num/src/posit.rs

+86-37
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ macro_rules! construct_posit {
2727
$nar:expr,
2828
$guard:ident,
2929
$guard_zero:expr,
30-
$guard_max:expr
30+
$guard_max:expr,
31+
$to:ident,
32+
$into:ident
3133
) => {
3234
#[derive(Copy, Clone, PartialEq, Eq, Hash, Default)]
3335
pub struct $name($internal);
@@ -37,10 +39,10 @@ macro_rules! construct_posit {
3739
pub const NAR: $name = $name($nar);
3840

3941
#[inline]
40-
pub fn as_inner(&self) -> &$internal { &self.0 }
42+
pub const fn $to(&self) -> $internal { self.0 }
4143

4244
#[inline]
43-
pub fn into_inner(self) -> $internal { self.0 }
45+
pub const fn $into(self) -> $internal { self.0 }
4446

4547
#[inline]
4648
pub fn is_nar(&self) -> bool { self == &Self::NAR }
@@ -446,9 +448,48 @@ macro_rules! construct_posit {
446448
};
447449
}
448450

449-
construct_posit!(Posit8, 8, 0, u8, 0, ::core::u8::MAX, 0x80, u16, 0, ::core::u16::MAX);
450-
construct_posit!(Posit16, 16, 1, u16, 0, ::core::u16::MAX, 0x8000, u32, 0, ::core::u32::MAX);
451-
construct_posit!(Posit32, 32, 2, u32, 0, ::core::u32::MAX, 0x8000_0000, u64, 0, ::core::u64::MAX);
451+
construct_posit!(
452+
Posit8,
453+
8,
454+
0,
455+
u8,
456+
0,
457+
::core::u8::MAX,
458+
0x80,
459+
u16,
460+
0,
461+
::core::u16::MAX,
462+
to_u8,
463+
into_u8
464+
);
465+
construct_posit!(
466+
Posit16,
467+
16,
468+
1,
469+
u16,
470+
0,
471+
::core::u16::MAX,
472+
0x8000,
473+
u32,
474+
0,
475+
::core::u32::MAX,
476+
to_u16,
477+
into_u16
478+
);
479+
construct_posit!(
480+
Posit32,
481+
32,
482+
2,
483+
u32,
484+
0,
485+
::core::u32::MAX,
486+
0x8000_0000,
487+
u64,
488+
0,
489+
::core::u64::MAX,
490+
to_u32,
491+
into_u32
492+
);
452493
construct_posit!(
453494
Posit64,
454495
64,
@@ -459,7 +500,9 @@ construct_posit!(
459500
0x8000_0000_0000_0000,
460501
u128,
461502
0,
462-
::core::u128::MAX
503+
::core::u128::MAX,
504+
to_u64,
505+
into_u64
463506
);
464507
construct_posit!(
465508
Posit128,
@@ -471,7 +514,9 @@ construct_posit!(
471514
0x8000_0000_0000_0000_0000_0000_0000_0000,
472515
u256,
473516
u256::ZERO,
474-
u256::MAX
517+
u256::MAX,
518+
to_u128,
519+
into_u128
475520
);
476521
construct_posit!(
477522
Posit256,
@@ -483,7 +528,9 @@ construct_posit!(
483528
u256::from_inner([0, 0, 0, 0x8000_0000_0000_0000]),
484529
u512,
485530
u512::ZERO,
486-
u512::MAX
531+
u512::MAX,
532+
to_u256,
533+
into_u256
487534
);
488535
construct_posit!(
489536
Posit512,
@@ -495,7 +542,9 @@ construct_posit!(
495542
u512::from_inner([0, 0, 0, 0, 0, 0, 0, 0x8000_0000_0000_0000]),
496543
u1024,
497544
u1024::ZERO,
498-
u1024::MAX
545+
u1024::MAX,
546+
to_u512,
547+
into_u512
499548
);
500549

501550
#[cfg(test)]
@@ -504,54 +553,54 @@ mod tests {
504553

505554
use super::*;
506555

507-
construct_posit!(Posit8Es1, 8, 1, u8, 0, 0xff, 0x80, u16, 0, 0xffff);
556+
construct_posit!(Posit8Es1, 8, 1, u8, 0, 0xff, 0x80, u16, 0, 0xffff, to_u8, into_u8);
508557

509558
#[test]
510559
fn posit_test() {
511-
assert_eq!(Posit16::from(1.).into_inner(), 0b0100_0000_0000_0000);
512-
assert_eq!(Posit16::from(1.125).into_inner(), 0b0100_0010_0000_0000);
513-
assert_eq!(Posit16::from(3.25).into_inner(), 0b0101_1010_0000_0000);
514-
assert_eq!(Posit16::from(4.).into_inner(), 0b0110_0000_0000_0000);
515-
assert_eq!(Posit16::from(8.).into_inner(), 0b0110_1000_0000_0000);
516-
assert_eq!(Posit16::from(1024.).into_inner(), 0b0111_1110_0000_0000);
517-
assert_eq!(Posit16::from(-10.).into_inner(), 0b1001_0110_0000_0000);
518-
assert_eq!(Posit16::from(-7. / 16.).into_inner(), 0b1101_0100_0000_0000);
519-
assert_eq!(Posit16::from(-256.).into_inner(), 0b1000_0100_0000_0000);
520-
assert_eq!(Posit16::from(0.).into_inner(), 0b0000_0000_0000_0000);
521-
assert_eq!(Posit16::from(-0.).into_inner(), 0b0000_0000_0000_0000);
560+
assert_eq!(Posit16::from(1.).into_u16(), 0b0100_0000_0000_0000);
561+
assert_eq!(Posit16::from(1.125).into_u16(), 0b0100_0010_0000_0000);
562+
assert_eq!(Posit16::from(3.25).into_u16(), 0b0101_1010_0000_0000);
563+
assert_eq!(Posit16::from(4.).into_u16(), 0b0110_0000_0000_0000);
564+
assert_eq!(Posit16::from(8.).into_u16(), 0b0110_1000_0000_0000);
565+
assert_eq!(Posit16::from(1024.).into_u16(), 0b0111_1110_0000_0000);
566+
assert_eq!(Posit16::from(-10.).into_u16(), 0b1001_0110_0000_0000);
567+
assert_eq!(Posit16::from(-7. / 16.).into_u16(), 0b1101_0100_0000_0000);
568+
assert_eq!(Posit16::from(-256.).into_u16(), 0b1000_0100_0000_0000);
569+
assert_eq!(Posit16::from(0.).into_u16(), 0b0000_0000_0000_0000);
570+
assert_eq!(Posit16::from(-0.).into_u16(), 0b0000_0000_0000_0000);
522571
}
523572

524573
#[test]
525574
fn posit_from_subnormal_test() {
526575
let sub = f32::from_bits(0b0000_0000_0000_1000 << 16); //2 ^ (-130)
527576
assert!(!sub.is_normal());
528577
// With es = 3, regime is -17 and exp is 6. -17 * 8 + 6 = 130
529-
assert_eq!(Posit64::from(sub).into_inner(), 0b0000_0000_0000_0000_0011_1000 << 40);
578+
assert_eq!(Posit64::from(sub).into_u64(), 0b0000_0000_0000_0000_0011_1000 << 40);
530579
assert_eq!(f32::from(Posit64::from(sub)), sub);
531580
}
532581

533582
#[test]
534583
fn posit8es1_test() {
535-
assert_eq!(Posit8Es1::from(1.).into_inner(), 0b0100_0000);
536-
assert_eq!(Posit8Es1::from(1.125).into_inner(), 0b0100_0010);
537-
assert_eq!(Posit8Es1::from(3.25).into_inner(), 0b0101_1010);
538-
assert_eq!(Posit8Es1::from(4.).into_inner(), 0b0110_0000);
539-
assert_eq!(Posit8Es1::from(8.).into_inner(), 0b0110_1000);
540-
assert_eq!(Posit8Es1::from(1024.).into_inner(), 0b0111_1110);
541-
assert_eq!(Posit8Es1::from(-10.).into_inner(), 0b1001_0110);
542-
assert_eq!(Posit8Es1::from(-7. / 16.).into_inner(), 0b1101_0100);
543-
assert_eq!(Posit8Es1::from(-256.).into_inner(), 0b1000_0100);
584+
assert_eq!(Posit8Es1::from(1.).into_u8(), 0b0100_0000);
585+
assert_eq!(Posit8Es1::from(1.125).into_u8(), 0b0100_0010);
586+
assert_eq!(Posit8Es1::from(3.25).into_u8(), 0b0101_1010);
587+
assert_eq!(Posit8Es1::from(4.).into_u8(), 0b0110_0000);
588+
assert_eq!(Posit8Es1::from(8.).into_u8(), 0b0110_1000);
589+
assert_eq!(Posit8Es1::from(1024.).into_u8(), 0b0111_1110);
590+
assert_eq!(Posit8Es1::from(-10.).into_u8(), 0b1001_0110);
591+
assert_eq!(Posit8Es1::from(-7. / 16.).into_u8(), 0b1101_0100);
592+
assert_eq!(Posit8Es1::from(-256.).into_u8(), 0b1000_0100);
544593
}
545594

546595
#[test]
547596
fn posit32_test() {
548-
assert_eq!(Posit32::from(1.).into_inner(), 0b0100_0000 << 24);
597+
assert_eq!(Posit32::from(1.).into_u32(), 0b0100_0000 << 24);
549598
}
550599

551600
#[test]
552601
fn posit256_test() {
553-
assert_eq!(Posit256::from(1.).into_inner(), u256::from(0b0100_0000u64) << 248);
554-
assert_eq!(Posit256::from(1.125).into_inner(), u256::from(0b0100_0000_0010_0000u64) << 240);
602+
assert_eq!(Posit256::from(1.).into_u256(), u256::from(0b0100_0000u64) << 248);
603+
assert_eq!(Posit256::from(1.125).into_u256(), u256::from(0b0100_0000_0010_0000u64) << 240);
555604
}
556605

557606
#[test]
@@ -583,8 +632,8 @@ mod tests {
583632

584633
#[test]
585634
fn posit_neg_test() {
586-
assert_eq!((-Posit256::from(1.)).into_inner(), Posit256::from(-1.).into_inner(),);
587-
assert_eq!((-Posit256::from(0.)).into_inner(), Posit256::from(0.).into_inner(),);
635+
assert_eq!((-Posit256::from(1.)).into_u256(), Posit256::from(-1.).into_u256(),);
636+
assert_eq!((-Posit256::from(0.)).into_u256(), Posit256::from(0.).into_u256(),);
588637
}
589638

590639
#[test]

0 commit comments

Comments
 (0)