Linux Assemblycollection of fast libraries

FPU libraryFPU.h

FPU library was designed to allow C/C++ programs to communicate with FPU directly via simple interface, to get its status, and change unit work mode. This is common routine for any mathematics and computation algorithms. But "C" and "C++" language just provide very abstract paradigm how programs should operate with hardware units. It is because of portability of both languages. But if we need high speed calculation, we also need to operate with hardware units as close as it is possible. That is why such programs require some kind of interface functions to manipulate with hardware units state and change working mode.

Contents

Function list

C function nameFunctionsC++ function nameFunctions
ClearExceptionMasks1 functionClearExceptionMasks1 function
ClearExceptions1 functionClearExceptions1 function
GetErrorSummaryFlag1 functionGetErrorSummaryFlag1 function
GetExceptionMasks1 functionGetExceptionMasks1 function
GetExceptions1 functionGetExceptions1 function
GetMode1 functionGetMode1 function
GetPrecisionMode1 functionGetPrecisionMode1 function
GetRoundMode1 functionGetRoundMode1 function
GetStackFaultFlag1 functionGetStackFaultFlag1 function
GetStackTop1 functionGetStackTop1 function
GetStatus1 functionGetStatus1 function
Init1 functionInit1 function
SetExceptionMasks1 functionSetExceptionMasks1 function
SetMode1 functionSetMode1 function
SetPrecisionMode1 functionSetPrecisionMode1 function
SetRoundMode1 functionSetRoundMode1 function
C function nameFunctionsC++ function nameFunctions

FPU constants

ConstantDescription
FPU rounding modes
FPU_RC_NEARESTRound to nearest even integer
FPU_RC_ROND_DOWNRound down (floor)
FPU_RC_ROND_UPRound up (ceil)
FPU_RC_TRUNCATERound to nearest integer, toward zero (truncation)
FPU precision modes
FPU_PC_SINGLESingle precision (float)
FPU_PC_DOUBLEDouble precision (double)
FPU_PC_EXTENDEDExtended precision (long double)
FPU exception masks
FPU_PMPrecision mask
FPU_UMUnderflow mask
FPU_OMOverflow mask
FPU_ZMDivide by zero mask
FPU_DMDenormal operand mask
FPU_IMInvalid operation mask
FPU_ALL_MASKSAll masks
FPU exception flags
FPU_PEPrecision flag
FPU_UEUnderflow flag
FPU_OEOverflow flag
FPU_ZEDivide by zero flag
FPU_DEDenormal operand flag
FPU_IEInvalid operation flag
FPU_ALL_EXCPTSAll flags
FPU defaults
FPU_DEFAULTDefault FPU mode

FPU data types

TypeDescriptionC/C++ equivalent
fpu_ctrl_tFPU control wordint
fpu_stat_tFPU status wordint

FPU control functions

The FPU control word controls the precision of the FPU and rounding method used. It also contains the FPU floating-point exception mask bits. The control word is cached in the FPU control register.

Init FPU

Cvoid FPU_Init (void);
C++void FPU::Init (void);

Description: Sets the FPU control, status, tag, instruction pointer, and data pointer registers to their default states.

Parameters: None.

Return value: None.

Set FPU mode

Cvoid FPU_SetMode (fpu_ctrl_t cword);
C++void FPU::SetMode (fpu_ctrl_t cword);

Description: Sets FPU mode according to the control word value, passed to the function.

Parameters:

  • cword - FPU control word

Return value: None.

Get FPU mode

Cfpu_ctrl_t FPU_GetMode (void);
C++fpu_ctrl_t FPU::GetMode (void);

Description: Returns current value of the FPU control word.

Parameters: None.

Return value: FPU control word.

FPU rounding mode

The rounding-control field of the FPU control word controls how the results of FPU floating-point instructions are rounded. The IEEE Standard 754 defines four rounding modes: round to nearest, round up, round down, and round toward zero. The default rounding mode is round to nearest. This mode provides the most accurate and statistically unbiased estimate of the true result and is suitable for most applications.

Set round mode

C/C++fpu_ctrl_t SetRoundMode (fpu_ctrl_t cword, int rmode);

Description: Changes round mode value into the FPU control word.

Parameters:

Return value: FPU control word with new round mode.

Get round mode

C/C++int GetRoundMode (fpu_ctrl_t cword);

Description: Extracts round mode from the FPU control word.

Parameters:

  • cword - FPU control word

Return value: Round mode value (see round modes).

FPU precision mode

The precision-control field determines the precision (64, 53, or 24 bits) of floating-point calculations made by the FPU. The default precision is double extended precision, which uses the full 64-bit significand available with the double extended-precision floating-point format of the FPU data registers. This setting is best suited for most applications, because it allows applications to take full advantage of the maximum precision available with the FPU data registers.

Set precision mode

C/C++fpu_ctrl_t SetPrecisionMode (fpu_ctrl_t cword, int pmode);

Description: Changes precision mode value into the FPU control word.

Parameters:

Return value: FPU control word with new precision mode.

Get precision mode

C/C++int GetPrecisionMode (fpu_ctrl_t cword);

Description: Extracts precision mode from the FPU control word.

Parameters:

  • cword - FPU control word

Return value: Precision mode value (see precision modes).

FPU exception masks

The exception-flag mask bits mask the 6 floating-point exception flags in the FPU status word. When one of these mask bits is set, its corresponding FPU floating-point exception is blocked from being generated.

Clear exception masks

C/C++fpu_ctrl_t ClearExceptionMasks (fpu_ctrl_t cword, int exceptions);

Description: Clears selected exception masks into the FPU control word.

Parameters:

Return value: FPU control word with cleared exception masks.

Set exception masks

C/C++fpu_ctrl_t SetExceptionMasks (fpu_ctrl_t cword, int exceptions);

Description: Sets selected exception masks into the FPU control word.

Parameters:

Return value: FPU control word with new exception masks.

Get exception masks

C/C++int GetExceptionMasks (fpu_ctrl_t cword, int exceptions);

Description: Extracts selected exception masks from the FPU control word.

Parameters:

Return value: Bit set of exception masks (see exception masks).

FPU status functions

The FPU status register indicates the current state of the FPU. The flags in the FPU status register include the FPU busy flag, top-of-stack (TOP) pointer, condition code flags, error summary status flag, stack fault flag, and exception flags. The FPU sets the flags in this register to show the results of operations.

Clear FPU exceptions

Cvoid FPU_ClearExceptions (void);
C++void FPU::ClearExceptions (void);

Description: Clears all floating-point exception flags, the exception summary status flag, the stack fault flag, and the busy flag of the FPU device.

Parameters: None.

Return value: None.

Get FPU status

Cfpu_stat_t FPU_GetStatus (void);
C++fpu_stat_t FPU::GetStatus (void);

Description: Returns current value of the FPU status word.

Parameters: None.

Return value: FPU status word.

FPU exception flags

C/C++int GetExceptions (fpu_stat_t sword, int exceptions);

Description: The six floating-point exception flags of the FPU status word indicate that one or more floating-point exceptions have been detected since the bits were last cleared. Each of the exception flags can be masked by an exception mask bit in the FPU control word. This function extracts selected exception flags from the FPU status word.

Parameters:

Return value: Bit set of FPU exception flags (see exception flags).

FPU exceptions summary status flag

C/C++bool GetErrorSummaryFlag (fpu_stat_t sword);

Description: The exception summary status flag is set when any of the unmasked exception flags are set. When the ES flag is set, the FPU exception handler is invoked. This function extracts exceptions summary flag from the FPU status word.

Parameters:

  • sword - FPU status word

Return value: Exceptions summary status flag.

Note: If an exception flag is masked, the FPU will still set the appropriate flag if the associated exception occurs, but it will not set the ES flag.

FPU stack fault flag

C/C++bool GetStackFaultFlag (fpu_stat_t sword);

Description: The stack fault flag indicates that stack overflow or stack underflow has occurred with data in the FPU data register stack. The FPU explicitly sets the SF flag when it detects a stack overflow or underflow condition, but it does not explicitly clear the flag when it detects an invalid-arithmetic-operand condition. This function extracts stack fault flag from the FPU status word.

Parameters:

  • sword - FPU status word

Return value: Stack fault flag.

FPU stack top index

C/C++int GetStackTop (fpu_stat_t sword);

Description: The FPU instructions treat the eight FPU data registers as a register stack. All addressing of the data registers is relative to the register on the top of the stack. The register number of the current top-of-stack register is stored in the TOP field in the FPU status word. This function extracts stack top index from the status word.

Parameters:

  • sword - FPU status word

Return value: Index of the FPU top of the stack register [0-7].

Copyright 2012-2018 Jack Black. All rights reserved.