GLAST / LAT > DAQ and FSW > FSW > Doxygen Index> EDS / V2-11-5 > eds / mv2304
#include <PBI/Inline.h>
#include <PBI/Attribute.h>
#include <PARITY.h>
Defines | |
#define | PARITY__EXP_PROTO INLINE_USR_EXP_PROTO |
#define | PARITY__LCL_PROTO INLINE_USR_LCL_PROTO |
#define | PARITY__EXP_FNC INLINE_USR_EXP_FNC |
#define | PARITY__LCL_FNC INLINE_USR_LCL_FNC |
Functions | |
PARITY__EXP_PROTO int | PARITY__calc8 (unsigned char byte) ATTR_UNUSED_OK |
Calculates the parity over the 8 bits in the specified byte. | |
PARITY__EXP_PROTO int | PARITY__calc16 (unsigned short int hword) ATTR_UNUSED_OK |
Calculates the parity over the 16 bits in the specified half word. | |
PARITY__EXP_PROTO int | PARITY__calc32 (unsigned int word) ATTR_UNUSED_OK |
Calculates the parity over the 32 bits in the specified word. | |
PARITY__EXP_PROTO int | PARITY__calc64 (unsigned long long int ll) ATTR_UNUSED_OK |
Calculates the parity over the 64 bits in the specified long long. | |
PARITY__EXP_PROTO int | PARITY__calc8N (const unsigned char *bytes, int nbytes) ATTR_UNUSED_OK |
Calculates the parity over all the bits in the specified array of bytes. | |
PARITY__EXP_PROTO int | PARITY__calc16N (const unsigned short int *hwords, int nhwords) ATTR_UNUSED_OK |
Calculates the parity over all the bits in the specified array of half words (16-bit integers). | |
PARITY__EXP_PROTO int | PARITY__calc32N (const unsigned int *words, int nwords) ATTR_UNUSED_OK |
Calculates the parity over all the bits in the specified array of words (32-bit integers). | |
PARITY__EXP_PROTO int | PARITY__calc64N (const unsigned long long int *lls, int nlls) ATTR_UNUSED_OK |
Calculates the parity over all the bits in the specified array of long long words (64-bit integers). | |
PARITY__EXP_PROTO int | PARITY__calcOdd8 (unsigned char byte) ATTR_UNUSED_OK |
Calculates the odd parity over the 8 bits in the specified byte. | |
PARITY__EXP_PROTO int | PARITY__calcOdd16 (unsigned short int hword) ATTR_UNUSED_OK |
Calculates the odd parity over the 16 bits in the specified half word. | |
PARITY__EXP_PROTO int | PARITY__calcOdd32 (unsigned int word) ATTR_UNUSED_OK |
Calculates the odd parity over the 32 bits in the specified word. | |
PARITY__EXP_PROTO int | PARITY__calcOdd64 (unsigned long long int ll) ATTR_UNUSED_OK |
Calculates the odd parity over the 64 bits in the specified long long. | |
PARITY__EXP_PROTO int | PARITY__calcOdd8N (const unsigned char *bytes, int nbytes) ATTR_UNUSED_OK |
Calculates the odd parity over all the bits in the specified array of bytes. | |
PARITY__EXP_PROTO int | PARITY__calcOdd16N (const unsigned short int *hwords, int nhwords) ATTR_UNUSED_OK |
Calculates the odd parity over all the bits in the specified array of half words (16-bit integers). | |
PARITY__EXP_PROTO int | PARITY__calcOdd32N (const unsigned int *words, int nwords) ATTR_UNUSED_OK |
Calculates the odd parity over all the bits in the specified array of words (32-bit integers). | |
PARITY__EXP_PROTO int | PARITY__calcOdd64N (const unsigned long long int *lls, int nlls) ATTR_UNUSED_OK |
Calculates the odd parity over all the bits in the specified array of long long words (64-bit integers). |
CVS $Id: PARITY.ih,v 1.2 2011/03/25 22:16:56 russell Exp $
as well as arrays of the same.
.
.
parity = (word >> 16) ^ (word);
parity = (parity >> 8) ^ (parity);
parity = (parity >> 4) ^ (parity);
parity = (parity >> 2) ^ (parity);
parity = (parity >> 1) ^ (parity);
return parity & 1;
This takes advantage of the fact that the XORs can be done in parallel. However, there is a small improvement that can be made on this algorithm. Consider building an array is that as big as the value of an input value and whose value at an given index is a single bit giving the parity of the value. Of course such an array for a 32-bit integer would be prohibitively large. Even if this was not true, on RISC machines, one should avoid referencing uncached memory for performance reasons.
There is an variation on this theme that does work. First, execute the above algorithm until only 4 bits remain. Now the range of these 4 bits is modest, 0-15, so one could consider an array. The array that will be indexed, however is not as a traditional memory array, but as a bit array built in a single 16-bit number. The indexing is accomplished by simply right shifting this 16-bit numer and masking off the low order bit.
.
.
index = word;
index ^= (index >> 16); / * Reduce to 16 significant bits * /
index ^= (index >> 8); / * Reduce to 8 significant bits * /
index ^= (index >> 4); / * Reduce to 8 significant bits * /
index &= 0xf; / * Pick off the lower 4 bits * /
return 0x6996 >> index; / * Index the answer array * /
Note another nice feature of this algortihm. If one desires a logical functions that returns TRUE if the parity is EVEN, or equivalently one that returns the ODD parity then one simply replaces the 0x6996 with 0x9669.
The above sequence should cost approximately 8 integer instructions on a PowerPC. Narrower widths cost less; a 16-bit value should cost about 6 instructions and a byte should cost 4 instructions.
The best advise here is, if one is calculating the parity over arrays less than say ~10 elements, use the inline versions. If one is calculating the parity over larger arrays, use the callable versions. The overhead of the call will be compensated by speedier routines. This statement is particularly true when calculating the parity over arrays of bytes and half words (short ints).
PARITY__EXP_FNC int PARITY__calc16 | ( | unsigned short int | hword | ) |
Calculates the parity over the 16 bits in the specified half word.
hword | The half word (short int) to calculate the parity of. |
References PARITY_K_EVEN.
PARITY__EXP_FNC int PARITY__calc16N | ( | const unsigned short int * | hwords, | |
int | nhwords | |||
) |
Calculates the parity over all the bits in the specified array of half words (16-bit integers).
hwords | The array of half words (16-bit integers) to calculate the parity over. | |
nhwords | The number of half words (16-bit integers) in the array. |
References PARITY_K_EVEN.
PARITY__EXP_FNC int PARITY__calc32 | ( | unsigned int | word | ) |
Calculates the parity over the 32 bits in the specified word.
word | The word to calculate the parity over. |
References PARITY_K_EVEN.
PARITY__EXP_FNC int PARITY__calc32N | ( | const unsigned int * | words, | |
int | nwords | |||
) |
Calculates the parity over all the bits in the specified array of words (32-bit integers).
words | The array of words (ints) to calculate the parity over. | |
nwords | The number of words (ints) in the array. |
References PARITY_K_EVEN.
PARITY__EXP_FNC int PARITY__calc64 | ( | unsigned long long int | ll | ) |
Calculates the parity over the 64 bits in the specified long long.
ll | The long long to calculate the parity over. |
References PARITY_K_EVEN.
PARITY__EXP_FNC int PARITY__calc64N | ( | const unsigned long long int * | lls, | |
int | nlls | |||
) |
Calculates the parity over all the bits in the specified array of long long words (64-bit integers).
lls | The array of long longs to calculate the parity over. | |
nlls | The number of long longs in the array. |
References PARITY_K_EVEN.
PARITY__EXP_FNC int PARITY__calc8 | ( | unsigned char | byte | ) |
Calculates the parity over the 8 bits in the specified byte.
byte | The byte to calculate the parity over. |
References PARITY_K_EVEN.
PARITY__EXP_FNC int PARITY__calc8N | ( | const unsigned char * | bytes, | |
int | nbytes | |||
) |
Calculates the parity over all the bits in the specified array of bytes.
bytes | The array of bytes to calculate the parity over. | |
nbytes | The number of bytes in array. |
References PARITY_K_EVEN.
PARITY__EXP_FNC int PARITY__calcOdd16 | ( | unsigned short int | hword | ) |
Calculates the odd parity over the 16 bits in the specified half word.
hword | The half word (short int) to calculate the parity of. |
References PARITY_K_ODD.
Referenced by EBF_sivUpdate().
PARITY__EXP_FNC int PARITY__calcOdd16N | ( | const unsigned short int * | hwords, | |
int | nhwords | |||
) |
Calculates the odd parity over all the bits in the specified array of half words (16-bit integers).
hwords | The array of half words (16-bit integers) to calculate the odd parity over. | |
nhwords | The number of half words (16-bit integers) in the array. |
References PARITY_K_ODD.
PARITY__EXP_FNC int PARITY__calcOdd32 | ( | unsigned int | word | ) |
Calculates the odd parity over the 32 bits in the specified word.
word | The word to calculate the odd parity over. |
References PARITY_K_ODD.
PARITY__EXP_FNC int PARITY__calcOdd32N | ( | const unsigned int * | words, | |
int | nwords | |||
) |
Calculates the odd parity over all the bits in the specified array of words (32-bit integers).
words | The array of words (ints) to calculate the odd parity over. | |
nwords | The number of words (ints) in the array. |
References PARITY_K_ODD.
PARITY__EXP_FNC int PARITY__calcOdd64 | ( | unsigned long long int | ll | ) |
Calculates the odd parity over the 64 bits in the specified long long.
ll | The long long to calculate the odd parity over. |
References PARITY_K_ODD.
PARITY__EXP_FNC int PARITY__calcOdd64N | ( | const unsigned long long int * | lls, | |
int | nlls | |||
) |
Calculates the odd parity over all the bits in the specified array of long long words (64-bit integers).
lls | The array of long long's to calculate the odd parity over. | |
nlls | The number of long long's in the array. |
References PARITY_K_ODD.
PARITY__EXP_FNC int PARITY__calcOdd8 | ( | unsigned char | byte | ) |
Calculates the odd parity over the 8 bits in the specified byte.
byte | The byte to calculate the odd parity over. |
References PARITY_K_ODD.
PARITY__EXP_FNC int PARITY__calcOdd8N | ( | const unsigned char * | bytes, | |
int | nbytes | |||
) |
Calculates the odd parity over all the bits in the specified array of bytes.
bytes | The array of bytes to calculate the odd parity over. | |
nbytes | The number of bytes in array. |
References PARITY_K_ODD.