ogl_beamforming

Ultrasound Beamforming Implemented with OpenGL
git clone anongit@rnpnr.xyz:ogl_beamforming.git
Log | Files | Refs | Feed | Submodules | LICENSE

Commit: c1ecb88aed169c4a3baabbeef7972e1910413c57
Parent: 61dff5f151e6b31211a1ad349c992880bbfea9c3
Author: Randy Palamar
Date:   Tue,  8 Oct 2024 15:18:40 -0600

implement basic float formatting

scientific notation still to come

Diffstat:
Mui.c | 8++++----
Mutil.c | 40+++++++++++++++++++++++++++++++---------
2 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/ui.c b/ui.c @@ -176,7 +176,7 @@ bmv_sprint(BPModifiableValue *bmv, Stream *s) { if (bmv->flags & MV_FLOAT) { f32 *value = bmv->value; - stream_append_f32(s, *value * bmv->scale); + stream_append_f64(s, *value * bmv->scale, 100); } else { ASSERT(bmv->flags & MV_INT); i32 *value = bmv->value; @@ -339,7 +339,7 @@ do_value_listing(s8 prefix, s8 suffix, f32 value, Font font, Arena a, Rect r) v2 suffix_p = {.x = r.pos.x + r.size.w - suffix_s.w, .y = r.pos.y}; Stream buf = stream_alloc(&a, 64); - stream_append_f32(&buf, value); + stream_append_f64(&buf, value, 100); v2 txt_p = {.x = r.pos.x + LISTING_LEFT_COLUMN_WIDTH, .y = r.pos.y}; draw_text(font, prefix, r.pos, 0, colour_from_normalized(FG_COLOUR)); @@ -679,7 +679,7 @@ draw_ui(BeamformerCtx *ctx, Arena arena) if (output_dim.x > 1e-6 && output_dim.y > 1e-6) { Stream buf = stream_alloc(&arena, 64); - stream_append_f32(&buf, -188.88f); + stream_append_f64(&buf, -188.8f, 10); stream_append_s8(&buf, s8(" mm")); v2 txt_s = measure_text(ctx->small_font, stream_to_s8(buf)); @@ -761,7 +761,7 @@ draw_ui(BeamformerCtx *ctx, Arena arena) DrawLineEx(start_pos.rl, end_pos.rl, 3, colour_from_normalized(FG_COLOUR)); buf.widx = 0; if (i == 0 && mm > 0) stream_append_s8(&buf, s8("+")); - stream_append_f32(&buf, mm); + stream_append_f64(&buf, mm, 10); stream_append_s8(&buf, s8(" mm")); draw_text(ctx->small_font, stream_to_s8(buf), txt_pos, rot[i], txt_colour); diff --git a/util.c b/util.c @@ -46,11 +46,21 @@ stream_to_s8(Stream s) } static void +stream_append_byte(Stream *s, u8 b) +{ + s->errors |= s->widx + 1 > s->cap; + if (!s->errors) + s->data[s->widx++] = b; +} + +static void stream_append_s8(Stream *s, s8 str) { s->errors |= (s->cap - s->widx) <= str.len; - for (size i = 0; !s->errors && i < str.len; i++) - s->data[s->widx++] = str.data[i]; + if (!s->errors) { + for (size i = 0; i < str.len; i++) + s->data[s->widx++] = str.data[i]; + } } static void @@ -84,17 +94,29 @@ stream_append_i64(Stream *s, i64 n) } static void -stream_append_f32(Stream *s, f32 f) +stream_append_f64(Stream *s, f64 f, i64 prec) { if (f < 0) { - stream_append_s8(s, s8("-")); + stream_append_byte(s, '-'); f *= -1; } - /* TODO */ - size remaining = s->cap - s->widx; - s->errors |= remaining <= snprintf(0, 0, "%0.02f", f); - if (!s->errors) - s->widx += snprintf((char *)(s->data + s->widx), remaining, "%0.02f", f); + + /* NOTE: round last digit */ + f += 0.5f / prec; + + if (f >= (f64)(-1UL >> 1)) { + stream_append_s8(s, s8("inf")); + } else { + u64 integral = f; + u64 fraction = (f - integral) * prec; + stream_append_u64(s, integral); + stream_append_byte(s, '.'); + for (i64 i = prec / 10; i > 1; i /= 10) { + if (i > fraction) + stream_append_byte(s, '0'); + } + stream_append_u64(s, fraction); + } } static void