From a8a020c53ed51496e421d4814f125fc4473f3fb5 Mon Sep 17 00:00:00 2001 From: francisli Date: Thu, 24 Mar 2005 01:50:05 +0000 Subject: [PATCH] Some fixed point and trigonometry implementation --- core/processing/core/PMIDlet.java | 411 ++++++++++++++++++++++++++++++ 1 file changed, 411 insertions(+) diff --git a/core/processing/core/PMIDlet.java b/core/processing/core/PMIDlet.java index 20ac661f8..681b66f60 100755 --- a/core/processing/core/PMIDlet.java +++ b/core/processing/core/PMIDlet.java @@ -383,4 +383,415 @@ public abstract class PMIDlet extends MIDlet implements Runnable { public final void println(String data) { System.out.println(data); } + + //// Experimental fixed point math routines here + + /** Precision, in number of bits for the fractional part. */ + public static final int FP_PRECISION = 8; + /** Convenience constant of the value 1 in fixed point. */ + public static final int ONE = 1 << FP_PRECISION; + /** Convenience constant of the value of pi in fixed point. */ + public static final int PI = (int) ((3.14159265358979323846f) * ONE); + /** Convenience constant of the value of 2*pi in fixed point. */ + public static final int TWO_PI = 2 * PI; + /** Convenience constant of the value of pi/2 in fixed point. */ + public static final int HALF_PI = PI / 2; + + /** Multiplies two fixed point values and returns a fixed point value. */ + public static final int mul(int value1, int value2) { + return (value1 * value2) >> FP_PRECISION; + } + + /** Returns the fixed point quotient from dividing the fixed point dividend by the fixed point divisor. */ + public static final int div(int dividend, int divisor) { + return (dividend << FP_PRECISION) / divisor; + } + + /** Returns the fixed point representation of the specified integer value. */ + public static final int itofp(int value1) { + return value1 << FP_PRECISION; + } + + /** Returns the closest integer value less than or equal to the specified fixed point value. */ + public static final int floor(int value1) { + return value1 >> FP_PRECISION; + } + + /** Returns the sin of the specified fixed-point radian angle as a fixed point value. */ + public static final int sin(int rad) { + //// convert to degrees + int index = rad * 180 / PI % 360; + return sin[index]; + } + + /** Returns the cos of the specified fixed-point radian angle as a fixed point value. */ + public static final int cos(int rad) { + //// convert to degrees + int index = (rad * 180 / PI + 90) % 360; + return sin[index]; + } + + /** Lookup table for sin function, indexed by degrees. */ + private static final int[] sin = { + (int) (0f * ONE), + (int) (0.0174524064372835f * ONE), + (int) (0.034899496702501f * ONE), + (int) (0.0523359562429438f * ONE), + (int) (0.0697564737441253f * ONE), + (int) (0.0871557427476582f * ONE), + (int) (0.104528463267653f * ONE), + (int) (0.121869343405147f * ONE), + (int) (0.139173100960065f * ONE), + (int) (0.156434465040231f * ONE), + (int) (0.17364817766693f * ONE), + (int) (0.190808995376545f * ONE), + (int) (0.207911690817759f * ONE), + (int) (0.224951054343865f * ONE), + (int) (0.241921895599668f * ONE), + (int) (0.258819045102521f * ONE), + (int) (0.275637355816999f * ONE), + (int) (0.292371704722737f * ONE), + (int) (0.309016994374947f * ONE), + (int) (0.325568154457157f * ONE), + (int) (0.342020143325669f * ONE), + (int) (0.3583679495453f * ONE), + (int) (0.374606593415912f * ONE), + (int) (0.390731128489274f * ONE), + (int) (0.4067366430758f * ONE), + (int) (0.422618261740699f * ONE), + (int) (0.438371146789077f * ONE), + (int) (0.453990499739547f * ONE), + (int) (0.469471562785891f * ONE), + (int) (0.484809620246337f * ONE), + (int) (0.5f * ONE), + (int) (0.515038074910054f * ONE), + (int) (0.529919264233205f * ONE), + (int) (0.544639035015027f * ONE), + (int) (0.559192903470747f * ONE), + (int) (0.573576436351046f * ONE), + (int) (0.587785252292473f * ONE), + (int) (0.601815023152048f * ONE), + (int) (0.615661475325658f * ONE), + (int) (0.629320391049837f * ONE), + (int) (0.642787609686539f * ONE), + (int) (0.656059028990507f * ONE), + (int) (0.669130606358858f * ONE), + (int) (0.681998360062498f * ONE), + (int) (0.694658370458997f * ONE), + (int) (0.707106781186547f * ONE), + (int) (0.719339800338651f * ONE), + (int) (0.73135370161917f * ONE), + (int) (0.743144825477394f * ONE), + (int) (0.754709580222772f * ONE), + (int) (0.766044443118978f * ONE), + (int) (0.777145961456971f * ONE), + (int) (0.788010753606722f * ONE), + (int) (0.798635510047293f * ONE), + (int) (0.809016994374947f * ONE), + (int) (0.819152044288992f * ONE), + (int) (0.829037572555042f * ONE), + (int) (0.838670567945424f * ONE), + (int) (0.848048096156426f * ONE), + (int) (0.857167300702112f * ONE), + (int) (0.866025403784439f * ONE), + (int) (0.874619707139396f * ONE), + (int) (0.882947592858927f * ONE), + (int) (0.891006524188368f * ONE), + (int) (0.898794046299167f * ONE), + (int) (0.90630778703665f * ONE), + (int) (0.913545457642601f * ONE), + (int) (0.92050485345244f * ONE), + (int) (0.927183854566787f * ONE), + (int) (0.933580426497202f * ONE), + (int) (0.939692620785908f * ONE), + (int) (0.945518575599317f * ONE), + (int) (0.951056516295154f * ONE), + (int) (0.956304755963035f * ONE), + (int) (0.961261695938319f * ONE), + (int) (0.965925826289068f * ONE), + (int) (0.970295726275996f * ONE), + (int) (0.974370064785235f * ONE), + (int) (0.978147600733806f * ONE), + (int) (0.981627183447664f * ONE), + (int) (0.984807753012208f * ONE), + (int) (0.987688340595138f * ONE), + (int) (0.99026806874157f * ONE), + (int) (0.992546151641322f * ONE), + (int) (0.994521895368273f * ONE), + (int) (0.996194698091746f * ONE), + (int) (0.997564050259824f * ONE), + (int) (0.998629534754574f * ONE), + (int) (0.999390827019096f * ONE), + (int) (0.999847695156391f * ONE), + (int) (1f * ONE), + (int) (0.999847695156391f * ONE), + (int) (0.999390827019096f * ONE), + (int) (0.998629534754574f * ONE), + (int) (0.997564050259824f * ONE), + (int) (0.996194698091746f * ONE), + (int) (0.994521895368273f * ONE), + (int) (0.992546151641322f * ONE), + (int) (0.99026806874157f * ONE), + (int) (0.987688340595138f * ONE), + (int) (0.984807753012208f * ONE), + (int) (0.981627183447664f * ONE), + (int) (0.978147600733806f * ONE), + (int) (0.974370064785235f * ONE), + (int) (0.970295726275996f * ONE), + (int) (0.965925826289068f * ONE), + (int) (0.961261695938319f * ONE), + (int) (0.956304755963036f * ONE), + (int) (0.951056516295154f * ONE), + (int) (0.945518575599317f * ONE), + (int) (0.939692620785908f * ONE), + (int) (0.933580426497202f * ONE), + (int) (0.927183854566787f * ONE), + (int) (0.92050485345244f * ONE), + (int) (0.913545457642601f * ONE), + (int) (0.90630778703665f * ONE), + (int) (0.898794046299167f * ONE), + (int) (0.891006524188368f * ONE), + (int) (0.882947592858927f * ONE), + (int) (0.874619707139396f * ONE), + (int) (0.866025403784439f * ONE), + (int) (0.857167300702112f * ONE), + (int) (0.848048096156426f * ONE), + (int) (0.838670567945424f * ONE), + (int) (0.829037572555042f * ONE), + (int) (0.819152044288992f * ONE), + (int) (0.809016994374947f * ONE), + (int) (0.798635510047293f * ONE), + (int) (0.788010753606722f * ONE), + (int) (0.777145961456971f * ONE), + (int) (0.766044443118978f * ONE), + (int) (0.754709580222772f * ONE), + (int) (0.743144825477394f * ONE), + (int) (0.731353701619171f * ONE), + (int) (0.719339800338651f * ONE), + (int) (0.707106781186548f * ONE), + (int) (0.694658370458997f * ONE), + (int) (0.681998360062499f * ONE), + (int) (0.669130606358858f * ONE), + (int) (0.656059028990507f * ONE), + (int) (0.642787609686539f * ONE), + (int) (0.629320391049838f * ONE), + (int) (0.615661475325658f * ONE), + (int) (0.601815023152048f * ONE), + (int) (0.587785252292473f * ONE), + (int) (0.573576436351046f * ONE), + (int) (0.559192903470747f * ONE), + (int) (0.544639035015027f * ONE), + (int) (0.529919264233205f * ONE), + (int) (0.515038074910054f * ONE), + (int) (0.5f * ONE), + (int) (0.484809620246337f * ONE), + (int) (0.469471562785891f * ONE), + (int) (0.453990499739547f * ONE), + (int) (0.438371146789077f * ONE), + (int) (0.422618261740699f * ONE), + (int) (0.4067366430758f * ONE), + (int) (0.390731128489274f * ONE), + (int) (0.374606593415912f * ONE), + (int) (0.3583679495453f * ONE), + (int) (0.342020143325669f * ONE), + (int) (0.325568154457157f * ONE), + (int) (0.309016994374948f * ONE), + (int) (0.292371704722737f * ONE), + (int) (0.275637355817f * ONE), + (int) (0.258819045102521f * ONE), + (int) (0.241921895599668f * ONE), + (int) (0.224951054343865f * ONE), + (int) (0.207911690817759f * ONE), + (int) (0.190808995376545f * ONE), + (int) (0.17364817766693f * ONE), + (int) (0.156434465040231f * ONE), + (int) (0.139173100960066f * ONE), + (int) (0.121869343405148f * ONE), + (int) (0.104528463267654f * ONE), + (int) (0.0871557427476586f * ONE), + (int) (0.0697564737441255f * ONE), + (int) (0.0523359562429438f * ONE), + (int) (0.0348994967025007f * ONE), + (int) (0.0174524064372834f * ONE), + (int) (1.22514845490862E-16f * ONE), + (int) (-0.0174524064372832f * ONE), + (int) (-0.0348994967025009f * ONE), + (int) (-0.0523359562429436f * ONE), + (int) (-0.0697564737441248f * ONE), + (int) (-0.0871557427476579f * ONE), + (int) (-0.104528463267653f * ONE), + (int) (-0.121869343405148f * ONE), + (int) (-0.139173100960066f * ONE), + (int) (-0.156434465040231f * ONE), + (int) (-0.17364817766693f * ONE), + (int) (-0.190808995376545f * ONE), + (int) (-0.207911690817759f * ONE), + (int) (-0.224951054343865f * ONE), + (int) (-0.241921895599668f * ONE), + (int) (-0.25881904510252f * ONE), + (int) (-0.275637355816999f * ONE), + (int) (-0.292371704722736f * ONE), + (int) (-0.309016994374948f * ONE), + (int) (-0.325568154457157f * ONE), + (int) (-0.342020143325669f * ONE), + (int) (-0.3583679495453f * ONE), + (int) (-0.374606593415912f * ONE), + (int) (-0.390731128489274f * ONE), + (int) (-0.4067366430758f * ONE), + (int) (-0.422618261740699f * ONE), + (int) (-0.438371146789077f * ONE), + (int) (-0.453990499739546f * ONE), + (int) (-0.469471562785891f * ONE), + (int) (-0.484809620246337f * ONE), + (int) (-0.5f * ONE), + (int) (-0.515038074910054f * ONE), + (int) (-0.529919264233205f * ONE), + (int) (-0.544639035015027f * ONE), + (int) (-0.559192903470747f * ONE), + (int) (-0.573576436351046f * ONE), + (int) (-0.587785252292473f * ONE), + (int) (-0.601815023152048f * ONE), + (int) (-0.615661475325658f * ONE), + (int) (-0.629320391049838f * ONE), + (int) (-0.642787609686539f * ONE), + (int) (-0.656059028990507f * ONE), + (int) (-0.669130606358858f * ONE), + (int) (-0.681998360062498f * ONE), + (int) (-0.694658370458997f * ONE), + (int) (-0.707106781186547f * ONE), + (int) (-0.719339800338651f * ONE), + (int) (-0.73135370161917f * ONE), + (int) (-0.743144825477394f * ONE), + (int) (-0.754709580222772f * ONE), + (int) (-0.766044443118978f * ONE), + (int) (-0.777145961456971f * ONE), + (int) (-0.788010753606722f * ONE), + (int) (-0.798635510047293f * ONE), + (int) (-0.809016994374947f * ONE), + (int) (-0.819152044288992f * ONE), + (int) (-0.829037572555041f * ONE), + (int) (-0.838670567945424f * ONE), + (int) (-0.848048096156426f * ONE), + (int) (-0.857167300702112f * ONE), + (int) (-0.866025403784438f * ONE), + (int) (-0.874619707139396f * ONE), + (int) (-0.882947592858927f * ONE), + (int) (-0.891006524188368f * ONE), + (int) (-0.898794046299167f * ONE), + (int) (-0.90630778703665f * ONE), + (int) (-0.913545457642601f * ONE), + (int) (-0.92050485345244f * ONE), + (int) (-0.927183854566787f * ONE), + (int) (-0.933580426497202f * ONE), + (int) (-0.939692620785908f * ONE), + (int) (-0.945518575599317f * ONE), + (int) (-0.951056516295154f * ONE), + (int) (-0.956304755963035f * ONE), + (int) (-0.961261695938319f * ONE), + (int) (-0.965925826289068f * ONE), + (int) (-0.970295726275996f * ONE), + (int) (-0.974370064785235f * ONE), + (int) (-0.978147600733806f * ONE), + (int) (-0.981627183447664f * ONE), + (int) (-0.984807753012208f * ONE), + (int) (-0.987688340595138f * ONE), + (int) (-0.99026806874157f * ONE), + (int) (-0.992546151641322f * ONE), + (int) (-0.994521895368273f * ONE), + (int) (-0.996194698091746f * ONE), + (int) (-0.997564050259824f * ONE), + (int) (-0.998629534754574f * ONE), + (int) (-0.999390827019096f * ONE), + (int) (-0.999847695156391f * ONE), + (int) (-1f * ONE), + (int) (-0.999847695156391f * ONE), + (int) (-0.999390827019096f * ONE), + (int) (-0.998629534754574f * ONE), + (int) (-0.997564050259824f * ONE), + (int) (-0.996194698091746f * ONE), + (int) (-0.994521895368273f * ONE), + (int) (-0.992546151641322f * ONE), + (int) (-0.99026806874157f * ONE), + (int) (-0.987688340595138f * ONE), + (int) (-0.984807753012208f * ONE), + (int) (-0.981627183447664f * ONE), + (int) (-0.978147600733806f * ONE), + (int) (-0.974370064785235f * ONE), + (int) (-0.970295726275997f * ONE), + (int) (-0.965925826289068f * ONE), + (int) (-0.961261695938319f * ONE), + (int) (-0.956304755963035f * ONE), + (int) (-0.951056516295154f * ONE), + (int) (-0.945518575599317f * ONE), + (int) (-0.939692620785909f * ONE), + (int) (-0.933580426497202f * ONE), + (int) (-0.927183854566787f * ONE), + (int) (-0.92050485345244f * ONE), + (int) (-0.913545457642601f * ONE), + (int) (-0.90630778703665f * ONE), + (int) (-0.898794046299167f * ONE), + (int) (-0.891006524188368f * ONE), + (int) (-0.882947592858927f * ONE), + (int) (-0.874619707139396f * ONE), + (int) (-0.866025403784439f * ONE), + (int) (-0.857167300702112f * ONE), + (int) (-0.848048096156426f * ONE), + (int) (-0.838670567945424f * ONE), + (int) (-0.829037572555042f * ONE), + (int) (-0.819152044288992f * ONE), + (int) (-0.809016994374948f * ONE), + (int) (-0.798635510047293f * ONE), + (int) (-0.788010753606722f * ONE), + (int) (-0.777145961456971f * ONE), + (int) (-0.766044443118978f * ONE), + (int) (-0.754709580222772f * ONE), + (int) (-0.743144825477395f * ONE), + (int) (-0.731353701619171f * ONE), + (int) (-0.719339800338652f * ONE), + (int) (-0.707106781186548f * ONE), + (int) (-0.694658370458998f * ONE), + (int) (-0.681998360062498f * ONE), + (int) (-0.669130606358858f * ONE), + (int) (-0.656059028990507f * ONE), + (int) (-0.64278760968654f * ONE), + (int) (-0.629320391049838f * ONE), + (int) (-0.615661475325659f * ONE), + (int) (-0.601815023152048f * ONE), + (int) (-0.587785252292473f * ONE), + (int) (-0.573576436351046f * ONE), + (int) (-0.559192903470747f * ONE), + (int) (-0.544639035015027f * ONE), + (int) (-0.529919264233206f * ONE), + (int) (-0.515038074910054f * ONE), + (int) (-0.5f * ONE), + (int) (-0.484809620246337f * ONE), + (int) (-0.469471562785891f * ONE), + (int) (-0.453990499739547f * ONE), + (int) (-0.438371146789077f * ONE), + (int) (-0.4226182617407f * ONE), + (int) (-0.4067366430758f * ONE), + (int) (-0.390731128489275f * ONE), + (int) (-0.374606593415912f * ONE), + (int) (-0.358367949545301f * ONE), + (int) (-0.342020143325669f * ONE), + (int) (-0.325568154457158f * ONE), + (int) (-0.309016994374948f * ONE), + (int) (-0.292371704722736f * ONE), + (int) (-0.275637355817f * ONE), + (int) (-0.258819045102521f * ONE), + (int) (-0.241921895599668f * ONE), + (int) (-0.224951054343865f * ONE), + (int) (-0.20791169081776f * ONE), + (int) (-0.190808995376545f * ONE), + (int) (-0.173648177666931f * ONE), + (int) (-0.156434465040231f * ONE), + (int) (-0.139173100960066f * ONE), + (int) (-0.121869343405148f * ONE), + (int) (-0.104528463267653f * ONE), + (int) (-0.0871557427476583f * ONE), + (int) (-0.0697564737441248f * ONE), + (int) (-0.0523359562429444f * ONE), + (int) (-0.0348994967025008f * ONE), + (int) (-0.0174524064372844f * ONE), + }; }