libostd/ostd/platform.hh

219 lines
5.2 KiB
C++
Raw Normal View History

/* Platform specific definitions for OctaSTD.
*
* This file is part of OctaSTD. See COPYING.md for futher information.
*/
2015-07-13 21:08:55 +02:00
#ifndef OSTD_PLATFORM_HH
#define OSTD_PLATFORM_HH
#include <stdlib.h>
2017-01-30 19:19:09 +01:00
#include <cstdint>
2015-06-27 03:05:25 +02:00
2017-02-08 01:06:50 +01:00
#include <type_traits>
#if defined(WIN32) || defined(_WIN32) || (defined(__WIN32) && !defined(__CYGWIN__))
2015-07-13 21:08:55 +02:00
# define OSTD_PLATFORM_WIN32 1
# if defined(WIN64) || defined(_WIN64)
2015-07-13 21:08:55 +02:00
# define OSTD_PLATFORM_WIN64 1
# endif
#else
2015-07-13 21:08:55 +02:00
# define OSTD_PLATFORM_POSIX 1
# if defined(__linux__)
2015-07-13 21:08:55 +02:00
# define OSTD_PLATFORM_LINUX 1
# endif
# if defined(__APPLE__)
2015-07-13 21:08:55 +02:00
# define OSTD_PLATFORM_OSX 1
# endif
# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2015-07-13 21:08:55 +02:00
# define OSTD_PLATFORM_FREEBSD 1
# define OSTD_PLATFORM_BSD 1
# endif
# if defined(__NetBSD__)
2015-07-13 21:08:55 +02:00
# define OSTD_PLATFORM_NETBSD 1
# define OSTD_PLATFORM_BSD 1
# endif
# if defined(__OpenBSD__)
2015-07-13 21:08:55 +02:00
# define OSTD_PLATFORM_OPENBSD 1
# define OSTD_PLATFORM_BSD 1
# endif
# if defined(__DragonFly__)
2015-07-13 21:08:55 +02:00
# define OSTD_PLATFORM_DRAGONFLYBSD 1
# define OSTD_PLATFORM_BSD 1
# endif
# if defined(sun) || defined(__sun)
2015-07-13 21:08:55 +02:00
# define OSTD_PLATFORM_SOLARIS 1
# endif
#endif
#if defined(__clang__)
2015-07-13 21:08:55 +02:00
# define OSTD_TOOLCHAIN_CLANG 1
#endif
#if defined(__GNUC__)
2015-07-13 21:08:55 +02:00
# define OSTD_TOOLCHAIN_GNU 1
#endif
#if defined(_MSC_VER)
2015-07-13 21:08:55 +02:00
# define OSTD_TOOLCHAIN_MSVC 1
#endif
2015-07-13 21:08:55 +02:00
#define OSTD_ENDIAN_LIL 1234
#define OSTD_ENDIAN_BIG 4321
2015-07-13 21:08:55 +02:00
#ifdef OSTD_PLATFORM_LINUX
# include <endian.h>
2015-07-13 21:08:55 +02:00
# define OSTD_BYTE_ORDER __BYTE_ORDER
#else
# if defined(__BIG_ENDIAN__) || defined(__ARMEB__) || defined(__THUMBEB__) || \
defined(__AARCH64EB__) || defined(__MIPSEB__) || defined(__MIPSEB) || \
defined(_MIPSEB) || defined(__ppc__) || defined(__POWERPC__) || \
defined(_M_PPC) || defined(__sparc__)
2015-07-13 21:08:55 +02:00
# define OSTD_BYTE_ORDER OSTD_ENDIAN_BIG
# else
2015-07-13 21:08:55 +02:00
# define OSTD_BYTE_ORDER OSTD_ENDIAN_LIL
# endif
#endif
#ifdef OSTD_PLATFORM_WIN32
# ifdef OSTD_LIBRARY_DLL
# ifdef OSTD_TOOLCHAIN_GNU
# define OSTD_EXPORT __attribute__((dllexport))
# else
# define OSTD_EXPORT __declspec(dllexport)
# endif
# else
# ifdef OSTD_TOOLCHAIN_GNU
# define OSTD_EXPORT __attribute__((dllimport))
# else
# define OSTD_EXPORT __declspec(dllimport)
# endif
# endif
# define OSTD_LOCAL
#else
# if __GNUC__ >= 4
# define OSTD_EXPORT __attribute__((visibility("default")))
# define OSTD_LOCAL __attribute__((visibility("hidden")))
# else
# define OSTD_EXPORT
# define OSTD_LOCAL
2015-09-30 20:18:09 +02:00
# endif
#endif
2015-07-13 21:07:14 +02:00
namespace ostd {
2015-06-27 03:05:25 +02:00
2015-07-13 21:08:55 +02:00
#if defined(OSTD_TOOLCHAIN_GNU)
2015-06-27 03:05:25 +02:00
/* using gcc/clang builtins */
2017-01-30 19:19:09 +01:00
inline std::uint16_t endian_swap16(std::uint16_t x) noexcept {
2015-06-27 03:05:25 +02:00
return __builtin_bswap16(x);
}
2017-01-30 19:19:09 +01:00
inline std::uint32_t endian_swap32(std::uint32_t x) noexcept {
2015-06-27 03:05:25 +02:00
return __builtin_bswap32(x);
}
2017-01-30 19:19:09 +01:00
inline std::uint64_t endian_swap64(std::uint64_t x) noexcept {
2015-06-27 03:05:25 +02:00
return __builtin_bswap64(x);
}
2015-07-13 21:08:55 +02:00
#elif defined(OSTD_TOOLCHAIN_MSVC)
2015-06-27 03:05:25 +02:00
/* using msvc builtins */
2017-01-30 19:19:09 +01:00
inline std::uint16_t endian_swap16(std::uint16_t x) noexcept {
2015-06-27 03:05:25 +02:00
return _byteswap_ushort(x);
}
2017-01-30 19:19:09 +01:00
inline std::uint32_t endian_swap32(std::uint32_t x) noexcept {
2015-06-27 03:05:25 +02:00
/* win64 is llp64 */
return _byteswap_ulong(x);
}
2017-01-30 19:19:09 +01:00
inline std::uint64_t endian_swap64(std::uint64_t x) noexcept {
2015-06-27 03:05:25 +02:00
return _byteswap_uint64(x);
}
#else
/* fallback */
2017-01-30 19:19:09 +01:00
inline std::uint16_t endian_swap16(std::uint16_t x) noexcept {
2015-06-27 03:05:25 +02:00
return (x << 8) | (x >> 8);
}
2017-01-30 19:19:09 +01:00
inline std::uint32_t endian_swap32(std::uint32_t x) noexcept {
2015-06-27 03:05:25 +02:00
return (x << 24) | (x >> 24) | ((x >> 8) & 0xFF00) | ((x << 8) & 0xFF0000);
}
2017-01-30 19:19:09 +01:00
inline std::uint64_t endian_swap64(std::uint64_t x) noexcept {
return endian_swap32(
2017-01-30 19:19:09 +01:00
std::uint32_t(x >> 32)) |
(std::uint64_t(endian_swap32(std::uint32_t(x))) << 32
);
2015-06-27 03:05:25 +02:00
}
#endif
2017-02-08 01:06:50 +01:00
/* endian swap */
template<typename T, size_t N = sizeof(T), bool IsNum = std::is_arithmetic_v<T>>
2017-02-16 20:39:05 +01:00
struct endian_swap;
2017-02-08 01:06:50 +01:00
template<typename T>
2017-02-16 20:39:05 +01:00
struct endian_swap<T, 2, true> {
2017-02-08 01:06:50 +01:00
T operator()(T v) const {
union { T iv; std::uint16_t sv; } u;
u.iv = v;
u.sv = endian_swap16(u.sv);
return u.iv;
}
};
template<typename T>
2017-02-16 20:39:05 +01:00
struct endian_swap<T, 4, true> {
2017-02-08 01:06:50 +01:00
T operator()(T v) const {
union { T iv; std::uint32_t sv; } u;
u.iv = v;
u.sv = endian_swap32(u.sv);
return u.iv;
}
};
template<typename T>
2017-02-16 20:39:05 +01:00
struct endian_swap<T, 8, true> {
2017-02-08 01:06:50 +01:00
T operator()(T v) const {
union { T iv; std::uint64_t sv; } u;
u.iv = v;
u.sv = endian_swap64(u.sv);
return u.iv;
}
};
namespace detail {
template<
typename T, size_t N = sizeof(T), bool IsNum = std::is_arithmetic_v<T>
>
2017-02-16 20:39:05 +01:00
struct endian_same;
2017-02-08 01:06:50 +01:00
template<typename T>
2017-02-16 20:39:05 +01:00
struct endian_same<T, 2, true> {
2017-02-08 01:06:50 +01:00
T operator()(T v) const { return v; }
};
template<typename T>
2017-02-16 20:39:05 +01:00
struct endian_same<T, 4, true> {
2017-02-08 01:06:50 +01:00
T operator()(T v) const { return v; }
};
template<typename T>
2017-02-16 20:39:05 +01:00
struct endian_same<T, 8, true> {
2017-02-08 01:06:50 +01:00
T operator()(T v) const { return v; }
};
}
#if OSTD_BYTE_ORDER == OSTD_ENDIAN_LIL
template<typename T>
2017-02-16 20:39:05 +01:00
struct from_lil_endian: detail::endian_same<T> {};
2017-02-08 01:06:50 +01:00
template<typename T>
2017-02-16 20:39:05 +01:00
struct from_big_endian: endian_swap<T> {};
2017-02-08 01:06:50 +01:00
#else
template<typename T>
2017-02-16 20:39:05 +01:00
struct from_lil_endian: endian_swap<T> {};
2017-02-08 01:06:50 +01:00
template<typename T>
2017-02-16 20:39:05 +01:00
struct from_big_endian: detail::endian_same<T> {};
2017-02-08 01:06:50 +01:00
#endif
2015-06-27 03:05:25 +02:00
}
2016-02-07 22:17:15 +01:00
#endif