From 2300d2813a524cbfeabac794335e7abe99263df6 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Tue, 29 Dec 2015 23:10:50 -0500 Subject: initial commit --- dfoxtest.c | 210 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 dfoxtest.c (limited to 'dfoxtest.c') diff --git a/dfoxtest.c b/dfoxtest.c new file mode 100644 index 0000000..aefdd5d --- /dev/null +++ b/dfoxtest.c @@ -0,0 +1,210 @@ +/*#include */ +typedef char int8_t; +typedef unsigned char uint8_t; +typedef short int16_t; +typedef unsigned short uint16_t; +typedef long int32_t; +typedef unsigned long uint32_t; + +/*#include */ +typedef uint8_t bool; +#ifndef true + #define true 1 +#endif +#ifndef false + #define false 0 +#endif + +/*Defines*/ +#define PREFIX_BITS_PER_BYTE 8 + /*Neither can be greater than 128*/ +#define PREFIX_intsize 4 +#define PREFIX_INTSIZE 8 + +/*Typedefs*/ +typedef uint8_t PREFIX_INT[PREFIX_INTSIZE]; +typedef int32_t PREFIX_int; + +/*Globals*/ +bool PREFIX_math_overflow; + +/*==============================================================CODE=============================================================*/ + +void PREFIX_zero_int(PREFIX_INT *ptr) +{ + uint8_t i; + + for (i = 0; i < PREFIX_INTSIZE; i++) + *ptr[i] = 0; +} + +bool PREFIX_isneg(PREFIX_INT *in) +{ + return (*in[PREFIX_INTSIZE-1] & 0x80) >> 7; +} + +void PREFIX_int_to_buf(PREFIX_INT *dest, PREFIX_int *src) +{ + PREFIX_int tmp; + uint8_t i; + + tmp = *src; + for (i = 0; i < PREFIX_intsize; i++) + *dest[i] = (tmp >> (i*PREFIX_BITS_PER_BYTE)) & 0xFF; +} + +void PREFIX_buf_to_int(PREFIX_int *dest, PREFIX_INT *src) +{ + PREFIX_int ret; + uint8_t i; + + ret = 0; + for (i = 0; i < PREFIX_INTSIZE; i++) { + ret += dest[i]; + ret <<= PREFIX_BITS_PER_BYTE; + } + + *src[0] = ret; +} + +void PREFIX_buf_to_buf(PREFIX_INT *dest, PREFIX_INT *src) +{ + uint8_t i; + + for (i = 0; i < PREFIX_INTSIZE; i++) + *dest[i] = *src[i]; +} + +/*True to*/ +bool PREFIX_equal(PREFIX_INT *left, PREFIX_INT *right) +{ + uint8_t i; + + for (i = 0; i < PREFIX_INTSIZE; i++) + if (*left[i] != *right[i]) + return false; + + return true; +} + +/*greater than*/ +bool PREFIX_greater(PREFIX_INT *left, PREFIX_INT *right) +{ + uint8_t i; + bool left_neg, right_neg; + + left_neg = PREFIX_isneg(left); + right_neg = PREFIX_isneg(right); + + if (left_neg || right_neg) { + if (left_neg && !right_neg) + return false; + else if (!left_neg && right_neg) + return true; + } + + for (i = 0; i < PREFIX_INTSIZE; i++) { + if (*left[i] > *right[i]) + return true; + else if (*left[i] < *right[i]) + return false; + } + + return false; +} + +/*less than*/ +bool PREFIX_less(PREFIX_INT *left, PREFIX_INT *right) +{ + return !PREFIX_greater(left, right); +} + +/*greater than or equal to*/ +bool PREFIX_greater_or_equal(PREFIX_INT *left, PREFIX_INT *right) +{ + return PREFIX_greater(left, right) || PREFIX_equal(left, right); +} + +/*less than or equal to*/ +bool PREFIX_less_or_equal(PREFIX_INT *left, PREFIX_INT *right) +{ + return PREFIX_less(left, right) || PREFIX_equal(left, right); +} + +/*Negatives not currently supported*/ +void PREFIX_add(PREFIX_INT *dest, PREFIX_INT *src) +{ + /*operator*/ + uint16_t op; + uint8_t i; + bool dest_neg, src_neg; + + src_neg = PREFIX_isneg(src); + dest_neg = PREFIX_isneg(dest); + + if (src_neg || dest_neg) { + PREFIX_math_overflow = true; + return; + } + + for (i = 0; i < PREFIX_INTSIZE; i++) { + op = *src[i]; + op += *dest[i]; + *dest[i] = op & 0xFF; + + op >>= PREFIX_BITS_PER_BYTE; + } + + if (i == PREFIX_INTSIZE && op != 0) + PREFIX_math_overflow = true; + + if (!(src_neg || dest_neg) && PREFIX_isneg(dest)) + PREFIX_math_overflow = true; +} + +/*Negatives not currently supported*/ +void PREFIX_sub(PREFIX_INT *dest, PREFIX_INT *src) +{ + uint16_t op; + uint8_t i, j; + bool dest_neg, src_neg; + uint16_t tmp_op = 0; + bool got_borrow = false; + PREFIX_INT tmp_dest, tmp_src; + + src_neg = PREFIX_isneg(src); + dest_neg = PREFIX_isneg(dest); + + if (src_neg || dest_neg) + PREFIX_math_overflow = true; + + PREFIX_buf_to_buf(&tmp_src, src); + PREFIX_buf_to_buf(&tmp_dest, dest); + for (i = 0; i < PREFIX_INTSIZE; i++) { + op = tmp_dest[i]; + + if (tmp_src[i] > tmp_dest[i]) { + if (i == (PREFIX_INTSIZE-1)) + PREFIX_math_overflow = true; + + for (j = (i+1); j < PREFIX_INTSIZE; j++ ) + if (tmp_dest[j]) { + tmp_dest[j] -= 1; + tmp_op = 0xFF; + got_borrow = true; + break; + } + + if (!got_borrow) + PREFIX_math_overflow = true; + + for (j--; j > i; j--) + tmp_src[j] = 0xFF; + + op += tmp_op; + } + + op -= tmp_src[i]; + *dest[i] = op & 0xFF; + } +} -- cgit v1.2.3