2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023 John Tsiombikas <nuclear@mutantstargoat.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
24 void *malloc_nf_impl(size_t sz, const char *file, int line)
27 if(!(p = malloc(sz))) {
28 fprintf(stderr, "%s:%d failed to allocate %lu bytes\n", file, line, (unsigned long)sz);
34 void *calloc_nf_impl(size_t num, size_t sz, const char *file, int line)
37 if(!(p = calloc(num, sz))) {
38 fprintf(stderr, "%s:%d failed to allocate %lu bytes\n", file, line, (unsigned long)(num * sz));
44 void *realloc_nf_impl(void *p, size_t sz, const char *file, int line)
46 if(!(p = realloc(p, sz))) {
47 fprintf(stderr, "%s:%d failed to realloc %lu bytes\n", file, line, (unsigned long)sz);
53 char *strdup_nf_impl(const char *s, const char *file, int line)
59 if(!(res = malloc(len + 1))) {
60 fprintf(stderr, "%s:%d failed to duplicate string\n", file, line);
63 memcpy(res, s, len + 1);
68 int match_prefix(const char *str, const char *prefix)
70 while(*str && *prefix) {
71 if(*str++ != *prefix++) {
75 return *prefix ? 0 : 1;
78 #if defined(__APPLE__) && !defined(TARGET_IPHONE)
79 #include <xmmintrin.h>
81 void enable_fpexcept(void)
84 bits = _MM_MASK_INVALID | _MM_MASK_DIV_ZERO | _MM_MASK_OVERFLOW | _MM_MASK_UNDERFLOW;
85 _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~bits);
88 void disable_fpexcept(void)
91 bits = _MM_MASK_INVALID | _MM_MASK_DIV_ZERO | _MM_MASK_OVERFLOW | _MM_MASK_UNDERFLOW;
92 _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() | bits);
95 #elif defined(__GLIBC__) && !defined(__MINGW32__)
101 void enable_fpexcept(void)
103 feenableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
106 void disable_fpexcept(void)
108 fedisableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
111 #elif defined(_MSC_VER) || defined(__MINGW32__) || defined(__WATCOMC__)
114 #if defined(__MINGW32__) && !defined(_EM_OVERFLOW)
115 /* if gcc's float.h gets precedence, the mingw MSVC includes won't be declared */
116 #define _MCW_EM 0x8001f
117 #define _EM_INVALID 0x10
118 #define _EM_ZERODIVIDE 0x08
119 #define _EM_OVERFLOW 0x04
120 unsigned int __cdecl _clearfp(void);
121 unsigned int __cdecl _controlfp(unsigned int, unsigned int);
122 #elif defined(__WATCOMC__)
123 #define _clearfp _clear87
124 #define _controlfp _control87
127 void enable_fpexcept(void)
130 _controlfp(_controlfp(0, 0) & ~(_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW), _MCW_EM);
133 void disable_fpexcept(void)
136 _controlfp(_controlfp(0, 0) | (_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW), _MCW_EM);
139 void enable_fpexcept(void) {}
140 void disable_fpexcept(void) {}