29 October 2021, 04:34 | #1 |
Registered User
Join Date: Jun 2020
Location: Druidia
Posts: 387
|
Fast rounding code?
Hi
I'm working on something where I need to efficiently round down to the nearest multiple for different rounding values. This is on 68000 and the rounding values are 2 through 16. Obviously for the like of 2, 4, 8, 16 it's easy enough to mask the lowest bits. But for the other values I'm not aware of any tricks I can pull. The best I've come up with is to use a table, which should be fine because the range of numbers I'm rounding is only 0...319. Using a table the code ends up as: Code:
add.w d0,d0 move.w (a0,d0.w),d0 |
29 October 2021, 10:06 | #2 |
ex. demoscener "Bigmama"
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,624
|
If you by "efficiently" mean "as fast as possible", a table is the best you can do (for any number between 2 and 16).
|
29 October 2021, 10:42 | #3 |
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,215
|
There are a couple of cases (2^n-1) where simple algorithms exist, but in general, it is either a table or a division and a multiplication.
|
13 December 2021, 23:05 | #4 |
Moderator
Join Date: Nov 2004
Location: Eksjö / Sweden
Posts: 5,602
|
Late reply, but the scholarly answer is that in binary arithmetic, to preserve precision you work in at least 5 bits higher resolution. Only at the end of the calculation do you round.
Rounding is done by a half-add (minus 1) and a shift down. In the example of 32x precision, you add 15 and shift down by 5. |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Fast switch statement code | Jobbo | Coders. Asm / Hardware | 10 | 22 October 2021 19:13 |
Rounding differences in mathieee and 68k FPUs | phx | Coders. System | 15 | 26 July 2021 08:33 |
SECTION CODE - Problem with chunks and FAST ram | KONEY | Coders. Asm / Hardware | 18 | 12 July 2021 15:17 |
Minimal/fast code for a Guru Meditation | prb28 | Coders. Asm / Hardware | 4 | 12 August 2018 10:28 |
FPU rounding issues | phx | support.WinUAE | 6 | 12 November 2014 10:03 |
|
|