Chapter 2
C Language Reference
©
National Instruments Corporation
2-35
Fixed-Point Conversion and Arithmetic Macros
Although this section explains different fixed-point operations in terms of
macros, all of these operations are supported as functions in the function
interface. Hence, in the following sections the term
macro
can be
completely substituted by the term
function
.
Three types of fixed-point macros are generated by AutoCode:
•
Conversion macros that convert one type of number to another. Refer
to the
section.
•
Arithmetic macros that perform addition, subtraction, multiplication,
or division. Refer to the
section.
•
Relational macros that compare two numbers and produce a Boolean
result. Refer to the
section.
Conversion Macros
Conversion macros are used for converting from one fixed-point data type
to another, from floating-point to fixed-point or from fixed-point to
floating-point, or from integer to fixed-point or from fixed-point to integer.
These macros in turn make use of the left-shift or right-shift macros defined
in
sa_fxprv.h
.
The right-shift macro shifts the bits of a value to the right. When a negative
number is shifted right, it results in flooring of that number instead of
truncating because of the two’s complement scheme of representing
negative numbers. Therefore, the right-shift macro with truncate behavior
checks for a negative number and does the needed adjustment to produce
truncation.
Whenever a fixed-point number needs to be aligned, a right-shift macro can
be called. Addition, subtraction, multiplication, division, and the relational
macros all make use of the alignment macros. Therefore, the right-shift
macro can be heavily used. If you can accept floor behavior for negative
numbers, you could replace the truncate macro with the floor macro, which
can increase the execution speed of the generated code. To do so, modify
the implementation of the (shift-right) macros
SHRsbpt
,
SHRsspt
, and
SHRslpt
in the
sa_fxprv.h
file to perform a simple right-shift operation
as follows:
#define SHRsbpt(x,y) ((x) >> (y))
#define SHRsspt(x,y) ((x) >> (y))
#define SHRslpt(x,y) ((x) >> (y))