Nios II Custom Instruction User Guide

ID 683242
Date 4/27/2020
Document Table of Contents

6.6.2. Floating Point Hardware 2 Conversions

The FPH2 component provides functions for conversion between signed integer types (C short, int and long types) and 32-bit single-precision floating point types (C float type). The Nios II GCC compiler infers these hardware functions when compiled code converts data between these types, for example in C casting.

The FPH2 component does not provide functions for conversion between unsigned integer types and floating point. When converting between unsigned integer types and float types, the compiler implements software emulation. Therefore conversion to and from unsigned integers is much slower than conversion to and from signed integers.

If you do not need the extra range of positive values obtained when converting a float to an unsigned integer directly, you can use the FPH2 and avoid using the software emulation if you modify your C code to first cast the float type to an int type or long type and then cast to the desired unsigned integer type.

For example, instead of:
float f; 
unsigned int s = (unsigned int)f; // Software emulation
float f; 
unsigned int s = (unsigned int)(int)f; // FPH2
The FPH2 provides two operations for converting single-precision floating point values to signed integer values:
  • fixsi
  • round
The fixsi operation performs truncation when converting a float to a signed integer. For example, fixsi converts 4.8 to 4 and -1.5 to -1. GCC follows the C standard and invokes the fixsi operation whenever source code uses a cast or any time that C automatically converts a float to a signed integer.

The round operation performs Nearest Rounding (tie-rounds-away) when converting a float to a signed integer. For example, round converts 4.8 to 5 and -1.5 to -2. Software can invoke the round operation by calling the custom instruction directly, or by using the #define provided in system.h, which replaces the newlib lroundf() function.