| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Hi, Is there any possibility in OCaml to perform bitwise logical operations on arbitrary precision integers? Thanks, Christian |
|
#2
| |||
| |||
| > 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 |
|
#3
| |||
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.