summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2015-10-14 21:49:10 +0100
committerFreeArtMan <dos21h@gmail.com>2015-10-14 21:49:10 +0100
commit34299217c0de1ad333eb692f3b7ad391a43251e9 (patch)
tree709c76edd48f2be49ceaf88079ef8579d676bb26
parenta6e5e403fe377ae20efe1d0aef1808e02d7bcea1 (diff)
downloadradiola-34299217c0de1ad333eb692f3b7ad391a43251e9.tar.gz
radiola-34299217c0de1ad333eb692f3b7ad391a43251e9.zip
Divide by 20 only with shifting and substract
-rw-r--r--test/div20.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/test/div20.c b/test/div20.c
new file mode 100644
index 0000000..1003ab8
--- /dev/null
+++ b/test/div20.c
@@ -0,0 +1,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;
+} \ No newline at end of file