And can now use different magic numbers to give different rounding modes:
'setup data types and unions for rounding to short
union float_to_short
_float_in as double
type
_pad as short
_short_out as short
end type
end union
'magic numbers for different rounding modes
dim as const double floor_float_to_short = 2^35 + 2^36
dim shared as double ceil_float_to_short = 2^35 + 2^36
*cast ( integer pointer , @ceil_float_to_short ) = 65535
dim shared as double nearest_float_to_short = 2^35 + 2^36
*cast ( integer pointer , @nearest_float_to_short ) = 32768
'rounding float to short macros
dim shared as float_to_short round_to_short
#macro round_down(my_float,my_short)
round_to_short._float_in = floor_float_to_short + my_float
my_short = round_to_short._short_out
#endmacro
#macro round_up(my_float,my_short)
round_to_short._float_in = ceil_float_to_short + my_float
my_short = round_to_short._short_out
#endmacro
#macro round_nearest(my_float,my_short)
round_to_short._float_in = nearest_float_to_short + my_float
my_short = round_to_short._short_out
#endmacro
'**********************************************************************
I'd prefer to make them all consts but I can't see how to put in the data, I guess it won't make much difference anyway since const floats aren't assembled the same way as const ints.
The 'nearest' will always round down xx.5
If you change 32767 to 32768 it'll always round up xx.5
I've still to try it in a real situation but it looks like it's possible to do the interpolation including perspective correction with the magic number added beforehand with the fpu almost acting like 16:16 fixed point and the padding could be split into 2 bytes with the high byte giving the fraction for bi-linear filtering.