English Amiga Board


Go Back   English Amiga Board > Coders > Coders. General

 
 
Thread Tools
Old 12 August 2020, 10:46   #1
sandruzzo
Registered User
 
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,299
Need Help to round number

I'm using this shitty way to round number:

lsr.w #4,d1
lsl.w #4,d1

Is there a fast way beside table?
sandruzzo is offline  
Old 12 August 2020, 11:30   #2
a/b
Registered User
 
Join Date: Jun 2016
Location: europe
Posts: 1,039
Code:
and.w #-1<<4,d1    ; %1111111111110000
a/b is offline  
Old 12 August 2020, 14:28   #3
sandruzzo
Registered User
 
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,299
@a/b

Soucery! How does it work?
sandruzzo is offline  
Old 12 August 2020, 15:54   #4
hooverphonique
ex. demoscener "Bigmama"
 
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,624
your shifting exercise does nothing but clear the lower 4 bits ;-)

edit: well, it also sets some status flags which you aren't using anyway, so...
hooverphonique is offline  
Old 12 August 2020, 16:03   #5
sandruzzo
Registered User
 
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,299
Quote:
Originally Posted by hooverphonique View Post
your shifting exercise does nothing but clear the lower 4 bits ;-)

edit: well, it also sets some status flags which you aren't using anyway, so...
Ok, I was focused on math operation not on bits...
sandruzzo is offline  
Old 12 August 2020, 16:39   #6
Samurai_Crow
Total Chaos forever!
 
Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,187
Except that it should be the bitwise inverse of #-1.
Samurai_Crow is offline  
Old 12 August 2020, 17:30   #7
mcgeezer
Registered User
 
Join Date: Oct 2017
Location: Sunderland, England
Posts: 2,702
errrr...

Code:
and.w #$fff0
that's the sourcery rounding....
mcgeezer is offline  
Old 12 August 2020, 18:02   #8
StingRay
move.l #$c0ff33,throat
 
StingRay's Avatar
 
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 6,863
Yeah, a/b's solution is completely correct!
StingRay is offline  
Old 12 August 2020, 18:48   #9
lmimmfn
Registered User
 
Join Date: May 2018
Location: Ireland
Posts: 678
Quote:
Originally Posted by mcgeezer View Post
errrr...

Code:
and.w #$fff0
that's the sourcery rounding....
This makes the most sense to me and is easily readable
lmimmfn is online now  
Old 12 August 2020, 20:02   #10
a/b
Registered User
 
Join Date: Jun 2016
Location: europe
Posts: 1,039
The idea behind -1 is that it works with any size, it's automagically expanded to $ff, $ffff, or $ffffffff by assembler.
If you want to be strict, you could go with ~(1<<N-1), where N is the number of bits you want to clear, and get the same size-independent expression but -1 is just so simple and I personally don't find it confusing. No big deal either way...
a/b is offline  
Old 12 August 2020, 20:44   #11
mcgeezer
Registered User
 
Join Date: Oct 2017
Location: Sunderland, England
Posts: 2,702
Quote:
Originally Posted by a/b View Post
The idea behind -1 is that it works with any size, it's automagically expanded to $ff, $ffff, or $ffffffff by assembler.
If you want to be strict, you could go with ~(1<<N-1), where N is the number of bits you want to clear, and get the same size-independent expression but -1 is just so simple and I personally don't find it confusing. No big deal either way...
Personally I find the solution over complicated, why complicate a simple boolean AND with assembler directives unnecessarily?

I use the bit shift stuff alot in VASM.

Consider the BLTSIZE register:

Code:
move.w #(32<<6)+(32/16),BLTSIZE
....makes perfect sense to me because you are telling the assembler you want to blit a 32x32 block while assigning the correct bits in BLTSIZE.

Using the OR directive is equally useful.

Code:
BIT15: equ $8000
BIT14: equ $4000

or.w #(BIT15|BIT14),d0
will set both bits 15 and 14 in d0, great for dealing with DMA stuff... like this.

Code:
move.w	#(DMAF_SETCLR!DMAF_MASTER!DMAF_COPPER!DMAF_RASTER!DMAF_BLITTER),DMACON(a5)
mcgeezer is offline  
Old 12 August 2020, 21:32   #12
a/b
Registered User
 
Join Date: Jun 2016
Location: europe
Posts: 1,039
Quote:
Originally Posted by mcgeezer View Post
Personally I find the solution over complicated, why complicate a simple boolean AND with assembler directives unnecessarily?
...
Because when you decide that 4 bits is not enough and you have to go change your code it's easier to change a single EQU from 4 to say 5, and the rest is automagically calculated, than figure out which instructions contain related hardcoded stuff, be it $fff0, $000f, etc.
And when I do hardcode something related to a 'global parameter', the reason typically being optimizations, I add an assert (simple macro) that stop the assember and I don't have to deal with the code exploding during run-time.
a/b 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
EAB/Lemon Super League 2019 Round 7: Mystery Round Nominations john4p EAB's competition 4 28 April 2019 13:00
EAB/Lemon Super League 2018: Round 7 - Mystery Round Nominations john4p EAB's competition 4 29 April 2018 01:09
EAB/Lemon Super League 2017: Round 7 - Mystery Round Nominations Graham Humphrey EAB's competition 4 21 May 2017 09:02
HELP! A600 number 2 down! :( Snowy support.Other 5 04 December 2011 22:12
Your wii number? T_hairy_bootson Retrogaming General Discussion 21 26 January 2007 18:35

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:54.

Top

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