Browse Source

docs, compile fixes.

Adam Ierymenko 5 years ago
parent
commit
f107a71796
3 changed files with 100 additions and 110 deletions
  1. 2 2
      node/MIMC52.cpp
  2. 18 0
      node/Tests.cpp
  3. 80 108
      node/Utils.hpp

File diff suppressed because it is too large
+ 2 - 2
node/MIMC52.cpp


+ 18 - 0
node/Tests.cpp

@@ -312,6 +312,24 @@ extern "C" const char *ZTT_general()
 			ZT_T_PRINTF("OK" ZT_EOL_S);
 			ZT_T_PRINTF("OK" ZT_EOL_S);
 		}
 		}
 
 
+		{
+			ZT_T_PRINTF("[general] Testing hton/ntoh byte order converters... ");
+			uint64_t a = Utils::hton((uint64_t)1);
+			uint32_t b = Utils::hton((uint32_t)1);
+			uint16_t c = Utils::hton((uint16_t)1);
+			if (
+				(reinterpret_cast<uint8_t *>(&a)[7] != 1)||
+				(reinterpret_cast<uint8_t *>(&b)[3] != 1)||
+				(reinterpret_cast<uint8_t *>(&c)[1] != 1)||
+				(Utils::ntoh(a) != 1)||
+				(Utils::ntoh(b) != 1)||
+				(Utils::ntoh(c) != 1)
+			) {
+				ZT_T_PRINTF("FAILED" ZT_EOL_S);
+				return "Utils::hton/ntoh not working properly";
+			}
+			ZT_T_PRINTF("OK" ZT_EOL_S);
+		}
 		ZT_T_PRINTF("[general] Utils::hash64() samples for 1, 2, int64_max, uint64_max: %.16llx %.16llx %.16llx %.16llx" ZT_EOL_S,Utils::hash64(1),Utils::hash64(2),Utils::hash64(0xffffffffffffffffULL),Utils::hash64(0x7fffffffffffffffULL));
 		ZT_T_PRINTF("[general] Utils::hash64() samples for 1, 2, int64_max, uint64_max: %.16llx %.16llx %.16llx %.16llx" ZT_EOL_S,Utils::hash64(1),Utils::hash64(2),Utils::hash64(0xffffffffffffffffULL),Utils::hash64(0x7fffffffffffffffULL));
 
 
 		{
 		{

+ 80 - 108
node/Utils.hpp

@@ -295,143 +295,115 @@ static ZT_ALWAYS_INLINE unsigned int countBits(T v) noexcept
 }
 }
 #endif
 #endif
 
 
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-static ZT_ALWAYS_INLINE uint8_t hton(uint8_t n) noexcept { return n; }
-static ZT_ALWAYS_INLINE int8_t hton(int8_t n) noexcept { return n; }
-static ZT_ALWAYS_INLINE uint16_t hton(uint16_t n) noexcept
+/**
+ * Unconditionally swap bytes regardless of host byte order
+ *
+ * @param n Integer to swap
+ * @return Integer with bytes reversed
+ */
+static ZT_ALWAYS_INLINE uint64_t swapBytes(const uint64_t n) noexcept
 {
 {
-#if defined(__GNUC__)
-#if defined(__FreeBSD__)
-	return htons(n);
-#elif (!defined(__OpenBSD__))
-	return __builtin_bswap16(n);
-#endif
+#ifdef __GNUC__
+	return __builtin_bswap64(n);
 #else
 #else
-	return htons(n);
+#ifdef _MSC_VER
+	return (uint64_t)_byteswap_uint64((unsigned __int64)n);
+#else
+	return (
+		((n & 0x00000000000000ffULL) << 56) |
+		((n & 0x000000000000ff00ULL) << 40) |
+		((n & 0x0000000000ff0000ULL) << 24) |
+		((n & 0x00000000ff000000ULL) <<  8) |
+		((n & 0x000000ff00000000ULL) >>  8) |
+		((n & 0x0000ff0000000000ULL) >> 24) |
+		((n & 0x00ff000000000000ULL) >> 40) |
+		((n & 0xff00000000000000ULL) >> 56)
+	);
+#endif
 #endif
 #endif
 }
 }
-static ZT_ALWAYS_INLINE int16_t hton(int16_t n) noexcept { return (int16_t)Utils::hton((uint16_t)n); }
-static ZT_ALWAYS_INLINE uint32_t hton(uint32_t n) noexcept
+
+/**
+ * Unconditionally swap bytes regardless of host byte order
+ *
+ * @param n Integer to swap
+ * @return Integer with bytes reversed
+ */
+static ZT_ALWAYS_INLINE uint32_t swapBytes(const uint32_t n) noexcept
 {
 {
-#if defined(__GNUC__)
-#if defined(__FreeBSD__)
-	return htonl(n);
-#elif (!defined(__OpenBSD__))
+#if defined(__GCC__)
 	return __builtin_bswap32(n);
 	return __builtin_bswap32(n);
-#endif
+#else
+#ifdef _MSC_VER
+	return (uint32_t)_byteswap_ulong((unsigned long)n);
 #else
 #else
 	return htonl(n);
 	return htonl(n);
 #endif
 #endif
-}
-static ZT_ALWAYS_INLINE int32_t hton(int32_t n) noexcept { return (int32_t)Utils::hton((uint32_t)n); }
-static ZT_ALWAYS_INLINE uint64_t hton(uint64_t n) noexcept
-{
-#if defined(__GNUC__)
-#if defined(__FreeBSD__)
-	return bswap64(n);
-#elif (!defined(__OpenBSD__))
-	return __builtin_bswap64(n);
-#endif
-#else
-	return (
-		((n & 0x00000000000000FFULL) << 56) |
-		((n & 0x000000000000FF00ULL) << 40) |
-		((n & 0x0000000000FF0000ULL) << 24) |
-		((n & 0x00000000FF000000ULL) <<  8) |
-		((n & 0x000000FF00000000ULL) >>  8) |
-		((n & 0x0000FF0000000000ULL) >> 24) |
-		((n & 0x00FF000000000000ULL) >> 40) |
-		((n & 0xFF00000000000000ULL) >> 56)
-	);
 #endif
 #endif
 }
 }
-static ZT_ALWAYS_INLINE int64_t hton(int64_t n) noexcept { return (int64_t)hton((uint64_t)n); }
-#else
-template<typename T>
-static ZT_ALWAYS_INLINE T hton(T n) noexcept { return n; }
-#endif
 
 
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-static ZT_ALWAYS_INLINE uint8_t ntoh(uint8_t n) noexcept { return n; }
-static ZT_ALWAYS_INLINE int8_t ntoh(int8_t n) noexcept { return n; }
-static ZT_ALWAYS_INLINE uint16_t ntoh(uint16_t n) noexcept
+/**
+ * Unconditionally swap bytes regardless of host byte order
+ *
+ * @param n Integer to swap
+ * @return Integer with bytes reversed
+ */
+static ZT_ALWAYS_INLINE uint16_t swapBytes(const uint16_t n) noexcept
 {
 {
-#if defined(__GNUC__)
-#if defined(__FreeBSD__)
-	return htons(n);
-#elif (!defined(__OpenBSD__))
+#if defined(__GCC__)
 	return __builtin_bswap16(n);
 	return __builtin_bswap16(n);
-#endif
+#else
+#ifdef _MSC_VER
+	return (uint16_t)_byteswap_ushort((unsigned short)n);
 #else
 #else
 	return htons(n);
 	return htons(n);
 #endif
 #endif
-}
-static ZT_ALWAYS_INLINE int16_t ntoh(int16_t n) noexcept { return (int16_t)Utils::ntoh((uint16_t)n); }
-static ZT_ALWAYS_INLINE uint32_t ntoh(uint32_t n) noexcept
-{
-#if defined(__GNUC__)
-#if defined(__FreeBSD__)
-	return ntohl(n);
-#elif (!defined(__OpenBSD__))
-	return __builtin_bswap32(n);
-#endif
-#else
-	return ntohl(n);
 #endif
 #endif
 }
 }
-static ZT_ALWAYS_INLINE int32_t ntoh(int32_t n) noexcept { return (int32_t)Utils::ntoh((uint32_t)n); }
-static ZT_ALWAYS_INLINE uint64_t ntoh(uint64_t n) noexcept
-{
-#if defined(__GNUC__)
-#if defined(__FreeBSD__)
-	return bswap64(n);
-#elif (!defined(__OpenBSD__))
-	return __builtin_bswap64(n);
+
+// ZZvvvzvzvvzzz... moooooo... http://www.catb.org/~esr/jargon/html/Y/yak-shaving.html
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+template<typename I,unsigned int S>
+class _hton_impl { public: static ZT_ALWAYS_INLINE I hton(const I n) { return n; } };
+template<typename I>
+class _hton_impl<I,2> { public: static ZT_ALWAYS_INLINE I hton(const I n) { return (I)swapBytes((uint16_t)n); } };
+template<typename I>
+class _hton_impl<I,4> { public: static ZT_ALWAYS_INLINE I hton(const I n) { return (I)swapBytes((uint32_t)n); } };
+template<typename I>
+class _hton_impl<I,8> { public: static ZT_ALWAYS_INLINE I hton(const I n) { return (I)swapBytes((uint64_t)n); } };
 #endif
 #endif
+
+/**
+ * Convert any signed or unsigned integer type to big-endian ("network") byte order
+ *
+ * @tparam I Integer type (usually inferred)
+ * @param n Value to convert
+ * @return Value in big-endian order
+ */
+template<typename I>
+static ZT_ALWAYS_INLINE I hton(const I n) noexcept
+{
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+	return _hton_impl<I,sizeof(I)>::hton(n);
 #else
 #else
-	return (
-		((n & 0x00000000000000FFULL) << 56) |
-		((n & 0x000000000000FF00ULL) << 40) |
-		((n & 0x0000000000FF0000ULL) << 24) |
-		((n & 0x00000000FF000000ULL) <<  8) |
-		((n & 0x000000FF00000000ULL) >>  8) |
-		((n & 0x0000FF0000000000ULL) >> 24) |
-		((n & 0x00FF000000000000ULL) >> 40) |
-		((n & 0xFF00000000000000ULL) >> 56)
-	);
+	return n;
 #endif
 #endif
 }
 }
-static ZT_ALWAYS_INLINE int64_t ntoh(int64_t n) noexcept { return (int64_t)ntoh((uint64_t)n); }
-#else
-template<typename T>
-static ZT_ALWAYS_INLINE T ntoh(T n) noexcept { return n; }
-#endif
 
 
 /**
 /**
- * Unconditionally swap bytes regardless of host byte order
+ * Convert any signed or unsigned integer type to host byte order from big-endian ("network") byte order
  *
  *
- * @param n Integer to swap
- * @return Integer with bytes reversed
+ * @tparam I Integer type (usually inferred)
+ * @param n Value to convert
+ * @return Value in host byte order
  */
  */
-static ZT_ALWAYS_INLINE uint64_t swapBytes(uint64_t n) noexcept
+template<typename I>
+static ZT_ALWAYS_INLINE I ntoh(const I n) noexcept
 {
 {
-#if defined(__GNUC__)
-#if defined(__FreeBSD__)
-	return bswap64(n);
-#elif (!defined(__OpenBSD__))
-	return __builtin_bswap64(n);
-#endif
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+	return _hton_impl<I,sizeof(I)>::hton(n);
 #else
 #else
-	return (
-		((n & 0x00000000000000ffULL) << 56) |
-		((n & 0x000000000000ff00ULL) << 40) |
-		((n & 0x0000000000ff0000ULL) << 24) |
-		((n & 0x00000000ff000000ULL) <<  8) |
-		((n & 0x000000ff00000000ULL) >>  8) |
-		((n & 0x0000ff0000000000ULL) >> 24) |
-		((n & 0x00ff000000000000ULL) >> 40) |
-		((n & 0xff00000000000000ULL) >> 56)
-	);
+	return n;
 #endif
 #endif
 }
 }
 
 

Some files were not shown because too many files changed in this diff