aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Galenson <jgalenson@google.com>2021-04-12 20:08:39 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2021-04-12 20:08:39 +0000
commit97ea5b79c65b5b661cb75f601ade727e7389dd8b (patch)
tree57181f2bea0fd16753c94981147761bdc4b79542
parent73f0661a62d8ddc8c4d60c61348808059ba53a29 (diff)
parent69dbd470328b6587bed4e500b820cd4c7c8b0e93 (diff)
downloadhalf-android12-mainline-adbd-release.tar.gz
Upgrade rust/crates/half to 1.7.1 am: e6eab9d4b0 am: 78f0ba923e am: c89be5be64 am: 69dbd47032android-mainline-12.0.0_r99android-mainline-12.0.0_r98android-mainline-12.0.0_r97android-mainline-12.0.0_r96android-mainline-12.0.0_r95android-mainline-12.0.0_r94android-mainline-12.0.0_r93android-mainline-12.0.0_r92android-mainline-12.0.0_r91android-mainline-12.0.0_r90android-mainline-12.0.0_r9android-mainline-12.0.0_r89android-mainline-12.0.0_r88android-mainline-12.0.0_r87android-mainline-12.0.0_r86android-mainline-12.0.0_r85android-mainline-12.0.0_r84android-mainline-12.0.0_r83android-mainline-12.0.0_r82android-mainline-12.0.0_r81android-mainline-12.0.0_r80android-mainline-12.0.0_r8android-mainline-12.0.0_r79android-mainline-12.0.0_r78android-mainline-12.0.0_r77android-mainline-12.0.0_r76android-mainline-12.0.0_r75android-mainline-12.0.0_r74android-mainline-12.0.0_r73android-mainline-12.0.0_r72android-mainline-12.0.0_r71android-mainline-12.0.0_r70android-mainline-12.0.0_r7android-mainline-12.0.0_r69android-mainline-12.0.0_r68android-mainline-12.0.0_r67android-mainline-12.0.0_r66android-mainline-12.0.0_r65android-mainline-12.0.0_r64android-mainline-12.0.0_r63android-mainline-12.0.0_r62android-mainline-12.0.0_r61android-mainline-12.0.0_r60android-mainline-12.0.0_r6android-mainline-12.0.0_r59android-mainline-12.0.0_r58android-mainline-12.0.0_r57android-mainline-12.0.0_r56android-mainline-12.0.0_r53android-mainline-12.0.0_r52android-mainline-12.0.0_r51android-mainline-12.0.0_r50android-mainline-12.0.0_r5android-mainline-12.0.0_r49android-mainline-12.0.0_r48android-mainline-12.0.0_r47android-mainline-12.0.0_r46android-mainline-12.0.0_r45android-mainline-12.0.0_r44android-mainline-12.0.0_r43android-mainline-12.0.0_r42android-mainline-12.0.0_r41android-mainline-12.0.0_r40android-mainline-12.0.0_r39android-mainline-12.0.0_r38android-mainline-12.0.0_r37android-mainline-12.0.0_r35android-mainline-12.0.0_r34android-mainline-12.0.0_r33android-mainline-12.0.0_r32android-mainline-12.0.0_r31android-mainline-12.0.0_r30android-mainline-12.0.0_r3android-mainline-12.0.0_r29android-mainline-12.0.0_r28android-mainline-12.0.0_r27android-mainline-12.0.0_r26android-mainline-12.0.0_r25android-mainline-12.0.0_r24android-mainline-12.0.0_r23android-mainline-12.0.0_r22android-mainline-12.0.0_r21android-mainline-12.0.0_r20android-mainline-12.0.0_r2android-mainline-12.0.0_r19android-mainline-12.0.0_r18android-mainline-12.0.0_r17android-mainline-12.0.0_r16android-mainline-12.0.0_r15android-mainline-12.0.0_r14android-mainline-12.0.0_r13android-mainline-12.0.0_r126android-mainline-12.0.0_r125android-mainline-12.0.0_r124android-mainline-12.0.0_r123android-mainline-12.0.0_r122android-mainline-12.0.0_r121android-mainline-12.0.0_r120android-mainline-12.0.0_r12android-mainline-12.0.0_r119android-mainline-12.0.0_r118android-mainline-12.0.0_r117android-mainline-12.0.0_r116android-mainline-12.0.0_r115android-mainline-12.0.0_r114android-mainline-12.0.0_r113android-mainline-12.0.0_r110android-mainline-12.0.0_r11android-mainline-12.0.0_r109android-mainline-12.0.0_r108android-mainline-12.0.0_r107android-mainline-12.0.0_r106android-mainline-12.0.0_r105android-mainline-12.0.0_r104android-mainline-12.0.0_r103android-mainline-12.0.0_r102android-mainline-12.0.0_r101android-mainline-12.0.0_r100android-mainline-12.0.0_r10android-mainline-12.0.0_r1aml_wif_311811030aml_tz3_311312010aml_tet_311811050aml_sdk_311710000aml_pco_311011000aml_mpr_311911090aml_doc_310851020android12-mainline-wifi-releaseandroid12-mainline-tethering-releaseandroid12-mainline-statsd-releaseandroid12-mainline-sdkext-releaseandroid12-mainline-resolv-releaseandroid12-mainline-permission-releaseandroid12-mainline-neuralnetworks-releaseandroid12-mainline-networkstack-releaseandroid12-mainline-mediaprovider-releaseandroid12-mainline-media-swcodec-releaseandroid12-mainline-media-releaseandroid12-mainline-ipsec-releaseandroid12-mainline-extservices-releaseandroid12-mainline-documentsui-releaseandroid12-mainline-conscrypt-releaseandroid12-mainline-cellbroadcast-releaseandroid12-mainline-captiveportallogin-releaseandroid12-mainline-art-releaseandroid12-mainline-adbd-release
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/half/+/1661995 Change-Id: I652eb813f9d9d1720cd1ea8c16d83ea6c1d796a4
-rw-r--r--.cargo_vcs_info.json2
-rw-r--r--.travis.yml50
-rw-r--r--CHANGELOG.md23
-rw-r--r--Cargo.toml16
-rw-r--r--Cargo.toml.orig16
-rw-r--r--METADATA10
-rw-r--r--README.md7
-rw-r--r--TEST_MAPPING16
-rw-r--r--appveyor.yml156
-rw-r--r--src/bfloat.rs106
-rw-r--r--src/binary16.rs106
-rw-r--r--src/lib.rs7
12 files changed, 284 insertions, 231 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
index a35bc0b..4c10491 100644
--- a/.cargo_vcs_info.json
+++ b/.cargo_vcs_info.json
@@ -1,5 +1,5 @@
{
"git": {
- "sha1": "04b83b7c1954e3c0659a027179ba144b90559e19"
+ "sha1": "4476873652bf25676f0610764b0f14a2f5d2ce62"
}
}
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index cbf87db..0000000
--- a/.travis.yml
+++ /dev/null
@@ -1,50 +0,0 @@
-os:
- - linux
- - windows
-sudo: false
-language: rust
-rust:
- - stable
- - beta
- - nightly
-matrix:
- include:
- - rust: stable
- env: CARGOFLAGS="--features serde"
- - rust: stable
- env: CARGOFLAGS="--features std"
- - rust: stable
- env: CARGOFLAGS="--features std,serde"
- - rust: stable
- env: CARGOFLAGS="--features alloc"
- - rust: nightly
- env: CARGOFLAGS=--all-features
-
- - rust: stable
- os: windows
- env: CARGOFLAGS="--features serde"
- - rust: stable
- os: windows
- env: CARGOFLAGS="--features std"
- - rust: stable
- os: windows
- env: CARGOFLAGS="--features alloc"
- - rust: stable
- os: windows
- env: CARGOFLAGS="--features std,serde"
- - rust: nightly
- os: windows
- env: CARGOFLAGS=--all-features
-
- - rust: 1.32.0
- script:
- - cargo update
- - cargo update -p unicode-normalization --precise 0.1.9
- - cargo update -p criterion --precise 0.3.0
- - cargo test --features std
-branches:
- except:
- - gh-pages
-script:
- - cargo build --verbose --all-targets $CARGOFLAGS
- - cargo test --verbose $CARGOFLAGS
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3488747..490cb6d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,23 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
+## [1.7.1] - 2021-01-17 <a name="1.7.1"></a>
+### Fixed
+- Docs.rs now generates docs for `bytemuck` and `num-traits` optional features.
+
+## [1.7.0] - 2021-01-17 <a name="1.7.0"></a>
+### Added
+- Added optional implementations of `bytemuck` traits `Zeroable` and `Pod` under `bytemuck` cargo
+ feature. By [@charles-r-earp].
+- Added optional implementations of `num-traits` traits `ToPrimitive` and `FromPrimitive` under
+ `num-traits` cargo feature. By [@charles-r-earp].
+- Added implementations of `Binary`, `Octal`, `LowerHex`, and `UpperHex` string format traits to
+ format raw `f16`/`bf16` bytes to string.
+
+### Changed
+- `Debug` trait implementation now formats `f16`/`bf16` as float instead of raw bytes hex. Use newly
+ implemented formatting traits to format in hex instead of `Debug`. Fixes [#37].
+
## [1.6.0] - 2020-05-09 <a name="1.6.0"></a>
### Added
@@ -145,6 +162,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
[#16]: https://github.com/starkat99/half-rs/issues/16
[#23]: https://github.com/starkat99/half-rs/issues/23
[#24]: https://github.com/starkat99/half-rs/issues/24
+[#37]: https://github.com/starkat99/half-rs/issues/37
[@tspiteri]: https://github.com/tspiteri
[@PSeitz]: https://github.com/PSeitz
@@ -153,9 +171,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
[@jfrimmel]: https://github.com/jfrimmel
[@zserik]: https://github.com/zserik
[@bzm3r]: https://github.com/bzm3r
+[@charles-r-earp]: https://github.com/charles-r-earp
-[Unreleased]: https://github.com/starkat99/half-rs/compare/v1.6.0...HEAD
+[Unreleased]: https://github.com/starkat99/half-rs/compare/v1.7.1...HEAD
+[1.7.1]: https://github.com/starkat99/half-rs/compare/v1.7.0...v1.7.1
+[1.7.0]: https://github.com/starkat99/half-rs/compare/v1.6.0...v1.7.0
[1.6.0]: https://github.com/starkat99/half-rs/compare/v1.5.0...v1.6.0
[1.5.0]: https://github.com/starkat99/half-rs/compare/v1.4.1...v1.5.0
[1.4.1]: https://github.com/starkat99/half-rs/compare/v1.4.0...v1.4.1
diff --git a/Cargo.toml b/Cargo.toml
index c122803..73f965d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,8 +13,9 @@
[package]
edition = "2018"
name = "half"
-version = "1.6.0"
+version = "1.7.1"
authors = ["Kathryn Long <squeeself@gmail.com>"]
+exclude = ["/ci", "/*.yml"]
description = "Half-precision floating point f16 and bf16 types for Rust implementing the IEEE 754-2008 standard binary16 and bfloat16 types."
readme = "README.md"
keywords = ["f16", "bfloat16", "no_std"]
@@ -22,11 +23,22 @@ categories = ["no-std", "data-structures", "encoding"]
license = "MIT/Apache-2.0"
repository = "https://github.com/starkat99/half-rs"
[package.metadata.docs.rs]
-features = ["std", "serde"]
+features = ["std", "serde", "bytemuck", "num-traits"]
[[bench]]
name = "convert"
harness = false
+[dependencies.bytemuck]
+version = "1.4.1"
+features = ["derive"]
+optional = true
+default-features = false
+
+[dependencies.num-traits]
+version = "0.2.14"
+optional = true
+default-features = false
+
[dependencies.serde]
version = "1.0"
features = ["derive"]
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index 5fa235f..5963bae 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -1,6 +1,6 @@
[package]
name = "half"
-version = "1.6.0" # Remember to keep in sync with html_root_url crate attribute
+version = "1.7.1" # Remember to keep in sync with html_root_url crate attribute
authors = ["Kathryn Long <squeeself@gmail.com>"]
description = "Half-precision floating point f16 and bf16 types for Rust implementing the IEEE 754-2008 standard binary16 and bfloat16 types."
repository = "https://github.com/starkat99/half-rs"
@@ -9,6 +9,7 @@ keywords = ["f16", "bfloat16", "no_std"]
license = "MIT/Apache-2.0"
categories = ["no-std", "data-structures", "encoding"]
edition = "2018"
+exclude = ["/ci", "/*.yml"]
[badges]
appveyor = { repository = "starkat99/half-rs" }
@@ -21,14 +22,25 @@ use-intrinsics = []
serialize = ["serde"] # Deprecated. Use serde directly.
alloc = []
+[dependencies.bytemuck]
+version = "1.4.1"
+optional = true
+default-features = false
+features = ["derive"]
+
[dependencies.serde]
version = "1.0"
optional = true
default-features = false
features = ["derive"]
+[dependencies.num-traits]
+version = "0.2.14"
+optional = true
+default-features = false
+
[package.metadata.docs.rs]
-features = ["std", "serde"]
+features = ["std", "serde", "bytemuck", "num-traits"]
[dev-dependencies]
criterion = "0.3"
diff --git a/METADATA b/METADATA
index 4d8192e..f66d855 100644
--- a/METADATA
+++ b/METADATA
@@ -7,13 +7,13 @@ third_party {
}
url {
type: ARCHIVE
- value: "https://static.crates.io/crates/half/half-1.6.0.crate"
+ value: "https://static.crates.io/crates/half/half-1.7.1.crate"
}
- version: "1.6.0"
+ version: "1.7.1"
license_type: NOTICE
last_upgrade_date {
- year: 2020
- month: 12
- day: 21
+ year: 2021
+ month: 4
+ day: 1
}
}
diff --git a/README.md b/README.md
index 8de9417..c6afe22 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,7 @@ minimal floating point storage and most major hardware does not implement them,
This crate provides [`no_std`](https://rust-embedded.github.io/book/intro/no-std.html) support by default so can easily be used in embedded
code where a smaller float format is most useful.
-*Requries Rust 1.32 or greater.* If you need support for older versions of Rust, use versions 1.3 and earlier of this crate.
+*Requires Rust 1.32 or greater.* If you need support for older versions of Rust, use versions 1.3 and earlier of this crate.
See the [crate documentation](https://docs.rs/half/) for more details.
@@ -36,6 +36,11 @@ not have hardware support. **Available only on Rust nightly channel.**
Enabling the `std` feature enables runtime CPU feature detection when the `use-intrsincis` feature is also enabled.
Without this feature detection, intrinsics are only used when compiler host target supports them.
+- **`num-traits`** - Enable `ToPrimitive` and `FromPrimitive` trait implementations from the
+ `num-traits` crate.
+
+- **`bytemuck`** - Enable `Zeroable` and `Pod` trait implementations from the `bytemuck` crate.
+
### More Documentation
- [Crate API Reference](https://docs.rs/half/)
diff --git a/TEST_MAPPING b/TEST_MAPPING
index d5f5785..7f5d34a 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -2,16 +2,13 @@
{
"presubmit": [
{
- "name": "serde_cbor_device_test_src_lib"
- },
- {
- "name": "serde_cbor_device_test_tests_value"
+ "name": "serde_cbor_device_test_tests_std_types"
},
{
- "name": "serde_cbor_device_test_tests_de"
+ "name": "serde_cbor_device_test_tests_enum"
},
{
- "name": "serde_cbor_device_test_tests_bennofs"
+ "name": "serde_cbor_device_test_tests_value"
},
{
"name": "serde_cbor_device_test_tests_canonical"
@@ -20,13 +17,16 @@
"name": "serde_cbor_device_test_tests_ser"
},
{
+ "name": "serde_cbor_device_test_tests_bennofs"
+ },
+ {
"name": "serde_cbor_device_test_tests_tags"
},
{
- "name": "serde_cbor_device_test_tests_enum"
+ "name": "serde_cbor_device_test_tests_de"
},
{
- "name": "serde_cbor_device_test_tests_std_types"
+ "name": "serde_cbor_device_test_src_lib"
}
]
}
diff --git a/appveyor.yml b/appveyor.yml
deleted file mode 100644
index 5147453..0000000
--- a/appveyor.yml
+++ /dev/null
@@ -1,156 +0,0 @@
-# Appveyor configuration template for Rust using rustup for Rust installation
-# https://github.com/starkat99/appveyor-rust
-
-branches:
- except:
- - gh-pages
-
-## Operating System (VM environment) ##
-
-# Rust needs at least Visual Studio 2013 Appveyor OS for MSVC targets.
-os: Visual Studio 2015
-
-## Build Matrix ##
-
-# This configuration will setup a build for each channel & target combination (12 windows
-# combinations in all).
-#
-# There are 3 channels: stable, beta, and nightly.
-#
-# Alternatively, the full version may be specified for the channel to build using that specific
-# version (e.g. channel: 1.5.0)
-#
-# The values for target are the set of windows Rust build targets. Each value is of the form
-#
-# ARCH-pc-windows-TOOLCHAIN
-#
-# Where ARCH is the target architecture, either x86_64 or i686, and TOOLCHAIN is the linker
-# toolchain to use, either msvc or gnu. See https://www.rust-lang.org/downloads.html#win-foot for
-# a description of the toolchain differences.
-# See https://github.com/rust-lang-nursery/rustup.rs/#toolchain-specification for description of
-# toolchains and host triples.
-#
-# Comment out channel/target combos you do not wish to build in CI.
-#
-# You may use the `cargoflags` and `RUSTFLAGS` variables to set additional flags for cargo commands
-# and rustc, respectively. For instance, you can uncomment the cargoflags lines in the nightly
-# channels to enable unstable features when building for nightly. Or you could add additional
-# matrix entries to test different combinations of features.
-environment:
- matrix:
- ### MSVC Toolchains ###
-
- # Stable 64-bit MSVC
- - channel: stable
- target: x86_64-pc-windows-msvc
- # Stable 32-bit MSVC
- - channel: stable
- target: i686-pc-windows-msvc
- # Stable 64-bit MSVC w/ optional features
- - channel: stable
- target: x86_64-pc-windows-msvc
- cargoflags: --features "std serde"
- # Stable 64-bit MSVC w/ optional features
- - channel: stable
- target: x86_64-pc-windows-msvc
- cargoflags: --features "alloc"
- # Stable 32-bit MSVC w/ optional features
- - channel: stable
- target: i686-pc-windows-msvc
- cargoflags: --features "std serde"
- # Stable 32-bit MSVC w/ optional features
- - channel: stable
- target: i686-pc-windows-msvc
- cargoflags: --features "alloc"
- # Beta 64-bit MSVC
- - channel: beta
- target: x86_64-pc-windows-msvc
- # Beta 32-bit MSVC
- - channel: beta
- target: i686-pc-windows-msvc
- # Nightly 64-bit MSVC w/ nightly features
- - channel: nightly
- target: x86_64-pc-windows-msvc
- cargoflags: --features "use-intrinsics"
- # Nightly 32-bit MSVC w/ nightly features
- - channel: nightly
- target: i686-pc-windows-msvc
- cargoflags: --features "use-intrinsics"
-
- ### GNU Toolchains ###
-
- # Stable 64-bit GNU
- - channel: stable
- target: x86_64-pc-windows-gnu
- # Stable 32-bit GNU
- - channel: stable
- target: i686-pc-windows-gnu
- # Stable 64-bit GNU w/ optional features
- - channel: stable
- target: x86_64-pc-windows-gnu
- cargoflags: --features "std serde"
- # Stable 64-bit GNU w/ optional features
- - channel: stable
- target: x86_64-pc-windows-gnu
- cargoflags: --features "alloc"
- # Stable 32-bit GNU w/ optional features
- - channel: stable
- target: i686-pc-windows-gnu
- cargoflags: --features "std serde"
- # Stable 32-bit GNU w/ optional features
- - channel: stable
- target: i686-pc-windows-gnu
- cargoflags: --features "alloc"
- # Beta 64-bit GNU
- - channel: beta
- target: x86_64-pc-windows-gnu
- # Beta 32-bit GNU
- - channel: beta
- target: i686-pc-windows-gnu
- # Nightly 64-bit GNU w/ nightly features
- - channel: nightly
- target: x86_64-pc-windows-gnu
- cargoflags: --features "use-intrinsics"
- # Nightly 32-bit GNU w/ nightly features
- - channel: nightly
- target: i686-pc-windows-gnu
- cargoflags: --features "use-intrinsics"
-
-### Allowed failures ###
-
-# See Appveyor documentation for specific details. In short, place any channel or targets you wish
-# to allow build failures on (usually nightly at least is a wise choice). This will prevent a build
-# or test failure in the matching channels/targets from failing the entire build.
-#matrix:
-# allow_failures:
-# - channel: nightly
-
-# If you only care about stable channel build failures, uncomment the following line:
-#- channel: beta
-
-## Install Script ##
-
-# This is the most important part of the Appveyor configuration. This installs the version of Rust
-# specified by the 'channel' and 'target' environment variables from the build matrix. This uses
-# rustup to install Rust.
-#
-# For simple configurations, instead of using the build matrix, you can simply set the
-# default-toolchain and default-host manually here.
-install:
- - appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe
- - rustup-init -yv --default-toolchain %channel% --default-host %target%
- - set PATH=%PATH%;%USERPROFILE%\.cargo\bin
- - rustc -vV
- - cargo -vV
-
-## Build Script ##
-
-# 'cargo test' takes care of building for us, so disable Appveyor's build stage. This prevents
-# the "directory does not contain a project or solution file" error.
-build: false
-
-# Uses 'cargo test' to run tests and build. Alternatively, the project may call compiled programs
-#directly or perform other testing commands. Rust will automatically be placed in the PATH
-# environment variable.
-test_script:
- - cargo test --verbose %cargoflags%
diff --git a/src/bfloat.rs b/src/bfloat.rs
index 09ad035..8d659f7 100644
--- a/src/bfloat.rs
+++ b/src/bfloat.rs
@@ -1,9 +1,14 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
+#[cfg(feature = "bytemuck")]
+use bytemuck::{Pod, Zeroable};
+
use core::{
cmp::Ordering,
- fmt::{Debug, Display, Error, Formatter, LowerExp, UpperExp},
+ fmt::{
+ Binary, Debug, Display, Error, Formatter, LowerExp, LowerHex, Octal, UpperExp, UpperHex,
+ },
num::{FpCategory, ParseFloatError},
str::FromStr,
};
@@ -28,6 +33,7 @@ pub(crate) mod convert;
#[derive(Clone, Copy, Default)]
#[repr(transparent)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "bytemuck", derive(Zeroable, Pod))]
pub struct bf16(u16);
impl bf16 {
@@ -593,7 +599,7 @@ impl FromStr for bf16 {
impl Debug for bf16 {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
- write!(f, "0x{:X}", self.0)
+ write!(f, "{:?}", self.to_f32())
}
}
@@ -615,6 +621,102 @@ impl UpperExp for bf16 {
}
}
+impl Binary for bf16 {
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
+ write!(f, "{:b}", self.0)
+ }
+}
+
+impl Octal for bf16 {
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
+ write!(f, "{:o}", self.0)
+ }
+}
+
+impl LowerHex for bf16 {
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
+ write!(f, "{:x}", self.0)
+ }
+}
+
+impl UpperHex for bf16 {
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
+ write!(f, "{:X}", self.0)
+ }
+}
+
+#[cfg(feature = "num-traits")]
+mod impl_num_traits {
+ use super::bf16;
+ use num_traits::{FromPrimitive, ToPrimitive};
+
+ impl ToPrimitive for bf16 {
+ fn to_i64(&self) -> Option<i64> {
+ Self::to_f32(*self).to_i64()
+ }
+ fn to_u64(&self) -> Option<u64> {
+ Self::to_f32(*self).to_u64()
+ }
+ fn to_i8(&self) -> Option<i8> {
+ Self::to_f32(*self).to_i8()
+ }
+ fn to_u8(&self) -> Option<u8> {
+ Self::to_f32(*self).to_u8()
+ }
+ fn to_i16(&self) -> Option<i16> {
+ Self::to_f32(*self).to_i16()
+ }
+ fn to_u16(&self) -> Option<u16> {
+ Self::to_f32(*self).to_u16()
+ }
+ fn to_i32(&self) -> Option<i32> {
+ Self::to_f32(*self).to_i32()
+ }
+ fn to_u32(&self) -> Option<u32> {
+ Self::to_f32(*self).to_u32()
+ }
+ fn to_f32(&self) -> Option<f32> {
+ Some(Self::to_f32(*self))
+ }
+ fn to_f64(&self) -> Option<f64> {
+ Some(Self::to_f64(*self))
+ }
+ }
+
+ impl FromPrimitive for bf16 {
+ fn from_i64(n: i64) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_u64(n: u64) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_i8(n: i8) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_u8(n: u8) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_i16(n: i16) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_u16(n: u16) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_i32(n: i32) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_u32(n: u32) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_f32(n: f32) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_f64(n: f64) -> Option<Self> {
+ n.to_f64().map(|x| Self::from_f64(x))
+ }
+ }
+}
+
#[allow(
clippy::cognitive_complexity,
clippy::float_cmp,
diff --git a/src/binary16.rs b/src/binary16.rs
index d5164f0..59ef1dc 100644
--- a/src/binary16.rs
+++ b/src/binary16.rs
@@ -1,9 +1,14 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
+#[cfg(feature = "bytemuck")]
+use bytemuck::{Pod, Zeroable};
+
use core::{
cmp::Ordering,
- fmt::{Debug, Display, Error, Formatter, LowerExp, UpperExp},
+ fmt::{
+ Binary, Debug, Display, Error, Formatter, LowerExp, LowerHex, Octal, UpperExp, UpperHex,
+ },
num::{FpCategory, ParseFloatError},
str::FromStr,
};
@@ -25,8 +30,81 @@ pub(crate) mod convert;
#[derive(Clone, Copy, Default)]
#[repr(transparent)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "bytemuck", derive(Zeroable, Pod))]
pub struct f16(u16);
+#[cfg(feature = "num-traits")]
+mod impl_num_traits {
+ use super::f16;
+ use num_traits::{FromPrimitive, ToPrimitive};
+
+ impl ToPrimitive for f16 {
+ fn to_i64(&self) -> Option<i64> {
+ Self::to_f32(*self).to_i64()
+ }
+ fn to_u64(&self) -> Option<u64> {
+ Self::to_f32(*self).to_u64()
+ }
+ fn to_i8(&self) -> Option<i8> {
+ Self::to_f32(*self).to_i8()
+ }
+ fn to_u8(&self) -> Option<u8> {
+ Self::to_f32(*self).to_u8()
+ }
+ fn to_i16(&self) -> Option<i16> {
+ Self::to_f32(*self).to_i16()
+ }
+ fn to_u16(&self) -> Option<u16> {
+ Self::to_f32(*self).to_u16()
+ }
+ fn to_i32(&self) -> Option<i32> {
+ Self::to_f32(*self).to_i32()
+ }
+ fn to_u32(&self) -> Option<u32> {
+ Self::to_f32(*self).to_u32()
+ }
+ fn to_f32(&self) -> Option<f32> {
+ Some(Self::to_f32(*self))
+ }
+ fn to_f64(&self) -> Option<f64> {
+ Some(Self::to_f64(*self))
+ }
+ }
+
+ impl FromPrimitive for f16 {
+ fn from_i64(n: i64) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_u64(n: u64) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_i8(n: i8) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_u8(n: u8) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_i16(n: i16) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_u16(n: u16) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_i32(n: i32) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_u32(n: u32) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_f32(n: f32) -> Option<Self> {
+ n.to_f32().map(|x| Self::from_f32(x))
+ }
+ fn from_f64(n: f64) -> Option<Self> {
+ n.to_f64().map(|x| Self::from_f64(x))
+ }
+ }
+}
+
#[deprecated(
since = "1.4.0",
note = "all constants moved to associated constants of [`f16`](../struct.f16.html)"
@@ -830,7 +908,7 @@ impl FromStr for f16 {
impl Debug for f16 {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
- write!(f, "0x{:X}", self.0)
+ write!(f, "{:?}", self.to_f32())
}
}
@@ -852,6 +930,30 @@ impl UpperExp for f16 {
}
}
+impl Binary for f16 {
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
+ write!(f, "{:b}", self.0)
+ }
+}
+
+impl Octal for f16 {
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
+ write!(f, "{:o}", self.0)
+ }
+}
+
+impl LowerHex for f16 {
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
+ write!(f, "{:x}", self.0)
+ }
+}
+
+impl UpperHex for f16 {
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
+ write!(f, "{:X}", self.0)
+ }
+}
+
#[allow(
clippy::cognitive_complexity,
clippy::float_cmp,
diff --git a/src/lib.rs b/src/lib.rs
index de35758..3f24662 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -29,6 +29,9 @@
//!
//! Support for [`serde`] crate `Serialize` and `Deserialize` traits is provided when the `serde`
//! feature is enabled. This adds a dependency on [`serde`] crate so is an optional cargo feature.
+//! Support for [`bytemuck`] crate `Zeroable` and `Pod` traits is provided with the `bytemuck`
+//! feature. Support for the [`num-traits`] crate `ToPrimitive` and `FromPrimitive` traits is
+//! provided with the `num-traits` feature.
//!
//! The crate uses `#[no_std]` by default, so can be used in embedded environments without using the
//! Rust `std` library. A `std` feature is available, which enables additional utilities using the
@@ -43,6 +46,8 @@
//! [`slice`]: slice/index.html
//! [`prelude`]: prelude/index.html
//! [`serde`]: https://crates.io/crates/serde
+//! [`bytemuck`]: https://crates.io/crates/bytemuck
+//! [`num-traits`]: https://crates.io/crates/num-traits
//! [`vec`]: vec/index.html
#![warn(
@@ -66,7 +71,7 @@
),
feature(stdsimd, f16c_target_feature)
)]
-#![doc(html_root_url = "https://docs.rs/half/1.6.0")]
+#![doc(html_root_url = "https://docs.rs/half/1.7.1")]
#[cfg(all(feature = "alloc", not(feature = "std")))]
extern crate alloc;