File: testpoly.c

package info (click to toggle)
hf 0.7.3-4etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 3,684 kB
  • ctags: 3,156
  • sloc: ansic: 26,447; cpp: 4,909; sh: 3,785; makefile: 309
file content (42 lines) | stat: -rw-r--r-- 1,066 bytes parent folder | download | duplicates (4)
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h> 
#include <stdio.h>

#define POLY_HI (1<<9)
#define POLY_LO ((1<<4)|(1<<0))
#define POLY (POLY_HI|POLY_LO)
#define POLY_REV ((1<<0)|(1<<5)|(1<<9))

#define POLY_MASK 0x1ff

extern __inline__ unsigned int hweight32(unsigned int w) 
{
        unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
        res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
        res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
        res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
        return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
}

int main(int argc, char *argv[])
{
	unsigned int divst, mulst = 0, cnt = 0;

	divst = 1;
	do {
		divst <<= 1;
		mulst <<= 1;
		if (divst & POLY_HI) {
			divst ^= POLY_LO;
			mulst |= 1;
		}
		divst &= POLY_MASK;
		cnt++;
		printf("Count: %d State: %4d  Out: %c Mul: %c  Weight: %d\n", cnt, divst, '0'+(mulst & 1), 
		       '0'+(hweight32(mulst & POLY_REV) & 1), hweight32(mulst & 0xfff));
	} while (cnt < 10000 && divst != 1);
	exit(0);
}