All files / src/chain float.ts

91.33% Statements 116/127
95.65% Branches 44/46
77.27% Functions 17/22
90.67% Lines 107/118

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250                  1x 1x 1x       86x 86x 86x 43x 86x 86x       61x 61x 49x 41x 30x     1x 1x           1459x       1459x 39x 39x 39x 1420x     1459x 1459x 8x   1459x           16345x 16345x 16345x 16345x 16345x 178x   16345x                                         17804x                 37960x 37960x 596x   37364x         16359x 16359x   179x 179x   16180x 15967x   15879x       16336x         1x 1x 1x       1x 1x 1x           1x   1x 1x 1x     20x 1x   19x 19x 6x 13x 6x     13x       9333x         35x 35x 35x 35x 35x 35x 35x   35x 16x 16x 19x 3x 16x 1x 1x 52x 52x   1x 1x   15x 15x   35x 35x 35x 560x 560x   35x         22x 22x 22x 3x   19x 19x 2x   19x                       9376x 2x   9374x                 19064x         9366x         7x       9333x      
import {ABISerializableObject} from '../serializer/serializable'
import {ABIDecoder} from '../serializer/decoder'
import {ABIEncoder} from '../serializer/encoder'
import {isInstanceOf, secureRandom} from '../utils'
 
import {Bytes, BytesType} from '../'
 
type FloatType = Float | number | string
 
const positiveInfinity = new Set(['inf', '+inf', 'infinity', '+infinity'])
const negativeInfinity = new Set(['-inf', '-infinity'])
const notANumber = new Set(['nan', '+nan', '-nan'])
 
// nodeos's default NaN carries the sign bit on every width (SoftFloat 8086-SSE).
function nanBytes(byteWidth: number, negative: boolean) {
    const bytes = new Uint8Array(byteWidth)
    const view = new DataView(bytes.buffer)
    if (byteWidth === 4) view.setFloat32(0, NaN, true)
    else view.setFloat64(0, NaN, true)
    if (negative) bytes[byteWidth - 1] |= 0x80
    return new Bytes(bytes)
}
 
function parseFloatText(text: string) {
    const word = text.trim().toLowerCase()
    if (positiveInfinity.has(word)) return Infinity
    if (negativeInfinity.has(word)) return -Infinity
    if (notANumber.has(word)) return NaN
    return Number.parseFloat(text)
}
 
class Float implements ABISerializableObject {
    static abiName = '__float'
    static byteWidth: number
 
    static from<T extends typeof Float>(this: T, value: FloatType): InstanceType<T>
    static from(value: FloatType): unknown
    static from(value: FloatType) {
        Iif (isInstanceOf(value, this)) {
            return value
        }
        let nanWord: string | undefined
        if (typeof value === 'string') {
            const word = value.trim().toLowerCase()
            if (notANumber.has(word)) nanWord = word
            value = parseFloatText(value)
        } else Iif (isInstanceOf(value, Float)) {
            value = value.value
        }
        const float = new this(value)
        if (nanWord) {
            float.data = nanBytes(this.byteWidth, nanWord === '-nan')
        }
        return float
    }
 
    static fromABI<T extends typeof Float>(this: T, decoder: ABIDecoder): InstanceType<T>
    static fromABI(decoder: ABIDecoder): unknown
    static fromABI(decoder: ABIDecoder) {
        const bytes = decoder.readArray(this.byteWidth)
        const view = new DataView(bytes.buffer, bytes.byteOffset, this.byteWidth)
        const value = this.byteWidth === 4 ? view.getFloat32(0, true) : view.getFloat64(0, true)
        const float = new this(value)
        if (Number.isNaN(value)) {
            float.data = new Bytes(bytes.slice())
        }
        return float
    }
 
    static abiDefault() {
        return this.from(0)
    }
 
    static random<T extends typeof Float>(this: T): InstanceType<T>
    static random(): unknown
    static random() {
        const bytes = secureRandom(this.byteWidth)
        const decoder = new ABIDecoder(bytes)
        return this.fromABI(decoder)
    }
 
    value: number
 
    /** Bytes this value decoded from, kept so a NaN survives engines that canonicalize it. */
    data?: Bytes
 
    constructor(value: number) {
        this.value = value
    }
 
    equals(other: FloatType) {
        const self = this.constructor as typeof Float
        return this.value === self.from(other).value
    }
 
    toABI(encoder: ABIEncoder) {
        const self = this.constructor as typeof Float
        if (Number.isNaN(this.value)) {
            encoder.writeArray((this.data || nanBytes(self.byteWidth, true)).array)
        } else {
            encoder.writeFloat(this.value, self.byteWidth)
        }
    }
 
    toString() {
        const self = this.constructor as typeof Float
        if (Number.isNaN(this.value)) {
            // A retained negative NaN keeps its sign in text, as nodeos renders it.
            const bytes = (this.data || nanBytes(self.byteWidth, true)).array
            return bytes[bytes.length - 1] & 0x80 ? '-nan' : 'nan'
        }
        if (this.value === Infinity) return 'inf'
        if (this.value === -Infinity) return '-inf'
        // Number.prototype.toString drops the sign of negative zero, which nodeos renders.
        return Object.is(this.value, -0) ? '-0' : this.value.toString()
    }
 
    toJSON() {
        return this.toString()
    }
}
 
export type Float32Type = Float32 | FloatType
export class Float32 extends Float {
    static abiName = 'float32'
    static byteWidth = 4
}
 
export type Float64Type = Float64 | FloatType
export class Float64 extends Float {
    static abiName = 'float64'
    static byteWidth = 8
}
 
export type Float128Type = Float128 | BytesType
 
// Only the text nodeos emits (std::fixed precision 17 or an inf/nan word) leaves the hex path.
const nodeosDecimal = /^[+-]?(\d+\.\d{17}|inf(inity)?|nan)$/i
 
export class Float128 implements ABISerializableObject {
    static abiName = 'float128'
    static byteWidth = 16
 
    static from(value: Float128Type) {
        if (isInstanceOf(value, this)) {
            return value
        }
        if (typeof value === 'string') {
            if (value.startsWith('0x')) {
                value = value.slice(2)
            } else if (nodeosDecimal.test(value.trim())) {
                return this.fromDecimal(value)
            }
        }
        return new this(Bytes.from(value))
    }
 
    static fromABI(decoder: ABIDecoder) {
        return new this(new Bytes(decoder.readArray(this.byteWidth)))
    }
 
    /** Widen a double to its exact binary128 bit pattern. */
    static fromDouble(value: number) {
        const view = new DataView(new ArrayBuffer(8))
        view.setFloat64(0, value)
        const high = view.getUint32(0)
        const low = view.getUint32(4)
        const sign = BigInt(high >>> 31)
        const exponent = (high >>> 20) & 0x7ff
        let mantissa = ((BigInt(high) & 0xfffffn) << 32n) | BigInt(low >>> 0)
        let exponent128: bigint
        if (exponent === 0x7ff) {
            exponent128 = 0x7fffn
            mantissa <<= 60n
        } else if (exponent === 0 && mantissa === 0n) {
            exponent128 = 0n
        } else if (exponent === 0) {
            let shift = 0n
            while ((mantissa & (1n << 52n)) === 0n) {
                mantissa <<= 1n
                shift++
            }
            mantissa = (mantissa & ((1n << 52n) - 1n)) << 60n
            exponent128 = BigInt(1 - 1023 + 16383) - shift
        } else {
            exponent128 = BigInt(exponent - 1023 + 16383)
            mantissa <<= 60n
        }
        let bits = (sign << 127n) | (exponent128 << 112n) | mantissa
        const bytes = new Uint8Array(16)
        for (let i = 0; i < 16; i++) {
            bytes[i] = Number(bits & 0xffn)
            bits >>= 8n
        }
        return new this(Bytes.from(bytes))
    }
 
    /** Decode the decimal text nodeos renders a float128 `next_key` as. */
    static fromDecimal(text: string) {
        const word = text.trim().toLowerCase()
        const value = parseFloatText(word)
        if (Number.isNaN(value) && !notANumber.has(word)) {
            throw new Error(`Invalid float128 decimal: ${text}`)
        }
        const result = this.fromDouble(value)
        if (word === '-nan') {
            result.data.array[15] |= 0x80
        }
        return result
    }
 
    static random() {
        const bytes = secureRandom(16)
        const decoder = new ABIDecoder(bytes)
        return this.fromABI(decoder)
    }
 
    data: Bytes
 
    constructor(data: Bytes) {
        if (data.array.length !== 16) {
            throw new Error('Invalid float128')
        }
        this.data = data
    }
 
    equals(other: Float128Type) {
        const self = this.constructor as typeof Float128
        return this.data.equals(self.from(other).data)
    }
 
    toABI(encoder: ABIEncoder) {
        encoder.writeArray(this.data.array)
    }
 
    toString() {
        // float128 uses 0x prefixed hex strings as opposed to everywhere else in where there is no prefix ¯\_(ツ)_/¯
        return '0x' + this.data.hexString
    }
 
    /** Spelling a `get_table_rows` bound needs: nodeos reads it as a big-endian integer. */
    toBoundHex() {
        return '0x' + new Bytes(this.data.array.slice().reverse()).hexString
    }
 
    toJSON() {
        return this.toString()
    }
}