Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user maak ! (Registered 2024-04-18) You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > fastest or smallest 'switch' statment
2015-09-05 15:02
mhindsbo
Account closed

Registered: Dec 2014
Posts: 50
fastest or smallest 'switch' statment

I use the following code a lot to switch between two values and was wondering what others do and if there is a faster, smaller or simply more elegant way someone has come up with.
      lda #value1    ; default value: AR = value1
      ldy switch     ; get value of switch in YR 
      cpy #case1     ; compare switch 
      beq @cont
      lda #value2    ; if switch != #case1 then AR = value2

@cont ...            ; AR = value1/value2 depending on switch
 
... 14 posts hidden. Click here to view all posts....
 
2015-09-06 23:56
mhindsbo
Account closed

Registered: Dec 2014
Posts: 50
thanks all! some good input. I'm glad I asked.
2015-09-10 14:07
Fred

Registered: Feb 2003
Posts: 284
There is also a way of doing this without using any branch instruction or jump table.

Certainly not the fastest and also not the smallest code on a 6510 CPU:
lda switch
cmp #case1
php
pla
lsr
and #$01
eor #$ff
adc #$00
and #value1 - value2
clc
adc #value2
This can be optimized a bit by using an undocumented instruction:
lda switch
cmp #case1
php
pla
asr #$02
eor #$ff
adc #$01
and #value1 - value2
clc
adc #value2
For the 6510 cpu it doesn't matter a lot to avoid branches. For e.g. Intel based CPUs it does matter in certain cases. Some compilers will optimize the following:
if (condition) {
  value = 12;
} else {
  value = 34;
}
into this:
value = - Integer(condition) and (12 - 34) + 34;
Btw, the example of Bitbreaker using ARR #$00 will not work since the zero flag isn't taken into account.
2015-09-10 14:17
Bitbreaker

Registered: Oct 2002
Posts: 498
Quoting Fred
Btw, the example of Bitbreaker using ARR #$00 will not work since the zero flag isn't taken into account.


As said, depending on the values: If value1 < value2 then carry will be either set on equal or cleared on not equal, just fair if you need to differ two cases with static values only.
2015-09-10 14:54
Fred

Registered: Feb 2003
Posts: 284
True. With the right values it is a nice and short solution.
2015-09-11 13:13
Bitbreaker

Registered: Oct 2002
Posts: 498
Quoting Fred

lda switch
cmp #case1
php
pla
asr #$02
eor #$ff
adc #$01
and #value1 - value2
clc
adc #value2

value = - Integer(condition) and (12 - 34) + 34;



Wouldn't this also be the same?
lda switch
cmp #case1
php
pla
asr #$02
sbc #$00              ;results in either $00 or $ff
and #value2 - value1
clc
adc #value1


Where we take:
value = (Integer(condition) - 1) and (34 - 12) + 12;
2015-09-12 06:18
Fred

Registered: Feb 2003
Posts: 284
Nice one.

Another optimization that can be done is when the AND uses a value of less than 128, the ANC instruction can be used instead so that the CLC afterwards can be removed.
2015-09-12 10:58
lft

Registered: Jul 2007
Posts: 369
Or, you know, don't use addition in the first place.

; accumulator is either 00 or ff
and #value2 ^ value1
eor #value1
2015-09-12 11:09
soci

Registered: Sep 2003
Posts: 473
BB/Fred: Ok, great. As mentioned earlier there are no pipeline stalls to avoid here, and that switching construct is suboptimal in every way.

What's next, how to avoid cache line bouncing on large multi processor 6502 systems? Various synchronization primitives for my threaded code? Pre-fetching? How to optimize unaligned access? Use of barriers for memory mapped I/O?
2015-09-12 11:27
Bitbreaker

Registered: Oct 2002
Posts: 498
Sure, but optimising is fun :-D
2015-09-12 11:39
soci

Registered: Sep 2003
Posts: 473
Yes, no problem with that. But it seemed quite a bit pointless, and then it was pushed even further ;)
Previous - 1 | 2 | 3 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
hedning/G★P
Acidchild/Padua
Bacchus/FairLight
Sentinel/Excess/TREX
sln.pixelrat
Martin Piper
Guests online: 140
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Wonderland XIV  (9.6)
9 The Ghost  (9.6)
10 Bromance  (9.6)
Top onefile Demos
1 It's More Fun to Com..  (9.9)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 Rainbow Connection  (9.5)
6 TRSAC, Gabber & Pebe..  (9.5)
7 Onscreen 5k  (9.5)
8 Dawnfall V1.1  (9.5)
9 Quadrants  (9.5)
10 Daah, Those Acid Pil..  (9.5)
Top Groups
1 Oxyron  (9.3)
2 Nostalgia  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Crackers
1 Mr. Z  (9.9)
2 S!R  (9.9)
3 Mr Zero Page  (9.8)
4 Antitrack  (9.8)
5 OTD  (9.8)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.04 sec.