mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.10 bitHelpers
This commit is contained in:
parent
dac025b101
commit
e8e7ccafe1
@ -6,7 +6,7 @@ jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- uses: arduino/arduino-lint-action@v1
|
||||
with:
|
||||
library-manager: update
|
||||
|
@ -8,7 +8,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: 2.6
|
||||
|
@ -10,7 +10,7 @@ jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- name: json-syntax-check
|
||||
uses: limitusus/json-syntax-check@v1
|
||||
with:
|
||||
|
@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
|
||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
## [0.1.10] - 2023-02-08
|
||||
- reorganize readme.md
|
||||
- update GitHub actions
|
||||
- update license 2023
|
||||
|
||||
|
||||
## [0.1.9] - 2022-10-29
|
||||
- add RP2040 to build-CI
|
||||
@ -12,7 +17,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
- minor edit unit test
|
||||
- add printHelpers lib in build-CI
|
||||
|
||||
|
||||
## [0.1.8] - 2022-04-13
|
||||
- split bitHelpers.h file into a .h and a .cpp file to prevent multiple declarations in some complexer projects.
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2015-2022 Rob Tillaart
|
||||
Copyright (c) 2015-2023 Rob Tillaart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -26,59 +26,83 @@ New bit functions can be added or investigated, please file an issue on GitHub.
|
||||
|
||||
## Interface
|
||||
|
||||
```cpp
|
||||
#include "bitHelpers.h"
|
||||
```
|
||||
|
||||
### 0.1.0
|
||||
#### BitCount
|
||||
|
||||
BitCount, several implementations to compare performance.
|
||||
several implementations to compare performance.
|
||||
|
||||
- **uint8_t bitCountReference(uint32_t value)** returns number of bits set in a value.
|
||||
- **uint8_t bitCountKR(uint32_t value)** Kerningham Ritchie bitCount.
|
||||
- **uint8_t bitCountArray(uint32_t value)** count per nybble with lookup table.
|
||||
- **uint8_t bitCountArray(uint32_t value)** count per nibble with lookup table.
|
||||
- **uint8_t bitCountF1(uint32_t value)** SWAG algorithm variant.
|
||||
- **uint8_t bitCountF2(uint32_t value)** SWAG algorithm variant.
|
||||
|
||||
BitCount - fastest version, SWAG algorithm
|
||||
|
||||
- **uint8_t bitCount(uint8_t value)** available for 16, 32 and 64 bit.
|
||||
- **uint8_t bitCount(uint8_t value)**
|
||||
- **uint8_t bitCount(uint16_t value)**
|
||||
- **uint8_t bitCount(uint32_t value)**
|
||||
- **uint8_t bitCount(uint64_t value)**
|
||||
|
||||
Reverse: uint8_t .. uint64_t
|
||||
|
||||
- **T bitReverse(T value)** reverses bits in a uint8_t .. uint64_t.
|
||||
#### Reverse
|
||||
|
||||
T = uint8_t .. uint64_t
|
||||
|
||||
- **T bitReverse(T value)** reverses bits.
|
||||
- **T nybbleReverse(T value)** reverses nibbles (4 bit) in a uint8_t .. uint64_t.
|
||||
- **T byteReverse(T value)** reverses bytes (8 bit) in a uint16_t .. uint64_t.
|
||||
- **T wordReverse(T value)** reverses words (16 bit) in uint32_t and uint64_t.
|
||||
|
||||
Swap upper and lower half: uint8_t .. uint64_t.
|
||||
#### Swap
|
||||
|
||||
swap upper and lower half: uint8_t .. uint64_t. Is like rotate 50%
|
||||
|
||||
- **T swap(T value)** 0x12345678 ==> 0x56781234.
|
||||
|
||||
|
||||
#### BitRotate
|
||||
|
||||
Rotate Left / Right: uint8_t .. uint64_t
|
||||
if pos larger than # bits original value is returned.
|
||||
|
||||
- **T bitRotateLeft(T value, uint8_t pos)**
|
||||
- **T bitRotateRight(T value, uint8_t pos)**
|
||||
|
||||
|
||||
#### BitFlip
|
||||
|
||||
BitFlip: uint8_t .. uint64_t a.k.a toggle
|
||||
if pos larger than # bits original value is returned.
|
||||
|
||||
- **T bitFlip(T value, uint8_t pos)** flips a single bit at pos
|
||||
|
||||
|
||||
#### BitRot
|
||||
|
||||
BitRot: uint8_t .. uint64_t
|
||||
|
||||
- **T bitRotRef(T value, float chance = 0.5, uint8_t times = 1)** reference implementation.
|
||||
- **T bitRot(T value, float chance = 0.5, uint8_t times = 1)** random damage to a single bit of a value,
|
||||
chance = float 0.0 .. 1.0 that one random bit is toggled.
|
||||
The times parameter allows to apply this n times.
|
||||
**bitRot()** is a function that can be used to mimic single bit errors in communication protocols.
|
||||
**bitRot()** is a function that can be used to mimic (single) bit errors in communication protocols.
|
||||
*Note: a chance of 50% for 2 uint8_t is not equal to 50% chance for 1 uint16_t.*
|
||||
|
||||
|
||||
### 0.1.1 added
|
||||
#### BitsNeeded
|
||||
|
||||
How many bits are needed to store / transmit a number?
|
||||
|
||||
- **bitsNeededReference(n)** reference implementation for uint8_t to uint64_t.
|
||||
- **bitsNeeded(n)** A 'recursive strategy' for uint8_t .. uint64_t provides a fast answer.
|
||||
|
||||
|
||||
#### BitSet64 et al.
|
||||
|
||||
The following functions are made as the normal **bitset()** etcetera do not work for 64 bit.
|
||||
These functions are optimized for speed for **AVR**, **ESP32** and **ESP8266**.
|
||||
|
||||
@ -97,46 +121,46 @@ Also added are macro versions of these five functions.
|
||||
- **mbitRead64(x, bit)** reads bit from uint64_t
|
||||
|
||||
|
||||
### 0.1.2 and beyond
|
||||
|
||||
See CHANGELOG.md
|
||||
|
||||
|
||||
## Operations
|
||||
|
||||
See examples.
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
#### Must
|
||||
|
||||
- improve documentation
|
||||
- improve readability of code
|
||||
|
||||
#### Should
|
||||
|
||||
- add performance tests
|
||||
- **bitRotateLeftRight()** should it do modulo pos?
|
||||
- **bitsNeededRef()** correct for value 0?
|
||||
- **nybbleReverse()** => **nibbleReverse()**
|
||||
|
||||
|
||||
#### Functions add
|
||||
#### Could
|
||||
|
||||
- besides **bitRot()** one can also have timing issues when clocking in bits.
|
||||
A function could be created to mimic such timing error, by shifting bits from a
|
||||
specific position. e.g.
|
||||
- **parShiftLeft(00001010, 3)** ==> 00011010
|
||||
- **bitBurst(00000000, 3)** ==> 00111000 any group of 3 bits will toggle. edges?
|
||||
- **bitRot(value, chance = 50%, times = 1)** extension...
|
||||
- **bitNoggle(value, bit)** - toggle all but one bit. (why?)
|
||||
- **bitSort(value)** 00101001 ==> 00000111
|
||||
or with minimal # toggles?
|
||||
- **bitReverse(uint32_t x, uint8_t n)**
|
||||
- **bitReverse(uint32_t x, uint8_t n)** see below.
|
||||
- **byteReverse24(uint32_t x)** dedicated 24 bit = 3 bytes e.g RGB
|
||||
- **byteInverse(uint32_t x)** (a,b,c,d) => (255-a, 255-b, 255-c, 255-d) = rather simple ~?
|
||||
- **isBitPalindrome()** byte, word ...
|
||||
- **bitSwap(value, p, q)**
|
||||
- many more :)
|
||||
|
||||
|
||||
#### Functions fix
|
||||
#### Wont
|
||||
|
||||
- **bitRotateLeftRight()** should it do modulo pos?
|
||||
- **bitsNeededRef()** correct for value 0?
|
||||
|
||||
|
||||
## ideas
|
||||
|
||||
#### BitReverse n bit number
|
||||
|
||||
Trick to reverse a number of n bits ( 0 < n < 32 ).
|
||||
@ -147,14 +171,14 @@ not as fast as a dedicated version.
|
||||
uint32_t bitReverse(uint32_t x, uint8_t n)
|
||||
{
|
||||
uint32_t r = bitReverse(x);
|
||||
return r >> (32 - n);
|
||||
return r >> (32 - n); // reverse only top n bits.
|
||||
}
|
||||
```
|
||||
Could be added in next release...
|
||||
|
||||
Q: what to do with the first (32-n) bits?
|
||||
Just reverse the last 24 bits and clear bit 24-31 is different than
|
||||
reversing the last 24 bits and keel bit 24-31 as is.
|
||||
reversing the last 24 bits and keep bit 24-31 as is.
|
||||
```cpp
|
||||
uint32_t bitReverse(uint32_t x, uint8_t n)
|
||||
{
|
||||
@ -163,26 +187,4 @@ uint32_t bitReverse(uint32_t x, uint8_t n)
|
||||
r >>= (32 - n);
|
||||
return y | r;
|
||||
}
|
||||
```
|
||||
|
||||
## Future
|
||||
|
||||
#### Must
|
||||
|
||||
- redo documentation
|
||||
- logical groups
|
||||
|
||||
#### Should
|
||||
|
||||
#### Could
|
||||
|
||||
- besides **bitRot()** one can also have timing issues when clocking in bits.
|
||||
A function could be created to mimic such timing error, by shifting bits from a
|
||||
specific position. e.g.
|
||||
- parShiftLeft(00001010, 4) ==> 00011010
|
||||
- bitBurst(00000000, 3) ==> 00111000 any group of 3 bits will toggle.
|
||||
- bitRot(value, chance = 50%, times = 1) extention...
|
||||
- bitNoggle(value, bit) - toggle all but one bit. (why?)
|
||||
- bitSort(value) ==> 00101001 ==> 00000111
|
||||
- many more :)
|
||||
|
||||
```
|
@ -1,12 +1,10 @@
|
||||
//
|
||||
// FILE: bitHelpers.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.9
|
||||
// VERSION: 0.1.10
|
||||
// DATE: 2015-11-07
|
||||
// PURPOSE: Arduino library with functions on bit level
|
||||
// URL: https://github.com/RobTillaart/bitHelpers
|
||||
//
|
||||
// HISTORY: See CHANGELOG.md
|
||||
|
||||
|
||||
#include "bitHelpers.h"
|
||||
@ -656,5 +654,5 @@ uint8_t bitsNeeded(uint64_t x)
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -2,18 +2,17 @@
|
||||
//
|
||||
// FILE: bitHelpers.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.9
|
||||
// VERSION: 0.1.10
|
||||
// DATE: 2015-11-07
|
||||
// PURPOSE: Arduino library with functions on bit level
|
||||
// URL: https://github.com/RobTillaart/bitHelpers
|
||||
//
|
||||
// HISTORY: See CHANGELOG.md
|
||||
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#define BITHELPER_LIB_VERSION (F("0.1.9"))
|
||||
|
||||
#define BITHELPER_LIB_VERSION (F("0.1.10"))
|
||||
|
||||
|
||||
// used by bitRot()
|
||||
// power of 2 gives better uniform distribution in the last bits
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/bitHelpers.git"
|
||||
},
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"license": "MIT",
|
||||
"frameworks": "*",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=bitHelpers
|
||||
version=0.1.9
|
||||
version=0.1.10
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library with functions on bit level
|
||||
|
@ -120,4 +120,6 @@ unittest(test_bitsNeeded)
|
||||
|
||||
unittest_main()
|
||||
|
||||
// --------
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user