time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (2174B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2021 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 IS_LITTLE_ENDIAN = require( '@stdlib/assert/is-little-endian' );
     24 var Uint8Array = require( '@stdlib/array/uint8' );
     25 var DataView = require( '@stdlib/array/dataview' );
     26 var floor = require( '@stdlib/math/base/special/floor' );
     27 
     28 
     29 // VARIABLES //
     30 
     31 // 0xFFFFFFFF = 2**32 - 1 => 11111111 11111111 11111111 11111111
     32 var LOW_MASK = 0xFFFFFFFF >>> 0;
     33 
     34 // 2**32
     35 var TWO_32 = 4294967296;
     36 
     37 
     38 // MAIN //
     39 
     40 /**
     41 * Converts an integer-valued double-precision floating-point number to a signed 64-bit integer byte array according to host byte order (endianness).
     42 *
     43 * ## Notes
     44 *
     45 * -   This function assumes that the input value is less than the maximum safe double-precision floating-point integer plus one (i.e., `2**53`).
     46 *
     47 * @param {number} x - input value
     48 * @returns {Uint8Array} byte array
     49 *
     50 * @example
     51 * var bytes = float64ToInt64Bytes( 1.0 );
     52 * // returns <Uint8Array>
     53 */
     54 function float64ToInt64Bytes( x ) {
     55 	var bytes;
     56 	var view;
     57 	var hi;
     58 	var lo;
     59 
     60 	bytes = new Uint8Array( 8 );
     61 	if ( x === 0 ) {
     62 		return bytes;
     63 	}
     64 	// Get the low 32-bit word:
     65 	lo = (x&LOW_MASK)>>>0;
     66 
     67 	// Get the high 32-bit word:
     68 	hi = floor( x/TWO_32 );
     69 
     70 	// Insert the high and low words according to host byte order (endianness):
     71 	view = new DataView( bytes.buffer );
     72 	if ( IS_LITTLE_ENDIAN ) {
     73 		view.setUint32( 0, lo, IS_LITTLE_ENDIAN );
     74 		view.setUint32( 4, hi, IS_LITTLE_ENDIAN );
     75 	} else {
     76 		view.setUint32( 0, hi, IS_LITTLE_ENDIAN );
     77 		view.setUint32( 4, lo, IS_LITTLE_ENDIAN );
     78 	}
     79 	return bytes;
     80 }
     81 
     82 
     83 // EXPORTS //
     84 
     85 module.exports = float64ToInt64Bytes;