123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2020 Nomadic Labs, <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)(* let () = assert (Sys.int_size = 63) *)type_t=inttypemul_safetypemay_saturateletmay_saturate:_t->may_saturatet=funx->xletto_intx=xlet(<):_t->_t->bool=Compare.Int.(<)let(<=):_t->_t->bool=Compare.Int.(<=)let(>):_t->_t->bool=Compare.Int.(>)let(>=):_t->_t->bool=Compare.Int.(>=)let(=):_t->_t->bool=Compare.Int.(=)letequal=(=)let(<>):_t->_t->bool=Compare.Int.(<>)letmax:_t->_t->_t=funxy->ifx>=ythenxelseyletmin:_t->_t->_t=funxy->ifx>=ythenyelsexletcompare:_t->_t->_t=Compare.Int.compareletsaturated=max_intletof_int_optt=ift>=0&&t<saturatedthenSometelseNoneletof_z_optz=matchZ.to_intzwithint->of_int_optint|exceptionZ.Overflow->Noneletto_zx=Z.of_intxletsaturate_if_undef=functionNone->saturated|Somex->xletsafe_intx=of_int_optx|>saturate_if_undefletnumbitsx=letx=refxandn=ref0in(lety=!xlsr32inify<>0then(n:=!n+32;x:=y));(lety=!xlsr16inify<>0then(n:=!n+16;x:=y));(lety=!xlsr8inify<>0then(n:=!n+8;x:=y));(lety=!xlsr4inify<>0then(n:=!n+4;x:=y));(lety=!xlsr2inify<>0then(n:=!n+2;x:=y));if!xlsr1<>0then!n+2else!n+!xletzero=0letone=1letsmall_enoughz=(* The following literal triggers an error if compiled under 32-bit
architectures, please do not modify it. This is a static way to
ensure that this file is compiled under a 64-bit architecture. *)zland0x7fffffff80000000=0letmul_safex=ifsmall_enoughxthenSomexelseNoneletmul_safe_exnx=ifsmall_enoughxthenxelsefailwith(Format.sprintf"mul_safe_exn: %d must be below 2147483648"x)letmul_safe_of_int_exnx=Option.bind(of_int_optx)mul_safe|>function|None->failwith(Format.sprintf"mul_safe_of_int_exn: %d must be below 2147483648"x)|Somex->x(* If [x] is positive, shifting to the right will produce a number
which is positive and is less than [x]. *)letshift_rightxy=(x:>int)lsryletshift_leftxy=ifshift_rightsaturatedy<xthensaturatedelse(x:>int)lslyletmulxy=(* assert (x >= 0 && y >= 0); *)matchxwith|0->0|x->ifsmall_enoughx&&small_enoughythenx*yelseifCompare.Int.(y>saturated/x)thensaturatedelsex*yletmul_fastxy=x*yletscale_fastxy=ifx=0then0elseifsmall_enoughythenx*yelseifCompare.Int.(y>saturated/x)thensaturatedelsex*yletaddxy=letz=x+yinifCompare.Int.(z>=0)thenzelsesaturatedletsuccx=addonexletsubxy=Compare.Int.max(x-y)0letsub_optxy=lets=x-yinifCompare.Int.(s>=0)thenSomeselseNone(* Notice that Z.erem does not behave as mod on negative numbers.
Fortunately, the inhabitant of [t] are non-negative. *)leteremxy=xmodyletedivxy=x/ylett_to_z_exnz=matchof_z_optzwith|None->(* since the encoding is applied to values of type [t]. *)assertfalse|Somex->xletz_encoding=Data_encoding.(check_size9(convto_zt_to_z_exnz))letn_encoding=Data_encoding.(check_size9(convto_zt_to_z_exnn))letppfmtx=Format.pp_print_intfmtx