time-to-botec

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

assign.js (2644B)


      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 // Byte array workspace:
     38 var BYTES = new Uint8Array( 8 );
     39 var VIEW = new DataView( BYTES.buffer );
     40 
     41 
     42 // MAIN //
     43 
     44 /**
     45 * Converts an integer-valued double-precision floating-point number to a signed 64-bit integer byte array according to host byte order (endianness).
     46 *
     47 * ## Notes
     48 *
     49 * -   This function assumes that the input value is less than the maximum safe double-precision floating-point integer plus one (i.e., `2**53`).
     50 *
     51 * @param {number} x - input value
     52 * @param {Collection} out - output array
     53 * @param {integer} stride - output array stride
     54 * @param {NonNegativeInteger} offset - output array index offset
     55 * @returns {Collection} output array
     56 *
     57 * @example
     58 * var Uint8Array = require( '@stdlib/array/uint8' );
     59 *
     60 * var out = new Uint8Array( 8 );
     61 * var bytes = float64ToInt64Bytes( 1.0, out, 1, 0 );
     62 * // returns <Uint8Array>
     63 */
     64 function float64ToInt64Bytes( x, out, stride, offset ) {
     65 	var hi;
     66 	var lo;
     67 	var i;
     68 
     69 	if ( x === 0 ) {
     70 		for ( i = 0; i < BYTES.length; i++ ) {
     71 			out[ offset ] = 0;
     72 			offset += stride;
     73 		}
     74 		return out;
     75 	}
     76 	// Get the low 32-bit word:
     77 	lo = (x&LOW_MASK)>>>0;
     78 
     79 	// Get the high 32-bit word:
     80 	hi = floor( x/TWO_32 );
     81 
     82 	// Insert the high and low words according to host byte order (endianness):
     83 	if ( IS_LITTLE_ENDIAN ) {
     84 		VIEW.setUint32( 0, lo, IS_LITTLE_ENDIAN );
     85 		VIEW.setUint32( 4, hi, IS_LITTLE_ENDIAN );
     86 	} else {
     87 		VIEW.setUint32( 0, hi, IS_LITTLE_ENDIAN );
     88 		VIEW.setUint32( 4, lo, IS_LITTLE_ENDIAN );
     89 	}
     90 	for ( i = 0; i < BYTES.length; i++ ) {
     91 		out[ offset ] = BYTES[ i ];
     92 		offset += stride;
     93 	}
     94 	return out;
     95 }
     96 
     97 
     98 // EXPORTS //
     99 
    100 module.exports = float64ToInt64Bytes;