GY-63_MS5611/libraries/Fletcher/Fletcher32.cpp

82 lines
1.5 KiB
C++
Raw Normal View History

2022-01-27 04:11:25 -05:00
//
// FILE: Fletcher32.cpp
2022-09-13 10:31:43 -04:00
// AUTHOR: Rob Tillaart, Daniel Mohr
2022-01-27 04:11:25 -05:00
// PURPOSE: Arduino class for Fletcher32
// URL: https://github.com/RobTillaart/Fletcher
#include "Fletcher32.h"
2022-09-13 10:31:43 -04:00
// UINT16_MAX = 65535UL = ((((uint32_t) 1) << 16) - 1)
#define FLETCHER_32 UINT16_MAX
2022-01-27 04:11:25 -05:00
Fletcher32::Fletcher32()
{
begin();
}
void Fletcher32::begin(uint16_t s1, uint16_t s2)
{
2022-09-13 10:31:43 -04:00
_s1 = (s1 == FLETCHER_32) ? 0 : s1;
_s2 = (s2 == FLETCHER_32) ? 0 : s2;
2022-01-27 04:11:25 -05:00
_count = 0;
}
void Fletcher32::add(uint16_t value)
{
_count++;
2022-09-13 10:31:43 -04:00
#ifdef ARDUINO_ARCH_AVR
unsigned int t = _s1;
// Serial.println("__builtin_uadd_overflow");
if (__builtin_uadd_overflow(t, value, &t)) {
t++;
}
_s1 = t;
t = _s2;
if (__builtin_uadd_overflow(t, _s1, &t)) {
t++;
}
_s2 = t;
#elif defined(ARDUINO_ARCH_SAMD) || defined(ESP32) || defined(ESP8266)
2022-01-27 04:11:25 -05:00
_s1 += value;
2022-09-13 10:31:43 -04:00
_s1 = (_s1 & FLETCHER_32) + (_s1 >> 16);
_s2 += _s1;
_s2 = (_s2 & FLETCHER_32) + (_s2 >> 16);
2022-09-08 07:31:39 -04:00
#else
2022-09-13 10:31:43 -04:00
_s1 += value;
2022-09-08 07:31:39 -04:00
if (_s1 >= FLETCHER_32) _s1 -= FLETCHER_32;
2022-01-27 04:11:25 -05:00
_s2 += _s1;
2022-09-08 07:31:39 -04:00
if (_s2 >= FLETCHER_32) _s2 -= FLETCHER_32;
#endif
2022-01-27 04:11:25 -05:00
}
// NOTE: padding with zero's error
//
void Fletcher32::add(const uint16_t * array, uint16_t length)
{
while (length--)
{
add(*array++);
}
}
2022-09-13 10:31:43 -04:00
uint32_t Fletcher32::getFletcher()
{
if (_s1 >= FLETCHER_32) _s1 -= FLETCHER_32;
if (_s2 >= FLETCHER_32) _s2 -= FLETCHER_32;
return (((uint32_t)_s2) << 16) | _s1;
};
uint32_t Fletcher32::count()
{
return _count;
};
2022-01-27 04:11:25 -05:00
// -- END OF FILE --