Last active
September 9, 2017 14:58
-
-
Save countingpine/be42c447c96844c37ad35313e03b06ae to your computer and use it in GitHub Desktop.
lngmath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void lngcpy(unsigned char *dst, int dst_length, unsigned char *const src, int src_length) { | |
if (dst_length <= src_length) { | |
memcpy(dst, src, dst_length); | |
} else { | |
/* src_length < dst_length */ | |
memcpy(dst, src, src_length); | |
memset(dst + src_length, 0, dst_length - src_length); | |
} | |
} | |
void lngadd(unsigned char *dst, int dst_length, unsigned char *const src, int src_length) { | |
int i = 0; | |
int carry = 0; | |
int tmp; | |
while (i < dst_length && i < src_length) { | |
tmp = carry + dst[i] + src[i]; | |
dst[i] = (unsigned char)(tmp & 0xff); | |
carry = tmp >> 8; | |
} | |
while (carry && i < dst_length) { | |
dst[i] = (unsigned char)(tmp & 0xff); | |
carry = tmp >> 8; | |
} | |
} | |
void lngsub(unsigned char *dst, int dst_length, unsigned char *const src, int src_length) { | |
int i = 0; | |
int carry = 0; | |
int tmp; | |
while (i < dst_length && i < src_length) { | |
tmp = carry + dst[i] - src[i]; | |
dst[i] = (unsigned char)(tmp & 0xff); | |
carry = tmp >> 8; | |
} | |
while (carry && i < dst_length) { | |
dst[i] = (unsigned char)(tmp & 0xff); | |
carry = tmp >> 8; | |
} | |
} | |
void lngmul(unsigned char *dst, int dst_length, unsigned char *const src, int src_length) { | |
int i, j, k; | |
unsigned long long tmp; | |
for (i = dst_length-1; i >= 0; i--) { | |
tmp = 0; | |
for (j = 0; j < src_length; j++) { | |
k = i - j; | |
if (k < 0) break; | |
tmp += src[j] * dst[k]; | |
} | |
dst[i] = (tmp & 0xff); tmp >>= 8; | |
k = i+1; | |
while (k < dst_length && tmp) { | |
tmp += dst[k]; | |
dst[k] = tmp & 0xff; | |
tmp >>= 8; | |
k++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment