aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhovertank3d <[email protected]>2025-01-18 23:27:52 +0100
committerhovertank3d <[email protected]>2025-01-18 23:28:53 +0100
commit1a5a86a5d4557200db20e20206bcdd7b9b2a7e55 (patch)
tree03aae452a7dc2d9f71cf7a83adbfd8e112ad562e
parent2aa29060c81fb52884b290603f1cf930259443b9 (diff)
downloadlzcnt.space-1a5a86a5d4557200db20e20206bcdd7b9b2a7e55.tar.xz
lzcnt.space-1a5a86a5d4557200db20e20206bcdd7b9b2a7e55.zip
refactor and add /about
-rw-r--r--about.go24
-rwxr-xr-xbuild.sh2
-rw-r--r--cmd/main.go19
-rw-r--r--log2.s7
-rw-r--r--main.go53
-rw-r--r--server.go67
-rw-r--r--templates/gen.go1
-rw-r--r--templates/index.html18
-rw-r--r--templates/link.html1
-rw-r--r--templates/log2.s.html11
-rw-r--r--templates/reset.html2
-rw-r--r--templates/source_code.html1
12 files changed, 133 insertions, 73 deletions
diff --git a/about.go b/about.go
new file mode 100644
index 0000000..e005d55
--- /dev/null
+++ b/about.go
@@ -0,0 +1,24 @@
+// i made this site to serve some of my future demos and fuuny
+// projects. currently there are only couple of interesting things i wrote:
+
+// * {{ template "link.html" (arr "46load" "https://github.com/hovertank3d/46load")}},
+// * {{ template "link.html" (arr "fire" "https://github.com/hovertank3d/fire")}},
+// * {{ template "link.html" (arr "bfc" "https://github.com/hovertank3d/bfc")}},
+// * {{ template "link.html" (arr "enigma16" "https://github.com/hovertank3d/enigma16")}},
+
+// so, come back when i'll write something
+
+// also, this site uses {{ template "link.html" (arr "genhl" "https://github.com/hovertank3d/genhl")}} to go:generate syntax highlight
+
+package lzcnt
+
+import "net/http"
+
+func (s *Server) aboutPage(w http.ResponseWriter, r *http.Request) {
+ payload := map[string]any{
+ "Page": "about.go.html",
+ "Title": "About",
+ }
+
+ tmpl.ExecuteTemplate(w, "index.html", payload)
+}
diff --git a/build.sh b/build.sh
index 42d2c23..bf7ea6c 100755
--- a/build.sh
+++ b/build.sh
@@ -4,4 +4,4 @@ pushd deps/genhl
make
popd
go generate ./...
-go build -o lzcnt.space \ No newline at end of file
+go build -o lzcnt.space ./cmd \ No newline at end of file
diff --git a/cmd/main.go b/cmd/main.go
new file mode 100644
index 0000000..5e8b373
--- /dev/null
+++ b/cmd/main.go
@@ -0,0 +1,19 @@
+package main
+
+import (
+ "os"
+
+ "github.com/hovertank3d/lzcnt.space"
+)
+
+import "C"
+
+func main() {
+ host := ":8080"
+ if len(os.Args) == 2 {
+ host = os.Args[1]
+ }
+
+ srv := lzcnt.New()
+ srv.ListenAndServe(host)
+}
diff --git a/log2.s b/log2.s
index 910f6c7..cae631b 100644
--- a/log2.s
+++ b/log2.s
@@ -1,10 +1,13 @@
+# {{template "link.html" (arr "about" "/about")}}
+# {{template "reset.html" .}}
.section .text
.global log2lzcnt
.type log2lzcnt, @function
-# {{template "source_code.html" .}}
-# {{template "reset.html" .}}
log2lzcnt: # log2lzcnt({{.Requests}});
lzcnt %rdi, %rdi
movq $63, %rax
sub %rdi, %rax
ret # 2^{{.Log2lzcnt}} requests handled
+
+# copyleft 2025 iskrim46
+# {{template "link.html" (arr "source" "https://github.com/hovertank3d/lzcnt.space")}}
diff --git a/main.go b/main.go
deleted file mode 100644
index b7ae65c..0000000
--- a/main.go
+++ /dev/null
@@ -1,53 +0,0 @@
-package main
-
-import (
- "html/template"
- "log"
- "net/http"
- "os"
- "sync/atomic"
-
- "embed"
-)
-
-/*
-#include <stdint.h>
-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("/reset", func(w http.ResponseWriter, r *http.Request) {
- counter.Store(0)
- http.Redirect(w, r, "/", http.StatusSeeOther)
- })
-
- 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/server.go b/server.go
new file mode 100644
index 0000000..6ea8e89
--- /dev/null
+++ b/server.go
@@ -0,0 +1,67 @@
+package lzcnt
+
+import (
+ "embed"
+ "html/template"
+ "net/http"
+ "sync/atomic"
+)
+
+/*
+#include <stdint.h>
+extern unsigned long int log2lzcnt(unsigned long int);
+*/
+import "C"
+
+var (
+ //go:embed templates
+ templates embed.FS
+
+ tmpl = template.New("")
+)
+
+func init() {
+ tmpl = tmpl.Funcs(template.FuncMap{
+ "arr": func(els ...any) []any {
+ return els
+ },
+ })
+
+ tmpl = template.Must(tmpl.ParseFS(templates, "templates/*.html"))
+}
+
+type Server struct {
+ mux *http.ServeMux
+ requestCounter atomic.Uint64
+}
+
+func New() *Server {
+ srv := &Server{
+ mux: http.NewServeMux(),
+ }
+
+ srv.mux.HandleFunc("/reset", func(w http.ResponseWriter, r *http.Request) {
+ srv.requestCounter.Store(0)
+ http.Redirect(w, r, "/", http.StatusSeeOther)
+ })
+
+ srv.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ n := srv.requestCounter.Add(1)
+ payload := map[string]any{
+ "Page": "log2.s.html",
+ "Title": "Count the Number of Leading Zero Bits",
+ "Requests": n,
+ "Log2lzcnt": uint64(C.log2lzcnt(C.uint64_t(n))),
+ }
+
+ tmpl.ExecuteTemplate(w, "index.html", payload)
+ })
+
+ srv.mux.HandleFunc("/about", srv.aboutPage)
+
+ return srv
+}
+
+func (s *Server) ListenAndServe(host string) error {
+ return http.ListenAndServe(host, s.mux)
+}
diff --git a/templates/gen.go b/templates/gen.go
index dbbaf24..0b5f5ab 100644
--- a/templates/gen.go
+++ b/templates/gen.go
@@ -1,3 +1,4 @@
package templates
//go:generate bash -c "cat ../log2.s | ../deps/genhl/genhl asmatt > log2.s.html"
+//go:generate bash -c "cat ../about.go | ../deps/genhl/genhl > about.go.html"
diff --git a/templates/index.html b/templates/index.html
index 85eccab..3ae5cf4 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -19,16 +19,26 @@
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<title>LZCNT</title>
-
- <form id="reset" action="/reset" method="get"></form>
</head>
+<style>
+ body {
+ background-color: rgb(22,22,22);
+ color: white;
+ }
+</style>
+
<body>
<div style="display: flex; justify-content: center;">
- <h2 style="text-align: center;"><code>LZCNT - Count the Number of Leading Zero Bits</code></h2>
+ <h2 style="text-align: center;"><code><a href="/">LZCNT</a> - {{.Title}}</code></h2>
</div>
<div style="width: fit-content; margin-left: auto; margin-right: auto;">
- {{template "log2.s.html" .}}
+ {{ if eq .Page "log2.s.html" }}
+ {{template "log2.s.html" .}}
+ {{ end }}
+ {{ if eq .Page "about.go.html"}}
+ {{template "about.go.html" .}}
+ {{ end }}
</div>
</body>
diff --git a/templates/link.html b/templates/link.html
new file mode 100644
index 0000000..9a43d43
--- /dev/null
+++ b/templates/link.html
@@ -0,0 +1 @@
+<a href="{{index . 1}}">[{{index . 0}}]</a> \ No newline at end of file
diff --git a/templates/log2.s.html b/templates/log2.s.html
deleted file mode 100644
index f2fb03b..0000000
--- a/templates/log2.s.html
+++ /dev/null
@@ -1,11 +0,0 @@
-<style>.stx_root{color: rgb(255,255,255);background-color: rgb(33,33,33);}.stx2{color: rgb(220,150,28);font-weight: bold;}.stx3{color: rgb(181,120,206);}.stx4{color: rgb(70,70,240);}.stx5{color: rgb(254,254,91);font-weight: bold;}.stx6{color: rgb(115,158,213);font-weight: bold;}.stx7{color: rgb(24,212,236);}.stx8{color: rgb(139,15,127);font-weight: bold;}.stx9{color: rgb(11,84,118);}.stxa{color: rgb(31,145,31);font-weight: bold;}.stxb{color: rgb(0,0,0);}</style><div class="stx_root"><pre><code> <span class="stx8">.section</span> <span class="stx3">.text</span>
- <span class="stx8">.global</span> <span class="stx3">log2lzcnt</span>
- <span class="stx8">.type</span> <span class="stx3">log2lzcnt</span>, <span class="stx3">@function</span>
-<span class="stxa"># {{template "source_code.html" .}}
-</span><span class="stxa"># {{template "reset.html" .}}
-</span><span class="stx9">log2lzcnt:</span> <span class="stxa"># log2lzcnt({{.Requests}});
-</span> <span class="stx5">lzcnt</span> <span class="stx6">%rdi</span>, <span class="stx6">%rdi</span>
- <span class="stx5">movq</span> <span class="stx3">$63</span>, <span class="stx6">%rax</span>
- <span class="stx5">sub</span> <span class="stx6">%rdi</span>, <span class="stx6">%rax</span>
- <span class="stx5">ret</span> <span class="stxa"># 2^{{.Log2lzcnt}} requests handled
-</span></code></pre></div> \ No newline at end of file
diff --git a/templates/reset.html b/templates/reset.html
index 090b273..d6bfa7e 100644
--- a/templates/reset.html
+++ b/templates/reset.html
@@ -1 +1 @@
-<a href="javascript:{}" onclick="document.getElementById('reset').submit(); return false;">[reset counter]</a> \ No newline at end of file
+<a href="javascript:{}" onclick="document.getElementById('reset').submit(); return false;">[reset counter]</a><form id="reset" action="/reset" method="get"></form> \ No newline at end of file
diff --git a/templates/source_code.html b/templates/source_code.html
deleted file mode 100644
index 631c649..0000000
--- a/templates/source_code.html
+++ /dev/null
@@ -1 +0,0 @@
-<a href="https://github.com/hovertank3d/lzcnt.space">[source code]</a> \ No newline at end of file