Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 8th, 2005, 7:55 AM   #11
M.Hirsch
Newbie
 
Join Date: Jun 2005
Posts: 26
Rep Power: 0 M.Hirsch is on a distinguished road
I am using this now to convert between Mac and Windows:
#define BTOL(x) ((x & 0xFF000000) >> 24) + ((x & 0xFF0000) >> 8) + ((x & 0xFF00) << 8) + ((x & 0xFF) << 24)
#define LTOB(x) BTOL(x)

M.
__________________
Multilingual Content Management
M.Hirsch is offline   Reply With Quote
Old Jul 8th, 2005, 9:11 AM   #12
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Bear in mind that the results of "(x & 0xFF000000) >> 24" will differ according to whether "x" is defined as signed or unsigned, if the high-order bit is set.

One of the problems with a macro is that the task of controlling "x" is thrust upon you, as the compiler hasn't even come into play at the time of the expansion, so there's no type checking. Blatant errors will cause a compiler error later, of course.

I'm not "against" macros, per se, as many are. Here's one I used to speed up some code Infamous had; it performs a caseless compare of two bytes:

#define eq(x,y) ((!(x^y))) || (((x^y) == 0x20) && (((x|y) > 0x60) && ((x|y) < 0x7b)))

It actually whittled the execution time dramatically, but one has to be careful; it is also specific to ASCII encoding.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers

Last edited by DaWei; Jul 8th, 2005 at 9:25 AM.
DaWei is offline   Reply With Quote
Old Jul 8th, 2005, 9:20 AM   #13
M.Hirsch
Newbie
 
Join Date: Jun 2005
Posts: 26
Rep Power: 0 M.Hirsch is on a distinguished road
Quote:
Originally Posted by DaWei
Bear in mind that the results of "(x & 0xFF000000) >> 24" will differ according to whether "x" is defined as signed or unsigned, if the high-order bit is set.
I don't think this applies here because my problem is about the way of storing numbers, not about interpreting them. The results should be correct for signed and unsigned ints as long as I am not mixing them up.
Anyways, I'll keep an eye on this and not use this function for anything but reading/writing AVI data. Thanks for the hint!

M.
__________________
Multilingual Content Management
M.Hirsch is offline   Reply With Quote
Old Jul 9th, 2005, 2:51 AM   #14
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
IIRC the x86 bswap instruction does just what you want. Other processors probably have something similar, as DaWei pointed out.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials.
--WilliamSChips on Slashdot
uman is offline   Reply With Quote
Old Jul 11th, 2005, 2:21 AM   #15
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Also note that you should probably have an extra set of brackets around the whole replacement expression and brackets around each x. Otherwise if you happen to write
a = BToL(b|1)*10;
it will expand out to
a = ((b|1 & 0xFF000000) >> 24) + ((b|1 & 0xFF0000) >> 8) + ((b|1 & 0xFF00) << 8) + ((b|1 & 0xFF) << 24)*10

Due to operator precedence, the *10 will only apply to the last bracketed part and all of the & operations will operate on the 1 before the | operation takes place.

Not that you'd write a horrible expression like that, but who knows what might happen 2 years down the track.
The Dark is offline   Reply With Quote
Old Jul 11th, 2005, 4:58 AM   #16
M.Hirsch
Newbie
 
Join Date: Jun 2005
Posts: 26
Rep Power: 0 M.Hirsch is on a distinguished road
Thanks, I added the extra brackets.
Agreed, it's a horrible expression. But since it's a macro, this is the (pre)compiler's problem, not mine So I guess it's ok.
Well, I still wish there was a more elegant solution...

M.
__________________
Multilingual Content Management
M.Hirsch is offline   Reply With Quote
Old Jul 11th, 2005, 8:05 PM   #17
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
Maybe make it a function instead of a macro? At least, that will avoid any fun side-effects with macros (for instance, y = BToL(x++) will cause very interesting issues).
Scorpions4ever is offline   Reply With Quote
Old Jul 13th, 2005, 5:56 AM   #18
M.Hirsch
Newbie
 
Join Date: Jun 2005
Posts: 26
Rep Power: 0 M.Hirsch is on a distinguished road
Quote:
Originally Posted by Scorpions4ever
Maybe make it a function instead of a macro? At least, that will avoid any fun side-effects with macros (for instance, y = BToL(x++) will cause very interesting issues).
Hmmm. Ok, you convinced me. I won't add more brackets or magic, I'll make a function of it If speed becomes an issue I can still switch to asm.

int btol(int x) {
  return ((x & 0xFF000000) >> 24) + ((x & 0xFF0000) >> 8) + ((x & 0xFF00) << 8) + ((x & 0xFF) << 24);
}
The function will be used to convert files of several 100MB, maybe even gigs...
Should I use a register here maybe? But then, I'll get the code done first, optimize later.

M.
__________________
Multilingual Content Management
M.Hirsch is offline   Reply With Quote
Old Jul 13th, 2005, 6:44 AM   #19
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Despite the use of the register keyword, you can't guarantee the use of a register. You can make it an inline function and avoid call overhead or you may want to drop into inline asm and use the registers directly.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 13th, 2005, 8:13 AM   #20
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
There's an XCHG instruction for the Intel device that's more general-purpose than the SWAB instruction. You could pull the value into, say, EAX, then exchange the two outer and two inner 8-bit registers. Probably doesn't get any faster. By making it inline, you'd have the locality of reference you need to ensure that cache doesn't get invalidated, so the pipelining would remain effective. Speedier than a fasting bullet, or however that goes .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:15 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC