Skip to content

SANE Floating Point

David Schmenk edited this page Feb 27, 2023 · 25 revisions

Apple created a floating point library called SANE: Standard Apple Numeric Environment. These libraries require the FP6502.CODE and ELEMS.CODE files be on the disk.


Floating Point <-> String Conversion

A string conversion library provides formatting for output and input of floating point values.

Compatibility: 1, II, ///

Usage: include "inc/fpstr.plh"

Values:

//
// String formatting bits
//
const FPSTR_FIXED = 1 // Fixed count of fractional digits
const FPSTR_FLOAT = 0 // Floating count of fractional digits
const FPSTR_STRIP = 2 // Strip trailing fractional zeros
const FPSTR_EXP   = 4 // Force exponential format
const FPSTR_FLEX  = 8 // Allow flexible switch to exponential format if over/underflow

Functions:

//
// Parse string into decrecord, return SANE conversion output
//
str2ext(str, ext)
//
// Convert extended FP to string using , return string
//
ext2str(ext, str, intdigits, fracdigits, format)

Source Code: fpstr.pla

Sample Source: rpncalc.pla


Floating Point Unit

A high level, RPN stack based interface to the SANE library. Accessed via:

Compatibility: 1, II, ///

Usage: include "inc/fpu.plh"

Values:

//
// FPU register type
//
struc t_fpureg
    byte[10] _regdata
end
//
// RPN Register indeces
//
const X_REG = 0
const Y_REG = 1
const Z_REG = 2
const T_REG = 3
//
// Comparison results
//
const FPUCMPGT = $4040      // Greater Than
const FPUCMPLT = $8080      // Less Than
const FPUCMPEQ = $0002      // EQual
const FPUCMPUN = $0101      // UNordered
//
// Exceptions
//
const FPUINVALID = $01
const FPUUFLOW   = $02
const FPUOFLOW   = $04
const FPUDIVZER  = $08
const FPUINEXACT = $10
//
// FP type
//
const FPUSNAN    = $00FC    // -4: signaling NAN
const FPUQNAN    = $00FD    // -3: quiet NAN
const FPUINF     = $00FE    // -2: infinite
const FPUZERO    = $00FF    // -1: zero
const FPUNORM    = $0000    //  0: normal
const FPUDENORM  = $0001    //  1: denormal
const FPUNEG     = $8000

FP6502 API:

fpu:reset
fpu:getEnv
fpu:setEnv
fpu:testExept
fpu:setExcept
fpu:enterProc
fpu:exitProc
fpu:constPi
fpu:constE
fpu:pushInt
fpu:pushSgl
fpu:pushDbl
fpu:pushExt
fpu:pushStr
fpu:pullInt
fpu:pullSgl
fpu:pullDbl
fpu:pullExt
fpu:pullStr
fpu:loadInt
fpu:loadSgl
fpu:loadDbl
fpu:loadExt
fpu:loadStr
fpu:storInt
fpu:storSgl
fpu:storDbl
fpu:storExt
fpu:storStr
fpu:shiftUp
fpu:shiftDown
fpu:dropX
fpu:rotateUp
fpu:rotateDown
fpu:dupX
fpu:swapXY
fpu:clearX
fpu:addXY
fpu:subXY
fpu:mulXY
fpu:divXY
fpu:remXY
fpu:negX
fpu:absX
fpu:typeX
fpu:cmpXY
fpu:logbX
fpu:scalebXInt
fpu:truncX
fpu:roundX
fpu:sqrtX
fpu:squareX

ELEMS API:

fpu:cosX
fpu:sinX
fpu:tanX
fpu:atanX
fpu:log2X
fpu:log21X
fpu:lnX
fpu:ln1X
fpu:pow2X
fpu:pow21X
fpu:powEX
fpu:powE1X
fpu:powE21X
fpu:powXInt
fpu:powXY
fpu:compXY
fpu:annuityXY
fpu:randNum

Source Code: fpu.pla

Sample Source: rpncalc.pla


SANE Interface

The low-level, direct interface to the SANE library. Accessed via:

Compatibility: 1, II, ///

Usage: include "inc/sane.plh"

Values:

//-----------------------------------------------------------
// Operation code masks.
//-----------------------------------------------------------
const FOADD     = $0000     // add
const FOSUB     = $0002     // subtract
const FOMUL     = $0004     // multiply
const FODIV     = $0006     // divide
const FOCMP     = $0008     // compare, no exception from unordered
const FOCPX     = $000A     // compare, signal invalid if unordered
const FOREM     = $000C     // remainder
const FOZ2X     = $000E     // convert to extended
const FOX2Z     = $0010     // convert from extended
const FOSQRT    = $0012     // square root
const FORTI     = $0014     // round to integral value
const FOTTI     = $0016     // truncate to integral value
const FOSCALB   = $0018     // binary scale
const FOLOGB    = $001A     // binary log
const FOCLASS   = $001C     // classify
const FONEXT    = $001E     // next-after
//
const FOSETENV  = $0001     // set environment
const FOGETENV  = $0003     // get environment
const FOSETHV   = $0005     // set halt vector
const FOGETHV   = $0007     // get halt vector
const FOD2B     = $0009     // convert decimal to binary
const FOB2D     = $000B     // convert binary to decimal
const FONEG     = $000D     // negate
const FOABS     = $000F     // absolute value
const FOCPYSGN  = $0011     // copy sign
// UNDEFINED    = $0013
const FOSETXCP  = $0015     // set exception
const FOPROCENTRY = $0017   // procedure-entry
const FOPROCEXIT = $0019    // procedure-exit
const FOTESTXCP = $001B     // test exception
// UNDEFINED    = $001D
// UNDEFINED    = $001F
//-----------------------------------------------------------
// Operand format masks.
//-----------------------------------------------------------
const FFEXT     = $0000     // extended -- 80-bit float
const FFDBL     = $0100     // double   -- 64-bit float
const FFSGL     = $0200     // single   -- 32-bit float
// UNDEFINED    = $0300
const FFINT     = $0400     // integer  -- 16-bit integer
const FFCOMP    = $0500     // comp     -- 64-bit integer
// UNDEFINED    = $0600
// UNDEFINED    = $0700
//-----------------------------------------------------------
// Class.
//-----------------------------------------------------------
const FCSNAN    = $FC       // -4: signaling NAN
const FCQNAN    = $FD       // -3: quiet NAN
const FCINF     = $FE       // -2: infinite
const FCZERO    = $FF       // -1: zero
const FCNORM    = $00       //  0: normal
const FCDENORM  = $01       //  1: denormal
//-----------------------------------------------------------
// Exceptions.
//-----------------------------------------------------------
const FBINVALID = $01
const FBUFLOW   = $02
const FBOFLOW   = $04
const FBDIVZER  = $08
const FBINEXACT = $10
//-----------------------------------------------------------
// Elementary function operation code masks.
//-----------------------------------------------------------
const FOLNX     = $0000     // base-e log
const FOLOG2X   = $0002     // base-2 log
const FOLN1X    = $0004     // ln (1 + x)
const FOLOG21X  = $0006     // log2 (1 + x)
const FOEXPX    = $0008     // base-e exponential
const FOEXP2X   = $000A     // base-2 exponential
const FOEXP1X   = $000C     // exp (x) - 1
const FOEXP21X  = $000E     // exp2 (x) - 1
const FOXPWRI   = $0010     // integer exponentiation
const FOXPWRY   = $0012     // general exponentiation
const FOCOMPND  = $0014     // compound
const FOANNUIT  = $0016     // annuity
const FOATANX   = $0018     // arctangent
const FOSINX    = $001A     // sine
const FOCOSX    = $001C     // cosine
const FOTANX    = $001E     // tangent
const FORANDX   = $0020     // random
//-----------------------------------------------------------
// NaN error codes.
//-----------------------------------------------------------
const NANSQRT   = 1         // Invalid square root such as sqrt(-1).
const NANADD    = 2         // Invalid addition such as +INF - +INF.
const NANDIV    = 4         // Invalid division such as 0/0.
const NANMUL    = 8         // Invalid multiply such as 0 * INF.
const NANREM    = 9         // Invalid remainder or mod such as x REM 0.
const NANASCBIN = 17        // Attempt to convert invalid ASCII string.
const NANCOMP   = 20        // Result of converting comp NaN to floating.
const NANZERO   = 21        // Attempt to create a NaN with a zero code.
const NANTRIG   = 33        // Invalid argument to trig routine.
const NANINVTRIG = 34       // Invalid argument to inverse trig routine.
const NANLOG    = 36        // Invalid argument to log routine.
const NANPOWER  = 37        // Invalid argument to x^i or x^y routine.
const NANFINAN  = 38        // Invalid argument to financial function.
const NANINIT   = 255       // Uninitialized storage.
//-----------------------------------------------------------
// Comparison results.
//-----------------------------------------------------------
const FCMPGT    = $4040     // Greater Than
const FCMPLT    = $8080     // Less Than
const FCMPEQ    = $0002     // EQual
const FCMPUN    = $0101     // UNordered
//-----------------------------------------------------------
//
// Data types
//
struc t_single
    byte[3] s_mantissa
    byte    s_exponent
end
struc t_double
    byte[6] d_mantissa
    word    d_exponent
end
struc t_extended
    byte[8] x_mantissa
    word    x_exponent
end
struc t_bigint
    byte[8] l_int
end

FP6502 API:

sane:initFP()
sane:haltFP
sane:op0FP
sane:op1FP
sane:op2FP
sane:op3FP
sane:saveZP
sane:restoreZP

ELEMS API:

sane:op1ELEM
sane:op2ELEM
sane:op3ELEM

Source Code: sane.pla

Sample Source: sanity.pla

Clone this wiki locally