Skip to content

Instantly share code, notes, and snippets.

@duemunk
Last active October 25, 2018 09:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duemunk/181c5f179aeda1718991aa912a931f45 to your computer and use it in GitHub Desktop.
Save duemunk/181c5f179aeda1718991aa912a931f45 to your computer and use it in GitHub Desktop.
/*
MIT License
Copyright 2018 Tobias Due Munk
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
extension UInt16 {
var halfFloat: Float {
// 1 bit sign
// 5 bit exponent
// 11 bit significand (10 bit explicitly stored)
let sign = Float((self & 0b1000_0000_0000_0000) >> 15)
let exponent = Float((self & 0b0111_1100_0000_0000) >> 10)
let significand = Float(self & 0b0000_0011_1111_1111)
if exponent == 0b0_0000 {
if significand == 0 {
return pow(-1, sign) * 0
} else {
// (−1)^signbit × 2^(−14) × 0.significantbits
let last = 0 + significand / 0b0011_1111_1111
return pow(-1, sign) * pow(2, -14) * last
}
} else if exponent == 0b1_1111 {
if significand == 0 {
return pow(-1, sign) * .infinity
} else {
return .nan
}
} else {
// (−1)^signbit × 2^(exponent−15) × 1.significantbits
let last = 1 + significand / 0b0011_1111_1111
return pow(-1, sign) * pow(2, exponent - 15) * last
}
}
}
extension Float {
var halfFloatBitPattern: UInt16 {
var value = self
let signBytes: UInt16
if value.sign == .plus {
signBytes = 0b0000_0000_0000_0000
} else {
signBytes = 0b1000_0000_0000_0000
value *= -1
}
guard value.isNaN == false else {
let exponentBytes: UInt16 = 0b1_1111
let significandBytes: UInt16 = 0
return signBytes | exponentBytes | significandBytes
}
guard value != 0 else {
return signBytes
}
let exponent = log2(value).rounded(.down)
let exponentBytes: UInt16
let significandBytes: UInt16
if exponent == 1 {
exponentBytes = 0
let significand = value * 1024
significandBytes = UInt16(significand)
} else if exponent == .infinity {
exponentBytes = 0b1111 << 10
significandBytes = 0
} else if exponent <= -15 {
exponentBytes = 0
let significand = value * 1024
significandBytes = UInt16(significand)
} else {
exponentBytes = UInt16(exponent + 15) << 10
value /= pow(2, exponent)
let significand = (value - 1) * 1024
significandBytes = UInt16(significand)
}
return signBytes | exponentBytes | significandBytes
}
}
@harlanhaskins
Copy link

Any chance you can pop a license on this? Thanks!

@duemunk
Copy link
Author

duemunk commented Oct 12, 2018

Yes I can. Sorry for the delay. Consider it MIT :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment