cosmopolitan/libc/mem/reverse.internal.h
Justine Tunney fa20edc44d
Reduce header complexity
- Remove most __ASSEMBLER__ __LINKER__ ifdefs
- Rename libc/intrin/bits.h to libc/serialize.h
- Block pthread cancelation in fchmodat() polyfill
- Remove `clang-format off` statements in third_party
2023-11-28 14:39:42 -08:00

30 lines
982 B
C

#ifndef COSMOPOLITAN_LIBC_ALG_REVERSE_H_
#define COSMOPOLITAN_LIBC_ALG_REVERSE_H_
#include "libc/intrin/xchg.internal.h"
/**
* Reverses array.
*
* @param ARRAY is a typed array or a pointer to one
* @param COUNT is the number of items
* @return pointer to start of array
* @see ARRAYLEN()
*/
#define reverse(ARRAY, COUNT) \
({ \
autotype(&(ARRAY)[0]) Array = (ARRAY); \
size_t Count = (COUNT); \
if (Count) { \
size_t Start = 0; \
size_t End = Count - 1; \
while (Start < End) { \
xchg(&Array[Start], &Array[End]); \
++Start; \
--End; \
} \
} \
Array; \
})
#endif /* COSMOPOLITAN_LIBC_ALG_REVERSE_H_ */