summaryrefslogtreecommitdiff
path: root/test/div20.c
blob: 1003ab8c734a74fdd91fd45f137ce851034eea8c (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>


/*
Page about div
http://bisqwit.iki.fi/story/howto/bitmath/#DivAndModDivisionAndModulo
*/

uint8_t div20u8( uint16_t num )
{
	uint16_t a = num;
	uint16_t b = 20;
	uint16_t r = 0;
	uint16_t m = 1;
	while ( b < a )
	{
		b = b << 1;
		m = m << 1;
	}
	do 
	{
		if ( a >= b )
		{
			a = a - b;
			r = r + m;
		}
		b = b >> 1;
		m = m >> 1;
	} while ( m != 0 );
	return (uint8_t)r;
}

int main()
{
	uint32_t a = 10000;
	uint32_t b = 5;
	uint32_t r = 0;
	uint32_t m = 1;
	printf("%04x(%d) %04x(%3d) %04x(%3d) %04x(%3d)\n",a,a,b,b,r,r,m,m);
	while ( b < a )
	{
		b = b << 1;
		m = m << 1;
		printf("%04x(%3d) %04x(%3d) %04x(%3d) %04x(%3d)\n",a,a,b,b,r,r,m,m);
	}
	do
	{
		if ( a >= b )
		{
			a = a - b;
			r = r + m;
		}
		b = b >> 1;
		m = m >> 1;
		printf("%04x(%3d) %04x(%3d) %04x(%3d) %04x(%3d)\n",a,a,b,b,r,r,m,m);
	} while ( m != 0 );
	return 0;
}