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