d48e09c0772143d0922b8dfaa9b09673e258a198
[clnl] / src / main / strictmath / ksin.lisp
1 (in-package #:strictmath)
2 ; This file is taken from part of Evita Common Lisp.
3 ;
4 ; Copyright (C) 1996-2007 by Project Vogue.
5 ; Written by Yoshifumi "VOGUE" INOUE. (yosi@msn.com)
6 ;
7 ; Before that, it was based off of fdlibm
8 ;
9 ;  See fdlibm (http://www.netlib.org/fdlibm/)
10 ;  See http://sources.redhat.com/newlib/
11
12 ; /* @(#)k_sin.c 5.1 93/09/24 */
13 ; /*
14 ;  * ====================================================
15 ;  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
16 ;  *
17 ;  * Developed at SunPro, a Sun Microsystems, Inc. business.
18 ;  * Permission to use, copy, modify, and distribute this
19 ;  * software is freely granted, provided that this notice 
20 ;  * is preserved.
21 ;  * ====================================================
22 ;  */
23
24 ; /* __kernel_sin( x, y, iy)
25 ;  * kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
26 ;  * Input x is assumed to be bounded by ~pi/4 in magnitude.
27 ;  * Input y is the tail of x.
28 ;  * Input iy indicates whether y is 0. (if iy=0, y assume to be 0). 
29 ;  *
30 ;  * Algorithm
31 ;  *      1. Since sin(-x) = -sin(x), we need only to consider positive x. 
32 ;  *      2. if x < 2^-27 (hx<#x3e400000 0), return x with inexact if x!=0.
33 ;  *      3. sin(x) is approximated by a polynomial of degree 13 on
34 ;  *         [0,pi/4]
35 ;  *                                 3            13
36 ;  *                 sin(x) ~ x + S1*x + ... + S6*x
37 ;  *         where
38 ;  *      
39 ;  *      |sin(x)         2     4     6     8     10     12  |     -58
40 ;  *      |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x  +S6*x   )| <= 2
41 ;  *      |  x                                                    | 
42 ;  * 
43 ;  *      4. sin(x+y) = sin(x) + sin'(x')*y
44 ;  *                  ~ sin(x) + (1-x*x/2)*y
45 ;  *         For better accuracy, let 
46 ;  *                   3      2      2      2      2
47 ;  *              r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
48 ;  *         then                   3    2
49 ;  *              sin(x) = x + (S1*x + (x *(r-y/2)+y))
50 ;  */
51
52 (defun float64-kernel-sin (x y iy)
53  (declare (values double-float))
54  (declare (type double-float x y))
55  (declare (type fixnum iy))
56  ;; iy=0 if y is zero
57  (prog*
58   ((half #+nil 5.00000000000000000000e-01 #.(encode-float64 #x3FE00000 #x00000000))
59    (S1 #+nil -1.66666666666666324348e-01 #.(encode-float64 #xBFC55555 #x55555549))
60    (S2 #+nil 8.33333333332248946124e-03 #.(encode-float64 #x3F811111 #x1110F8A6))
61    (S3 #+nil -1.98412698298579493134e-04 #.(encode-float64 #xBF2A01A0 #x19C161D5))
62    (S4 #+nil 2.75573137070700676789e-06 #.(encode-float64 #x3EC71DE3 #x57B1FE7D))
63    (S5 #+nil -2.50507602534068634195e-08 #.(encode-float64 #xBE5AE5E6 #x8A2B9CEB))
64    (S6 #+nil 1.58969099521155010221e-10 #.(encode-float64 #x3DE5D93A #x5ACFD57C))
65    ;;
66    (hx (decode-float64 x)) ;  high word of x
67    (ix (logand hx #x7fffffff)))
68
69   ;; |x| < 2**-27 
70   (when (< ix #x3e400000)
71    ;; generate inexact
72    (when (eql (truncate x) 0) (return x)))
73   (let* ((z (* x x))
74          (v (* z x))
75          (r (+ s2 (* z (+ s3 (* z (+ s4 (* z (+ s5 (* z s6))))))))))
76    (if (eql iy 0)
77     (return (+ x (* v (+ s1 (* z r)))))
78     (return (- x (- (- (* z (- (* half y) (* v r))) y) (* v s1))))))))