Hi,
is there a way to make use of MMX/SSE to do calculations like 'a mod b'
where 'a' and 'b' are integers?
I haven't found any useful instruction for such a task. Maybe this can be
done using floating point arithmetic. If yes - how?
Thanks,
Tom
|
|
0
|
|
|
|
Reply
|
T
|
5/28/2006 11:53:33 PM |
|
T.Kaz. wrote:
> Hi,
>
> is there a way to make use of MMX/SSE to do calculations like 'a mod b'
> where 'a' and 'b' are integers?
>
> I haven't found any useful instruction for such a task. Maybe this can be
> done using floating point arithmetic. If yes - how?
Yes, it can:
First, use the approximate reciprocal lookup to generate a set of
starting values, then one or two NR iterations (also in SIMD fp mode) to
get a more precise value.
Do the divisions in SIMD fp by multiplication by the reciprocal, then do
a parallel convert of these approximate/fp results to int.
Finally, you should multiply each of these results (which will either be
correct or maximum one off) by the original divisors, subtract, and
check the remainders:
If negative (assuming all values positive to start with), subtract one
from the result, add divisor to remainder.
If >= divisor, add one to result, subtract divisor from remainder.
Both of these can of course also be done easily on SIMD values with
parallel compares (which generate 0 or -1 masks), then using the mask to
adjust the result, PAND in the divisor, and adjust the remainder!
This will give you both division result and remainder at the same time,
and it should be significantly faster than a single integer MOD operation.
OK?
Terje
--
- <Terje.Mathisen@hda.hydro.com>
"almost all programming can be viewed as an exercise in caching"
|
|
0
|
|
|
|
Reply
|
Terje
|
5/29/2006 4:55:12 AM
|
|