Title  fxpemu (Fixedpoint emulation library for ANSI C) 
Author  Nikolaos Kavvadias (C) 2010, 2011, 2012, 2013, 2014 
Contact  nikos@nkavvadias.com 
Website  http://www.nkavvadias.com 
Release Date  05 October 2014 
Version  0.1.0 
Rev. history  
v0.1.0  20141005 Updated for first public release on Github. 
v0.0.4  20130406 Standalone version. 
v0.0.3  20110427 Changes to fxp_to_int32. Added fxp_ceil. 
v0.0.2  20101124 Added fxp_to_double. 
v0.0.1  20101123 Added fxp, fxp_resize. 
v0.0.0  20101122 Initial version. Includes the definitions, declarations and implementations of:

fxpemu is an ANSI C library that implements basic fixedpoint arithmetic data structures alongside a usable side. Its purpose is to be used in the context of software and hardware compilers and code generators.
The implementation of the fixed struct, the "overflow" and "rounding" routines, and the general form of fixed_xyz routines is based on the following reference:
The draft of the reference paper is available (as of 2014Sep20) from:
It should be noted that the additional functionality is based on newlydesigned code.
The fxpemu abstract data types and API code base includes the following files:
/fxpemu  Toplevel directory 
AUTHORS  List of authors. 
LICENSE  License argeement (Modified BSD license). 
Makefile  GNU Makefile for building testfxpemu.exe. 
README.rst  This file. 
README.html  HTML version of README. 
README.pdf  PDF version of README. 
VERSION  Current version. 
fxpemu.c  C code implementing the fixedpoint arithmetic API. 
fxpemu.h  C header file for the above. Also defines data structures along some arithmetic macros needed. 
rst2docs.sh  Bash script for generating the HTML and PDF versions. 
testfxpemu.c  Application code for exercising basic functionality of the implemented fixedpoint arithmetic API. 
This section provides a quick reference of the functions used for implementing the fxpemu data structures (enums, structs and basic macros) and API.
This enum provides definitions for the possible quantization (truncation or rounding) modes in fixedpoint arithmetic. It is defined as follows:
typedef enum { UNKNOWN_QUANT_TYPE = 1, AC_TRN, /* Default in ACDT (Algorithmic C Datatypes). */ AC_TRN_ZERO, AC_RND, /* Default in VHDL fixedpoint package. */ AC_RND_ZERO, AC_RND_INF, /* Implemented as "SC_RND" in SystemC 2005. */ AC_RND_MIN_INF, AC_RND_CONV } QuantizationType;
This enum provides definitions for the possible overflowhandling (wrapping or saturation) modes in fixedpoint arithmetic. It is defined as follows:
typedef enum { UNKNOWN_OVERFLOW_TYPE = 1, AC_WRAP, /* Default in ACDT (Algorithmic C Datatypes). */ AC_SAT, /* Default in VHDL fixedpoint package. */ AC_SAT_ZERO, AC_SAT_SYM } OverflowType;
This struct defines the fixed data structure which essentially is the container of a fixedpoint value. It consists of the val` (value), ``wl (word length), iwl (integer word length), lbp (location of binary point), and the overflow and rounding flags. The fixed struct is defined as follows:
typedef struct { int val; /* 32bit value value of the number */ int wl; /* Word length, in bits */ int iwl; /* Integer word length, in bits */ int lbp; /* Location of binary point, in bits */ int overflow; int rounding; } _fixed; typedef _fixed fixed;
The FIXED_INIT macro initializes a fixedpoint variable x to a set of given values: w, i, l, ovr, rnd according to the definition of the fixed struct. It is defined as follows:
#define FIXED_INIT(x, w, i, l, ovr, rnd) \ x.wl = w; \ x.iwl = i; \ x.lbp = l; \ x.overflow = ovr; \ x.rounding = rnd
Apply fixedpoint arithmetic quantization rules to fixed r. These rules are used for handling the lowsignificance bits of r.
Apply fixedpoint arithmetic overflow rules to fixed r. These rules are used for handling the highsignificance bits of r.
Constructor for a signed fixedpoint variable.
Constructor for a signed fixedpoint variable. This version is provided for compatibility to a thirdparty tool/plugin, namely the Agility RMS.
This constructor cannot set the overflow mode. The overflow mode of AC_SAT is used by default, and can be changed by explicit modification of the "overflow" field of a fixedpoint variable. An "offset" (for establishing.
"is_signed" is currently left unused.
Extends or shrinks the number of bits left or right of the binary of a specified "fixed" arithmetic type. This version uses an additional specifier, "sign" which can take the values of 'u' or 's'. For proper use, the specified sign must be the same to that supposed for fixed r.
The location of the binarypoint (LBP(.)) is not affected.
Fixedpoint addition.
Fixedpoint subtraction.
Fixedpoint multiplication.
Fixedpoint division.
Convert a signed int to a fixedpoint value.
Convert a fixedpoint value to a signed int.
Fixedpoint negation.
Fixedpoint absolute value.
Fixedpoint minimum of two numbers.
Fixedpoint maximum of two numbers.
Converts a fixedpoint number of the form i.f to a string, assuming arithmetic in base b.
Prints the argument as a fixed point i.f number in base b.
Reads a string representing a bbase (b=2 for binary) fixedpoint number and performs the conversion to an actual "fixed"type number.
Converts a double (64bit floatingpoint) to a binary fixedpoint number with possible loss of precision.
Converts a binary fixedpoint number (actually its integer emulation) to the corresponding double (64bit floatingpoint) representation.
Fixedpoint ceiling (rounding to positive infinity).
The implementation of the fixedpoint arithmetic API can be used in context of a provided test application, named testfxpemu.c. The Makefile can be used for building this application as follows:
This will also build the static library implementation of fxpemu, which is the libfxpemu.a file. Thirdparty/user applications can be implemented by including the fxpemu.h header file and statically linking to the library.
To run the test application do the following:
Executing the application will produce a stream of diagnostic messages to standard output.
Standard UNIXbased tools (tested with gcc4.6.2 on MinGW/x86 and gcc4.8.2 on Cygwin/x86/Windows 7)
On Windows (e.g. Windows 7, 64bit), MinGW (http://www.mingw.org) or Cygwin (http://sources.redhat.com/cygwin) are suggested.
The sources should be able to compile without any messages on any recent Linux distribution.