Logo

index : 0x2lib

Library extension for Jai

  • summary
  • about
  • tree
  • log
  • branches
<< path: root/public/0x2lib.git/html/0x2_Bitpack.jai blob: 5a5f2e6e1cca0bdecb52411a9f6d01562dbf3db3 [raw] [clear marker]

        
0
1
2
3Bits_Packed :: struct {
4 count: int;
5 width: int;
6 value: int;
7 top: int;
8}
9
10
11bits_pack :: ($type: Type, values: ..type) -> Bits_Packed #modify {
12 if !array_find(My_Allowed_Types, type) return false, "Invalid type";
13 return true;
14} {
15 result: int;
16 bit_count := size_of(type) * 8;
17
18 for < values {
19 result = (result << bit_count) | it;
20 }
21
22 packed: Bits_Packed;
23 packed.count = values.count;
24 packed.width = bit_count;
25 packed.value = result;
26 packed.top = (1 << bit_count) - 1;
27
28 return packed;
29}
30
31/** Reminder: You must free the array */
32bits_unpack :: (packed: Bits_Packed) -> []int {
33 arr := NewArray(packed.count, int);
34 for i: 0..packed.count-1 {
35 unpacked := packed.value >> (i * packed.width) & packed.top;
36 arr[i] = unpacked;
37 }
38 return arr;
39}
40
41bits_unpack_by_index :: (packed: Bits_Packed, index: int) -> int {
42 return packed.value >> (index * packed.width) & packed.top;
43}
44
45
46#scope_file
47
48
49My_Allowed_Types :: Type.[u8, u16, u32, u64];
50
51
52using,only(array_find, NewArray) Basic :: #import "Basic";
53
54
55/*
56------------------------------------------------------------------------------
57This software is available under 2 licenses -- choose whichever you prefer.
58------------------------------------------------------------------------------
59ALTERNATIVE A - MIT License
60Copyright (c) 2026 Adam Blazeowsky
61Permission is hereby granted, free of charge, to any person obtaining a copy of
62this software and associated documentation files (the "Software"), to deal in
63the Software without restriction, including without limitation the rights to
64use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
65of the Software, and to permit persons to whom the Software is furnished to do
66so, subject to the following conditions:
67The above copyright notice and this permission notice shall be included in all
68copies or substantial portions of the Software.
69THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
70IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
71FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
72AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
73LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
74OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
75SOFTWARE.
76------------------------------------------------------------------------------
77ALTERNATIVE B - Public Domain (www.unlicense.org)
78This is free and unencumbered software released into the public domain.
79Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
80software, either in source code form or as a compiled binary, for any purpose,
81commercial or non-commercial, and by any means.
82In jurisdictions that recognize copyright laws, the author or authors of this
83software dedicate any and all copyright interest in the software to the public
84domain. We make this dedication for the benefit of the public at large and to
85the detriment of our heirs and successors. We intend this dedication to be an
86overt act of relinquishment in perpetuity of all present and future rights to
87this software under copyright law.
88THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
89IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
90FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
91AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
92ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
93WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
94------------------------------------------------------------------------------
95*/
96
97
Copyright 2026  E766CB298A6D1E64 | Git-Thing heavily inspired by cgit