Skip to content

Instantly share code, notes, and snippets.

@MasterDuke17
Created August 31, 2019 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasterDuke17/a93f11587b8ad9a1c30bc6c3d182edf3 to your computer and use it in GitHub Desktop.
Save MasterDuke17/a93f11587b8ad9a1c30bc6c3d182edf3 to your computer and use it in GitHub Desktop.
diff --git a/demo/main.c b/demo/main.c
index 4cebb12..70bbcaf 100644
--- a/demo/main.c
+++ b/demo/main.c
@@ -1,50 +1,145 @@
#include "shared.h"
+#include <time.h>
+#include <unistd.h>
+#include <inttypes.h>
-int mtest_opponent(void);
-int unit_tests(int argc, char **argv);
-
-void ndraw(mp_int *a, const char *name)
+/* RDTSC from Scott Duplichan */
+static uint64_t TIMFUNC(void)
{
- char *buf;
- int size;
-
- mp_radix_size(a, 10, &size);
- buf = (char *)malloc((size_t) size);
- if (buf == NULL) {
- fprintf(stderr, "\nndraw: malloc(%d) failed\n", size);
- exit(EXIT_FAILURE);
- }
-
- printf("%s: ", name);
- mp_toradix(a, buf, 10);
- printf("%s\n", buf);
- mp_toradix(a, buf, 16);
- printf("0x%s\n", buf);
+#if defined __GNUC__
+#if defined(__i386__) || defined(__x86_64__)
+ /* version from http://www.mcs.anl.gov/~kazutomo/rdtsc.html
+ * the old code always got a warning issued by gcc, clang did not complain...
+ */
+ unsigned hi, lo;
+ __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
+ return ((uint64_t)lo)|(((uint64_t)hi)<<32);
+#else /* gcc-IA64 version */
+ unsigned long result;
+ __asm__ __volatile__("mov %0=ar.itc":"=r"(result)::"memory");
- free(buf);
-}
+ while (__builtin_expect((int) result == -1, 0))
+ __asm__ __volatile__("mov %0=ar.itc":"=r"(result)::"memory");
-int main(int argc, char **argv)
-{
-#ifdef MP_8BIT
- printf("Digit size 8 Bit \n");
-#endif
-#ifdef MP_16BIT
- printf("Digit size 16 Bit \n");
+ return result;
#endif
-#ifdef MP_32BIT
- printf("Digit size 32 Bit \n");
+
+ /* Microsoft and Intel Windows compilers */
+#elif defined _M_IX86
+ __asm rdtsc
+#elif defined _M_AMD64
+ return __rdtsc();
+#elif defined _M_IA64
+#if defined __INTEL_COMPILER
+#include <ia64intrin.h>
#endif
-#ifdef MP_64BIT
- printf("Digit size 64 Bit \n");
+ return __getReg(3116);
+#else
+#error need rdtsc function for this build
#endif
- printf("Size of mp_digit: %u\n", (unsigned int)sizeof(mp_digit));
- printf("Size of mp_word: %u\n", (unsigned int)sizeof(mp_word));
- printf("MP_DIGIT_BIT: %d\n", MP_DIGIT_BIT);
- printf("MP_PREC: %d\n", MP_PREC);
+}
+
+int main(void)
+{
+ uint64_t tt, gg, CLK_PER_SEC;
+ CLK_PER_SEC = TIMFUNC();
+ sleep(1);
+ CLK_PER_SEC = TIMFUNC() - CLK_PER_SEC;
+
+ printf("CLK_PER_SEC == %" PRIu64 "\n", CLK_PER_SEC);
+ for (int i = 1; i <= 100001; i *= 2) {
+ mp_int a, b;
+ char *buf_a, *buf_b, *buf_c, *buf_a2, *buf_b2, *buf_c2;
+ int size_a, size_b, size_c, size_c2;
+ unsigned rr;
+ mp_init(&a);
+ mp_2expt(&a, i);
+ mp_init_i32(&b, 13);
+ mp_expt_u32(&b, i, &b);
- if (LTM_DEMO_TEST_VS_MTEST) {
- return mtest_opponent();
+ mp_radix_size(&a, 10, &size_a);
+ mp_radix_size(&b, 10, &size_b);
+ mp_radix_size(&a, 16, &size_c);
+ mp_radix_size(&b, 16, &size_c2);
+ buf_a = (char *)malloc((size_t) size_a);
+ buf_b = (char *)malloc((size_t) size_b);
+ buf_c = (char *)malloc((size_t) size_c);
+ buf_a2 = (char *)malloc((size_t) size_a);
+ buf_b2 = (char *)malloc((size_t) size_b);
+ buf_c2 = (char *)malloc((size_t) size_c2);
+ rr = 0u;
+ tt = UINT64_MAX;
+ do {
+ gg = TIMFUNC();
+ mp_tohex(&a, buf_c);
+ gg = (TIMFUNC() - gg) >> 1;
+ if (tt > gg)
+ tt = gg;
+ } while (++rr < 100u);
+ printf("mp_tohex of 2**%d => %9" PRIu64 "/sec, %9" PRIu64 " cycles, %0.4f seconds\n",
+ i, CLK_PER_SEC / tt, tt, (float) tt / (float) CLK_PER_SEC);
+ rr = 0u;
+ tt = UINT64_MAX;
+ do {
+ gg = TIMFUNC();
+ mp_toradix(&a, buf_a, 10);
+ gg = (TIMFUNC() - gg) >> 1;
+ if (tt > gg)
+ tt = gg;
+ } while (++rr < 100u);
+ printf("mp_torad of 2**%d => %9" PRIu64 "/sec, %9" PRIu64 " cycles, %0.4f seconds\n",
+ i, CLK_PER_SEC / tt, tt, (float) tt / (float) CLK_PER_SEC);
+ rr = 0u;
+ tt = UINT64_MAX;
+ do {
+ gg = TIMFUNC();
+ mp_todecimal(&a, buf_a2);
+ gg = (TIMFUNC() - gg) >> 1;
+ if (tt > gg)
+ tt = gg;
+ } while (++rr < 100u);
+ printf("mp_todec of 2**%d => %9" PRIu64 "/sec, %9" PRIu64 " cycles, %0.4f seconds\n",
+ i, CLK_PER_SEC / tt, tt, (float) tt / (float) CLK_PER_SEC);
+ rr = 0u;
+ tt = UINT64_MAX;
+ do {
+ gg = TIMFUNC();
+ mp_tohex(&b, buf_c2);
+ gg = (TIMFUNC() - gg) >> 1;
+ if (tt > gg)
+ tt = gg;
+ } while (++rr < 100u);
+ printf("mp_tohex of 13**%d => %9" PRIu64 "/sec, %9" PRIu64 " cycles, %0.4f seconds\n",
+ i, CLK_PER_SEC / tt, tt, (float) tt / (float) CLK_PER_SEC);
+ rr = 0u;
+ tt = UINT64_MAX;
+ do {
+ gg = TIMFUNC();
+ mp_toradix(&b, buf_b, 10);
+ gg = (TIMFUNC() - gg) >> 1;
+ if (tt > gg)
+ tt = gg;
+ } while (++rr < 100u);
+ printf("mp_torad of 13**%d => %9" PRIu64 "/sec, %9" PRIu64 " cycles, %0.4f seconds\n",
+ i, CLK_PER_SEC / tt, tt, (float) tt / (float) CLK_PER_SEC);
+ rr = 0u;
+ tt = UINT64_MAX;
+ do {
+ gg = TIMFUNC();
+ mp_todecimal(&b, buf_b2);
+ gg = (TIMFUNC() - gg) >> 1;
+ if (tt > gg)
+ tt = gg;
+ } while (++rr < 100u);
+ printf("mp_todec of 13**%d => %9" PRIu64 "/sec, %9" PRIu64 " cycles, %0.4f seconds\n\n",
+ i, CLK_PER_SEC / tt, tt, (float) tt / (float) CLK_PER_SEC);
+ free(buf_a);
+ free(buf_b);
+ free(buf_c);
+ free(buf_a2);
+ free(buf_b2);
+ free(buf_c2);
}
- return unit_tests(argc, argv);
+
+ return 0;
}
diff --git a/makefile b/makefile
index fabd2c4..f5273bd 100644
--- a/makefile
+++ b/makefile
@@ -21,7 +21,7 @@ include makefile_include.mk
ifneq ($V,1)
@echo " * ${CC} $@"
endif
- ${silent} ${CC} -c ${CFLAGS} $< -o $@
+ ${silent} ${CC} -g -c ${CFLAGS} $< -o $@
LCOV_ARGS=--directory .
@@ -102,6 +102,9 @@ uninstall:
test: demo/main.o demo/opponent.o demo/test.o $(LIBNAME)
$(CC) $(CFLAGS) $^ $(LFLAGS) -o test
+main: demo/main.o $(LIBNAME)
+ $(CC) -g $(CFLAGS) $^ $(LFLAGS) -o main
+
test_standalone: demo/main.o demo/opponent.o demo/test.o $(LIBNAME)
$(CC) $(CFLAGS) $^ $(LFLAGS) -o test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment