time-to-botec

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

klog.js (2424B)


      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 * ## Notice
     20 *
     21 * The following copyright and license were part of the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/9.3.0/lib/msun/src/k_log.h}. The implementation follows the original, but has been modified for JavaScript.
     22 *
     23 * ```text
     24 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
     25 *
     26 * Developed at SunPro, a Sun Microsystems, Inc. business.
     27 * Permission to use, copy, modify, and distribute this
     28 * software is freely granted, provided that this notice
     29 * is preserved.
     30 * ```
     31 */
     32 
     33 'use strict';
     34 
     35 // MODULES //
     36 
     37 var getHighWord = require( '@stdlib/number/float64/base/get-high-word' );
     38 var polyvalP = require( './polyval_p.js' );
     39 var polyvalQ = require( './polyval_q.js' );
     40 
     41 
     42 // VARIABLES //
     43 
     44 // 0x000fffff = 1048575 => 0 00000000000 11111111111111111111
     45 var HIGH_SIGNIFICAND_MASK = 0x000fffff|0; // asm type annotation
     46 
     47 // 1/3
     48 var ONE_THIRD = 0.33333333333333333;
     49 
     50 
     51 // MAIN //
     52 
     53 /**
     54 * Return `log(x) - (x-1)` for `x` in `~[sqrt(2)/2, sqrt(2)]`.
     55 *
     56 * @private
     57 * @param {number} x - input value
     58 * @returns {number} function value
     59 */
     60 function klog( x ) {
     61 	var hfsq;
     62 	var t1;
     63 	var t2;
     64 	var hx;
     65 	var f;
     66 	var s;
     67 	var z;
     68 	var R;
     69 	var w;
     70 	var i;
     71 	var j;
     72 
     73 	hx = getHighWord( x );
     74 	f = x - 1.0;
     75 	if ( ( HIGH_SIGNIFICAND_MASK & (2+hx) ) < 3 ) {
     76 		// Case: -2**-20 <= f < 2**-20
     77 		if ( f === 0.0 ) {
     78 			return 0.0;
     79 		}
     80 		return f * f * ( ( ONE_THIRD*f )- 0.5 );
     81 	}
     82 	s = f / ( 2.0 + f );
     83 	z = s * s;
     84 	hx &= HIGH_SIGNIFICAND_MASK;
     85 	i = ( hx - 0x6147a )|0; // asm type annotation
     86 	w = z * z;
     87 	j = ( 0x6b851 - hx )|0; // asm type annotation
     88 	t1 = w * polyvalP( w );
     89 	t2 = z * polyvalQ( w );
     90 	i |= j;
     91 	R = t2 + t1;
     92 	if ( i > 0 ) {
     93 		hfsq = 0.5 * f * f;
     94 		return ( s * (hfsq+R) ) - hfsq;
     95 	}
     96 	return s * (R-f);
     97 }
     98 
     99 
    100 // EXPORTS //
    101 
    102 module.exports = klog;