1. Bit-shifting
- Posted by Pete Eberlein <xseal at HARBORSIDE.COM>
May 03, 1997
-
Last edited May 04, 1997
Will bit-shifting operators be available in the next version of
Euphoria? I would much rather use a built-in machine code instead of a
multiply or divide. Does Euphoria optimize multiplication into a shift
when one of the numbers is an integer power of two? Does it convert a
value to floating-point when dividing?
I would much rather be able to
integer SHR 1 -- shift right
integer SAR 1 -- shift arithmetic right (keeps the sign)
instead of using
floor(integer / 2)
If bit-shifting becomes available I would like a function that does bit
slicing. It would work with integers the same way as sequences, without
having to convert the integer into a sequence of integers and back
again. I think it could be coded like this:
-- returns source[lower_bit..upper_bit]
function bit_slice(source, lower_bit, upper_bit)
integer mask
mask = #FFFFFFFF shr (32 - upper_bit + lower_bit)
source = source shr lower_bit
return and_bits(source, mask)
end function
-- returns dest with [lower_bit..upper_bit] overwritten by source
function bit_splice(source, dest, lower_bit, upper_bit)
integer mask
mask = (#FFFFFFFF shr (32 - upper_bit + lower_bit)) shl lower_bit
source = source shl lower_bit
return or_bits(and_bits(dest, not_bits(mask)), and_bits(source,
mask))
end function
I could see a lot of code optimizations using these functions. Speed is
one thing I like about Euphoria, and I would like to see it pushed to
the absolute max.
Also would it be possible to have a 32-bit peek and poke for integers?
Thanks
--
_____ _____ _____
________ /\ \ /\ \ /\ \
/ \ \ / \____\ / \____\ / \____\
/ _ \____\ / / ___/_ / /____/ / / ___/_
/ / \ |____|/ / /\____\ / \ \ / / /\____\
\ \_/ / / \ \/ / ___/_\ \ \ \ \/ / ___/_
\ /____/ \ / /\ \\/\ \ \ \ / /\ \
\ \ \ \ \/ \____\ \ \ \ \ \/ \____\
\ \ \ \ / / \ \____\ \ / /
\ \____\ \ / / \ / / \ / /
\ / / \ / / \/____/ \ / /
\/____/ \/____/ \/____/
2. Bit-shifting
Pete Eberlein writes:
> I would much rather be able to
> integer SHR 1 -- shift right
> integer SAR 1 -- shift arithmetic right (keeps the sign)
> instead of using
> floor(integer / 2)
Thanks for the suggestions about shift operators.
Maybe some day. But I just want to point out that
Euphoria already optimizes floor(integer/2) into
a single right-shift machine instruction (plus a few instructions
of overhead needed by any interpretive language).
In general, floor(integer1/integer2) does NOT use
a floating-point divide instruction. It performs a machine-level
integer divide. The compiler portion of Euphoria is smart enough
to see that divide of 2 integers is followed
immediately by floor, so it generates an intermediate-language
op-code that performs a machine-level integer divide, and
then there's no need for a separate "floor" operation at all.
Regards,
Rob Craig
Rapid Deployment Software
3. Re: Bit-shifting
- Posted by Architek <architek at GEOCITIES.COM>
May 04, 1997
-
Last edited May 05, 1997
It would be nice to have also a BYTE type. Many times I work with
binary files (images, compressionm, etc..) and think it's a waste
of memory having integers values using 4 bytes in memory. I think
this could be easily implemented on further releases.
Bye,
--
************************************
This message sent by Daniel Berstein
************************************
email: architek at geocities.com
homepages: http://www.geocities.com/SiliconValley/Heights/9316
http://www.cybercity.hko.net/silicon_valley/architek