-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitjuggle.zig
485 lines (408 loc) · 14.9 KB
/
bitjuggle.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2025 Lee Cannon <[email protected]>
/// Returns `true` if the the bit at index `bit` is set (equals 1).
///
/// Note: that index 0 is the least significant bit, while index `length() - 1` is the most significant bit.
///
/// ```zig
/// const a: u8 = 0b00000010;
///
/// try std.testing.expect(!isBitSet(a, 0));
/// try std.testing.expect(isBitSet(a, 1));
/// ```
pub inline fn isBitSet(target: anytype, comptime bit: comptime_int) bool {
const TargetType = @TypeOf(target);
comptime {
if (@typeInfo(TargetType) == .int) {
if (@typeInfo(TargetType).int.signedness != .unsigned) {
@compileError("requires an unsigned integer, found " ++ @typeName(TargetType));
}
if (bit >= @bitSizeOf(TargetType)) {
@compileError("bit index is out of bounds of the bit field");
}
} else if (@typeInfo(TargetType) == .comptime_int) {
if (target < 0) {
@compileError("requires an unsigned integer, found " ++ @typeName(TargetType));
}
} else {
@compileError("requires an unsigned integer, found " ++ @typeName(TargetType));
}
}
const mask: TargetType = comptime blk: {
const MaskType = std.meta.Int(.unsigned, bit + 1);
var temp: MaskType = std.math.maxInt(MaskType);
temp <<= bit;
break :blk temp;
};
return (target & mask) != 0;
}
test isBitSet {
// comptime
comptime {
const a: comptime_int = 0b00000000;
try std.testing.expect(!isBitSet(a, 0));
try std.testing.expect(!isBitSet(a, 1));
const b: comptime_int = 0b11111111;
try std.testing.expect(isBitSet(b, 0));
try std.testing.expect(isBitSet(b, 1));
const c: comptime_int = 0b00000010;
try std.testing.expect(!isBitSet(c, 0));
try std.testing.expect(isBitSet(c, 1));
}
// runtime
{
var value: u8 = 0b00000000;
try std.testing.expect(!isBitSet(value, 0));
try std.testing.expect(!isBitSet(value, 1));
value = 0b11111111;
try std.testing.expect(isBitSet(value, 0));
try std.testing.expect(isBitSet(value, 1));
value = 0b00000010;
try std.testing.expect(!isBitSet(value, 0));
try std.testing.expect(isBitSet(value, 1));
}
}
/// Get the value of the bit at index `bit`.
///
/// Note: that index 0 is the least significant bit, while index `length() - 1` is the most significant bit.
///
/// ```zig
/// const a: u8 = 0b00000010;
///
/// try std.testing.expect(getBit(a, 0) == 0);
/// try std.testing.expect(getBit(a, 1) == 1);
/// ```
pub inline fn getBit(target: anytype, comptime bit: comptime_int) u1 {
return @intFromBool(isBitSet(target, bit));
}
test getBit {
// comptime
comptime {
const a: comptime_int = 0b00000000;
try std.testing.expectEqual(@as(u1, 0), getBit(a, 0));
try std.testing.expectEqual(@as(u1, 0), getBit(a, 1));
const b: comptime_int = 0b11111111;
try std.testing.expectEqual(@as(u1, 1), getBit(b, 0));
try std.testing.expectEqual(@as(u1, 1), getBit(b, 1));
const c: comptime_int = 0b00000010;
try std.testing.expectEqual(@as(u1, 0), getBit(c, 0));
try std.testing.expectEqual(@as(u1, 1), getBit(c, 1));
}
// runtime
{
var value: u8 = 0b00000000;
try std.testing.expectEqual(@as(u1, 0), getBit(value, 0));
try std.testing.expectEqual(@as(u1, 0), getBit(value, 1));
value = 0b11111111;
try std.testing.expectEqual(@as(u1, 1), getBit(value, 0));
try std.testing.expectEqual(@as(u1, 1), getBit(value, 1));
value = 0b00000010;
try std.testing.expectEqual(@as(u1, 0), getBit(value, 0));
try std.testing.expectEqual(@as(u1, 1), getBit(value, 1));
}
}
/// Obtains the `number_of_bits` bits starting at `start_bit`.
///
/// Where `start_bit` is the lowest significant bit to fetch.
///
/// ```zig
/// const a: u8 = 0b01101100;
/// const b = getBits(a, 2, 4);
/// try std.testing.expectEqual(@as(u4,0b1011), b);
/// ```
pub inline fn getBits(
target: anytype,
comptime start_bit: comptime_int,
comptime number_of_bits: comptime_int,
) std.meta.Int(.unsigned, number_of_bits) {
const TargetType = @TypeOf(target);
comptime {
if (number_of_bits == 0) @compileError("non-zero number_of_bits must be provided");
if (@typeInfo(TargetType) == .int) {
if (@typeInfo(TargetType).int.signedness != .unsigned) {
@compileError("requires an unsigned integer, found " ++ @typeName(TargetType));
}
if (start_bit >= @bitSizeOf(TargetType)) {
@compileError("start_bit index is out of bounds of the bit field");
}
if (start_bit + number_of_bits > @bitSizeOf(TargetType)) {
@compileError("start_bit + number_of_bits is out of bounds of the bit field");
}
} else if (@typeInfo(TargetType) == .comptime_int) {
if (target < 0) {
@compileError("requires a positive integer, found a negative");
}
} else {
@compileError("requires an unsigned integer, found " ++ @typeName(TargetType));
}
}
return @truncate(target >> start_bit);
}
test getBits {
// comptime
comptime {
const a: comptime_int = 0b01101100;
const b = getBits(a, 2, 4);
try std.testing.expectEqual(@as(u4, 0b1011), b);
}
// runtime
{
var value: u8 = 0b01101100;
try std.testing.expectEqual(
@as(u4, 0b1011),
getBits(value, 2, 4),
);
value = 0b01101100;
try std.testing.expectEqual(
@as(u3, 0b100),
getBits(value, 0, 3),
);
}
}
/// Sets the bit at the index `bit` to the value `value` (where true means a value of '1' and false means a value of '0').
///
/// Note: that index 0 is the least significant bit, while index `length() - 1` is the most significant bit.
///
/// ```zig
/// var val: u8 = 0b00000000;
/// try std.testing.expect(!getBit(val, 0));
/// setBit( &val, 0, true);
/// try std.testing.expect(getBit(val, 0));
/// ```
pub inline fn setBit(target: anytype, comptime bit: comptime_int, value: u1) void {
const ptr_type_info: std.builtin.Type = @typeInfo(@TypeOf(target));
comptime {
if (ptr_type_info != .pointer) @compileError("not a pointer");
}
const TargetType = ptr_type_info.pointer.child;
comptime {
if (@typeInfo(TargetType) == .int) {
if (@typeInfo(TargetType).int.signedness != .unsigned) {
@compileError("requires an unsigned integer, found " ++ @typeName(TargetType));
}
if (bit >= @bitSizeOf(TargetType)) {
@compileError("bit index is out of bounds of the bit field");
}
} else if (@typeInfo(TargetType) == .comptime_int) {
@compileError("comptime_int is unsupported");
} else {
@compileError("requires an unsigned integer, found " ++ @typeName(TargetType));
}
}
const mask = comptime ~(@as(TargetType, 1) << bit);
target.* = (target.* & mask) | (@as(TargetType, value) << bit);
}
test setBit {
var val: u8 = 0b00000000;
try std.testing.expect(!isBitSet(val, 0));
setBit(&val, 0, 1);
try std.testing.expect(isBitSet(val, 0));
setBit(&val, 0, 0);
try std.testing.expect(!isBitSet(val, 0));
}
/// Sets the range of bits starting at `start_bit` upto and excluding `start_bit` + `number_of_bits`.
///
/// ```zig
/// var val: u8 = 0b10000000;
/// setBits(&val, 2, 4, 0b00001101);
/// try std.testing.expectEqual(@as(u8, 0b10110100), val);
/// ```
///
/// ## Panic
/// In safe modes this method will panic if the `value` exceeds the bit range of the type of `target`.
pub fn setBits(
target: anytype,
comptime start_bit: comptime_int,
comptime number_of_bits: comptime_int,
value: anytype,
) void {
const ptr_type_info: std.builtin.Type = @typeInfo(@TypeOf(target));
comptime {
if (ptr_type_info != .pointer) @compileError("not a pointer");
}
const TargetType = ptr_type_info.pointer.child;
const end_bit = start_bit + number_of_bits;
comptime {
if (number_of_bits == 0) @compileError("non-zero number_of_bits must be provided");
if (@typeInfo(TargetType) == .int) {
if (@typeInfo(TargetType).int.signedness != .unsigned) {
@compileError("requires an unsigned integer, found " ++ @typeName(TargetType));
}
if (start_bit >= @bitSizeOf(TargetType)) {
@compileError("start_bit index is out of bounds of the bit field");
}
if (end_bit > @bitSizeOf(TargetType)) {
@compileError("start_bit + number_of_bits is out of bounds of the bit field");
}
} else if (@typeInfo(TargetType) == .comptime_int) {
@compileError("comptime_int is unsupported");
} else {
@compileError("requires an unsigned integer, found " ++ @typeName(TargetType));
}
}
const peer_value: TargetType = value;
if (std.debug.runtime_safety) {
if (getBits(peer_value, 0, (end_bit - start_bit)) != peer_value) {
@panic("value exceeds bit range");
}
}
const bitmask: TargetType = comptime blk: {
var bitmask = ~@as(TargetType, 0);
bitmask <<= (@bitSizeOf(TargetType) - end_bit);
bitmask >>= (@bitSizeOf(TargetType) - end_bit);
bitmask >>= start_bit;
bitmask <<= start_bit;
break :blk ~bitmask;
};
target.* = (target.* & bitmask) | (peer_value << start_bit);
}
test setBits {
var val: u8 = 0b10000000;
setBits(&val, 2, 4, 0b00001101);
try std.testing.expectEqual(@as(u8, 0b10110100), val);
}
/// Defines a bitfield.
pub fn Bitfield(
/// The type of the underlying integer containing the bitfield.
comptime FieldType: type,
/// The starting bit index of the bitfield.
comptime shift_amount: usize,
/// The number of bits in the bitfield.
comptime num_bits: usize,
) type {
if (shift_amount + num_bits > @bitSizeOf(FieldType)) {
@compileError("bitfield doesn't fit");
}
const self_mask: FieldType = ((1 << num_bits) - 1) << shift_amount;
const ValueType: type = std.meta.Int(.unsigned, num_bits);
return extern struct {
dummy: FieldType,
const Self = @This();
pub fn write(self: *Self, val: ValueType) void {
self.writeNoShiftFullSize(@as(FieldType, val) << shift_amount);
}
/// Writes a value to the bitfield without shifting, all bits in `val` not in the bitfield are ignored.
///
/// Not atomic.
pub fn writeNoShiftFullSize(self: *Self, val: FieldType) void {
self.field().* =
(self.field().* & ~self_mask) |
(val & self_mask);
}
pub fn read(self: Self) ValueType {
return @truncate(self.readNoShiftFullSize() >> shift_amount);
}
/// Reads the full value of the bitfield without shifting and without truncating the type.
///
/// All bits not in the bitfield will be zero.
pub inline fn readNoShiftFullSize(self: Self) FieldType {
return (self.field().* & self_mask);
}
/// A function to access the underlying integer as `FieldType`.
/// Uses `anytype` to support both const and non-const access.
inline fn field(self: anytype) PtrCastPreserveCV(Self, @TypeOf(self), FieldType) {
return @ptrCast(self);
}
};
}
test Bitfield {
const S = extern union {
low: Bitfield(u32, 0, 16),
high: Bitfield(u32, 16, 16),
val: u32,
};
try std.testing.expect(@sizeOf(S) == 4);
try std.testing.expect(@bitSizeOf(S) == 32);
var s: S = .{ .val = 0x13376969 };
try std.testing.expect(s.low.read() == 0x6969);
try std.testing.expect(s.high.read() == 0x1337);
s.low.write(0x1337);
s.high.write(0x6969);
try std.testing.expect(s.val == 0x69691337);
}
/// Defines a struct representing a single bit.
fn BitType(
/// The type of the underlying integer containing the bit.
comptime FieldType: type,
/// The bit index of the bit.
comptime shift_amount: usize,
/// The type of the bit value, either u1 or bool.
comptime ValueType: type,
) type {
return extern struct {
bits: Bitfield(FieldType, shift_amount, 1),
const Self = @This();
pub fn read(self: Self) ValueType {
return @bitCast(getBit(self.bits.field().*, shift_amount));
}
pub fn write(self: *Self, val: ValueType) void {
setBit(self.bits.field(), shift_amount, @bitCast(val));
}
};
}
/// Defines a struct representing a single bit with a u1 value.
pub fn Bit(
/// The type of the underlying integer containing the bit.
comptime FieldType: type,
/// The bit index of the bit.
comptime shift_amount: usize,
) type {
return BitType(FieldType, shift_amount, u1);
}
test Bit {
const S = extern union {
low: Bit(u32, 0),
high: Bit(u32, 1),
val: u32,
};
try std.testing.expect(@sizeOf(S) == 4);
try std.testing.expect(@bitSizeOf(S) == 32);
var s: S = .{ .val = 1 };
try std.testing.expect(s.low.read() == 1);
try std.testing.expect(s.high.read() == 0);
s.low.write(0);
s.high.write(1);
try std.testing.expect(s.val == 2);
}
/// Defines a struct representing a single bit with a boolean value.
pub fn Boolean(
/// The type of the underlying integer containing the bit.
comptime FieldType: type,
/// The bit index of the bit.
comptime shift_amount: usize,
) type {
return BitType(FieldType, shift_amount, bool);
}
test Boolean {
const S = extern union {
low: Boolean(u32, 0),
high: Boolean(u32, 1),
val: u32,
};
try std.testing.expect(@sizeOf(S) == 4);
try std.testing.expect(@bitSizeOf(S) == 32);
var s: S = .{ .val = 2 };
try std.testing.expect(s.low.read() == false);
try std.testing.expect(s.high.read() == true);
s.low.write(true);
s.high.write(false);
try std.testing.expect(s.val == 1);
}
/// Casts a pointer while preserving const/volatile qualifiers.
inline fn PtrCastPreserveCV(comptime T: type, comptime PtrToT: type, comptime NewT: type) type {
return switch (PtrToT) {
*T => *NewT,
*const T => *const NewT,
*volatile T => *volatile NewT,
*const volatile T => *const volatile NewT,
else => @compileError("invalid type " ++ @typeName(PtrToT) ++ " given to PtrCastPreserveCV"),
};
}
comptime {
if (builtin.cpu.arch.endian() != .little) @compileError("'bitjuggle' assumes little endian");
}
comptime {
std.testing.refAllDeclsRecursive(@This());
}
const std = @import("std");
const builtin = @import("builtin");