Nebula
Loading...
Searching...
No Matches
types.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12
13#include <new>
14
15#ifdef __cpp_lib_hardware_interference_size
16#define NEBULA_CACHE_LINE_SIZE std::hardware_destructive_interference_size
17#else
18#define NEBULA_CACHE_LINE_SIZE 64
19#endif
20
21#if !SPU
22#include "memory/memory.h"
23#endif
24
25#include <stddef.h>
26#include <stdint.h>
27#include <inttypes.h>
28
29// fixing Windows defines...
30#ifdef DeleteFile
31#undef DeleteFile
32#endif
33#ifdef CopyFile
34#undef CopyFile
35#endif
36#ifdef GetObject
37#undef GetObject
38#endif
39
40typedef unsigned long ulong;
41typedef unsigned int uint;
42typedef unsigned short ushort;
43typedef unsigned char uchar;
44typedef unsigned char ubyte;
45
46typedef uintptr_t uintptr;
47typedef ptrdiff_t ptrdiff;
48
49typedef int IndexT; // the index type
50typedef int SizeT; // the size type
51typedef int64_t Index64T; // the index type
52typedef int64_t Size64T; // the size type
53typedef uintptr PtrT; // the ptr type
55static const int InvalidIndex = -1;
56
57//------------------------------------------------------------------------------
60#define N_BIT(x) (1 << x)
61template <class MASK, class BITS>
62constexpr MASK
63SetBits(const MASK mask, const BITS bit)
64{
65 return MASK(uint(mask) | uint(bit));
66}
67
68//------------------------------------------------------------------------------
71template <class MASK, class BITS>
72constexpr MASK
73UnsetBits(const MASK mask, const BITS bit)
74{
75 return MASK(uint(mask) & ~uint(bit));
76}
77
78#define N_ARGB(a,r,g,b) ((uint32_t)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff)))
79#define N_RGBA(r,g,b,a) N_ARGB(a,r,g,b)
80#define N_XRGB(r,g,b) N_ARGB(0xff,r,g,b)
81#define N_COLORVALUE(r,g,b,a) N_RGBA((uint32_t)((r)*255.f),(uint)((g)*255.f),(uint)((b)*255.f),(uint)((a)*255.f))
82#define N_IP_ADDR(a,b,c,d) (uint32_t)((((a)&0xff)<<24)|(((b)&0xff)<<16)|(((c)&0xff)<<8)|((d)&0xff))
83
84//------------------------------------------------------------------------------
88template <class FLAGS, class BITS>
89constexpr bool
90AllBits(const FLAGS flags, const BITS bits)
91{
92 return (flags & bits) == bits;
93}
94
95//------------------------------------------------------------------------------
99template <class FLAGS, class BITS>
100constexpr bool
101AnyBits(const FLAGS flags, const BITS bits)
102{
103 return (flags & bits) != (FLAGS)0;
104}
105
106//------------------------------------------------------------------------------
109template <class FLAGS, class BITS>
110constexpr bool
111OnlyBits(const FLAGS flags, const BITS bits)
112{
113 return (flags & bits) == flags;
114}
115
116// byte bit calc
117#define BITS_TO_BYTES(x) (((x)+7)>>3)
118#define BYTES_TO_BITS(x) ((x)<<3)
119
120#if (__OSX__ || __linux__)
121inline ushort _byteswap_ushort(ushort x) { return ((x>>8) | (x<<8)); }
122inline ulong _byteswap_ulong(ulong x) { return ((x&0xff000000)>>24) | ((x&0x00ff0000)>>8) | ((x&0x00000ff00)<<8) | ((x&0x000000ff)<<24); }
123inline unsigned long long _byteswap_uint64(unsigned long long x) { return ((((unsigned long long)_byteswap_ulong((ulong)(x & 0xffffffff))) << 32) | ((unsigned long long)_byteswap_ulong((ulong)(x >> 32)))); }
124#endif
125
126#if __linux__
127typedef unsigned char byte;
128#endif
129#if __WIN32__
130#define n_stricmp stricmp
131#define n_snprintf StringCchPrintf
132#elif (__OSX__ || __APPLE__ || __linux__ )
133#define n_stricmp strcasecmp
134#define n_snprintf sprintf
135#else
136#error "Unsupported platform!"
137#endif
138
139#if __WIN32__
140#define ThreadLocal __declspec(thread)
141#elif __linux__
142#define ThreadLocal __thread
143#elif (__OSX__ || __APPLE__)
144#define ThreadLocal thread_local
145#else
146#error "Unsupported platform!"
147#endif
148
149#define lengthof(x) (sizeof(x) / sizeof(*x))
150
151#define NEBULA_ALIGN16 alignas(16)
152//------------------------------------------------------------------------------
Implements a memory related functions.
constexpr bool AllBits(const FLAGS flags, const BITS bits)
Check if all bits are set in flags.
Definition types.h:90
constexpr MASK UnsetBits(const MASK mask, const BITS bit)
Definition types.h:73
static const int InvalidIndex
Definition types.h:55
ptrdiff_t ptrdiff
Definition types.h:47
constexpr bool OnlyBits(const FLAGS flags, const BITS bits)
Definition types.h:111
unsigned char ubyte
Definition types.h:44
ptrdiff PtrDiff
Definition types.h:54
uintptr_t uintptr
Definition types.h:46
unsigned char uchar
Definition types.h:43
unsigned long ulong
Definition types.h:40
constexpr bool AnyBits(const FLAGS flags, const BITS bits)
Check if any bits are set in flags.
Definition types.h:101
int SizeT
Definition types.h:50
int64_t Size64T
Definition types.h:52
unsigned int uint
Definition types.h:41
uintptr PtrT
Definition types.h:53
constexpr MASK SetBits(const MASK mask, const BITS bit)
Definition types.h:63
unsigned short ushort
Definition types.h:42
int64_t Index64T
Definition types.h:51
int IndexT
Definition types.h:49