Skip to content

Commit bf7adb8

Browse files
committed
Added additional formats
0.3.2-dev
1 parent a7f05fd commit bf7adb8

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

py-posit/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "jposit"
3-
version = "0.3.1-dev"
3+
version = "0.3.2-dev"
44
authors = ["Francesco Urbani <francescourbanidue@gmail.com>"]
55
description = "Rust and Python bindings for the Posit numerical format library."
66
license = "MIT OR Apache-2.0"

py-posit/jposit/demo.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
def demo():
22
from . import from_bits, from_double
3+
print (f"{from_bits(0b0110, 4, 0)=}")
4+
35
print (f"{from_bits(123, 8, 0)=}")
46
print (f"{from_bits(123, 8, 5)=}")
7+
58
print (f"{from_bits(123, 16, 1)=}")
69
print (f"{from_double(44.3, 16, 1)=}")
710
print (f"{from_double(44.3, 16, 6)=}")
11+
12+
print (f"{from_double(0.55222, 32, 2)=}")
13+
14+
print (f"{from_double(214.13, 64, 3)=}")

py-posit/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "maturin"
44

55
[project]
66
name = "jposit"
7-
version = "0.3.1-dev"
7+
version = "0.3.2-dev"
88
description = "jposit"
99
authors = [{ name = "Franceco Urbani", email = "francescourbanidue@gmail.com" }]
1010
readme = "README.md"

py-posit/src/lib.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,46 @@ macro_rules! match_double {
4242
#[pyfunction]
4343
fn __from_bits(bits: u64, n: u32, es: u32) -> Option<f64> {
4444
match_bits!(n, es, bits => {
45+
4, 0 => P4E0,
4546
8, 0 => P8E0,
4647
8, 1 => P8E1,
4748
8, 2 => P8E2,
4849
8, 5 => P8E5,
4950
16, 0 => P16E0,
5051
16, 1 => P16E1,
51-
16, 2 => P16E2
52+
16, 2 => P16E2,
53+
32, 0 => P32E0,
54+
32, 1 => P32E1,
55+
32, 2 => P32E2,
56+
32, 3 => P32E3,
57+
32, 4 => P32E4,
58+
32, 5 => P32E5,
59+
32, 6 => P32E6,
60+
32, 7 => P32E7,
61+
32, 8 => P32E8,
5262
})
5363
}
5464

5565
#[pyfunction]
5666
fn __from_double(x: f64, n: u32, es: u32) -> Option<f64> {
5767
let result = match_double!(x, n, es => {
68+
4, 0 => posit::P4E0,
5869
8, 0 => posit::P8E0,
5970
8, 1 => posit::P8E1,
6071
8, 2 => posit::P8E2,
6172
8, 5 => posit::P8E5,
6273
16, 0 => posit::P16E0,
6374
16, 1 => posit::P16E1,
64-
16, 2 => posit::P16E2
75+
16, 2 => posit::P16E2,
76+
32, 0 => posit::P32E0,
77+
32, 1 => posit::P32E1,
78+
32, 2 => posit::P32E2,
79+
32, 3 => posit::P32E3,
80+
32, 4 => posit::P32E4,
81+
32, 5 => posit::P32E5,
82+
32, 6 => posit::P32E6,
83+
32, 7 => posit::P32E7,
84+
32, 8 => posit::P32E8,
6585
});
6686

6787
result.unwrap_or(None)

src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use core::{f64, fmt, ops, u16, u32, u64, u8};
5656
use cast::{i16, i32, u16, u32, u64, Error};
5757
use typenum::{Cmp, Less, Unsigned, U0, U1, U16, U17, U2, U3, U32, U4, U5, U6, U7, U8, U9};
5858
#[cfg(not(feature = "const-fn"))]
59-
use typenum::{Greater, U33};
59+
use typenum::{Greater, U33, U64};
6060
#[cfg(feature = "const-fn")]
6161
use typenum::{
6262
U10, U11, U12, U13, U14, U15, U18, U19, U20, U21, U22, U23, U24, U25, U26, U27, U28, U29, U30,
@@ -881,6 +881,8 @@ new!(U30, u32, 0b11_1111_1111_1111_1111_1111_1111_1111);
881881
new!(U31, u32, 0b111_1111_1111_1111_1111_1111_1111_1111);
882882
new!(U32, u32, 0b1111_1111_1111_1111_1111_1111_1111_1111);
883883

884+
new!(U64, u64, 0xffff_ffff_ffff_ffff);
885+
884886
// Type alias and checked cast function
885887
macro_rules! ty {
886888
($ty:ident, $bits:ident, $nbits:ident, $es:ident) => {
@@ -896,6 +898,8 @@ macro_rules! ty {
896898
};
897899
}
898900

901+
ty!(P4E0, u8, U4, U0);
902+
899903
ty!(P8E0, u8, U8, U0);
900904
ty!(P8E1, u8, U8, U1);
901905
ty!(P8E2, u8, U8, U2);
@@ -920,6 +924,16 @@ ty!(P32E6, u32, U32, U6);
920924
ty!(P32E7, u32, U32, U7);
921925
ty!(P32E8, u32, U32, U8);
922926

927+
ty!(P64E0, u64, U64, U0);
928+
ty!(P64E1, u64, U64, U1);
929+
ty!(P64E2, u64, U64, U2);
930+
ty!(P64E3, u64, U64, U3);
931+
ty!(P64E4, u64, U64, U4);
932+
ty!(P64E5, u64, U64, U5);
933+
ty!(P64E6, u64, U64, U6);
934+
ty!(P64E7, u64, U64, U7);
935+
ty!(P64E8, u64, U64, U8);
936+
923937
#[cfg(test)]
924938
mod tests {
925939
use cast::{f64, Error, From};

0 commit comments

Comments
 (0)