diff options
Diffstat (limited to 'node/Utils.hpp')
-rw-r--r-- | node/Utils.hpp | 66 |
1 files changed, 24 insertions, 42 deletions
diff --git a/node/Utils.hpp b/node/Utils.hpp index a24f2c9a..86db8b02 100644 --- a/node/Utils.hpp +++ b/node/Utils.hpp @@ -1,6 +1,6 @@ /* * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/ + * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/ * * 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 @@ -13,7 +13,7 @@ * 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, see <http://www.gnu.org/licenses/>. + * along with this program. If not, see <http://www.gnu.org/licenses/>. * * -- * @@ -38,46 +38,12 @@ #include <vector> #include <map> -#include "Constants.hpp" - -#ifdef __LINUX__ -//#if (defined(_MSC_VER) || defined(__GNUC__)) && (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64)) -#if 0 -#include <emmintrin.h> -static inline void ZT_FAST_MEMCPY(void *a,const void *b,unsigned long k) -{ - char *aa = reinterpret_cast<char *>(a); - const char *bb = reinterpret_cast<const char *>(b); - while (k >= 64) { - __m128 t1 = _mm_loadu_ps(reinterpret_cast<const float *>(bb)); - __m128 t2 = _mm_loadu_ps(reinterpret_cast<const float *>(bb + 16)); - __m128 t3 = _mm_loadu_ps(reinterpret_cast<const float *>(bb + 32)); - __m128 t4 = _mm_loadu_ps(reinterpret_cast<const float *>(bb + 48)); - _mm_storeu_ps(reinterpret_cast<float *>(aa),t1); - _mm_storeu_ps(reinterpret_cast<float *>(aa + 16),t2); - _mm_storeu_ps(reinterpret_cast<float *>(aa + 32),t3); - _mm_storeu_ps(reinterpret_cast<float *>(aa + 48),t4); - bb += 64; - aa += 64; - k -= 64; - } - while (k >= 16) { - __m128 t1 = _mm_loadu_ps(reinterpret_cast<const float *>(bb)); - _mm_storeu_ps(reinterpret_cast<float *>(aa),t1); - bb += 16; - aa += 16; - k -= 16; - } - for(unsigned long i=0;i<k;++i) - aa[i] = bb[i]; -} -#else -#define ZT_FAST_MEMCPY(a,b,c) memcpy(a,b,c) -#endif -#else -#define ZT_FAST_MEMCPY(a,b,c) memcpy(a,b,c) +#if defined(__FreeBSD__) +#include <sys/endian.h> #endif +#include "Constants.hpp" + namespace ZeroTier { /** @@ -261,6 +227,14 @@ public: return l; } + static inline float normalize(float value, int64_t bigMin, int64_t bigMax, int32_t targetMin, int32_t targetMax) + { + int64_t bigSpan = bigMax - bigMin; + int64_t smallSpan = targetMax - targetMin; + float valueScaled = (value - (float)bigMin) / (float)bigSpan; + return (float)targetMin + valueScaled * (float)smallSpan; + } + /** * Generate secure random bytes * @@ -411,8 +385,12 @@ public: static inline uint64_t hton(uint64_t n) { #if __BYTE_ORDER == __LITTLE_ENDIAN -#if defined(__GNUC__) && (!defined(__OpenBSD__)) +#if defined(__GNUC__) +#if defined(__FreeBSD__) + return bswap64(n); +#elif (!defined(__OpenBSD__)) return __builtin_bswap64(n); +#endif #else return ( ((n & 0x00000000000000FFULL) << 56) | @@ -440,8 +418,12 @@ public: static inline uint64_t ntoh(uint64_t n) { #if __BYTE_ORDER == __LITTLE_ENDIAN -#if defined(__GNUC__) && !defined(__OpenBSD__) +#if defined(__GNUC__) +#if defined(__FreeBSD__) + return bswap64(n); +#elif (!defined(__OpenBSD__)) return __builtin_bswap64(n); +#endif #else return ( ((n & 0x00000000000000FFULL) << 56) | |