i8.fz
# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation. If not, see <https://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------
#
# Tokiwa Software GmbH, Germany
#
# Source code of Fuzion standard library feature i8
#
# Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------
# i8 -- 8-bit signed integer values
#
public i8(public val i8) : num.wrap_around, has_interval is
public thiz => i8.this.val
# overflow checking
# would negation -thiz cause an overflow?
redef wrapped_on_neg => is_min
# would addition thiz + other cause an overflow or underflow?
public fixed overflow_on_add (other i8) => thiz > 0 && i8.max -° thiz < other
public fixed underflow_on_add(other i8) => thiz < 0 && i8.min -° thiz > other
# would subtraction thiz - other cause an overflow or underflow?
public fixed overflow_on_sub (other i8) => thiz > 0 && thiz -° i8.max > other
public fixed underflow_on_sub(other i8) => thiz < 0 && thiz -° i8.min < other
# would multiplication thiz * other cause an overflow or underflow?
public fixed overflow_on_mul (other i8) => as_i32 *° other.as_i32 > i8.max.as_i32
public fixed underflow_on_mul(other i8) => as_i32 *° other.as_i32 < i8.min.as_i32
# neg, add, sub, mul with wrap-around semantics
public fixed prefix -° i8 => intrinsic
public fixed infix +° (other i8) i8 => intrinsic
public fixed infix -° (other i8) i8 => intrinsic
public fixed infix *° (other i8) i8 => intrinsic
# division and remainder with check for div-by-zero
public fixed infix / (other i8)
pre
safety: other != 0
=> div other
fixed redef infix % (other i8)
pre
safety: other != 0
=> mod other
# private division and remainder with crash in case of div-by-zero
private div (other i8) i8 => intrinsic
private mod (other i8) i8 => intrinsic
# bitwise and, or and xor operations
public fixed infix & (other i8) i8 => intrinsic
public fixed infix | (other i8) i8 => intrinsic
public fixed infix ^ (other i8) i8 => intrinsic
# shift operations (signed)
public fixed infix >> (other i8) i8 => intrinsic
public fixed infix << (other i8) i8 => intrinsic
# conversion to u32, i64 and u64, with range check
public as_i16 => as_i32.as_i16
public as_i32 i32 => intrinsic
public as_i64 => as_i32.as_i64
# as_i128 => as_i32.as_i128
public as_u8 u8
pre
debug: thiz ≥ 0
=>
cast_to_u8
public as_u16 u16
pre
debug: thiz ≥ 0
=>
cast_to_u16
public as_u32 u32
pre
debug: thiz ≥ 0
=>
cast_to_u32
public as_u64 u64
pre
debug: thiz ≥ 0
=>
cast_to_u64
public as_u128 u128
pre
debug: thiz ≥ 0
=>
cast_to_u128
public as_int int =>
int as_i64
# casting to unsigned, adding 1<<8 if negative
public cast_to_u8 u8 => intrinsic # 3 -> 3, -3 -> 0xfd
public cast_to_u16 u16 => cast_to_u8.as_u16 # 3 -> 3, -3 -> 0x00fd
public cast_to_u32 u32 => cast_to_u8.as_u32 # 3 -> 3, -3 -> 0x0000_00fd
public cast_to_u64 u64 => cast_to_u8.as_u64 # 3 -> 3, -3 -> 0x0000_0000_0000_00fd
public cast_to_u128 u128 => cast_to_u8.as_u128 # 3 -> 3, -3 -> 0x0000_0000_0000_0000_0000_0000_0000_00fd
# create hash code from this number
public type.hash_code(a i8.this) u64 =>
hash a.cast_to_u64
# find the highest 1 bit in this integer and return integer with
# this single bit set or 0 if this is zero.
#
public highest_one_bit i8 =>
val.cast_to_u8.highest_one_bit.cast_to_i8
# count the number of trailing zeros in this integer.
#
public trailing_zeros i32 =>
val.cast_to_u8.trailing_zeros
# count the number of 1 bits in the binary representation of this
# integer.
#
public ones_count i32 =>
val.cast_to_u8.ones_count
# -----------------------------------------------------------------------
#
# type features:
# identity element for 'infix +'
#
fixed type.zero i8 => 0
# identity element for 'infix *'
#
fixed type.one i8 => 1
# equality
#
fixed type.equality(a, b i8) bool => intrinsic_constructor
# total order
#
fixed type.lteq(a, b i8) bool => intrinsic_constructor
# returns the number in whose bit representation all bits are ones
fixed redef type.all_bits_set => i8 -1
# minimum
#
public fixed type.min => i8 -0x80
# maximum
#
public fixed type.max => i8 0x7f
last changed: 2024-03-07