Scoop API

scoop:scoop.core:0.1.0

src/integer_uint8.scoop

Selected production source · src/integer_uint8.scoop

1/**
2 * A 8-bit unsigned integer. Arithmetic wraps modulo 2^8; division by zero throws
3 * [ArithmeticException]. Conversions are explicit and preserve the language's fixed-width rules on
4 * every target.
5 */
6@Intrinsic("core_uint8")
7public struct UInt8 :
8 Add<UInt8, UInt8>,
9 Sub<UInt8, UInt8>,
10 Mul<UInt8, UInt8>,
11 Div<UInt8, UInt8>,
12 Rem<UInt8, UInt8>,
13 UnaryPlus<UInt8>,
14 UnaryMinus<UInt8>,
15 Increment<UInt8>,
16 Decrement<UInt8>,
17 Compare<UInt8>,
18 RangeTo<UInt8, UIntRange>,
19 RangeUntil<UInt8, UIntRange>,
20 Equals<UInt8>, ToString, Hash {
21 /**
22 * Rounds the integer directly to IEEE binary32 using round-to-nearest, ties-to-even.
23 */
24 @NoGC @Intrinsic("uint8_to_float")
25 public fun toFloat(): Float
27 /**
28 * Rounds the integer directly to IEEE binary64 using round-to-nearest, ties-to-even.
29 */
30 @NoGC @Intrinsic("uint8_to_double")
31 public fun toDouble(): Double
33 /**
34 * Compares the mathematical values of two integers of the same type.
35 */
36 @NoGC
37 @Intrinsic("uint8_equals")
38 public override operator fun equals(other: UInt8): Boolean
41 /**
42 * Returns the unchanged integer value.
43 */
44 @NoGC
45 @Intrinsic("uint8_unary_plus")
46 public override operator fun unaryPlus(): UInt8
48 /**
49 * Returns the additive inverse modulo 2^8, including at the representation boundary.
50 */
51 @NoGC
52 @Intrinsic("uint8_unary_minus")
53 public override operator fun unaryMinus(): UInt8
55 /**
56 * Returns the value plus one, wrapping modulo 2^8.
57 */
58 @NoGC
59 @Intrinsic("uint8_inc")
60 public override operator fun inc(): UInt8
62 /**
63 * Returns the value minus one, wrapping modulo 2^8.
64 */
65 @NoGC
66 @Intrinsic("uint8_dec")
67 public override operator fun dec(): UInt8
69 /**
70 * Returns the sum modulo 2^8; overflow does not throw.
71 */
72 @NoGC
73 @Intrinsic("uint8_add")
74 public override operator fun plus(other: UInt8): UInt8
76 /**
77 * Returns the difference modulo 2^8; overflow does not throw.
78 */
79 @NoGC
80 @Intrinsic("uint8_sub")
81 public override operator fun minus(other: UInt8): UInt8
83 /**
84 * Returns the product modulo 2^8; overflow does not throw.
85 */
86 @NoGC
87 @Intrinsic("uint8_mul")
88 public override operator fun times(other: UInt8): UInt8
90 /**
91 * Returns the quotient truncated toward zero. A zero divisor throws [ArithmeticException].
92 */
93 @Intrinsic("uint8_div")
94 public override operator fun div(other: UInt8): UInt8
96 /**
97 * Returns the remainder associated with division truncated toward zero. A zero divisor throws
98 * [ArithmeticException].
99 */
100 @Intrinsic("uint8_rem")
101 public override operator fun rem(other: UInt8): UInt8
103 /**
104 * Returns -1, 0, or 1 using unsigned mathematical ordering.
105 */
106 @NoGC
107 @Intrinsic("uint8_compare_to")
108 public override operator fun compareTo(other: UInt8): Long
110 /**
111 * Returns the bitwise intersection of the 8-bit operands.
112 */
113 @NoGC
114 @Intrinsic("uint8_and")
115 public infix fun and(other: UInt8): UInt8
117 /**
118 * Returns the bitwise union of the 8-bit operands.
119 */
120 @NoGC
121 @Intrinsic("uint8_or")
122 public infix fun or(other: UInt8): UInt8
124 /**
125 * Sets each result bit where the corresponding 8-bit operand bits differ.
126 */
127 @NoGC
128 @Intrinsic("uint8_xor")
129 public infix fun xor(other: UInt8): UInt8
131 /**
132 * Flips all 8 bits of the representation.
133 */
134 @NoGC
135 @Intrinsic("uint8_inv")
136 public fun inv(): UInt8
138 /**
139 * Shifts left using the low 3 bits of the Long count; shifted-out bits are discarded.
140 */
141 @NoGC
142 @Intrinsic("uint8_shl")
143 public infix fun shl(count: Long): UInt8
145 /**
146 * Shifts right using the low 3 bits of the Long count, filling with zero bits.
147 */
148 @NoGC
149 @Intrinsic("uint8_shr")
150 public infix fun shr(count: Long): UInt8
152 /**
153 * Converts modulo 2^8 and interprets the result as a 8-bit signed integer. The conversion
154 * always succeeds.
155 */
156 @NoGC
157 @Intrinsic("uint8_to_int8")
158 public fun toByte(): Byte
160 /**
161 * Converts modulo 2^16 and interprets the result as a 16-bit signed integer. The conversion
162 * always succeeds.
163 */
164 @NoGC
165 @Intrinsic("uint8_to_int16")
166 public fun toShort(): Short
168 /**
169 * Converts modulo 2^32 and interprets the result as a 32-bit signed integer. The conversion
170 * always succeeds.
171 */
172 @NoGC
173 @Intrinsic("uint8_to_int")
174 public fun toInt(): Int
176 /**
177 * Converts modulo 2^64 and interprets the result as a 64-bit signed integer. The conversion
178 * always succeeds.
179 */
180 @NoGC
181 @Intrinsic("uint8_to_long")
182 public fun toLong(): Long
184 /**
185 * Converts modulo 2^8 and interprets the result as a 8-bit unsigned integer. The conversion
186 * always succeeds.
187 */
188 @NoGC
189 @Intrinsic("uint8_to_uint8")
190 public fun toUByte(): UByte
192 /**
193 * Converts modulo 2^16 and interprets the result as a 16-bit unsigned integer. The conversion
194 * always succeeds.
195 */
196 @NoGC
197 @Intrinsic("uint8_to_uint16")
198 public fun toUShort(): UShort
200 /**
201 * Converts modulo 2^32 and interprets the result as a 32-bit unsigned integer. The conversion
202 * always succeeds.
203 */
204 @NoGC
205 @Intrinsic("uint8_to_uint")
206 public fun toUInt(): UInt
208 /**
209 * Converts modulo 2^64 and interprets the result as a 64-bit unsigned integer. The conversion
210 * always succeeds.
211 */
212 @NoGC
213 @Intrinsic("uint8_to_ulong")
214 public fun toULong(): ULong
216 /**
217 * Creates an ascending range including both this value and the endpoint.
218 */
219 public override operator fun rangeTo(endpoint: UInt8): UIntRange =
220 UIntRange(this.toUInt(), endpoint.toUInt(), 1u, false, true)
222 /**
223 * Creates an ascending range including this value and excluding the endpoint.
224 */
225 public override operator fun rangeUntil(endpoint: UInt8): UIntRange =
226 UIntRange(this.toUInt(), endpoint.toUInt(), 1u, false, false)
228 /**
229 * Creates an ascending range with an exclusive endpoint.
230 */
231 public infix fun until(endpoint: UInt8): UIntRange =
232 UIntRange(this.toUInt(), endpoint.toUInt(), 1u, false, false)
234 /**
235 * Creates a descending range including both endpoints.
236 */
237 public infix fun downTo(endpoint: UInt8): UIntRange =
238 UIntRange(this.toUInt(), endpoint.toUInt(), 1u, true, true)
240 /**
241 * Returns the locale-independent decimal representation of this integer.
242 */
243 public override fun toString(): String = coreULongToString(this.toULong())
245 /**
246 * Returns a hash consistent with same-type integer equality.
247 */
248 public override fun hash(): Long {
249 @Unsafe { return coreULongHash(this.toULong()) }
250 }
252 /**
253 * Fixed representation bounds and the scalar encoding, decoding, and inspection protocols.
254 */
255 public companion object : ValueEncoder<UInt8>, ValueDecoder<UInt8>, ValueInspector<UInt8> {
256 /**
257 * Exposes the integer as a scalar value observation without reference identity.
258 */
259 public override fun inspect(value: UInt8): ValueView =
260 ValueObservation(typeInfo<UInt8>().type, None, { ValueShape.Scalar(ScalarValue.Unsigned(value.toULong())) })
262 /**
263 * The smallest representable 8-bit unsigned integer.
264 */
265 public const val MIN_VALUE: UInt8 = 0u
266 /**
267 * The largest representable 8-bit unsigned integer.
268 */
269 public const val MAX_VALUE: UInt8 = 255u
271 /**
272 * Writes the integer through the encoder's single-value container.
273 */
274 public override fun encode(value: UInt8, encoder: Encoder): Unit =
275 encoder.singleValue().writeULong(value.toULong())
277 /**
278 * Decodes an integer and rejects values outside the 8-bit unsigned range with
279 * [DecodingException].
280 */
281 public override fun decode(decoder: Decoder): UInt8 =
282 decodeUnsigned(decoder, 255UL).toUByte()
283 }