blob: f61a477ec333e332fda018d855d7fafe4ed6602a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.java2d.pisces;
27
28public class PiscesMath {
29
30 private PiscesMath() {}
31
32 private static final int SINTAB_LG_ENTRIES = 10;
33 private static final int SINTAB_ENTRIES = 1 << SINTAB_LG_ENTRIES;
34 private static int[] sintab;
35
36 public static final int PI = (int)(Math.PI*65536.0);
37 public static final int TWO_PI = (int)(2.0*Math.PI*65536.0);
38 public static final int PI_OVER_TWO = (int)((Math.PI/2.0)*65536.0);
39 public static final int SQRT_TWO = (int)(Math.sqrt(2.0)*65536.0);
40
41 static {
42 sintab = new int[SINTAB_ENTRIES + 1];
43 for (int i = 0; i < SINTAB_ENTRIES + 1; i++) {
44 double theta = i*(Math.PI/2.0)/SINTAB_ENTRIES;
45 sintab[i] = (int)(Math.sin(theta)*65536.0);
46 }
47 }
48
49 public static int sin(int theta) {
50 int sign = 1;
51 if (theta < 0) {
52 theta = -theta;
53 sign = -1;
54 }
55 // 0 <= theta
56 while (theta >= TWO_PI) {
57 theta -= TWO_PI;
58 }
59 // 0 <= theta < 2*PI
60 if (theta >= PI) {
61 theta = TWO_PI - theta;
62 sign = -sign;
63 }
64 // 0 <= theta < PI
65 if (theta > PI_OVER_TWO) {
66 theta = PI - theta;
67 }
68 // 0 <= theta <= PI/2
69 int itheta = (int)((long)theta*SINTAB_ENTRIES/(PI_OVER_TWO));
70 return sign*sintab[itheta];
71 }
72
73 public static int cos(int theta) {
74 return sin(PI_OVER_TWO - theta);
75 }
76
77// public static double sqrt(double x) {
78// double dsqrt = Math.sqrt(x);
79// int ix = (int)(x*65536.0);
80// Int Isqrt = Isqrt(Ix);
81
82// Long Lx = (Long)(X*65536.0);
83// Long Lsqrt = Lsqrt(Lx);
84
85// System.Out.Println();
86// System.Out.Println("X = " + X);
87// System.Out.Println("Dsqrt = " + Dsqrt);
88
89// System.Out.Println("Ix = " + Ix);
90// System.Out.Println("Isqrt = " + Isqrt/65536.0);
91
92// System.Out.Println("Lx = " + Lx);
93// System.Out.Println("Lsqrt = " + Lsqrt/65536.0);
94
95// Return Dsqrt;
96// }
97
98 // From Ken Turkowski, _Fixed-Point Square Root_, In Graphics Gems V
99 public static int isqrt(int x) {
100 int fracbits = 16;
101
102 int root = 0;
103 int remHi = 0;
104 int remLo = x;
105 int count = 15 + fracbits/2;
106
107 do {
108 remHi = (remHi << 2) | (remLo >>> 30); // N.B. - unsigned shift R
109 remLo <<= 2;
110 root <<= 1;
111 int testdiv = (root << 1) + 1;
112 if (remHi >= testdiv) {
113 remHi -= testdiv;
114 root++;
115 }
116 } while (count-- != 0);
117
118 return root;
119 }
120
121 public static long lsqrt(long x) {
122 int fracbits = 16;
123
124 long root = 0;
125 long remHi = 0;
126 long remLo = x;
127 int count = 31 + fracbits/2;
128
129 do {
130 remHi = (remHi << 2) | (remLo >>> 62); // N.B. - unsigned shift R
131 remLo <<= 2;
132 root <<= 1;
133 long testDiv = (root << 1) + 1;
134 if (remHi >= testDiv) {
135 remHi -= testDiv;
136 root++;
137 }
138 } while (count-- != 0);
139
140 return root;
141 }
142
143 public static double hypot(double x, double y) {
144 // new RuntimeException().printStackTrace();
145 return Math.sqrt(x*x + y*y);
146 }
147
148 public static int hypot(int x, int y) {
149 return (int)((lsqrt((long)x*x + (long)y*y) + 128) >> 8);
150 }
151
152 public static long hypot(long x, long y) {
153 return (lsqrt(x*x + y*y) + 128) >> 8;
154 }
155}