0.1.4 bitHelpers

This commit is contained in:
rob tillaart 2021-10-19 16:59:51 +02:00
parent c3428ffc12
commit 0a5792b6ee
11 changed files with 123 additions and 20 deletions

View File

@ -2,6 +2,10 @@ compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
- leonardo
- due
- zero
# - due
# - zero
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560

View File

@ -4,10 +4,14 @@ name: Arduino CI
on: [push, pull_request]
jobs:
arduino_ci:
runTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: Arduino-CI/action@master
# Arduino-CI/action@v0.1.1
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb

View File

@ -1,5 +1,7 @@
[![Arduino CI](https://github.com/RobTillaart/bitHelpers/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/bitHelpers/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/bitHelpers/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/bitHelpers/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/bitHelpers/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/bitHelpers/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/bitHelpers.svg?maxAge=3600)](https://github.com/RobTillaart/bitHelpers/releases)
@ -8,6 +10,7 @@
Arduino library with functions on bit level
## Description
This library contains functions to manipulate bits and bit patterns in an
@ -94,6 +97,12 @@ Added Arduino-CI and unit tests
- update unit tests
### 0.1.4
- update build-CI (e.g. ESP32 compilation of tests)
- update readme.md with badges
## BitReverse n bit number
Trick to reverse a number of n bits ( 0 < n < 32 ).
@ -117,13 +126,14 @@ 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...
- bitRot(value, chance = 50%, times = 1) extension...
- bitNoggle(value, bit) - toggle all but one bit. (why?)
- bitSort(value) ==> 00101001 ==> 00000111
- many more :)
- add **bitReverse(uint32_t x, uint8_t n)**
- add **byteReverse24(uint32_t x)** dedicated 24 bit = 3 bytes e.g RGB
- add **byteInverse(uint32_t x)** (a,b,c,d) => (255-a, 255-b, 255-c, 255-d)
- performance tests
## Operations

View File

@ -2,22 +2,24 @@
//
// FILE: bitHelpers.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// VERSION: 0.1.4
// DATE: 2015-11-07
// PURPOSE: Arduino library with functions on bit level
// URL: https://github.com/RobTillaart/bitHelpers
//
// 0.0.1 2015-11-07 initial version
// 0.1.0 2020-07-29 initial release
// 0.1.1 2020-08-10 added BitsNeeded, bitSet64 family
// 0.1.2 2020-12-14 add Arduino-CI + unit tests
// 0.1.3 2021-08-09 update readme.md + unit tests
// HISTORY
// 0.0.1 2015-11-07 initial version
// 0.1.0 2020-07-29 initial release
// 0.1.1 2020-08-10 added BitsNeeded, bitSet64 family
// 0.1.2 2020-12-14 add Arduino-CI + unit tests
// 0.1.3 2021-08-09 update readme.md + unit tests
// 0.1.4 2021-10-19 update Arduino-CI + badges
#include "Arduino.h"
#define BH_BIG_NR 1000000000
#define BITHELPER_LIB_VERSION (F("0.1.4"))
#define BH_BIG_NR 1000000000
////////////////////////////////////////////////
@ -36,6 +38,7 @@ uint8_t bitCountReference(uint32_t value)
return count;
};
uint8_t bitCountKR(uint32_t value)
{
// Kerningham & Ritchie
@ -49,6 +52,7 @@ uint8_t bitCountKR(uint32_t value)
return count;
};
uint8_t bitCountArray(uint32_t value)
{
uint8_t ar[] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
@ -62,6 +66,7 @@ uint8_t bitCountArray(uint32_t value)
return count;
};
uint8_t bitCountF1(uint32_t value)
{
// parallel adding in a register SWAG algorithm
@ -74,6 +79,7 @@ uint8_t bitCountF1(uint32_t value)
return v & 0x0000003F;
};
uint8_t bitCountF2(uint32_t value)
{
// parallel adding in a register SWAG algorithm
@ -84,6 +90,7 @@ uint8_t bitCountF2(uint32_t value)
return ( v * 0x01010101) >> 24;
};
////////////////////////////////////////////////
//
// BIT COUNT
@ -98,6 +105,7 @@ uint8_t bitCount(uint8_t value)
return v;
};
uint8_t bitCount(uint16_t value)
{
// parallel adding in a register SWAG algorithm
@ -109,6 +117,7 @@ uint8_t bitCount(uint16_t value)
return v & 0xFF;
};
uint8_t bitCount(uint32_t value)
{
// parallel adding in a register SWAG algorithm
@ -121,6 +130,7 @@ uint8_t bitCount(uint32_t value)
return v & 0xFF;
};
uint8_t bitCount(uint64_t value)
{
// parallel adding in a register SWAG algorithm
@ -148,6 +158,7 @@ uint8_t bitReverse(uint8_t val)
return x;
}
uint16_t bitReverse(uint16_t val)
{
uint16_t x = val;
@ -158,6 +169,7 @@ uint16_t bitReverse(uint16_t val)
return x;
}
uint32_t bitReverse(uint32_t val)
{
uint32_t x = val;
@ -169,6 +181,7 @@ uint32_t bitReverse(uint32_t val)
return x;
}
uint64_t bitReverse(uint64_t val)
{
uint64_t x = val;
@ -193,6 +206,7 @@ uint8_t nybbleReverse(uint8_t val)
return x;
}
uint16_t nybbleReverse(uint16_t val)
{
uint16_t x = val;
@ -201,6 +215,7 @@ uint16_t nybbleReverse(uint16_t val)
return x;
}
uint32_t nybbleReverse(uint32_t val)
{
uint32_t x = val;
@ -210,6 +225,7 @@ uint32_t nybbleReverse(uint32_t val)
return x;
}
uint64_t nybbleReverse(uint64_t val)
{
uint64_t x = val;
@ -220,6 +236,7 @@ uint64_t nybbleReverse(uint64_t val)
return x;
}
////////////////////////////////////////////////
//
// BYTE REVERSE
@ -231,6 +248,7 @@ uint16_t byteReverse(uint16_t val)
return x;
}
uint32_t byteReverse(uint32_t val)
{
uint32_t x = val;
@ -239,6 +257,7 @@ uint32_t byteReverse(uint32_t val)
return x;
}
uint64_t byteReverse(uint64_t val)
{
uint64_t x = val;
@ -260,6 +279,7 @@ uint32_t wordReverse(uint32_t val)
return x;
}
uint64_t wordReverse(uint64_t val)
{
uint64_t x = val;
@ -278,16 +298,19 @@ uint8_t swap(uint8_t val)
return (val << 4) | (val >> 4);
}
uint16_t swap(uint16_t val)
{
return (val << 8) | (val >> 8);
}
uint32_t swap(uint32_t val)
{
return (val << 16) | (val >> 16);
}
uint64_t swap(uint64_t val)
{
return (val << 32) | (val >> 32);
@ -304,18 +327,21 @@ uint8_t bitRotateLeft(uint8_t value, uint8_t pos)
return (value << pos) | (value >> (8 - pos));
}
uint16_t bitRotateLeft(uint16_t value, uint8_t pos)
{
if (pos > 15) return value;
return (value << pos) | (value >> (16 - pos));
}
uint32_t bitRotateLeft(uint32_t value, uint8_t pos)
{
if (pos > 31) return value;
return (value << pos) | (value >> (32 - pos));
}
uint64_t bitRotateLeft(uint64_t value, uint8_t pos)
{
if (pos > 63) return value;
@ -333,18 +359,21 @@ uint8_t bitRotateRight(uint8_t value, uint8_t pos)
return (value << (8 - pos)) | (value >> pos);
}
uint16_t bitRotateRight(uint16_t value, uint8_t pos)
{
if (pos > 15) return value;
return (value << (16 - pos)) | (value >> pos);
}
uint32_t bitRotateRight(uint32_t value, uint8_t pos)
{
if (pos > 31) return value;
return (value << (32 - pos)) | (value >> pos);
}
uint64_t bitRotateRight(uint64_t value, uint8_t pos)
{
if (pos > 63) return value;
@ -362,18 +391,21 @@ uint8_t bitFlip(uint8_t value, uint8_t pos)
return value ^ (1 << pos);
}
uint16_t bitFlip(uint16_t value, uint8_t pos)
{
if (pos > 15) return value;
return value ^ (1 << pos);
}
uint32_t bitFlip(uint32_t value, uint8_t pos)
{
if (pos > 31) return value;
return value ^ (1UL << pos);
}
uint64_t bitFlip(uint64_t value, uint8_t pos)
{
if (pos > 63) return value;
@ -391,18 +423,21 @@ uint8_t bitRot(uint8_t value, float chance = 0.5)
return value ^ (1 << random(8));
}
uint16_t bitRot(uint16_t value, float chance = 0.5)
{
if (random(BH_BIG_NR) > chance * BH_BIG_NR) return value;
return value ^ (1UL << random(16));
}
uint32_t bitRot(uint32_t value, float chance = 0.5)
{
if (random(BH_BIG_NR) > chance * BH_BIG_NR) return value;
return value ^ (1UL << random(32));
}
uint64_t bitRot(uint64_t value, float chance = 0.5)
{
if (random(BH_BIG_NR) > chance * BH_BIG_NR) return value;
@ -437,6 +472,7 @@ void bitSet64(uint64_t & x, uint8_t n)
else x |= 0x1 << n;
}
void bitClear64(uint64_t & x, uint8_t n)
{
if (n > 47) x &= ~(0x1000000000000 << (n - 48));
@ -446,6 +482,7 @@ void bitClear64(uint64_t & x, uint8_t n)
else x &= ~(0x1 << n);
}
void bitToggle64(uint64_t & x, uint8_t n)
{
if (n > 47) x ^= (0x1000000000000 << (n - 48));
@ -455,6 +492,7 @@ void bitToggle64(uint64_t & x, uint8_t n)
else x ^= (0x1 << n);
}
#elif defined(ESP32) || defined(ESP8266)
void bitSet64(uint64_t & x, uint8_t n)
@ -463,18 +501,21 @@ void bitSet64(uint64_t & x, uint8_t n)
else x |= 0x1 << n;
}
void bitClear64(uint64_t & x, uint8_t n)
{
if (n > 31) x &= ~(0x100000000 << (n - 32));
else x &= ~(0x1 << n);
}
void bitToggle64(uint64_t & x, uint8_t n)
{
if (n > 31) x ^= (0x100000000 << (n - 32));
else x ^= (0x1 << n);
}
#else
void bitSet64(uint64_t & x, uint8_t bit)
@ -482,16 +523,19 @@ void bitSet64(uint64_t & x, uint8_t bit)
x |= 1ULL << bit;
}
void bitClear64(uint64_t & x, uint8_t bit)
{
x &= ~(1ULL << bit);
}
void bitToggle64(uint64_t & x, uint8_t bit)
{
x ^= 1ULL << bit;
}
#endif
@ -500,6 +544,7 @@ uint8_t bitRead64(uint64_t & x, uint8_t bit)
return x & (1ULL << bit);
}
void bitWrite64(uint64_t & x, uint8_t bit, uint8_t value)
{
if (value) bitSet64(x, bit);
@ -524,6 +569,7 @@ uint8_t bitsNeededRef(uint64_t x)
return n;
}
// workers
uint8_t bitsNeeded(uint8_t x)
{
@ -536,6 +582,7 @@ uint8_t bitsNeeded(uint8_t x)
return n;
}
uint8_t bitsNeeded(uint16_t x)
{
uint8_t y = x >> 8;
@ -543,6 +590,7 @@ uint8_t bitsNeeded(uint16_t x)
return bitsNeeded((uint8_t)x);
}
uint8_t bitsNeeded(uint32_t x)
{
uint16_t y = x >> 16;
@ -550,6 +598,7 @@ uint8_t bitsNeeded(uint32_t x)
return bitsNeeded((uint16_t)x);
}
uint8_t bitsNeeded(uint64_t x)
{
uint32_t y = x >> 32;
@ -567,3 +616,4 @@ uint8_t bitsNeeded(uint64_t x)
// -- END OF FILE --

View File

@ -6,10 +6,12 @@
// DATE: 2020-07-28
// URL: https://github.com/RobTillaart/bitHelpers
#include "bitHelpers.h"
uint32_t start, stop;
void setup()
{
Serial.begin(115200);
@ -31,10 +33,12 @@ void setup()
Serial.println("\nDone...");
}
void loop()
{
}
//////////////////////////////////
void test_bitCount1_32_bits_variations()
@ -110,6 +114,7 @@ void test_bitCount1_32_bits_variations()
Serial.println();
}
void test_bitCount2()
{
Serial.println();
@ -176,6 +181,7 @@ void test_bitCount2()
Serial.println();
}
void test_bitReverse()
{
Serial.println();
@ -213,6 +219,7 @@ void test_bitReverse()
Serial.println();
}
void test_nybbleReverse()
{
Serial.println();
@ -250,6 +257,7 @@ void test_nybbleReverse()
Serial.println();
}
void test_byteReverse()
{
Serial.println();
@ -281,6 +289,7 @@ void test_byteReverse()
Serial.println();
}
void test_swap()
{
Serial.println();
@ -402,6 +411,7 @@ void test_bitRotateRight()
Serial.println();
}
void test_bitRot()
{
Serial.println();
@ -432,3 +442,4 @@ void test_bitRot()
// -- END OF FILE --

View File

@ -6,12 +6,14 @@
// DATE: 2020-08-10
// URL: https://github.com/RobTillaart/bitHelpers
#include "bitHelpers.h"
#include "printHelpers.h"
uint32_t start, stop, d1, d2;
uint64_t x = 0;
void setup()
{
Serial.begin(115200);
@ -70,8 +72,11 @@ void setup()
Serial.println();
}
void loop()
{
}
// -- END OF FILE --

View File

@ -78,6 +78,7 @@ char * print64(int64_t n, uint8_t base = 10)
return buf;
}
char * print64(uint64_t n, uint8_t base = 10)
{
char * buf = __printbuffer;
@ -116,6 +117,7 @@ char * print64(uint64_t n, uint8_t base = 10)
return buf;
}
////////////////////////////////////////////////////////////
//
// SCIENTIFIC NOTATIION
@ -223,16 +225,19 @@ char * scieng(double number, uint8_t digits, uint8_t em)
return buf;
}
char * eng(double number, uint8_t digits)
{
return scieng(number, digits, 3);
}
char * sci(double number, uint8_t digits)
{
return scieng(number, digits, 1);
}
void sci(Stream &str, double f, uint8_t digits)
{
str.print(sci(f, digits));

View File

@ -1,6 +1,7 @@
# Syntax Coloring Map For bitHelpers
# Syntax Colouring Map For bitHelpers
# Data types (KEYWORD1)
# Datatypes (KEYWORD1)
# Methods and Functions (KEYWORD2)
bitCountReference KEYWORD2
@ -27,5 +28,8 @@ bitWrite64 KEYWORD2
bitsNeededRef KEYWORD2
bitsNeeded KEYWORD2
# Constants (LITERAL1)
# Constants (LITERAL1)
BITHELPER_LIB_VERSION LITERAL1

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/bitHelpers.git"
},
"version": "0.1.3",
"version": "0.1.4",
"license": "MIT",
"frameworks": "*",
"platforms": "*"

View File

@ -1,5 +1,5 @@
name=bitHelpers
version=0.1.3
version=0.1.4
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library with functions on bit level

View File

@ -28,10 +28,12 @@ unittest_setup()
{
}
unittest_teardown()
{
}
unittest(test_bitCount)
{
assertEqual(32, bitCountReference((uint32_t)0xFFFFFFFF));
@ -42,6 +44,7 @@ unittest(test_bitCount)
assertEqual(32, bitCount((uint32_t)0xFFFFFFFF));
}
unittest(test_bitReverse)
{
assertEqual(0xAA, bitReverse((uint8_t)0x55));
@ -50,6 +53,7 @@ unittest(test_bitReverse)
assertEqual(0xFFFFAAAAFFFFAAAA, bitReverse((uint64_t)0x5555FFFF5555FFFF));
}
unittest(test_nybbleReverse)
{
assertEqual(0xAB, nybbleReverse((uint8_t)0xBA));
@ -58,6 +62,7 @@ unittest(test_nybbleReverse)
assertEqual(0x0123456789ABCDEF, nybbleReverse((uint64_t)0xFEDCBA9876543210));
}
unittest(test_byteReverse)
{
assertEqual(0xABCD, byteReverse((uint16_t)0xCDAB));
@ -65,6 +70,7 @@ unittest(test_byteReverse)
assertEqual(0x0123456789ABCDEF, byteReverse((uint64_t)0xEFCDAB8967452301));
}
unittest(test_swap)
{
assertEqual(0x15, swap((uint8_t)0x51));
@ -73,6 +79,7 @@ unittest(test_swap)
assertEqual(0x0123456789ABCDEF, swap((uint64_t)0x89ABCDEF01234567));
}
unittest(test_bitRotateLeft)
{
assertEqual(0x8A, bitRotateLeft((uint8_t)0x51, 3));
@ -81,6 +88,7 @@ unittest(test_bitRotateLeft)
assertEqual(0x456789ABCDEF0123, bitRotateLeft((uint64_t)0x89ABCDEF01234567, 48));
}
unittest(test_bitRotateRight)
{
assertEqual(0x2A, bitRotateRight((uint8_t)0x51, 3));
@ -89,6 +97,7 @@ unittest(test_bitRotateRight)
assertEqual(0x0123456789ABCDEF, bitRotateRight((uint64_t)0x89ABCDEF01234567, 32));
}
unittest(test_bitFlip)
{
assertEqual(0x59, bitFlip((uint8_t)0x51, 3));
@ -97,6 +106,7 @@ unittest(test_bitFlip)
assertEqual(0x89ABC5EF01234567, bitFlip((uint64_t)0x89ABCDEF01234567, 43));
}
unittest(test_bitsNeeded)
{
assertEqual(15, bitsNeededRef(0x5167));