-/* Accessors for each type of encoding */
-#define INTSET_VALUE_ENCODING(__val) (((__val) < INT32_MIN || (__val) > INT32_MAX) ? \
- INTSET_ENC_INT64 : (((__val) < INT16_MIN || (__val) > INT16_MAX) ? \
- INTSET_ENC_INT32 : INTSET_ENC_INT16))
-#define INTSET_GET_ENCODED(__is,__pos,__enc) ((__enc == INTSET_ENC_INT64) ? \
- ((int64_t*)(__is)->contents)[__pos] : ((__enc == INTSET_ENC_INT32) ? \
- ((int32_t*)(__is)->contents)[__pos] : ((int16_t*)(__is)->contents)[__pos]))
-#define INTSET_GET(__is,__pos) (INTSET_GET_ENCODED(__is,__pos,(__is)->encoding))
-#define INTSET_SET(__is,__pos,__val) { \
- if ((__is)->encoding == INTSET_ENC_INT64) \
- ((int64_t*)(__is)->contents)[__pos] = (__val); \
- else if ((__is)->encoding == INTSET_ENC_INT32) \
- ((int32_t*)(__is)->contents)[__pos] = (__val); \
- else \
- ((int16_t*)(__is)->contents)[__pos] = (__val); }
+/* Return the required encoding for the provided value. */
+static uint8_t _intsetValueEncoding(int64_t v) {
+ if (v < INT32_MIN || v > INT32_MAX)
+ return INTSET_ENC_INT64;
+ else if (v < INT16_MIN || v > INT16_MAX)
+ return INTSET_ENC_INT32;
+ else
+ return INTSET_ENC_INT16;
+}
+
+/* Return the value at pos, given an encoding. */
+static int64_t _intsetGetEncoded(intset *is, int pos, uint8_t enc) {
+ int64_t v64;
+ int32_t v32;
+ int16_t v16;
+
+ if (enc == INTSET_ENC_INT64) {
+ memcpy(&v64,((int64_t*)is->contents)+pos,sizeof(v64));
+ memrev64ifbe(&v64);
+ return v64;
+ } else if (enc == INTSET_ENC_INT32) {
+ memcpy(&v32,((int32_t*)is->contents)+pos,sizeof(v32));
+ memrev32ifbe(&v32);
+ return v32;
+ } else {
+ memcpy(&v16,((int16_t*)is->contents)+pos,sizeof(v16));
+ memrev16ifbe(&v16);
+ return v16;
+ }
+}
+
+/* Return the value at pos, using the configured encoding. */
+static int64_t _intsetGet(intset *is, int pos) {
+ return _intsetGetEncoded(is,pos,intrev32ifbe(is->encoding));
+}
+
+/* Set the value at pos, using the configured encoding. */
+static void _intsetSet(intset *is, int pos, int64_t value) {
+ uint32_t encoding = intrev32ifbe(is->encoding);
+
+ if (encoding == INTSET_ENC_INT64) {
+ ((int64_t*)is->contents)[pos] = value;
+ memrev64ifbe(((int64_t*)is->contents)+pos);
+ } else if (encoding == INTSET_ENC_INT32) {
+ ((int32_t*)is->contents)[pos] = value;
+ memrev32ifbe(((int32_t*)is->contents)+pos);
+ } else {
+ ((int16_t*)is->contents)[pos] = value;
+ memrev16ifbe(((int16_t*)is->contents)+pos);
+ }
+}