-
Notifications
You must be signed in to change notification settings - Fork 10
/
mem.cc
88 lines (67 loc) · 2.12 KB
/
mem.cc
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//---------------------------------------------------------------------------
#include "mem.h"
#include <assert.h>
//---------------------------------------------------------------------------
bool AlignedMem<1>::ConstantTimeEq(const void *p, const void *q,
size_t length) {
void *pEnd = (char *)p + length;
const uint8_t *bp = (const uint8_t *)p;
const uint8_t *bq = (const uint8_t *)q;
uint8_t delta = 0;
while (bp < pEnd) {
delta |= *bp++ ^ *bq++;
}
return delta == 0;
}
//---------------------------------------------------------------------------
void AlignedMem<sizeof(size_t)>::Clear(void *p, size_t length) {
assert(length % sizeof(size_t) == 0);
void *pEnd = (char *)p + length;
size_t *sp = (size_t *)p;
while (sp < pEnd) {
*sp++ = 0;
}
}
bool AlignedMem<sizeof(size_t)>::ConstantTimeEq(const void *p, const void *q,
size_t length) {
assert(length % sizeof(size_t) == 0);
void *pEnd = (char *)p + length;
const size_t *sp = (const size_t *)p;
const size_t *sq = (const size_t *)q;
size_t delta = 0;
while (sp < pEnd) {
delta |= *sp++ ^ *sq++;
}
return delta == 0;
}
void AlignedMem<sizeof(size_t)>::Copy(void *d, const void *s, size_t length) {
assert(length % sizeof(size_t) == 0);
void *sEnd = (char *)s + length;
const size_t *ss = (const size_t *)s;
size_t *sd = (size_t *)d;
while (ss < sEnd) {
*sd++ = *ss++;
}
}
bool AlignedMem<sizeof(size_t)>::Eq(const void *p, const void *q,
size_t length) {
assert(length % sizeof(size_t) == 0);
void *pEnd = (char *)p + length;
const size_t *sp = (const size_t *)p;
const size_t *sq = (const size_t *)q;
while (sp < pEnd) {
if (*sp++ != *sq++) {
return false;
}
}
return true;
}
void AlignedMem<sizeof(size_t)>::Fill(void *p, size_t length) {
assert(length % sizeof(size_t) == 0);
void *pEnd = (char *)p + length;
size_t *sp = (size_t *)p;
while (sp < pEnd) {
*sp++ = (size_t)-1;
}
}
//---------------------------------------------------------------------------