This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
forked from powturbo/Turbo-Base64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turbob64_.h
145 lines (121 loc) · 7.04 KB
/
turbob64_.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
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
/**
Copyright (C) powturbo 2016-2022
GPL v3 License
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- homepage : https://sites.google.com/site/powturbo/
- github : https://github.com/powturbo
- twitter : https://twitter.com/powturbo
- email : powturbo [_AT_] gmail [_DOT_] com
**/
// Turbo-Base64: internal include
size_t _tb64xdec( const unsigned char *in, size_t inlen, unsigned char *out);
size_t tb64memcpy(const unsigned char *in, size_t inlen, unsigned char *out); // testing only
#define PREFETCH(_ip_,_i_,_rw_) __builtin_prefetch(_ip_+(_i_),_rw_)
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
#define BSWAP32(a) a
#define BSWAP64(a) a
#else
#define BSWAP32(a) bswap32(a)
#define BSWAP64(a) bswap64(a)
#endif
#ifdef NB64CHECK // decoding without checking
#define CHECK0(a)
#define CHECK1(a)
#else // decoding incl. checking
#define CHECK0(a) a
#ifdef B64CHECK // Full check
#define CHECK1(a) a
#else
#define CHECK1(a)
#endif
#endif
//------- Encode: scalar helper macros & functions ----------------------------------------------------------
extern unsigned char tb64lutse[];
#define SU32(_u_) (tb64lutse[(_u_>> 8) & 0x3f] << 24 |\
tb64lutse[(_u_>>14) & 0x3f] << 16 |\
tb64lutse[(_u_>>20) & 0x3f] << 8 |\
tb64lutse[(_u_>>26) & 0x3f])
#define ETAIL()\
unsigned _l = (in+inlen) - ip; \
if(_l == 3) { unsigned _u = ip[0]<<24 | ip[1]<<16 | ip[2]<<8; stou32(op, SU32(_u)); op+=4; }\
else if(_l == 2) { op[0] = tb64lutse[(ip[0]>>2)&0x3f]; op[1] = tb64lutse[(ip[0] & 0x3) << 4 | (ip[1] & 0xf0) >> 4]; op[2] = tb64lutse[(ip[1] & 0xf) << 2]; op[3] = '='; op+=4; }\
else if(_l) { op[0] = tb64lutse[(ip[0]>>2)&0x3f]; op[1] = tb64lutse[(ip[0] & 0x3) << 4], op[2] = '='; op[3] = '='; op+=4; }
extern const unsigned short tb64lute[];
#define XU32(_u_) (tb64lute[(_u_ >> 8) & 0xfff] << 16 |\
tb64lute[ _u_ >> 20])
#define EXTAIL() for(; op < (out+outlen)-4; op += 4, ip += 3) { unsigned _u = BSWAP32(ctou32(ip)); stou32(op, XU32(_u)); } ETAIL()
//------- Decode: scalar helper macros & functions ----------------------------------------------------------
extern const unsigned tb64lutd0[];
extern const unsigned tb64lutd1[];
extern const unsigned tb64lutd2[];
extern const unsigned tb64lutd3[];
#define DU32(_u_) (tb64lutd0[(unsigned char)(_u_ )] |\
tb64lutd1[(unsigned char)(_u_>> 8)] |\
tb64lutd2[(unsigned char)(_u_>> 16)] |\
tb64lutd3[ _u_>> 24 ] )
#define DXTAILC(ip,out,op,_check_) {\
if(ip[3] != '=') { unsigned u = ctou32(ip); u = DU32(u); op[0] = u; op[1] = u>>8; op[2] = u>>16; op+=3; _check_; } /*4->3*/\
else if(ip[2] != '=') { unsigned u = tb64lutd0[ip[0]] | tb64lutd1[ip[1]] | tb64lutd2[ip[2]]; op[0] = u; op[1] = u>>8; op+=2; _check_; } /*3->2*/\
else if(ip[1] != '=') { unsigned u = tb64lutd0[ip[0]] | tb64lutd1[ip[1]]; *op++ = u; _check_; } /*2->1*/\
else { unsigned u = tb64lutd0[ip[0]]; *op++ = u; _check_; } /*1->1*/\
}
#define DXTAIL(ip,out,op) {\
if(ip[3] != '=') { unsigned u = ctou32(ip); u = DU32(u); op[0] = u; op[1] = u>>8; op[2] = u>>16; op+=3;} /*4->3*/\
else if(ip[2] != '=') { uint16_t u = tb64lutd0[ip[0]] | tb64lutd1[ip[1]] | tb64lutd2[ip[2]]; op[0] = u; op[1] = u>>8; op+=2; } /*3->2*/\
else if(ip[1] != '=') { *op++ = tb64lutd0[ip[0]] | tb64lutd1[ip[1]]; } /*2->1*/\
else { *op++ = tb64lutd0[ip[0]]; } /*1->1*/\
}
static ALWAYS_INLINE size_t _tb64xd(const unsigned char *in, size_t inlen, unsigned char *out) {
const unsigned char *ip = in;
unsigned char *op = out;
#ifdef B64CHECK
unsigned cu = 0;
for(; ip < (in+inlen)-4; ip += 4, op += 3) { unsigned u = ctou32(ip); u = DU32(u); stou32(op, u); cu |= u; }
DXTAILC(ip,out,op, cu |= u);
return (cu == -1)?0:(op-out);
#else
for(; ip < (in+inlen)-4; ip += 4, op += 3) { unsigned u = ctou32(ip); u = DU32(u); stou32(op, u); }
DXTAIL(ip,out,op)
return op - out;
#endif
}
//------- SSE helper macros & functions ----------------------------------------------------------
#if defined(__SSSE3__)
#include <tmmintrin.h>
#define MM_PACK8TO6(v, cpv) {\
const __m128i merge_ab_and_bc = _mm_maddubs_epi16(v, _mm_set1_epi32(0x01400140)); /*dec_reshuffle: https://arxiv.org/abs/1704.00605 P.17*/\
v = _mm_madd_epi16(merge_ab_and_bc, _mm_set1_epi32(0x00011000));\
v = _mm_shuffle_epi8(v, cpv);\
}
#define MM_MAP8TO6(iv, shifted, delta_asso, delta_values, ov) { /*map 8-bits ascii to 6-bits bin*/\
shifted = _mm_srli_epi32(iv, 3);\
const __m128i delta_hash = _mm_avg_epu8(_mm_shuffle_epi8(delta_asso, iv), shifted);\
ov = _mm_add_epi8(_mm_shuffle_epi8(delta_values, delta_hash), iv);\
}
#define MM_B64CHK(iv, shifted, check_asso, check_values, vx) {\
const __m128i check_hash = _mm_avg_epu8( _mm_shuffle_epi8(check_asso, iv), shifted);\
const __m128i chk = _mm_adds_epi8(_mm_shuffle_epi8(check_values, check_hash), iv);\
vx = _mm_or_si128(vx, chk);\
}
static ALWAYS_INLINE __m128i mm_map6to8(const __m128i v) {
const __m128i offsets = _mm_set_epi8( 0, 0,-16,-19, -4,-4,-4,-4, -4,-4,-4,-4, -4,-4,71,65);
__m128i vidx = _mm_subs_epu8(v, _mm_set1_epi8(51));
vidx = _mm_sub_epi8(vidx, _mm_cmpgt_epi8(v, _mm_set1_epi8(25)));
return _mm_add_epi8(v, _mm_shuffle_epi8(offsets, vidx));
}
static ALWAYS_INLINE __m128i mm_unpack6to8(__m128i v) {
__m128i va = _mm_mulhi_epu16(_mm_and_si128(v, _mm_set1_epi32(0x0fc0fc00)), _mm_set1_epi32(0x04000040));
__m128i vb = _mm_mullo_epi16(_mm_and_si128(v, _mm_set1_epi32(0x003f03f0)), _mm_set1_epi32(0x01000010));
return _mm_or_si128(va, vb);
}
#endif