GY-63_MS5611/libraries/bitHelpers/bitHelpers.h

218 lines
4.4 KiB
C
Raw Normal View History

2021-01-29 06:31:58 -05:00
#pragma once
//
// FILE: bitHelpers.h
// AUTHOR: Rob Tillaart
2022-10-29 09:51:15 -04:00
// VERSION: 0.1.9
2021-01-29 06:31:58 -05:00
// DATE: 2015-11-07
// PURPOSE: Arduino library with functions on bit level
// URL: https://github.com/RobTillaart/bitHelpers
//
2022-04-12 09:50:42 -04:00
// HISTORY: See CHANGELOG.md
2021-08-09 15:39:09 -04:00
2021-01-29 06:31:58 -05:00
#include "Arduino.h"
2022-10-29 09:51:15 -04:00
#define BITHELPER_LIB_VERSION (F("0.1.9"))
2022-04-12 09:50:42 -04:00
// used by bitRot()
// power of 2 gives better uniform distribution in the last bits
#define BH_BIG_NR (1073741824)
2021-01-29 06:31:58 -05:00
2021-08-09 15:39:09 -04:00
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////
//
2022-10-29 09:51:15 -04:00
// BIT COUNT TEST
2021-01-29 06:31:58 -05:00
//
2022-04-15 08:46:58 -04:00
uint8_t bitCountReference(uint32_t value);
uint8_t bitCountKR(uint32_t value);
uint8_t bitCountArray(uint32_t value);
uint8_t bitCountF1(uint32_t value);
uint8_t bitCountF2(uint32_t value);
2021-01-29 06:31:58 -05:00
2021-10-19 10:59:51 -04:00
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////
//
2022-10-29 09:51:15 -04:00
// BIT COUNT
2021-01-29 06:31:58 -05:00
//
2022-04-15 08:46:58 -04:00
uint8_t bitCount(uint8_t value);
uint8_t bitCount(uint16_t value);
uint8_t bitCount(uint32_t value);
uint8_t bitCount(uint64_t value);
2021-01-29 06:31:58 -05:00
2021-08-09 15:39:09 -04:00
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////
//
2022-10-29 09:51:15 -04:00
// BIT REVERSE
2021-01-29 06:31:58 -05:00
//
2022-04-15 08:46:58 -04:00
uint8_t bitReverse(uint8_t value);
uint16_t bitReverse(uint16_t value);
uint32_t bitReverse(uint32_t value);
uint64_t bitReverse(uint64_t value);
2021-01-29 06:31:58 -05:00
2021-08-09 15:39:09 -04:00
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////
//
2022-10-29 09:51:15 -04:00
// NYBBLE REVERSE
2021-01-29 06:31:58 -05:00
//
2022-04-15 08:46:58 -04:00
uint8_t nybbleReverse(uint8_t value);
uint16_t nybbleReverse(uint16_t value);
uint32_t nybbleReverse(uint32_t value);
uint64_t nybbleReverse(uint64_t value);
2021-01-29 06:31:58 -05:00
2021-10-19 10:59:51 -04:00
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////
//
2022-10-29 09:51:15 -04:00
// BYTE REVERSE
2021-01-29 06:31:58 -05:00
//
2022-04-15 08:46:58 -04:00
uint16_t byteReverse(uint16_t value);
2021-01-29 06:31:58 -05:00
2022-04-15 08:46:58 -04:00
uint32_t byteReverse(uint32_t value);
2021-10-19 10:59:51 -04:00
2022-04-15 08:46:58 -04:00
uint64_t byteReverse(uint64_t value);
2021-01-29 06:31:58 -05:00
2021-08-09 15:39:09 -04:00
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////
//
2022-10-29 09:51:15 -04:00
// WORD REVERSE
2021-01-29 06:31:58 -05:00
//
2022-04-15 08:46:58 -04:00
uint32_t wordReverse(uint32_t value);
2021-01-29 06:31:58 -05:00
2022-04-15 08:46:58 -04:00
uint64_t wordReverse(uint64_t value);
2021-01-29 06:31:58 -05:00
2021-08-09 15:39:09 -04:00
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////
//
2022-10-29 09:51:15 -04:00
// SWAP HI LO
2021-01-29 06:31:58 -05:00
//
2022-04-15 08:46:58 -04:00
uint8_t swap(uint8_t value);
2021-10-19 10:59:51 -04:00
2022-04-15 08:46:58 -04:00
uint16_t swap(uint16_t value);
2021-01-29 06:31:58 -05:00
2022-04-15 08:46:58 -04:00
uint32_t swap(uint32_t value);
2021-10-19 10:59:51 -04:00
2022-04-15 08:46:58 -04:00
uint64_t swap(uint64_t value);
2021-01-29 06:31:58 -05:00
2021-08-09 15:39:09 -04:00
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////
//
2022-10-29 09:51:15 -04:00
// BIT ROTATE LEFT
2021-01-29 06:31:58 -05:00
//
2022-04-15 08:46:58 -04:00
uint8_t bitRotateLeft(uint8_t value, uint8_t pos);
2021-01-29 06:31:58 -05:00
2022-04-15 08:46:58 -04:00
uint16_t bitRotateLeft(uint16_t value, uint8_t pos);
2021-10-19 10:59:51 -04:00
2022-04-15 08:46:58 -04:00
uint32_t bitRotateLeft(uint32_t value, uint8_t pos);
2021-01-29 06:31:58 -05:00
2022-04-15 08:46:58 -04:00
uint64_t bitRotateLeft(uint64_t value, uint8_t pos);
2021-01-29 06:31:58 -05:00
2021-08-09 15:39:09 -04:00
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////
//
2022-10-29 09:51:15 -04:00
// BIT ROTATE RIGHT
2021-01-29 06:31:58 -05:00
//
2022-04-15 08:46:58 -04:00
uint8_t bitRotateRight(uint8_t value, uint8_t pos);
2021-10-19 10:59:51 -04:00
2022-04-15 08:46:58 -04:00
uint16_t bitRotateRight(uint16_t value, uint8_t pos);
2021-01-29 06:31:58 -05:00
2022-04-15 08:46:58 -04:00
uint32_t bitRotateRight(uint32_t value, uint8_t pos);
2021-10-19 10:59:51 -04:00
2022-04-15 08:46:58 -04:00
uint64_t bitRotateRight(uint64_t value, uint8_t pos);
2021-01-29 06:31:58 -05:00
2021-08-09 15:39:09 -04:00
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////////
//
2022-10-29 09:51:15 -04:00
// BIT FLIP
2021-01-29 06:31:58 -05:00
//
2022-04-15 08:46:58 -04:00
uint8_t bitFlip(uint8_t value, uint8_t pos);
2021-01-29 06:31:58 -05:00
2022-04-15 08:46:58 -04:00
uint16_t bitFlip(uint16_t value, uint8_t pos);
2021-10-19 10:59:51 -04:00
2022-04-15 08:46:58 -04:00
uint32_t bitFlip(uint32_t value, uint8_t pos);
2021-01-29 06:31:58 -05:00
2022-04-15 08:46:58 -04:00
uint64_t bitFlip(uint64_t value, uint8_t pos);
2021-01-29 06:31:58 -05:00
2021-08-09 15:39:09 -04:00
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////////
//
2022-10-29 09:51:15 -04:00
// BIT ROT
2021-01-29 06:31:58 -05:00
//
2022-04-15 08:46:58 -04:00
uint8_t bitRotRef(uint8_t value, float chance = 0.5);
uint16_t bitRotRef(uint16_t value, float chance = 0.5);
uint32_t bitRotRef(uint32_t value, float chance = 0.5);
2022-04-12 09:50:42 -04:00
2022-04-15 08:46:58 -04:00
uint64_t bitRotRef(uint64_t value, float chance = 0.5);
2022-04-12 09:50:42 -04:00
2022-04-15 08:46:58 -04:00
uint8_t bitRot(uint8_t value, float chance = 0.5, uint16_t times = 1);
uint16_t bitRot(uint16_t value, float chance = 0.5, uint16_t times = 1);
uint32_t bitRot(uint32_t value, float chance = 0.5, uint16_t times = 1);
uint64_t bitRot(uint64_t value, float chance = 0.5, uint16_t times = 1);
2022-04-12 09:50:42 -04:00
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////////
//
2022-10-29 09:51:15 -04:00
// BIT-SET64 -CLEAR64 -TOGGLE64 -READ64 -WRITE64
2021-01-29 06:31:58 -05:00
//
2022-10-29 09:51:15 -04:00
// MACROS
// only 64 bit data types are handled 64 bit.
2022-04-12 09:50:42 -04:00
#define mbitSet64(value, bit) ((value) |= (sizeof(value)<5?1UL:1ULL) <<(bit))
2021-01-29 06:31:58 -05:00
#define mbitClear64(value, bit) ((value) &= ~(sizeof(value)<5?1UL:1ULL) <<(bit))
2022-04-12 09:50:42 -04:00
#define mbitToggle64(value, bit) ((value) ^= (sizeof(value)<5?1UL:1ULL) <<(bit))
2021-01-29 06:31:58 -05:00
2022-04-12 09:50:42 -04:00
#define mbitRead64(value, bit) ( ((value) & ((sizeof(value)<5?1UL:1ULL) <<(bit))) ? 1 : 0)
2021-01-29 06:31:58 -05:00
#define mbitWrite64(value, bit, bitvalue) (bitvalue ? mbitSet64(value, bit) : mbitClear64(value, bit))
2021-08-09 15:39:09 -04:00
2022-10-29 09:51:15 -04:00
// FUNCTIONS
2021-01-29 06:31:58 -05:00
2022-04-15 08:46:58 -04:00
void bitSet64(uint64_t & x, uint8_t n);
2021-10-19 10:59:51 -04:00
2022-04-15 08:46:58 -04:00
void bitClear64(uint64_t & x, uint8_t n);
2021-01-29 06:31:58 -05:00
2022-04-15 08:46:58 -04:00
void bitToggle64(uint64_t & x, uint8_t n);
2021-10-19 10:59:51 -04:00
2022-04-15 08:46:58 -04:00
uint8_t bitRead64(uint64_t & x, uint8_t bit);
2021-01-29 06:31:58 -05:00
2022-04-15 08:46:58 -04:00
void bitWrite64(uint64_t & x, uint8_t bit, uint8_t value);
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////////
//
2022-10-29 09:51:15 -04:00
// BITS NEEDED
2021-01-29 06:31:58 -05:00
//
2022-10-29 09:51:15 -04:00
// reference
2022-04-15 08:46:58 -04:00
uint8_t bitsNeededRef(uint64_t x);
2021-10-19 10:59:51 -04:00
2022-10-29 09:51:15 -04:00
// workers
2022-04-15 08:46:58 -04:00
uint8_t bitsNeeded(uint8_t x);
2021-08-09 15:39:09 -04:00
2022-04-15 08:46:58 -04:00
uint8_t bitsNeeded(uint16_t x);
2021-01-29 06:31:58 -05:00
2022-04-15 08:46:58 -04:00
uint8_t bitsNeeded(uint32_t x);
2021-01-29 06:31:58 -05:00
2022-04-15 08:46:58 -04:00
uint8_t bitsNeeded(uint64_t x);
2021-01-29 06:31:58 -05:00
// -- END OF FILE --
2021-10-19 10:59:51 -04:00