ObjFW
macros.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2025 Jonathan Schleifer <js@nil.im>
3  *
4  * All rights reserved.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License version 3.0 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * version 3.0 for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * version 3.0 along with this program. If not, see
17  * <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef OBJFW_MACROS_H
21 #define OBJFW_MACROS_H
22 
23 #include "objfw-defs.h"
24 
25 #ifndef __STDC_LIMIT_MACROS
26 # define __STDC_LIMIT_MACROS
27 #endif
28 #ifndef __STDC_CONSTANT_MACROS
29 # define __STDC_CONSTANT_MACROS
30 #endif
31 
32 #include <limits.h>
33 #include <stdbool.h>
34 #include <stddef.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include <sys/time.h>
41 
44 #include "platform.h"
45 
46 #ifdef OF_OBJFW_RUNTIME
47 # ifdef OF_COMPILING_OBJFW
48 # include "ObjFWRT.h"
49 # else
50 # include <ObjFWRT/ObjFWRT.h>
51 # endif
52 #endif
53 #ifdef OF_APPLE_RUNTIME
54 # include <objc/objc.h>
55 # include <objc/runtime.h>
56 # include <objc/message.h>
57 #endif
58 
59 #if defined(__GNUC__)
60 # define restrict __restrict__
61 #elif __STDC_VERSION__ < 199901L
62 # define restrict
63 #endif
64 
65 #if __STDC_VERSION__ >= 201112L && !defined(static_assert)
66 /* C11 compiler, but old libc */
67 # define static_assert _Static_assert
68 #endif
69 
70 #if defined(OF_HAVE__THREAD_LOCAL)
71 # define OF_HAVE_COMPILER_TLS
72 # ifdef OF_HAVE_THREADS_H
73 # include <threads.h>
74 # ifdef OF_AIX
75 /* AIX has a bug where thread_local is defined to "Thread_local;". */
76 # undef thread_local
77 # define thread_local _Thread_local
78 # endif
79 # else
80 # define thread_local _Thread_local
81 # endif
82 #elif defined(OF_HAVE___THREAD)
83 # define OF_HAVE_COMPILER_TLS
84 # define thread_local __thread
85 #endif
86 
87 /*
88  * Do not use compiler TLS when targeting the iOS simulator, as the iOS 9
89  * simulator does not support it (fails at runtime).
90  */
91 #if defined(OF_HAVE_COMPILER_TLS) && defined(OF_IOS) && defined(OF_X86)
92 # undef OF_HAVE_COMPILER_TLS
93 #endif
94 
95 #ifdef __GNUC__
96 # define OF_INLINE inline __attribute__((__always_inline__))
97 # define OF_LIKELY(cond) (__builtin_expect(!!(cond), 1))
98 # define OF_UNLIKELY(cond) (__builtin_expect(!!(cond), 0))
99 # define OF_CONST_FUNC __attribute__((__const__))
100 # define OF_NO_RETURN_FUNC __attribute__((__noreturn__))
101 # define OF_WEAK_REF(sym) __attribute__((__weakref__(sym)))
102 # if defined(OF_ELF) || defined(OF_MACHO)
103 # define OF_VISIBILITY_HIDDEN __attribute__((__visibility__("hidden")))
104 # define OF_VISIBILITY_INTERNAL __attribute__((__visibility__("internal")))
105 # else
106 # define OF_VISIBILITY_HIDDEN
107 # define OF_VISIBILITY_INTERNAL
108 # endif
109 # define OF_MALLOC_FUNC __attribute__((__malloc__))
110 #else
111 # define OF_INLINE inline
112 # define OF_LIKELY(cond) (cond)
113 # define OF_UNLIKELY(cond) (cond)
114 # define OF_CONST_FUNC
115 # define OF_NO_RETURN_FUNC
116 # define OF_WEAK_REF(sym)
117 # define OF_VISIBILITY_HIDDEN
118 # define OF_VISIBILITY_INTERNAL
119 # define OF_MALLOC_FUNC
120 #endif
121 
122 #if __STDC_VERSION__ >= 201112L
123 # define OF_ALIGN(size) _Alignas(size)
124 # define OF_ALIGNOF(type) _Alignof(type)
125 # define OF_ALIGNAS(type) _Alignas(type)
126 #else
127 # define OF_ALIGN(size) __attribute__((__aligned__(size)))
128 # define OF_ALIGNOF(type) __alignof__(type)
129 # define OF_ALIGNAS(type) OF_ALIGN(OF_ALIGNOF(type))
130 #endif
131 
132 #ifdef __BIGGEST_ALIGNMENT__
133 # define OF_BIGGEST_ALIGNMENT __BIGGEST_ALIGNMENT__
134 #else
135 /* Hopefully no arch needs more than 16 byte alignment */
136 # define OF_BIGGEST_ALIGNMENT 16
137 #endif
138 /*
139  * We use SSE inline assembly on AMD64 and x86, so it must never be smaller
140  * than 16.
141  */
142 #if (defined(OF_AMD64) || defined(OF_X86)) && OF_BIGGEST_ALIGNMENT < 16
143 # undef OF_BIGGEST_ALIGNMENT
144 # define OF_BIGGEST_ALIGNMENT 16
145 #endif
146 
147 #define OF_PREPROCESSOR_CONCAT2(a, b) a##b
148 #define OF_PREPROCESSOR_CONCAT(a, b) OF_PREPROCESSOR_CONCAT2(a, b)
149 #define OF_PREPROCESSOR_STRINGIFY2(x) #x
150 #define OF_PREPROCESSOR_STRINGIFY(x) OF_PREPROCESSOR_STRINGIFY2(x)
151 
152 #if __OBJFW_RUNTIME_ABI__ || (defined(OF_APPLE_RUNTIME) && defined(__OBJC2__))
153 # define OF_HAVE_NONFRAGILE_IVARS
154 #endif
155 
156 #ifdef OF_HAVE_NONFRAGILE_IVARS
157 # define OF_RESERVE_IVARS(cls, num)
158 #else
159 # define OF_RESERVE_IVARS(cls, num) \
160  @private \
161  void *OF_PREPROCESSOR_CONCAT(_reserved_, cls)[num];
162 #endif
163 
164 #ifdef __GNUC__
165 # define OF_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
166 #else
167 # define OF_GCC_VERSION 0
168 #endif
169 
170 #define OF_STRINGIFY(s) OF_STRINGIFY2(s)
171 #define OF_STRINGIFY2(s) #s
172 
173 #ifndef __has_feature
174 # define __has_feature(x) 0
175 #endif
176 
177 #ifndef __has_attribute
178 # define __has_attribute(x) 0
179 #endif
180 
181 #if __has_feature(objc_bool)
182 # undef YES
183 # define YES __objc_yes
184 # undef NO
185 # define NO __objc_no
186 # ifndef __cplusplus
187 # undef true
188 # define true ((bool)1)
189 # undef false
190 # define false ((bool)0)
191 # endif
192 #endif
193 
194 #if !__has_feature(objc_instancetype)
195 # define instancetype id
196 #endif
197 
198 #if __has_feature(blocks)
199 # define OF_HAVE_BLOCKS
200 #endif
201 
202 #if __has_feature(objc_arc)
203 # define OF_RETURNS_RETAINED __attribute__((__ns_returns_retained__))
204 # define OF_RETURNS_NOT_RETAINED __attribute__((__ns_returns_not_retained__))
205 # define OF_RETURNS_INNER_POINTER \
206  __attribute__((__objc_returns_inner_pointer__))
207 # define OF_CONSUMED __attribute__((__ns_consumed__))
208 # define OF_WEAK_UNAVAILABLE __attribute__((__objc_arc_weak_unavailable__))
209 #else
210 # define OF_RETURNS_RETAINED
211 # define OF_RETURNS_NOT_RETAINED
212 # define OF_RETURNS_INNER_POINTER
213 # define OF_CONSUMED
214 # define OF_WEAK_UNAVAILABLE
215 /*
216  * undef them first, as new Clang versions have these as built-in defines even
217  * when ARC is disabled.
218  */
219 # undef __unsafe_unretained
220 # undef __bridge
221 # undef __autoreleasing
222 # define __unsafe_unretained
223 # define __bridge
224 # define __autoreleasing
225 #endif
226 
227 #if __has_feature(objc_generics)
228 # define OF_HAVE_GENERICS
229 # define OF_GENERIC(...) <__VA_ARGS__>
230 #else
231 # define OF_GENERIC(...)
232 #endif
233 
234 #if __has_feature(nullability)
235 # define OF_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
236 # define OF_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
237 # define OF_NULLABLE_PROPERTY(...) (__VA_ARGS__, nullable)
238 # define OF_NULL_RESETTABLE_PROPERTY(...) (__VA_ARGS__, null_resettable)
239 #else
240 # define OF_ASSUME_NONNULL_BEGIN
241 # define OF_ASSUME_NONNULL_END
242 # define _Nonnull
243 # define _Nullable
244 # define _Null_unspecified
245 # define OF_NULLABLE_PROPERTY
246 # define OF_NULL_RESETTABLE_PROPERTY
247 # define nonnull
248 # define nullable
249 # define null_unspecified
250 #endif
251 
252 #if __has_feature(objc_kindof)
253 # define OF_KINDOF(class_) __kindof class_
254 #else
255 # define OF_KINDOF(class_) id
256 #endif
257 
258 #if __has_feature(objc_class_property)
259 # define OF_HAVE_CLASS_PROPERTIES
260 #endif
261 
262 #if defined(__clang__) || OF_GCC_VERSION >= 405
263 # define OF_UNREACHABLE __builtin_unreachable();
264 #else
265 # define OF_UNREACHABLE abort();
266 #endif
267 
268 #if defined(__clang__) || OF_GCC_VERSION >= 406
269 # define OF_SENTINEL __attribute__((__sentinel__))
270 # define OF_NO_RETURN __attribute__((__noreturn__))
271 #else
272 # define OF_SENTINEL
273 # define OF_NO_RETURN
274 #endif
275 
276 #ifdef __clang__
277 # define OF_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
278 #else
279 # define OF_WARN_UNUSED_RESULT
280 #endif
281 
282 #if __has_attribute(__unavailable__)
283 # define OF_UNAVAILABLE __attribute__((__unavailable__))
284 #else
285 # define OF_UNAVAILABLE
286 #endif
287 
288 #if __has_attribute(__objc_requires_super__)
289 # define OF_REQUIRES_SUPER __attribute__((__objc_requires_super__))
290 #else
291 # define OF_REQUIRES_SUPER
292 #endif
293 
294 #if __has_attribute(__objc_root_class__)
295 # define OF_ROOT_CLASS __attribute__((__objc_root_class__))
296 #else
297 # define OF_ROOT_CLASS
298 #endif
299 
300 #if __has_attribute(__objc_subclassing_restricted__)
301 # define OF_SUBCLASSING_RESTRICTED \
302  __attribute__((__objc_subclassing_restricted__))
303 #else
304 # define OF_SUBCLASSING_RESTRICTED
305 #endif
306 
307 #if __has_attribute(__objc_method_family__)
308 # define OF_METHOD_FAMILY(f) __attribute__((__objc_method_family__(f)))
309 #else
310 # define OF_METHOD_FAMILY(f)
311 #endif
312 
313 #if __has_attribute(__objc_designated_initializer__)
314 # define OF_DESIGNATED_INITIALIZER \
315  __attribute__((__objc_designated_initializer__))
316 #else
317 # define OF_DESIGNATED_INITIALIZER
318 #endif
319 
320 #if defined(__clang__) || OF_GCC_VERSION >= 405
321 # define OF_DEPRECATED(project, major, minor, msg) \
322  __attribute__((__deprecated__("Deprecated in " #project " " \
323  #major "." #minor ": " msg)))
324 #elif defined(__GNUC__)
325 # define OF_DEPRECATED(project, major, minor, msg) \
326  __attribute__((__deprecated__))
327 #else
328 # define OF_DEPRECATED(project, major, minor, msg)
329 #endif
330 
331 #if __has_attribute(__objc_boxable__)
332 # define OF_BOXABLE __attribute__((__objc_boxable__))
333 #else
334 # define OF_BOXABLE
335 #endif
336 
337 #if __has_attribute(__swift_name__)
338 # define OF_SWIFT_NAME(name) __attribute__((__swift_name__(name)))
339 #else
340 # define OF_SWIFT_NAME(name)
341 #endif
342 
343 #if defined(OF_APPLE_RUNTIME) || (defined(OF_OBJFW_RUNTIME) && \
344  defined(__clang_major__) && __clang_major__ >= 21)
345 # if __has_attribute(__objc_direct__)
346 # define OF_DIRECT __attribute__((__objc_direct__))
347 # define OF_DIRECT_PROPERTY(...) (__VA_ARGS__, direct)
348 # endif
349 # if __has_attribute(__objc_direct_members__)
350 # define OF_DIRECT_MEMBERS __attribute__((__objc_direct_members__))
351 # endif
352 #endif
353 #ifndef OF_DIRECT
354 # define OF_DIRECT
355 #endif
356 #ifndef OF_DIRECT_PROPERTY
357 # define OF_DIRECT_PROPERTY
358 #endif
359 #ifndef OF_DIRECT_MEMBERS
360 # define OF_DIRECT_MEMBERS
361 #endif
362 
363 #ifdef OF_APPLE_RUNTIME
364 # if defined(OF_AMD64) || defined(OF_X86) || defined(OF_ARM64) || \
365  defined(OF_ARM) || defined(OF_POWERPC)
366 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
367 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
368 # endif
369 #else
370 # if defined(OF_ELF)
371 # if defined(OF_AMD64) || defined(OF_X86) || \
372  defined(OF_ARM64) || defined(OF_ARM) || \
373  defined(OF_POWERPC) || defined(OF_POWERPC64) || \
374  defined(OF_MIPS64_N64) || defined(OF_MIPS) || \
375  defined(OF_SPARC64) || defined(OF_SPARC) || \
376  defined(OF_RISCV64) || defined(OF_LOONGARCH64)
377 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
378 # if __OBJFW_RUNTIME_ABI__ >= 800
379 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
380 # endif
381 # endif
382 # elif defined(OF_MACH_O)
383 # if defined(OF_AMD64)
384 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
385 # if __OBJFW_RUNTIME_ABI__ >= 800
386 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
387 # endif
388 # endif
389 # elif defined(OF_WINDOWS)
390 # if defined(OF_AMD64) || defined(OF_X86) || defined(OF_ARM64)
391 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
392 # if __OBJFW_RUNTIME_ABI__ >= 800
393 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
394 # endif
395 # endif
396 # endif
397 #endif
398 
399 #define OFMaxRetainCount UINT_MAX
400 
401 #ifdef OBJC_COMPILING_RUNTIME
402 # define OFEnsure(cond) \
403  do { \
404  if OF_UNLIKELY (!(cond)) \
405  objc_error("ObjFWRT @ " __FILE__ ":" \
406  OF_STRINGIFY(__LINE__), \
407  "Failed to ensure condition:\n" #cond); \
408  } while(0)
409 #else
410 @class OFConstantString;
411 # ifdef __cplusplus
412 extern "C" {
413 # endif
414 extern void OFLog(OFConstantString *_Nonnull, ...);
415 # ifdef __cplusplus
416 }
417 # endif
418 # define OFEnsure(cond) \
419  do { \
420  if OF_UNLIKELY (!(cond)) { \
421  OFLog(@"Failed to ensure condition in " \
422  @__FILE__ ":%d: " @#cond, __LINE__); \
423  abort(); \
424  } \
425  } while (0)
426 #endif
427 
428 #ifndef NDEBUG
429 # define OFAssert(...) OFEnsure(__VA_ARGS__)
430 #else
431 # define OFAssert(...)
432 #endif
433 
434 #define OF_UNRECOGNIZED_SELECTOR OFMethodNotFound(self, _cmd);
435 #if __has_feature(objc_arc)
436 # define OF_INVALID_INIT_METHOD OFMethodNotFound(self, _cmd);
437 #else
438 # define OF_INVALID_INIT_METHOD \
439  @try { \
440  OFMethodNotFound(self, _cmd); \
441  } @catch (id e) { \
442  objc_release(self); \
443  @throw e; \
444  } \
445  \
446  abort();
447 #endif
448 #ifdef __clang__
449 # define OF_DEALLOC_UNSUPPORTED \
450  [self doesNotRecognizeSelector: _cmd]; \
451  \
452  abort(); \
453  \
454  _Pragma("clang diagnostic push"); \
455  _Pragma("clang diagnostic ignored \"-Wunreachable-code\""); \
456  [super dealloc]; /* Get rid of a stupid warning */ \
457  _Pragma("clang diagnostic pop");
458 #else
459 # define OF_DEALLOC_UNSUPPORTED \
460  [self doesNotRecognizeSelector: _cmd]; \
461  \
462  abort(); \
463  \
464  [super dealloc]; /* Get rid of a stupid warning */
465 #endif
466 #define OF_SINGLETON_METHODS \
467  - (instancetype)autorelease \
468  { \
469  return self; \
470  } \
471  \
472  - (instancetype)retain \
473  { \
474  return self; \
475  } \
476  \
477  - (void)release \
478  { \
479  } \
480  \
481  - (unsigned int)retainCount \
482  { \
483  return OFMaxRetainCount; \
484  } \
485  \
486  - (void)dealloc \
487  { \
488  OF_DEALLOC_UNSUPPORTED \
489  }
490 
491 #define OF_CONSTRUCTOR(prio) \
492  static void __attribute__((__constructor__(prio))) \
493  OF_PREPROCESSOR_CONCAT(constructor, __LINE__)(void)
494 #define OF_DESTRUCTOR(prio) \
495  static void __attribute__((__destructor__(prio))) \
496  OF_PREPROCESSOR_CONCAT(destructor, __LINE__)(void)
497 
498 static OF_INLINE uint16_t OF_CONST_FUNC
499 _OFByteSwap16Const(uint16_t i)
500 {
501  return (i & UINT16_C(0xFF00)) >> 8 | (i & UINT16_C(0x00FF)) << 8;
502 }
503 
504 static OF_INLINE uint32_t OF_CONST_FUNC
505 _OFByteSwap32Const(uint32_t i)
506 {
507  return (i & UINT32_C(0xFF000000)) >> 24 |
508  (i & UINT32_C(0x00FF0000)) >> 8 |
509  (i & UINT32_C(0x0000FF00)) << 8 |
510  (i & UINT32_C(0x000000FF)) << 24;
511 }
512 
513 static OF_INLINE uint64_t OF_CONST_FUNC
514 _OFByteSwap64Const(uint64_t i)
515 {
516  return (i & UINT64_C(0xFF00000000000000)) >> 56 |
517  (i & UINT64_C(0x00FF000000000000)) >> 40 |
518  (i & UINT64_C(0x0000FF0000000000)) >> 24 |
519  (i & UINT64_C(0x000000FF00000000)) >> 8 |
520  (i & UINT64_C(0x00000000FF000000)) << 8 |
521  (i & UINT64_C(0x0000000000FF0000)) << 24 |
522  (i & UINT64_C(0x000000000000FF00)) << 40 |
523  (i & UINT64_C(0x00000000000000FF)) << 56;
524 }
525 
526 static OF_INLINE uint16_t OF_CONST_FUNC
527 _OFByteSwap16NonConst(uint16_t i)
528 {
529 #if defined(OF_HAVE_BUILTIN_BSWAP16)
530  return __builtin_bswap16(i);
531 #elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
532  __asm__ (
533  "xchg{b} { %h0, %b0 | %b0, %h0 }"
534  : "=Q" (i)
535  : "0" (i)
536  );
537 #elif defined(OF_POWERPC) && defined(__GNUC__)
538  __asm__ (
539  "lhbrx %0, 0, %1"
540  : "=r" (i)
541  : "r" (&i),
542  "m" (i)
543  );
544 #elif defined(OF_ARMV6) && defined(__GNUC__)
545  __asm__ (
546  "rev16 %0, %0"
547  : "=r" (i)
548  : "0" (i)
549  );
550 #else
551  i = (i & UINT16_C(0xFF00)) >> 8 |
552  (i & UINT16_C(0x00FF)) << 8;
553 #endif
554  return i;
555 }
556 
557 static OF_INLINE uint32_t OF_CONST_FUNC
558 _OFByteSwap32NonConst(uint32_t i)
559 {
560 #if defined(OF_HAVE_BUILTIN_BSWAP32)
561  return __builtin_bswap32(i);
562 #elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
563  __asm__ (
564  "bswap %0"
565  : "=q" (i)
566  : "0" (i)
567  );
568 #elif defined(OF_POWERPC) && defined(__GNUC__)
569  __asm__ (
570  "lwbrx %0, 0, %1"
571  : "=r" (i)
572  : "r" (&i),
573  "m" (i)
574  );
575 #elif defined(OF_ARMV6) && defined(__GNUC__)
576  __asm__ (
577  "rev %0, %0"
578  : "=r" (i)
579  : "0" (i)
580  );
581 #else
582  i = (i & UINT32_C(0xFF000000)) >> 24 |
583  (i & UINT32_C(0x00FF0000)) >> 8 |
584  (i & UINT32_C(0x0000FF00)) << 8 |
585  (i & UINT32_C(0x000000FF)) << 24;
586 #endif
587  return i;
588 }
589 
590 static OF_INLINE uint64_t OF_CONST_FUNC
591 _OFByteSwap64NonConst(uint64_t i)
592 {
593 #if defined(OF_HAVE_BUILTIN_BSWAP64)
594  return __builtin_bswap64(i);
595 #elif defined(OF_AMD64) && defined(__GNUC__)
596  __asm__ (
597  "bswap %0"
598  : "=r" (i)
599  : "0" (i)
600  );
601 #elif defined(OF_X86) && defined(__GNUC__)
602  __asm__ (
603  "bswap {%%}eax\n\t"
604  "bswap {%%}edx\n\t"
605  "xchg{l} { %%eax, %%edx | edx, eax }"
606  : "=A" (i)
607  : "0" (i)
608  );
609 #else
610  i = (uint64_t)_OFByteSwap32NonConst(
611  (uint32_t)(i & UINT32_C(0xFFFFFFFF))) << 32 |
612  _OFByteSwap32NonConst((uint32_t)(i >> 32));
613 #endif
614  return i;
615 }
616 
617 #if defined(__GNUC__) || defined(DOXYGEN)
624 # define OFByteSwap16(i) \
625  (__builtin_constant_p(i) ? _OFByteSwap16Const(i) : _OFByteSwap16NonConst(i))
626 
633 # define OFByteSwap32(i) \
634  (__builtin_constant_p(i) ? _OFByteSwap32Const(i) : _OFByteSwap32NonConst(i))
635 
642 # define OFByteSwap64(i) \
643  (__builtin_constant_p(i) ? _OFByteSwap64Const(i) : _OFByteSwap64NonConst(i))
644 #else
645 # define OFByteSwap16(i) _OFByteSwap16Const(i)
646 # define OFByteSwap32(i) _OFByteSwap32Const(i)
647 # define OFByteSwap64(i) _OFByteSwap64Const(i)
648 #endif
649 
656 static OF_INLINE uint32_t OF_CONST_FUNC
658 {
659  uint32_t ret;
660  memcpy(&ret, &f, 4);
661  return ret;
662 }
663 
670 static OF_INLINE float OF_CONST_FUNC
672 {
673  float ret;
674  memcpy(&ret, &uInt32, 4);
675  return ret;
676 }
677 
684 static OF_INLINE uint64_t OF_CONST_FUNC
686 {
687  uint64_t ret;
688  memcpy(&ret, &d, 8);
689  return ret;
690 }
691 
698 static OF_INLINE double OF_CONST_FUNC
700 {
701  double ret;
702  memcpy(&ret, &uInt64, 8);
703  return ret;
704 }
705 
712 static OF_INLINE float OF_CONST_FUNC
714 {
717 }
718 
725 static OF_INLINE double OF_CONST_FUNC
727 {
730 }
731 
732 #if defined(OF_BIG_ENDIAN) || defined(DOXYGEN)
740 # define OFFromBigEndian16(i) (i)
741 
749 # define OFFromBigEndian32(i) (i)
750 
758 # define OFFromBigEndian64(i) (i)
759 
767 # define OFFromLittleEndian16(i) OFByteSwap16(i)
768 
776 # define OFFromLittleEndian32(i) OFByteSwap32(i)
777 
785 # define OFFromLittleEndian64(i) OFByteSwap64(i)
786 
794 # define OFToBigEndian16(i) (i)
795 
803 # define OFToBigEndian32(i) (i)
804 
812 # define OFToBigEndian64(i) (i)
813 
821 # define OFToLittleEndian16(i) OFByteSwap16(i)
822 
830 # define OFToLittleEndian32(i) OFByteSwap32(i)
831 
839 # define OFToLittleEndian64(i) OFByteSwap64(i)
840 #else
841 # define OFFromBigEndian16(i) OFByteSwap16(i)
842 # define OFFromBigEndian32(i) OFByteSwap32(i)
843 # define OFFromBigEndian64(i) OFByteSwap64(i)
844 # define OFFromLittleEndian16(i) (i)
845 # define OFFromLittleEndian32(i) (i)
846 # define OFFromLittleEndian64(i) (i)
847 # define OFToBigEndian16(i) OFByteSwap16(i)
848 # define OFToBigEndian32(i) OFByteSwap32(i)
849 # define OFToBigEndian64(i) OFByteSwap64(i)
850 # define OFToLittleEndian16(i) (i)
851 # define OFToLittleEndian32(i) (i)
852 # define OFToLittleEndian64(i) (i)
853 #endif
854 
855 #if defined(OF_FLOAT_BIG_ENDIAN) || defined(DOXYGEN)
862 # define OFFromBigEndianFloat(f) (f)
863 
870 # define OFFromBigEndianDouble(d) (d)
871 
878 # define OFFromLittleEndianFloat(f) OFByteSwapFloat(f)
879 
886 # define OFFromLittleEndianDouble(d) OFByteSwapDouble(d)
887 
894 # define OFToBigEndianFloat(f) (f)
895 
902 # define OFToBigEndianDouble(d) (d)
903 
910 # define OFToLittleEndianFloat(f) OFByteSwapFloat(f)
911 
918 # define OFToLittleEndianDouble(d) OFByteSwapDouble(d)
919 #else
920 # define OFFromBigEndianFloat(f) OFByteSwapFloat(f)
921 # define OFFromBigEndianDouble(d) OFByteSwapDouble(d)
922 # define OFFromLittleEndianFloat(f) (f)
923 # define OFFromLittleEndianDouble(d) (d)
924 # define OFToBigEndianFloat(f) OFByteSwapFloat(f)
925 # define OFToBigEndianDouble(d) OFByteSwapDouble(d)
926 # define OFToLittleEndianFloat(f) (f)
927 # define OFToLittleEndianDouble(d) (d)
928 #endif
929 
937 #define OFRotateLeft(value, bits) \
938  (((bits) % (sizeof(value) * 8)) > 0 \
939  ? ((value) << ((bits) % (sizeof(value) * 8))) | \
940  ((value) >> (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \
941  : (value))
942 
950 #define OFRotateRight(value, bits) \
951  (((bits) % (sizeof(value) * 8)) > 0 \
952  ? ((value) >> ((bits) % (sizeof(value) * 8))) | \
953  ((value) << (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \
954  : (value))
955 
963 #define OFRoundUpToPowerOf2(pow2, value) \
964  (((value) + (pow2) - 1) & ~((pow2) - 1))
965 
966 #define OF_ULONG_BIT (sizeof(unsigned long) * CHAR_BIT)
967 
968 static OF_INLINE bool
969 OFBitSetIsSet(unsigned long *_Nonnull storage, size_t idx)
970 {
971  return storage[idx / OF_ULONG_BIT] & (1ul << (idx % OF_ULONG_BIT));
972 }
973 
974 static OF_INLINE void
975 OFBitSetSet(unsigned long *_Nonnull storage, size_t idx)
976 {
977  storage[idx / OF_ULONG_BIT] |= (1ul << (idx % OF_ULONG_BIT));
978 }
979 
980 static OF_INLINE void
981 OFBitSetClear(unsigned long *_Nonnull storage, size_t idx)
982 {
983  storage[idx / OF_ULONG_BIT] &= ~(1ul << (idx % OF_ULONG_BIT));
984 }
985 
986 static OF_INLINE void
987 OFZeroMemory(void *_Nonnull buffer_, size_t length)
988 {
989  volatile unsigned char *buffer = (volatile unsigned char *)buffer_;
990 
991  while (buffer < (unsigned char *)buffer_ + length)
992  *buffer++ = '\0';
993 }
994 
995 static OF_INLINE bool
996 OFASCIIIsAlpha(char c)
997 {
998  return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
999 }
1000 
1001 static OF_INLINE bool
1002 OFASCIIIsDigit(char c)
1003 {
1004  return (c >= '0' && c <= '9');
1005 }
1006 
1007 static OF_INLINE bool
1008 OFASCIIIsAlnum(char c)
1009 {
1010  return (OFASCIIIsAlpha(c) || OFASCIIIsDigit(c));
1011 }
1012 
1013 static OF_INLINE bool
1014 OFASCIIIsSpace(char c)
1015 {
1016  return (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' ||
1017  c == '\v');
1018 }
1019 
1020 static OF_INLINE char
1021 OFASCIIToUpper(char c)
1022 {
1023  return (c >= 'a' && c <= 'z' ? 'A' + (c - 'a') : c);
1024 }
1025 
1026 static OF_INLINE char
1027 OFASCIIToLower(char c)
1028 {
1029  return (c >= 'A' && c <= 'Z' ? 'a' + (c - 'A') : c);
1030 }
1031 #endif
void OFLog(OFConstantString *format,...)
Logs the specified printf-style format to OFStdErr.
Definition: OFStdIOStream.m:115
A class for storing constant strings using the @"" literal.
Definition: OFConstantString.h:42
static OF_INLINE float OF_CONST_FUNC OFBitConvertUInt32ToFloat(uint32_t uInt32)
Bit-converts the specified uint32_t to a float.
Definition: macros.h:671
#define OFByteSwap32(i)
Byte swaps the specified 32 bit integer.
Definition: macros.h:633
static OF_INLINE double OF_CONST_FUNC OFBitConvertUInt64ToDouble(uint64_t uInt64)
Bit-converts the specified uint64_t to a double.
Definition: macros.h:699
#define OFByteSwap64(i)
Byte swaps the specified 64 bit integer.
Definition: macros.h:642
static OF_INLINE double OF_CONST_FUNC OFByteSwapDouble(double d)
Byte swaps the specified double.
Definition: macros.h:726
static OF_INLINE float OF_CONST_FUNC OFByteSwapFloat(float f)
Byte swaps the specified float.
Definition: macros.h:713
static OF_INLINE uint64_t OF_CONST_FUNC OFBitConvertDoubleToUInt64(double d)
Bit-converts the specified double to a uint64_t.
Definition: macros.h:685
static OF_INLINE uint32_t OF_CONST_FUNC OFBitConvertFloatToUInt32(float f)
Bit-converts the specified float to a uint32_t.
Definition: macros.h:657