Home | History | Annotate | Line # | Download | only in rasops
rasops_masks.h revision 1.8
      1 /* 	$NetBSD: rasops_masks.h,v 1.8 2013/12/02 14:05:51 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _RASOPS_MASKS_H_
     33 #define _RASOPS_MASKS_H_ 1
     34 
     35 #include <sys/types.h>
     36 #include <machine/endian.h>
     37 
     38 /*
     39  * Convenience macros. To get around the problem of dealing with properly
     40  * ordered bits on little-endian machines, we just convert everything to
     41  * big-endian and back again when we're done.
     42  *
     43  * MBL: move bits left
     44  * MBR: move bits right
     45  * MBE: make big-endian
     46  */
     47 #if BYTE_ORDER == BIG_ENDIAN
     48 
     49 #define MBL(x,y)	((y) > 31 ? 0 : (x) << (y))
     50 #define MBR(x,y)    	((y) > 31 ? 0 : (x) >> (y))
     51 #define MBE(x)		(x)
     52 
     53 #else
     54 
     55 #define MBL(x,y)    	((y) > 31 ? 0 : MBE(MBE(x) << (y)))
     56 #define MBR(x,y)    	((y) > 31 ? 0 : MBE(MBE(x) >> (y)))
     57 #define MBE(x)		( (((x) & 0x000000FFU) << 24) \
     58                         | (((x) & 0x0000FF00U) <<  8) \
     59                         | (((x) & 0x00FF0000U) >>  8) \
     60                         | (((x) & 0xFF000000U) >> 24) )
     61 #endif
     62 
     63 /*
     64  * Using GETBITS() and PUTBITS() inside a loop mightn't be such a good idea.
     65  * There's probably some CSE and strength-reduction that the compiler won't
     66  * even think about - really should have a few assumptions/separate cases.
     67  */
     68 
     69 /* Get a number of bits ( <= 32 ) from *sp and store in dw */
     70 #define GETBITS(sp, x, w, dw) do {					\
     71 	dw = MBL(*(sp), (x));						\
     72 	if (((x) + (w)) > 32)						\
     73 		dw |= (MBR((sp)[1], 32 - (x))); 			\
     74 } while(0);
     75 
     76 /* Put a number of bits ( <= 32 ) from sw to *dp */
     77 #define PUTBITS(sw, x, w, dp) do {					\
     78 	int n = (x) + (w) - 32;						\
     79 									\
     80 	if (n <= 0) {							\
     81 		n = rasops_pmask[x & 31][w & 31];			\
     82 		*(dp) = (*(dp) & ~n) | (MBR(sw, x) & n);		\
     83 	} else {							\
     84 		*(dp) = (*(dp) & rasops_rmask[x]) | (MBR((sw), x));	\
     85 		(dp)[1] = ((dp)[1] & rasops_rmask[n]) |			\
     86 			(MBL(sw, 32-(x)) & rasops_lmask[n]);		\
     87 	}								\
     88 } while(0);
     89 
     90 /* rasops_masks.c */
     91 extern const uint32_t	rasops_lmask[32+1];
     92 extern const uint32_t	rasops_rmask[32+1];
     93 extern const uint32_t	rasops_pmask[32][32];
     94 
     95 #endif /* _RASOPS_MASKS_H_ */
     96