1
0
mirror of https://github.com/elua/elua.git synced 2025-01-08 20:56:17 +08:00
elua/inc/utils.h
James Snyder 5a6a4d017e Switch byte copying in buf.c to a Duff's device. This seems to work as well
for me as memcpy did.

Check out inc/utils.h, since this Duff's device is implemented as a macro
which could be used for unrolling other loops selectively.
2009-02-17 03:52:29 +00:00

36 lines
1.1 KiB
C

// General purpose function/macros
#ifndef __UTILS_H__
#define __UTILS_H__
#define ABSDIFF( x, y ) ( ( x ) >= ( y ) ? ( x ) - ( y ) : ( y ) - ( x ) )
#define UMIN( x, y ) ( ( x ) <= ( y ) ? ( x ) : ( y ) )
#define UMAX( x, y ) ( ( x ) >= ( y ) ? ( x ) : ( y ) )
#define UABS( x ) ( ( x ) >= 0 ? ( x ) : -( x ) )
// Macro version of Duff's device found in
// "A Reusable Duff Device" by Ralf Holly
// Dr Dobb's Journal, August 1, 2005
#define DUFF_DEVICE_8(count, action) \
do { \
int _count = ( count ); \
int _times = ( _count + 7 ) >> 3; \
switch ( _count & 7 ){ \
case 0: do { action; \
case 7: action; \
case 6: action; \
case 5: action; \
case 4: action; \
case 3: action; \
case 2: action; \
case 1: action; \
} while (--_times > 0); \
} \
} while (0)
#define STD_CTRLZ_CODE 26
#endif