aboutsummaryrefslogtreecommitdiff
path: root/src/integers.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/integers.nim')
-rw-r--r--src/integers.nim7
1 files changed, 5 insertions, 2 deletions
diff --git a/src/integers.nim b/src/integers.nim
index fddbfdc..7b0f166 100644
--- a/src/integers.nim
+++ b/src/integers.nim
@@ -15,13 +15,16 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17const wordBitLength* = 8 17const wordBitLength* = 8
18const wordBitMask* = 0b1111_1111'u8
19 18
20proc `/^`*[T: Natural](x, y: T): T = 19proc `/^`*[T: Natural](x, y: T): T =
21 (x + y - 1) div y 20 (x + y - 1) div y
22 21
23proc truncateToUint8*(x: SomeUnsignedInt): uint8 = 22proc truncateToUint8*(x: SomeUnsignedInt): uint8 =
24 (x and wordBitMask).uint8 23 (x and uint8.high).uint8
24
25proc leastSignificantBits*[T: SomeUnsignedInt](x: T, bits: int): T =
26 let maskOffset = sizeof(T) * wordBitLength - bits
27 if maskOffset >= 0: (x shl maskOffset) shr maskOffset else: x
25 28
26iterator chunks*(totalBitLength: int, chunkType: typedesc[SomeInteger]): tuple[index: int, chunkBitLength: int] = 29iterator chunks*(totalBitLength: int, chunkType: typedesc[SomeInteger]): tuple[index: int, chunkBitLength: int] =
27 let chunkBitLength = sizeof(chunkType) * wordBitLength 30 let chunkBitLength = sizeof(chunkType) * wordBitLength