File: bitfields.c

package info (click to toggle)
iraf 2.18.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,000 kB
  • sloc: ansic: 115,890; fortran: 74,576; lisp: 18,888; yacc: 5,642; sh: 961; lex: 596; makefile: 509; asm: 159; csh: 54; xml: 33; sed: 4
file content (70 lines) | stat: -rw-r--r-- 2,315 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
 */

#define	import_spp
#define import_knames
#include <iraf.h>

/*
 * BITFIELDS.C -- Portable C routines for extracting and inserting small
 * integers into an integer value.
 */

XUINT bitmask[] = {	0,		/* MACHDEP		*/
  01,			    03,			     07,
  017,			    037,		     077,
  0177,			    0377,		     0777,
  01777,		    03777,		     07777,
  017777,		    037777,		     077777,
  0177777,		    0377777,		     0777777,
  01777777,		    03777777,		     07777777,
  017777777,		    037777777,		     077777777,
  0177777777,		    0377777777,		     0777777777,
  01777777777,		    03777777777,	     07777777777,
  017777777777,		    037777777777,   	     077777777777,
  0177777777777,	    0377777777777,   	     0777777777777,
  01777777777777,	    03777777777777,   	     07777777777777,
  017777777777777,	    037777777777777,   	     077777777777777,
  0177777777777777,	    0377777777777777,        0777777777777777,
  01777777777777777,	    03777777777777777,       07777777777777777,
  017777777777777777,	    037777777777777777,      077777777777777777,
  0177777777777777777,	    0377777777777777777,     0777777777777777777,
  01777777777777777777,	    03777777777777777777,    07777777777777777777,
  017777777777777777777,    037777777777777777777,   077777777777777777777,
  0177777777777777777777,   0377777777777777777777,  0777777777777777777777,
  01777777777777777777777
};



/* BITPAK -- Pack an unsigned integer value into a bitfield in a longword.
 * The size of the bitfield may not exceed the number of bits in an integer.
 */
void
BITPAK (
  XUINT	*ival,			/* value to be placed in bitfield	*/
  XUINT	*wordp,			/* longword to be written into		*/
  XINT	*offset,		/* one-indexed offset of first bit	*/
  XINT	*nbits 			/* number of bits to be set		*/
)
{
	register XUINT	shift;
	register XUINT	mask;

	shift = *offset - 1;
	mask = bitmask[*nbits] << shift;
	*wordp = (*wordp & ~mask) | ((*ival << shift) & mask);
}


/* BITUPK -- Unpack an unsigned integer bit field from a longword.
 */
XINT
BITUPK (
  XUINT	*wordp,			/* longword to be examined		*/
  XINT	*offset,		/* one-indexed offset of first bit	*/
  XINT	*nbits 			/* number of bits to be set		*/
)
{
	return ((*wordp >> (*offset-1)) & bitmask[*nbits]);
}