001: /*
002: *
003: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025:
026: package com.sun.pisces;
027:
028: public class PiscesMath {
029:
030: private PiscesMath() {
031: }
032:
033: private static final int SINTAB_LG_ENTRIES = 10;
034: private static final int SINTAB_ENTRIES = 1 << SINTAB_LG_ENTRIES;
035: private static int[] sintab;
036:
037: public static final int PI = (int) (Math.PI * 65536.0);
038: public static final int TWO_PI = (int) (2.0 * Math.PI * 65536.0);
039: public static final int PI_OVER_TWO = (int) ((Math.PI / 2.0) * 65536.0);
040: public static final int SQRT_TWO = (int) (Math.sqrt(2.0) * 65536.0);
041:
042: static {
043: sintab = new int[SINTAB_ENTRIES + 1];
044: for (int i = 0; i < SINTAB_ENTRIES + 1; i++) {
045: double theta = i * (Math.PI / 2.0) / SINTAB_ENTRIES;
046: sintab[i] = (int) (Math.sin(theta) * 65536.0);
047: }
048: }
049:
050: public static int sin(int theta) {
051: int sign = 1;
052: if (theta < 0) {
053: theta = -theta;
054: sign = -1;
055: }
056: // 0 <= theta
057: while (theta >= TWO_PI) {
058: theta -= TWO_PI;
059: }
060: // 0 <= theta < 2*PI
061: if (theta >= PI) {
062: theta = TWO_PI - theta;
063: sign = -sign;
064: }
065: // 0 <= theta < PI
066: if (theta > PI_OVER_TWO) {
067: theta = PI - theta;
068: }
069: // 0 <= theta <= PI/2
070: int itheta = (int) ((long) theta * SINTAB_ENTRIES / (PI_OVER_TWO));
071: return sign * sintab[itheta];
072: }
073:
074: public static int cos(int theta) {
075: return sin(PI_OVER_TWO - theta);
076: }
077:
078: // public static double sqrt(double x) {
079: // double dsqrt = Math.sqrt(x);
080: // int ix = (int)(x*65536.0);
081: // Int Isqrt = Isqrt(Ix);
082:
083: // Long Lx = (Long)(X*65536.0);
084: // Long Lsqrt = Lsqrt(Lx);
085:
086: // System.Out.Println();
087: // System.Out.Println("X = " + X);
088: // System.Out.Println("Dsqrt = " + Dsqrt);
089:
090: // System.Out.Println("Ix = " + Ix);
091: // System.Out.Println("Isqrt = " + Isqrt/65536.0);
092:
093: // System.Out.Println("Lx = " + Lx);
094: // System.Out.Println("Lsqrt = " + Lsqrt/65536.0);
095:
096: // Return Dsqrt;
097: // }
098:
099: // From Ken Turkowski, _Fixed-Point Square Root_, In Graphics Gems V
100: public static int isqrt(int x) {
101: int fracbits = 16;
102:
103: int root = 0;
104: int remHi = 0;
105: int remLo = x;
106: int count = 15 + fracbits / 2;
107:
108: do {
109: remHi = (remHi << 2) | (remLo >>> 30); // N.B. - unsigned shift R
110: remLo <<= 2;
111: root <<= 1;
112: int testdiv = (root << 1) + 1;
113: if (remHi >= testdiv) {
114: remHi -= testdiv;
115: root++;
116: }
117: } while (count-- != 0);
118:
119: return root;
120: }
121:
122: public static long lsqrt(long x) {
123: int fracbits = 16;
124:
125: long root = 0;
126: long remHi = 0;
127: long remLo = x;
128: int count = 31 + fracbits / 2;
129:
130: do {
131: remHi = (remHi << 2) | (remLo >>> 62); // N.B. - unsigned shift R
132: remLo <<= 2;
133: root <<= 1;
134: long testDiv = (root << 1) + 1;
135: if (remHi >= testDiv) {
136: remHi -= testDiv;
137: root++;
138: }
139: } while (count-- != 0);
140:
141: return root;
142: }
143:
144: public static double hypot(double x, double y) {
145: // new RuntimeException().printStackTrace();
146: return Math.sqrt(x * x + y * y);
147: }
148:
149: public static int hypot(int x, int y) {
150: return (int) ((lsqrt((long) x * x + (long) y * y) + 128) >> 8);
151: }
152:
153: public static long hypot(long x, long y) {
154: return (lsqrt(x * x + y * y) + 128) >> 8;
155: }
156: }
|