Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
alignment.h
Go to the documentation of this file.
1/* Copyright (C) 2021 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23#ifndef INCLUDED_ALIGNMENT
24#define INCLUDED_ALIGNMENT
25
26#include "lib/sysdep/compiler.h" // MSC_VERSION
27#include "lib/sysdep/arch.h" // ARCH_AMD64
28
29#include <cstdint>
30
31template<typename T>
32inline bool IsAligned(T t, uintptr_t multiple)
33{
34 return (uintptr_t(t) % multiple) == 0;
35}
36
37template<size_t multiple>
38inline size_t Align(size_t n)
39{
40 cassert(multiple != 0 && ((multiple & (multiple-1)) == 0)); // is power of 2
41 return (n + multiple-1) & ~(multiple-1);
42}
43
44
45// bridge the differences between MSC and GCC alignment definitions.
46// example: ALIGNED(int, 8) myAlignedVariable = 0;
47#if MSC_VERSION
48# define ALIGNED(type, multiple) __declspec(align(multiple)) type
49#elif GCC_VERSION
50# define ALIGNED(type, multiple) type __attribute__((aligned(multiple)))
51#else
52# define ALIGNED(type, multiple) type
53#endif
54
55
56//
57// SIMD vector
58//
59
60static const size_t vectorSize = 16;
61#define VECTOR_ALIGNED(type) ALIGNED(type, 16) // ALIGNED() requires a literal; keep in sync with vectorSize
62
63#define ASSERT_VECTOR_MULTIPLE(size)\
64 ASSERT(IsAligned(size, vectorSize))
65
66#define ASSERT_VECTOR_ALIGNED(pointer)\
67 ASSERT_VECTOR_MULTIPLE(pointer);\
68 ASSUME_ALIGNED(pointer, vectorSize)
69
70
71//
72// CPU cache
73//
74
75static const size_t cacheLineSize = 64; // (L2)
76#define CACHE_ALIGNED(type) ALIGNED(type, 64) // ALIGNED() requires a literal; keep in sync with cacheLineSize
77
78
79
80
81//
82// MMU pages
83//
84
85#ifdef ARCH_PPC64
86// NOTE: ppc64 can operate in either 4k or 64k page size mode
87// If the define page size is larger than the active page size,
88// the allocator functions normally. If the defined page size
89// is less than the active page size, the allocator fails tests.
90//
91// Define the page size to the maximum known architectural page
92// size on ppc64 systems.
93static const size_t g_PageSize = 64 * 1024; // 64 KB
94#else
95static const size_t g_PageSize = 4 * 1024; // 4 KB
96#endif
97static const size_t g_LargePageSize = 2 * 1024 * 1024; // 2 MB
98
99
100//
101// misc
102//
103
104static const size_t allocationAlignment = 16;
105
106static const size_t KiB = size_t(1) << 10;
107static const size_t MiB = size_t(1) << 20;
108static const size_t GiB = size_t(1) << 30;
109
110// waio opens files with FILE_FLAG_NO_BUFFERING, so Windows requires
111// file offsets / buffers and sizes to be sector-aligned. querying the
112// actual sector size via GetDiskFreeSpace is inconvenient and slow.
113// we always request large blocks anyway, so just check whether inputs
114// are aligned to a `maximum' sector size. this catches common mistakes
115// before they cause scary "IO failed" errors. if the value turns out
116// to be too low, the Windows APIs will still complain.
117static const uintptr_t maxSectorSize = 0x1000;
118
119#endif // #ifndef INCLUDED_ALIGNMENT
static const size_t KiB
Definition: alignment.h:106
static const size_t cacheLineSize
Definition: alignment.h:75
static const size_t g_PageSize
Definition: alignment.h:93
static const size_t allocationAlignment
Definition: alignment.h:104
static const size_t MiB
Definition: alignment.h:107
static const size_t GiB
Definition: alignment.h:108
bool IsAligned(T t, uintptr_t multiple)
Definition: alignment.h:32
static const size_t vectorSize
Definition: alignment.h:60
static const uintptr_t maxSectorSize
Definition: alignment.h:117
static const size_t g_LargePageSize
Definition: alignment.h:97
size_t Align(size_t n)
Definition: alignment.h:38
#define cassert(expr)
Compile-time assertion.
Definition: code_annotation.h:209
#define T(string_literal)
Definition: secure_crt.cpp:77