7
7
8
8
#pragma once
9
9
10
+ #include < faiss/impl/platform_macros.h>
10
11
#include < cstdint>
11
12
12
13
namespace faiss {
@@ -31,7 +32,11 @@ struct Uint8Reader {
31
32
if (N_ELEMENTS > CPOS + 3 ) {
32
33
const uint32_t code32 = *reinterpret_cast <const uint32_t *>(
33
34
codes + ELEMENT_TO_READ * 4 );
35
+ #ifdef FAISS_BIG_ENDIAN
36
+ return (code32) >> 24 ;
37
+ #else
34
38
return (code32 & 0x000000FF );
39
+ #endif
35
40
} else {
36
41
return codes[CPOS];
37
42
}
@@ -40,7 +45,11 @@ struct Uint8Reader {
40
45
if (N_ELEMENTS > CPOS + 2 ) {
41
46
const uint32_t code32 = *reinterpret_cast <const uint32_t *>(
42
47
codes + ELEMENT_TO_READ * 4 );
48
+ #ifdef FAISS_BIG_ENDIAN
49
+ return (code32 & 0x00FF0000 ) >> 16 ;
50
+ #else
43
51
return (code32 & 0x0000FF00 ) >> 8 ;
52
+ #endif
44
53
} else {
45
54
return codes[CPOS];
46
55
}
@@ -49,7 +58,11 @@ struct Uint8Reader {
49
58
if (N_ELEMENTS > CPOS + 1 ) {
50
59
const uint32_t code32 = *reinterpret_cast <const uint32_t *>(
51
60
codes + ELEMENT_TO_READ * 4 );
61
+ #ifdef FAISS_BIG_ENDIAN
62
+ return (code32 & 0x0000FF00 ) >> 8 ;
63
+ #else
52
64
return (code32 & 0x00FF0000 ) >> 16 ;
65
+ #endif
53
66
} else {
54
67
return codes[CPOS];
55
68
}
@@ -58,7 +71,11 @@ struct Uint8Reader {
58
71
if (N_ELEMENTS > CPOS) {
59
72
const uint32_t code32 = *reinterpret_cast <const uint32_t *>(
60
73
codes + ELEMENT_TO_READ * 4 );
74
+ #ifdef FAISS_BIG_ENDIAN
75
+ return (code32 & 0x000000FF );
76
+ #else
61
77
return (code32) >> 24 ;
78
+ #endif
62
79
} else {
63
80
return codes[CPOS];
64
81
}
@@ -87,40 +104,61 @@ struct Uint10Reader {
87
104
switch (SUB_ELEMENT) {
88
105
case 0 : {
89
106
if (N_ELEMENTS > CPOS + 2 ) {
90
- const uint32_t code32 = *reinterpret_cast <const uint32_t *>(
107
+ uint32_t code32 = *reinterpret_cast <const uint32_t *>(
91
108
codes + ELEMENT_TO_READ * 5 );
109
+ #ifdef FAISS_BIG_ENDIAN
110
+ code32 = Swap4Bytes (code32);
111
+ #endif
92
112
return (code32 & 0b0000001111111111 );
93
113
} else {
94
- const uint16_t code16 = *reinterpret_cast <const uint16_t *>(
114
+ uint16_t code16 = *reinterpret_cast <const uint16_t *>(
95
115
codes + ELEMENT_TO_READ * 5 + 0 );
116
+ #ifdef FAISS_BIG_ENDIAN
117
+ code16 = Swap2Bytes (code16);
118
+ #endif
96
119
return (code16 & 0b0000001111111111 );
97
120
}
98
121
}
99
122
case 1 : {
100
123
if (N_ELEMENTS > CPOS + 1 ) {
101
- const uint32_t code32 = *reinterpret_cast <const uint32_t *>(
124
+ uint32_t code32 = *reinterpret_cast <const uint32_t *>(
102
125
codes + ELEMENT_TO_READ * 5 );
126
+ #ifdef FAISS_BIG_ENDIAN
127
+ code32 = Swap4Bytes (code32);
128
+ #endif
103
129
return (code32 & 0b000011111111110000000000 ) >> 10 ;
104
130
} else {
105
- const uint16_t code16 = *reinterpret_cast <const uint16_t *>(
131
+ uint16_t code16 = *reinterpret_cast <const uint16_t *>(
106
132
codes + ELEMENT_TO_READ * 5 + 1 );
133
+ #ifdef FAISS_BIG_ENDIAN
134
+ code16 = Swap2Bytes (code16);
135
+ #endif
107
136
return (code16 & 0b0000111111111100 ) >> 2 ;
108
137
}
109
138
}
110
139
case 2 : {
111
140
if (N_ELEMENTS > CPOS) {
112
- const uint32_t code32 = *reinterpret_cast <const uint32_t *>(
141
+ uint32_t code32 = *reinterpret_cast <const uint32_t *>(
113
142
codes + ELEMENT_TO_READ * 5 );
143
+ #ifdef FAISS_BIG_ENDIAN
144
+ code32 = Swap4Bytes (code32);
145
+ #endif
114
146
return (code32 & 0b00111111111100000000000000000000 ) >> 20 ;
115
147
} else {
116
- const uint16_t code16 = *reinterpret_cast <const uint16_t *>(
148
+ uint16_t code16 = *reinterpret_cast <const uint16_t *>(
117
149
codes + ELEMENT_TO_READ * 5 + 2 );
150
+ #ifdef FAISS_BIG_ENDIAN
151
+ code16 = Swap2Bytes (code16);
152
+ #endif
118
153
return (code16 & 0b0011111111110000 ) >> 4 ;
119
154
}
120
155
}
121
156
case 3 : {
122
- const uint16_t code16 = *reinterpret_cast <const uint16_t *>(
157
+ uint16_t code16 = *reinterpret_cast <const uint16_t *>(
123
158
codes + ELEMENT_TO_READ * 5 + 3 );
159
+ #ifdef FAISS_BIG_ENDIAN
160
+ code16 = Swap2Bytes (code16);
161
+ #endif
124
162
return (code16 & 0b1111111111000000 ) >> 6 ;
125
163
}
126
164
}
@@ -147,45 +185,69 @@ struct Uint12Reader {
147
185
switch (SUB_ELEMENT) {
148
186
case 0 : {
149
187
if (N_ELEMENTS > CPOS + 2 ) {
150
- const uint32_t code32 = *reinterpret_cast <const uint32_t *>(
188
+ uint32_t code32 = *reinterpret_cast <const uint32_t *>(
151
189
codes + ELEMENT_TO_READ * 6 );
190
+ #ifdef FAISS_BIG_ENDIAN
191
+ code32 = Swap4Bytes (code32);
192
+ #endif
152
193
return (code32 & 0b0000111111111111 );
153
194
} else {
154
- const uint16_t code16 = *reinterpret_cast <const uint16_t *>(
195
+ uint16_t code16 = *reinterpret_cast <const uint16_t *>(
155
196
codes + ELEMENT_TO_READ * 6 + 0 );
197
+ #ifdef FAISS_BIG_ENDIAN
198
+ code16 = Swap2Bytes (code16);
199
+ #endif
156
200
return (code16 & 0b0000111111111111 );
157
201
}
158
202
}
159
203
case 1 : {
160
204
if (N_ELEMENTS > CPOS + 1 ) {
161
- const uint32_t code32 = *reinterpret_cast <const uint32_t *>(
205
+ uint32_t code32 = *reinterpret_cast <const uint32_t *>(
162
206
codes + ELEMENT_TO_READ * 6 );
207
+ #ifdef FAISS_BIG_ENDIAN
208
+ code32 = Swap4Bytes (code32);
209
+ #endif
163
210
return (code32 & 0b111111111111000000000000 ) >> 12 ;
164
211
} else {
165
- const uint16_t code16 = *reinterpret_cast <const uint16_t *>(
212
+ uint16_t code16 = *reinterpret_cast <const uint16_t *>(
166
213
codes + ELEMENT_TO_READ * 6 + 1 );
214
+ #ifdef FAISS_BIG_ENDIAN
215
+ code16 = Swap2Bytes (code16);
216
+ #endif
167
217
return (code16 & 0b1111111111110000 ) >> 4 ;
168
218
}
169
219
}
170
220
case 2 : {
171
221
if (N_ELEMENTS > CPOS + 1 ) {
172
- const uint32_t code32 = *reinterpret_cast <const uint32_t *>(
222
+ uint32_t code32 = *reinterpret_cast <const uint32_t *>(
173
223
codes + ELEMENT_TO_READ * 6 + 2 );
224
+ #ifdef FAISS_BIG_ENDIAN
225
+ code32 = Swap4Bytes (code32);
226
+ #endif
174
227
return (code32 & 0b000011111111111100000000 ) >> 8 ;
175
228
} else {
176
- const uint16_t code16 = *reinterpret_cast <const uint16_t *>(
229
+ uint16_t code16 = *reinterpret_cast <const uint16_t *>(
177
230
codes + ELEMENT_TO_READ * 6 + 3 );
231
+ #ifdef FAISS_BIG_ENDIAN
232
+ code16 = Swap2Bytes (code16);
233
+ #endif
178
234
return (code16 & 0b0000111111111111 );
179
235
}
180
236
}
181
237
case 3 : {
182
238
if (N_ELEMENTS > CPOS) {
183
- const uint32_t code32 = *reinterpret_cast <const uint32_t *>(
239
+ uint32_t code32 = *reinterpret_cast <const uint32_t *>(
184
240
codes + ELEMENT_TO_READ * 6 + 2 );
241
+ #ifdef FAISS_BIG_ENDIAN
242
+ code32 = Swap4Bytes (code32);
243
+ #endif
185
244
return (code32 & 0b11111111111100000000000000000000 ) >> 20 ;
186
245
} else {
187
- const uint16_t code16 = *reinterpret_cast <const uint16_t *>(
246
+ uint16_t code16 = *reinterpret_cast <const uint16_t *>(
188
247
codes + ELEMENT_TO_READ * 6 + 4 );
248
+ #ifdef FAISS_BIG_ENDIAN
249
+ code16 = Swap2Bytes (code16);
250
+ #endif
189
251
return (code16 & 0b1111111111110000 ) >> 4 ;
190
252
}
191
253
}
@@ -208,23 +270,39 @@ struct Uint16Reader {
208
270
switch (SUB_ELEMENT) {
209
271
case 0 : {
210
272
if (N_ELEMENTS > CPOS + 1 ) {
211
- const uint32_t code32 = *reinterpret_cast <const uint32_t *>(
273
+ uint32_t code32 = *reinterpret_cast <const uint32_t *>(
212
274
codes + ELEMENT_TO_READ * 4 );
275
+ #ifdef FAISS_BIG_ENDIAN
276
+ code32 = Swap4Bytes (code32);
277
+ #endif
213
278
return (code32 & 0x0000FFFF );
214
279
} else {
215
280
const uint16_t * const __restrict codesFp16 =
216
281
reinterpret_cast <const uint16_t *>(codes);
282
+ #ifdef FAISS_BIG_ENDIAN
283
+ uint16_t rt = codesFp16[CPOS];
284
+ rt = Swap2Bytes (rt);
285
+ return rt;
286
+ #endif
217
287
return codesFp16[CPOS];
218
288
}
219
289
}
220
290
case 1 : {
221
291
if (N_ELEMENTS > CPOS) {
222
- const uint32_t code32 = *reinterpret_cast <const uint32_t *>(
292
+ uint32_t code32 = *reinterpret_cast <const uint32_t *>(
223
293
codes + ELEMENT_TO_READ * 4 );
294
+ #ifdef FAISS_BIG_ENDIAN
295
+ code32 = Swap4Bytes (code32);
296
+ #endif
224
297
return code32 >> 16 ;
225
298
} else {
226
299
const uint16_t * const __restrict codesFp16 =
227
300
reinterpret_cast <const uint16_t *>(codes);
301
+ #ifdef FAISS_BIG_ENDIAN
302
+ uint16_t rt = codesFp16[CPOS];
303
+ rt = Swap2Bytes (rt);
304
+ return rt;
305
+ #endif
228
306
return codesFp16[CPOS];
229
307
}
230
308
}
0 commit comments