time-to-botec

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

cos.js (3253B)


      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, license, and long comment were part of the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/9.3.0/lib/msun/src/s_cos.c}. 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 kernelCos = require( './../../../../base/special/kernel-cos' );
     39 var kernelSin = require( './../../../../base/special/kernel-sin' );
     40 var rempio2 = require( './../../../../base/special/rempio2' );
     41 
     42 
     43 // VARIABLES //
     44 
     45 // Scratch array for storing temporary values:
     46 var buffer = [ 0.0, 0.0 ]; // WARNING: not thread safe
     47 
     48 // High word absolute value mask: 0x7fffffff => 01111111111111111111111111111111
     49 var HIGH_WORD_ABS_MASK = 0x7fffffff|0; // asm type annotation
     50 
     51 // High word of π/4: 0x3fe921fb => 00111111111010010010000111111011
     52 var HIGH_WORD_PIO4 = 0x3fe921fb|0; // asm type annotation
     53 
     54 // High word of 2^-27: 0x3e400000 => 00111110010000000000000000000000
     55 var HIGH_WORD_TWO_NEG_27 = 0x3e400000|0; // asm type annotation
     56 
     57 // High word exponent mask: 0x7ff00000 => 01111111111100000000000000000000
     58 var HIGH_WORD_EXPONENT_MASK = 0x7ff00000|0; // asm type annotation
     59 
     60 
     61 // MAIN //
     62 
     63 /**
     64 * Computes the cosine of a number.
     65 *
     66 * @param {number} x - input value (in radians)
     67 * @returns {number} cosine
     68 *
     69 * @example
     70 * var v = cos( 0.0 );
     71 * // returns 1.0
     72 *
     73 * @example
     74 * var v = cos( 3.141592653589793/4.0 );
     75 * // returns ~0.707
     76 *
     77 * @example
     78 * var v = cos( -3.141592653589793/6.0 );
     79 * // returns ~0.866
     80 *
     81 * @example
     82 * var v = cos( NaN );
     83 * // returns NaN
     84 */
     85 function cos( x ) {
     86 	var ix;
     87 	var n;
     88 
     89 	ix = getHighWord( x );
     90 	ix &= HIGH_WORD_ABS_MASK;
     91 
     92 	// Case: |x| ~< pi/4
     93 	if ( ix <= HIGH_WORD_PIO4 ) {
     94 		// Case: x < 2**-27
     95 		if ( ix < HIGH_WORD_TWO_NEG_27 ) {
     96 			return 1.0;
     97 		}
     98 		return kernelCos( x, 0.0 );
     99 	}
    100 	// Case: cos(Inf or NaN) is NaN */
    101 	if ( ix >= HIGH_WORD_EXPONENT_MASK ) {
    102 		return NaN;
    103 	}
    104 	// Case: Argument reduction needed...
    105 	n = rempio2( x, buffer );
    106 	switch ( n & 3 ) {
    107 	case 0:
    108 		return kernelCos( buffer[ 0 ], buffer[ 1 ] );
    109 	case 1:
    110 		return -kernelSin( buffer[ 0 ], buffer[ 1 ] );
    111 	case 2:
    112 		return -kernelCos( buffer[ 0 ], buffer[ 1 ] );
    113 	default:
    114 		return kernelSin( buffer[ 0 ], buffer[ 1 ] );
    115 	}
    116 }
    117 
    118 
    119 // EXPORTS //
    120 
    121 module.exports = cos;