In order to more closely match a Matlab implementation, much of our low-frequency work is done in floats. However, the conversion of floating point numbers to fract32s is emerging as a problem.

The default rounding done when converting from float (single) to fract32 appears to be of the type "_float32_to_int32_round_to_zero". As I understand it from documentation elsewhere, this means that the conversion is performed according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic, except that the conversion is always rounded toward zero.

Is there a way to change this default? In particular, the Matlab implementation I need to match always rounds *away* from zero.

Thanks,

Dana Good

Please find attached a Matlab script that can be used to configure Matlab's Fixed-Point toolbox to behave in a similar way to the Blackfin architecture. The way rounding is performed is determined by the RoundMode property. Setting the RoundMode to convergent is equivalent to Blackfin's round-to-nearest with unbiased rounding (the default mode), setting it to round is equivalent to Blackfin's round-to-nearest with biased rounding and setting it to fix is equivalent to truncation (round-to-zero).

An example on how the fractional types defined in the script can be used:

init_Blackfin_fract()

global fr16 fr32 fr40

global op16b op32b op40b op16u op32u op40u op16t op32t op40t

x=(2^-3+2^-16); % least significant bit of a fract16 not set: 0x1000 1

y=(2^-3+2^-15+2^-16); % least significant bit of a fract16 set: 0x1001 1

% expected result: 0x1001, 0x1000, 0x1000

fi(x,fr16,op16b), fi(x,fr16,op16u), fi(x,fr16,op16t)

% expected result: 0x1002, 0x1002, 0x1001

fi(y,fr16,op16b), fi(y,fr16,op16u), fi(y,fr16,op16t)

When converting from floating point to fixed point on Blackfin, one should make use of the following conversion routines:

#include <fract2float_conv.h>

fract32 float_to_fr32 (float _x);

fract16 float_to_fr16 (float _x);

float fr32_to_float (fract32 _x);

float fr16_to_float (fract16 _x);

These are faster than computing (short) (float_val * 32768.0) for a fract16 for example while the converted result is the same (using truncation for rounding).