Scoop API

scoop:scoop.math:0.1.0

src/internal/integer/conversions.scoop

Selected production source · src/internal/integer/conversions.scoop

1package scoop.math
3import scoop.bytes.ByteOrder
5internal fun integerFromBytes(bytes: List<Byte>, order: ByteOrder, signed: Boolean, sign: Int): IntegerStorage {
6 if (signed && bytes.size == 0L) throw IllegalArgumentException(Some("empty two's-complement input"))
7 if (!signed && (sign < -1 || sign > 1)) throw IllegalArgumentException(Some("invalid signum"))
8 val little = when (order) { case LittleEndian -> true; case BigEndian -> false }
9 val count = bytes.size / 4L + (if (bytes.size % 4L == 0L) 0L else 1L)
10 val words = MutableArray<UInt>(count) { _ -> 0u }
11 var negative = false
12 var index = 0L
13 while (index < bytes.size) {
14 val byte = bytes[if (little) index else bytes.size - 1L - index]
15 words[index / 4L] = words[index / 4L] or (byte.toUByte().toUInt() shl ((index % 4L) * 8L))
16 if (index == bytes.size - 1L) negative = byte < 0
17 index += 1L
18 }
19 if (signed) {
20 if (negative && bytes.size % 4L != 0L) words[count - 1L] = words[count - 1L] or (UInt.MAX_VALUE shl ((bytes.size % 4L) * 8L))
21 return integerFromTwosComplement(words.toArray())
22 }
23 val magnitude = magnitudeSnapshot(words)
24 if (sign == 0 && magnitude.size != 0L) throw IllegalArgumentException(Some("zero sign with nonzero magnitude"))
25 return integerFromMagnitude(sign < 0, magnitude)
28internal fun integerBytes(value: IntegerStorage, order: ByteOrder, signed: Boolean): Array<Byte> {
29 val words = integerMagnitude(value)
30 val negative = signed && integerSign(value) < 0
31 val bits = if (signed) integerBitLength(value) else magnitudeBits(words)
32 val count = if (signed) addExact(bits / 8L, 1L) else max(1L, bits / 8L + (if (bits % 8L == 0L) 0L else 1L))
33 val result = MutableArray<Byte>(count) { _ -> 0.toByte() }
34 val little = when (order) { case LittleEndian -> true; case BigEndian -> false }
35 var carry = 1u
36 var index = 0L
37 while (index < count) {
38 val raw = (magnitudeWord(words, index / 4L) shr ((index % 4L) * 8L)) and 255u
39 val byte = if (negative) (raw xor 255u) + carry else raw
40 carry = byte shr 8L
41 result[if (little) index else count - 1L - index] = byte.toByte()
42 index += 1L
43 }
44 return result.toArray()
47// Rounds a positive magnitude to an integer significand, directly at the target precision.
48internal fun integerFloatParts(value: IntegerStorage, precision: Long): (ULong, Long) {
49 val words = integerMagnitude(value)
50 val bits = magnitudeBits(words)
51 if (bits <= precision) return (integerLowBits(integerAbs(value)), 0L)
52 val shift = bits - precision
53 val top = magnitudeShiftRight(words, shift)
54 var significand = magnitudeWord(top, 0L).toULong() or (magnitudeWord(top, 1L).toULong() shl 32L)
55 val guard = (magnitudeWord(words, (shift - 1L) / 32L) and (1u shl ((shift - 1L) % 32L))) != 0u
56 val sticky = magnitudeLowestBit(words) < shift - 1L
57 if (guard && (sticky || (significand and 1uL) != 0uL)) significand += 1uL
58 return (significand, shift)
61internal fun integerDouble(value: IntegerStorage): Double {
62 val negative = integerSign(value) < 0
63 if (magnitudeBits(integerMagnitude(value)) > 1024L) return if (negative) Double.NEGATIVE_INFINITY else Double.POSITIVE_INFINITY
64 val parts = integerFloatParts(value, 53L)
65 val result = scalb(parts._1.toDouble(), parts._2.toInt())
66 return if (negative) -result else result
69internal fun integerFloat(value: IntegerStorage): Float {
70 val negative = integerSign(value) < 0
71 if (magnitudeBits(integerMagnitude(value)) > 128L) return if (negative) Float.NEGATIVE_INFINITY else Float.POSITIVE_INFINITY
72 val parts = integerFloatParts(value, 24L)
73 val result = scalb(parts._1.toFloat(), parts._2.toInt())
74 return if (negative) -result else result