-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.h
68 lines (55 loc) · 1.62 KB
/
utility.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
#include <limits.h>
#include <stdint.h>
#define countof(x) (sizeof(x) / sizeof(*x))
#define LEAP_YEAR(Y) ( (Y>0) && !(Y%4) && ( (Y%100) || !(Y%400) )) // from time-lib
enum class DST {
UNKNOWN,
OFF,
ON,
TRANS
};
static inline void xchg(int &a, int &b)
{
int x = a;
a = b;
b = x;
}
static inline uint16_t rotl16(uint16_t n, unsigned int c)
{
const unsigned int mask = (CHAR_BIT*sizeof(n) - 1);
c &= mask;
return (n<<c) | (n>>( (-c)&mask ));
}
static inline uint16_t rotr16(uint16_t n, unsigned int c)
{
const unsigned int mask = (CHAR_BIT*sizeof(n) - 1);
c &= mask;
return (n>>c) | (n<<( (-c)&mask ));
}
static inline uint32_t rotr32(uint32_t n, unsigned int c)
{
const unsigned int mask = (CHAR_BIT*sizeof(n) - 1);
c &= mask;
return (n>>c) | (n<<( (-c)&mask ));
}
uint8_t crc8itu_update(uint8_t val, uint8_t data);
uint8_t crc8itu(const void * data, size_t size);
uint8_t checksum(const void * data, size_t size);
uint8_t decode6to8(uint8_t code);
uint8_t bcdPack(uint8_t x);
uint8_t bcdUnpack(uint8_t x);
void nibble2hex(uint8_t value, char *str);
void byte2hex(uint8_t value, char *str);
void word2hex(uint16_t value, char *str);
// WARNING: this does not terminate string with '\0'
void value2str(int value, char *str);
// calculates clock error in PPM from actual ambient temperature
int clock_error(int T);
// calculates precise day of week
int dayOfWeek(uint8_t year, uint8_t month, uint8_t day);
int getMonthDays(uint8_t month, uint8_t year);
DST isDaylightSavingPeriod(uint8_t y, uint8_t m, uint8_t d, uint8_t h);
// lightweight linear pseudo-RNG
int getRand();
void seedRand(int seed);