![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Newbie
Join Date: Jun 2005
Posts: 26
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#12 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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. |
|
|
|
|
|
#13 | |
|
Newbie
Join Date: Jun 2005
Posts: 26
Rep Power: 0
![]() |
Quote:
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 |
|
|
|
|
|
|
#14 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#15 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#16 |
|
Newbie
Join Date: Jun 2005
Posts: 26
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#17 |
|
Programmer
Join Date: Jun 2005
Posts: 86
Rep Power: 4
![]() |
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).
|
|
|
|
|
|
#18 | |
|
Newbie
Join Date: Jun 2005
Posts: 26
Rep Power: 0
![]() |
Quote:
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);
}Should I use a register here maybe? But then, I'll get the code done first, optimize later. M.
__________________
Multilingual Content Management |
|
|
|
|
|
|
#19 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#20 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|