Contiki-NG
Toggle main menu visibility
Loading...
Searching...
No Matches
bitrev.c
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2023, RISE Research Institutes of Sweden AB
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the copyright holder nor the names of its
14
* contributors may be used to endorse or promote products derived
15
* from this software without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28
* OF THE POSSIBILITY OF SUCH DAMAGE.
29
*/
30
31
/**
32
* \addtogroup bitrev
33
* @{
34
*/
35
36
/**
37
* \file
38
* Bit reversal library implementation
39
* \author
40
* Joakim Eriksson <joakim.eriksson@ri.se>
41
*/
42
43
#include "
bitrev.h
"
44
/*---------------------------------------------------------------------------*/
45
/*
46
* Lookup table for bit reversal
47
*
48
* This precomputed lookup table allows O(1) bit reversal for each byte.
49
* The table is generated using the bit manipulation technique where:
50
* - R2(n) generates 4 entries: n, n+128, n+64, n+192
51
* - R4(n) generates 16 entries by applying R2 to 4 different bases
52
* - R6(n) generates 64 entries by applying R4 to 4 different bases
53
* - Final call generates all 256 entries
54
*/
55
static
const
uint8_t bitrev_lookup_table[256] = {
56
#define R2(n) n, n + 2*64, n + 1*64, n + 3*64
57
#define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16)
58
#define R6(n) R4(n), R4(n + 2*4), R4(n + 1*4), R4(n + 3*4)
59
R6(0), R6(2), R6(1), R6(3)
60
#undef R2
61
#undef R4
62
#undef R6
63
};
64
/*---------------------------------------------------------------------------*/
65
uint8_t
66
bitrev_byte
(uint8_t
byte
)
67
{
68
return
bitrev_lookup_table[byte];
69
}
70
/*---------------------------------------------------------------------------*/
71
void
72
bitrev_array
(uint8_t *data,
size_t
len)
73
{
74
size_t
i;
75
for
(i = 0; i < len; i++) {
76
data[i] = bitrev_lookup_table[data[i]];
77
}
78
}
79
/*---------------------------------------------------------------------------*/
80
void
81
bitrev_array_copy
(
const
uint8_t *input, uint8_t *output,
size_t
len)
82
{
83
size_t
i;
84
for
(i = 0; i < len; i++) {
85
output[i] = bitrev_lookup_table[input[i]];
86
}
87
}
88
/*---------------------------------------------------------------------------*/
89
/** @} */
bitrev.h
Bit reversal library header.
bitrev_byte
uint8_t bitrev_byte(uint8_t byte)
Reverse the bits in a single byte.
Definition
bitrev.c:66
bitrev_array_copy
void bitrev_array_copy(const uint8_t *input, uint8_t *output, size_t len)
Reverse bits in all bytes of an array (copy to output).
Definition
bitrev.c:81
bitrev_array
void bitrev_array(uint8_t *data, size_t len)
Reverse bits in all bytes of an array (in-place).
Definition
bitrev.c:72
os
lib
bitrev.c
Generated on
for Contiki-NG by
1.17.0