xref: /netbsd-src/sys/dev/rasops/rasops_masks.h (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /* 	$NetBSD: rasops_masks.h,v 1.6 2001/01/12 23:03:52 bjh21 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #ifndef _RASOPS_MASKS_H_
40 #define _RASOPS_MASKS_H_ 1
41 
42 #include <sys/types.h>
43 #include <machine/endian.h>
44 
45 /*
46  * Convenience macros. To get around the problem of dealing with properly
47  * ordered bits on little-endian machines, we just convert everything to
48  * big-endian and back again when we're done.
49  *
50  * MBL: move bits left
51  * MBR: move bits right
52  * MBE: make big-endian
53  */
54 #if BYTE_ORDER == BIG_ENDIAN
55 
56 #define MBL(x,y)	((y) > 31 ? 0 : (x) >> (y))
57 #define MBR(x,y)    	((y) > 31 ? 0 : (x) << (y))
58 #define MBE(x)		(x)
59 
60 #else
61 
62 #define MBL(x,y)    	((y) > 31 ? 0 : MBE(MBE(x) << (y)))
63 #define MBR(x,y)    	((y) > 31 ? 0 : MBE(MBE(x) >> (y)))
64 #define MBE(x)		( (((x) & 0x000000FFU) << 24) \
65                         | (((x) & 0x0000FF00U) <<  8) \
66                         | (((x) & 0x00FF0000U) >>  8) \
67                         | (((x) & 0xFF000000U) >> 24) )
68 #endif
69 
70 /*
71  * Using GETBITS() and PUTBITS() inside a loop mightn't be such a good idea.
72  * There's probably some CSE and strength-reduction that the compiler won't
73  * even think about - really should have a few assumptions/separate cases.
74  */
75 
76 /* Get a number of bits ( <= 32 ) from *sp and store in dw */
77 #define GETBITS(sp, x, w, dw) do {					\
78 	dw = MBL(*(sp), (x));						\
79 	if (((x) + (w)) > 32)						\
80 		dw |= (MBR((sp)[1], 32 - (x))); 			\
81 } while(0);
82 
83 /* Put a number of bits ( <= 32 ) from sw to *dp */
84 #define PUTBITS(sw, x, w, dp) do {					\
85 	int n = (x) + (w) - 32;						\
86 									\
87 	if (n <= 0) {							\
88 		n = rasops_pmask[x & 31][w & 31];			\
89 		*(dp) = (*(dp) & ~n) | (MBR(sw, x) & n);		\
90 	} else {							\
91 		*(dp) = (*(dp) & rasops_rmask[x]) | (MBR((sw), x));	\
92 		(dp)[1] = ((dp)[1] & rasops_rmask[n]) |			\
93 			(MBL(sw, 32-(x)) & rasops_lmask[n]);		\
94 	}								\
95 } while(0);
96 
97 /* rasops_masks.c */
98 extern const int32_t	rasops_lmask[32+1];
99 extern const int32_t	rasops_rmask[32+1];
100 extern const int32_t	rasops_pmask[32][32];
101 
102 #endif /* _RASOPS_MASKS_H_ */
103