-
Notifications
You must be signed in to change notification settings - Fork 0
efficient_functions
AN QIULIANG edited this page Nov 14, 2016
·
1 revision
void fast_to_upper(const char *src, char *dest, size_t len) { size_t i = 0; const uint64_t *src_data = (const uint64_t*) src; uint64_t *dest_data = (uint64_t*) dest; size_t blocks = len / sizeof(uint64_t); uint64_t src8, a, b; unsigned char src_char, a_char, b_char; for (; i < blocks; i++) { src8 = src_data[i] & 0x7f7f7f7f7f7f7f7f; a = src8 + 0x0505050505050505; b = src8 + 0x7f7f7f7f7f7f7f7f; a = a & b; a = a & (a >> 1) & 0x2020202020202020; dest_data[i] = src8 - a; } i *= sizeof(uint64_t); for (; i < len; i++) { src_char = src[i] & 0x7f; a_char = src_char + 0x05; b_char = src_char + 0x7f; a_char = a_char & b_char; a_char = a_char & (a_char >> 1) & 0x20; dest[i] = src_char - a_char; } dest[i] = 0; }
int abs(int x) { int s[] = {-x, x}; return s[x > 0]; } int abs(int x) { return (x ^ (x >> 31)) - (x >> 31); }
void swap(int& x, int& y) { x = x ^ y; y = x ^ y; x = x ^ y; } void swap(int& x, int& y) { x ^= y ^= x ^= y; }
bool ispow2(int n) { return (n & -n) == n; }