Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file int.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357open!ImportincludeInt_intfincludeInt0moduleT=structtypet=int[@@deriving_inlinehash,sexp,sexp_grammar]let(hash_fold_t:Ppx_hash_lib.Std.Hash.state->t->Ppx_hash_lib.Std.Hash.state)=hash_fold_intand(hash:t->Ppx_hash_lib.Std.Hash.hash_value)=letfunc=hash_intinfunx->funcx;;lett_of_sexp=(int_of_sexp:Ppx_sexp_conv_lib.Sexp.t->t)letsexp_of_t=(sexp_of_int:t->Ppx_sexp_conv_lib.Sexp.t)let(t_sexp_grammar:Ppx_sexp_conv_lib.Sexp.Private.Raw_grammar.t)=let(_the_generic_group:Ppx_sexp_conv_lib.Sexp.Private.Raw_grammar.generic_group)={implicit_vars=["int"];ggid="\146e\023\249\235eE\139c\132W\195\137\129\235\025";types=["t",Implicit_var0]}inlet(_the_group:Ppx_sexp_conv_lib.Sexp.Private.Raw_grammar.group)={gid=Ppx_sexp_conv_lib.Lazy_group_id.create();apply_implicit=[int_sexp_grammar];generic_group=_the_generic_group;origin="int.ml.T"}inlet(t_sexp_grammar:Ppx_sexp_conv_lib.Sexp.Private.Raw_grammar.t)=Ref("t",_the_group)int_sexp_grammar;;[@@@end]letcomparexy=Int_replace_polymorphic_compare.comparexyletof_strings=tryof_stringswith|_->Printf.failwithf"Int.of_string: %S"s();;letto_string=to_stringendletnum_bits=Int_conversions.num_bits_intletfloat_lower_bound=Float0.lower_bound_for_intnum_bitsletfloat_upper_bound=Float0.upper_bound_for_intnum_bitsletto_float=Caml.float_of_intletof_float_unchecked=Caml.int_of_floatletof_floatf=ifFloat_replace_polymorphic_compare.(>=)ffloat_lower_bound&&Float_replace_polymorphic_compare.(<=)ffloat_upper_boundthenCaml.int_of_floatfelsePrintf.invalid_argf"Int.of_float: argument (%f) is out of range or NaN"(Float0.boxf)();;letzero=0letone=1letminus_one=-1includeTincludeComparator.Make(T)includeComparable.Validate_with_zero(structincludeTletzero=zeroend)moduleConv=Int_conversionsincludeConv.Make(T)includeConv.Make_hex(structopenInt_replace_polymorphic_comparetypet=int[@@deriving_inlinecompare,hash]letcompare=(compare_int:t->t->int)let(hash_fold_t:Ppx_hash_lib.Std.Hash.state->t->Ppx_hash_lib.Std.Hash.state)=hash_fold_intand(hash:t->Ppx_hash_lib.Std.Hash.hash_value)=letfunc=hash_intinfunx->funcx;;[@@@end]letzero=zeroletneg=(~-)let(<)=(<)letto_stringi=Printf.sprintf"%x"iletof_strings=Caml.Scanf.sscanfs"%x"Fn.idletmodule_name="Base.Int.Hex"end)includePretty_printer.Register(structtypenonrect=tletto_string=to_stringletmodule_name="Base.Int"end)(* Open replace_polymorphic_compare after including functor instantiations so
they do not shadow its definitions. This is here so that efficient versions
of the comparison functions are available within this module. *)open!Int_replace_polymorphic_compareletinvariant(_:t)=()letbetweent~low~high=low<=t&&t<=highletclamp_uncheckedt~min~max=ift<minthenminelseift<=maxthentelsemaxletclamp_exnt~min~max=assert(min<=max);clamp_uncheckedt~min~max;;letclampt~min~max=ifmin>maxthenOr_error.error_s(Sexp.message"clamp requires [min <= max]"["min",T.sexp_of_tmin;"max",T.sexp_of_tmax])elseOk(clamp_uncheckedt~min~max);;letpredi=i-1letsucci=i+1letto_inti=iletto_int_exn=to_intletof_inti=iletof_int_exn=of_intletmax_value=Caml.max_intletmin_value=Caml.min_intletmax_value_30_bits=0x3FFF_FFFFletof_int32=Conv.int32_to_intletof_int32_exn=Conv.int32_to_int_exnletof_int32_trunc=Conv.int32_to_int_truncletto_int32=Conv.int_to_int32letto_int32_exn=Conv.int_to_int32_exnletto_int32_trunc=Conv.int_to_int32_truncletof_int64=Conv.int64_to_intletof_int64_exn=Conv.int64_to_int_exnletof_int64_trunc=Conv.int64_to_int_truncletto_int64=Conv.int_to_int64letof_nativeint=Conv.nativeint_to_intletof_nativeint_exn=Conv.nativeint_to_int_exnletof_nativeint_trunc=Conv.nativeint_to_int_truncletto_nativeint=Conv.int_to_nativeintletto_nativeint_exn=to_nativeintletabsx=absxlet(+)xy=x+ylet(-)xy=x-ylet(*)xy=x*ylet(/)xy=x/yletnegx=-xlet(~-)=neg(* note that rem is not same as % *)letremab=amodbletincr=Caml.incrletdecr=Caml.decrletshift_rightab=aasrbletshift_right_logicalab=alsrbletshift_leftab=alslbletbit_nota=lnotaletbit_orab=alorbletbit_andab=alandbletbit_xorab=alxorbletpow=Int_math.Private.int_powlet(**)be=powbemodulePow2=structopen!ImportmoduleSys=Sys0letraise_s=Error.raise_sletnon_positive_argument()=Printf.invalid_argf"argument must be strictly positive"();;(** "ceiling power of 2" - Least power of 2 greater than or equal to x. *)letceil_pow2x=ifx<=0thennon_positive_argument();letx=x-1inletx=xlor(xlsr1)inletx=xlor(xlsr2)inletx=xlor(xlsr4)inletx=xlor(xlsr8)inletx=xlor(xlsr16)in(* The next line is superfluous on 32-bit architectures, but it's faster to do it
anyway than to branch *)letx=xlor(xlsr32)inx+1;;(** "floor power of 2" - Largest power of 2 less than or equal to x. *)letfloor_pow2x=ifx<=0thennon_positive_argument();letx=xlor(xlsr1)inletx=xlor(xlsr2)inletx=xlor(xlsr4)inletx=xlor(xlsr8)inletx=xlor(xlsr16)in(* The next line is superfluous on 32-bit architectures, but it's faster to do it
anyway than to branch *)letx=xlor(xlsr32)inx-(xlsr1);;letis_pow2x=ifx<=0thennon_positive_argument();xland(x-1)=0;;(* C stubs for int clz and ctz to use the CLZ/BSR/CTZ/BSF instruction where possible *)externalclz:(* Note that we pass the tagged int here. See int_math_stubs.c for details on why
this is correct. *)int->(int[@untagged])="Base_int_math_int_clz""Base_int_math_int_clz_untagged"[@@noalloc]externalctz:(int[@untagged])->(int[@untagged])="Base_int_math_int_ctz""Base_int_math_int_ctz_untagged"[@@noalloc](** Hacker's Delight Second Edition p106 *)letfloor_log2i=ifi<=0thenraise_s(Sexp.message"[Int.floor_log2] got invalid input"["",sexp_of_inti]);num_bits-1-clzi;;letceil_log2i=ifi<=0thenraise_s(Sexp.message"[Int.ceil_log2] got invalid input"["",sexp_of_inti]);ifi=1then0elsenum_bits-clz(i-1);;endincludePow2(* This is already defined by Comparable.Validate_with_zero, but Sign.of_int is
more direct. *)letsign=Sign.of_intletpopcount=Popcount.int_popcountmodulePre_O=structlet(+)=(+)let(-)=(-)let(*)=(*)let(/)=(/)let(~-)=(~-)let(**)=(**)include(Int_replace_polymorphic_compare:Comparisons.Infixwithtypet:=t)letabs=absletneg=negletzero=zeroletof_int_exn=of_int_exnendmoduleO=structincludePre_OmoduleF=Int_math.Make(structtypenonrect=tincludePre_Oletrem=remletto_float=to_floatletof_float=of_floatletof_string=T.of_stringletto_string=T.to_stringend)includeFexternalbswap16:int->int="%bswap16"(* These inlined versions of (%), (/%), and (//) perform better than their functorized
counterparts in [F] (see benchmarks below).
The reason these functions are inlined in [Int] but not in any of the other integer
modules is that they existed in [Int] and [Int] alone prior to the introduction of
the [Int_math.Make] functor, and we didn't want to degrade their performance.
We won't pre-emptively do the same for new functions, unless someone cares, on a case
by case fashion. *)let(%)xy=ify<=zerothenPrintf.invalid_argf"%s %% %s in core_int.ml: modulus should be positive"(to_stringx)(to_stringy)();letrval=remxyinifrval<zerothenrval+yelserval;;let(/%)xy=ify<=zerothenPrintf.invalid_argf"%s /%% %s in core_int.ml: divisor should be positive"(to_stringx)(to_stringy)();ifx<zerothen((x+one)/y)-oneelsex/y;;let(//)xy=to_floatx/.to_floatylet(land)=(land)let(lor)=(lor)let(lxor)=(lxor)letlnot=lnotlet(lsl)=(lsl)let(asr)=(asr)let(lsr)=(lsr)endincludeO(* [Int] and [Int.O] agree value-wise *)modulePrivate=structmoduleO_F=O.Fend(* Include type-specific [Replace_polymorphic_compare] at the end, after including functor
application that could shadow its definitions. This is here so that efficient versions
of the comparison functions are exported by this module. *)includeInt_replace_polymorphic_compare