123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120# 2 "int32.ml"(**************************************************************************)(* *)(* OCaml *)(* *)(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)(* *)(* Copyright 1996 Institut National de Recherche en Informatique et *)(* en Automatique. *)(* *)(* All rights reserved. This file is distributed under the terms of *)(* the GNU Lesser General Public License version 2.1, with the *)(* special exception on linking described in the file LICENSE. *)(* *)(**************************************************************************)open!Stdlib[@@@ocaml.flambda_o3](* Module [Int32]: 32-bit integers *)externalneg:(int32[@local_opt])->(int32[@local_opt])@@portable="%int32_neg"externaladd:(int32[@local_opt])->(int32[@local_opt])->(int32[@local_opt])@@portable="%int32_add"externalsub:(int32[@local_opt])->(int32[@local_opt])->(int32[@local_opt])@@portable="%int32_sub"externalmul:(int32[@local_opt])->(int32[@local_opt])->(int32[@local_opt])@@portable="%int32_mul"externaldiv:(int32[@local_opt])->(int32[@local_opt])->(int32[@local_opt])@@portable="%int32_div"externalrem:(int32[@local_opt])->(int32[@local_opt])->(int32[@local_opt])@@portable="%int32_mod"externallogand:(int32[@local_opt])->(int32[@local_opt])->(int32[@local_opt])@@portable="%int32_and"externallogor:(int32[@local_opt])->(int32[@local_opt])->(int32[@local_opt])@@portable="%int32_or"externallogxor:(int32[@local_opt])->(int32[@local_opt])->(int32[@local_opt])@@portable="%int32_xor"externalshift_left:(int32[@local_opt])->int->(int32[@local_opt])@@portable="%int32_lsl"externalshift_right:(int32[@local_opt])->int->(int32[@local_opt])@@portable="%int32_asr"externalshift_right_logical:(int32[@local_opt])->int->(int32[@local_opt])@@portable="%int32_lsr"externalof_int:int->(int32[@local_opt])@@portable="%int32_of_int"externalto_int:(int32[@local_opt])->int@@portable="%int32_to_int"externalof_float:float->int32@@portable="caml_int32_of_float""caml_int32_of_float_unboxed"[@@unboxed][@@noalloc]externalto_float:int32->float@@portable="caml_int32_to_float""caml_int32_to_float_unboxed"[@@unboxed][@@noalloc]externalbits_of_float:float->int32@@portable="caml_int32_bits_of_float""caml_int32_bits_of_float_unboxed"[@@unboxed][@@noalloc]externalfloat_of_bits:int32->float@@portable="caml_int32_float_of_bits""caml_int32_float_of_bits_unboxed"[@@unboxed][@@noalloc]letzero=0lletone=1lletminus_one=-1llet[@inlineavailable]succn=addn1llet[@inlineavailable]predn=subn1llet[@inlineavailable]absn=ifn>=0lthennelsenegnletmin_int=0x80000000lletmax_int=0x7FFFFFFFllet[@inlineavailable]lognotn=logxorn(-1l)letunsigned_to_int=matchSys.word_sizewith|32->letmax_int=of_intStdlib.max_intinfun[@inlineavailable]n->ifn>=0l&&n<=max_intthenSome(to_intn)elseNone|64->(* So that it compiles in 32-bit *)letmask=0xFFFFlsl16lor0xFFFFinfun[@inlineavailable]n->Some(to_intnlandmask)|_->assertfalseexternalformat:string->int32->string@@portable="caml_int32_format"let[@inlineavailable]to_stringn=format"%d"nexternalof_string:string->(int32[@unboxed])@@portable="caml_int32_of_string""caml_int32_of_string_unboxed"let[@inlineavailable]of_string_opts=trySome(of_strings)withFailure_->Nonetypet=int32let[@inlineavailable]compare(x:t)(y:t)=Stdlib.comparexylet[@inlineavailable]equal(x:t)(y:t)=x=ylet[@inlineavailable]unsigned_comparenm=compare(subnmin_int)(submmin_int)letunsigned_ltnm=subnmin_int<submmin_intlet[@inlineavailable]minxy:t=ifx<=ythenxelseylet[@inlineavailable]maxxy:t=ifx>=ythenxelsey(* Unsigned division from signed division of the same bitness.
See Warren Jr., Henry S. (2013). Hacker's Delight (2 ed.), Sec 9-3.
*)let[@inlineavailable]unsigned_divnd=ifd<zerothenifunsigned_ltndthenzeroelseoneelseletq=shift_left(div(shift_right_logicaln1)d)1inletr=subn(mulqd)inifunsigned_ltrdthenqelsesuccqlet[@inlineavailable]unsigned_remnd=subn(mul((unsigned_div[@inlined])nd)d)(* [caml_hash_exn] doesn't raise on int32s, so it's safe for
it to be marked as [@@noalloc].
*)externalseeded_hash_param:int->int->int->int32->int@@portable="caml_hash_exn"[@@noalloc]letseeded_hashseedx=seeded_hash_param10100seedxlethashx=seeded_hash_param101000x