Programming Guide: Math Routines

# Programming Guide

## Math Routines

This section contains functions and macros to perform math operations and conversions for the following types:

ODFixed | A 32-bit fixed-point value used to represent non-integer numbers in the range [-32768, 32768). The most-significant 16 bits are integral and the least-significant 16 bits are fractional.
ODFract | A 32-bit fixed-point value used to represent non-integer numbers in the range [-2, 2). The most-significant 2 bits are integral and the least-significant 30 bits are fractional.
ODFloat | A floating-point value.
ODSLong | A signed 32-bit integer value.
ODSShort | A signed 16-bit integer value.
ODULong | An unsigned 32-bit integer value.
ODUShort | An unsigned 16-bit integer value.
ODWide | A 64-bit signed integer.

The following lists the math functions and macros in alphabetic order.

- ODFirstBit
- ODFixedDivide
- ODFixedRound
- ODFixedMultiply
- ODFixedToFloat
- ODFixedToFract
- ODFloatToFixed
- ODFloatToFract
- ODFractDivide
- ODFractMultiply
- ODFractSinCos
- ODFractToFixed
- ODFractToFloat
- IntToODFixed
- ODWideAdd
- ODWideCompare
- ODWideDivide
- ODWideIsLong
- ODWideMultiply
- ODWideNegate
- ODWideShift
- ODWideSquareRoot
- ODWideSubtract

### ODFirstBit

This function returns the position of the first bit set in a signed long.

Signature

    #include <odmath.h>

    ODSShort ODFirstBit (ODSLong a)

Parameters

a (ODSLong) - input

  The signed long to be queried.

Returns

rv (ODSShort) - returns

  The position of the first bit set in the specified signed long.

### ODFixedDivide

This function divides one fixed-point number by another fixed-point number.

Signature

    #include <odmath.h>

    ODFixed ODFixedDivide (ODFixed a, ODFixed b)

Parameters

a (ODFixed) - input

  The dividend.

b (ODFixed) - input

  The divisor.

Returns

rv (ODFixed) - returns

  The quotient.

### ODFixedRound

This macro rounds a fixed-point number to a signed short.

Signature

    #include <odmath.h>

    ODSShort ODFixedRound (ODFixed a)

Parameters

a (ODFixed) - input

  The fixed-point number to be rounded.

Returns

rv (ODSShort) - returns

  The signed short containing the rounded number.

### ODFixedMultiply

This function multiplies two fixed-point numbers.

Signature

    #include <odmath.h>

    ODFixed ODFixedMultiply (ODFixed a, ODFixed b)

Parameters

a (ODFixed) - input

  The first fixed-point number to be multiplied.

b (ODFixed) - input

  The second fixed-point number to be multiplied.

Returns

rv (ODFixed) - returns

  The product of the multiplication.

### ODFixedToFloat

This macro converts a fixed-point number to a floating-point number.

Signature

    #include <odmath.h>

    ODFloat ODFixedToFloat (ODFixed a)

Parameters

a (ODFixed) - input

  The fixed-point number to be converted.

Returns

rv (ODFloat) - returns

  The converted floating-point number.

### ODFixedToFract

This macro converts a fixed-point number (16:16) to a fractional number (2:30).

Signature

    #include <odmath.h>

    ODFract ODFixedToFract (ODFixed a)

Parameters

a (ODFixed) - input

  The fixed-point number to be converted.

Returns

rv (ODFract) - returns

  The converted fractional number.

### ODFloatToFixed

This macro converts a floating-point number to a fixed-point number (16:16).

Signature

    #include <odmath.h>

    ODFixed ODFloatToFixed (ODFloat a)

Parameters

a (ODFloat) - input

  The floating-point number to be converted.

Returns

rv (ODFixed) - returns

  The converted fixed-point number.

### ODFloatToFract

This macro converts a floating-point number to a fractional number (2:30).

Signature

    #include <odmath.h>

    ODFract ODFloatToFract (ODFloat a)

Parameters

a (ODFloat) - input

  The floating-point number to be converted.

Returns

rv (ODFract) - returns

  The converted fractional number.

### ODFractDivide

This function divides one factional number by another fractional number.

Signature

    #include <odmath.h>

    ODFract ODFractDivide (ODFract a, ODFract b)

Parameters

a (ODFract) - input

  The dividend.

b (ODFract) - input

  The divisor.

Returns

rv (ODFract) - returns

  The quotient.

### ODFractMultiply

This function multiplies two fractional numbers.

Signature

    #include <odmath.h>

    ODFract ODFractMultiply (ODFract a, ODFract b)

Parameters

a (ODFract) - input

  The first fractional number to be multiplied.

b (ODFract) - input

  The second fractional number to be multiplied.

Returns

rv (ODFract) - returns

  The product of the multiplication.

### ODFractSinCos

This function returns the sine and cosine of a fractional number.

Signature

    #include <odmath.h>

    ODFract ODFractSinCos (ODFixed angle, ODFract *cos)

Parameters

angle (ODFixed) - input

  The angle of the radius.

cos (ODFract *) - output

  The cosine of the angle.

Returns

rv (ODFract) - returns

  The sine of the angle.

### ODFractToFixed

This macro converts a fractional number (2:30) to a fixed-point number (16:16).

Signature

    #include <odmath.h>

    ODFixed ODFractToFixed (ODFract a)

Parameters

a (ODFract) - input

  The fractional number to be converted.

Returns

rv (ODFixed) - returns

  The converted fixed-point number.

### ODFractToFloat

This function converts a fractional number (2:30) to a floating-point number.

Signature

    #include <odmath.h>

    ODFloat ODFractToFloat (ODFract a)

Parameters

a (ODFract) - input

  The fractional number to be converted.

Returns

rv (ODFloat) - returns

  The converted floating-point number.

### IntToODFixed

This macro converts a signed short to a fixed-point number (16:16).

Signature

    #include <odmath.h>

    ODFixed IntToODFixed (ODSShort a)

Parameters

a (ODSShort) - input

  The signed short to be converted.

Returns

rv (ODFixed) - returns

  The converted fixed-point number.

### ODWideAdd

This function adds two wide integers.

Signature

    #include <odmath.h>

    ODWide *ODWideAdd (ODWide *a, const ODWide *b)

Parameters

a (ODWide *) - input

  The first wide integer to be added.

b (const ODWide *) - input

  The second wide integer to be added.

Returns

rv (ODWide *) - returns

  The sum of the two wide integers.

### ODWideCompare

This function compares two wide integers.

Signature

    #include <odmath.h>

    ODSShort ODWideCompare (const ODWide *a, const ODWide *b)

Parameters

a (const ODWide *) - input

  The first wide integer to be compared.

b (const ODWide *) - input

  The second wide integer to be compared.

Returns

rv (ODSShort) - returns

  The result of the comparison. This parameter returns one of the following values:

  1 | a > b
  0 | a = b
  -1 | a < b

### ODWideDivide

This function divides one wide integer by a long integer.

Signature

    #include <odmath.h>

    DSLong ODWideDivide (const ODWide *dividend,
                         ODSLong divisor,
                         ODSLong *remainder)

Parameters

dividend (const ODWide *) - input

  The dividend.

divisor (ODSLong) - input

  The divisor.

remainder (ODSLong *) - output

  The remainder.

Returns

rv (ODSLong) - returns

  The quotient.

### ODWideIsLong

This macro indicates whether a wide integer has less than 32 bits of significance.

Signature

    #include <odmath.h>

    ODBoolean ODWideIsLong (ODWide w)

Parameters

w (ODWide) - input

  The wide integer to be checked.

Returns

rv (ODBoolean) - returns

  A flag indicating whether the specified wide integer has less than 32 bits of significance.

  kODTrue | The specified wide integer has less than 32 bits of significance.
  kODFalse | The specified wide integer has at least than 32 bits of significance.

### ODWideMultiply

This function multiplies two signed, long integers.

Signature

    #include <odmath.h>

    ODWide *ODWideMultiply (ODSLong a,
                            ODSLong b,
                            ODWide *result)

Parameters

a (ODSLong) - input

  The first integer to be multiplied.

b (ODSLong) - input

  The second integer to be multiplied.

result (ODWide *) - output

  A pointer to the product of the multiplication.

Returns

rv (ODWide *) - returns

  A pointer to the product of the multiplication.

### ODWideNegate

This function negates a wide integer.

Signature

    #include <odmath.h>

    ODWide *ODWideNegate

Parameters

w (ODWide *) - in/out

  On input, this parameter is the wide integer to be negated. On output, the 2's complement of the wide integer is returned.

Returns

rv (ODWide *) - returns

  The 2's complement of the specified wide integer.

### ODWideShift

This function shifts a wide integer by the specified number of bits.

Signature

    #include <odmath.h>

    ODWide *ODWideShift (ODWide *w, ODSShort bits)

Parameters

w (ODWide *) - in/out

  The wide integer to be shifted.

bits (ODSShort) - input

  The number of bits to shift the wide integer. This parameter can be set to one of the following values:

  >0 | Shifts to the right.
  <0 | Shifts to the left.
  0 | Does not shift.

Returns

rv (ODWide *) - returns

  The wide integer that resulted from the shift.

### ODWideSquareRoot

This function returns the approximate square root of a wide integer.

Signature

    #include <odmath.h>

    ODULong ODWideSquareRoot (const ODWide *src)

Parameters

src (const ODWide *) - input

  A pointer to the wide integer whose square root is being requested. This must be a positive integer.

Returns

rv (ODULong) - returns

  The approximate square root of the specified wide integer.

### ODWideSubtract

This function subtracts two wide integers.

Signature

    #include <odmath.h>

    ODWide *ODWideSubtract (ODWide *a, const ODWide *b)

Parameters

a (ODWide *) - in/out

  On input, this parameter is the wide integer to be subtracted from. On output, the difference between a and b is returned.

b (const ODWide *) - input

  The wide integer to be subtracted.

Returns

rv (ODWide *) - returns

  The difference between a and b.

[ Top | Previous | Next | Contents | Index | Documentation Homepage ]