2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-10-28 08:36:21 +00:00
|
|
|
#ifdef KERNEL
|
2018-10-16 11:59:28 +00:00
|
|
|
#include <Kernel/StdLib.h>
|
|
|
|
#else
|
2018-10-28 08:36:21 +00:00
|
|
|
#include <LibC/stdlib.h>
|
|
|
|
#include <LibC/string.h>
|
|
|
|
#endif
|
2018-10-16 11:59:28 +00:00
|
|
|
|
2019-04-05 13:54:56 +00:00
|
|
|
#define UNUSED_PARAM(x) (void)x
|
|
|
|
|
2019-01-13 03:31:16 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
2019-03-21 12:36:40 +00:00
|
|
|
extern "C" void* mmx_memcpy(void* to, const void* from, size_t);
|
2019-02-07 07:46:52 +00:00
|
|
|
|
2019-02-15 11:30:48 +00:00
|
|
|
[[gnu::always_inline]] inline void fast_dword_copy(dword* dest, const dword* src, size_t count)
|
2019-01-12 20:45:45 +00:00
|
|
|
{
|
2019-02-07 07:46:52 +00:00
|
|
|
if (count >= 256) {
|
|
|
|
mmx_memcpy(dest, src, count * sizeof(count));
|
|
|
|
return;
|
|
|
|
}
|
2019-01-12 20:45:45 +00:00
|
|
|
asm volatile(
|
|
|
|
"rep movsl\n"
|
2019-01-16 18:43:01 +00:00
|
|
|
: "=S"(src), "=D"(dest), "=c"(count)
|
2019-01-12 20:45:45 +00:00
|
|
|
: "S"(src), "D"(dest), "c"(count)
|
|
|
|
: "memory"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-15 11:30:48 +00:00
|
|
|
[[gnu::always_inline]] inline void fast_dword_fill(dword* dest, dword value, size_t count)
|
2019-01-12 20:45:45 +00:00
|
|
|
{
|
|
|
|
asm volatile(
|
|
|
|
"rep stosl\n"
|
2019-01-16 18:43:01 +00:00
|
|
|
: "=D"(dest), "=c"(count)
|
2019-01-12 20:45:45 +00:00
|
|
|
: "D"(dest), "c"(count), "a"(value)
|
|
|
|
: "memory"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T min(const T& a, const T& b)
|
|
|
|
{
|
|
|
|
return a < b ? a : b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T max(const T& a, const T& b)
|
|
|
|
{
|
|
|
|
return a < b ? b : a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-08 14:39:26 +00:00
|
|
|
template<typename T, typename U>
|
2019-01-31 16:31:23 +00:00
|
|
|
static inline T ceil_div(T a, U b)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-11-08 14:39:26 +00:00
|
|
|
static_assert(sizeof(T) == sizeof(U));
|
2018-10-10 09:53:07 +00:00
|
|
|
T result = a / b;
|
|
|
|
if ((a % b) != 0)
|
|
|
|
++result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:43:52 +00:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wconsumed"
|
|
|
|
#endif
|
2018-10-16 10:10:01 +00:00
|
|
|
template <typename T>
|
|
|
|
T&& move(T& arg)
|
|
|
|
{
|
|
|
|
return static_cast<T&&>(arg);
|
|
|
|
}
|
2019-02-25 11:43:52 +00:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-16 10:20:51 +00:00
|
|
|
template<typename T>
|
2018-10-17 08:55:43 +00:00
|
|
|
struct Identity {
|
|
|
|
typedef T Type;
|
2018-10-16 10:20:51 +00:00
|
|
|
};
|
2018-10-17 08:55:43 +00:00
|
|
|
|
2018-10-16 11:59:28 +00:00
|
|
|
template<class T>
|
2018-10-17 08:55:43 +00:00
|
|
|
constexpr T&& forward(typename Identity<T>::Type& param)
|
2018-10-16 10:20:51 +00:00
|
|
|
{
|
2018-10-16 11:59:28 +00:00
|
|
|
return static_cast<T&&>(param);
|
2018-10-16 10:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename U>
|
|
|
|
T exchange(T& a, U&& b)
|
|
|
|
{
|
|
|
|
T tmp = move(a);
|
|
|
|
a = move(b);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2018-10-16 12:33:16 +00:00
|
|
|
template<typename T, typename U>
|
|
|
|
void swap(T& a, U& b)
|
|
|
|
{
|
|
|
|
U tmp = move((U&)a);
|
|
|
|
a = (T&&)move(b);
|
|
|
|
b = move(tmp);
|
|
|
|
}
|
2018-10-16 10:20:51 +00:00
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<bool B, class T = void>
|
|
|
|
struct EnableIf
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct EnableIf<true, T>
|
|
|
|
{
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T> struct RemoveConst { typedef T Type; };
|
|
|
|
template<class T> struct RemoveConst<const T> { typedef T Type; };
|
|
|
|
template<class T> struct RemoveVolatile { typedef T Type; };
|
|
|
|
template<class T> struct RemoveVolatile<const T> { typedef T Type; };
|
|
|
|
template<class T> struct RemoveCV {
|
|
|
|
typedef typename RemoveVolatile<typename RemoveConst<T>::Type>::Type Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T, T v>
|
|
|
|
struct IntegralConstant {
|
|
|
|
static constexpr T value = v;
|
|
|
|
typedef T ValueType;
|
|
|
|
typedef IntegralConstant Type;
|
|
|
|
constexpr operator ValueType() const { return value; }
|
|
|
|
constexpr ValueType operator()() const { return value; }
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef IntegralConstant<bool, false> FalseType;
|
|
|
|
typedef IntegralConstant<bool, true> TrueType;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct __IsPointerHelper : FalseType { };
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct __IsPointerHelper<T*> : TrueType { };
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct IsPointer : __IsPointerHelper<typename RemoveCV<T>::Type> { };
|
|
|
|
|
|
|
|
template<class> struct IsFunction : FalseType { };
|
|
|
|
|
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...)> : TrueType { };
|
2018-11-09 11:20:44 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...)> : TrueType { };
|
2018-10-17 08:55:43 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...) const> : TrueType { };
|
2018-11-09 11:20:44 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const> : TrueType { };
|
2018-10-17 08:55:43 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...) volatile> : TrueType { };
|
2018-11-09 11:20:44 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) volatile> : TrueType { };
|
2018-10-17 08:55:43 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...) const volatile> : TrueType { };
|
2018-11-09 11:20:44 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const volatile> : TrueType { };
|
2018-10-17 08:55:43 +00:00
|
|
|
|
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...) &> : TrueType { };
|
2018-11-09 11:20:44 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) &> : TrueType { };
|
2018-10-17 08:55:43 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...) const &> : TrueType { };
|
2018-11-09 11:20:44 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const &> : TrueType { };
|
2018-10-17 08:55:43 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...) volatile &> : TrueType { };
|
2018-11-09 11:20:44 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) volatile &> : TrueType { };
|
2018-10-17 08:55:43 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...) const volatile &> : TrueType { };
|
2018-11-09 11:20:44 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const volatile &> : TrueType { };
|
2018-10-17 08:55:43 +00:00
|
|
|
|
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...) &&> : TrueType { };
|
2018-11-09 11:20:44 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) &&> : TrueType { };
|
2018-10-17 08:55:43 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...) const &&> : TrueType { };
|
2018-11-09 11:20:44 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const &&> : TrueType { };
|
2018-10-17 08:55:43 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...) volatile &&> : TrueType { };
|
2018-11-09 11:20:44 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) volatile &&> : TrueType { };
|
2018-10-17 08:55:43 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...) const volatile &&> : TrueType { };
|
2018-11-09 11:20:44 +00:00
|
|
|
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const volatile &&> : TrueType { };
|
2018-10-17 08:55:43 +00:00
|
|
|
|
|
|
|
template<class T> struct IsRvalueReference : FalseType { };
|
|
|
|
template<class T> struct IsRvalueReference<T&&> : TrueType { };
|
|
|
|
|
|
|
|
template<class T> struct RemovePointer { typedef T Type; };
|
|
|
|
template<class T> struct RemovePointer<T*> { typedef T Type; };
|
|
|
|
template<class T> struct RemovePointer<T* const> { typedef T Type; };
|
|
|
|
template<class T> struct RemovePointer<T* volatile> { typedef T Type; };
|
|
|
|
template<class T> struct RemovePointer<T* const volatile> { typedef T Type; };
|
|
|
|
|
2019-02-20 11:28:41 +00:00
|
|
|
template<typename T, typename U>
|
|
|
|
struct IsSame {
|
|
|
|
enum { value = 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct IsSame<T, T> {
|
|
|
|
enum { value = 1 };
|
|
|
|
};
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::min;
|
|
|
|
using AK::max;
|
2018-10-16 10:20:51 +00:00
|
|
|
using AK::move;
|
|
|
|
using AK::forward;
|
|
|
|
using AK::exchange;
|
2018-10-16 12:33:16 +00:00
|
|
|
using AK::swap;
|
2019-01-31 16:31:23 +00:00
|
|
|
using AK::ceil_div;
|
2019-02-20 11:28:41 +00:00
|
|
|
using AK::IsSame;
|