1. 32-bit operations

Hi all,
it turns out that I can't use Juergen's routines after all. I need to do
arithetic that can have temporary values that extend beyond Euphoria's
30-bit integer limit. Thanks to being reminded about the bits_to_int() and
int_to_bits() routines, I've come up with this little function that gives me
the correct values now. It seems to run fast enough for my uses too!

  global function ClipBits(atom pValue)
    if pValue > #FFFFFFFF  or pValue < 0 then
      pValue = bits_to_int(int_to_bits(pValue,32))
    end if
    return pValue
  end function


I use it in this context ...

  global function TEA_encypher(atom y, atom z, atom k1, atom k2, atom sum)
    -- The input parameters are all 32-bit unsigned integers.
    -- I return a 32-bit unsigned integer

    -- The seperate assignments are there to ensure that I
    -- don't lose any precision by simulating 32-bit UINTs
    -- with atoms.

    y = ClipBits(y + ClipBits(z * 16))  -- z<<4
    y = ClipBits(y + xor_bits(k1, z))
    y = ClipBits(y + xor_bits(sum, (z / 32))) -- z>>5
    y = ClipBits(y + k2)
    return y
  end function

--
Derek

new topic     » topic index » view message » categorize

2. Re: 32-bit operations

Derek wrote:

> Hi all,
> it turns out that I can't use Juergen's routines after all. I need to do
> arithetic that can have temporary values that extend beyond Euphoria's
> 30-bit integer limit. Thanks to being reminded about the bits_to_int() and
> int_to_bits() routines, I've come up with this little function that gives me
> the correct values now. It seems to run fast enough for my uses too!
>
>   global function ClipBits(atom pValue)
>     if pValue > #FFFFFFFF  or pValue < 0 then
>       pValue = bits_to_int(int_to_bits(pValue,32))
>     end if
>     return pValue
>   end function

My routines in 'bit.e' should handle any 32-bit signed or unsigned
integer. They were not designed to handle values > #FFFFFFFF, that are
beyond the 32-bit limit. The Euphoria wrapper functions use user defined
types, to catch those "oversized" parameters.

But the machine functions themselves seem to clip the parameters, like
you want. Using the following test program, I wasn't able to generate an
error. What is/are the error/s that you get with 'bit.e', Derek?

--------------------------[ bit_test.exw ]--------------------------
-- using Eu 2.4, exw.exe
include get.e
include bit.e           -- v1.10

constant HEX = "0123456789ABCDEF"

function rand_hex (integer digits)
   -- return a random hexadecimal number as string
   sequence ret

   ret = repeat(0, digits)
   for i = 1 to digits do
      ret[i] = HEX[rand(16)]
   end for
   return ret
end function


sequence r32, r40, s
atom v32, v40, a, b
integer n, count

n = 10000
for i = 1 to n do
   -- generate a random 32-bit number as string
   r32 = rand_hex(8)

   -- generate a 40-bit number as string,
   -- by prepending a random 8-bit number to the 32-bit number
   r40 = rand_hex(2) & r32

   -- get values of the numbers
   s = value("#" & r32)
   if s[1] = GET_SUCCESS then
      v32 = s[2]
   else
      printf(1, "Error getting value(#%s)\n", {r32})
      abort(0)
   end if

   s = value("#" & r40)
   if s[1] = GET_SUCCESS then
      v40 = s[2]
   else
      printf(1, "Error getting value(#%s)\n", {r40})
      abort(0)
   end if

   count = rand(32)

   -- shift left both numbers (results should be equal)
   a = c_func(SHIFT_LEFT, {v32, count})
   b = c_func(SHIFT_LEFT, {v40, count})
   if a != b then
      printf(1, "SHIFT_LEFT didn't clip #%s as expected.\n", {r40})
   end if

   -- shift right both numbers (results should be equal)
   a = c_func(SHIFT_RIGHT, {v32, count})
   b = c_func(SHIFT_RIGHT, {v40, count})
   if a != b then
      printf(1, "SHIFT_RIGHT didn't clip #%s as expected.\n", {r40})
   end if
end for
--------------------------------------------------------------------


> I use it in this context ...
>
>   global function TEA_encypher(atom y, atom z, atom k1, atom k2, atom sum)
>     -- The input parameters are all 32-bit unsigned integers.
>     -- I return a 32-bit unsigned integer
>
>     -- The seperate assignments are there to ensure that I
>     -- don't lose any precision by simulating 32-bit UINTs
>     -- with atoms.
>
>     y = ClipBits(y + ClipBits(z * 16))  -- z<<4
>     y = ClipBits(y + xor_bits(k1, z))
>     y = ClipBits(y + xor_bits(sum, (z / 32))) -- z>>5
>     y = ClipBits(y + k2)
>     return y
>   end function


Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu