bfloat16

package
v0.1.37 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 2 Imported by: 6 modules, 9 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package bfloat16 is a trivial implementation for the bfloat16 type, based on https://github.com/x448/float16 and the pending issue in https://github.com/x448/float16/issues/22

Index

Constants

View Source
const SmallestNonzero = BFloat16(0x0001) // 5.9604645e-08 (effectively 0x1p-14 * 0x1p-10)

SmallestNonzero is the smallest nonzero denormal value for bfloat16 (9.1835e-41). It's the float16 equivalent for math.SmallestNonzeroFloat32 and math.SmallestNonzeroFloat64. For context, math.SmallestNonzeroFloat32 used the formula 1 / 2**(127 - 1 + 23) to produce the smallest denormal value for float32 (1.401298464324817070923729583289916131280e-45). The equivalent formula for float16 is 1 / 2**(15 - 1 + 10). We use Float16(0x0001) to compile as const.

Variables

This section is empty.

Functions

This section is empty.

Types

type BFloat16

type BFloat16 uint16

BFloat16 (brain floating point)[1][2] floating-point format is a computer number format occupying 16 bits in computer memory; it represents a wide dynamic range of numeric values by using a floating radix point. This format is a shortened (16-bit) version of the 32-bit IEEE 754 single-precision floating-point format (binary32) with the intent of accelerating machine learning and near-sensor computing.

bfloat16 and patents:

func From

func From[T numberTypes](value T) BFloat16

From converts the value from a standard numeric type to BFloat16.

func FromBits

func FromBits(uint16 uint16) BFloat16

FromBits convert an uint16 to a BFloat16.

func FromFloat32

func FromFloat32(x float32) BFloat16

FromFloat32 converts a float32 to a BFloat16 using round-to-nearest-even.

func FromFloat32s

func FromFloat32s(values ...float32) []BFloat16

FromFloat32s converts a variadic list of float32s to a []BFloat16.

func FromFloat64

func FromFloat64(x float64) BFloat16

FromFloat64 converts a float64 to a BFloat16.

func FromNumbers

func FromNumbers[T numberTypes](values ...T) []BFloat16

FromNumbers converts a variadic list of numeric values to a []BFloat16.

func Inf

func Inf(sign int) BFloat16

Inf returns a BFloat16 with an infinity value with the specified sign. A sign >= returns positive infinity. A sign < 0 returns negative infinity.

func NaN

func NaN() BFloat16

NaN returns a BFloat16 with a NaN value.

func (BFloat16) Bits

func (f BFloat16) Bits() uint16

Bits convert BFloat16 to an uint16.

func (BFloat16) Float32

func (f BFloat16) Float32() float32

Float32 converts the BFloat16 to a float32.

func (BFloat16) Float64

func (f BFloat16) Float64() float64

Float64 converts the BFloat16 to a float64.

func (BFloat16) Neg

func (f BFloat16) Neg() BFloat16

Neg returns the negative of the current value.

func (*BFloat16) SetFloat32

func (f *BFloat16) SetFloat32(v float32)

SetFloat32 sets the values from a float32.

func (*BFloat16) SetFloat64

func (f *BFloat16) SetFloat64(v float64)

SetFloat64 sets the values from a float64.

func (BFloat16) String

func (f BFloat16) String() string

String implements fmt.Stringer, and prints a float representation of the BFloat16.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL