Main Content

Overflow Handling and Rounding Methods

Tokens for Overflow Handling and Rounding Methods

The API for user-written fixed-point S-functions provides functions for some mathematical operations, such as conversions. When these operations are performed, a loss of precision or overflow may occur. The tokens in the following tables allow you to control the way an API function handles precision loss and overflow. The data type of the overflow handling methods is fxpModeOverflow. The data type of the rounding modes is fxpModeRounding.

Overflow Handling Tokens

Token

Description

FXP_OVERFLOW_SATURATE

Saturate overflows

FXP_OVERFLOW_WRAP

Wrap overflows

Rounding Method Tokens

Token

Description

FXP_ROUND_CEIL

Round to the closest representable number in the direction of positive infinity

FXP_ROUND_CONVERGENT

Round toward nearest integer with ties rounding to nearest even integer

FXP_ROUND_FLOOR

Round to the closest representable number in the direction of negative infinity

FXP_ROUND_NEAR

Round to the closest representable number, with the exact midpoint rounded in the direction of positive infinity

FXP_ROUND_NEAR_ML

Round toward nearest. Ties round toward negative infinity for negative numbers, and toward positive infinity for positive numbers

FXP_ROUND_SIMPLEST

Automatically chooses between round toward floor and round toward zero to produce generated code that is as efficient as possible

FXP_ROUND_ZERO

Round to the closest representable number in the direction of zero

Overflow Logging Structure

Math functions of the API, such as ssFxpConvert, can encounter overflows when carrying out an operation. These functions provide a mechanism to log the occurrence of overflows and to report that log back to the caller.

You can use a fixed-point overflow logging structure in your S-function by defining a variable of data type fxpOverflowLogs. Some API functions, such as ssFxpConvert, accept a pointer to this structure as an argument. The function initializes the logging structure and maintains a count of each the following events that occur while the function is being performed:

  • Overflows

  • Saturations

  • Divide-by-zeros

When a function that accepts a pointer to the logging structure is invoked, the function initializes the event counts of the structure to zero. The requested math operations are then carried out. Each time an event is detected, the appropriate event count is incremented by one.

The following fields contain the event-count information of the structure:

  • OverflowOccurred

  • SaturationOccurred

  • DivisionByZeroOccurred