aboutsummaryrefslogtreecommitdiff
path: root/extract-function.sh
diff options
context:
space:
mode:
authorGravatar Andres Erbsen <andreser@mit.edu>2017-06-18 15:46:40 -0400
committerGravatar Andres Erbsen <andreser@mit.edu>2017-06-18 15:47:33 -0400
commit87bf48fccf89460b8264bb5cedf6b0e966dde563 (patch)
tree4222fdb4f6e3b496623b28a26e994f07154e1a47 /extract-function.sh
parent989f6a49cb390b6279ee8ce0ed70eb993ff1b809 (diff)
compile X25519 C code from Makefile
Diffstat (limited to 'extract-function.sh')
-rwxr-xr-xextract-function.sh71
1 files changed, 71 insertions, 0 deletions
diff --git a/extract-function.sh b/extract-function.sh
new file mode 100755
index 000000000..4056f242c
--- /dev/null
+++ b/extract-function.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+case "$#" in
+ 0)
+ funcname=f
+ ;;
+ 1)
+ funcname="$1"
+ ;;
+ *)
+ exit 111
+ ;;
+esac
+
+cat <<EOF
+#include <stdint.h>
+#include <stdbool.h>
+#include <x86intrin.h>
+
+#include "$funcname.h"
+
+typedef unsigned int uint128_t __attribute__((mode(TI)));
+
+#undef force_inline
+#define force_inline __attribute__((always_inline))
+
+EOF
+
+lines=0
+show=false
+while IFS= read -r line; do
+ case "$line" in
+ *"λ '"*)
+ echo -n "void force_inline $funcname("
+ echo -n "uint64_t* out"
+ echo "$line" | grep -owP -- '\w+\d+' | \
+ while IFS= read -r arg; do
+ echo -n ", uint64_t $arg"
+ done
+ echo ')'
+ show=true
+ ;;
+ *"Return "*|*"return "*)
+ i=0
+ echo "$line" | \
+ sed 's:return::g' | sed 's:Return::g' | tr -d '(' | tr -d ')' | tr , '\n' | sed 's/^\s\+//g' | \
+ ( while IFS= read -r ret; do
+ echo "out[$i] = $ret;"
+ ((i++))
+ done;
+ seq 2 "$lines" | while IFS= read -r _; do
+ echo -n "}"
+ done
+ echo "}"
+ echo "// caller: uint64_t out[$i];" )
+ show=false
+ break
+ ;;
+ *)
+ case "$show" in
+ true)
+ ((lines++))
+ echo "{ $line" | \
+ sed s':^\([^,]*\) \([^, ]*\)\(\s*\),\(.*\)\(addcarryx.*\))\([; ]*\)$:\1 \2\3;\4_\5, \&\2)\6:' | \
+ sed s':^\([^,]*\) \([^, ]*\)\(\s*\),\(.*\)\(subborrow.*\))\([; ]*\)$:\1 \2\3;\4_\5, \&\2)\6:'
+ ;;
+ esac
+ ;;
+ esac
+done
+