Skip to content

od: Add support for -t fL (long double, 80-bit on x86) #8426

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/uu/od/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# spell-checker:ignore bigdecimal cfgs extendedbigdecimal

[package]
name = "uu_od"
description = "od ~ (uutils) display formatted representation of input"
Expand All @@ -18,10 +20,15 @@ workspace = true
path = "src/od.rs"

[dependencies]
bigdecimal = { workspace = true }
byteorder = { workspace = true }
clap = { workspace = true }
half = { workspace = true }
uucore = { workspace = true, features = ["parser"] }
uucore = { workspace = true, features = [
"extendedbigdecimal",
"format",
"parser",
] }
fluent = { workspace = true }

[[bin]]
Expand Down
1 change: 1 addition & 0 deletions src/uu/od/src/byteorder_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ gen_byte_order_ops! {
read_u16, write_u16 -> u16,
read_u32, write_u32 -> u32,
read_u64, write_u64 -> u64,
read_u128, write_u128 -> u128,
read_i16, write_i16 -> i16,
read_i32, write_i32 -> i32,
read_i64, write_i64 -> i64,
Expand Down
8 changes: 7 additions & 1 deletion src/uu/od/src/formatter_item_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore (ToDO) formatteriteminfo
// spell-checker:ignore (ToDO) formatteriteminfo extendedbigdecimal

use std::fmt;
use uucore::extendedbigdecimal::ExtendedBigDecimal;

#[allow(clippy::enum_variant_names)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum FormatWriter {
IntWriter(fn(u64) -> String),
FloatWriter(fn(f64) -> String),
BFloatWriter(fn(f64) -> String),
ExtendedBigDecimalWriter(fn(&ExtendedBigDecimal) -> String),
MultibyteWriter(fn(&[u8]) -> String),
}

Expand All @@ -30,6 +32,10 @@ impl fmt::Debug for FormatWriter {
f.write_str("BFloatWriter:")?;
fmt::Pointer::fmt(p, f)
}
Self::ExtendedBigDecimalWriter(ref p) => {
f.write_str("ExtendedBigDecimalWriter:")?;
fmt::Pointer::fmt(&(*p as *const ()), f)
}
Self::MultibyteWriter(ref p) => {
f.write_str("MultibyteWriter:")?;
fmt::Pointer::fmt(&(*p as *const ()), f)
Expand Down
79 changes: 70 additions & 9 deletions src/uu/od/src/input_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore bfloat multifile
// spell-checker:ignore bfloat bigdecimal extendedbigdecimal multifile

use half::{bf16, f16};
use std::io;
use uucore::extendedbigdecimal::ExtendedBigDecimal;

use crate::byteorder_io::ByteOrder;
use crate::multifile_reader::HasError;
Expand Down Expand Up @@ -165,6 +166,51 @@ impl MemoryDecoder<'_> {
let val = f32::from(bf16::from_bits(bits));
f64::from(val)
}

/// Returns an `ExtendedBigDecimal` from the internal buffer at position `start`.
/// Only able to parse 16-bytes padded "f80", at least for now
pub fn read_extended_big_decimal(&self, start: usize, byte_size: usize) -> ExtendedBigDecimal {
assert!(byte_size == 16, "Invalid byte_size: {byte_size}");
let data = self.byte_order.read_u128(&self.data[start..start + 16]);

fn bits(data: u128, offset: usize, size: usize) -> u64 {
(data >> offset & (u128::MAX >> (128 - size))) as u64
}

// Parse an f80 number, see https://en.wikipedia.org/wiki/Extended_precision for details.
// let _pad = bits(data, 80, 48); // Top 48 bits of padding ignored.
let sign = bits(data, 79, 1);
let exp = bits(data, 64, 15) as i64;
let one = bits(data, 63, 1);
// m includes the leading `one`, and needs to be divided by 2**63
let m = bits(data, 0, 64);

let ebd = if exp == 0 {
// Can be zero, subnormal or pseudo-subnormal, but the computation is always the same.
ExtendedBigDecimal::from_number_exp2(m, -16382 - 63)
} else if exp == 0x7fff {
if one == 0 {
// Pseudo-infinity or Pseudo-NaN, both treated as nan.
ExtendedBigDecimal::Nan
} else if m == (1u64 << 63) {
// one == 1, frac == 0
ExtendedBigDecimal::Infinity
} else {
// one == 1, frac != 0
ExtendedBigDecimal::Nan
}
} else {
// exp is not all 0 or 1.
if one == 0 {
// Un-normal, treat as nan
ExtendedBigDecimal::Nan
} else {
ExtendedBigDecimal::from_number_exp2(m, exp - 16383 - 63)
}
};

if sign == 1 { -ebd } else { ebd }
}
}

#[cfg(test)]
Expand All @@ -178,30 +224,45 @@ mod tests {
#[allow(clippy::float_cmp)]
#[allow(clippy::cognitive_complexity)]
fn smoke_test() {
let data = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xff, 0xff];
let data = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xbf, 0xca, 0xfe, 0xca, 0xfe,
0xca, 0xfe, 0xff, 0xff,
];
let mut input = PeekReader::new(Cursor::new(&data));
let mut sut = InputDecoder::new(&mut input, 8, 2, ByteOrder::Little);
let mut sut = InputDecoder::new(&mut input, 16, 2, ByteOrder::Little);

// Peek normal length
let mut mem = sut.peek_read().unwrap();

assert_eq!(8, mem.length());
assert_eq!(16, mem.length());

assert_eq!(-2.0, mem.read_float(0, 8));
assert_eq!(-2.0, mem.read_float(4, 4));
// sign = 1 (negative)
// exp = 0x3fff
assert_eq!(
Into::<ExtendedBigDecimal>::into(-1.5),
mem.read_extended_big_decimal(0, 16)
);
assert_eq!(0xc000_0000_0000_0000, mem.read_uint(0, 8));
assert_eq!(0xc000_0000, mem.read_uint(4, 4));
assert_eq!(0xc000, mem.read_uint(6, 2));
assert_eq!(0xc0, mem.read_uint(7, 1));
assert_eq!(&[0, 0xc0], mem.get_buffer(6));
assert_eq!(&[0, 0xc0, 0xff, 0xff], mem.get_full_buffer(6));
assert_eq!(&[0xca, 0xfe], mem.get_buffer(14));
assert_eq!(&[0xca, 0xfe, 0xff, 0xff], mem.get_full_buffer(14));

let mut copy: Vec<u8> = Vec::new();
mem.clone_buffer(&mut copy);
assert_eq!(vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0], copy);
assert_eq!(
vec![
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xbf, 0xca, 0xfe, 0xca, 0xfe,
0xca, 0xfe
],
copy
);

mem.zero_out_buffer(7, 8);
assert_eq!(&[0, 0, 0xff, 0xff], mem.get_full_buffer(6));
mem.zero_out_buffer(14, 16);
assert_eq!(&[0, 0, 0xff, 0xff], mem.get_full_buffer(14));

// Peek tail
let mem = sut.peek_read().unwrap();
Expand Down
5 changes: 5 additions & 0 deletions src/uu/od/src/od.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,11 @@ fn print_bytes(prefix: &str, input_decoder: &MemoryDecoder, output_info: &Output
let p = input_decoder.read_bfloat(b);
output_text.push_str(&func(p));
}
FormatWriter::ExtendedBigDecimalWriter(func) => {
let p =
input_decoder.read_extended_big_decimal(b, f.formatter_item_info.byte_size);
output_text.push_str(&func(&p));
}
FormatWriter::MultibyteWriter(func) => {
output_text.push_str(&func(input_decoder.get_full_buffer(b)));
}
Expand Down
Loading
Loading