Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-foundation

Source of release 0.6.0

0.6.0 / src / ak / integral_math.lucb

#==============================================================================================
#
#   integral_math - exp2, log2, ceil_log2, reinterpret_as_octal, gcd and lcm on integers
#
#   DESCRIPTION:
#       Port of AK/IntegralMath.h (lines 1-145), each template instantiated per integer type
#       with the type as a suffix (`ceil_log2_usize`), and of the count_leading_zeroes of
#       AK/BuiltinWrappers.h that log2 uses.
#
#==============================================================================================

# mark: count_leading_zeroes ====================================================================

# BuiltinWrappers.h count_leading_zeroes for u8 (__builtin_clz*: undefined for zero).
func count_leading_zeroes_u8(value: u8) -> i32:
    assert(value != 0)
    var count: i32 = 0
    var bit: u8 = (u8)1 << (8 - 1)
    while (value & bit) == 0:
        count += 1
        bit = bit >> 1
    return count

# BuiltinWrappers.h count_leading_zeroes for u16 (__builtin_clz*: undefined for zero).
func count_leading_zeroes_u16(value: u16) -> i32:
    assert(value != 0)
    var count: i32 = 0
    var bit: u16 = (u16)1 << (16 - 1)
    while (value & bit) == 0:
        count += 1
        bit = bit >> 1
    return count

# BuiltinWrappers.h count_leading_zeroes for u32 (__builtin_clz*: undefined for zero).
func count_leading_zeroes_u32(value: u32) -> i32:
    assert(value != 0)
    var count: i32 = 0
    var bit: u32 = (u32)1 << (32 - 1)
    while (value & bit) == 0:
        count += 1
        bit = bit >> 1
    return count

# BuiltinWrappers.h count_leading_zeroes for u64 (__builtin_clz*: undefined for zero).
func count_leading_zeroes_u64(value: u64) -> i32:
    assert(value != 0)
    var count: i32 = 0
    var bit: u64 = (u64)1 << (64 - 1)
    while (value & bit) == 0:
        count += 1
        bit = bit >> 1
    return count

# BuiltinWrappers.h count_leading_zeroes for usize (__builtin_clz*: undefined for zero).
func count_leading_zeroes_usize(value: usize) -> i32:
    assert(value != 0)
    var count: i32 = 0
    var bit: usize = (usize)1 << (64 - 1)
    while (value & bit) == 0:
        count += 1
        bit = bit >> 1
    return count

# mark: exp2, log2, ceil_log2 ===================================================================

## Ported from AK/IntegralMath.h:16 AK::exp2, for i8.
pub func exp2_i8(exponent: i8) -> i8:
    return (i8)1 << exponent

## Ported from AK/IntegralMath.h:16 AK::exp2, for i16.
pub func exp2_i16(exponent: i16) -> i16:
    return (i16)1 << exponent

## Ported from AK/IntegralMath.h:16 AK::exp2, for i32.
pub func exp2_i32(exponent: i32) -> i32:
    return (i32)1 << exponent

## Ported from AK/IntegralMath.h:16 AK::exp2, for i64.
pub func exp2_i64(exponent: i64) -> i64:
    return (i64)1 << exponent

## Ported from AK/IntegralMath.h:16 AK::exp2, for u8.
pub func exp2_u8(exponent: u8) -> u8:
    return (u8)1 << exponent

## Ported from AK/IntegralMath.h:16 AK::exp2, for u16.
pub func exp2_u16(exponent: u16) -> u16:
    return (u16)1 << exponent

## Ported from AK/IntegralMath.h:16 AK::exp2, for u32.
pub func exp2_u32(exponent: u32) -> u32:
    return (u32)1 << exponent

## Ported from AK/IntegralMath.h:16 AK::exp2, for u64.
pub func exp2_u64(exponent: u64) -> u64:
    return (u64)1 << exponent

## Ported from AK/IntegralMath.h:16 AK::exp2, for usize.
pub func exp2_usize(exponent: usize) -> usize:
    return (usize)1 << exponent

## Ported from AK/IntegralMath.h:22 AK::log2, for i8.
pub func log2_i8(x: i8) -> i8:
    if x == 0:
        return 0
    return (i8)((8 - 1) - count_leading_zeroes_u8((u8)x))

## Ported from AK/IntegralMath.h:22 AK::log2, for i16.
pub func log2_i16(x: i16) -> i16:
    if x == 0:
        return 0
    return (i16)((16 - 1) - count_leading_zeroes_u16((u16)x))

## Ported from AK/IntegralMath.h:22 AK::log2, for i32.
pub func log2_i32(x: i32) -> i32:
    if x == 0:
        return 0
    return (i32)((32 - 1) - count_leading_zeroes_u32((u32)x))

## Ported from AK/IntegralMath.h:22 AK::log2, for i64.
pub func log2_i64(x: i64) -> i64:
    if x == 0:
        return 0
    return (i64)((64 - 1) - count_leading_zeroes_u64((u64)x))

## Ported from AK/IntegralMath.h:22 AK::log2, for u8.
pub func log2_u8(x: u8) -> u8:
    if x == 0:
        return 0
    return (u8)((8 - 1) - count_leading_zeroes_u8((u8)x))

## Ported from AK/IntegralMath.h:22 AK::log2, for u16.
pub func log2_u16(x: u16) -> u16:
    if x == 0:
        return 0
    return (u16)((16 - 1) - count_leading_zeroes_u16((u16)x))

## Ported from AK/IntegralMath.h:22 AK::log2, for u32.
pub func log2_u32(x: u32) -> u32:
    if x == 0:
        return 0
    return (u32)((32 - 1) - count_leading_zeroes_u32((u32)x))

## Ported from AK/IntegralMath.h:22 AK::log2, for u64.
pub func log2_u64(x: u64) -> u64:
    if x == 0:
        return 0
    return (u64)((64 - 1) - count_leading_zeroes_u64((u64)x))

## Ported from AK/IntegralMath.h:22 AK::log2, for usize.
pub func log2_usize(x: usize) -> usize:
    if x == 0:
        return 0
    return (usize)((64 - 1) - count_leading_zeroes_usize((usize)x))

## Ported from AK/IntegralMath.h:28 AK::ceil_log2, for i8.
pub func ceil_log2_i8(x: i8) -> i8:
    if x <= 1:
        return 0
    return log2_i8(x - 1) + 1

## Ported from AK/IntegralMath.h:28 AK::ceil_log2, for i16.
pub func ceil_log2_i16(x: i16) -> i16:
    if x <= 1:
        return 0
    return log2_i16(x - 1) + 1

## Ported from AK/IntegralMath.h:28 AK::ceil_log2, for i32.
pub func ceil_log2_i32(x: i32) -> i32:
    if x <= 1:
        return 0
    return log2_i32(x - 1) + 1

## Ported from AK/IntegralMath.h:28 AK::ceil_log2, for i64.
pub func ceil_log2_i64(x: i64) -> i64:
    if x <= 1:
        return 0
    return log2_i64(x - 1) + 1

## Ported from AK/IntegralMath.h:28 AK::ceil_log2, for u8.
pub func ceil_log2_u8(x: u8) -> u8:
    if x <= 1:
        return 0
    return log2_u8(x - 1) + 1

## Ported from AK/IntegralMath.h:28 AK::ceil_log2, for u16.
pub func ceil_log2_u16(x: u16) -> u16:
    if x <= 1:
        return 0
    return log2_u16(x - 1) + 1

## Ported from AK/IntegralMath.h:28 AK::ceil_log2, for u32.
pub func ceil_log2_u32(x: u32) -> u32:
    if x <= 1:
        return 0
    return log2_u32(x - 1) + 1

## Ported from AK/IntegralMath.h:28 AK::ceil_log2, for u64.
pub func ceil_log2_u64(x: u64) -> u64:
    if x <= 1:
        return 0
    return log2_u64(x - 1) + 1

## Ported from AK/IntegralMath.h:28 AK::ceil_log2, for usize.
pub func ceil_log2_usize(x: usize) -> usize:
    if x <= 1:
        return 0
    return log2_usize(x - 1) + 1

# mark: reinterpret_as_octal, gcd, lcm ==========================================================

## Ported from AK/IntegralMath.h:77 AK::reinterpret_as_octal, for u32.
pub func reinterpret_as_octal_u32(decimal: u32) -> u32:
    var value = decimal
    var result: u32 = 0
    var n: u32 = 0
    while value > 0:
        result += pow_integral_u32(8, n) * (value % 10)
        n += 1
        value = value // 10
    return result

## Ported from AK/IntegralMath.h:77 AK::reinterpret_as_octal, for u64.
pub func reinterpret_as_octal_u64(decimal: u64) -> u64:
    var value = decimal
    var result: u64 = 0
    var n: u64 = 0
    while value > 0:
        result += pow_integral_u64(8, n) * (value % 10)
        n += 1
        value = value // 10
    return result

## Ported from AK/IntegralMath.h:77 AK::reinterpret_as_octal, for usize.
pub func reinterpret_as_octal_usize(decimal: usize) -> usize:
    var value = decimal
    var result: usize = 0
    var n: usize = 0
    while value > 0:
        result += pow_integral_usize(8, n) * (value % 10)
        n += 1
        value = value // 10
    return result

## Ported from AK/IntegralMath.h:89 AK::gcd (unsigned), for u32.
pub func gcd_u32(x: u32, y: u32) -> u32:
    var a = x
    var b = y
    if a == 0:
        return b
    if b == 0:
        return a
    var shift: u32 = 0
    while ((a | b) & 1) == 0:
        a = a >> 1
        b = b >> 1
        shift += 1
    while a != b:
        if (a & 1) != 0:
            if (b & 1) != 0:
                if a > b:
                    a -= b
                else:
                    b -= a
            else:
                b = b >> 1
        else:
            a = a >> 1
            if (b & 1) != 0:
                if a < b:
                    swap_5(&a, &b)
    return a << shift

## Ported from AK/IntegralMath.h:89 AK::gcd (unsigned), for u64.
pub func gcd_u64(x: u64, y: u64) -> u64:
    var a = x
    var b = y
    if a == 0:
        return b
    if b == 0:
        return a
    var shift: u64 = 0
    while ((a | b) & 1) == 0:
        a = a >> 1
        b = b >> 1
        shift += 1
    while a != b:
        if (a & 1) != 0:
            if (b & 1) != 0:
                if a > b:
                    a -= b
                else:
                    b -= a
            else:
                b = b >> 1
        else:
            a = a >> 1
            if (b & 1) != 0:
                if a < b:
                    swap_5(&a, &b)
    return a << shift

## Ported from AK/IntegralMath.h:89 AK::gcd (unsigned), for usize.
pub func gcd_usize(x: usize, y: usize) -> usize:
    var a = x
    var b = y
    if a == 0:
        return b
    if b == 0:
        return a
    var shift: usize = 0
    while ((a | b) & 1) == 0:
        a = a >> 1
        b = b >> 1
        shift += 1
    while a != b:
        if (a & 1) != 0:
            if (b & 1) != 0:
                if a > b:
                    a -= b
                else:
                    b -= a
            else:
                b = b >> 1
        else:
            a = a >> 1
            if (b & 1) != 0:
                if a < b:
                    swap_5(&a, &b)
    return a << shift

## Ported from AK/IntegralMath.h:132 AK::lcm (unsigned), for u32.
pub func lcm_u32(x: u32, y: u32) -> u32:
    if x == 0 or y == 0:
        return 0
    return x // gcd_u32(x, y) * y

## Ported from AK/IntegralMath.h:132 AK::lcm (unsigned), for u64.
pub func lcm_u64(x: u64, y: u64) -> u64:
    if x == 0 or y == 0:
        return 0
    return x // gcd_u64(x, y) * y

## Ported from AK/IntegralMath.h:132 AK::lcm (unsigned), for usize.
pub func lcm_usize(x: usize, y: usize) -> usize:
    if x == 0 or y == 0:
        return 0
    return x // gcd_usize(x, y) * y

# IntegralMath.h:38 AK::pow<I> (integral exponentiation by squaring), for u32.
func pow_integral_u32(base: u32, exponent: u32) -> u32:
    # https://en.wikipedia.org/wiki/Exponentiation_by_squaring
    if exponent == 0:
        return 1
    var b = base
    var e = exponent
    var res: u32 = 1
    while e > 0:
        if (e & 1) != 0:
            res = res *% b
        b = b *% b
        e = e // 2
    return res

# IntegralMath.h:38 AK::pow<I> (integral exponentiation by squaring), for u64.
func pow_integral_u64(base: u64, exponent: u64) -> u64:
    # https://en.wikipedia.org/wiki/Exponentiation_by_squaring
    if exponent == 0:
        return 1
    var b = base
    var e = exponent
    var res: u64 = 1
    while e > 0:
        if (e & 1) != 0:
            res = res *% b
        b = b *% b
        e = e // 2
    return res

# IntegralMath.h:38 AK::pow<I> (integral exponentiation by squaring), for usize.
func pow_integral_usize(base: usize, exponent: usize) -> usize:
    # https://en.wikipedia.org/wiki/Exponentiation_by_squaring
    if exponent == 0:
        return 1
    var b = base
    var e = exponent
    var res: usize = 1
    while e > 0:
        if (e & 1) != 0:
            res = res *% b
        b = b *% b
        e = e // 2
    return res

## Ported from AK/IntegralMath.h:125 AK::gcd (signed), for i32.
pub func gcd_i32(x: i32, y: i32) -> i32:
    return (i32)gcd_u32((u32)abs_i32(x), (u32)abs_i32(y))

## Ported from AK/IntegralMath.h:125 AK::gcd (signed), for i64.
pub func gcd_i64(x: i64, y: i64) -> i64:
    return (i64)gcd_u64((u64)abs_i64(x), (u64)abs_i64(y))

## Ported from AK/IntegralMath.h:140 AK::lcm (signed), for i32.
pub func lcm_i32(x: i32, y: i32) -> i32:
    return (i32)lcm_u32((u32)abs_i32(x), (u32)abs_i32(y))

## Ported from AK/IntegralMath.h:140 AK::lcm (signed), for i64.
pub func lcm_i64(x: i64, y: i64) -> i64:
    return (i64)lcm_u64((u64)abs_i64(x), (u64)abs_i64(y))