reature request: fixed point
Jb
jb at nowhere.com
Sat Mar 1 19:54:25 PST 2008
"Walter Bright" <newshound1 at digitalmars.com> wrote in message
news:fqd3c0$12pt$1 at digitalmars.com...
> kov_serg wrote:
>> Is it possible to add to "Basic Data Types" one more type?
>>
>> fixed point type is intermediate type between fload and integer. Build in
>> support of fixed point type will great help in realtime signal and image
>> processing. 32 bit fixed point with 16bit for integer part and 16 bit of
>> fraction is very easy implemented on any 32bit CPU and have good
>> performance. Especially if CPU has no FPU.
>>
>> fixed x; // 32bit min=-0x8000.0000 max=0x7FFF.FFFF
>> ...
>> fixed a=3.14, b=2.3, c=-5.3;
>> fixed d=a*b/c;
>>
>> What you think about including "fixed" type in D 2.0?
>
> It isn't too hard to do fixed point arithmetic:
>
> typedef int Fixed;
> Fixed fixed(int m, int n) { return cast(Fixed)(m * 0x10000 + n); }
>
> Fixed a = fixed(3,14);
> Fixed b = fixed(2,3);
> Fixed c = -fixed(5,3);
> Fixed d = a * b / c;
The results of multiplies need to be shifted right, and divisions need to be
shifted left. Actualy for division it's best to shift the numerator left
before the division. But i dont see how your example would do that. Your
example should be...
(((a*b) >> 16) << 16) / c
In that case the shifts cancel out. But this..
(((a*b)+c) / d
Would have to be..
((((a*b) >> 16)+c) << 16) / d
And one problem is whether the compiler will return an int64 for a 32 bit
multiply, and then will it do the correct shift. I mean to multiply two
16.16 numbers you need at least a 48 bit result.
In assembler it should be somthing like...
MOV EAX,A
IMUL B
SHRD EAX,EDX,16
Is the compiler smart enough to generate that kind of code from...
d = (a*b) shr 16;
??
More information about the Digitalmars-d
mailing list