dymokomi/luce-browser-foundation
Source of release 0.6.1
0.6.1 / src / ak / fixed_point_3.lucb
#==============================================================================================
#
# fixed_point_3 - FixedPoint<precision, u64>
#
# DESCRIPTION:
# Port of AK/FixedPoint.h (lines 28-400) for the u64 underlying type; see fixed_point_1.
#
#==============================================================================================
# mark: FixedPoint<precision, u64> ==============================================================
# radix_mask for u64: the fraction bits.
func fixed_point_radix_mask_u64(precision: usize) -> u64:
return ((u64)1 << (u64)precision) -% 1
## Ported from AK/FixedPoint.h:41 AK::FixedPoint::FixedPoint(Integral), for u64: `value` in the
## integer part.
pub func fixed_point_fixed_point_precision_underlying_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*, value: i64):
let precision = fixed_point_precision[P]()
this.m_value = (u64)value << (u64)precision
## Ported from AK/FixedPoint.h:46 AK::FixedPoint::FixedPoint(FloatingPoint), for u64 and f64:
## rounded to nearest, ties to even.
pub func fixed_point_construct_f64_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*, value: f64):
let precision = fixed_point_precision[P]()
this.m_value = rounding_round_to_f64_u64(value * (f64)((u64)1 << (u64)precision))
## Ported from AK/FixedPoint.h:46 AK::FixedPoint::FixedPoint(FloatingPoint), for u64 and f32.
pub func fixed_point_construct_f32_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*, value: f32):
let precision = fixed_point_precision[P]()
this.m_value = rounding_round_to_f32_u64(value * (f32)((u64)1 << (u64)precision))
## Ported from AK/FixedPoint.h:59 AK::FixedPoint::operator F, for u64 and f64.
pub func fixed_point_to_f64_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> f64:
let precision = fixed_point_precision[P]()
return (f64)this.m_value * pow_f64(0.5, (f64)precision)
## Ported from AK/FixedPoint.h:59 AK::FixedPoint::operator F, for u64 and f32.
pub func fixed_point_to_f32_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> f32:
let precision = fixed_point_precision[P]()
return (f32)this.m_value * pow_f32(0.5, (f32)precision)
## Ported from AK/FixedPoint.h:65 AK::FixedPoint::operator I, for u64: the truncated integer part.
pub func fixed_point_to_integer_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> u64:
let precision = fixed_point_precision[P]()
let truncated = fixed_point_trunc_u64(this)
return truncated.m_value >> (u64)precision
## Ported from AK/FixedPoint.h:70 AK::FixedPoint::create_raw, for u64.
pub func fixed_point_create_raw_u64[P: FixedPointPrecision](value: u64) -> FixedPoint[u64, P]:
return FixedPoint[u64, P](m_value = value)
## Ported from AK/FixedPoint.h:77 AK::FixedPoint::raw, for u64.
pub func fixed_point_raw_const_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> u64:
return this.m_value
## Ported from AK/FixedPoint.h:81 AK::FixedPoint::raw, for u64.
pub func fixed_point_raw_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*) -> u64*:
return &this.m_value
## Ported from AK/FixedPoint.h:86 AK::FixedPoint::fract, for u64.
pub func fixed_point_fract_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> FixedPoint[u64, P]:
let precision = fixed_point_precision[P]()
return fixed_point_create_raw_u64[P](this.m_value & fixed_point_radix_mask_u64(precision))
## Ported from AK/FixedPoint.h:91 AK::FixedPoint::clamp, for u64.
pub func fixed_point_clamp_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, minimum: FixedPoint[u64, P], maximum: FixedPoint[u64, P]) -> FixedPoint[u64, P]:
if fixed_point_lt_fixed_point_u64(this, minimum):
return minimum
if fixed_point_gt_fixed_point_u64(this, maximum):
return maximum
return *this
## Ported from AK/FixedPoint.h:100 AK::FixedPoint::rint, for u64.
pub func fixed_point_rint_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> FixedPoint[u64, P]:
let precision = fixed_point_precision[P]()
let radix_mask = fixed_point_radix_mask_u64(precision)
# Note: Round fair, break tie to even
var value = this.m_value >> (u64)precision
# Note: For negative numbers the ordering are reversed,
# and they were already decremented by the shift, so we need to
# add 1 when we see a fract values behind the `.5`s place set,
# because that means they are smaller than .5
# fract(m_value) >= .5?
if (this.m_value & ((u64)1 << (u64)(precision - 1))) != 0:
# fract(m_value) > .5?
if (this.m_value & (radix_mask >> 1)) != 0:
# yes: round "up";
value = value +% 1
else:
# no: round to even;
value = value +% (value & 1)
var result: FixedPoint[u64, P] = ---
fixed_point_fixed_point_precision_underlying_u64(&result, (i64)value)
return result
## Ported from AK/FixedPoint.h:122 AK::FixedPoint::floor, for u64.
pub func fixed_point_floor_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> FixedPoint[u64, P]:
let precision = fixed_point_precision[P]()
return fixed_point_create_raw_u64[P](this.m_value & ~fixed_point_radix_mask_u64(precision))
## Ported from AK/FixedPoint.h:126 AK::FixedPoint::ceil, for u64.
pub func fixed_point_ceil_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> FixedPoint[u64, P]:
let precision = fixed_point_precision[P]()
let radix_mask = fixed_point_radix_mask_u64(precision)
let one: u64 = (u64)1 << (u64)precision
return fixed_point_create_raw_u64[P]((this.m_value & ~radix_mask) +% (one if (this.m_value & radix_mask) != 0 else 0))
## Ported from AK/FixedPoint.h:131 AK::FixedPoint::trunc, for u64.
pub func fixed_point_trunc_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> FixedPoint[u64, P]:
let precision = fixed_point_precision[P]()
let radix_mask = fixed_point_radix_mask_u64(precision)
let one: u64 = (u64)1 << (u64)precision
var adjustment: u64 = 0
if (this.m_value & radix_mask) != 0:
adjustment = 0 if this.m_value > 0 else one
return fixed_point_create_raw_u64[P]((this.m_value & ~radix_mask) +% adjustment)
## Ported from AK/FixedPoint.h:139 AK::FixedPoint::lrint, for u64.
pub func fixed_point_lrint_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> u64:
let precision = fixed_point_precision[P]()
let rounded = fixed_point_rint_u64(this)
return rounded.m_value >> (u64)precision
## Ported from AK/FixedPoint.h:140 AK::FixedPoint::lfloor, for u64.
pub func fixed_point_lfloor_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> u64:
let precision = fixed_point_precision[P]()
return this.m_value >> (u64)precision
## Ported from AK/FixedPoint.h:141 AK::FixedPoint::lceil, for u64.
pub func fixed_point_lceil_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> u64:
let precision = fixed_point_precision[P]()
let radix_mask = fixed_point_radix_mask_u64(precision)
return (this.m_value >> (u64)precision) +% (1 if (this.m_value & radix_mask) != 0 else 0)
## Ported from AK/FixedPoint.h:146 AK::FixedPoint::ltrunc, for u64.
pub func fixed_point_ltrunc_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> u64:
let precision = fixed_point_precision[P]()
let radix_mask = fixed_point_radix_mask_u64(precision)
var adjustment: u64 = 0
if (this.m_value & radix_mask) != 0:
adjustment = 0 if this.m_value > 0 else 1
return (this.m_value >> (u64)precision) +% adjustment
## Ported from AK/FixedPoint.h:155 AK::FixedPoint::log2, for u64.
# http://www.claysturner.com/dsp/BinaryLogarithm.pdf
pub func fixed_point_log2_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*) -> FixedPoint[u64, P]:
let precision = fixed_point_precision[P]()
# 0.5
var b = fixed_point_create_raw_u64[P]((u64)1 << (u64)(precision - 1))
var y: FixedPoint[u64, P] = ---
fixed_point_fixed_point_precision_underlying_u64(&y, 0)
var x = *this
# FIXME: There's no negative infinity.
if x.m_value <= 0:
return fixed_point_create_raw_u64[P](u64_min)
if fixed_point_ne_i_u64(&x, 1):
let shift_amount = (i32)log2_u64(x.m_value) - (i32)precision
if shift_amount > 0:
_ = fixed_point_shr_assign_u64(&x, (i64)shift_amount)
else:
_ = fixed_point_shl_assign_u64(&x, (i64)-shift_amount)
_ = fixed_point_add_assign_i_u64(&y, (i64)shift_amount)
for _i in 0..<precision:
_ = fixed_point_mul_assign_fixed_point_u64(&x, x)
if fixed_point_ge_i_u64(&x, 2):
_ = fixed_point_shr_assign_u64(&x, 1)
_ = fixed_point_add_assign_fixed_point_u64(&y, b)
_ = fixed_point_shr_assign_u64(&b, 1)
return y
## Ported from AK/FixedPoint.h:199 AK::FixedPoint::operator+(FixedPoint), for u64.
pub func fixed_point_add_fixed_point_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> FixedPoint[u64, P]:
return fixed_point_create_raw_u64[P](this.m_value +% other.m_value)
## Ported from AK/FixedPoint.h:203 AK::FixedPoint::operator-(FixedPoint), for u64.
pub func fixed_point_sub_fixed_point_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> FixedPoint[u64, P]:
return fixed_point_create_raw_u64[P](this.m_value -% other.m_value)
## Ported from AK/FixedPoint.h:207 AK::FixedPoint::operator*(FixedPoint), for u64: MulRes is a
## 128-bit integer, here U128 (both operands are non-negative).
pub func fixed_point_mul_fixed_point_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> FixedPoint[u64, P]:
let precision = fixed_point_precision[P]()
# FIXME: Figure out a nicer way to use more narrow types and avoid __int128
var value = U128(m_data = [this.m_value, 0])
_ = u_fixed_big_int_mul_assign_opaque(&value, other.m_value)
let shifted = u_fixed_big_int_shr(&value, precision)
var ret = fixed_point_create_raw_u64[P](shifted.m_data[0])
# Rounding:
# If last bit cut off is 1:
if (value.m_data[0] & ((u64)1 << (u64)(precision - 1))) != 0:
# If the bit after is 1 as well
if (value.m_data[0] & (fixed_point_radix_mask_u64(precision) >> 1)) != 0:
# We round away from 0
ret.m_value = ret.m_value +% 1
else:
# Otherwise we round to the next even value
# Which means we add the least significant bit of the raw return value
ret.m_value = ret.m_value +% (ret.m_value & 1)
return ret
## Ported from AK/FixedPoint.h:240 AK::FixedPoint::operator/(FixedPoint), for u64: DivRes is a
## 128-bit integer, here U128.
pub func fixed_point_div_fixed_point_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> FixedPoint[u64, P]:
let precision = fixed_point_precision[P]()
# FIXME: Figure out a nicer way to use more narrow types and avoid __int128
var value = U128(m_data = [this.m_value, 0])
_ = u_fixed_big_int_shl_assign(&value, precision)
_ = u_fixed_big_int_div_assign_t(&value, other.m_value)
return fixed_point_create_raw_u64[P](value.m_data[0])
## Ported from AK/FixedPoint.h:273 AK::FixedPoint::operator+(Integral), for u64.
pub func fixed_point_add_i_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: i64) -> FixedPoint[u64, P]:
let precision = fixed_point_precision[P]()
return fixed_point_create_raw_u64[P](this.m_value +% ((u64)other << (u64)precision))
## Ported from AK/FixedPoint.h:278 AK::FixedPoint::operator-(Integral), for u64.
pub func fixed_point_sub_i_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: i64) -> FixedPoint[u64, P]:
let precision = fixed_point_precision[P]()
return fixed_point_create_raw_u64[P](this.m_value -% ((u64)other << (u64)precision))
## Ported from AK/FixedPoint.h:283 AK::FixedPoint::operator*(Integral), for u64.
pub func fixed_point_mul_i_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: i64) -> FixedPoint[u64, P]:
return fixed_point_create_raw_u64[P](this.m_value *% (u64)other)
## Ported from AK/FixedPoint.h:288 AK::FixedPoint::operator/(Integral), for u64.
pub func fixed_point_div_i_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: i64) -> FixedPoint[u64, P]:
return fixed_point_create_raw_u64[P](this.m_value // (u64)other)
## Ported from AK/FixedPoint.h:293 AK::FixedPoint::operator>>(Integral), for u64.
pub func fixed_point_shr_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: i64) -> FixedPoint[u64, P]:
return fixed_point_create_raw_u64[P](this.m_value >> (u64)other)
## Ported from AK/FixedPoint.h:298 AK::FixedPoint::operator<<(Integral), for u64.
pub func fixed_point_shl_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: i64) -> FixedPoint[u64, P]:
return fixed_point_create_raw_u64[P](this.m_value << (u64)other)
## Ported from AK/FixedPoint.h:303 AK::FixedPoint::operator+=(FixedPoint), for u64.
pub func fixed_point_add_assign_fixed_point_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> FixedPoint[u64, P]*:
this.m_value = this.m_value +% other.m_value
return this
## Ported from AK/FixedPoint.h:308 AK::FixedPoint::operator-=(FixedPoint), for u64.
pub func fixed_point_sub_assign_fixed_point_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> FixedPoint[u64, P]*:
this.m_value = this.m_value -% other.m_value
return this
## Ported from AK/FixedPoint.h:313 AK::FixedPoint::operator*=(FixedPoint), for u64.
pub func fixed_point_mul_assign_fixed_point_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> FixedPoint[u64, P]*:
*this = fixed_point_mul_fixed_point_u64(this, other)
return this
## Ported from AK/FixedPoint.h:318 AK::FixedPoint::operator/=(FixedPoint), for u64.
pub func fixed_point_div_assign_fixed_point_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> FixedPoint[u64, P]*:
*this = fixed_point_div_fixed_point_u64(this, other)
return this
## Ported from AK/FixedPoint.h:325 AK::FixedPoint::operator+=(Integral), for u64.
pub func fixed_point_add_assign_i_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*, other: i64) -> FixedPoint[u64, P]*:
*this = fixed_point_add_i_u64(this, other)
return this
## Ported from AK/FixedPoint.h:331 AK::FixedPoint::operator-=(Integral), for u64.
pub func fixed_point_sub_assign_i_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*, other: i64) -> FixedPoint[u64, P]*:
*this = fixed_point_sub_i_u64(this, other)
return this
## Ported from AK/FixedPoint.h:337 AK::FixedPoint::operator*=(Integral), for u64.
pub func fixed_point_mul_assign_i_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*, other: i64) -> FixedPoint[u64, P]*:
*this = fixed_point_mul_i_u64(this, other)
return this
## Ported from AK/FixedPoint.h:343 AK::FixedPoint::operator/=(Integral), for u64.
pub func fixed_point_div_assign_i_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*, other: i64) -> FixedPoint[u64, P]*:
*this = fixed_point_div_i_u64(this, other)
return this
## Ported from AK/FixedPoint.h:349 AK::FixedPoint::operator>>=(Integral), for u64.
pub func fixed_point_shr_assign_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*, other: i64) -> FixedPoint[u64, P]*:
*this = fixed_point_shr_u64(this, other)
return this
## Ported from AK/FixedPoint.h:355 AK::FixedPoint::operator<<=(Integral), for u64.
pub func fixed_point_shl_assign_u64[P: FixedPointPrecision](this: FixedPoint[u64, P]*, other: i64) -> FixedPoint[u64, P]*:
*this = fixed_point_shl_u64(this, other)
return this
## Ported from AK/FixedPoint.h:361 AK::FixedPoint::operator==(FixedPoint), for u64.
pub func fixed_point_eq_fixed_point_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> bool:
return this.m_value == other.m_value
## Ported from AK/FixedPoint.h:362 AK::FixedPoint::operator!=(FixedPoint), for u64.
pub func fixed_point_ne_fixed_point_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> bool:
return this.m_value != other.m_value
## Ported from AK/FixedPoint.h:363 AK::FixedPoint::operator>(FixedPoint), for u64.
pub func fixed_point_gt_fixed_point_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> bool:
return this.m_value > other.m_value
## Ported from AK/FixedPoint.h:364 AK::FixedPoint::operator>=(FixedPoint), for u64.
pub func fixed_point_ge_fixed_point_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> bool:
return this.m_value >= other.m_value
## Ported from AK/FixedPoint.h:365 AK::FixedPoint::operator<(FixedPoint), for u64.
pub func fixed_point_lt_fixed_point_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> bool:
return this.m_value < other.m_value
## Ported from AK/FixedPoint.h:366 AK::FixedPoint::operator<=(FixedPoint), for u64.
pub func fixed_point_le_fixed_point_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: FixedPoint[u64, P]) -> bool:
return this.m_value <= other.m_value
## Ported from AK/FixedPoint.h:370 AK::FixedPoint::operator==(Integral), for u64.
# FIXME: There are probably better ways to do these
pub func fixed_point_eq_i_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: i64) -> bool:
let precision = fixed_point_precision[P]()
return (u64)(this.m_value >> (u64)precision) == (u64)other and (this.m_value & fixed_point_radix_mask_u64(precision)) == 0
## Ported from AK/FixedPoint.h:375 AK::FixedPoint::operator!=(Integral), for u64.
pub func fixed_point_ne_i_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: i64) -> bool:
let precision = fixed_point_precision[P]()
return (u64)(this.m_value >> (u64)precision) != (u64)other or (this.m_value & fixed_point_radix_mask_u64(precision)) != 0
## Ported from AK/FixedPoint.h:380 AK::FixedPoint::operator>(Integral), for u64.
pub func fixed_point_gt_i_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: i64) -> bool:
return not fixed_point_le_i_u64(this, other)
## Ported from AK/FixedPoint.h:385 AK::FixedPoint::operator>=(Integral), for u64.
pub func fixed_point_ge_i_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: i64) -> bool:
return not fixed_point_lt_i_u64(this, other)
## Ported from AK/FixedPoint.h:390 AK::FixedPoint::operator<(Integral), for u64.
pub func fixed_point_lt_i_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: i64) -> bool:
let precision = fixed_point_precision[P]()
return (u64)(this.m_value >> (u64)precision) < (u64)other or this.m_value < ((u64)other << (u64)precision)
## Ported from AK/FixedPoint.h:395 AK::FixedPoint::operator<=(Integral), for u64.
pub func fixed_point_le_i_u64[P: FixedPointPrecision](this: const FixedPoint[u64, P]*, other: i64) -> bool:
return fixed_point_lt_i_u64(this, other) or fixed_point_eq_i_u64(this, other)