-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmisc.c
More file actions
88 lines (77 loc) · 2.21 KB
/
misc.c
File metadata and controls
88 lines (77 loc) · 2.21 KB
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 "nocrypto.h"
#define u_long_s sizeof (unsigned long)
static inline void xor_into (uint8_t *src, uint8_t *dst, size_t n) {
#if defined (__nc_SSE2__)
while (n >= 16) {
_mm_storeu_si128 (
(__m128i*) dst,
_mm_xor_si128 (
_mm_loadu_si128 ((__m128i*) src),
_mm_loadu_si128 ((__m128i*) dst)));
src += 16;
dst += 16;
n -= 16;
}
#endif
while (n >= u_long_s) {
*((u_long *) dst) ^= *((u_long *) src);
src += u_long_s;
dst += u_long_s;
n -= u_long_s;
}
while (n-- > 0) {
*dst = *(src ++) ^ *dst;
dst++;
}
}
static inline void nc_count_8_be (uint64_t *init, uint64_t *dst, size_t blocks) {
uint64_t qw = be64_to_cpu (*init);
while (blocks --) {
*dst = cpu_to_be64(qw);
++qw;
++dst;
}
}
static inline void nc_count_16_be (uint64_t *init, uint64_t *dst, size_t blocks) {
uint64_t qw1 = be64_to_cpu (init[0]),
qw2 = be64_to_cpu (init[1]);
while (blocks --) {
dst[0] = cpu_to_be64 (qw1);
dst[1] = cpu_to_be64 (qw2);
qw1 += ((++qw2) == 0);
dst += 2;
}
}
static inline void nc_add_16_be (uint64_t *init, uint64_t n) {
uint64_t h = be64_to_cpu (init[0]);
uint64_t l = be64_to_cpu (init[1]);
uint64_t nl = l + n; /* let it wrap */
if (nl < l)
h++;
init[0] = cpu_to_be64 (h);
init[1] = cpu_to_be64 (nl);
}
CAMLprim value
caml_nc_xor_into (value b1, value off1, value b2, value off2, value n) {
xor_into (_ba_uint8_off (b1, off1), _ba_uint8_off (b2, off2), Int_val (n));
return Val_unit;
}
CAMLprim value
caml_nc_count_8_be (value init, value off1, value dst, value off2, value blocks) {
nc_count_8_be ( (uint64_t *) _ba_uint8_off (init, off1),
(uint64_t *) _ba_uint8_off (dst, off2),
Long_val (blocks) );
return Val_unit;
}
CAMLprim value
caml_nc_count_16_be (value init, value off1, value dst, value off2, value blocks) {
nc_count_16_be ( (uint64_t *) _ba_uint8_off (init, off1),
(uint64_t *) _ba_uint8_off (dst, off2),
Long_val (blocks) );
return Val_unit;
}
CAMLprim value
caml_nc_add_16_be (value init, value off, value n) {
nc_add_16_be ( (uint64_t *) _ba_uint8_off (init, off), Int64_val(n));
return Val_unit;
}