From 17d4bcc5d8a379c764990d3f4dbc4f2d8d35f8bb Mon Sep 17 00:00:00 2001 From: David Duvenaud Date: Sun, 2 Jul 2023 16:17:32 -0400 Subject: [PATCH 1/2] Replace floating point sizing formula in stack with integer operations. --- lib/prelude.dx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/prelude.dx b/lib/prelude.dx index 5aec70f91..60e9dc14b 100644 --- a/lib/prelude.dx +++ b/lib/prelude.dx @@ -2477,8 +2477,7 @@ struct Stack(h:Heap, a|Data) = def ensure_size_at_least(req_size:Nat) -> {State h} () = if req_size > self.buf_size() then - -- TODO: maybe this should use integer arithmetic? - new_buf_size = f_to_n $ 2.0 `pow` (ceil $ log2 $ n_to_f req_size) + new_buf_size = intpow2 (1 + natlog2 req_size) buf = self.unsafe_get_buffer() logical_size = self.size() cur_data = get $ unsafe_coerce(to=Ref(h, Fin logical_size => a), buf) From 41be2ec4844528db70fcc42437be3652d112532e Mon Sep 17 00:00:00 2001 From: David Duvenaud Date: Sun, 2 Jul 2023 17:08:37 -0400 Subject: [PATCH 2/2] Make new stack size arithmetic match the old one exactly. --- lib/fft.dx | 5 ----- lib/prelude.dx | 7 ++++++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/fft.dx b/lib/fft.dx index 6d9034c26..df6fece04 100644 --- a/lib/fft.dx +++ b/lib/fft.dx @@ -20,11 +20,6 @@ def odd_sized_palindrome(mid:a, seq:n=>a) -> ((n `Either` () `Either` n)=>a) giv Right () -> mid Right i -> seq[i] -def nextpow2(x:Nat) -> Nat = - case is_power_of_2 x of - True -> natlog2 x - False -> 1 + natlog2 x - '## Inner FFT functions data FTDirection = diff --git a/lib/prelude.dx b/lib/prelude.dx index 60e9dc14b..773978b03 100644 --- a/lib/prelude.dx +++ b/lib/prelude.dx @@ -2241,6 +2241,11 @@ def natlog2(x:Nat) -> Nat = False unsafe_nat_diff(tmp, 1) -- TODO: something less horrible +def nextpow2(x:Nat) -> Nat = + case is_power_of_2 x of + True -> natlog2 x + False -> 1 + natlog2 x + def general_integer_power( times:(a,a)->a, one:a, base:a, @@ -2477,7 +2482,7 @@ struct Stack(h:Heap, a|Data) = def ensure_size_at_least(req_size:Nat) -> {State h} () = if req_size > self.buf_size() then - new_buf_size = intpow2 (1 + natlog2 req_size) + new_buf_size = intpow2 $ nextpow2 req_size buf = self.unsafe_get_buffer() logical_size = self.size() cur_data = get $ unsafe_coerce(to=Ref(h, Fin logical_size => a), buf)