UNPKG

math-float64-to-words

Version:

Splits a floating-point number into a higher order word and a lower order word.

63 lines (47 loc) 1.74 kB
'use strict'; // MODULES // var indices = require( './indices.js' ); // NOTES // /** * float64 (64 bits) * f := fraction (significand/mantissa) (52 bits) * e := exponent (11 bits) * s := sign bit (1 bit) * * |-------- -------- -------- -------- -------- -------- -------- --------| * | Float64 | * |-------- -------- -------- -------- -------- -------- -------- --------| * | Uint32 | Uint32 | * |-------- -------- -------- -------- -------- -------- -------- --------| * * If little endian (more significant bits last): * <-- lower higher --> * | f7 f6 f5 f4 f3 f2 e2 | f1 |s| e1 | * * If big endian (more significant bits first): * <-- higher lower --> * |s| e1 e2 | f1 f2 f3 f4 f5 f6 f7 | * * * Note: in which Uint32 can we find the higher order bits? If LE, the second; if BE, the first. * Refs: http://pubs.opengroup.org/onlinepubs/9629399/chap14.htm */ // VARIABLES // var FLOAT64_VIEW = new Float64Array( 1 ); var UINT32_VIEW = new Uint32Array( FLOAT64_VIEW.buffer ); var HIGH = indices.HIGH; var LOW = indices.LOW; // WORDS // /** * FUNCTION: words( x ) * Splits a floating-point number into a higher order word (32-bit integer) and a lower order word (32-bit integer). * * @param {Number} x - input value * @returns {Number[]} two-element array containing a higher order word and a lower order word */ function words( x ) { FLOAT64_VIEW[ 0 ] = x; return [ UINT32_VIEW[ HIGH ], UINT32_VIEW[ LOW ] ]; } // end FUNCTION words() // EXPORTS // module.exports = words;