#include /* Set the precision, ie. the maximum number of bits to the right of the 'binary' point */ #define PREC 8 /********************************************************************** Function : bit_limit Implements a precision limit on integers by regarding them as fixed point numbers with 'l' bits allowed to the left of the 'binary' point and 'r' bits to the right. If 'sgn_ext' is on then the MSB of the resulting truncated number is sign-extended. *********************************************************************/ int long bit_limit( int long l, /* Bits left of the 'binary' point */ int long r, /* Bits right of the 'binary' point */ int long in_val /* Input Value */ ) { int long mask; /* Build a mask which has 1's where data is valid */ mask = (((int long)1 << (l+r)) - 1) << (PREC-r); /* Mask the input data */ return mask & in_val; } /********************************************************************** Function : main Sample code to display the effect of limited precision fixed-point binary arithmetic. *********************************************************************/ int main(void) { float a_fl, b_fl, e_fl; int long A, B, C, D, E; /* Pick some arbitrary input values */ a_fl = 201.8; b_fl = 0.19; /* Convert a_fl and b_fl to justified integers and limit to 8 bits and 4 bits respectively */ A = bit_limit(8, 0, a_fl*(1<>PREC); /* Truncate D to 7 bits total */ D = bit_limit(6, 1, A>>2); /* Truncate the output to 9 bits */ E = bit_limit(8, 1, C+D); /* Re-scale to convert back to float */ e_fl = ((float)E)/(1<