OCaml: Bitwise operations on arbitrary precision integers.

This is a discussion on OCaml: Bitwise operations on arbitrary precision integers. within the ml forums in Programming Languages category; Hi, Is there any possibility in OCaml to perform bitwise logical operations on arbitrary precision integers? Thanks, Christian...

Go Back   Application Development Forum > Programming Languages > ml

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 07-10-2003, 12:19 PM
Christian Szegedy
Guest
 
Default OCaml: Bitwise operations on arbitrary precision integers.

Hi,

Is there any possibility in OCaml to perform bitwise
logical operations on arbitrary precision integers?

Thanks, Christian


Reply With Quote
  #2  
Old 07-10-2003, 05:58 PM
Shill
Guest
 
Default Re: OCaml: Bitwise operations on arbitrary precision integers.

> Is there any possibility in OCaml to perform bitwise
> logical operations on arbitrary precision integers?


I'm not sure... However

The big_int type [1] is just a struct:
type big_int = { sign : int; abs_value : nat }

There are 3 interesting functions [2] defined over nat:
land_digit_nat, lor_digit_nat, lxor_digit_nat

CAMLprim value
lxor_digit_nat(value nat1, value ofs1, value nat2, value ofs2)
{
BnXorDigits(Bignum_val(nat1), Long_val(ofs1),
Bignum_val(nat2), Long_val(ofs2));
return Val_unit;
}

We find the definition for BnXorDigits [4] if we dig deeper:
#define BnXorDigits(m, md, n, nd) BnnXorDigits ((m+md), *(n+nd))

Finally (?) we find both a generic [5] and a machine-specific [6]
definition for BnnXorDigits:

#define BnnXorDigits(nn, d) (*(nn) ^= (d))

.globl BnnXorDigits
BnnXorDigits: movl 4(%esp),%eax
movl 8(%esp),%edx
xorl %edx,(%eax)
ret

I'm not sure whether anything I've written makes any sense... Bleh!

[1] big_int.ml
[2] nat.ml
[3] nat_stubs.c
[4] BntoBnn.h
[5] BigNum.h
[6] x86KerN.s


Reply With Quote
  #3  
Old 07-11-2003, 12:29 PM
Doug Currie
Guest
 
Default Re[2]: OCaml: Bitwise operations on arbitrary precision integers.

Be careful; using big_int.nat in the code below doesn't produce
results that are necessarily correct for integers, assuming you may
have some negative arbitrary precision integers. For that you need to
choose a bitwise representation for negative integers. Common Lisp
chose twos complement. To do a twos complement bitwise operation using
the big_int type you'd need to propagate a borrow from the lsb toward
the msb. You can do that in one loop over the numbers if you do the
twos complement(s) and logical operation in parallel.

e

>> Is there any possibility in OCaml to perform bitwise
>> logical operations on arbitrary precision integers?


> I'm not sure... However


> The big_int type [1] is just a struct:
> type big_int = { sign : int; abs_value : nat }


> There are 3 interesting functions [2] defined over nat:
> land_digit_nat, lor_digit_nat, lxor_digit_nat


> CAMLprim value
> lxor_digit_nat(value nat1, value ofs1, value nat2, value ofs2)
> {
> BnXorDigits(Bignum_val(nat1), Long_val(ofs1),
> Bignum_val(nat2), Long_val(ofs2));
> return Val_unit;
> }


> We find the definition for BnXorDigits [4] if we dig deeper:
> #define BnXorDigits(m, md, n, nd) BnnXorDigits ((m+md), *(n+nd))


> Finally (?) we find both a generic [5] and a machine-specific [6]
> definition for BnnXorDigits:


> #define BnnXorDigits(nn, d) (*(nn) ^= (d))


> .globl BnnXorDigits
> BnnXorDigits: movl 4(%esp),%eax
> movl 8(%esp),%edx
> xorl %edx,(%eax)
> ret


> I'm not sure whether anything I've written makes any sense... Bleh!


> [1] big_int.ml
> [2] nat.ml
> [3] nat_stubs.c
> [4] BntoBnn.h
> [5] BigNum.h
> [6] x86KerN.s



Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:04 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.