English Amiga Board


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

 
 
Thread Tools
Old 03 March 2020, 21:06   #181
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
Does optimisation reduce readability ?
Not always, but sometimes yes. However you should notice that at the time of doing it. And then it might be a good idea to add some comment explaining what you've done, so it doesn't get abused later.
meynaf is offline  
Old 03 March 2020, 23:27   #182
VladR
Registered User
 
Join Date: Dec 2019
Location: North Dakota
Posts: 741
Quote:
Originally Posted by StingRay View Post
Optimising doesn't necessarily equals unreadable!




And neither does it mean the code is unmaintainable.
It's probably just my patience (or lack thereof), but to me, unmaintainable means that I have to spend more than 30 seconds figuring out what I did there. It's perhaps just side effect of getting older
VladR is offline  
Old 03 March 2020, 23:34   #183
VladR
Registered User
 
Join Date: Dec 2019
Location: North Dakota
Posts: 741
Quote:
Originally Posted by meynaf View Post
Does optimisation reduce readability ?
Not always, but sometimes yes. However you should notice that at the time of doing it. And then it might be a good idea to add some comment explaining what you've done, so it doesn't get abused later.
That's a Catch-22 right there for me unfortunately

But, on 1.79 MHz 6502, the approach I found works best is wrapping each version in a separate compile-time block with two lines of description of the changes to the previous one.

One such texturing method received about dozen versions and it never takes more than 15 minutes to understand them.

But if I had just the last one, it would be merely WTF moment

I'm all ears how you guys handle this.
VladR is offline  
Old 04 March 2020, 00:49   #184
Galahad/FLT
Going nowhere
 
Galahad/FLT's Avatar
 
Join Date: Oct 2001
Location: United Kingdom
Age: 50
Posts: 8,986
Quote:
Originally Posted by VladR View Post
That's a Catch-22 right there for me unfortunately

But, on 1.79 MHz 6502, the approach I found works best is wrapping each version in a separate compile-time block with two lines of description of the changes to the previous one.

One such texturing method received about dozen versions and it never takes more than 15 minutes to understand them.

But if I had just the last one, it would be merely WTF moment

I'm all ears how you guys handle this.
Always comment your code
Galahad/FLT is online now  
Old 04 March 2020, 08:43   #185
StingRay
move.l #$c0ff33,throat
 
StingRay's Avatar
 
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 6,863
Quote:
Originally Posted by VladR View Post
It's probably just my patience (or lack thereof), but to me, unmaintainable means that I have to spend more than 30 seconds figuring out what I did there. It's perhaps just side effect of getting older

That can be avoided by properly commenting the code. I guess each of us has a different definition what unmaintainable means, in the end it is important that, no matter how much you optimise code, you should still be able to understand why/how something has been done.
StingRay is offline  
Old 04 March 2020, 11:19   #186
hooverphonique
ex. demoscener "Bigmama"
 
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,624
Interesting and relevant, but I think we're getting off topic here
hooverphonique is offline  
Old 12 December 2020, 00:10   #187
jotd
This cat is no more
 
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 52
Posts: 8,163
anyone took a look at this old page?

https://web.archive.org/web/20040905...ldin/main.html

algorithm

Code:
signum(x)
if x > 0 then return 1
else if x < 0 then return -1
else return 0
there's some optimization of signum which is baffling (and also computed automatically!). I think this is 68000

(x in D0)

Code:
ADD.L             D0,D0              (Add D0 to itself)
SUBX.L           D1,D1              (Subtract (D1 + Carry) from D1)
NEGX.L           D0                   (Put (0 – D0 – Carry) into D0)
ADDX.L           D1,D1              (Add (D1 + Carry) to D1
(signum(x) is now in D1)

Quote:
At first glimpse, this makes absolutely no sense. Most glaringly, there are no conditional jumps, while the original code was made of nothing but conditional jumps. Some closer analysis reveals what is actually going on.


ADD.L D0,D0 doubles D0. This is unimportant, as it will not be referenced again, but it takes advantage of a peculiarity of 2’s-complement notation, which is used to store number in registers in binary. Any negative number will be stored as a bit string beginning with 1; thus, when it is doubled, it will overflow, and the carry bit on the processor will be set. SUBX.L D1,D1 clears D1 (by subtracting it from itself), then subtracts the carry bit from it. Since this was set by the previous instruction, D1 is now -1 if the original number was negative, and 0 otherwise. NEGX.L D0 negates D0, which is not important, and sets the carry bit to 1 if and only if D0 was not originally equal to zero. ADDX.L D1,D1 now puts this entire process together by adding D1 to itself, then adding the carry bit. If D0 was originally positive, D1 will be zero and the carry bit will be set. Thus, 0 + 0 + 1 = 1, and D1 will contain 1. If D0 was equal to zero, D1 will be equal to zero and the carry bit will not be set, so 0 + 0 + 0 =0. If D0 was negative, D1 will equal -1 and the carry bit will be set, so -1 + -1 + 1 = -1.

This code is bizarre in ways rarely seen, and would be nearly unreadable to anyone who did not know what was going on. Yet, it works every time, and is considerably faster than the original. Other code fragments generated by Superoptimizer as just as cryptic, but always work perfectly and more efficiently than their predecessors.
Seems that an automated way of doing this is possible. If it is and there's a 68k version somewhere, it could be very interesting for our old machines...
jotd is online now  
Old 12 December 2020, 09:51   #188
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
Quote:
Originally Posted by jotd View Post
there's some optimization of signum which is baffling (and also computed automatically!). I think this is 68000
SGN is a known one. See post #62.
meynaf is offline  
Old 12 December 2020, 13:08   #189
jotd
This cat is no more
 
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 52
Posts: 8,163
yeah it's exactly the same code. But it just illustrated the result of a machine optimizing asm. I fail to see how it works exactly but I'd like to see that in action.

Let's imagine some 3D games or otherwise slow games benefitting from this... (if it's not a blitter limitation)
jotd is online now  
Old 12 December 2020, 14:40   #190
DanScott
Lemon. / Core Design
 
DanScott's Avatar
 
Join Date: Mar 2016
Location: Tier 5
Posts: 1,211
Quote:
Originally Posted by jotd View Post
yeah it's exactly the same code. But it just illustrated the result of a machine optimizing asm. I fail to see how it works exactly but I'd like to see that in action.

Let's imagine some 3D games or otherwise slow games benefitting from this... (if it's not a blitter limitation)
I've never used a signum in any of my Amiga code

Don't think it will magically speed up many games to be honest.
DanScott is offline  
Old 12 December 2020, 15:12   #191
Antiriad_UK
OCS forever!
 
Antiriad_UK's Avatar
 
Join Date: Mar 2019
Location: Birmingham, UK
Posts: 418
Quote:
Originally Posted by DanScott View Post
I've never used a signum in any of my Amiga code

Don't think it will magically speed up many games to be honest.
I've got a few routines that do something whether a value is -ve/0/+ve but it's always within a loop where just bmi/beq are easier to do.

I've never used negx. Trying to think of a useful scenario for it. I guess you could save a couple of cycles if you were counting the number of zeros in a steam or something:

Code:
	moveq	#0,d2	;number of non zeros
	moveq	#0,d1	;leave 0
	...
	moveq	#0,d0	;get number to test
	negx.w	d0	;carry set unless 0
	addx.w	d1,d2	;Increase count by 1 if non zero
.notzero:
vs
Code:
	moveq	#0,d2	;number of non zeros
	...
	moveq	#0,d0	;get number to test
	beq.s	.zero
	addq.w	#1,d2
.zero:
Edit: All above is wrong

Any other creative uses?

Last edited by Antiriad_UK; 12 December 2020 at 17:03.
Antiriad_UK is offline  
Old 12 December 2020, 16:03   #192
roondar
Registered User
 
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,409
The article isn't about signum though (that's just the example they picked to showcase what it's about). It's about a rather extreme way to optimise code where the optimizer doesn't use pre-designed optimisations but instead tries out every single combination of instructions that exists for that particular problem and leads to a valid result. Then the optimizer picks the smallest/fastest result out of the valid solutions it generated.

They point out it's only viable for very small algorithms as the required computing & storage power quickly spirals out of control. Still, an interesting approach.

Last edited by roondar; 12 December 2020 at 16:12.
roondar is offline  
Old 12 December 2020, 16:11   #193
DanScott
Lemon. / Core Design
 
DanScott's Avatar
 
Join Date: Mar 2016
Location: Tier 5
Posts: 1,211
i'm not sure negx.w does exactly what you want there
DanScott is offline  
Old 12 December 2020, 17:02   #194
Antiriad_UK
OCS forever!
 
Antiriad_UK's Avatar
 
Join Date: Mar 2019
Location: Birmingham, UK
Posts: 418
Quote:
Originally Posted by DanScott View Post
i'm not sure negx.w does exactly what you want there
Hmm you are right.
"Subtracts the destination operand AND the extend bit from zero"

wtf is it used for?
Antiriad_UK is offline  
Old 12 December 2020, 17:21   #195
DanScott
Lemon. / Core Design
 
DanScott's Avatar
 
Join Date: Mar 2016
Location: Tier 5
Posts: 1,211
Quote:
Originally Posted by Antiriad_UK View Post
wtf is it used for?
a sigma optimisation only currently

I don't think I've used it, but probably there are some good use cases for it
DanScott is offline  
Old 12 December 2020, 17:23   #196
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,215
Quote:
Originally Posted by Antiriad_UK View Post
wtf is it used for?
For higher precision arithmetic. To negate a multi-byte/word/longword number, you start by negating the least significant field with "NEG", and then work upwards to the more significant digits by "NEGX".
Thomas Richter is offline  
Old 12 December 2020, 17:24   #197
jotd
This cat is no more
 
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 52
Posts: 8,163
you're getting me wrong

I mean: if there's a tool that can super-optimize people code automatically, then using it on some existing code (3D for instance) could speed it up. For instance 3D games, or games requiring a lot of CPU. Signum was just an example.
jotd is online now  
Old 12 December 2020, 17:48   #198
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
This kind of optimizer dies as soon as the code is more than just a few lines. For this reason i doubt it's of any use for 3d (or any game at all).
meynaf is offline  
Old 12 December 2020, 18:03   #199
jotd
This cat is no more
 
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 52
Posts: 8,163
that was just too good to be true
seemed to rely on bruteforce check of all combinations... but maybe this kind of optimizer is able to work by blocks to reduce combinatorics. Combining that and winuae hotspots to know where the code spends the most of the time...
jotd is online now  
Old 05 June 2021, 18:33   #200
jotd
This cat is no more
 
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 52
Posts: 8,163
I saw that in Supercars 2

Code:
		move.l	a0,(a4)+	;mask
		move.l	a1,(a4)+	;data0
		move.l	a2,(a4)+	;scn0
wouldn't it be faster to use

Code:
   movem.l a0-a2,(a4)+
(and would it be equivalent) ? Maybe there's a threshold where movem is faster. Is it 2, 3, or more registers?
jotd is online now  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
68000 boot code billt Coders. General 15 05 May 2012 20:13
Wasted Dreams on 68000 sanjyuubi support.Games 5 27 May 2011 17:11
680x0 to 68000 Counia Hardware mods 1 01 March 2011 10:18
quitting on 68000? Hungry Horace project.WHDLoad 60 19 December 2006 20:17
3D code and/or internet code for Blitz Basic 2.1 EdzUp Retrogaming General Discussion 0 10 February 2002 11:40

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 10:15.

Top

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