From 230f803abb92020bc27442334c872f3601fd702f Mon Sep 17 00:00:00 2001 From: giles Date: Mon, 11 May 2026 04:54:36 +0000 Subject: [PATCH] ocaml: phase 5.1 count_bits.ml baseline (sum popcount 0..100 = 319) DP recurrence for popcount that avoids host bitwise operations: result[i] = result[i / 2] + (i mod 2) Drops the low bit (i / 2 stands in for i lsr 1) and adds it back if it was 1 (i mod 2 stands in for i land 1). sum over 0..100 of popcount(i) = 319 Tests pure-arithmetic popcount, accumulating ref + DP array, classic look-back to half-index pattern. 197 baseline programs total. --- lib/ocaml/baseline/count_bits.ml | 14 ++++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 7 +++++++ 3 files changed, 22 insertions(+) create mode 100644 lib/ocaml/baseline/count_bits.ml diff --git a/lib/ocaml/baseline/count_bits.ml b/lib/ocaml/baseline/count_bits.ml new file mode 100644 index 00000000..a395c2f5 --- /dev/null +++ b/lib/ocaml/baseline/count_bits.ml @@ -0,0 +1,14 @@ +let count_bits n = + let result = Array.make (n + 1) 0 in + for i = 1 to n do + result.(i) <- result.(i / 2) + (i mod 2) + done; + let sum = ref 0 in + for i = 0 to n do + sum := !sum + result.(i) + done; + !sum + +;; + +count_bits 100 diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index a0d601ac..7259d399 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -32,6 +32,7 @@ "convex_hull.ml": 5, "coin_change.ml": 6, "coin_min.ml": 6, + "count_bits.ml": 319, "count_change.ml": 406, "count_paths_dag.ml": 3, "count_inversions.ml": 12, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index ae9dfdc7..943d0d44 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,13 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-11 Phase 5.1 — count_bits.ml baseline (sum of popcount for + 0..100 = 319). DP recurrence: popcount(i) = popcount(i/2) + + (i mod 2) — drop the low bit and recurse, adding back the bit + if it was 1. Avoids needing host bitwise ops (i mod 2 stands in + for `i land 1`, i/2 for `i lsr 1`). Sum over 0..100 = 319. + Tests pure-arithmetic popcount, accumulating ref + array. + 197 baseline programs total. - 2026-05-11 Phase 5.1 — bs_rotated.ml baseline (binary search in rotated sorted array; encoded result -66). For [4;5;6;7;0;1;2]: - search 0 → index 4