From dd4a4aaf8e7cab8e196f7452b8a3f89411cdf742 Mon Sep 17 00:00:00 2001 From: hovertank3d Date: Fri, 17 Jan 2025 15:52:03 +0100 Subject: initial commit --- go.mod | 3 +++ log2.s | 8 ++++++++ main.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ templates/index.html | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 go.mod create mode 100644 log2.s create mode 100644 main.go create mode 100644 templates/index.html diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7ee2f33 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/hovertank3d/lzcnt.space + +go 1.23.4 diff --git a/log2.s b/log2.s new file mode 100644 index 0000000..63499e6 --- /dev/null +++ b/log2.s @@ -0,0 +1,8 @@ + .section .text + .global log2lzcnt + .type log2lzcnt, @function +log2lzcnt: + lzcnt %rdi, %rdi + movq $63, %rax + sub %rdi, %rax + ret diff --git a/main.go b/main.go new file mode 100644 index 0000000..7eba79e --- /dev/null +++ b/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "html/template" + "log" + "net/http" + "os" + "sync/atomic" + + "embed" +) + +/* +#include +extern unsigned long int log2lzcnt(unsigned long int); +*/ +import "C" + +//go:embed templates +var templates embed.FS + +func main() { + counter := atomic.Uint64{} + host := ":8080" + if len(os.Args) == 2 { + host = os.Args[1] + } + + tmpl, err := template.ParseFS(templates, "templates/*.html") + if err != nil { + log.Fatal(err) + } + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + n := counter.Add(1) + payload := struct { + Requests uint64 + Log2lzcnt uint64 + }{ + Requests: n, + Log2lzcnt: uint64(C.log2lzcnt(C.uint64_t(n))), + } + + tmpl.Execute(w, payload) + }) + + http.ListenAndServe(host, nil) +} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..89e78bf --- /dev/null +++ b/templates/index.html @@ -0,0 +1,38 @@ + + + + + + LZCNT + + + + + + + + +

LZCNT - Count the Number of Leading Zero Bits

+
log2lzcnt:                  # log2lzcnt({{.Requests}});
+        lzcnt   %rdi, %rdi
+        movq    $63,  %rax
+        sub     %rdi, %rax
+        ret                 # 2^{{.Log2lzcnt}} requests handled
+
+ + \ No newline at end of file -- cgit v1.2.3