GitXplorerGitXplorer
i

BFloat16.swift

public
8 stars
1 forks
0 issues

Commits

List of commits on branch main.
Verified
e30a4845062117dd8de7199d80ef4acc475f1edf

Update README.md

iivarflakstad committed 7 months ago
Verified
1bad27ec11568bd06deffc45010576eb97680ea0

Merge pull request #1 from pcuenca/patch-1

iivarflakstad committed 8 months ago
Verified
02118fe3af942d2eb6652d8358ba800c988d5813

Typo Half -> BFloat16

ppcuenca committed 8 months ago
Unverified
f6aa0e1b1d1089594b9ade0f9177a35effa4cd9e

Initial Commit

iivarflakstad committed 8 months ago

README

The README file for this repository.

BFloat16

Brain floating point type for Swift.

Inspired by floating point implementations in

To install via the Swift Package Manager add the following line to your Package.swift file's dependencies:

.package(url: "https://github.com/ivarflakstad/BFloat16.swift.git", from: "1.0.0")

Disclaimer

I have never written Swift before. Is implementing a floating point type an odd choice for a first project in a new language? Yes. Yes it is. I suddenly had the need for bfloat in Swift, it didn't exist, and here we are.

That does not automatically mean this implementation is bad, in fact I think it's pretty decent, but I can't guarantee there are no subtle bugs. The testing is pretty decent though.

Usage

Import BFloat16:

import BFloat16

After importing, use the imported BFloat16 type exactly like you'd use Swift's builtin Float, Double, or Float80 types.

func printDouble(value: BFloat16) {
    print(value * 2.0)
}
printDouble(7.891)

> 15.782

Also supports SIMD

var actual = SIMD4<BFloat16>(1.0, 2.0, 3.0, 4.0)
for _ in 0...10 {
  actual += actual
}
let expected = SIMD4<BFloat16>(2048.0, 4096.0, 6144.0, 8192.0)
assert(actual == expected)

License

BFloat16 is available under the MIT license. See the LICENSE file for more info.