main.js (3157B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var Uint32Array = require( '@stdlib/array/uint32' ); 24 var Float64Array = require( '@stdlib/array/float64' ); 25 var indices = require( './indices.js' ); 26 27 28 // VARIABLES // 29 30 var FLOAT64_VIEW = new Float64Array( 1 ); 31 var UINT32_VIEW = new Uint32Array( FLOAT64_VIEW.buffer ); 32 33 var HIGH = indices.HIGH; 34 var LOW = indices.LOW; 35 36 37 // MAIN // 38 39 /** 40 * Creates a double-precision floating-point number from a higher order word (unsigned 32-bit integer) and a lower order word (unsigned 32-bit integer). 41 * 42 * ## Notes 43 * 44 * ```text 45 * float64 (64 bits) 46 * f := fraction (significand/mantissa) (52 bits) 47 * e := exponent (11 bits) 48 * s := sign bit (1 bit) 49 * 50 * |-------- -------- -------- -------- -------- -------- -------- --------| 51 * | Float64 | 52 * |-------- -------- -------- -------- -------- -------- -------- --------| 53 * | Uint32 | Uint32 | 54 * |-------- -------- -------- -------- -------- -------- -------- --------| 55 * ``` 56 * 57 * If little endian (more significant bits last): 58 * 59 * ```text 60 * <-- lower higher --> 61 * | f7 f6 f5 f4 f3 f2 e2 | f1 |s| e1 | 62 * ``` 63 * 64 * If big endian (more significant bits first): 65 * 66 * ```text 67 * <-- higher lower --> 68 * |s| e1 e2 | f1 f2 f3 f4 f5 f6 f7 | 69 * ``` 70 * 71 * 72 * In which Uint32 should we place the higher order bits? If little endian, the second; if big endian, the first. 73 * 74 * 75 * ## References 76 * 77 * - [Open Group][1] 78 * 79 * [1]: http://pubs.opengroup.org/onlinepubs/9629399/chap14.htm 80 * 81 * @param {uinteger32} high - higher order word (unsigned 32-bit integer) 82 * @param {uinteger32} low - lower order word (unsigned 32-bit integer) 83 * @returns {number} floating-point number 84 * 85 * @example 86 * var v = fromWords( 1774486211, 2479577218 ); 87 * // returns 3.14e201 88 * 89 * @example 90 * var v = fromWords( 3221823995, 1413754136 ); 91 * // returns -3.141592653589793 92 * 93 * @example 94 * var v = fromWords( 0, 0 ); 95 * // returns 0.0 96 * 97 * @example 98 * var v = fromWords( 2147483648, 0 ); 99 * // returns -0.0 100 * 101 * @example 102 * var v = fromWords( 2146959360, 0 ); 103 * // returns NaN 104 * 105 * @example 106 * var v = fromWords( 2146435072, 0 ); 107 * // returns Infinity 108 * 109 * @example 110 * var v = fromWords( 4293918720, 0 ); 111 * // returns -Infinity 112 */ 113 function fromWords( high, low ) { 114 UINT32_VIEW[ HIGH ] = high; 115 UINT32_VIEW[ LOW ] = low; 116 return FLOAT64_VIEW[ 0 ]; 117 } 118 119 120 // EXPORTS // 121 122 module.exports = fromWords;