-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.h
52 lines (42 loc) · 1.23 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
#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
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 bcdPack(uint8_t x);
uint8_t bcdUnpack(uint8_t x);
void nibble2hex(uint8_t value, char *str);
void byte2hex(uint8_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);
// lightweight linear pseudo-RNG
int getRand();
void seedRand(int seed);