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