ObjFW
macros.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2024 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 #else
103 # define OF_INLINE inline
104 # define OF_LIKELY(cond) (cond)
105 # define OF_UNLIKELY(cond) (cond)
106 # define OF_CONST_FUNC
107 # define OF_NO_RETURN_FUNC
108 # define OF_WEAK_REF(sym)
109 #endif
110 
111 #ifndef OF_DJGPP
112 # define OF_VISIBILITY_HIDDEN __attribute__((__visibility__("hidden")))
113 #else
114 # define OF_VISIBILITY_HIDDEN
115 #endif
116 
117 #if __STDC_VERSION__ >= 201112L
118 # define OF_ALIGN(size) _Alignas(size)
119 # define OF_ALIGNOF(type) _Alignof(type)
120 # define OF_ALIGNAS(type) _Alignas(type)
121 #else
122 # define OF_ALIGN(size) __attribute__((__aligned__(size)))
123 # define OF_ALIGNOF(type) __alignof__(type)
124 # define OF_ALIGNAS(type) OF_ALIGN(OF_ALIGNOF(type))
125 #endif
126 
127 #ifdef __BIGGEST_ALIGNMENT__
128 # define OF_BIGGEST_ALIGNMENT __BIGGEST_ALIGNMENT__
129 #else
130 /* Hopefully no arch needs more than 16 byte alignment */
131 # define OF_BIGGEST_ALIGNMENT 16
132 #endif
133 /*
134  * We use SSE inline assembly on AMD64 and x86, so it must never be smaller
135  * than 16.
136  */
137 #if (defined(OF_AMD64) || defined(OF_X86)) && OF_BIGGEST_ALIGNMENT < 16
138 # undef OF_BIGGEST_ALIGNMENT
139 # define OF_BIGGEST_ALIGNMENT 16
140 #endif
141 
142 #define OF_PREPROCESSOR_CONCAT2(a, b) a##b
143 #define OF_PREPROCESSOR_CONCAT(a, b) OF_PREPROCESSOR_CONCAT2(a, b)
144 
145 #if __OBJFW_RUNTIME_ABI__ || (defined(OF_APPLE_RUNTIME) && defined(__OBJC2__))
146 # define OF_HAVE_NONFRAGILE_IVARS
147 #endif
148 
149 #ifdef OF_HAVE_NONFRAGILE_IVARS
150 # define OF_RESERVE_IVARS(cls, num)
151 #else
152 # define OF_RESERVE_IVARS(cls, num) \
153  @private \
154  void *OF_PREPROCESSOR_CONCAT(_reserved_, cls)[num];
155 #endif
156 
157 #ifdef __GNUC__
158 # define OF_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
159 #else
160 # define OF_GCC_VERSION 0
161 #endif
162 
163 #define OF_STRINGIFY(s) OF_STRINGIFY2(s)
164 #define OF_STRINGIFY2(s) #s
165 
166 #ifndef __has_feature
167 # define __has_feature(x) 0
168 #endif
169 
170 #ifndef __has_attribute
171 # define __has_attribute(x) 0
172 #endif
173 
174 #if __has_feature(objc_bool)
175 # undef YES
176 # define YES __objc_yes
177 # undef NO
178 # define NO __objc_no
179 # ifndef __cplusplus
180 # undef true
181 # define true ((bool)1)
182 # undef false
183 # define false ((bool)0)
184 # endif
185 #endif
186 
187 #if !__has_feature(objc_instancetype)
188 # define instancetype id
189 #endif
190 
191 #if __has_feature(blocks)
192 # define OF_HAVE_BLOCKS
193 #endif
194 
195 #if __has_feature(objc_arc)
196 # define OF_RETURNS_RETAINED __attribute__((__ns_returns_retained__))
197 # define OF_RETURNS_NOT_RETAINED __attribute__((__ns_returns_not_retained__))
198 # define OF_RETURNS_INNER_POINTER \
199  __attribute__((__objc_returns_inner_pointer__))
200 # define OF_CONSUMED __attribute__((__ns_consumed__))
201 # define OF_WEAK_UNAVAILABLE __attribute__((__objc_arc_weak_unavailable__))
202 #else
203 # define OF_RETURNS_RETAINED
204 # define OF_RETURNS_NOT_RETAINED
205 # define OF_RETURNS_INNER_POINTER
206 # define OF_CONSUMED
207 # define OF_WEAK_UNAVAILABLE
208 /*
209  * undef them first, as new Clang versions have these as built-in defines even
210  * when ARC is disabled.
211  */
212 # undef __unsafe_unretained
213 # undef __bridge
214 # undef __autoreleasing
215 # define __unsafe_unretained
216 # define __bridge
217 # define __autoreleasing
218 #endif
219 
220 #if __has_feature(objc_generics)
221 # define OF_HAVE_GENERICS
222 # define OF_GENERIC(...) <__VA_ARGS__>
223 #else
224 # define OF_GENERIC(...)
225 #endif
226 
227 #if __has_feature(nullability)
228 # define OF_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
229 # define OF_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
230 # define OF_NULLABLE_PROPERTY(...) (__VA_ARGS__, nullable)
231 # define OF_NULL_RESETTABLE_PROPERTY(...) (__VA_ARGS__, null_resettable)
232 #else
233 # define OF_ASSUME_NONNULL_BEGIN
234 # define OF_ASSUME_NONNULL_END
235 # define _Nonnull
236 # define _Nullable
237 # define _Null_unspecified
238 # define OF_NULLABLE_PROPERTY
239 # define OF_NULL_RESETTABLE_PROPERTY
240 # define nonnull
241 # define nullable
242 # define null_unspecified
243 #endif
244 
245 #if __has_feature(objc_kindof)
246 # define OF_KINDOF(class_) __kindof class_
247 #else
248 # define OF_KINDOF(class_) id
249 #endif
250 
251 #if __has_feature(objc_class_property)
252 # define OF_HAVE_CLASS_PROPERTIES
253 #endif
254 
255 #if defined(__clang__) || OF_GCC_VERSION >= 405
256 # define OF_UNREACHABLE __builtin_unreachable();
257 #else
258 # define OF_UNREACHABLE abort();
259 #endif
260 
261 #if defined(__clang__) || OF_GCC_VERSION >= 406
262 # define OF_SENTINEL __attribute__((__sentinel__))
263 # define OF_NO_RETURN __attribute__((__noreturn__))
264 #else
265 # define OF_SENTINEL
266 # define OF_NO_RETURN
267 #endif
268 
269 #ifdef __clang__
270 # define OF_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
271 #else
272 # define OF_WARN_UNUSED_RESULT
273 #endif
274 
275 #if __has_attribute(__unavailable__)
276 # define OF_UNAVAILABLE __attribute__((__unavailable__))
277 #else
278 # define OF_UNAVAILABLE
279 #endif
280 
281 #if __has_attribute(__objc_requires_super__)
282 # define OF_REQUIRES_SUPER __attribute__((__objc_requires_super__))
283 #else
284 # define OF_REQUIRES_SUPER
285 #endif
286 
287 #if __has_attribute(__objc_root_class__)
288 # define OF_ROOT_CLASS __attribute__((__objc_root_class__))
289 #else
290 # define OF_ROOT_CLASS
291 #endif
292 
293 #if __has_attribute(__objc_subclassing_restricted__)
294 # define OF_SUBCLASSING_RESTRICTED \
295  __attribute__((__objc_subclassing_restricted__))
296 #else
297 # define OF_SUBCLASSING_RESTRICTED
298 #endif
299 
300 #if __has_attribute(__objc_method_family__)
301 # define OF_METHOD_FAMILY(f) __attribute__((__objc_method_family__(f)))
302 #else
303 # define OF_METHOD_FAMILY(f)
304 #endif
305 
306 #if __has_attribute(__objc_designated_initializer__)
307 # define OF_DESIGNATED_INITIALIZER \
308  __attribute__((__objc_designated_initializer__))
309 #else
310 # define OF_DESIGNATED_INITIALIZER
311 #endif
312 
313 #if defined(__clang__) || OF_GCC_VERSION >= 405
314 # define OF_DEPRECATED(project, major, minor, msg) \
315  __attribute__((__deprecated__("Deprecated in " #project " " \
316  #major "." #minor ": " msg)))
317 #elif defined(__GNUC__)
318 # define OF_DEPRECATED(project, major, minor, msg) \
319  __attribute__((__deprecated__))
320 #else
321 # define OF_DEPRECATED(project, major, minor, msg)
322 #endif
323 
324 #if __has_attribute(__objc_boxable__)
325 # define OF_BOXABLE __attribute__((__objc_boxable__))
326 #else
327 # define OF_BOXABLE
328 #endif
329 
330 #if __has_attribute(__swift_name__)
331 # define OF_SWIFT_NAME(name) __attribute__((__swift_name__(name)))
332 #else
333 # define OF_SWIFT_NAME(name)
334 #endif
335 
336 #if __has_attribute(__objc_direct__) && defined(OF_APPLE_RUNTIME)
337 # define OF_DIRECT __attribute__((__objc_direct__))
338 #else
339 # define OF_DIRECT
340 #endif
341 #if __has_attribute(__objc_direct_members__) && defined(OF_APPLE_RUNTIME)
342 # define OF_DIRECT_MEMBERS __attribute__((__objc_direct_members__))
343 #else
344 # define OF_DIRECT_MEMBERS
345 #endif
346 
347 #ifdef OF_APPLE_RUNTIME
348 # if defined(OF_AMD64) || defined(OF_X86) || defined(OF_ARM64) || \
349  defined(OF_ARM) || defined(OF_POWERPC)
350 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
351 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
352 # endif
353 #else
354 # if defined(OF_ELF)
355 # if defined(OF_AMD64) || defined(OF_X86) || \
356  defined(OF_ARM64) || defined(OF_ARM) || \
357  defined(OF_POWERPC) || defined(OF_POWERPC64) || \
358  defined(OF_MIPS64_N64) || defined(OF_MIPS) || \
359  defined(OF_SPARC64) || defined(OF_SPARC) || \
360  defined(OF_RISCV64) || defined(OF_LOONGARCH64)
361 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
362 # if __OBJFW_RUNTIME_ABI__ >= 800
363 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
364 # endif
365 # endif
366 # elif defined(OF_MACH_O)
367 # if defined(OF_AMD64)
368 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
369 # if __OBJFW_RUNTIME_ABI__ >= 800
370 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
371 # endif
372 # endif
373 # elif defined(OF_WINDOWS)
374 # if defined(OF_AMD64) || defined(OF_X86) || defined(OF_ARM64)
375 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
376 # if __OBJFW_RUNTIME_ABI__ >= 800
377 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
378 # endif
379 # endif
380 # endif
381 #endif
382 
383 #define OFMaxRetainCount UINT_MAX
384 
385 #ifdef OBJC_COMPILING_RUNTIME
386 # define OFEnsure(cond) \
387  do { \
388  if OF_UNLIKELY (!(cond)) \
389  objc_error("ObjFWRT @ " __FILE__ ":" \
390  OF_STRINGIFY(__LINE__), \
391  "Failed to ensure condition:\n" #cond); \
392  } while(0)
393 #else
394 @class OFConstantString;
395 # ifdef __cplusplus
396 extern "C" {
397 # endif
398 extern void OFLog(OFConstantString *_Nonnull, ...);
399 # ifdef __cplusplus
400 }
401 # endif
402 # define OFEnsure(cond) \
403  do { \
404  if OF_UNLIKELY (!(cond)) { \
405  OFLog(@"Failed to ensure condition in " \
406  @__FILE__ ":%d: " @#cond, __LINE__); \
407  abort(); \
408  } \
409  } while (0)
410 #endif
411 
412 #ifndef NDEBUG
413 # define OFAssert(...) OFEnsure(__VA_ARGS__)
414 #else
415 # define OFAssert(...)
416 #endif
417 
418 #define OF_UNRECOGNIZED_SELECTOR OFMethodNotFound(self, _cmd);
419 #if __has_feature(objc_arc)
420 # define OF_INVALID_INIT_METHOD OFMethodNotFound(self, _cmd);
421 #else
422 # define OF_INVALID_INIT_METHOD \
423  @try { \
424  OFMethodNotFound(self, _cmd); \
425  } @catch (id e) { \
426  [self release]; \
427  @throw e; \
428  } \
429  \
430  abort();
431 #endif
432 #ifdef __clang__
433 # define OF_DEALLOC_UNSUPPORTED \
434  [self doesNotRecognizeSelector: _cmd]; \
435  \
436  abort(); \
437  \
438  _Pragma("clang diagnostic push"); \
439  _Pragma("clang diagnostic ignored \"-Wunreachable-code\""); \
440  [super dealloc]; /* Get rid of a stupid warning */ \
441  _Pragma("clang diagnostic pop");
442 #else
443 # define OF_DEALLOC_UNSUPPORTED \
444  [self doesNotRecognizeSelector: _cmd]; \
445  \
446  abort(); \
447  \
448  [super dealloc]; /* Get rid of a stupid warning */
449 #endif
450 #define OF_SINGLETON_METHODS \
451  - (instancetype)autorelease \
452  { \
453  return self; \
454  } \
455  \
456  - (instancetype)retain \
457  { \
458  return self; \
459  } \
460  \
461  - (void)release \
462  { \
463  } \
464  \
465  - (unsigned int)retainCount \
466  { \
467  return OFMaxRetainCount; \
468  } \
469  \
470  - (void)dealloc \
471  { \
472  OF_DEALLOC_UNSUPPORTED \
473  }
474 
475 #define OF_CONSTRUCTOR(prio) \
476  static void __attribute__((__constructor__(prio))) \
477  OF_PREPROCESSOR_CONCAT(constructor, __LINE__)(void)
478 #define OF_DESTRUCTOR(prio) \
479  static void __attribute__((__destructor__(prio))) \
480  OF_PREPROCESSOR_CONCAT(destructor, __LINE__)(void)
481 
482 static OF_INLINE uint16_t OF_CONST_FUNC
483 _OFByteSwap16Const(uint16_t i)
484 {
485  return (i & UINT16_C(0xFF00)) >> 8 | (i & UINT16_C(0x00FF)) << 8;
486 }
487 
488 static OF_INLINE uint32_t OF_CONST_FUNC
489 _OFByteSwap32Const(uint32_t i)
490 {
491  return (i & UINT32_C(0xFF000000)) >> 24 |
492  (i & UINT32_C(0x00FF0000)) >> 8 |
493  (i & UINT32_C(0x0000FF00)) << 8 |
494  (i & UINT32_C(0x000000FF)) << 24;
495 }
496 
497 static OF_INLINE uint64_t OF_CONST_FUNC
498 _OFByteSwap64Const(uint64_t i)
499 {
500  return (i & UINT64_C(0xFF00000000000000)) >> 56 |
501  (i & UINT64_C(0x00FF000000000000)) >> 40 |
502  (i & UINT64_C(0x0000FF0000000000)) >> 24 |
503  (i & UINT64_C(0x000000FF00000000)) >> 8 |
504  (i & UINT64_C(0x00000000FF000000)) << 8 |
505  (i & UINT64_C(0x0000000000FF0000)) << 24 |
506  (i & UINT64_C(0x000000000000FF00)) << 40 |
507  (i & UINT64_C(0x00000000000000FF)) << 56;
508 }
509 
510 static OF_INLINE uint16_t OF_CONST_FUNC
511 _OFByteSwap16NonConst(uint16_t i)
512 {
513 #if defined(OF_HAVE_BUILTIN_BSWAP16)
514  return __builtin_bswap16(i);
515 #elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
516  __asm__ (
517  "xchg{b} { %h0, %b0 | %b0, %h0 }"
518  : "=Q" (i)
519  : "0" (i)
520  );
521 #elif defined(OF_POWERPC) && defined(__GNUC__)
522  __asm__ (
523  "lhbrx %0, 0, %1"
524  : "=r" (i)
525  : "r" (&i),
526  "m" (i)
527  );
528 #elif defined(OF_ARMV6) && defined(__GNUC__)
529  __asm__ (
530  "rev16 %0, %0"
531  : "=r" (i)
532  : "0" (i)
533  );
534 #else
535  i = (i & UINT16_C(0xFF00)) >> 8 |
536  (i & UINT16_C(0x00FF)) << 8;
537 #endif
538  return i;
539 }
540 
541 static OF_INLINE uint32_t OF_CONST_FUNC
542 _OFByteSwap32NonConst(uint32_t i)
543 {
544 #if defined(OF_HAVE_BUILTIN_BSWAP32)
545  return __builtin_bswap32(i);
546 #elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
547  __asm__ (
548  "bswap %0"
549  : "=q" (i)
550  : "0" (i)
551  );
552 #elif defined(OF_POWERPC) && defined(__GNUC__)
553  __asm__ (
554  "lwbrx %0, 0, %1"
555  : "=r" (i)
556  : "r" (&i),
557  "m" (i)
558  );
559 #elif defined(OF_ARMV6) && defined(__GNUC__)
560  __asm__ (
561  "rev %0, %0"
562  : "=r" (i)
563  : "0" (i)
564  );
565 #else
566  i = (i & UINT32_C(0xFF000000)) >> 24 |
567  (i & UINT32_C(0x00FF0000)) >> 8 |
568  (i & UINT32_C(0x0000FF00)) << 8 |
569  (i & UINT32_C(0x000000FF)) << 24;
570 #endif
571  return i;
572 }
573 
574 static OF_INLINE uint64_t OF_CONST_FUNC
575 _OFByteSwap64NonConst(uint64_t i)
576 {
577 #if defined(OF_HAVE_BUILTIN_BSWAP64)
578  return __builtin_bswap64(i);
579 #elif defined(OF_AMD64) && defined(__GNUC__)
580  __asm__ (
581  "bswap %0"
582  : "=r" (i)
583  : "0" (i)
584  );
585 #elif defined(OF_X86) && defined(__GNUC__)
586  __asm__ (
587  "bswap {%%}eax\n\t"
588  "bswap {%%}edx\n\t"
589  "xchg{l} { %%eax, %%edx | edx, eax }"
590  : "=A" (i)
591  : "0" (i)
592  );
593 #else
594  i = (uint64_t)_OFByteSwap32NonConst(
595  (uint32_t)(i & UINT32_C(0xFFFFFFFF))) << 32 |
596  _OFByteSwap32NonConst((uint32_t)(i >> 32));
597 #endif
598  return i;
599 }
600 
601 #if defined(__GNUC__) || defined(DOXYGEN)
602 
608 # define OFByteSwap16(i) \
609  (__builtin_constant_p(i) ? _OFByteSwap16Const(i) : _OFByteSwap16NonConst(i))
610 
617 # define OFByteSwap32(i) \
618  (__builtin_constant_p(i) ? _OFByteSwap32Const(i) : _OFByteSwap32NonConst(i))
619 
626 # define OFByteSwap64(i) \
627  (__builtin_constant_p(i) ? _OFByteSwap64Const(i) : _OFByteSwap64NonConst(i))
628 #else
629 # define OFByteSwap16(i) _OFByteSwap16Const(i)
630 # define OFByteSwap32(i) _OFByteSwap32Const(i)
631 # define OFByteSwap64(i) _OFByteSwap64Const(i)
632 #endif
633 
640 static OF_INLINE uint32_t OF_CONST_FUNC
642 {
643  uint32_t ret;
644  memcpy(&ret, &f, 4);
645  return ret;
646 }
647 
654 static OF_INLINE float OF_CONST_FUNC
656 {
657  float ret;
658  memcpy(&ret, &uInt32, 4);
659  return ret;
660 }
661 
668 static OF_INLINE uint64_t OF_CONST_FUNC
670 {
671  uint64_t ret;
672  memcpy(&ret, &d, 8);
673  return ret;
674 }
675 
682 static OF_INLINE double OF_CONST_FUNC
684 {
685  double ret;
686  memcpy(&ret, &uInt64, 8);
687  return ret;
688 }
689 
696 static OF_INLINE float OF_CONST_FUNC
698 {
701 }
702 
709 static OF_INLINE double OF_CONST_FUNC
711 {
714 }
715 
716 #if defined(OF_BIG_ENDIAN) || defined(DOXYGEN)
717 
724 # define OFFromBigEndian16(i) (i)
725 
733 # define OFFromBigEndian32(i) (i)
734 
742 # define OFFromBigEndian64(i) (i)
743 
751 # define OFFromLittleEndian16(i) OFByteSwap16(i)
752 
760 # define OFFromLittleEndian32(i) OFByteSwap32(i)
761 
769 # define OFFromLittleEndian64(i) OFByteSwap64(i)
770 
778 # define OFToBigEndian16(i) (i)
779 
787 # define OFToBigEndian32(i) (i)
788 
796 # define OFToBigEndian64(i) (i)
797 
805 # define OFToLittleEndian16(i) OFByteSwap16(i)
806 
814 # define OFToLittleEndian32(i) OFByteSwap32(i)
815 
823 # define OFToLittleEndian64(i) OFByteSwap64(i)
824 #else
825 # define OFFromBigEndian16(i) OFByteSwap16(i)
826 # define OFFromBigEndian32(i) OFByteSwap32(i)
827 # define OFFromBigEndian64(i) OFByteSwap64(i)
828 # define OFFromLittleEndian16(i) (i)
829 # define OFFromLittleEndian32(i) (i)
830 # define OFFromLittleEndian64(i) (i)
831 # define OFToBigEndian16(i) OFByteSwap16(i)
832 # define OFToBigEndian32(i) OFByteSwap32(i)
833 # define OFToBigEndian64(i) OFByteSwap64(i)
834 # define OFToLittleEndian16(i) (i)
835 # define OFToLittleEndian32(i) (i)
836 # define OFToLittleEndian64(i) (i)
837 #endif
838 
839 #if defined(OF_FLOAT_BIG_ENDIAN) || defined(DOXYGEN)
840 
846 # define OFFromBigEndianFloat(f) (f)
847 
854 # define OFFromBigEndianDouble(d) (d)
855 
862 # define OFFromLittleEndianFloat(f) OFByteSwapFloat(f)
863 
870 # define OFFromLittleEndianDouble(d) OFByteSwapDouble(d)
871 
878 # define OFToBigEndianFloat(f) (f)
879 
886 # define OFToBigEndianDouble(d) (d)
887 
894 # define OFToLittleEndianFloat(f) OFByteSwapFloat(f)
895 
902 # define OFToLittleEndianDouble(d) OFByteSwapDouble(d)
903 #else
904 # define OFFromBigEndianFloat(f) OFByteSwapFloat(f)
905 # define OFFromBigEndianDouble(d) OFByteSwapDouble(d)
906 # define OFFromLittleEndianFloat(f) (f)
907 # define OFFromLittleEndianDouble(d) (d)
908 # define OFToBigEndianFloat(f) OFByteSwapFloat(f)
909 # define OFToBigEndianDouble(d) OFByteSwapDouble(d)
910 # define OFToLittleEndianFloat(f) (f)
911 # define OFToLittleEndianDouble(d) (d)
912 #endif
913 
921 #define OFRotateLeft(value, bits) \
922  (((bits) % (sizeof(value) * 8)) > 0 \
923  ? ((value) << ((bits) % (sizeof(value) * 8))) | \
924  ((value) >> (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \
925  : (value))
926 
934 #define OFRotateRight(value, bits) \
935  (((bits) % (sizeof(value) * 8)) > 0 \
936  ? ((value) >> ((bits) % (sizeof(value) * 8))) | \
937  ((value) << (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \
938  : (value))
939 
947 #define OFRoundUpToPowerOf2(pow2, value) \
948  (((value) + (pow2) - 1) & ~((pow2) - 1))
949 
950 #define OF_ULONG_BIT (sizeof(unsigned long) * CHAR_BIT)
951 
952 static OF_INLINE bool
953 OFBitSetIsSet(unsigned long *_Nonnull storage, size_t idx)
954 {
955  return storage[idx / OF_ULONG_BIT] & (1ul << (idx % OF_ULONG_BIT));
956 }
957 
958 static OF_INLINE void
959 OFBitSetSet(unsigned long *_Nonnull storage, size_t idx)
960 {
961  storage[idx / OF_ULONG_BIT] |= (1ul << (idx % OF_ULONG_BIT));
962 }
963 
964 static OF_INLINE void
965 OFBitSetClear(unsigned long *_Nonnull storage, size_t idx)
966 {
967  storage[idx / OF_ULONG_BIT] &= ~(1ul << (idx % OF_ULONG_BIT));
968 }
969 
970 static OF_INLINE void
971 OFZeroMemory(void *_Nonnull buffer_, size_t length)
972 {
973  volatile unsigned char *buffer = (volatile unsigned char *)buffer_;
974 
975  while (buffer < (unsigned char *)buffer_ + length)
976  *buffer++ = '\0';
977 }
978 
979 static OF_INLINE bool
980 OFASCIIIsAlpha(char c)
981 {
982  return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
983 }
984 
985 static OF_INLINE bool
986 OFASCIIIsDigit(char c)
987 {
988  return (c >= '0' && c <= '9');
989 }
990 
991 static OF_INLINE bool
992 OFASCIIIsAlnum(char c)
993 {
994  return (OFASCIIIsAlpha(c) || OFASCIIIsDigit(c));
995 }
996 
997 static OF_INLINE bool
998 OFASCIIIsSpace(char c)
999 {
1000  return (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' ||
1001  c == '\v');
1002 }
1003 
1004 static OF_INLINE char
1005 OFASCIIToUpper(char c)
1006 {
1007  return (c >= 'a' && c <= 'z' ? 'A' + (c - 'a') : c);
1008 }
1009 
1010 static OF_INLINE char
1011 OFASCIIToLower(char c)
1012 {
1013  return (c >= 'A' && c <= 'Z' ? 'a' + (c - 'A') : c);
1014 }
1015 #endif
static OF_INLINE double OF_CONST_FUNC OFBitConvertUInt64ToDouble(uint64_t uInt64)
Bit-converts the specified uint64_t to a double.
Definition: macros.h:683
static OF_INLINE double OF_CONST_FUNC OFByteSwapDouble(double d)
Byte swaps the specified double.
Definition: macros.h:710
A class for storing constant strings using the @"" literal.
Definition: OFConstantString.h:41
static OF_INLINE float OF_CONST_FUNC OFBitConvertUInt32ToFloat(uint32_t uInt32)
Bit-converts the specified uint32_t to a float.
Definition: macros.h:655
#define OFByteSwap64(i)
Byte swaps the specified 64 bit integer.
Definition: macros.h:626
static OF_INLINE uint64_t OF_CONST_FUNC OFBitConvertDoubleToUInt64(double d)
Bit-converts the specified double to a uint64_t.
Definition: macros.h:669
static OF_INLINE uint32_t OF_CONST_FUNC OFBitConvertFloatToUInt32(float f)
Bit-converts the specified float to a uint32_t.
Definition: macros.h:641
static OF_INLINE float OF_CONST_FUNC OFByteSwapFloat(float f)
Byte swaps the specified float.
Definition: macros.h:697
void OFLog(OFConstantString *format,...)
Logs the specified printf-style format to OFStdErr.
Definition: OFStdIOStream.m:113
#define OFByteSwap32(i)
Byte swaps the specified 32 bit integer.
Definition: macros.h:617