1/*	$NetBSD$	*/
2
3/*-
4 * Copyright (c) 1994
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *	@(#)maskbits.h	8.2 (Berkeley) 3/21/94
32 */
33
34/*
35 * Derived from X11R4
36 */
37
38/* the following notes use the following conventions:
39SCREEN LEFT				SCREEN RIGHT
40in this file and maskbits.c, left and right refer to screen coordinates,
41NOT bit numbering in registers.
42
43starttab[n]
44	bits[0,n-1] = 0	bits[n,31] = 1
45endtab[n] =
46	bits[0,n-1] = 1	bits[n,31] = 0
47
48maskbits(x, w, startmask, endmask, nlw)
49	for a span of width w starting at position x, returns
50a mask for ragged bits at start, mask for ragged bits at end,
51and the number of whole longwords between the ends.
52
53*/
54
55#define maskbits(x, w, startmask, endmask, nlw)				\
56do {									\
57	startmask = starttab[(x) & 0x1f];				\
58	endmask = endtab[((x)+(w)) & 0x1f];				\
59	if (startmask)							\
60		nlw = (((w) - (32 - ((x)&0x1f))) >> 5);			\
61	else								\
62		nlw = (w) >> 5;						\
63} while (/* CONSTCOND */ 0)
64
65#define FASTGETBITS(psrc, x, w, dst)					\
66    __asm ("bfextu %3{%1:%2},%0"					\
67	: "=d" (dst) : "di" (x), "di" (w), "o" (*(char *)(psrc)))
68
69#define FASTPUTBITS(src, x, w, pdst)					\
70    __asm ("bfins %3,%0{%1:%2}"						\
71	: "=o" (*(char *)(pdst))					\
72	: "di" (x), "di" (w), "d" (src))
73
74#define getandputrop(psrc, srcbit, dstbit, width, pdst, rop)		\
75do {									\
76	unsigned int _tmpsrc, _tmpdst;					\
77	FASTGETBITS(pdst, dstbit, width, _tmpdst);			\
78	FASTGETBITS(psrc, srcbit, width, _tmpsrc);			\
79	DoRop(_tmpdst, rop, _tmpsrc, _tmpdst);				\
80	FASTPUTBITS(_tmpdst, dstbit, width, pdst);			\
81} while (/* CONSTCOND */ 0)
82
83#define getandputrop0(psrc, srcbit, width, pdst, rop) \
84	getandputrop(psrc, srcbit, 0, width, pdst, rop)
85
86#define getunalignedword(psrc, x, dst)					\
87do {									\
88	int _tmp;							\
89	FASTGETBITS(psrc, x, 32, _tmp);					\
90	dst = _tmp;							\
91} while (/* CONSTCOND */ 0)
92
93#define fnCLEAR(src, dst)       (0)
94#define fnCOPY(src, dst)        (src)
95#define fnXOR(src, dst)         (src ^ dst)
96#define fnCOPYINVERTED(src, dst)(~src)
97
98#define DoRop(result, alu, src, dst) \
99do {									\
100	if (alu == RR_COPY)						\
101		result = fnCOPY (src, dst);				\
102	else {								\
103		switch (alu) {						\
104		case RR_CLEAR:						\
105			result = fnCLEAR (src, dst);			\
106			break;						\
107		case RR_XOR:						\
108			result = fnXOR (src, dst);			\
109			break;						\
110		case RR_COPYINVERTED:					\
111			result = fnCOPYINVERTED (src, dst);		\
112			break;						\
113		}							\
114	}								\
115} while (/* CONSTCOND */ 0)
116