TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: andrew clarke
from: David Nugent
date: 1996-06-29 01:01:40
subject: optimisation

> Anybody know how to simplify (or more specifically
 > optimise) the code in the raw_to_mystruct function?  I
 > was thinking along the lines of

 >   x->thang = (raw[3] << 8) | (raw[2] << 8) | (raw[1]
<< 8) | raw[0];

Nope, won't work. You're ORing all 4 bytes into the one byte.

x->thang = ((unsigned long)raw[3] << 24) |
           ((unsigned long)raw[2] << 16) |
           ((unsigned long)raw[1] << 8)  |
           (unsigned long)raw[0];

The cast prevents overflow since the default conversion before the bitshift
will be to an int, which will overflow on < 32 bit systems.

Whether or not the code ends up being faster depends on your compiler's
optimisation capabilities.

--- MaltEd/2 1.0.b6
* Origin: Unique Computing Pty Limited (3:632/348)
SEEN-BY: 50/99 620/243 623/630 632/103 107 348 360 633/371 634/388 396
SEEN-BY: 635/301 502 503 506 544 639/252 711/401 409 410 413 430 808 809 932
SEEN-BY: 711/934 712/515 713/888 714/906 800/1
@PATH: 632/348 635/503 50/99 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.