-module(ranged_int@utils). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -define(FILEPATH, "src/ranged_int/utils.gleam"). -export([overflow/3]). -export_type([overflow/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -type overflow() :: {would_overflow, bigi:big_int()} | {would_underflow, bigi:big_int()}. -file("src/ranged_int/utils.gleam", 13). ?DOC(" Calculate the overflowed value given an overflow result and the limits.\n"). -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).