time-to-botec

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

kernel_sincos.js (3061B)


      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 [k_sin.c]{@link https://svnweb.freebsd.org/base/release/9.3.0/lib/msun/src/k_sin.c} and [k_cos.c]{@link https://svnweb.freebsd.org/base/release/9.3.0/lib/msun/src/k_cos.c}. The implementation follows the original sine and cosine kernels, but has been modified for JavaScript and combined into a single function.
     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 // VARIABLES //
     36 
     37 var S1 = -1.66666666666666324348e-01; // 0xBFC55555, 0x55555549
     38 var S2 = 8.33333333332248946124e-03;  // 0x3F811111, 0x1110F8A6
     39 var S3 = -1.98412698298579493134e-04; // 0xBF2A01A0, 0x19C161D5
     40 var S4 = 2.75573137070700676789e-06;  // 0x3EC71DE3, 0x57B1FE7D
     41 var S5 = -2.50507602534068634195e-08; // 0xBE5AE5E6, 0x8A2B9CEB
     42 var S6 = 1.58969099521155010221e-10;  // 0x3DE5D93A, 0x5ACFD57C
     43 
     44 var C1 = 4.16666666666666019037e-02;  // 0x3FA55555, 0x5555554C
     45 var C2 = -1.38888888888741095749e-03; // 0xBF56C16C, 0x16C15177
     46 var C3 = 2.48015872894767294178e-05;  // 0x3EFA01A0, 0x19CB1590
     47 var C4 = -2.75573143513906633035e-07; // 0xBE927E4F, 0x809C52AD
     48 var C5 = 2.08757232129817482790e-09;  // 0x3E21EE9E, 0xBDB4B1C4
     49 var C6 = -1.13596475577881948265e-11; // 0xBDA8FAE9, 0xBE8838D4
     50 
     51 
     52 // MAIN //
     53 
     54 /**
     55 * Computes the sine and cosine on \\( \approx \[-\pi/4, \pi/4\] \\) (except for \\(-0\\)), where \\( \pi/4 \approx 0.7854 \\).
     56 *
     57 * @private
     58 * @param {(Array|TypedArray|Object)} out - destination array
     59 * @param {number} x - input value (in radians, assumed to be bounded by `~π/4` in magnitude)
     60 * @param {number} y - tail of `x`
     61 * @returns {(Array|TypedArray|Object)} sine and cosine
     62 */
     63 function kernelSincos( out, x, y ) {
     64 	var hz;
     65 	var r;
     66 	var v;
     67 	var w;
     68 	var z;
     69 
     70 	z = x * x;
     71 	w = z * z;
     72 	r = S2 + (z * (S3 + (z*S4))) + (z * w * (S5 + (z*S6)));
     73 	v = z * x;
     74 	if ( y === 0.0 ) {
     75 		out[ 0 ] = x + (v * (S1 + (z*r)));
     76 	} else {
     77 		out[ 0 ] = x - (((z*((0.5*y) - (v*r))) - y) - (v*S1));
     78 	}
     79 	r = z * (C1 + (z * (C2 + (z*C3))));
     80 	r += w * w * (C4 + (z * (C5 + (z*C6))));
     81 	hz = 0.5 * z;
     82 	w = 1.0 - hz;
     83 	out[ 1 ] = w + ( ((1.0-w) - hz) + ((z*r) - (x*y)) );
     84 
     85 	return out;
     86 }
     87 
     88 
     89 // EXPORTS //
     90 
     91 module.exports = kernelSincos;