apt @VERSION@

macros.h

00001 // -*- mode: cpp; mode: fold -*-
00002 // Description                                                          /*{{{*/
00003 /* ######################################################################
00004    
00005    Macros Header - Various useful macro definitions
00006 
00007    This source is placed in the Public Domain, do with it what you will
00008    It was originally written by Brian C. White.
00009    
00010    ##################################################################### */
00011                                                                         /*}}}*/
00012 // Private header
00013 #ifndef MACROS_H
00014 #define MACROS_H
00015 
00016 // MIN_VAL(SINT16) will return -0x8000 and MAX_VAL(SINT16) = 0x7FFF
00017 #define MIN_VAL(t)      (((t)(-1) > 0) ? (t)( 0) : (t)(((1L<<(sizeof(t)*8-1))  )))
00018 #define MAX_VAL(t)      (((t)(-1) > 0) ? (t)(-1) : (t)(((1L<<(sizeof(t)*8-1))-1)))
00019 
00020 // Min/Max functions
00021 #if !defined(MIN)
00022 #if defined(__HIGHC__)
00023 #define MIN(x,y) _min(x,y)
00024 #define MAX(x,y) _max(x,y)
00025 #endif
00026 
00027 // GNU C++ has a min/max operator <coolio>
00028 #if defined(__GNUG__)
00029 #define MIN(A,B) ((A) <? (B))
00030 #define MAX(A,B) ((A) >? (B))
00031 #endif
00032 
00033 /* Templates tend to mess up existing code that uses min/max because of the
00034    strict matching requirements */
00035 #if !defined(MIN)
00036 #define MIN(A,B) ((A) < (B)?(A):(B))
00037 #define MAX(A,B) ((A) > (B)?(A):(B))
00038 #endif
00039 #endif
00040 
00041 /* Bound functions, bound will return the value b within the limits a-c
00042    bounv will change b so that it is within the limits of a-c. */
00043 #define _bound(a,b,c) MIN(c,MAX(b,a))
00044 #define _boundv(a,b,c) b = _bound(a,b,c)
00045 #define ABS(a) (((a) < (0)) ?-(a) : (a))
00046 
00047 /* Usefull count macro, use on an array of things and it will return the
00048    number of items in the array */
00049 #define _count(a) (sizeof(a)/sizeof(a[0]))
00050 
00051 // Flag Macros
00052 #define FLAG(f)                 (1L << (f))
00053 #define SETFLAG(v,f)    ((v) |= FLAG(f))
00054 #define CLRFLAG(v,f)    ((v) &=~FLAG(f))
00055 #define CHKFLAG(v,f)    ((v) &  FLAG(f) ? true : false)
00056 
00057 // some nice optional GNUC features
00058 #if __GNUC__ >= 3
00059         #define __must_check    __attribute__ ((warn_unused_result))
00060         #define __deprecated    __attribute__ ((deprecated))
00061         #define __attrib_const  __attribute__ ((__const__))
00062         /* likely() and unlikely() can be used to mark boolean expressions
00063            as (not) likely true which will help the compiler to optimise */
00064         #define likely(x)       __builtin_expect (!!(x), 1)
00065         #define unlikely(x)     __builtin_expect (!!(x), 0)
00066 #else
00067         #define __must_check    /* no warn_unused_result */
00068         #define __deprecated    /* no deprecated */
00069         #define __attrib_const  /* no const attribute */
00070         #define likely(x)       (x)
00071         #define unlikely(x)     (x)
00072 #endif
00073 
00074 // cold functions are unlikely() to be called
00075 #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4
00076         #define __cold  __attribute__ ((__cold__))
00077         #define __hot   __attribute__ ((__hot__))
00078 #else
00079         #define __cold  /* no cold marker */
00080         #define __hot   /* no hot marker */
00081 #endif
00082 
00083 #ifdef __GNUG__
00084 // Methods have a hidden this parameter that is visible to this attribute
00085         #define __like_printf(n)        __attribute__((format(printf, n, n + 1)))
00086 #else
00087         #define __like_printf(n)        /* no like-printf */
00088 #endif
00089 
00090 #endif