Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 128 additions & 17 deletions faiss/cppcontrib/detail/UintReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@

#include <cstdint>

#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))

#define Swap2Bytes(val) ((((val) >> 8) & 0x00FF) | (((val) << 8) & 0xFF00))

#define Swap4Bytes(val) \
((((val) >> 24) & 0x000000FF) | (((val) >> 8) & 0x0000FF00) | \
(((val) << 8) & 0x00FF0000) | (((val) << 24) & 0xFF000000))

#endif

namespace faiss {
namespace cppcontrib {
namespace detail {
Expand All @@ -31,7 +42,12 @@ struct Uint8Reader {
if (N_ELEMENTS > CPOS + 3) {
const uint32_t code32 = *reinterpret_cast<const uint32_t*>(
codes + ELEMENT_TO_READ * 4);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
return (code32) >> 24;
#else
return (code32 & 0x000000FF);
#endif
} else {
return codes[CPOS];
}
Expand All @@ -40,7 +56,12 @@ struct Uint8Reader {
if (N_ELEMENTS > CPOS + 2) {
const uint32_t code32 = *reinterpret_cast<const uint32_t*>(
codes + ELEMENT_TO_READ * 4);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
return (code32 & 0x00FF0000) >> 16;
#else
return (code32 & 0x0000FF00) >> 8;
#endif
} else {
return codes[CPOS];
}
Expand All @@ -49,7 +70,12 @@ struct Uint8Reader {
if (N_ELEMENTS > CPOS + 1) {
const uint32_t code32 = *reinterpret_cast<const uint32_t*>(
codes + ELEMENT_TO_READ * 4);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
return (code32 & 0x0000FF00) >> 8;
#else
return (code32 & 0x00FF0000) >> 16;
#endif
} else {
return codes[CPOS];
}
Expand All @@ -58,7 +84,12 @@ struct Uint8Reader {
if (N_ELEMENTS > CPOS) {
const uint32_t code32 = *reinterpret_cast<const uint32_t*>(
codes + ELEMENT_TO_READ * 4);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
return (code32 & 0x000000FF);
#else
return (code32) >> 24;
#endif
} else {
return codes[CPOS];
}
Expand Down Expand Up @@ -87,40 +118,68 @@ struct Uint10Reader {
switch (SUB_ELEMENT) {
case 0: {
if (N_ELEMENTS > CPOS + 2) {
const uint32_t code32 = *reinterpret_cast<const uint32_t*>(
uint32_t code32 = *reinterpret_cast<const uint32_t*>(
codes + ELEMENT_TO_READ * 5);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code32 = Swap4Bytes(code32);
#endif
return (code32 & 0b0000001111111111);
} else {
const uint16_t code16 = *reinterpret_cast<const uint16_t*>(
uint16_t code16 = *reinterpret_cast<const uint16_t*>(
codes + ELEMENT_TO_READ * 5 + 0);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code16 = Swap2Bytes(code16);
#endif
return (code16 & 0b0000001111111111);
}
}
case 1: {
if (N_ELEMENTS > CPOS + 1) {
const uint32_t code32 = *reinterpret_cast<const uint32_t*>(
uint32_t code32 = *reinterpret_cast<const uint32_t*>(
codes + ELEMENT_TO_READ * 5);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code32 = Swap4Bytes(code32);
#endif
return (code32 & 0b000011111111110000000000) >> 10;
} else {
const uint16_t code16 = *reinterpret_cast<const uint16_t*>(
uint16_t code16 = *reinterpret_cast<const uint16_t*>(
codes + ELEMENT_TO_READ * 5 + 1);
#if !defined(_MSC_VER) && \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see other PR, please use a macro in platform_macros.

(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code16 = Swap2Bytes(code16);
#endif
return (code16 & 0b0000111111111100) >> 2;
}
}
case 2: {
if (N_ELEMENTS > CPOS) {
const uint32_t code32 = *reinterpret_cast<const uint32_t*>(
uint32_t code32 = *reinterpret_cast<const uint32_t*>(
codes + ELEMENT_TO_READ * 5);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code32 = Swap4Bytes(code32);
#endif
return (code32 & 0b00111111111100000000000000000000) >> 20;
} else {
const uint16_t code16 = *reinterpret_cast<const uint16_t*>(
uint16_t code16 = *reinterpret_cast<const uint16_t*>(
codes + ELEMENT_TO_READ * 5 + 2);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code16 = Swap2Bytes(code16);
#endif
return (code16 & 0b0011111111110000) >> 4;
}
}
case 3: {
const uint16_t code16 = *reinterpret_cast<const uint16_t*>(
uint16_t code16 = *reinterpret_cast<const uint16_t*>(
codes + ELEMENT_TO_READ * 5 + 3);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code16 = Swap2Bytes(code16);
#endif
return (code16 & 0b1111111111000000) >> 6;
}
}
Expand All @@ -147,45 +206,77 @@ struct Uint12Reader {
switch (SUB_ELEMENT) {
case 0: {
if (N_ELEMENTS > CPOS + 2) {
const uint32_t code32 = *reinterpret_cast<const uint32_t*>(
uint32_t code32 = *reinterpret_cast<const uint32_t*>(
codes + ELEMENT_TO_READ * 6);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code32 = Swap4Bytes(code32);
#endif
return (code32 & 0b0000111111111111);
} else {
const uint16_t code16 = *reinterpret_cast<const uint16_t*>(
uint16_t code16 = *reinterpret_cast<const uint16_t*>(
codes + ELEMENT_TO_READ * 6 + 0);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code16 = Swap2Bytes(code16);
#endif
return (code16 & 0b0000111111111111);
}
}
case 1: {
if (N_ELEMENTS > CPOS + 1) {
const uint32_t code32 = *reinterpret_cast<const uint32_t*>(
uint32_t code32 = *reinterpret_cast<const uint32_t*>(
codes + ELEMENT_TO_READ * 6);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code32 = Swap4Bytes(code32);
#endif
return (code32 & 0b111111111111000000000000) >> 12;
} else {
const uint16_t code16 = *reinterpret_cast<const uint16_t*>(
uint16_t code16 = *reinterpret_cast<const uint16_t*>(
codes + ELEMENT_TO_READ * 6 + 1);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code16 = Swap2Bytes(code16);
#endif
return (code16 & 0b1111111111110000) >> 4;
}
}
case 2: {
if (N_ELEMENTS > CPOS + 1) {
const uint32_t code32 = *reinterpret_cast<const uint32_t*>(
uint32_t code32 = *reinterpret_cast<const uint32_t*>(
codes + ELEMENT_TO_READ * 6 + 2);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code32 = Swap4Bytes(code32);
#endif
return (code32 & 0b000011111111111100000000) >> 8;
} else {
const uint16_t code16 = *reinterpret_cast<const uint16_t*>(
uint16_t code16 = *reinterpret_cast<const uint16_t*>(
codes + ELEMENT_TO_READ * 6 + 3);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code16 = Swap2Bytes(code16);
#endif
return (code16 & 0b0000111111111111);
}
}
case 3: {
if (N_ELEMENTS > CPOS) {
const uint32_t code32 = *reinterpret_cast<const uint32_t*>(
uint32_t code32 = *reinterpret_cast<const uint32_t*>(
codes + ELEMENT_TO_READ * 6 + 2);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code32 = Swap4Bytes(code32);
#endif
return (code32 & 0b11111111111100000000000000000000) >> 20;
} else {
const uint16_t code16 = *reinterpret_cast<const uint16_t*>(
uint16_t code16 = *reinterpret_cast<const uint16_t*>(
codes + ELEMENT_TO_READ * 6 + 4);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code16 = Swap2Bytes(code16);
#endif
return (code16 & 0b1111111111110000) >> 4;
}
}
Expand All @@ -208,23 +299,43 @@ struct Uint16Reader {
switch (SUB_ELEMENT) {
case 0: {
if (N_ELEMENTS > CPOS + 1) {
const uint32_t code32 = *reinterpret_cast<const uint32_t*>(
uint32_t code32 = *reinterpret_cast<const uint32_t*>(
codes + ELEMENT_TO_READ * 4);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code32 = Swap4Bytes(code32);
#endif
return (code32 & 0x0000FFFF);
} else {
const uint16_t* const __restrict codesFp16 =
reinterpret_cast<const uint16_t*>(codes);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
uint16_t rt = codesFp16[CPOS];
rt = Swap2Bytes(rt);
return rt;
#endif
return codesFp16[CPOS];
}
}
case 1: {
if (N_ELEMENTS > CPOS) {
const uint32_t code32 = *reinterpret_cast<const uint32_t*>(
uint32_t code32 = *reinterpret_cast<const uint32_t*>(
codes + ELEMENT_TO_READ * 4);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
code32 = Swap4Bytes(code32);
#endif
return code32 >> 16;
} else {
const uint16_t* const __restrict codesFp16 =
reinterpret_cast<const uint16_t*>(codes);
#if !defined(_MSC_VER) && \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
uint16_t rt = codesFp16[CPOS];
rt = Swap2Bytes(rt);
return rt;
#endif
return codesFp16[CPOS];
}
}
Expand Down