1// 2// bitfield.c - extract and set bit fields 3// 4// Written by Eryk Vershen 5// 6// See comments in bitfield.h 7// 8 9/* 10 * Copyright 1996, 1997 by Apple Computer, Inc. 11 * All Rights Reserved 12 * 13 * Permission to use, copy, modify, and distribute this software and 14 * its documentation for any purpose and without fee is hereby granted, 15 * provided that the above copyright notice appears in all copies and 16 * that both the copyright notice and this permission notice appear in 17 * supporting documentation. 18 * 19 * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE 20 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 * FOR A PARTICULAR PURPOSE. 22 * 23 * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR 24 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 25 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, 26 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 27 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 28 */ 29 30#include <stdint.h> 31#include "bitfield.h" 32 33 34// 35// Defines 36// 37 38 39// 40// Types 41// 42 43 44// 45// Global Constants 46// 47const uint32_t masks[] = { 48 0x00000000, 49 0x00000001, 0x00000003, 0x00000007, 0x0000000F, 50 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF, 51 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF, 52 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF, 53 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF, 54 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF, 55 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF, 56 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF 57}; 58 59 60// 61// Global Variables 62// 63 64 65// 66// Forward declarations 67// 68 69 70// 71// Routines 72// 73uint32_t 74bitfield_set(uint32_t *bf, int base, int length, uint32_t value) 75{ 76 uint32_t t; 77 uint32_t m; 78 int s; 79 80 // compute shift & mask, coerce value to correct number of bits, 81 // zap the old bits and stuff the new value 82 // return the masked value in case someone wants it. 83 s = (base + 1) - length; 84 m = masks[length]; 85 t = value & m; 86 *bf = (*bf & ~(m << s)) | (t << s); 87 return t; 88} 89 90 91uint32_t 92bitfield_get(uint32_t bf, int base, int length) 93{ 94 uint32_t m; 95 int s; 96 97 // compute shift & mask 98 // return the correct number of bits (shifted to low end) 99 s = (base + 1) - length; 100 m = masks[length]; 101 return ((bf >> s) & m); 102} 103