Current section
Files
Jump to
Current section
Files
src/ranged_int@utils.erl
-module(ranged_int@utils).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([overflow/3]).
-export_type([overflow/0]).
-type overflow() :: {would_overflow, bigi:big_int()} |
{would_underflow, bigi:big_int()}.
-spec overflow(overflow(), bigi:big_int(), bigi:big_int()) -> bigi:big_int().
overflow(Overflow, Min, Max) ->
Total_values = begin
_pipe = bigi_ffi:subtract(Max, Min),
bigi_ffi:add(_pipe, bigi_ffi:from(1))
end,
Actual = case Overflow of
{would_overflow, Amount} ->
bigi_ffi:add(Max, Amount);
{would_underflow, Amount@1} ->
bigi_ffi:subtract(Min, Amount@1)
end,
_pipe@1 = Actual,
_pipe@2 = bigi_ffi:subtract(_pipe@1, Min),
_pipe@3 = bigi_ffi:modulo(_pipe@2, Total_values),
bigi_ffi:add(_pipe@3, Min).