main.js (3194B)
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 HIGH = require( './high.js' ); 26 27 28 // VARIABLES // 29 30 var FLOAT64_VIEW = new Float64Array( 1 ); 31 var UINT32_VIEW = new Uint32Array( FLOAT64_VIEW.buffer ); 32 33 34 // MAIN // 35 36 /** 37 * Sets the more significant 32 bits of a double-precision floating-point number. 38 * 39 * ## Notes 40 * 41 * ```text 42 * float64 (64 bits) 43 * f := fraction (significand/mantissa) (52 bits) 44 * e := exponent (11 bits) 45 * s := sign bit (1 bit) 46 * 47 * |-------- -------- -------- -------- -------- -------- -------- --------| 48 * | Float64 | 49 * |-------- -------- -------- -------- -------- -------- -------- --------| 50 * | Uint32 | Uint32 | 51 * |-------- -------- -------- -------- -------- -------- -------- --------| 52 * ``` 53 * 54 * If little endian (more significant bits last): 55 * 56 * ```text 57 * <-- lower higher --> 58 * | f7 f6 f5 f4 f3 f2 e2 | f1 |s| e1 | 59 * ``` 60 * 61 * If big endian (more significant bits first): 62 * 63 * ```text 64 * <-- higher lower --> 65 * |s| e1 e2 | f1 f2 f3 f4 f5 f6 f7 | 66 * ``` 67 * 68 * In which Uint32 can we find the higher order bits? If little endian, the second; if big endian, the first. 69 * 70 * 71 * ## References 72 * 73 * - [Open Group][1] 74 * 75 * [1]: http://pubs.opengroup.org/onlinepubs/9629399/chap14.htm 76 * 77 * @param {number} x - double 78 * @param {uinteger32} high - unsigned 32-bit integer to replace the higher order word of `x` 79 * @returns {number} double having the same lower order word as `x` 80 * 81 * @example 82 * var high = 5 >>> 0; // => 0 00000000000 00000000000000000101 83 * 84 * var y = setHighWord( 3.14e201, high ); // => 0 00000000000 0000000000000000010110010011110010110101100010000010 85 * // returns 1.18350528745e-313 86 * 87 * @example 88 * var PINF = require( '@stdlib/constants/float64/pinf' ); // => 0 11111111111 00000000000000000000 00000000000000000000000000000000 89 * 90 * var high = 1072693248 >>> 0; // => 0 01111111111 00000000000000000000 91 * 92 * // Set the higher order bits of `+infinity` to return `1`: 93 * var y = setHighWord( PINF, high ); // => 0 01111111111 0000000000000000000000000000000000000000000000000000 94 * // returns 1.0 95 */ 96 function setHighWord( x, high ) { 97 FLOAT64_VIEW[ 0 ] = x; 98 UINT32_VIEW[ HIGH ] = ( high >>> 0 ); // identity bit shift to ensure integer 99 return FLOAT64_VIEW[ 0 ]; 100 } 101 102 103 // EXPORTS // 104 105 module.exports = setHighWord;