1 /*
2 
3   ZPL - Global module
4 
5 Credits:
6   Read AUTHORS.md
7 
8 GitHub:
9   https://github.com/zpl-c/zpl
10 
11   This Software is dual licensed under the following licenses:
12 
13   Unlicense
14   This is free and unencumbered software released into the public domain.
15 
16   Anyone is free to copy, modify, publish, use, compile, sell, or
17   distribute this software, either in source code form or as a compiled
18   binary, for any purpose, commercial or non-commercial, and by any
19   means.
20 
21   In jurisdictions that recognize copyright laws, the author or authors
22   of this software dedicate any and all copyright interest in the
23   software to the public domain. We make this dedication for the benefit
24   of the public at large and to the detriment of our heirs and
25   successors. We intend this dedication to be an overt act of
26   relinquishment in perpetuity of all present and future rights to this
27   software under copyright law.
28 
29   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
33   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
34   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35   OTHER DEALINGS IN THE SOFTWARE.
36 
37   For more information, please refer to <http://unlicense.org/>
38 
39   Apache 2.0
40   Copyright 2017-2019 Dominik Madarász <zaklaus@outlook.com>
41   Licensed under the Apache License, Version 2.0 (the "License");
42   you may not use this file except in compliance with the License.
43   You may obtain a copy of the License at
44 
45       http://www.apache.org/licenses/LICENSE-2.0
46 
47   Unless required by applicable law or agreed to in writing, software
48   distributed under the License is distributed on an "AS IS" BASIS,
49   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50   See the License for the specific language governing permissions and
51   limitations under the License.
52 
53 */
54 
55 #ifndef ZPL_INCLUDE_ZPL_H
56 #define ZPL_INCLUDE_ZPL_H
57 
58 
59 /*
60  * This file has been gutted for EDuke32.
61  * There is a lot of cool stuff in ZPL.
62  * However, 10000 lines of everything and the kitchen sink is an unacceptable burden.
63  * Additionally, implementations are often lacking outside of a few well-tested targets.
64  * It is far from portable enough for our needs to include anything beyond the specific pieces we want to use.
65  * Even then, changes are often required to allow portable use.
66  */
67 
68 #define ZPL_DEF extern
69 
70 #define zpl_inline FORCE_INLINE
71 
72 typedef int32_t zpl_i32;
73 typedef zpl_i32 zpl_b32;
74 typedef uint32_t zpl_u32;
75 typedef uint64_t zpl_u64;
76 
77 #define zpl_size_of(x) sizeof(x)
78 #define zpl_pointer_add(ptr, bytes) ((void *)((char *)ptr + bytes))
79 #define cast(Type) (Type)
80 
81 #define ZPL_ASSERT(x) Bassert(x)
82 
83 
84 /* Begin ZPL. */
85 
86 #if defined(__GCC__) || defined(__GNUC__) || defined(__clang__)
87 #pragma GCC diagnostic push
88 #pragma GCC diagnostic ignored "-Wunused-function"
89 #endif
90 
91 #if defined(_MSC_VER)
92 #pragma warning(push)
93 #pragma warning(disable : 4201)
94 #pragma warning(disable : 4127) // Conditional expression is constant
95 #endif
96 
97 #if defined(__cplusplus)
98 extern "C" {
99 #endif
100 
101 
102 typedef struct zpl_virtual_memory {
103     void *data;
104     size_t size;
105 } zpl_virtual_memory;
106 
107 ZPL_DEF zpl_virtual_memory zpl_vm(void *data, size_t size);
108 ZPL_DEF zpl_virtual_memory zpl_vm_alloc(void *addr, size_t size);
109 ZPL_DEF zpl_b32 zpl_vm_free(zpl_virtual_memory vm);
110 
111 
112 #if defined _MSC_VER && !defined __clang__ && !defined(_M_ARM64)
113 #define ZPL_HAVE_RDTSC
zpl_rdtsc(void)114 zpl_inline zpl_u64 zpl_rdtsc(void) { return __rdtsc( ); }
115 #elif defined __i386__
116 #define ZPL_HAVE_RDTSC
zpl_rdtsc(void)117 zpl_inline zpl_u64 zpl_rdtsc(void) {
118     zpl_u64 x;
119     __asm__ volatile(".byte 0x0f, 0x31" : "=A"(x));
120     return x;
121 }
122 #elif defined __x86_64__
123 #define ZPL_HAVE_RDTSC
zpl_rdtsc(void)124 zpl_inline zpl_u64 zpl_rdtsc(void) {
125     zpl_u32 hi, lo;
126     __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
127     return (cast(zpl_u64) lo) | ((cast(zpl_u64) hi) << 32);
128 }
129 #elif defined EDUKE32_CPU_PPC
130 #define ZPL_HAVE_RDTSC
zpl_rdtsc(void)131 zpl_inline zpl_u64 zpl_rdtsc(void) {
132     zpl_u64 result = 0;
133     zpl_u32 upper, lower, tmp;
134     __asm__ volatile("0:                   \n"
135                      "\tmftbu   %0         \n"
136                      "\tmftb    %1         \n"
137                      "\tmftbu   %2         \n"
138                      "\tcmpw    %2,%0      \n"
139                      "\tbne     0b         \n"
140                      : "=r"(upper), "=r"(lower), "=r"(tmp));
141     result = upper;
142     result = result << 32;
143     result = result | lower;
144 
145     return result;
146 }
147 #elif defined EDUKE32_CPU_ARM
148 #define ZPL_HAVE_RDTSC
zpl_rdtsc(void)149 zpl_inline zpl_u64 zpl_rdtsc(void) {
150 #if defined(__aarch64__)
151     int64_t r = 0;
152     asm volatile("mrs %0, cntvct_el0" : "=r"(r));
153 #elif (__ARM_ARCH >= 6)
154     uint32_t r = 0;
155     uint32_t pmccntr;
156     uint32_t pmuseren;
157     uint32_t pmcntenset;
158 
159     // Read the user mode perf monitor counter access permissions.
160     asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r"(pmuseren));
161     if (pmuseren & 1) { // Allows reading perfmon counters for user mode code.
162         asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r"(pmcntenset));
163         if (pmcntenset & 0x80000000ul) { // Is it counting?
164             asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(pmccntr));
165             // The counter is set up to count every 64th cycle
166             return ((int64_t)pmccntr) * 64; // Should optimize to << 6
167         }
168     }
169 #else
170 #error "No suitable method for zpl_rdtsc for this cpu type"
171 #endif
172     return r;
173 }
174 #endif
175 
176 
177 #if defined(__cplusplus)
178 }
179 #endif
180 
181 #if defined(_MSC_VER)
182 #pragma warning(pop)
183 #endif
184 
185 #if defined(__GCC__) || defined(__GNUC__) || defined(__clang__)
186 #pragma GCC diagnostic pop
187 #endif
188 
189 
190 /* End ZPL. */
191 
192 #undef cast
193 #undef ZPL_DEF
194 #undef zpl_inline
195 
xvm_alloc(void * const ptr,const size_t size)196 static FORCE_INLINE zpl_virtual_memory xvm_alloc(void * const ptr, const size_t size)
197 {
198     zpl_virtual_memory vm = zpl_vm_alloc(ptr, size);
199 
200     if (EDUKE32_PREDICT_FALSE(vm.data == NULL))
201         vm.data = handle_memerr(vm.data);
202 
203     return vm;
204 }
205 #define Xvm_alloc(ptr, size) (EDUKE32_PRE_XALLOC xvm_alloc(ptr, size))
206 #define Xvm_free(ptr) (zpl_vm_free(ptr))
207 #define Xvm_free(ptr) (zpl_vm_free(ptr))
208 
209 #endif /* ZPL_INCLUDE_ZPL_H */
210