English Amiga Board


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

 
 
Thread Tools
Old 21 August 2024, 07:47   #21
morbid
Registered User
 
Join Date: Aug 2020
Location: Huddinge
Posts: 26
If you make sure your dx and dy are between -128 and 127, and have lots of memory you could do something like:

Code:
; d0 = dy
; d1 = dx
; a0 = pointer to middle of table with high byte
; a1 = pointer to middle of table with low byte
; result in d2
lsl.w #8,d0
move.b d1,d0
move.b (a0,d0.w),d2
lsl.w #8,d2
move.b (a1,d0.w),d2
morbid is offline  
Old 21 August 2024, 10:21   #22
Rotschi
Registered User
 
Join Date: Apr 2023
Location: "Hamcastle"
Posts: 30
I do not know if it is of interest for you, but you could also try to eliminate the divu in your posted code by simply replacing the division with a multiplication of the reciprocal (catchword: lut).

Last edited by Rotschi; 21 August 2024 at 10:37.
Rotschi is offline  
Old 21 August 2024, 20:45   #23
dansalvato
Registered User
 
Join Date: Jun 2009
Location: United States
Posts: 67
Here is my atan2 function: https://gist.github.com/dansalvato/7...59eca30583716e

Code:
; atan2: Returns the angle from origin to the given XY coordinate.
; Angle is from 0-255 (assume a 256-degree circle).
; d0: X value
; d1: Y value
atan2:
            moveq       #32,d3
            tst.w       d0
            blt         .xneg
.xpos:      tst.w       d1
            blt         .quad4
.quad1:     moveq       #0,d2
            bra         .0
.xneg:      tst.w       d1
            blt         .quad3
.quad2:     moveq       #64,d2
            neg.w       d0
            exg         d0,d1
            bra         .0
.quad3:     moveq       #-128,d2
            neg.w       d0
            neg.w       d1
            bra         .0
.quad4:     moveq       #-64,d2
            neg.w       d1
            exg         d0,d1
.0:         cmp.w       d3,d0
            blo         .1
.0b:        lsr.w       #1,d0
            lsr.w       #1,d1
            cmp.w       d3,d0
            bhs         .0b
.1:         cmp.w       d3,d1
            blo         .2
.1b:        lsr.w       #1,d0
            lsr.w       #1,d1
            cmp.w       d3,d1
            bhs         .1b
.2:         lsl.w       #5,d1
            add.w       d0,d1
            add.b       AtanTable(pc,d1.w),d2
            move.l      d2,d0
            rts
Feature breakdown:
  • No divides
  • No use of address registers
  • 256-degree circle
  • 1,024 byte lookup table (32x32), 8-bit table entries
  • Return value is longword signed

It balances speed, accuracy, and memory usage—not highly optimized for one specific metric. The lookup table is for one quadrant of the circle, which is "mirrored" depending on the sign of the coordinates.

There is a loop to shift the XY coordinates down to <32 each, which can probably be made slightly more efficient. For the time being, it's good enough for my purposes.

The whole function takes 136-146 cycles (including rts) depending on quadrant, plus ~32 cycles for each shift required to reach <32 on the larger value.
dansalvato is offline  
Old Yesterday, 10:22   #24
PaulF
Registered User
 
Join Date: Jan 2021
Location: UK
Posts: 13
Quote:
Originally Posted by dansalvato View Post
Here is my atan2 function: https://gist.github.com/dansalvato/7...59eca30583716e

Code:
; atan2: Returns the angle from origin to the given XY coordinate.
; Angle is from 0-255 (assume a 256-degree circle).
; d0: X value
; d1: Y value
atan2:
            moveq       #32,d3
            tst.w       d0
            blt         .xneg
.xpos:      tst.w       d1
            blt         .quad4
.quad1:     moveq       #0,d2
            bra         .0
.xneg:      tst.w       d1
            blt         .quad3
.quad2:     moveq       #64,d2
            neg.w       d0
            exg         d0,d1
            bra         .0
.quad3:     moveq       #-128,d2
            neg.w       d0
            neg.w       d1
            bra         .0
.quad4:     moveq       #-64,d2
            neg.w       d1
            exg         d0,d1
.0:         cmp.w       d3,d0
            blo         .1
.0b:        lsr.w       #1,d0
            lsr.w       #1,d1
            cmp.w       d3,d0
            bhs         .0b
.1:         cmp.w       d3,d1
            blo         .2
.1b:        lsr.w       #1,d0
            lsr.w       #1,d1
            cmp.w       d3,d1
            bhs         .1b
.2:         lsl.w       #5,d1
            add.w       d0,d1
            add.b       AtanTable(pc,d1.w),d2
            move.l      d2,d0
            rts
Feature breakdown:
  • No divides
  • No use of address registers
  • 256-degree circle
  • 1,024 byte lookup table (32x32), 8-bit table entries
  • Return value is longword signed

It balances speed, accuracy, and memory usage—not highly optimized for one specific metric. The lookup table is for one quadrant of the circle, which is "mirrored" depending on the sign of the coordinates.

There is a loop to shift the XY coordinates down to <32 each, which can probably be made slightly more efficient. For the time being, it's good enough for my purposes.

The whole function takes 136-146 cycles (including rts) depending on quadrant, plus ~32 cycles for each shift required to reach <32 on the larger value.
That's great. Thankyou.
PaulF 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
"Voices8" 8 Channel Soundtracker "DemoSongI" song - "This is the Amiga with 8 Voices" DemosongIHunter request.Music 45 23 May 2022 20:07
Most "recent" and "modern" IDE to write in C/C++/assembler on the Amiga? guybrush Coders. General 11 27 February 2022 22:26
[FOUND: PHANTASIE III!] 2D RPG game with map screen, "black background combat screen" Artifex 28 Looking for a game name ? 1 22 May 2014 17:42
RTG "full-screen" vs. "full windowed" - advantages/disadvantages? ral-clan support.WinUAE 2 19 April 2011 01:15
The 6502 assembler code in "The Terminator" (1984) Shoonay Nostalgia & memories 2 15 May 2009 13:52

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 12:18.

Top

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