English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 29 October 2021, 04:34   #1
Jobbo
Registered User
 
Jobbo's Avatar
 
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
Not terrible, but I was curious if anyone knows of any alternatives?
Jobbo is offline  
Old 29 October 2021, 10:06   #2
hooverphonique
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).
hooverphonique is offline  
Old 29 October 2021, 10:42   #3
Thomas Richter
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.
Thomas Richter is offline  
Old 13 December 2021, 23:05   #4
Photon
Moderator
 
Photon's Avatar
 
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.
Photon is offline  
 


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 22:23.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.09335 seconds with 13 queries