Scoop API

scoop:scoop.core:0.1.0

src/integer_uint.scoop

Selected production source · src/integer_uint.scoop

1/**
2 * A 32-bit unsigned integer. Arithmetic wraps modulo 2^32; 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_uint")
7public struct UInt :
8 Add<UInt, UInt>,
9 Sub<UInt, UInt>,
10 Mul<UInt, UInt>,
11 Div<UInt, UInt>,
12 Rem<UInt, UInt>,
13 UnaryPlus<UInt>,
14 UnaryMinus<UInt>,
15 Increment<UInt>,
16 Decrement<UInt>,
17 Compare<UInt>,
18 RangeTo<UInt, UIntRange>,
19 RangeUntil<UInt, UIntRange>,
20 Equals<UInt>, ToString, Hash {
21 /**
22 * Rounds the integer directly to IEEE binary32 using round-to-nearest, ties-to-even.
23 */
24 @NoGC @Intrinsic("uint_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("uint_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("uint_equals")
38 public override operator fun equals(other: UInt): Boolean
41 /**
42 * Returns the unchanged integer value.
43 */
44 @NoGC
45 @Intrinsic("uint_unary_plus")
46 public override operator fun unaryPlus(): UInt
48 /**
49 * Returns the additive inverse modulo 2^32, including at the representation boundary.
50 */
51 @NoGC
52 @Intrinsic("uint_unary_minus")
53 public override operator fun unaryMinus(): UInt
55 /**
56 * Returns the value plus one, wrapping modulo 2^32.
57 */
58 @NoGC
59 @Intrinsic("uint_inc")
60 public override operator fun inc(): UInt
62 /**
63 * Returns the value minus one, wrapping modulo 2^32.
64 */
65 @NoGC
66 @Intrinsic("uint_dec")
67 public override operator fun dec(): UInt
69 /**
70 * Returns the sum modulo 2^32; overflow does not throw.
71 */
72 @NoGC
73 @Intrinsic("uint_add")
74 public override operator fun plus(other: UInt): UInt
76 /**
77 * Returns the difference modulo 2^32; overflow does not throw.
78 */
79 @NoGC
80 @Intrinsic("uint_sub")
81 public override operator fun minus(other: UInt): UInt
83 /**
84 * Returns the product modulo 2^32; overflow does not throw.
85 */
86 @NoGC
87 @Intrinsic("uint_mul")
88 public override operator fun times(other: UInt): UInt
90 /**
91 * Returns the quotient truncated toward zero. A zero divisor throws [ArithmeticException].
92 */
93 @Intrinsic("uint_div")
94 public override operator fun div(other: UInt): UInt
96 /**
97 * Returns the remainder associated with division truncated toward zero. A zero divisor throws
98 * [ArithmeticException].
99 */
100 @Intrinsic("uint_rem")
101 public override operator fun rem(other: UInt): UInt
103 /**
104 * Returns -1, 0, or 1 using unsigned mathematical ordering.
105 */
106 @NoGC
107 @Intrinsic("uint_compare_to")
108 public override operator fun compareTo(other: UInt): Long
110 /**
111 * Returns the bitwise intersection of the 32-bit operands.
112 */
113 @NoGC
114 @Intrinsic("uint_and")
115 public infix fun and(other: UInt): UInt
117 /**
118 * Returns the bitwise union of the 32-bit operands.
119 */
120 @NoGC
121 @Intrinsic("uint_or")
122 public infix fun or(other: UInt): UInt
124 /**
125 * Sets each result bit where the corresponding 32-bit operand bits differ.
126 */
127 @NoGC
128 @Intrinsic("uint_xor")
129 public infix fun xor(other: UInt): UInt
131 /**
132 * Flips all 32 bits of the representation.
133 */
134 @NoGC
135 @Intrinsic("uint_inv")
136 public fun inv(): UInt
138 /**
139 * Shifts left using the low 5 bits of the Long count; shifted-out bits are discarded.
140 */
141 @NoGC
142 @Intrinsic("uint_shl")
143 public infix fun shl(count: Long): UInt
145 /**
146 * Shifts right using the low 5 bits of the Long count, filling with zero bits.
147 */
148 @NoGC
149 @Intrinsic("uint_shr")
150 public infix fun shr(count: Long): UInt
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("uint_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("uint_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("uint_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("uint_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("uint_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("uint_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("uint_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("uint_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: UInt): UIntRange =
220 UIntRange(this, endpoint, 1u, false, true)
222 /**
223 * Creates an ascending range including this value and excluding the endpoint.
224 */
225 public override operator fun rangeUntil(endpoint: UInt): UIntRange =
226 UIntRange(this, endpoint, 1u, false, false)
228 /**
229 * Creates an ascending range with an exclusive endpoint.
230 */
231 public infix fun until(endpoint: UInt): UIntRange =
232 UIntRange(this, endpoint, 1u, false, false)
234 /**
235 * Creates a descending range including both endpoints.
236 */
237 public infix fun downTo(endpoint: UInt): UIntRange =
238 UIntRange(this, endpoint, 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<UInt>, ValueDecoder<UInt>, ValueInspector<UInt> {
256 /**
257 * Exposes the integer as a scalar value observation without reference identity.
258 */
259 public override fun inspect(value: UInt): ValueView =
260 ValueObservation(typeInfo<UInt>().type, None, { ValueShape.Scalar(ScalarValue.Unsigned(value.toULong())) })
262 /**
263 * The smallest representable 32-bit unsigned integer.
264 */
265 public const val MIN_VALUE: UInt = 0u
266 /**
267 * The largest representable 32-bit unsigned integer.
268 */
269 public const val MAX_VALUE: UInt = 4294967295u
271 /**
272 * Writes the integer through the encoder's single-value container.
273 */
274 public override fun encode(value: UInt, encoder: Encoder): Unit =
275 encoder.singleValue().writeULong(value.toULong())
277 /**
278 * Decodes an integer and rejects values outside the 32-bit unsigned range with
279 * [DecodingException].
280 */
281 public override fun decode(decoder: Decoder): UInt =
282 decodeUnsigned(decoder, 4294967295UL).toUInt()
283 }