summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjonsykkel <jonrevold@gmail.com>2021-09-08 08:31:32 +0200
committerjonsykkel <jonrevold@gmail.com>2021-09-08 08:33:28 +0200
commit1cf15ef93e09b692f566096fb72ff11cc04b76e4 (patch)
tree8f2a60bad4710909cdc4f19f4a0cb8ec9d75cef6
parent61d9e9ad4a4e80d2a43592b492e64f3b0562bb21 (diff)
downloadokeffa-1cf15ef93e09b692f566096fb72ff11cc04b76e4.tar.gz
fz_div
-rw-r--r--inc/okeffa/fz_div.h11
-rw-r--r--src/fz_div.c41
2 files changed, 52 insertions, 0 deletions
diff --git a/inc/okeffa/fz_div.h b/inc/okeffa/fz_div.h
new file mode 100644
index 0000000..6f84437
--- /dev/null
+++ b/inc/okeffa/fz_div.h
@@ -0,0 +1,11 @@
+#ifndef OKEFFA_FZ_DIV_H
+#define OKEFFA_FZ_DIV_H
+
+#include <okeffa/word.h>
+
+void fz_idiv (word_t *a,word_t *b,word_t *q,word_t *r,size_t len);
+void fz_div (word_t *a,word_t *b,word_t *q,size_t len);
+void fz_mod (word_t *a,word_t *b,word_t *r,size_t len);
+
+#endif
+
diff --git a/src/fz_div.c b/src/fz_div.c
new file mode 100644
index 0000000..77b2200
--- /dev/null
+++ b/src/fz_div.c
@@ -0,0 +1,41 @@
+#include <okeffa/fz_div.h>
+#include <okeffa/fz_basic.h>
+#include <okeffa/fz_arith.h>
+#include <okeffa/fz_shift.h>
+#include <okeffa/fz_bitop.h>
+#include <okeffa/fz.h>
+#include <okeffa/w_op.h>
+#include <assert.h>
+
+void fz_idiv(word_t *a,word_t *b,word_t *q_out,word_t *r_out,size_t len){
+ assert(len <= FZ_MAXLEN);
+ word_t qr[FZ_MAXLEN*2];
+ size_t qr_len = len*2;
+ word_t *q = qr;
+ word_t *r = qr+len;
+
+ fz_copy(a,q,len);
+ fz_clear(r,len);
+ for(size_t x = 0;x < len;x++){
+ wbool_t c;
+ fz_shl(qr,qr,qr_len,1);
+ c = fz_sub(r,b,r,len);
+ fz_add_gated(r,b,r,len,c);
+ fz_or_w(q,wb_not(c));
+ }
+ fz_copy(q,q_out,len);
+ fz_copy(r,r_out,len);
+}
+
+void fz_div(word_t *a,word_t *b,word_t *q,size_t len){
+ assert(len <= FZ_MAXLEN);
+ word_t r[FZ_MAXLEN];
+ fz_idiv(a,b,q,r,len);
+}
+
+void fz_mod(word_t *a,word_t *b,word_t *r,size_t len){
+ assert(len <= FZ_MAXLEN);
+ word_t q[FZ_MAXLEN];
+ fz_idiv(a,b,q,r,len);
+}
+