Home | History | Annotate | Download | only in utils
      1 /* Copyright (C) 2011 The Android Open Source Project
      2 **
      3 ** This software is licensed under the terms of the GNU General Public
      4 ** License version 2, as published by the Free Software Foundation, and
      5 ** may be copied, distributed, and modified under those terms.
      6 **
      7 ** This program is distributed in the hope that it will be useful,
      8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 ** GNU General Public License for more details.
     11 */
     12 #ifndef ANDROID_UTILS_DUFF_H
     13 #define ANDROID_UTILS_DUFF_H
     14 
     15 /*********************************************************************
     16  *********************************************************************
     17  *****
     18  *****    DUFF'S DEVICES
     19  *****
     20  *****/
     21 
     22 #define  DUFF1(_count,_stmnt) \
     23     do { \
     24         int __n = (_count); \
     25         do { \
     26             _stmnt; \
     27         } while (--__n > 0); \
     28     } while (0);
     29 
     30 #define  DUFF2(_count,_stmnt)  \
     31     ({ \
     32         int   __count = (_count); \
     33         int   __n     = (__count +1)/2; \
     34         switch (__count & 1) { \
     35         case 0: do { _stmnt; \
     36         case 1:      _stmnt; \
     37                 } while (--__n > 0); \
     38         } \
     39     })
     40 
     41 #define  DUFF4(_count,_stmnt)  \
     42     ({ \
     43         int   __count = (_count); \
     44         int   __n     = (__count +3)/4; \
     45         switch (__count & 3) { \
     46         case 0: do { _stmnt; \
     47         case 3:      _stmnt; \
     48         case 2:      _stmnt; \
     49         case 1:      _stmnt; \
     50                 } while (--__n > 0); \
     51         } \
     52     })
     53 
     54 #define  DUFF8(_count,_stmnt)  \
     55     ({ \
     56         int   __count = (_count); \
     57         int   __n     = (__count+7)/8; \
     58         switch (__count & 7) { \
     59         case 0: do { _stmnt; \
     60         case 7:      _stmnt; \
     61         case 6:      _stmnt; \
     62         case 5:      _stmnt; \
     63         case 4:      _stmnt; \
     64         case 3:      _stmnt; \
     65         case 2:      _stmnt; \
     66         case 1:      _stmnt; \
     67                 } while (--__n > 0); \
     68         } \
     69     })
     70 
     71 #endif /* ANDROID_UTILS_DUFF_H */
     72