[futurebasic] Re: [FB] Intel Words and Longs to mac Words and Longs

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : September 1999 : Group Archive : Group : All Groups

From: Robert Purves <robert.purves@...>
Date: Sun, 26 Sep 1999 17:33:25 +1200
>I need to parse some image files from the dark side where words (2 bytes)
>and longs (4 bytes) are the reverse of their Mac counterparts. What I've
>come up with is below. Is there a better or faster method?

>CLEAR LOCAL
>LOCAL FN intelWordToMacWord%(anIntelWord%)
<snip>

>CLEAR LOCAL
>LOCAL FN intelLongToMacLong&(anIntelLong&)
<snip>

Well, for a start you should remove CLEAR LOCAL, which wastes time by
setting your local variables to zero on every call. You don't need that.

Then you can gain a little more speed by removing the byte% variables
altogether.

LOCAL FN NintelWordToMacWord%(anIntelWord%)
 DIM aMacWord%
 POKE @aMacWord%, PEEK(@anIntelWord%+1)
 POKE @aMacWord%+1,PEEK(@anIntelWord%)
END FN = aMacWord%

LOCAL FN NintelLongToMacLong&(anIntelLong&)
 DIM aMacLong&
 POKE @aMacLong&,   PEEK(@anIntelLong&+3)
 POKE @aMacLong&+1, PEEK(@anIntelLong&+2)
 POKE @aMacLong&+2, PEEK(@anIntelLong&+1)
 POKE @aMacLong&+3, PEEK(@anIntelLong&)
END FN = aMacLong&

In FB2, these new versions take about 2/3 the time of your functions; a
worthwhile but not impressive speed-up. In FB^3 the speed-up is less.

You may like to consider making them "in-line" functions, by putting the
code into your loop. Assuming you have something like:

FOR j& = 1 TO gazillion&
 macNum&(j&)=FN intelLongToMacLong&(intelNum&(j&))
NEXT j&

Try this:
DIM aMacLong&, anIntelLong&
FOR j& = 1 TO gazillion&
 anIntelLong&=intelNum&(j&)
 POKE @aMacLong&,   PEEK(@anIntelLong&+3)
 POKE @aMacLong&+1, PEEK(@anIntelLong&+2)
 POKE @aMacLong&+2, PEEK(@anIntelLong&+1)
 POKE @aMacLong&+3, PEEK(@anIntelLong&)
 macNum&(j&)=aMacLong&
NEXT j&

The in-line version is more than twice as fast as calling your functions,
because it does not incur the overhead of FN calls. The code is however
ugly and hard to follow; your functions are not.

Robert Purves