Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file batInt.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355(*
* BatInt - Extended integers
* Copyright (C) 2007 Bluestorm <bluestorm dot dylc on-the-server gmail dot com>
* 2008 David Teller
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)##V>=5##modulePervasives=Stdlib(*$inject
##V>=5##module Pervasives = Stdlib
*)openBatNumberletenum()=letcurrent_value=refmin_intinletalready_through=reffalseinletf()=if!current_value=max_intthenif!already_throughthenraiseBatEnum.No_more_elementselse(already_through:=true;max_int)elseBatRef.post_incrcurrent_valueinBatEnum.fromfmoduleBaseInt=structtypet=intletzero,one =0,1externalneg:int->int="%negint"externaladd:int->int->int="%addint"externalsub:int->int->int="%subint"externalmul:int->int->int="%mulint"externaldiv:int->int->int="%divint"external(+):int->int->int="%addint"external(-):int->int->int="%subint"external(*):int->int->int="%mulint"external(/):int->int->int="%divint"externalpred:int->int="%predint"externalsucc:int->int="%succint"letabs=absexternalmodulo:int->int->int="%modint"letpowab=ifb<0theninvalid_arg"Int.pow"elseletdiv_twon=n/2andmod_twon=nmod2ingeneric_pow~zero~one~div_two~mod_two~mulab(*$Q pow
Q.int (fun a -> pow a 0 = 1)
Q.int (fun a -> pow a 1 = a)
Q.int (fun a -> pow a 2 = a * a)
Q.pos_int (fun b -> b = 0 || pow 0 b = 0)
Q.pos_int (fun b -> pow 1 b = 1)
(Q.pair Q.int Q.neg_int) (fun (a,b) -> \
b = 0 || Result.(catch2 pow a b |> is_exn (Invalid_argument "Int.pow")))
*)(*$= pow
(pow (-2) 3) (-8)
(pow 0 0) 1
*)letmin_num,max_num=min_int,max_int(* this function is performance sensitive : it is heavily used by
associative data structures using ordered keys (Set, Map). The
current version, due to Mauricio "mfp" Fernandez, only uses
a type annotation to benefit from the excellent compilation of
statically-known integer comparisons. It outperforms the previous
version calling directly the external primitive
"caml_int_compare". *)letcompare(x:int)y=ifx>ythen1elseify>xthen-1else0externalof_int:int->int="%identity"externalto_int:int->int="%identity"letto_string=string_of_intletenum=enumletminus_one=(-1)externalto_float:int->float="%floatofint"externalof_float:float->int="%intoffloat"externalof_string:string->int="caml_int_of_string"externalrem:int->int->int="%modint"let(<>)(a:int)b=a<>blet(<=)(a:int)b=a<=blet(>=)(a:int)b=a>=blet(<)(a:int)b=a<blet(>)(a:int)b=a>blet(=)(a:int)b=a=blet(**)ab=powab(*$T ( ** )
0 ** 0 = 1
0 ** 1 = 0
(-1) ** 3 = (-1)
(-1) ** 4 = 1
15 ** 3 = 3375
7 ** 4 = 2401
*)letprintoutt=BatInnerIO.nwriteout(string_of_int t)letprint_hexoutt=BatPrintf.fprintfout"%X"tlet(--)xy=BatEnum.seqx(addone)((>=)y)let(---)xy=ifx<=ythenx--yelseBatEnum.seqxpred((<=)y)end(* We want BaseInt versions of these function instead of MakeNumeric ones *)moduleCompare=structtypebat__compare_t=intlet (<>),(>=),(<=),(>),(<),(=)=BaseInt.((<>),(>=),(<=),(>),(<),(=))endinclude(BatNumber.MakeNumeric(BaseInt):BatNumber.Numericwithtypet:=intandmoduleCompare:=Compare)includeBaseIntletminab=ifa<bthenaelsebletmaxab=ifa>bthenaelseb(*$T min
min 3 4 = 3
min 4 4 = 4
min (-3) 5 = -3
min min_int max_int = min_int
*)(*$T max
max 3 4 = 4
max 4 4 = 4
max (-3) 5 = 5
max min_int max_int = max_int
max max_int max_int = max_int
max min_int min_int = min_int
*)letmidab=alandb+((alxorb)asr1)(*$Q mid
(Q.pair Q.int Q.int) (fun (a,b) -> \
let m = mid a b in \
(a <= b && a <= m && m <= b && abs ((m-a) - (b-m)) <= 1) || \
(b < a && b <= m && m <= a && abs ((m-b) - (a-m)) <= 1))
(Q.int) (fun a -> mid a a = a)
*)letpopcount=ifSys.word_size=32 thenletk1=0x55555555 inletk2=0x33333333 inletk3=0x0f0f0f0f in(funx->letx=x-(xlsr1)landk1inletx=((xlsr2)landk2)+(xlandk2)inletx=(x+(xlsr4))landk3inletx=x+xlsr8in(x+xlsr16)land0x3f)else(* word_size = 64 *)(* uses int_of_string to hide these constants from the 32-bit compiler *)letk1=int_of_string "0x5555_5555_5555_5555" inletk2=int_of_string "0x3333_3333_3333_3333" inletk4=int_of_string "0x0f0f_0f0f_0f0f_0f0f" in(funx->letx=x-(xlsr1)landk1inletx=(xlandk2)+((xlsr2)landk2)inletx=(x+xlsr4)landk4inletx=x+xasr8inletx=x+xasr16inletx=x+xasr32inxland0x7f)letpopcount_sparsex=letrecloopnx=ifx=0thennelseloop(n+1)(xland(x-1))inloop0x(*$Q popcount
(Q.int) (fun x -> popcount x = popcount_sparse x)
*)letcopysignno=matchnwith|0->0|nwhenn>0->o|_->-o(*$T copysign
copysign 2 1 = 1
copysign 3 1 = 1
copysign 3 5 = 5
copysign max_int min_int = min_int
copysign (-22) 12 = -12
copysign 0 42 = 0
*)moduleBaseSafeInt=structincludeBaseInt(** Open this module and [SafeInt] to replace traditional integer
operators with their safe counterparts *)letaddab=letc=a+binifa<0&&b<0&&c>=0||a>0&&b>0&&c<=0thenraiseOverflowelsecletsubab=letc=a-binifa<0&&b>0&&c>=0||a>0&&b<0&&c<=0thenraiseOverflowelsecletnegx=ifx<>min_intthen~-xelse raiseOverflowletsuccx=ifx<>max_intthensuccxelseraiseOverflowletpredx=ifx<>min_intthenpredxelseraiseOverflowletabsx=ifx<>min_intthenabsxelseraiseOverflow(* Performance hack: if both operands of the multiplication operator can be
represented using the specified amount of bits (not counting the sign
bit), then it is safe to assume that overflow does not happen. *)letmul_shift_bits=matchSys.word_sizewith|64->31(* 64 = sign bit + 31*2 + tag bit *)|32->15(* 32 = sign bit + 15*2 + tag bit *)|_->0(* Uses a formula taken from Hacker's Delight, chapter "Overflow Detection",
plus a fast-path check (see comment above) *)letmul(a:int)(b:int):int=(* let open Pervasives in *)letc=a*binif(alorb)asrmul_shift_bits=0||not((a=min_int&&b<0)||(b<>0&&c/b<>a))thencelseraiseBatNumber.Overflowletpowab=ifb<0theninvalid_arg"Int.Safe_int.pow"elseletdiv_twon=n/2andmod_twon=nmod2inBatNumber.generic_pow~zero~one~div_two~mod_two~mulabendmoduleSafe_int=structmoduleCompare=structtypebat__compare_t=tlet(<>),(>=),(<=),(>),(<),(=)=(<>),(>=),(<=),(>),(<),(=)endinclude(BatNumber.MakeNumeric(BaseSafeInt):BatNumber.Numericwithtypet:=intandmoduleCompare:=Compare)includeBaseSafeInt(* for performance, replace functor-values with direct values *)end(*$T &
Result.(catch (Safe_int.add max_int) max_int |> is_exn Number.Overflow)
Result.(catch (Safe_int.add min_int) min_int |> is_exn Number.Overflow)
Safe_int.add 0 0 = 0
Safe_int.add max_int min_int = (-1)
Result.(catch (Safe_int.sub min_int) max_int |> is_exn Number.Overflow)
Result.(catch (Safe_int.sub max_int) min_int |> is_exn Number.Overflow)
Safe_int.sub 0 0 = 0
Safe_int.neg max_int = -max_int
Result.(catch Safe_int.neg min_int |> is_exn Number.Overflow)
Result.(catch (List.reduce Safe_int.mul) \
[1 lsl 18 * 21; 3*3*3*3*3*3*3*3; 5*5*5*5*7*7*11*13*17*19] \
|> is_exn Number.Overflow)
Safe_int.mul 0 min_int = 0
Safe_int.mul min_int 0 = 0
Safe_int.mul 1 min_int = min_int
Safe_int.mul min_int 1 = min_int
Safe_int.mul (-1) max_int = -max_int
Safe_int.mul max_int (-1) = -max_int
Result.(catch (Safe_int.mul min_int) (-1) |> is_exn Number.Overflow)
Result.(catch (Safe_int.mul (-1)) min_int |> is_exn Number.Overflow)
Result.(catch (Safe_int.Infix.(+) max_int) 1 |> is_exn Number.Overflow)
Safe_int.succ 1 = 2
Safe_int.succ (-1) = 0
Safe_int.succ (-2) = (-1)
Safe_int.succ 0 = 1
Result.(catch Safe_int.succ max_int |> is_exn Number.Overflow)
Safe_int.pred 1 = 0
Safe_int.pred 0 = (-1)
Safe_int.pred (-1) = (-2)
Result.(catch Safe_int.pred min_int |> is_exn Number.Overflow)
Safe_int.abs 0 = 0
Safe_int.abs (-5) = 5
Safe_int.abs 5 = 5
Safe_int.abs max_int = max_int
Result.(catch Safe_int.abs min_int |> is_exn Number.Overflow)
*)(*$Q &
(Q.pair Q.pos_int Q.pos_int) (fun (a,b) -> let (a,b) = max a b, min a b in \
let b = max_int - a + b in try Safe_int.add a b |>ignore; false \
with BatNumber.Overflow -> true)
(Q.pair Q.pos_int Q.pos_int) (fun (a,b) -> let (a,b) = max a b, min a b in \
let b = max_int - a + b in try Safe_int.sub (-a) b|>ignore; false \
with BatNumber.Overflow -> true)
(Q.pair Q.int Q.int) (fun (a,b) -> \
let slow_mul a b = \
if b = 0 then 0 \
else if (abs a) > max_int / (abs b) then raise BatNumber.Overflow else a*b \
in Legacy.(=) \
(Result.catch (Safe_int.mul a) b) (Result.catch (slow_mul a) b))
*)(*
module Int = struct
include BaseInt
module Numeric = struct include Numeric(BaseInt) end
end
module SafeInt = struct
include BaseSafeInt
module Numeric = struct include Numeric(BaseSafeInt) end
end
*)