-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopt.c
More file actions
176 lines (148 loc) · 3.71 KB
/
opt.c
File metadata and controls
176 lines (148 loc) · 3.71 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "opt.h"
void fast_memset(void* loc, size_t val, size_t len){
size_t len_8 = (len>>3);
uint64_t* loc_ul = (uint64_t*)loc;
int32_t i;
for(i=0;i<len_8;i++){
loc_ul[i] = val;
}
i <<= 3;
//if mem for some reason is not 8 byte aligned
unsigned char* loc_ch = (unsigned char*)loc;
char* val_bytes = (char*)(&val);
for(;i<len;i++){
loc_ch[i] = val_bytes[i&0x7];
}
}
void fast_bytecopy(void* dst, void* src, size_t len){
uint64_t* dst_ul = (uint64_t*)dst;
uint64_t* src_ul = (uint64_t*)src;
size_t len_8 = len>>3;
int32_t i=0;
for(i=0;i<len_8;i++){
dst_ul[i]=src_ul[i];
}
i <<= 3;
unsigned char* dst_ch = (unsigned char*)dst;
unsigned char* src_ch = (unsigned char*)src;
for(;i<len;i++){
dst_ch[i]=src_ch[i];
}
}
int32_t fast_bytecmp_u(const void* a, const void* b, size_t len){
uint64_t* a_ul = (uint64_t*)a;
uint64_t* b_ul = (uint64_t*)b;
int32_t i;
size_t len_8 = len>>3;
for(i=0;i<len_8;i++){
if(a_ul[i]!=b_ul[i]){
return a_ul[i] > b_ul[i] ? 1 : -1;
}
}
i <<= 3;
unsigned char* a_ch = (unsigned char*)a;
unsigned char* b_ch = (unsigned char*)b;
for(;i<len;i++){
if(a_ch[i]!=b_ch[i]){
return a_ch[i] > b_ch[i] ? 1 : -1;
}
}
return 0;
}
int32_t fast_bytecmp(const void* a, const void* b, size_t len){
int64_t* a_ul = (int64_t*)a;
int64_t* b_ul = (int64_t*)b;
int32_t i;
size_t len_8 = len>>3;
for(i=0;i<len_8;i++){
if(a_ul[i]!=b_ul[i]){
return a_ul[i] > b_ul[i] ? 1 : -1;
}
}
i <<= 3;
char* a_ch = (char*)a;
char* b_ch = (char*)b;
for(;i<len;i++){
if(a_ch[i]!=b_ch[i]){
return a_ch[i] > b_ch[i] ? 1 : -1;
}
}
return 0;
}
#ifdef x86_64
int32_t ff1_asm(int32_t x){
int32_t loc;
__asm__("bsf %1, %0" : "=r" (loc) : "rm" (x));
return loc;
}
int32_t ff0_asm(int32_t x){
int32_t loc;
__asm__("bsf %1, %0" : "=r" (loc) : "rm" ((~x)));
return loc;
}
int32_t fl1_asm(int32_t x){
int32_t loc;
__asm__("bsr %1, %0" : "=r" (loc) : "rm" (x));
return loc;
}
int32_t fl0_asm(int32_t x){
int32_t loc;
__asm__("bsr %1, %0" : "=r" (loc) : "rm" ((~x)));
return loc;
}
int32_t ff1_64_asm(uint64_t x){
int32_t bot_half = x, top_half = (x >> 32);
return bot_half ? ff1_asm(bot_half) : (32 + ff1_asm(top_half));
}
int32_t ff0_64_asm(uint64_t x){
x = ~x;
int32_t bot_half = x, top_half = (x >> 32);
return bot_half ? ff1_asm(bot_half) : (32 + ff1_asm(top_half));
}
int32_t fl1_64_asm(uint64_t x){
int32_t bot_half = x, top_half = (x >> 32);
return top_half ? fl1_asm(top_half) : (32 + fl1_asm(bot_half));
}
int32_t fl0_64_asm(uint64_t x){
x = ~x;
int32_t bot_half = x, top_half = (x >> 32);
return top_half ? fl1_asm(top_half) : (32 + fl1_asm(bot_half));
}
#endif
#ifdef armv7l
int32_t ff1_asm(int32_t x){
return fl1_asm(x & (~x + 1));
}
int32_t ff0_asm(int32_t x){
return ff1_asm(~x);
}
int32_t fl1_asm(int32_t x){
uint32_t s, t, n = x;
t = (n > 0xffff) << 4; n >>= t;
s = (n > 0xff ) << 3; n >>= s, t |= s;
s = (n > 0xf ) << 2; n >>= s, t |= s;
s = (n > 0x3 ) << 1; n >>= s, t |= s;
return (t | (n >> 1));
}
int32_t fl0_asm(int32_t x){
return fl1_asm(~x);
}
#endif
uint32_t ulog2(uint32_t n){
uint32_t s, t;
t = (n > 0xffff) << 4; n >>= t;
s = (n > 0xff ) << 3; n >>= s, t |= s;
s = (n > 0xf ) << 2; n >>= s, t |= s;
s = (n > 0x3 ) << 1; n >>= s, t |= s;
return (t | (n >> 1));
}
uint32_t bitcount(uint64_t v){
uint64_t c;
c = v - ((v >> 1) & 0x5555555555555555UL);
c = ((c >> 2) & 0x3333333333333333UL) + (c & 0x3333333333333333UL);
c = ((c >> 4) + c) & 0x0F0F0F0F0F0F0F0FUL;
c = ((c >> 8) + c) & 0x00FF00FF00FF00FFUL;
c = ((c >> 16) + c) & 0x0000FFFF0000FFFFUL;
c = ((c >> 32) + c) & 0x00000000FFFFFFFFUL;
return c;
}