Thanks, very good points about the rounding. I needed it for an Euclidean distance and rounding problem was negligible for me, so I didn't catch it before.
The code is on the MIT license so feel free to use it or add it to your benchmark.
So you need an integer SQRT function, but which to choose? Here is my implementation that is using 2 tables of 256 bytes and binary search. The code is in MADS assembler syntax for table generation.
; 2 tables with low and high bytes of all x*x values squared_table_lo: :256 dta l(#*#) squared ...
that's the shortest one, indeed. Great job, thank you! Hold on there, buddy! mul: ; result16 = factor8 * 16 lda value sta result lda #$10 mul2: asl result rol bcc mul2 sta result+1 rts Wow, you are a 6502 wizard! :-) As the others pointed, in such sizecoding everything depends on what ...
Are you sure the code is correct? With either 4 or 5 ROLs it's giving a wrong result.
The code I tested was right, but that was a hurried re-write after my first go, and I didn't edit the post correctly. Swap the AND #15 and AND #240 and it should work better.
I'm looking for a way to shorten (by the size, not the cycles) a procedure that is performing multiplication of 8bit value to 16bit result (where the result address is not overlapping the value address). So far I got it down to 21 ($15) bytes. Any ideas how to shorten it more?
Thank you all for great answers so far! I'll need some time to process them to find the best approach to my cases. For sure idea of using logarithms is a good one and worth checking from accuracy perspective. Scaling is multiplication and there are some very fast multiplication routines e.g. https ...
What is the best (fast) method on 6502 to make a scaling down of specific 8bit value by other 8bit value. Lets say I have byte value A and I want to scale it by [0,1) in form of byte value B ($0-$FF, so $80 is 0.5).
scaled= A * B (hex%)
A = $C0 B = $80 scaled = $C0 * $80 / $100 scaled ...
Hi,
I wrote an article about optimizing C code for 6502, aiming CC65 compiler. I think it may be useful also for other programming environments, therefore sharing it here too. Enjoy the reading! All comments are welcome. https://github.com/ilmenit/CC65-Advanced-Optimizations