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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*! Ordering of bits within register elements.

`bitvec` structures are parametric over any ordering of bits within a register.
The `BitOrder` trait maps a cursor position (indicated by the `BitIdx` type) to an
electrical position (indicated by the `BitPos` type) within that element, and
also defines the order of traversal over a register.

The only requirement on implementors of `BitOrder` is that the transform function
from cursor (`BitIdx`) to position (`BitPos`) is *total* (every integer in the
domain `0 .. T::BITS` is used) and *unique* (each cursor maps to one and only
one position, and each position is mapped by one and only one cursor).
Contiguity is not required.

`BitOrder` is a stateless trait, and implementors should be zero-sized types.
!*/

use crate::index::{
	BitIdx,
	BitMask,
	BitPos,
	BitRegister,
	BitSel,
	BitTail,
};

/** An ordering over a register.

# Usage

`bitvec` structures store and operate on semantic counts, not bit positions. The
`BitOrder::at` function takes a semantic ordering, `BitIdx`, and produces an
electrical position, `BitPos`.

# Safety

If your implementation violates any of the requirements on these functions, then
the program will become incorrect and have unspecified behavior. The best-case
scenario is that operations relying on your implementation will crash the
program; the worst-case is that memory access will silently become corrupt.

You are responsible for adhering to the requirements of these functions. In the
future, a verification function may be provided for your test suite; however, it
is not yet possible to verify your implementation at compile-time.

This is an `unsafe trait` to implement, because you are responsible for
upholding the state requirements. The types you manipulate have `unsafe fn`
constructors, because they require you to maintain correct and consistent
processes in order for the rest of the library to use them.

The implementations of `BitOrder` are trusted to drive safe code, and once data
leaves a `BitOrder` implementation, it is considered safe to use as the basis
for interaction with memory.

# Verification

Rust currently lacks Zig’s compile-time computation capability. This means that
`bitvec` cannot fail a compile if it detects that a `BitOrder` implementation is
invalid and breaks the stated requirements. `bitvec` does offer a function,
[`verify`], which ensures the correctness of an implementation. When Rust gains
the capability to run this function in generic `const` contexts, `bitvec` will
use it to prevent at compile-time the construction of data structures that use
incorrect ordering implementations.

The verifier function panics when it detects invalid behavior, with an error
message intended to clearly indicate the broken requirement.

```rust
use bitvec::{
  index::{BitIdx, BitPos, BitRegister},
  order::{self, BitOrder},
};
# use bitvec::{index::*, order::Lsb0};

pub struct Custom;
unsafe impl BitOrder for Custom {
  fn at<R: BitRegister>(idx: BitIdx<R>) -> BitPos<R> {
  // impl
  # return Lsb0::at::<R>(idx);
  }
}

#[test]
#[cfg(test)]
fn prove_custom() {
  order::verify::<Custom>();
}
```

[`verify`]: fn.verify.html
**/
pub unsafe trait BitOrder {
	/// Converts a semantic bit index into an electrical bit position.
	///
	/// This function is the basis of the trait, and must adhere to a number of
	/// requirements in order for an implementation to be considered correct.
	///
	/// # Parameters
	///
	/// - `index`: The semantic index of a bit within a register `R`.
	///
	/// # Returns
	///
	/// The electrical position of the indexed bit within a register `R`. See
	/// the `BitPos` documentation for what electrical positions are considered
	/// to mean.
	///
	/// # Type Parameters
	///
	/// - `R`: The register type which the index and position describe.
	///
	/// # Requirements
	///
	/// This function must satisfy the following requirements for all possible
	/// input and output values for all possible type parameters:
	///
	/// ## Totality
	///
	/// This function must be able to accept every input in the `BitIdx<R>`
	/// value range, and produce a corresponding `BitPos<R>`. It must not abort
	/// the program or return an invalid `BitPos<R>` for any input value in the
	/// `BitIdx<R>` range.
	///
	/// ## Bijection
	///
	/// There must be an exactly one-to-one correspondence between input value
	/// and output value. No input index may select from a set of more than one
	/// output position, and no output position may be produced by more than one
	/// input index.
	///
	/// ## Purity
	///
	/// The translation from index to position must be consistent for the
	/// lifetime of the program. This function *may* refer to global state, but
	/// that state **must** be immutable for the program lifetime, and must not
	/// be used to violate the totality or bijection requirements.
	///
	/// ## Output Validity
	///
	/// The produced `BitPos<R>` must be within the valid range of that type.
	/// Call sites of this function will not take any steps to constrain the
	/// output value. If you use `unsafe` code to produce an invalid
	/// `BitPos<R>`, the program is permanently incorrect, and will likely
	/// crash.
	///
	/// # Usage
	///
	/// This function will only ever be called with input values in the valid
	/// `BitIdx<R>` range. Implementors are not required to consider any values
	/// outside this range in their function body.
	fn at<R>(index: BitIdx<R>) -> BitPos<R>
	where R: BitRegister;

	/// Converts a semantic bit index into a one-hot selector mask.
	///
	/// This is an optional function; a default implementation is provided for
	/// you.
	///
	/// The default implementation of this function calls `Self::at` to produce
	/// an electrical position, then turns that into a selector mask by setting
	/// the `n`th bit more significant than the least significant bit of the
	/// element. `BitOrder` implementations may choose to provide a faster mask
	/// production here, but they must satisfy the requirements listed below.
	///
	/// # Parameters
	///
	/// - `index`: The semantic index of a bit within a register `R`.
	///
	/// # Returns
	///
	/// A one-hot selector mask for the bit indicated by the index value.
	///
	/// # Type Parameters
	///
	/// - `R`: The storage type for which the mask will be calculated. The mask
	///   must also be this type, as it will be applied to a register of `R` in
	///   order to set, clear, or test a single bit.
	///
	/// # Requirements
	///
	/// A one-hot encoding means that there is exactly one bit set in the
	/// produced value. It must be equivalent to `1 << Self::at::<R>(place)`.
	///
	/// As with `at`, this function must produce a unique mapping from each
	/// legal index in the `R` domain to a one-hot value of `R`.
	#[inline]
	fn select<R>(index: BitIdx<R>) -> BitSel<R>
	where R: BitRegister {
		Self::at::<R>(index).select()
	}

	/// Constructs a multi-bit selector mask for batch operations on a single
	/// register `R`.
	///
	/// The default implementation of this function traverses the index range,
	/// converting each index into a single-bit selector with `Self::select` and
	/// accumulating into a combined register value.
	///
	/// # Parameters
	///
	/// - `from`: The inclusive starting index for the mask.
	/// - `upto`: The exclusive ending index for the mask.
	///
	/// # Returns
	///
	/// A bit-mask with all bits corresponding to the input index range set high
	/// and all others set low.
	///
	/// # Type Parameters
	///
	/// - `R`: The storage type for which the mask will be calculated. The mask
	///   must also be this type, as it will be applied to a register of `R` in
	///   order to set, clear, or test all the selected bits.
	///
	/// # Requirements
	///
	/// This function must always be equivalent to
	///
	/// ```rust,ignore
	/// (from .. upto)
	///   .map(1 << Self::at::<R>)
	///   .fold(0, |mask, sel| mask | sel)
	/// ```
	#[inline]
	fn mask<R>(
		from: impl Into<Option<BitIdx<R>>>,
		upto: impl Into<Option<BitTail<R>>>,
	) -> BitMask<R>
	where
		R: BitRegister,
	{
		let (from, upto) = match (from.into(), upto.into()) {
			(None, None) => return BitMask::ALL,
			(Some(from), None) => (from, BitTail::<R>::END),
			(None, Some(upto)) => (BitIdx::<R>::ZERO, upto),
			(Some(from), Some(upto)) => (from, upto),
		};
		BitIdx::<R>::range(from, upto).map(Self::select::<R>).sum()
	}
}

/// Traverses a register from `MSbit` to `LSbit`.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Msb0;

unsafe impl BitOrder for Msb0 {
	#[cfg_attr(not(tarpaulin_include), inline(always))]
	fn at<R>(index: BitIdx<R>) -> BitPos<R>
	where R: BitRegister {
		unsafe { BitPos::new_unchecked(R::MASK - index.value()) }
	}

	#[cfg_attr(not(tarpaulin_include), inline(always))]
	fn select<R>(index: BitIdx<R>) -> BitSel<R>
	where R: BitRegister {
		/* Set the MSbit, then shift it down. The left expr is const-folded.
		Note: this is not equivalent to `1 << (mask - index)`, because that
		requires a runtime subtraction, but the expression below is only a
		single right-shift.
		*/
		unsafe { BitSel::new_unchecked((R::ONE << R::MASK) >> index.value()) }
	}

	#[inline]
	fn mask<R>(
		from: impl Into<Option<BitIdx<R>>>,
		upto: impl Into<Option<BitTail<R>>>,
	) -> BitMask<R>
	where
		R: BitRegister,
	{
		let from = from.into().unwrap_or(BitIdx::ZERO).value();
		let upto = upto.into().unwrap_or(BitTail::END).value();
		debug_assert!(upto >= from, "Ranges must run from low index to high");
		let ct = upto - from;
		if ct == R::BITS {
			return BitMask::ALL;
		}
		//  1. Set all bits high.
		//  2. Shift right by the number of bits in the mask. These are now low.
		//  3. Invert. The mask bits (at MSedge) are high; the rest are low.
		//  4. Shift right by the distance from MSedge.
		unsafe { BitMask::new(!(R::ALL >> ct) >> from) }
	}
}

/// Traverses a register from `LSbit` to `MSbit`.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Lsb0;

unsafe impl BitOrder for Lsb0 {
	#[cfg_attr(not(tarpaulin), inline(always))]
	fn at<R>(index: BitIdx<R>) -> BitPos<R>
	where R: BitRegister {
		unsafe { BitPos::new_unchecked(index.value()) }
	}

	#[cfg_attr(not(tarpaulin), inline(always))]
	fn select<R>(index: BitIdx<R>) -> BitSel<R>
	where R: BitRegister {
		unsafe { BitSel::new_unchecked(R::ONE << index.value()) }
	}

	#[inline]
	fn mask<R>(
		from: impl Into<Option<BitIdx<R>>>,
		upto: impl Into<Option<BitTail<R>>>,
	) -> BitMask<R>
	where
		R: BitRegister,
	{
		let from = from.into().unwrap_or(BitIdx::ZERO).value();
		let upto = upto.into().unwrap_or(BitTail::END).value();
		debug_assert!(upto >= from, "Ranges must run from low index to high");
		let ct = upto - from;
		if ct == R::BITS {
			return BitMask::ALL;
		}
		//  1. Set all bits high.
		//  2. Shift left by the number of bits in the mask. These are now low.
		//  3. Invert. The mask bits at LSedge are high; the rest are low.
		//  4. Shift left by the distance from LSedge.
		unsafe { BitMask::new(!(R::ALL << ct) << from) }
	}
}

/** A default bit ordering.

Typically, your platform’s C compiler uses most-significant-bit-first ordering
for bitfields. The Msb0 bit ordering and big-endian byte ordering are otherwise
completely unrelated.
**/
#[cfg(target_endian = "big")]
pub type LocalBits = Msb0;

/** A default bit ordering.

Typically, your platform’s C compiler uses least-significant-bit-first ordering
for bitfields. The Lsb0 bit ordering and little-endian byte ordering are
otherwise completely unrelated.
**/
#[cfg(target_endian = "little")]
pub type LocalBits = Lsb0;

#[cfg(not(any(target_endian = "big", target_endian = "little")))]
compile_fail!(concat!(
	"This architecture is currently not supported. File an issue at ",
	env!(CARGO_PKG_REPOSITORY)
));

/** Verifies a `BitOrder` implementation’s adherence to the stated rules.

This function checks some `BitOrder` implementation’s behavior on each of the
`BitRegister` types it must handle, and reports any violation of the rules that
it detects.

# Type Parameters

- `O`: The `BitOrder` implementation to test.

# Parameters

- `verbose`: Sets whether the test should print diagnostic information to
  `stdout`.

# Panics

This panics if it detects any violation of the `BitOrder` implementation rules
for `O`.
**/
#[cfg(test)]
pub fn verify<O>(verbose: bool)
where O: BitOrder {
	verify_for_type::<O, u8>(verbose);
	verify_for_type::<O, u16>(verbose);
	verify_for_type::<O, u32>(verbose);
	verify_for_type::<O, usize>(verbose);

	#[cfg(target_pointer_width = "64")]
	verify_for_type::<O, u64>(verbose);
}

/** Verifies a `BitOrder` implementation’s adherence to the stated rules, for
one register type.

This function checks some `BitOrder` implementation against only one of the
`BitRegister` types that it will encounter. This is useful if you are
implementing an ordering that only needs to be concerned with a subset of the
types, and you know that you will never use it with the types it does not
support.

# Type Parameters

- `O`: The `BitOrder` implementation to test.
- `R`: The `BitRegister` type for which to test `O`.

# Parameters

- `verbose`: Sets whether the test should print diagnostic information to
  `stdout`.

# Panics

This panics if it detects any violation of the `BitOrder` implementation rules
for the combination of input types and index values.
**/
#[cfg(test)]
pub fn verify_for_type<O, R>(verbose: bool)
where
	O: BitOrder,
	R: BitRegister,
{
	use core::any::type_name;
	let mut accum = BitMask::<R>::ZERO;

	let oname = type_name::<O>();
	let mname = type_name::<R>();

	for n in 0 .. R::BITS {
		//  Wrap the counter as an index.
		let idx = unsafe { BitIdx::<R>::new_unchecked(n) };

		//  Compute the bit position for the index.
		let pos = O::at::<R>(idx);
		if verbose {
			#[cfg(feature = "std")]
			println!(
				"`<{} as BitOrder>::at::<{}>({})` produces {}",
				oname,
				mname,
				n,
				pos.value(),
			);
		}

		//  If the computed position exceeds the valid range, fail.
		assert!(
			pos.value() < R::BITS,
			"Error when verifying the implementation of `BitOrder` for `{}`: \
			 Index {} produces a bit position ({}) that exceeds the type width \
			 {}",
			oname,
			n,
			pos.value(),
			R::BITS,
		);

		//  Check `O`’s implementation of `select`
		let sel = O::select::<R>(idx);
		if verbose {
			#[cfg(feature = "std")]
			println!(
				"`<{} as BitOrder>::select::<{}>({})` produces {:b}",
				oname, mname, n, sel,
			);
		}

		//  If the selector bit is not one-hot, fail.
		assert_eq!(
			sel.value().count_ones(),
			1,
			"Error when verifying the implementation of `BitOrder` for `{}`: \
			 Index {} produces a bit selector ({:b}) that is not a one-hot mask",
			oname,
			n,
			sel,
		);

		//  Check that the selection computed from the index matches the
		//  selection computed from the position.
		let shl = pos.select();
		//  If `O::select(idx)` does not produce `1 << pos`, fail.
		assert_eq!(
			sel,
			shl,
			"Error when verifying the implementation of `BitOrder` for `{}`: \
			 Index {} produces a bit selector ({:b}) that is not equal to `1 \
			 << {}` ({:b})",
			oname,
			n,
			sel,
			pos.value(),
			shl,
		);

		//  Check that the produced selector bit has not already been added to
		//  the accumulator.
		assert!(
			!accum.test(sel),
			"Error when verifying the implementation of `BitOrder` for `{}`: \
			 Index {} produces a bit position ({}) that has already been \
			 produced by a prior index",
			oname,
			n,
			pos.value(),
		);
		accum.insert(sel);
		if verbose {
			#[cfg(feature = "std")]
			println!(
				"`<{} as BitOrder>::at::<{}>({})` accumulates  {:b}",
				oname, mname, n, accum,
			);
		}
	}

	//  Check that all indices produced all positions.
	assert_eq!(
		accum,
		BitMask::ALL,
		"Error when verifying the implementation of `BitOrder` for `{}`: The \
		 bit positions marked with a `0` here were never produced from an \
		 index, despite all possible indices being passed in for translation: \
		 {:b}",
		oname,
		accum,
	);

	//  Check that `O::mask` is correct for all range combinations.
	for from in BitIdx::<R>::range_all() {
		for upto in BitTail::<R>::range_from(from) {
			let mask = O::mask(from, upto);
			let check = BitIdx::<R>::range(from, upto)
				.map(O::at::<R>)
				.map(BitPos::<R>::select)
				.sum::<BitMask<R>>();
			assert_eq!(
				mask,
				check,
				"Error when verifying the implementation of `BitOrder` for \
				 `{o}`: `{o}::mask::<{m}>({f}, {u})` produced {bad:b}, but \
				 expected {good:b}",
				o = oname,
				m = mname,
				f = from,
				u = upto,
				bad = mask,
				good = check,
			);
		}
	}
}

#[cfg(all(test, not(miri)))]
mod tests {
	use super::*;

	#[test]
	fn verify_impls() {
		verify::<Lsb0>(cfg!(feature = "testing"));
		verify::<Msb0>(cfg!(feature = "testing"));

		struct DefaultImpl;
		unsafe impl BitOrder for DefaultImpl {
			fn at<R>(index: BitIdx<R>) -> BitPos<R>
			where R: BitRegister {
				unsafe { BitPos::new_unchecked(index.value()) }
			}
		}

		verify::<DefaultImpl>(cfg!(feature = "testing"));
	}
}