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