Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-foundation

Source of release 0.6.1

0.6.1 / src / ak / checked_2.lucb

#==============================================================================================
#
#   checked_2 - Checked<T>: the arithmetic of the signed instantiations
#
#   DESCRIPTION:
#       Port of AK/Checked.h (lines 118-386) for i64 and i32; see checked_1.
#
#==============================================================================================

# mark: i64 =====================================================================================

## Ported from AK/Checked.h:124 AK::Checked::Checked<T> (from another integer type), for i64:
## the value is truncated and the overflow flag set when it does not fit.
pub func checked_checked_t_i64(this: Checked[i64]*, value: i64):
    this.m_overflow = not is_within_range_i64(value)
    this.m_value = (i64)value

## Ported from AK/Checked.h:124 AK::Checked::Checked<T> (from an unsigned 64-bit value), for i64.
pub func checked_checked_t_u64_i64(this: Checked[i64]*, value: u64):
    this.m_overflow = not is_within_range_u64_i64(value)
    this.m_value = (i64)value

## Ported from AK/Checked.h:159 AK::Checked::operator!, for i64.
pub func checked_not_i64(this: const Checked[i64]*) -> bool:
    assert(not this.m_overflow)
    return this.m_value == 0

## Ported from AK/Checked.h:176 AK::Checked::add, for i64.
pub func checked_add_i64(this: Checked[i64]*, other: i64):
    let overflowed = (this.m_value +? other) == none
    this.m_value = this.m_value +% other
    this.m_overflow = this.m_overflow or overflowed

## Ported from AK/Checked.h:181 AK::Checked::sub, for i64.
pub func checked_sub_i64(this: Checked[i64]*, other: i64):
    let overflowed = (this.m_value -? other) == none
    this.m_value = this.m_value -% other
    this.m_overflow = this.m_overflow or overflowed

## Ported from AK/Checked.h:186 AK::Checked::mul, for i64.
pub func checked_mul_i64(this: Checked[i64]*, other: i64):
    let overflowed = (this.m_value *? other) == none
    this.m_value = this.m_value *% other
    this.m_overflow = this.m_overflow or overflowed

## Ported from AK/Checked.h:191 AK::Checked::div, for i64.
pub func checked_div_i64(this: Checked[i64]*, other: i64):
    # Ensure that the resulting value won't be out of range, this can only happen when dividing by -1.
    if other == -1 and this.m_value == i64_min:
        this.m_overflow = true
        return
    if other == 0:
        this.m_overflow = true
        return
    this.m_value = this.m_value // other

## Ported from AK/Checked.h:207 AK::Checked::mod, for i64.
pub func checked_mod_i64(this: Checked[i64]*, other: i64):
    let initial = this.m_value
    checked_div_i64(this, other)
    this.m_value = this.m_value *% other
    this.m_value = initial -% this.m_value

## Ported from AK/Checked.h:215 AK::Checked::operator+=(Checked), for i64.
pub func checked_add_assign_checked_i64(this: Checked[i64]*, other: Checked[i64]) -> Checked[i64]*:
    this.m_overflow = this.m_overflow or other.m_overflow
    checked_add_i64(this, checked_value_unchecked(&other))
    return this

## Ported from AK/Checked.h:222 AK::Checked::operator+=(T), for i64.
pub func checked_add_assign_t_i64(this: Checked[i64]*, other: i64) -> Checked[i64]*:
    checked_add_i64(this, other)
    return this

## Ported from AK/Checked.h:228 AK::Checked::operator-=(Checked), for i64.
pub func checked_sub_assign_checked_i64(this: Checked[i64]*, other: Checked[i64]) -> Checked[i64]*:
    this.m_overflow = this.m_overflow or other.m_overflow
    checked_sub_i64(this, checked_value_unchecked(&other))
    return this

## Ported from AK/Checked.h:235 AK::Checked::operator-=(T), for i64.
pub func checked_sub_assign_t_i64(this: Checked[i64]*, other: i64) -> Checked[i64]*:
    checked_sub_i64(this, other)
    return this

## Ported from AK/Checked.h:241 AK::Checked::operator*=(Checked), for i64.
pub func checked_mul_assign_checked_i64(this: Checked[i64]*, other: Checked[i64]) -> Checked[i64]*:
    this.m_overflow = this.m_overflow or other.m_overflow
    checked_mul_i64(this, checked_value_unchecked(&other))
    return this

## Ported from AK/Checked.h:248 AK::Checked::operator*=(T), for i64.
pub func checked_mul_assign_t_i64(this: Checked[i64]*, other: i64) -> Checked[i64]*:
    checked_mul_i64(this, other)
    return this

## Ported from AK/Checked.h:254 AK::Checked::operator/=(Checked), for i64.
pub func checked_div_assign_checked_i64(this: Checked[i64]*, other: Checked[i64]) -> Checked[i64]*:
    this.m_overflow = this.m_overflow or other.m_overflow
    checked_div_i64(this, checked_value_unchecked(&other))
    return this

## Ported from AK/Checked.h:261 AK::Checked::operator/=(T), for i64.
pub func checked_div_assign_t_i64(this: Checked[i64]*, other: i64) -> Checked[i64]*:
    checked_div_i64(this, other)
    return this

## Ported from AK/Checked.h:267 AK::Checked::operator%=(Checked), for i64.
pub func checked_rem_assign_checked_i64(this: Checked[i64]*, other: Checked[i64]) -> Checked[i64]*:
    this.m_overflow = this.m_overflow or other.m_overflow
    checked_mod_i64(this, checked_value_unchecked(&other))
    return this

## Ported from AK/Checked.h:274 AK::Checked::operator%=(T), for i64.
pub func checked_rem_assign_t_i64(this: Checked[i64]*, other: i64) -> Checked[i64]*:
    checked_mod_i64(this, other)
    return this

## Ported from AK/Checked.h:280 AK::Checked::operator++ (prefix), for i64.
pub func checked_inc_i64(this: Checked[i64]*) -> Checked[i64]*:
    checked_add_i64(this, 1)
    return this

## Ported from AK/Checked.h:286 AK::Checked::operator++ (postfix), for i64.
pub func checked_inc_i32_i64(this: Checked[i64]*, arg0: i32) -> Checked[i64]:
    _ = arg0
    let old = *this
    checked_add_i64(this, 1)
    return old

## Ported from AK/Checked.h:293 AK::Checked::operator-- (prefix), for i64.
pub func checked_dec_i64(this: Checked[i64]*) -> Checked[i64]*:
    checked_sub_i64(this, 1)
    return this

## Ported from AK/Checked.h:299 AK::Checked::operator-- (postfix), for i64.
pub func checked_dec_i32_i64(this: Checked[i64]*, arg0: i32) -> Checked[i64]:
    _ = arg0
    let old = *this
    checked_sub_i64(this, 1)
    return old

## Ported from AK/Checked.h:307 AK::Checked::addition_would_overflow, for i64.
pub func checked_addition_would_overflow_i64(u: i64, v: i64) -> bool:
    return (u +? v) == none

## Ported from AK/Checked.h:327 AK::Checked::subtraction_would_overflow, for i64.
pub func checked_subtraction_would_overflow_i64(u: i64, v: i64) -> bool:
    return (u -? v) == none

## Ported from AK/Checked.h:338 AK::Checked::multiplication_would_overflow, for i64.
pub func checked_multiplication_would_overflow_i64(u: i64, v: i64) -> bool:
    return (u *? v) == none

## Ported from AK/Checked.h:354 AK::operator+(Checked, Checked), for i64.
pub func add_i64(a: Checked[i64], b: Checked[i64]) -> Checked[i64]:
    var c = a
    _ = checked_add_assign_checked_i64(&c, b)
    return c

## Ported from AK/Checked.h:362 AK::operator-(Checked, Checked), for i64.
pub func sub_i64(a: Checked[i64], b: Checked[i64]) -> Checked[i64]:
    var c = a
    _ = checked_sub_assign_checked_i64(&c, b)
    return c

## Ported from AK/Checked.h:370 AK::operator*(Checked, Checked), for i64.
pub func mul_i64(a: Checked[i64], b: Checked[i64]) -> Checked[i64]:
    var c = a
    _ = checked_mul_assign_checked_i64(&c, b)
    return c

## Ported from AK/Checked.h:378 AK::operator/(Checked, Checked), for i64.
pub func div_i64(a: Checked[i64], b: Checked[i64]) -> Checked[i64]:
    var c = a
    _ = checked_div_assign_checked_i64(&c, b)
    return c

## Ported from AK/Checked.h:386 AK::operator%(Checked, Checked), for i64.
pub func rem_i64(a: Checked[i64], b: Checked[i64]) -> Checked[i64]:
    var c = a
    _ = checked_rem_assign_checked_i64(&c, b)
    return c

# mark: i32 =====================================================================================

## Ported from AK/Checked.h:124 AK::Checked::Checked<T> (from another integer type), for i32:
## the value is truncated and the overflow flag set when it does not fit.
pub func checked_checked_t_i32(this: Checked[i32]*, value: i64):
    this.m_overflow = not is_within_range_i32(value)
    this.m_value = (i32)value

## Ported from AK/Checked.h:124 AK::Checked::Checked<T> (from an unsigned 64-bit value), for i32.
pub func checked_checked_t_u64_i32(this: Checked[i32]*, value: u64):
    this.m_overflow = not is_within_range_u64_i32(value)
    this.m_value = (i32)value

## Ported from AK/Checked.h:159 AK::Checked::operator!, for i32.
pub func checked_not_i32(this: const Checked[i32]*) -> bool:
    assert(not this.m_overflow)
    return this.m_value == 0

## Ported from AK/Checked.h:176 AK::Checked::add, for i32.
pub func checked_add_i32(this: Checked[i32]*, other: i32):
    let overflowed = (this.m_value +? other) == none
    this.m_value = this.m_value +% other
    this.m_overflow = this.m_overflow or overflowed

## Ported from AK/Checked.h:181 AK::Checked::sub, for i32.
pub func checked_sub_i32(this: Checked[i32]*, other: i32):
    let overflowed = (this.m_value -? other) == none
    this.m_value = this.m_value -% other
    this.m_overflow = this.m_overflow or overflowed

## Ported from AK/Checked.h:186 AK::Checked::mul, for i32.
pub func checked_mul_i32(this: Checked[i32]*, other: i32):
    let overflowed = (this.m_value *? other) == none
    this.m_value = this.m_value *% other
    this.m_overflow = this.m_overflow or overflowed

## Ported from AK/Checked.h:191 AK::Checked::div, for i32.
pub func checked_div_i32(this: Checked[i32]*, other: i32):
    # Ensure that the resulting value won't be out of range, this can only happen when dividing by -1.
    if other == -1 and this.m_value == i32_min:
        this.m_overflow = true
        return
    if other == 0:
        this.m_overflow = true
        return
    this.m_value = this.m_value // other

## Ported from AK/Checked.h:207 AK::Checked::mod, for i32.
pub func checked_mod_i32(this: Checked[i32]*, other: i32):
    let initial = this.m_value
    checked_div_i32(this, other)
    this.m_value = this.m_value *% other
    this.m_value = initial -% this.m_value

## Ported from AK/Checked.h:215 AK::Checked::operator+=(Checked), for i32.
pub func checked_add_assign_checked_i32(this: Checked[i32]*, other: Checked[i32]) -> Checked[i32]*:
    this.m_overflow = this.m_overflow or other.m_overflow
    checked_add_i32(this, checked_value_unchecked(&other))
    return this

## Ported from AK/Checked.h:222 AK::Checked::operator+=(T), for i32.
pub func checked_add_assign_t_i32(this: Checked[i32]*, other: i32) -> Checked[i32]*:
    checked_add_i32(this, other)
    return this

## Ported from AK/Checked.h:228 AK::Checked::operator-=(Checked), for i32.
pub func checked_sub_assign_checked_i32(this: Checked[i32]*, other: Checked[i32]) -> Checked[i32]*:
    this.m_overflow = this.m_overflow or other.m_overflow
    checked_sub_i32(this, checked_value_unchecked(&other))
    return this

## Ported from AK/Checked.h:235 AK::Checked::operator-=(T), for i32.
pub func checked_sub_assign_t_i32(this: Checked[i32]*, other: i32) -> Checked[i32]*:
    checked_sub_i32(this, other)
    return this

## Ported from AK/Checked.h:241 AK::Checked::operator*=(Checked), for i32.
pub func checked_mul_assign_checked_i32(this: Checked[i32]*, other: Checked[i32]) -> Checked[i32]*:
    this.m_overflow = this.m_overflow or other.m_overflow
    checked_mul_i32(this, checked_value_unchecked(&other))
    return this

## Ported from AK/Checked.h:248 AK::Checked::operator*=(T), for i32.
pub func checked_mul_assign_t_i32(this: Checked[i32]*, other: i32) -> Checked[i32]*:
    checked_mul_i32(this, other)
    return this

## Ported from AK/Checked.h:254 AK::Checked::operator/=(Checked), for i32.
pub func checked_div_assign_checked_i32(this: Checked[i32]*, other: Checked[i32]) -> Checked[i32]*:
    this.m_overflow = this.m_overflow or other.m_overflow
    checked_div_i32(this, checked_value_unchecked(&other))
    return this

## Ported from AK/Checked.h:261 AK::Checked::operator/=(T), for i32.
pub func checked_div_assign_t_i32(this: Checked[i32]*, other: i32) -> Checked[i32]*:
    checked_div_i32(this, other)
    return this

## Ported from AK/Checked.h:267 AK::Checked::operator%=(Checked), for i32.
pub func checked_rem_assign_checked_i32(this: Checked[i32]*, other: Checked[i32]) -> Checked[i32]*:
    this.m_overflow = this.m_overflow or other.m_overflow
    checked_mod_i32(this, checked_value_unchecked(&other))
    return this

## Ported from AK/Checked.h:274 AK::Checked::operator%=(T), for i32.
pub func checked_rem_assign_t_i32(this: Checked[i32]*, other: i32) -> Checked[i32]*:
    checked_mod_i32(this, other)
    return this

## Ported from AK/Checked.h:280 AK::Checked::operator++ (prefix), for i32.
pub func checked_inc_i32(this: Checked[i32]*) -> Checked[i32]*:
    checked_add_i32(this, 1)
    return this

## Ported from AK/Checked.h:286 AK::Checked::operator++ (postfix), for i32.
pub func checked_inc_i32_i32(this: Checked[i32]*, arg0: i32) -> Checked[i32]:
    _ = arg0
    let old = *this
    checked_add_i32(this, 1)
    return old

## Ported from AK/Checked.h:293 AK::Checked::operator-- (prefix), for i32.
pub func checked_dec_i32(this: Checked[i32]*) -> Checked[i32]*:
    checked_sub_i32(this, 1)
    return this

## Ported from AK/Checked.h:299 AK::Checked::operator-- (postfix), for i32.
pub func checked_dec_i32_i32(this: Checked[i32]*, arg0: i32) -> Checked[i32]:
    _ = arg0
    let old = *this
    checked_sub_i32(this, 1)
    return old

## Ported from AK/Checked.h:307 AK::Checked::addition_would_overflow, for i32.
pub func checked_addition_would_overflow_i32(u: i32, v: i32) -> bool:
    return (u +? v) == none

## Ported from AK/Checked.h:327 AK::Checked::subtraction_would_overflow, for i32.
pub func checked_subtraction_would_overflow_i32(u: i32, v: i32) -> bool:
    return (u -? v) == none

## Ported from AK/Checked.h:338 AK::Checked::multiplication_would_overflow, for i32.
pub func checked_multiplication_would_overflow_i32(u: i32, v: i32) -> bool:
    return (u *? v) == none

## Ported from AK/Checked.h:354 AK::operator+(Checked, Checked), for i32.
pub func add_i32(a: Checked[i32], b: Checked[i32]) -> Checked[i32]:
    var c = a
    _ = checked_add_assign_checked_i32(&c, b)
    return c

## Ported from AK/Checked.h:362 AK::operator-(Checked, Checked), for i32.
pub func sub_i32(a: Checked[i32], b: Checked[i32]) -> Checked[i32]:
    var c = a
    _ = checked_sub_assign_checked_i32(&c, b)
    return c

## Ported from AK/Checked.h:370 AK::operator*(Checked, Checked), for i32.
pub func mul_i32(a: Checked[i32], b: Checked[i32]) -> Checked[i32]:
    var c = a
    _ = checked_mul_assign_checked_i32(&c, b)
    return c

## Ported from AK/Checked.h:378 AK::operator/(Checked, Checked), for i32.
pub func div_i32(a: Checked[i32], b: Checked[i32]) -> Checked[i32]:
    var c = a
    _ = checked_div_assign_checked_i32(&c, b)
    return c

## Ported from AK/Checked.h:386 AK::operator%(Checked, Checked), for i32.
pub func rem_i32(a: Checked[i32], b: Checked[i32]) -> Checked[i32]:
    var c = a
    _ = checked_rem_assign_checked_i32(&c, b)
    return c