Skip to content

Commit

Permalink
Move NaN constants into array module
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Nov 16, 2023
1 parent 7d53767 commit 89191fc
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 56 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `Array::builder()` and `ArrayBuilder::from_array()`
- Added more `ArrayBuilder` modifiers and made internals public
- Added a fast path to `Array::retrieve_array_subset` methods if the array subset matches a chunk
- Added `ZARR_NAN_F64/F32/F16/BF16` which match the zarr nan bit representation on all implementations
- Added `array::{ZARR_NAN_F64,ZARR_NAN_F32,ZARR_NAN_F16,ZARR_NAN_BF16}` which match the zarr nan bit representation on all implementations
- Added `size_usize()` to `ArrayRepresentation`
- Add generic implementations of storage supertraits (`ReadableWritableStorageTraits` and `ReadableListableStorageTraits`)
- Add `size_prefix()` to stores
Expand Down
4 changes: 1 addition & 3 deletions examples/array_write_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ fn array_write_read() -> Result<(), Box<dyn std::error::Error>> {
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use std::sync::Arc;
use zarrs::{
array::FillValue,
array::{chunk_grid::ChunkGridTraits, DataType},
array::{chunk_grid::ChunkGridTraits, DataType, FillValue, ZARR_NAN_F32},
array_subset::ArraySubset,
node::Node,
storage::store,
ZARR_NAN_F32,
};

// Create a store
Expand Down
7 changes: 5 additions & 2 deletions examples/rectangular_array_write_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
fn rectangular_array_write_read() -> Result<(), Box<dyn std::error::Error>> {
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use zarrs::array::ChunkGrid;
use zarrs::ZARR_NAN_F32;
use zarrs::{array::DataType, array_subset::ArraySubset, storage::store};
use zarrs::{
array::{chunk_grid::RectangularChunkGrid, codec, FillValue},
node::Node,
};
use zarrs::{
array::{DataType, ZARR_NAN_F32},
array_subset::ArraySubset,
storage::store,
};

// Create a store
// let path = tempfile::TempDir::new()?;
Expand Down
3 changes: 1 addition & 2 deletions examples/zip_array_write_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use std::{
};

use zarrs::{
array::Array,
array::{Array, ZARR_NAN_F32},
array_subset::ArraySubset,
storage::{ReadableStorageTraits, ReadableWritableStorageTraits},
ZARR_NAN_F32,
};

// const ARRAY_PATH: &'static str = "/array";
Expand Down
4 changes: 3 additions & 1 deletion src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub mod data_type;
mod dimension_name;
mod fill_value;
mod fill_value_metadata;
mod nan_representations;
mod unsafe_cell_slice;

use std::{collections::HashMap, sync::Arc};
Expand All @@ -58,6 +59,7 @@ pub use self::{
dimension_name::DimensionName,
fill_value::FillValue,
fill_value_metadata::FillValueMetadata,
nan_representations::{ZARR_NAN_BF16, ZARR_NAN_F16, ZARR_NAN_F32, ZARR_NAN_F64},
};

use parking_lot::Mutex;
Expand Down Expand Up @@ -1739,7 +1741,7 @@ mod tests {
use itertools::Itertools;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};

use crate::{storage::store::MemoryStore, ZARR_NAN_F32};
use crate::storage::store::MemoryStore;

use super::*;

Expand Down
4 changes: 2 additions & 2 deletions src/array/array_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ use super::{
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use std::sync::Arc;
/// use zarrs::array::{ArrayBuilder, DataType, FillValue};
/// use zarrs::array::{ArrayBuilder, DataType, FillValue, ZARR_NAN_F32};
/// # let store = Arc::new(zarrs::storage::store::MemoryStore::default());
/// let mut array = ArrayBuilder::new(
/// vec![8, 8], // array shape
/// DataType::Float32,
/// vec![4, 4].into(), // regular chunk shape
/// FillValue::from(zarrs::ZARR_NAN_F32),
/// FillValue::from(ZARR_NAN_F32),
/// )
/// .bytes_to_bytes_codecs(vec![
/// #[cfg(feature = "gzip")]
Expand Down
7 changes: 5 additions & 2 deletions src/array/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use derive_more::From;
use half::{bf16, f16};
use thiserror::Error;

use crate::{metadata::Metadata, ZARR_NAN_F32, ZARR_NAN_F64};
use crate::{
array::{ZARR_NAN_F32, ZARR_NAN_F64},
metadata::Metadata,
};

use super::{
fill_value_metadata::{
Expand Down Expand Up @@ -407,7 +410,7 @@ impl core::fmt::Display for DataType {

#[cfg(test)]
mod tests {
use crate::{ZARR_NAN_BF16, ZARR_NAN_F32, ZARR_NAN_F64};
use crate::array::{ZARR_NAN_BF16, ZARR_NAN_F32, ZARR_NAN_F64};

use super::*;

Expand Down
2 changes: 1 addition & 1 deletion src/array/fill_value_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use half::{bf16, f16};
use num::traits::float::FloatCore;
use serde::{Deserialize, Serialize};

use crate::{ZARR_NAN_BF16, ZARR_NAN_F16, ZARR_NAN_F32, ZARR_NAN_F64};
use crate::array::{ZARR_NAN_BF16, ZARR_NAN_F16, ZARR_NAN_F32, ZARR_NAN_F64};

/// Fill value metadata.
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug, Display)]
Expand Down
41 changes: 41 additions & 0 deletions src/array/nan_representations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::mem::transmute;

use half::{bf16, f16};

// https://github.com/rust-lang/rust/issues/72447

/// The Zarr "NaN" fill value for a 64-bit IEEE 754 floating point number.
#[allow(clippy::unusual_byte_groupings)]
pub const ZARR_NAN_F64: f64 = unsafe {
transmute::<u64, f64>(0b0_11111111111_1000000000000000000000000000000000000000000000000000)
};
// const ZARR_NAN_F64: f64 = f64::from_bits(0b0_11111111111_1000000000000000000000000000000000000000000000000000);

/// The Zarr "NaN" fill value for a 32-bit IEEE 754 floating point number.
#[allow(clippy::unusual_byte_groupings)]
pub const ZARR_NAN_F32: f32 =
unsafe { transmute::<u32, f32>(0b0_11111111_10000000000000000000000) };
// const ZARR_NAN_F32: f32 = f32::from_bits(0b0_11111111_10000000000000000000000);

/// The Zarr "NaN" fill value for a 16-bit IEEE 754 floating point number.
pub const ZARR_NAN_F16: f16 = f16::NAN;

/// The Zarr "NaN" fill value for a 16-bit brain floating point number.
pub const ZARR_NAN_BF16: bf16 = bf16::NAN;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn nan_representations() {
assert_eq!(
bf16::NAN.to_ne_bytes(),
bf16::from_bits(0b0_11111111_1000000).to_ne_bytes()
);
assert_eq!(
f16::NAN.to_ne_bytes(),
f16::from_bits(0b0_11111_1000000000).to_ne_bytes()
);
}
}
42 changes: 0 additions & 42 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@
#![deny(clippy::missing_panics_doc)]
#![cfg_attr(nightly, feature(doc_auto_cfg))]

use std::mem::transmute;

use half::{bf16, f16};

pub mod array;
pub mod array_subset;
pub mod byte_range;
Expand All @@ -72,41 +68,3 @@ pub mod node;
pub mod plugin;
pub mod storage;
pub mod version;

// https://github.com/rust-lang/rust/issues/72447

/// The Zarr "NaN" fill value for a 64-bit IEEE 754 floating point number.
#[allow(clippy::unusual_byte_groupings)]
pub const ZARR_NAN_F64: f64 = unsafe {
transmute::<u64, f64>(0b0_11111111111_1000000000000000000000000000000000000000000000000000)
};
// const ZARR_NAN_F64: f64 = f64::from_bits(0b0_11111111111_1000000000000000000000000000000000000000000000000000);

/// The Zarr "NaN" fill value for a 32-bit IEEE 754 floating point number.
#[allow(clippy::unusual_byte_groupings)]
pub const ZARR_NAN_F32: f32 =
unsafe { transmute::<u32, f32>(0b0_11111111_10000000000000000000000) };
// const ZARR_NAN_F32: f32 = f32::from_bits(0b0_11111111_10000000000000000000000);

/// The Zarr "NaN" fill value for a 16-bit IEEE 754 floating point number.
pub const ZARR_NAN_F16: f16 = f16::NAN;

/// The Zarr "NaN" fill value for a 16-bit brain floating point number.
pub const ZARR_NAN_BF16: bf16 = bf16::NAN;

#[cfg(test)]
pub mod tests {
use super::*;

#[test]
fn nan_representations() {
assert_eq!(
bf16::NAN.to_ne_bytes(),
bf16::from_bits(0b0_11111111_1000000).to_ne_bytes()
);
assert_eq!(
f16::NAN.to_ne_bytes(),
f16::from_bits(0b0_11111_1000000000).to_ne_bytes()
);
}
}

0 comments on commit 89191fc

Please sign in to comment.