CodeWarrior for Palm Pilot Math Tutorial

I had a devil of a time getting floating point operations to work using the CodeWarrior IDE for Palm (R6), so I wrote this tutorial for others who may be following the same path to save a little time. All of this was ultimately available in small pieces on the web or in the CodeWarrior directory. This is just a tutorial, the real work was done by people like Rick Huebner, who ported the GCC IEEE-754 double-precision math functions to a shared library, and Chris Tutty and Dick Chan, who answered my Usenet post with highly useful pointers.

If you just want to use +, -, *, /

To understand my choice, you can read Jun-Kiat Lam's answer to a Usenet post by Stanley Gelman here. I'm using the Palm OS 2.0 and 3.0 version of the Metrowerks floating point library in ROM.

Setting up the ROM library

First, find your target in the IDE. Note the target tab is selected.

Double click on the target icon. The following window will appear:

Click on 'Code Generation-68K Processor' in the 'Target Settings Panels' window. Select 'PalmOS' in the 'Floating Point:' field.

Click on the 'Save' button.

Coding

You will need to use the NewFloatMgr header file, so include it:

#include <NewFloatMgr.h>

I found this file in

C:\Program Files\Metrowerks\CW for Palm OS R6\Palm OS 3.1 Support\Incs\System You might find it useful to peruse the header file. You'll most likely be using the FlpCompDouble type, which is a union. You assign values with the ".d" extension, and call NewFloatMgr routines with the ".fd" extension. This example code should give you an idea:

FlpCompDouble aF, cF; float a, b, c; char atext[] = {"2"}, ctext[20]; aF.fd = FlpAToF(atext); a = aF.d; b = 3; c = a / b; cF.d = c; FlpFToA (cF.fd, ctext); WinDrawChars(ctext, StrLen(ctext), 0, 140); Note the use of the FlpFToA and FlpAToF functions to convert to and from ASCII. Note also (like everything in life) their case. I only say this because I spent about 2 hours debugging code that had "FlpAtoF" instead of "FlpAToF".

If you want to use functions like sin( ) and cos( )...

Setting up your project in the IDE

Rick Huebner ported the GCC IEEE-754 double-precision math functions to a shared library, and as far as I'm concerned, the world will always owe him one for that. Go to the PilotBrew page and download MathLib.zip. Much of what follows is taken straight from his User's Manual.

Don't forget to transfer the MathLib.prc file to the Pilot before you debug or run your code.

After you unzip Rick's archive, you'll find "MathLib.c" in the "src" directory. You'll need to add it to your project. The "Files" tab in the project window should look something like:

Drag and drop the "MathLib.c" icon

Onto the "AppSource" folder in the project window:

Make sure you're dragging "MathLib.c" and not "MathLib.h" for this operation.

Coding

Copy "MathLib.h" into the directory where you have your C file, so that you can include it in your source code:

#include "MathLib.h"

From Rick's User's Manual, put this code or something like it in your StartApplication( ) routine or equivalent:

Err error; error = SysLibFind(MathLibName, &MathLibRef); if (error) error = SysLibLoad(LibType, MathLibCreator, &MathLibRef); ErrFatalDisplayIf(error, "Can't find MathLib"); // Just an example; handle it gracefully error = MathLibOpen(MathLibRef, MathLibVersion); ErrFatalDisplayIf(error, "Can't open MathLib");

And this code or something like it in your StopApplication( ) routine or equivalent:

UInt usecount; error = MathLibClose(MathLibRef, &usecount); ErrFatalDisplayIf(error, "Can't close MathLib"); if (usecount == 0) SysLibRemove(MathLibRef);

You can now use IEEE math functions a la my previous example:

FlpCompDouble aF, cF; float a, b, c; char atext[] = {"2"}, ctext[20]; aF.fd = FlpAToF(atext); a = aF.d; b = 3; c = sqrt(a) / sin(b); cF.d = c; FlpFToA (cF.fd, ctext); WinDrawChars(ctext, StrLen(ctext), 0, 140);

A list of Rick's ported IEEE functions:

The following table is taken straight out of Rick's header file:

Trigonometric functions
acos(x)Arc cosine of x
asin(x)Arc sine of x
atan(x)Arc tangent of x
atan2(y, x)Arc tangent of y/x
cos(x)Cosine of x
sin(x)Sine of x
tan(x)Tangent of x
sincos(x, *sinx, *cosx)Sine and cosine of x
Hyperbolic functions
cosh(x)Hyperbolic cosine of x
sinh(x)Hyperbolic sine of x
tanh(x)Hyperbolic tangent of x
acosh(x)Hyperbolic arc cosine of x
asinh(x)Hyperbolic arc sine of x
atanh(x)Hyperbolic arc tangent of x
Exponential and logarithmic functions
exp(x)Exponential function of x [pow(e,x)]
frexp(x,*exponent)Break x into normalized fraction and an integral power of 2
ldexp(x, exponent)x * pow(2,exponent)
log(x)Natural logarithm of x
log10(x)Base 10 logarithm of x
modf(x, *intpart)Break x into integral and fractional parts
expm1(x)exp(x) - 1
log1p(x)log(1+x)
logb(x)Base 2 signed integral exponent of x
log2(x)Base 2 logarithm of x
Power functions
pow(x, y)x to the y power [x**y]
sqrt(x)Square root of x [x**0.5]
hypot(x, y)sqrt(x*x + y*y) [hypotenuse of right triangle]
cbrt(x)Cube root of x [x**(1/3)]
Nearest integer, absolute value, and remainder functions
ceil(x)Smallest integral value not less than x
fabs(x)Absolute value of x
floor(x)Largest integral value not greater than x
fmod(x, y)Modulo remainder of x/y
Miscellaneous functions
isinf(x)Return 0 if x is finite or NaN, +1 if +Infinity, or -1 if -Infinity
finite(x)Return nonzero if x is finite and not NaN
scalbn(x, exponent)x * pow(2,exponent)
drem(x, y)Remainder of x/y
significand(x)Fractional part of x after dividing out ilogb(x)
copysign(x, y)Return x with its sign changed to match y's
isnan(x)Return nonzero if x is NaN (Not a Number)
ilogb(x)Binary exponent of non-zero x
rint(x)Integral value nearest x in direction of prevailing rounding mode
nextafter(x, y)Next machine value after x in the direction towards y
remainder(x, y)Remainder of integer division x/y with infinite precision
scalb(x, exponent)x * pow(2,exponent)
round(x)Round x to nearest integral value away from zero
trunc(x)Round x to nearest integral value not larger than x
signbit(x)Return signbit of x's machine representation


Hope this tutorial helps!

Craig Niederberger, June 2000