This is the mail archive of the cygwin-patches@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH]: Add some interoperability macros to sys/param.h


Hi,

This patch adds 11 new macros to Cygwin's sys/param.h. They include:

setbit(a,i)
clrbit(a,i)
isset(a,i)
isclr(a,i)
howmany(x, y)
rounddown(x, y)
roundup(x, y)
roundup2(x, y)
powerof2(x)
MIN(a,b)
MAX(a,b)

I find some to be very useful for many mundane routines (esp. MIN/MAX). It should be noted that I chose to add all 11 of them (as opposed to just the ones I use) because this subset of macros seems to be common across all *bsd and linux distributions. In doing so we improve our interoperability and make porting easier. Although I "peeked" at the param.h header on my linux box to see if they were all defined, the macros I used were copied verbatim from the freebsd cvs src/sys/sys/param.h. Furthermore, I didn't actually check the definition expression in the linux header, so I feel confident that I have violated no licenses. I have an assignment pending, but I was hoping this change would be small enough to go in before then.

Cheers,
Nicholas
2003-08-07  Nicholas Wourms  <nwourms@netscape.net>

    * include/sys/param.h (setbit): Add new bitmap related macro.
    (clrbit): Likewise.
    (isset): Likewise.
    (isclr): Likewise.
    (howmany): Add new counting/rounding macro.
    (rounddown): Likewise.
    (roundup): Likewise.
    (roundup2): Likewise.
    (powerof2): Likewise
    (MIN): Add macro for calculating min.
    (MAX): Add macro for calculating max.
Index: cygwin/include/sys/param.h
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/include/sys/param.h,v
retrieving revision 1.2
diff -u -3 -p -r1.2 param.h
--- cygwin/include/sys/param.h  30 May 2003 08:39:02 -0000  1.2
+++ cygwin/include/sys/param.h  7 Aug 2003 17:03:14 -0000
@@ -53,4 +53,23 @@
 #define NULL            0L
 #endif
 
+/* Bit map related macros. */
+#define    setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
+#define    clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
+#define    isset(a,i)  ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
+#define    isclr(a,i)  (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
+
+/* Macros for counting and rounding. */
+#ifndef howmany
+#define    howmany(x, y)   (((x)+((y)-1))/(y))
+#endif
+#define    rounddown(x, y) (((x)/(y))*(y))
+#define    roundup(x, y)   ((((x)+((y)-1))/(y))*(y))  /* to any y */
+#define    roundup2(x, y)  (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
+#define powerof2(x)    ((((x)-1)&(x))==0)
+
+/* Macros for min/max. */
+#define    MIN(a,b)    (((a)<(b))?(a):(b))
+#define    MAX(a,b)    (((a)>(b))?(a):(b))
+

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]