1 /* $OpenBSD: rasops_masks.h,v 1.4 2008/06/26 05:42:18 ray 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 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #ifndef _RASOPS_MASKS_H_ 34 #define _RASOPS_MASKS_H_ 1 35 36 #include <sys/types.h> 37 #include <machine/endian.h> 38 39 /* 40 * Convenience macros. To get around the problem of dealing with properly 41 * ordered bits on little-endian machines, we just convert everything to 42 * big-endian and back again when we're done. 43 * 44 * MBL: move bits left 45 * MBR: move bits right 46 * MBE: make big-endian 47 */ 48 49 #define MBL(x,y) ((y) > 31 ? 0 : (x) >> (y)) 50 #define MBR(x,y) ((y) > 31 ? 0 : (x) << (y)) 51 52 #if BYTE_ORDER == BIG_ENDIAN 53 #define MBE(x) (x) 54 #else 55 #define MBE(x) \ 56 ({ \ 57 u_int32_t tmp = (x); \ 58 tmp = ((tmp >> 1) & 0x55555555) | ((tmp << 1) & 0xaaaaaaaa); \ 59 tmp = ((tmp >> 2) & 0x33333333) | ((tmp << 2) & 0xcccccccc); \ 60 tmp = ((tmp >> 4) & 0x0f0f0f0f) | ((tmp << 4) & 0xf0f0f0f0); \ 61 tmp = ((tmp >> 8) & 0x00ff00ff) | ((tmp << 8) & 0xff00ff00); \ 62 tmp = ((tmp >> 16) & 0x0000ffff) | ((tmp << 16) & 0xffff0000); \ 63 tmp; \ 64 }) 65 #endif 66 67 /* 68 * Using GETBITS() and PUTBITS() inside a loop mightn't be such a good idea. 69 * There's probably some CSE and strength-reduction that the compiler won't 70 * even think about - really should have a few assumptions/separate cases. 71 */ 72 73 /* Get a number of bits ( <= 32 ) from *sp and store in dw */ 74 #define GETBITS(sp, x, w, dw) do { \ 75 dw = MBL(*(sp), (x)); \ 76 if (((x) + (w)) > 32) \ 77 dw |= (MBR((sp)[1], 32 - (x))); \ 78 } while(0); 79 80 /* Put a number of bits ( <= 32 ) from sw to *dp */ 81 #define PUTBITS(sw, x, w, dp) do { \ 82 int n = (x) + (w) - 32; \ 83 \ 84 if (n <= 0) { \ 85 n = rasops_pmask[x & 31][w & 31]; \ 86 *(dp) = (*(dp) & ~n) | (MBR(sw, x) & n); \ 87 } else { \ 88 *(dp) = (*(dp) & rasops_rmask[x]) | (MBR((sw), x)); \ 89 (dp)[1] = ((dp)[1] & rasops_rmask[n]) | \ 90 (MBL(sw, 32-(x)) & rasops_lmask[n]); \ 91 } \ 92 } while(0); 93 94 /* rasops_masks.c */ 95 #if BYTE_ORDER == BIG_ENDIAN 96 extern const int32_t rasops_lmask[32+1]; 97 extern const int32_t rasops_rmask[32+1]; 98 extern const int32_t rasops_pmask[32][32]; 99 #define rasops_masks_init() do { } while (0) 100 #else 101 extern int32_t rasops_lmask[32+1]; 102 extern int32_t rasops_rmask[32+1]; 103 extern int32_t rasops_pmask[32][32]; 104 void rasops_masks_init(void); 105 #endif 106 107 #endif /* _RASOPS_MASKS_H_ */ 108