1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889includeInt32externalof_int32:int32->t="%identity"externalof_unsigned_int32:int32->t="%identity"externalto_int32:t->int32="%identity"externalto_unsigned_int32:t->int32="%identity"letto_int64=Int64.of_int32letof_int64=Int64.to_int32letppppf(x:t)=Format.fprintfppf"%ld"xletwithout_bit_sign(x:int)=ifx>=0thenxelsexland(lnot0x40000000)letinvalid_argfmt=Format.kasprintfinvalid_argfmt(* XXX(dinosaure): the diff between [to_int] and [to_unsigned_int]
* is about the sign-bit [0x40000000][int]/[0x80000000][int32].
*
* For [to_int], we ensure for a negative number that we use only
* [0x3fffffff][int32] bits two most significant bits are set to [1].
* In that case, it safes to cast the [int32] to and [int] (31 bits).
*
* For [to_unsigned_int], we don't want to interpret if the value is
* negative or positive. However, if the number can be interpreted as a
* negative nnumber, due to the two's complement layout, we are sure
* to lost, at least, the most significant bit which is a part of unsigned
* [int32]. So we are able to only accept "positive" numbers.
*
* NOTE: we trust on the two's complement! *)letto_intx=letmax_int=of_intStdlib.max_intinifcomparezerox<=0&&comparexmax_int<=0thento_intx(* XXX(dinosaure): positive and can fit into a 31-bit integer. *)elseifcomparezerox>0&&Int32.logand0xC0000000lx=0xC0000000lthenletx=Int32.logandx0x7ffffffflinto_intxelseinvalid_arg"Optint.to_int: %lx can not fit into a 31 bits integer"xletto_unsigned_intx=letmax_int=of_intStdlib.max_intinifcomparezerox<=0&&comparexmax_int<=0thento_intxelseinvalid_arg"Optint.to_unsigned_int: %lx can not fit into a 31 bits unsigned integer"xletof_intx=ifx<0thenlogor0xC0000000l(of_int(without_bit_signx))elseof_intxletof_unsigned_intx=ifx<0thenlogor0x40000000l(of_int(without_bit_signx))elseof_intxletencoded_size=4externalset_32:bytes->int->int32->unit="%caml_bytes_set32u"externalget_32:string->int->int32="%caml_string_get32"externalswap32:int32->int32="%bswap_int32"letencodebuf~offt=lett=to_int32tinlett=ifnotSys.big_endianthenswap32telsetinset_32bufofftletdecodebuf~off=lett=get_32bufoffinlett=ifnotSys.big_endianthenswap32telsetinof_int32tmoduleInfix=structlet(+)ab=addablet(-)ab=subablet(*)ab=mulablet(%)ab=remablet(/)ab=divablet(land)ab=logandablet(lor)ab=logorablet(lsr)ab=shift_rightablet(lsl)ab=shift_leftablet(&&)=(land)let(||)=(lor)let(>>)=(lsr)let(<<)=(lsl)end