From f5bb931b88ea967b5e440aeab6b0c4d21b76759b Mon Sep 17 00:00:00 2001 From: Alessandro Piccini Date: Mon, 24 Aug 2026 19:08:20 +0200 Subject: [PATCH 1/2] Add OBPMark 1.1 port to Astral PULP cluster + host reference - Cluster benchmarks (sw/tests/bare-metal/pulpd-benchmarks/): sequential, 8-core parallel, and 16-bit SIMD variants (offset/bin vectorization, partial and add2div2 scrub) with per-kernel cycle profiling - Host CVA6 reference implementations (sequential and SMP) in hostd/ - Modified offloader and l2_bigstack.ld linker script for SMP stack sizing - README documenting the build/simulation flow --- .gitignore | 11 + Bender.lock | 8 +- sw/link/l2_bigstack.ld | 6 + sw/tests/bare-metal/hostd/obpmark_1_1.c | 310 +++++++++++++++ .../bare-metal/hostd/obpmark_1_1_seq_32.c | 269 +++++++++++++ sw/tests/bare-metal/hostd/obpmark_1_1_smp.c | 373 ++++++++++++++++++ .../bare-metal/hostd/obpmark_1_1_smp_32.c | 315 +++++++++++++++ .../hostd/pulpd_offloader_blocking.c | 5 + .../bare-metal/pulpd-benchmarks/README.md | 177 +++++++++ .../pulpd-benchmarks/obpmark_1_1/Makefile | 6 + .../obpmark_1_1/obpmark_1_1_pulp.c | 306 ++++++++++++++ .../pulpd-benchmarks/obpmark_1_1_par/Makefile | 4 + .../obpmark_1_1_par/obpmark_1_1_pulp_par.c | 290 ++++++++++++++ .../obpmark_1_1_par_simd/Makefile | 4 + .../obpmark_1_1_pulp_par_simd.c | 321 +++++++++++++++ .../obpmark_1_1_par_simd_v2_prof/Makefile | 4 + .../obpmark_1_1_pulp_par_simd_v2_prof.c | 367 +++++++++++++++++ .../obpmark_1_1_par_simd_v3_prof/Makefile | 4 + .../obpmark_1_1_pulp_par_simd_v3_prof.c | 373 ++++++++++++++++++ .../pulpd-benchmarks/obpmark_1_1_seq/Makefile | 4 + .../obpmark_1_1_seq/obpmark_1_1_pulp_seq.c | 254 ++++++++++++ 21 files changed, 3407 insertions(+), 4 deletions(-) create mode 100644 sw/link/l2_bigstack.ld create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_1.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_1_seq_32.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_1_smp.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_1_smp_32.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/README.md create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1/obpmark_1_1_pulp.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par/obpmark_1_1_pulp_par.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd/obpmark_1_1_pulp_par_simd.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v2_prof/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v2_prof/obpmark_1_1_pulp_par_simd_v2_prof.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v3_prof/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v3_prof/obpmark_1_1_pulp_par_simd_v3_prof.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_seq/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_seq/obpmark_1_1_pulp_seq.c diff --git a/.gitignore b/.gitignore index 7433c473d..afe7e9f1e 100644 --- a/.gitignore +++ b/.gitignore @@ -200,3 +200,14 @@ pyrightconfig.json # End of https://www.toptal.com/developers/gitignore/api/python /nonfree/ + +# OBPMark / offload host build artifacts +sw/tests/bare-metal/**/*.car.dram.elf +sw/tests/bare-metal/**/*.car.dram.dump +sw/tests/bare-metal/**/*.car.l2.elf +sw/tests/bare-metal/**/*.car.l2.dump + +# QuestaSim simulation runtime artifacts +target/sim/vsim/uart +target/sim/vsim/wlft* +target/sim/vsim/vsim_stacktrace.vstf diff --git a/Bender.lock b/Bender.lock index 4ae7e94f8..9fc93a89b 100644 --- a/Bender.lock +++ b/Bender.lock @@ -191,8 +191,8 @@ packages: dependencies: - hci common_cells: - revision: 9afda9abb565971649c2aa0985639c096f351171 - version: 1.38.0 + revision: 9ca8a7655f741e7dd5736669a20a301325194c28 + version: 1.39.0 source: Git: https://github.com/pulp-platform/common_cells.git dependencies: @@ -501,8 +501,8 @@ packages: - register_interface - tech_cells_generic register_interface: - revision: 8e8c209ea559d3b54f45cf30fcce95ce70ff5e49 - version: 0.4.6 + revision: d6e1d4cdaab7870f4faf3f88a1c788eaf5ac129d + version: 0.4.7 source: Git: https://github.com/pulp-platform/register_interface.git dependencies: diff --git a/sw/link/l2_bigstack.ld b/sw/link/l2_bigstack.ld new file mode 100644 index 000000000..014aa0b5e --- /dev/null +++ b/sw/link/l2_bigstack.ld @@ -0,0 +1,6 @@ +/* Script linker custom: include lo script L2 originale, poi sovrascrive + * __stack_size$ con un valore maggiore (16 KiB invece di 4 KiB di default), + * necessario per evitare la sovrapposizione di stack tra i 2 hart nel + * porting SMP di OBPMark (pipeline a 6 funzioni innestate). */ +INCLUDE l2.ld +__stack_size$ = 0x4000; diff --git a/sw/tests/bare-metal/hostd/obpmark_1_1.c b/sw/tests/bare-metal/hostd/obpmark_1_1.c new file mode 100644 index 000000000..ea9d5b057 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_1.c @@ -0,0 +1,310 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.1 - Image Calibration and Correction +// Bare-metal port for Astral/Carfield (CVA6, RV64) +// +// Porting notes vs versione Linux originale: +// - Algorithm (processing.c) copiato identico +// - malloc/free ? array statici globali (no heap su bare-metal) +// - File I/O ? generazione sintetica con rand() seed 21121993 (come -r) +// - clock() POSIX ? csrr mcycle (cycle counter hardware RISC-V) +// - Argomenti CLI ? costanti hardcoded (no OS, no argv) +// - Output in ms usando ClkPeriodRef = 20ns (50 MHz, da astral_fix.sv) +// +// Build targets (stesso .c, linker script diverso): +// make sw/tests/bare-metal/hostd/obpmark_1_1.car.l2.elf (Dynamic SPM ~512KB) +// make sw/tests/bare-metal/hostd/obpmark_1_1.car.dram.elf (HyperRAM, nessun limite) +// +// Memoria occupata con IMG_W=IMG_H=128: +// 12 frame uint16: 12 x 128 x 128 x 2 = 393 KB +// offsets + gains: 2 x 128 x 128 x 2 = 64 KB +// bad_pixels: 128 x 128 x 1 = 16 KB +// output + binned: 2 x 64 x 64 x 4 = 32 KB +// TOTALE = ~505 KB + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include + +// ============================================================ +// Configurazione benchmark +// ============================================================ +#define IMG_W 128 +#define IMG_H 128 +#define NUM_FRAMES 8 +#define NUM_NEIGHBOURS 4 +#define TOTAL_FRAMES (NUM_FRAMES + NUM_NEIGHBOURS) +#define OFFSET_NEIGHBOURS 2 + +// Clock CVA6 in simulazione Astral (da astral_fix.sv: ClkPeriodRef = 20ns) +#define CVA6_CLK_PERIOD_NS 20ULL +#define CVA6_FREQ_HZ 50000000ULL + +// ============================================================ +// Strutture dati (identiche a obpmark_image.h originale, 1D flat buffer) +// ============================================================ +typedef struct { uint16_t *f; unsigned int w; unsigned int h; } frame16_t; +typedef struct { uint8_t *f; unsigned int w; unsigned int h; } frame8_t; +typedef struct { uint32_t *f; unsigned int w; unsigned int h; } frame32_t; + +#define PIXEL(frame, x, y) ((frame)->f[(y) * (frame)->w + (x)]) + +// ============================================================ +// Backing storage statico (sostituisce malloc) +// ============================================================ +static uint16_t _frames_data [TOTAL_FRAMES][IMG_H * IMG_W]; +static uint16_t _offsets_data [IMG_H * IMG_W]; +static uint16_t _gains_data [IMG_H * IMG_W]; +static uint8_t _badpix_data [IMG_H * IMG_W]; +static uint32_t _output_data [(IMG_H/2) * (IMG_W/2)]; +static uint32_t _binned_data [(IMG_H/2) * (IMG_W/2)]; + +static frame16_t frames[TOTAL_FRAMES]; +static frame16_t offsets_frame; +static frame16_t gains_frame; +static frame8_t bad_pixels_frame; +static frame32_t output_frame; +static frame32_t binned_frame; + +// ============================================================ +// init_frames: collega puntatori ai buffer statici +// (sostituisce device_memory_init + copy_memory_to_device) +// ============================================================ +static void init_frames(void) { + unsigned int i; + for (i = 0; i < TOTAL_FRAMES; i++) { + frames[i].f = _frames_data[i]; + frames[i].w = IMG_W; + frames[i].h = IMG_H; + } + offsets_frame.f = _offsets_data; offsets_frame.w = IMG_W; offsets_frame.h = IMG_H; + gains_frame.f = _gains_data; gains_frame.w = IMG_W; gains_frame.h = IMG_H; + bad_pixels_frame.f = _badpix_data; bad_pixels_frame.w = IMG_W; bad_pixels_frame.h = IMG_H; + output_frame.f = _output_data; output_frame.w = IMG_W/2; output_frame.h = IMG_H/2; + binned_frame.f = _binned_data; binned_frame.w = IMG_W/2; binned_frame.h = IMG_H/2; +} + +// ============================================================ +// Generazione dati sintetici (identica a util_data_rand.c originale) +// Seed 21121993 = BENCHMARK_RAND_SEED +// LCG compatibile con glibc rand() +// ============================================================ +static unsigned int _rand_state; + +static unsigned int bm_rand(void) { + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} + +static void gen_rand_data(void) { + unsigned int f, x, y; + const unsigned int randmax = 65535u; + _rand_state = 21121993u; + + for (f = 0; f < TOTAL_FRAMES; f++) + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&frames[f], x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&offsets_frame, x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&gains_frame, x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&bad_pixels_frame, x, y) = ((uint8_t)(bm_rand() % randmax)) & 0x01u; + + for (x = 0; x < IMG_W/2; x++) + for (y = 0; y < IMG_H/2; y++) + PIXEL(&output_frame, x, y) = 0u; +} + +// ============================================================ +// KERNEL FUNCTIONS — copiate identiche da processing.c originale +// ============================================================ + +// [I] Bias offset correction +static void f_offset(frame16_t *frame, frame16_t *offsets) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) -= PIXEL(offsets, x, y); +} + +// Helper per [II] +static uint32_t f_neighbour_masked_sum(frame16_t *frame, frame8_t *mask, + int x_mid, int y_mid) { + int x, y; + int x_start = -1, x_stop = 1, y_start = -1, y_stop = 1; + unsigned int n_sum = 0; + uint32_t sum = 0, mean; + + if (x_mid == 0) x_start = 0; + else if (x_mid == (int)(frame->w - 1)) x_stop = 0; + if (y_mid == 0) y_start = 0; + else if (y_mid == (int)(frame->h - 1)) y_stop = 0; + + for (x = x_start; x < (x_stop + 1); x++) + for (y = y_start; y < (y_stop + 1); y++) + if (PIXEL(mask, (x_mid+x), (y_mid+y)) == 0) { + sum += PIXEL(frame, (x_mid+x), (y_mid+y)); + n_sum++; + } + + mean = (n_sum == 0u) ? 0u : sum / n_sum; + return mean; +} + +// [II] Bad pixel correction +static void f_mask_replace(frame16_t *frame, frame8_t *mask) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + if (PIXEL(mask, x, y) == 1u) + PIXEL(frame, x, y) = (uint16_t)f_neighbour_masked_sum( + frame, mask, (int)x, (int)y); +} + +// [III] Radiation scrubbing +static void f_scrub(frame16_t *frame, frame16_t *fs, unsigned int frame_i) { + unsigned int x, y; + static unsigned int num_neighbour = 4u; + uint32_t sum, mean, thr; + + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) { + sum = PIXEL(&fs[frame_i-2], x, y) + PIXEL(&fs[frame_i-1], x, y) + + PIXEL(&fs[frame_i+1], x, y) + PIXEL(&fs[frame_i+2], x, y); + mean = sum / num_neighbour; + thr = 2u * mean; + if (PIXEL(frame, x, y) > thr) + PIXEL(frame, x, y) = (uint16_t)mean; + } +} + +// [IV] Gain correction +static void f_gain(frame16_t *frame, frame16_t *gains) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) = (uint16_t)( + ((uint32_t)PIXEL(frame, x, y) * + (uint32_t)PIXEL(gains, x, y)) >> 16); +} + +// [V] 2x2 Spatial binning +static void f_2x2_bin(frame16_t *frame, frame32_t *binned) { + unsigned int x, y, x2, y2; + x2 = 0u; + for (x = 0; x < frame->w; x += 2u) { + y2 = 0u; + for (y = 0; y < frame->h; y += 2u) { + PIXEL(binned, x2, y2) = PIXEL(frame, x, y ) + + PIXEL(frame, (x+1u), y ) + + PIXEL(frame, x, (y+1u)) + + PIXEL(frame, (x+1u),(y+1u)); + ++y2; + } + ++x2; + } +} + +// [VI] Frame co-adding +static void f_coadd(frame32_t *sum_frame, frame32_t *add_frame) { + unsigned int x, y; + for (x = 0; x < sum_frame->w; x++) + for (y = 0; y < sum_frame->h; y++) + PIXEL(sum_frame, x, y) += PIXEL(add_frame, x, y); +} + +// ============================================================ +// Pipeline identica a process_benchmark() di device.c originale +// ============================================================ +static void prepare_image_frame(frame16_t *frame) { + f_offset(frame, &offsets_frame); + f_mask_replace(frame, &bad_pixels_frame); +} + +static void proc_image_frame(frame16_t *frame, unsigned int frame_i) { + f_scrub(frame, frames, frame_i); + f_gain(frame, &gains_frame); + f_2x2_bin(frame, &binned_frame); + f_coadd(&output_frame, &binned_frame); +} + +// ============================================================ +// Cycle counter RISC-V (sostituisce clock() di obpmark_time.h) +// ============================================================ +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +// Conversione cicli ? tempo e throughput +// ClkPeriodRef = 20ns ? 50 MHz (da astral_fix.sv, modalita' senza TECH_SIM) +static void print_results(uint64_t cycles) { + uint64_t time_ns = cycles * CVA6_CLK_PERIOD_NS; + uint64_t time_us = time_ns / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac= time_us % 1000ULL; + + // throughput [Mpixel/s] = (W * H * N_frames * freq_Hz) / (cycles * 1e6) + // = (W * H * N_frames * 50) / cycles + uint64_t pixels = (uint64_t)IMG_W * (uint64_t)IMG_H * (uint64_t)NUM_FRAMES; + uint64_t tp_int = (pixels * 50ULL) / cycles; + uint64_t tp_frac = ((pixels * 50ULL) % cycles) * 100ULL / cycles; + + printf("Benchmark metrics:\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n",(unsigned long long)time_ms_int, + (unsigned long long)time_ms_frac); + printf(" Throughput : %llu.%02llu Mpixel/s\r\n", + (unsigned long long)tp_int, (unsigned long long)tp_frac); +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + if (hart_id() != 0) wfi(); + + car_init_start(); + + printf("OBPMark #1.1 - Image Calibration and Correction\r\n"); + printf("Bare-metal - Astral CVA6 RV64 @ 50 MHz\r\n"); + printf("Image: %dx%d px | Frames: %d\r\n", IMG_W, IMG_H, NUM_FRAMES); + + init_frames(); + gen_rand_data(); + + // Loop identico a process_benchmark() in device.c + uint64_t t_start = read_mcycle(); + + unsigned int frame_i; + for (frame_i = OFFSET_NEIGHBOURS; + frame_i < TOTAL_FRAMES - OFFSET_NEIGHBOURS; + frame_i++) { + prepare_image_frame(&frames[frame_i + 2u]); + proc_image_frame(&frames[frame_i], frame_i); + } + + uint64_t t_end = read_mcycle(); + + printf("Done.\r\n"); + print_results(t_end - t_start); + printf("Output[0][0]: %u\r\n", (unsigned int)PIXEL(&output_frame, 0, 0)); + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_1_seq_32.c b/sw/tests/bare-metal/hostd/obpmark_1_1_seq_32.c new file mode 100644 index 000000000..8fd075f5a --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_1_seq_32.c @@ -0,0 +1,269 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.1 - Image Calibration and Correction +// Bare-metal SEQUENZIALE per Astral (1x CVA6, RV64) - VERSIONE 32x32 +// +// Identica nella struttura e nel reporting alla versione 128x128 sequenziale +// gia' testata (stesso schema cicli/ms/throughput), solo IMG_W/IMG_H diversi +// e target L2 invece di DRAM (32x32 entra comodamente in L2). +// Serve come riferimento di correttezza per confrontare Output[0][0] con +// la versione SMP a 2 hart sulla stessa identica dimensione. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include + +// ============================================================ +// Configurazione benchmark +// ============================================================ +#define IMG_W 32 +#define IMG_H 32 +#define NUM_FRAMES 8 +#define NUM_NEIGHBOURS 4 +#define TOTAL_FRAMES (NUM_FRAMES + NUM_NEIGHBOURS) +#define OFFSET_NEIGHBOURS 2 + +#define CVA6_CLK_PERIOD_NS 20ULL +#define CVA6_FREQ_HZ 50000000ULL + +// ============================================================ +// Strutture dati +// ============================================================ +typedef struct { uint16_t *f; unsigned int w; unsigned int h; } frame16_t; +typedef struct { uint8_t *f; unsigned int w; unsigned int h; } frame8_t; +typedef struct { uint32_t *f; unsigned int w; unsigned int h; } frame32_t; + +#define PIXEL(frame, x, y) ((frame)->f[(y) * (frame)->w + (x)]) + +// ============================================================ +// Backing storage statico +// ============================================================ +static uint16_t _frames_data [TOTAL_FRAMES][IMG_H * IMG_W]; +static uint16_t _offsets_data [IMG_H * IMG_W]; +static uint16_t _gains_data [IMG_H * IMG_W]; +static uint8_t _badpix_data [IMG_H * IMG_W]; +static uint32_t _output_data [(IMG_H/2) * (IMG_W/2)]; +static uint32_t _binned_data [(IMG_H/2) * (IMG_W/2)]; + +static frame16_t frames[TOTAL_FRAMES]; +static frame16_t offsets_frame; +static frame16_t gains_frame; +static frame8_t bad_pixels_frame; +static frame32_t output_frame; +static frame32_t binned_frame; + +static void init_frames(void) { + unsigned int i; + for (i = 0; i < TOTAL_FRAMES; i++) { + frames[i].f = _frames_data[i]; + frames[i].w = IMG_W; + frames[i].h = IMG_H; + } + offsets_frame.f = _offsets_data; offsets_frame.w = IMG_W; offsets_frame.h = IMG_H; + gains_frame.f = _gains_data; gains_frame.w = IMG_W; gains_frame.h = IMG_H; + bad_pixels_frame.f = _badpix_data; bad_pixels_frame.w = IMG_W; bad_pixels_frame.h = IMG_H; + output_frame.f = _output_data; output_frame.w = IMG_W/2; output_frame.h = IMG_H/2; + binned_frame.f = _binned_data; binned_frame.w = IMG_W/2; binned_frame.h = IMG_H/2; +} + +// ============================================================ +// Generazione dati sintetici (stesso seed delle versioni SMP) +// ============================================================ +static unsigned int _rand_state; + +static unsigned int bm_rand(void) { + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} + +static void gen_rand_data(void) { + unsigned int f, x, y; + const unsigned int randmax = 65535u; + _rand_state = 21121993u; + + for (f = 0; f < TOTAL_FRAMES; f++) + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&frames[f], x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&offsets_frame, x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&gains_frame, x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&bad_pixels_frame, x, y) = ((uint8_t)(bm_rand() % randmax)) & 0x01u; + + for (x = 0; x < IMG_W/2; x++) + for (y = 0; y < IMG_H/2; y++) + PIXEL(&output_frame, x, y) = 0u; +} + +// ============================================================ +// KERNEL FUNCTIONS (sequenziali, su tutta la larghezza) +// ============================================================ +static void f_offset(frame16_t *frame, frame16_t *offsets) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) -= PIXEL(offsets, x, y); +} + +static uint32_t f_neighbour_masked_sum(frame16_t *frame, frame8_t *mask, + int x_mid, int y_mid) { + int x, y; + int x_start = -1, x_stop = 1, y_start = -1, y_stop = 1; + unsigned int n_sum = 0; + uint32_t sum = 0, mean; + + if (x_mid == 0) x_start = 0; + else if (x_mid == (int)(frame->w - 1)) x_stop = 0; + if (y_mid == 0) y_start = 0; + else if (y_mid == (int)(frame->h - 1)) y_stop = 0; + + for (x = x_start; x < (x_stop + 1); x++) + for (y = y_start; y < (y_stop + 1); y++) + if (PIXEL(mask, (x_mid+x), (y_mid+y)) == 0) { + sum += PIXEL(frame, (x_mid+x), (y_mid+y)); + n_sum++; + } + + mean = (n_sum == 0u) ? 0u : sum / n_sum; + return mean; +} + +static void f_mask_replace(frame16_t *frame, frame8_t *mask) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + if (PIXEL(mask, x, y) == 1u) + PIXEL(frame, x, y) = (uint16_t)f_neighbour_masked_sum( + frame, mask, (int)x, (int)y); +} + +static void f_scrub(frame16_t *frame, frame16_t *fs, unsigned int frame_i) { + unsigned int x, y; + static unsigned int num_neighbour = 4u; + uint32_t sum, mean, thr; + + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) { + sum = PIXEL(&fs[frame_i-2], x, y) + PIXEL(&fs[frame_i-1], x, y) + + PIXEL(&fs[frame_i+1], x, y) + PIXEL(&fs[frame_i+2], x, y); + mean = sum / num_neighbour; + thr = 2u * mean; + if (PIXEL(frame, x, y) > thr) + PIXEL(frame, x, y) = (uint16_t)mean; + } +} + +static void f_gain(frame16_t *frame, frame16_t *gains) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) = (uint16_t)( + ((uint32_t)PIXEL(frame, x, y) * + (uint32_t)PIXEL(gains, x, y)) >> 16); +} + +static void f_2x2_bin(frame16_t *frame, frame32_t *binned) { + unsigned int x, y; + for (x = 0; x < frame->w; x += 2) + for (y = 0; y < frame->h; y += 2) + PIXEL(binned, x/2, y/2) = PIXEL(frame, x, y ) + + PIXEL(frame, (x+1u), y ) + + PIXEL(frame, x, (y+1u)) + + PIXEL(frame, (x+1u),(y+1u)); +} + +static void f_coadd(frame32_t *sum_frame, frame32_t *add_frame) { + unsigned int x, y; + for (x = 0; x < sum_frame->w; x++) + for (y = 0; y < sum_frame->h; y++) + PIXEL(sum_frame, x, y) += PIXEL(add_frame, x, y); +} + +// ============================================================ +// Pipeline sequenziale +// ============================================================ +static void prepare_image_frame(frame16_t *frame) { + f_offset(frame, &offsets_frame); + f_mask_replace(frame, &bad_pixels_frame); +} + +static void proc_image_frame(frame16_t *frame, unsigned int frame_i) { + f_scrub(frame, frames, frame_i); + f_gain(frame, &gains_frame); + f_2x2_bin(frame, &binned_frame); + f_coadd(&output_frame, &binned_frame); +} + +// ============================================================ +// Cycle counter +// ============================================================ +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +static void print_results(uint64_t cycles) { + uint64_t time_ns = cycles * CVA6_CLK_PERIOD_NS; + uint64_t time_us = time_ns / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + uint64_t pixels = (uint64_t)IMG_W * (uint64_t)IMG_H * (uint64_t)NUM_FRAMES; + uint64_t tp_int = (pixels * 50ULL) / cycles; + uint64_t tp_frac = ((pixels * 50ULL) % cycles) * 100ULL / cycles; + + printf("Benchmark metrics (sequenziale, 1x CVA6):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n",(unsigned long long)time_ms_int, + (unsigned long long)time_ms_frac); + printf(" Throughput : %llu.%02llu Mpixel/s\r\n", + (unsigned long long)tp_int, (unsigned long long)tp_frac); +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + car_init_start(); + + printf("OBPMark #1.1 - Image Calibration and Correction\r\n"); + printf("Bare-metal sequenziale - Astral CVA6 RV64 @ 50 MHz\r\n"); + printf("Image: %dx%d px | Frames: %d\r\n", IMG_W, IMG_H, NUM_FRAMES); + + init_frames(); + gen_rand_data(); + + uint64_t t_start = read_mcycle(); + + unsigned int frame_i; + for (frame_i = OFFSET_NEIGHBOURS; + frame_i < TOTAL_FRAMES - OFFSET_NEIGHBOURS; + frame_i++) { + prepare_image_frame(&frames[frame_i + 2u]); + proc_image_frame(&frames[frame_i], frame_i); + } + + uint64_t t_end = read_mcycle(); + + printf("Done.\r\n"); + print_results(t_end - t_start); + printf("Output[0][0]: %u\r\n", (unsigned int)PIXEL(&output_frame, 0, 0)); + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_1_smp.c b/sw/tests/bare-metal/hostd/obpmark_1_1_smp.c new file mode 100644 index 000000000..14d232f5f --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_1_smp.c @@ -0,0 +1,373 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.1 - Image Calibration and Correction +// Bare-metal SMP port for Astral/Carfield host domain (2x CVA6, RV64) +// +// Porting notes vs versione OpenMP originale (processing.c con #pragma omp): +// - OpenMP non disponibile bare-metal (libgomp richiede pthread/OS) +// - "#pragma omp parallel for" su asse x -> split manuale per hart_id() +// - Sincronizzazione thread OpenMP (join implicito) -> barrier() custom +// - Risveglio hart1: smp_resume() (libreria Cheshire, sw/lib/smp.c) +// IMPORTANTE: smp_resume() fa RIPARTIRE hart1 dall'inizio di main(), +// non lo "sveglia" semplicemente -> hart1 deve saltare l'init (if hart_id()==0) +// +// Memoria: identica alla versione sequenziale (stessi buffer statici, IMG_W=IMG_H=128) + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +// ============================================================ +// Configurazione benchmark +// ============================================================ +#define IMG_W 128 +#define IMG_H 128 +#define NUM_FRAMES 8 +#define NUM_NEIGHBOURS 4 +#define TOTAL_FRAMES (NUM_FRAMES + NUM_NEIGHBOURS) +#define OFFSET_NEIGHBOURS 2 + +#define NUM_HARTS 2 // 2x CVA6 nel domain host (CheshireNumInternalHarts) + +#define CVA6_CLK_PERIOD_NS 20ULL +#define CVA6_FREQ_HZ 50000000ULL + +// ============================================================ +// Strutture dati (identiche a obpmark_image.h originale) +// ============================================================ +typedef struct { uint16_t *f; unsigned int w; unsigned int h; } frame16_t; +typedef struct { uint8_t *f; unsigned int w; unsigned int h; } frame8_t; +typedef struct { uint32_t *f; unsigned int w; unsigned int h; } frame32_t; + +#define PIXEL(frame, x, y) ((frame)->f[(y) * (frame)->w + (x)]) + +// ============================================================ +// Backing storage statico (condiviso tra i 2 hart, cache-coherent) +// ============================================================ +static uint16_t _frames_data [TOTAL_FRAMES][IMG_H * IMG_W]; +static uint16_t _offsets_data [IMG_H * IMG_W]; +static uint16_t _gains_data [IMG_H * IMG_W]; +static uint8_t _badpix_data [IMG_H * IMG_W]; +static uint32_t _output_data [(IMG_H/2) * (IMG_W/2)]; +static uint32_t _binned_data [(IMG_H/2) * (IMG_W/2)]; + +static frame16_t frames[TOTAL_FRAMES]; +static frame16_t offsets_frame; +static frame16_t gains_frame; +static frame8_t bad_pixels_frame; +static frame32_t output_frame; +static frame32_t binned_frame; + +static void init_frames(void) { + unsigned int i; + for (i = 0; i < TOTAL_FRAMES; i++) { + frames[i].f = _frames_data[i]; + frames[i].w = IMG_W; + frames[i].h = IMG_H; + } + offsets_frame.f = _offsets_data; offsets_frame.w = IMG_W; offsets_frame.h = IMG_H; + gains_frame.f = _gains_data; gains_frame.w = IMG_W; gains_frame.h = IMG_H; + bad_pixels_frame.f = _badpix_data; bad_pixels_frame.w = IMG_W; bad_pixels_frame.h = IMG_H; + output_frame.f = _output_data; output_frame.w = IMG_W/2; output_frame.h = IMG_H/2; + binned_frame.f = _binned_data; binned_frame.w = IMG_W/2; binned_frame.h = IMG_H/2; +} + +// ============================================================ +// Generazione dati sintetici (identica alla versione sequenziale) +// Eseguita SOLO da hart 0, prima del fork (smp_resume) +// ============================================================ +static unsigned int _rand_state; + +static unsigned int bm_rand(void) { + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} + +static void gen_rand_data(void) { + unsigned int f, x, y; + const unsigned int randmax = 65535u; + _rand_state = 21121993u; + + for (f = 0; f < TOTAL_FRAMES; f++) + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&frames[f], x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&offsets_frame, x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&gains_frame, x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&bad_pixels_frame, x, y) = ((uint8_t)(bm_rand() % randmax)) & 0x01u; + + for (x = 0; x < IMG_W/2; x++) + for (y = 0; y < IMG_H/2; y++) + PIXEL(&output_frame, x, y) = 0u; +} + +// ============================================================ +// Sincronizzazione SPMD: barriera a 2 hart con contatore atomico +// (stesso meccanismo amoswap visto in smp_hello.c, logica diversa: +// barriera invece di mutex) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +// Barriera sense-reversing: funziona per chiamate ripetute senza race +// tra un hart che corre via velocemente e l'altro che arriva in ritardo +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; // rilascia tutti gli hart in attesa + } else { + while (barrier_sense == local_sense) { /* spin */ } + } +} + +// ============================================================ +// Divisione del lavoro tra hart (equivalente a #pragma omp parallel for) +// ============================================================ +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// KERNEL FUNCTIONS — stesso algoritmo, ma operano su [x_start, x_end) +// invece che su tutta la larghezza (equivalente alla divisione che +// faceva "#pragma omp parallel for" sull'asse x) +// ============================================================ + +// [I] Bias offset correction +static void f_offset_range(frame16_t *frame, frame16_t *offsets, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) -= PIXEL(offsets, x, y); +} + +static uint32_t f_neighbour_masked_sum(frame16_t *frame, frame8_t *mask, + int x_mid, int y_mid) { + int x, y; + int x_start = -1, x_stop = 1, y_start = -1, y_stop = 1; + unsigned int n_sum = 0; + uint32_t sum = 0, mean; + + if (x_mid == 0) x_start = 0; + else if (x_mid == (int)(frame->w - 1)) x_stop = 0; + if (y_mid == 0) y_start = 0; + else if (y_mid == (int)(frame->h - 1)) y_stop = 0; + + for (x = x_start; x < (x_stop + 1); x++) + for (y = y_start; y < (y_stop + 1); y++) + if (PIXEL(mask, (x_mid+x), (y_mid+y)) == 0) { + sum += PIXEL(frame, (x_mid+x), (y_mid+y)); + n_sum++; + } + + mean = (n_sum == 0u) ? 0u : sum / n_sum; + return mean; +} + +// [II] Bad pixel correction +// NOTA: legge pixel vicini (3x3) -> serve barrier() DOPO f_offset_range +// completo (entrambi gli hart) prima di chiamare questa, altrimenti un +// hart potrebbe leggere un pixel di confine non ancora corretto dall'altro. +static void f_mask_replace_range(frame16_t *frame, frame8_t *mask, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + if (PIXEL(mask, x, y) == 1u) + PIXEL(frame, x, y) = (uint16_t)f_neighbour_masked_sum( + frame, mask, (int)x, (int)y); +} + +// [III] Radiation scrubbing +static void f_scrub_range(frame16_t *frame, frame16_t *fs, unsigned int frame_i, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + static unsigned int num_neighbour = 4u; + uint32_t sum, mean, thr; + + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) { + sum = PIXEL(&fs[frame_i-2], x, y) + PIXEL(&fs[frame_i-1], x, y) + + PIXEL(&fs[frame_i+1], x, y) + PIXEL(&fs[frame_i+2], x, y); + mean = sum / num_neighbour; + thr = 2u * mean; + if (PIXEL(frame, x, y) > thr) + PIXEL(frame, x, y) = (uint16_t)mean; + } +} + +// [IV] Gain correction +static void f_gain_range(frame16_t *frame, frame16_t *gains, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) = (uint16_t)( + ((uint32_t)PIXEL(frame, x, y) * + (uint32_t)PIXEL(gains, x, y)) >> 16); +} + +// [V] 2x2 Spatial binning (opera su indici half-resolution x2 in [xs,xe)) +static void f_2x2_bin_range(frame16_t *frame, frame32_t *binned, + unsigned int xs2, unsigned int xe2) { + unsigned int x2, y2; + for (x2 = xs2; x2 < xe2; x2++) { + unsigned int x = x2 * 2u; + for (y2 = 0; y2 < binned->h; y2++) { + unsigned int y = y2 * 2u; + PIXEL(binned, x2, y2) = PIXEL(frame, x, y ) + + PIXEL(frame, (x+1u), y ) + + PIXEL(frame, x, (y+1u)) + + PIXEL(frame, (x+1u),(y+1u)); + } + } +} + +// [VI] Frame co-adding (half-resolution, split per indici x2) +static void f_coadd_range(frame32_t *sum_frame, frame32_t *add_frame, + unsigned int xs2, unsigned int xe2) { + unsigned int x, y; + for (x = xs2; x < xe2; x++) + for (y = 0; y < sum_frame->h; y++) + PIXEL(sum_frame, x, y) += PIXEL(add_frame, x, y); +} + +// ============================================================ +// Pipeline SPMD: ogni hart calcola il proprio range, esegue il kernel +// sulla sua porzione, poi barrier() prima del kernel successivo +// ============================================================ +static void prepare_image_frame_smp(frame16_t *frame, unsigned int hid) { + unsigned int xs, xe, xs2, xe2; + split_range(IMG_W, hid, NUM_HARTS, &xs, &xe); + split_range(IMG_W/2, hid, NUM_HARTS, &xs2, &xe2); + (void)xs2; (void)xe2; + + f_offset_range(frame, &offsets_frame, xs, xe); // [I] + barrier(); + f_mask_replace_range(frame, &bad_pixels_frame, xs, xe); // [II] + barrier(); +} + +static void proc_image_frame_smp(frame16_t *frame, unsigned int frame_i, + unsigned int hid) { + unsigned int xs, xe, xs2, xe2; + split_range(IMG_W, hid, NUM_HARTS, &xs, &xe); + split_range(IMG_W/2, hid, NUM_HARTS, &xs2, &xe2); + + f_scrub_range(frame, frames, frame_i, xs, xe); // [III] + barrier(); + f_gain_range(frame, &gains_frame, xs, xe); // [IV] + barrier(); + f_2x2_bin_range(frame, &binned_frame, xs2, xe2); // [V] + barrier(); + f_coadd_range(&output_frame, &binned_frame, xs2, xe2); // [VI] + barrier(); +} + +// ============================================================ +// Cycle counter RISC-V +// ============================================================ +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +static void print_results(uint64_t cycles) { + uint64_t time_ns = cycles * CVA6_CLK_PERIOD_NS; + uint64_t time_us = time_ns / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + uint64_t pixels = (uint64_t)IMG_W * (uint64_t)IMG_H * (uint64_t)NUM_FRAMES; + uint64_t tp_int = (pixels * 50ULL) / cycles; + uint64_t tp_frac = ((pixels * 50ULL) % cycles) * 100ULL / cycles; + + printf("Benchmark metrics (2x CVA6 SMP):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n",(unsigned long long)time_ms_int, + (unsigned long long)time_ms_frac); + printf(" Throughput : %llu.%02llu Mpixel/s\r\n", + (unsigned long long)tp_int, (unsigned long long)tp_frac); +} + +// ============================================================ +// Main — SPMD: hart 0 inizializza e fa il fork, poi entrambi lavorano +// ============================================================ +int main(void) { + unsigned int hid = (unsigned int)hart_id(); + + if (hid == 0) { + car_init_start(); + + printf("OBPMark #1.1 - Image Calibration and Correction\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + printf("Image: %dx%d px | Frames: %d | Harts: %d\r\n", + IMG_W, IMG_H, NUM_FRAMES, NUM_HARTS); + + init_frames(); + gen_rand_data(); + + smp_resume(); // hart 1 RIPARTE da qui, entra in main() in parallelo + } + + // ------------------------------------------------------- + // Da qui in poi: ENTRAMBI gli hart eseguono questo codice. + // hart 1 (hid != 0) salta l'if sopra e arriva qui direttamente, + // senza rifare init (che ha già fatto hart 0). + // ------------------------------------------------------- + barrier(); // assicura che hart1 sia arrivato prima di iniziare il timer + + uint64_t t_start = (hid == 0) ? read_mcycle() : 0; + + unsigned int frame_i; + for (frame_i = OFFSET_NEIGHBOURS; + frame_i < TOTAL_FRAMES - OFFSET_NEIGHBOURS; + frame_i++) { + prepare_image_frame_smp(&frames[frame_i + 2u], hid); + proc_image_frame_smp(&frames[frame_i], frame_i, hid); + } + + barrier(); // assicura che entrambi gli hart abbiano finito + + if (hid == 0) { + uint64_t t_end = read_mcycle(); + printf("Done.\r\n"); + print_results(t_end - t_start); + printf("Output[0][0]: %u\r\n", (unsigned int)PIXEL(&output_frame, 0, 0)); + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_1_smp_32.c b/sw/tests/bare-metal/hostd/obpmark_1_1_smp_32.c new file mode 100644 index 000000000..e74edb41e --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_1_smp_32.c @@ -0,0 +1,315 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.1 - Image Calibration and Correction +// Bare-metal SMP port for Astral host domain (2x CVA6, RV64) - 32x32 + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define IMG_W 32 +#define IMG_H 32 +#define NUM_FRAMES 8 +#define NUM_NEIGHBOURS 4 +#define TOTAL_FRAMES (NUM_FRAMES + NUM_NEIGHBOURS) +#define OFFSET_NEIGHBOURS 2 +#define NUM_HARTS 2 +#define CVA6_CLK_PERIOD_NS 20ULL + +typedef struct { uint16_t *f; unsigned int w; unsigned int h; } frame16_t; +typedef struct { uint8_t *f; unsigned int w; unsigned int h; } frame8_t; +typedef struct { uint32_t *f; unsigned int w; unsigned int h; } frame32_t; + +#define PIXEL(frame, x, y) ((frame)->f[(y) * (frame)->w + (x)]) + +static uint16_t _frames_data [TOTAL_FRAMES][IMG_H * IMG_W]; +static uint16_t _offsets_data [IMG_H * IMG_W]; +static uint16_t _gains_data [IMG_H * IMG_W]; +static uint8_t _badpix_data [IMG_H * IMG_W]; +static uint32_t _output_data [(IMG_H/2) * (IMG_W/2)]; +static uint32_t _binned_data [(IMG_H/2) * (IMG_W/2)]; + +static frame16_t frames[TOTAL_FRAMES]; +static frame16_t offsets_frame; +static frame16_t gains_frame; +static frame8_t bad_pixels_frame; +static frame32_t output_frame; +static frame32_t binned_frame; + +static void init_frames(void) { + unsigned int i; + for (i = 0; i < TOTAL_FRAMES; i++) { + frames[i].f = _frames_data[i]; + frames[i].w = IMG_W; + frames[i].h = IMG_H; + } + offsets_frame.f = _offsets_data; offsets_frame.w = IMG_W; offsets_frame.h = IMG_H; + gains_frame.f = _gains_data; gains_frame.w = IMG_W; gains_frame.h = IMG_H; + bad_pixels_frame.f = _badpix_data; bad_pixels_frame.w = IMG_W; bad_pixels_frame.h = IMG_H; + output_frame.f = _output_data; output_frame.w = IMG_W/2; output_frame.h = IMG_H/2; + binned_frame.f = _binned_data; binned_frame.w = IMG_W/2; binned_frame.h = IMG_H/2; +} + +static unsigned int _rand_state; + +static unsigned int bm_rand(void) { + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} + +static void gen_rand_data(void) { + unsigned int f, x, y; + _rand_state = 21121993u; + for (f = 0; f < TOTAL_FRAMES; f++) + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&frames[f], x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&offsets_frame, x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&gains_frame, x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&bad_pixels_frame, x, y) = ((uint8_t)(bm_rand() % 65535u)) & 0x01u; + for (x = 0; x < IMG_W/2; x++) + for (y = 0; y < IMG_H/2; y++) + PIXEL(&output_frame, x, y) = 0u; +} + +// Sense-reversing barrier for 2 harts +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// Kernel functions — each operates on a column range [xs, xe) +static void f_offset_range(frame16_t *frame, frame16_t *offsets, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) -= PIXEL(offsets, x, y); +} + +static uint32_t f_neighbour_masked_sum(frame16_t *frame, frame8_t *mask, + int x_mid, int y_mid) { + int x, y, x_start = -1, x_stop = 1, y_start = -1, y_stop = 1; + unsigned int n_sum = 0; + uint32_t sum = 0; + + if (x_mid == 0) x_start = 0; + else if (x_mid == (int)(frame->w - 1)) x_stop = 0; + if (y_mid == 0) y_start = 0; + else if (y_mid == (int)(frame->h - 1)) y_stop = 0; + + for (x = x_start; x < (x_stop + 1); x++) + for (y = y_start; y < (y_stop + 1); y++) + if (PIXEL(mask, (x_mid+x), (y_mid+y)) == 0) { + sum += PIXEL(frame, (x_mid+x), (y_mid+y)); + n_sum++; + } + return (n_sum == 0u) ? 0u : sum / n_sum; +} + +static void f_mask_replace_range(frame16_t *frame, frame8_t *mask, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + if (PIXEL(mask, x, y) == 1u) + PIXEL(frame, x, y) = (uint16_t)f_neighbour_masked_sum( + frame, mask, (int)x, (int)y); +} + +static void f_scrub_range(frame16_t *frame, frame16_t *fs, unsigned int frame_i, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + uint32_t sum, mean, thr; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) { + sum = PIXEL(&fs[frame_i-2], x, y) + PIXEL(&fs[frame_i-1], x, y) + + PIXEL(&fs[frame_i+1], x, y) + PIXEL(&fs[frame_i+2], x, y); + mean = sum / 4u; + thr = 2u * mean; + if (PIXEL(frame, x, y) > thr) + PIXEL(frame, x, y) = (uint16_t)mean; + } +} + +static void f_gain_range(frame16_t *frame, frame16_t *gains, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) = (uint16_t)( + ((uint32_t)PIXEL(frame, x, y) * + (uint32_t)PIXEL(gains, x, y)) >> 16); +} + +static void f_2x2_bin_range(frame16_t *frame, frame32_t *binned, + unsigned int xs2, unsigned int xe2) { + unsigned int x2, y2; + for (x2 = xs2; x2 < xe2; x2++) { + unsigned int x = x2 * 2u; + for (y2 = 0; y2 < binned->h; y2++) { + unsigned int y = y2 * 2u; + PIXEL(binned, x2, y2) = PIXEL(frame, x, y ) + + PIXEL(frame, (x+1u), y ) + + PIXEL(frame, x, (y+1u)) + + PIXEL(frame, (x+1u),(y+1u)); + } + } +} + +static void f_coadd_range(frame32_t *sum_frame, frame32_t *add_frame, + unsigned int xs2, unsigned int xe2) { + unsigned int x, y; + for (x = xs2; x < xe2; x++) + for (y = 0; y < sum_frame->h; y++) + PIXEL(sum_frame, x, y) += PIXEL(add_frame, x, y); +} + +// Re-read hart_id() inside pipeline functions to avoid LTO register corruption +static void prepare_image_frame_smp(frame16_t *frame, + unsigned int hid __attribute__((unused))) { + unsigned int id = (unsigned int)hart_id(); + unsigned int xs, xe; + split_range(IMG_W, id, NUM_HARTS, &xs, &xe); + + f_offset_range(frame, &offsets_frame, xs, xe); + barrier(); + f_mask_replace_range(frame, &bad_pixels_frame, xs, xe); + barrier(); +} + +static void proc_image_frame_smp(frame16_t *frame, unsigned int frame_i, + unsigned int hid __attribute__((unused))) { + unsigned int id = (unsigned int)hart_id(); + unsigned int xs, xe, xs2, xe2; + split_range(IMG_W, id, NUM_HARTS, &xs, &xe); + split_range(IMG_W/2, id, NUM_HARTS, &xs2, &xe2); + + f_scrub_range(frame, frames, frame_i, xs, xe); + barrier(); + f_gain_range(frame, &gains_frame, xs, xe); + barrier(); + f_2x2_bin_range(frame, &binned_frame, xs2, xe2); + barrier(); + f_coadd_range(&output_frame, &binned_frame, xs2, xe2); + barrier(); +} + +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +static void print_results(uint64_t cycles) { + uint64_t time_us = cycles * CVA6_CLK_PERIOD_NS / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac= time_us % 1000ULL; + uint64_t pixels = (uint64_t)IMG_W * IMG_H * NUM_FRAMES; + uint64_t tp_int = (pixels * 50ULL) / cycles; + uint64_t tp_frac = ((pixels * 50ULL) % cycles) * 100ULL / cycles; + + printf("Benchmark metrics (2x CVA6 SMP):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n",(unsigned long long)time_ms_int, + (unsigned long long)time_ms_frac); + printf(" Throughput : %llu.%02llu Mpixel/s\r\n", + (unsigned long long)tp_int, (unsigned long long)tp_frac); +} + +int main(void) { + unsigned int hid = (unsigned int)hart_id(); + + if (hid == 0) { + car_init_start(); + printf("OBPMark #1.1 - Image Calibration and Correction\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + printf("Image: %dx%d px | Frames: %d | Harts: %d\r\n", + IMG_W, IMG_H, NUM_FRAMES, NUM_HARTS); + init_frames(); + gen_rand_data(); + smp_resume(); + } + + barrier(); + + uint64_t t_start = (hid == 0) ? read_mcycle() : 0; + + unsigned int frame_i; + for (frame_i = OFFSET_NEIGHBOURS; + frame_i < TOTAL_FRAMES - OFFSET_NEIGHBOURS; + frame_i++) { + prepare_image_frame_smp(&frames[frame_i + 2u], hid); + proc_image_frame_smp(&frames[frame_i], frame_i, hid); + } + + barrier(); + + if (hid == 0) { + uint64_t t_end = read_mcycle(); + printf("Done.\r\n"); + print_results(t_end - t_start); + // Use uart_write_str+flush for the last line: the testbench triggers + // $finish as soon as uart_reading_byte==0, which can happen between + // two consecutive printf calls before Output[0][0] is transmitted. + { + unsigned int v = (unsigned int)PIXEL(&output_frame, 0, 0); + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='O';*p++='u';*p++='t';*p++='p';*p++='u';*p++='t'; + *p++='[';*p++='0';*p++=']';*p++='[';*p++='0';*p++=']'; + *p++=':';*p++=' '; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must not return from main(): _exit() would trigger $finish + // before hart 0 finishes printing results. + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/pulpd_offloader_blocking.c b/sw/tests/bare-metal/hostd/pulpd_offloader_blocking.c index 504cc5ece..8d4078880 100644 --- a/sw/tests/bare-metal/hostd/pulpd_offloader_blocking.c +++ b/sw/tests/bare-metal/hostd/pulpd_offloader_blocking.c @@ -44,11 +44,16 @@ int main(void) volatile uint32_t pulp_ret_val = 0; pulp_cluster_set_bootaddress(pulp_boot_default); + fence(); + /* Readback di verifica: forza completamento scrittura boot address via AXI */ + volatile uint32_t *boot_reg = (volatile uint32_t*)CAR_INT_CLUSTER_BOOT_ADDR_REG(car_integer_cluster); + while (*boot_reg != pulp_boot_default) { /* spin until boot addr visible */ } uart_init(&__base_uart, reset_freq, 115200); uart_write_str(&__base_uart, str, sizeof(str)); uart_write_flush(&__base_uart); + fencei(); pulp_cluster_start(); pulp_cluster_wait_eoc(); diff --git a/sw/tests/bare-metal/pulpd-benchmarks/README.md b/sw/tests/bare-metal/pulpd-benchmarks/README.md new file mode 100644 index 000000000..b284bbb92 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/README.md @@ -0,0 +1,177 @@ +# OBPMark benchmarks on the Astral PULP integer cluster + +This directory contains the source of the OBPMark #1.1 (Image Calibration and +Correction) benchmark ported to the Astral heterogeneous SoC. The benchmark is +offloaded from the Cheshire host (CVA6, RV64) to the PULP integer cluster +(8x RV32IMC cores with the XpulpV2/xgap9 SIMD extension) and run in RTL +simulation with QuestaSim. + +## What lives here + +Each subdirectory is one benchmark variant and contains a cluster-side source +file (`.c`) and its `Makefile`. These are the **only** files that must be +version-controlled; everything downstream (`build/`, `.h`, `payload.h`, ELFs) +is generated by the flow described below and must NOT be committed. + +| Directory | Variant | +|------------------------------------|----------------------------------------------------------------| +| `obpmark_1_1` | Reference cluster port (single core) | +| `obpmark_1_1_seq` | Sequential cluster (1 core, baseline) | +| `obpmark_1_1_par` | Parallel cluster (8 cores, SPMD) | +| `obpmark_1_1_par_simd` | Parallel + 16-bit SIMD on f_offset and f_2x2_bin | +| `obpmark_1_1_par_simd_v2_prof` | As above + partial SIMD f_scrub + per-kernel cycle profiling | +| `obpmark_1_1_par_simd_v3_prof` | As v2 but f_scrub mean via add2div2 (fully SIMD) + profiling | + +The host-side counterparts (sequential CVA6 and SMP CVA6 reference +implementations) and the offload driver live under +`sw/tests/bare-metal/hostd/` in the main repository, not here. + +## Where the build actually happens + +IMPORTANT: the build system compiles the cluster tests from inside the +bender checkout, NOT from this directory: + +``` +.bender/git/checkouts/pulp_cluster-/regression_tests/astral// +``` + +That checkout is a local copy of the `pulp_cluster` dependency, recreated by +`bender update` and excluded from version control. The sources kept here are +the authoritative copies. To build a benchmark, copy its directory +(source + Makefile) into the bender checkout's `regression_tests/astral/` +folder, then run the flow below. Keeping the copies here means the work is not +lost when bender refreshes the checkout. + +A test directory needs exactly two files: + +- `.c` — the cluster source +- `Makefile` — with `PULP_APP_SRCS` pointing at that exact `.c` filename: + +```make +PULP_APP = test +PULP_APP_SRCS = .c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk +``` + +The `-I$(PULPRT_HOME)/drivers/include` is required so the runtime's UART +driver header is found; without it the build still links but emits +implicit-declaration warnings for `uart_write`. + +## Build and run flow + +The pipeline turns a cluster `.c` into a host binary that QuestaSim executes: + +``` +.c + | pulp-runtime build (RV32 gcc) + v +build/test/test (cluster ELF) + | elf2header.py + v +.h (C array with the cluster binary + load vectors) + | copied to payload.h, compiled into the offloader + v +pulpd_offloader_blocking..car.dram.elf (host CVA6 ELF) + | QuestaSim + v +simulation (UART prints results) +``` + +### 0. Environment (once per shell, ORDER MATTERS) + +```bash +cd +source env/env-iis.sh # IIS toolchain + bender in PATH +source .bender/git/checkouts/pulp_cluster-/pulp-runtime/configs/astral-cluster.sh +``` + +`env-iis.sh` must come first: it puts the RISC-V toolchain and `bender` on the +PATH. `PULPD_ROOT` in the makefiles is computed by `bender path pulp_cluster`, +so if bender is missing the whole test list is empty and every target fails +with "No rule to make target". `astral-cluster.sh` then exports +`CONFIG_NO_FC=1` (selects the correct crt0 variant for the FC-less Astral +cluster) and `PULPRT_TARGET=astral-cluster`. Verify with: + +```bash +echo "CONFIG_NO_FC=$CONFIG_NO_FC PULPRT_HOME=$PULPRT_HOME" +``` + +### 1. Build the cluster binary + +```bash +TEST=obpmark_1_1_par_simd_v2_prof +CHK=.bender/git/checkouts/pulp_cluster-/regression_tests/astral +make -B CONFIG_IO_UART=2 $CHK/$TEST/build +``` + +- `CONFIG_IO_UART=2` MUST be passed on the command line (not as an env var): + the runtime's `configs/default.mk` sets it to 0 with a plain `=`, which would + override an env value. Value 2 routes cluster `printf` through the host UART + driver (`uart_write` + flush, with LSR check) so output is captured by the + testbench. With the default 0, prints are written to a device the testbench + does not capture and nothing appears. +- `-B` forces a rebuild ignoring timestamps; needed when only flags or env + changed, since make tracks files, not variables. + +The build copies the ELF to `sw/tests/bare-metal/pulpd/$TEST.elf`. + +### 2. Generate the header + +```bash +make $(pwd)/sw/tests/bare-metal/pulpd/$TEST.h +``` + +The header rule target is an ABSOLUTE path, so it must be invoked with an +absolute path (`$(pwd)/...`). Calling it with a relative path gives +"No rule to make target". (Fallback: run the script directly with +`./.venv/bin/python scripts/elf2header.py --binary /test/test +--vectors sw/tests/bare-metal/pulpd/$TEST.h`.) + +### 3. Build the offloader + +```bash +rm -f sw/tests/bare-metal/hostd/pulpd_offloader_blocking.$TEST.car.dram.elf +make car-pulpd-sw-offload-tests +``` + +This template copies each `.h` to `payload.h`, compiles the offloader, +and removes `payload.h` — once per available header. It fails only if some +header is missing, so make sure step 2 ran for the test you want. + +### 4. Simulate + +```bash +make car-vsim-sim-run \ + CHS_BINARY=./sw/tests/bare-metal/hostd/pulpd_offloader_blocking.$TEST.car.dram.elf \ + DEBUG=1 +``` + +The UART log ends with the benchmark report: elapsed cycles, throughput, +`Output[0][0]` (reference value 125700), and — for the `_prof` variants — the +per-kernel cycle breakdown. + +## Data buffers must be in TCDM + +Static data buffers used by the cluster must be placed in the `.heapsram` +section (macro `HEAPSRAM` in the sources), which the linker maps to L1/TCDM. +Otherwise they end up in the ELF payload, `_start` shifts above the expected +boot address, and the cluster fails to boot. `malloc` is not available in this +runtime. + +## Notes on the SIMD variants + +The 16-bit SIMD vectorization uses the `v2u`/`v2s` vector types (two 16-bit +lanes in a 32-bit word) with native `+`/`-` operators, which the compiler maps +to `pv.add.h`/`pv.sub.h`. Verify vectorization on the ELF with: + +```bash +riscv32-unknown-elf-objdump -d sw/tests/bare-metal/pulpd/$TEST.elf | grep 'pv\.' +``` + +- `f_offset` and `f_2x2_bin` vectorize cleanly (data contiguous, no overflow / + a single 32-bit reduction at the end). +- `f_scrub` sums four temporal frames; the sum exceeds 16 bits, so v2 keeps + only the loads in SIMD, while v3 computes the 4-frame mean entirely in SIMD + via chained `add2div2` (`pv.add.h.div2`). The chained halvings truncate, so + v3's output must be checked against the reference value. diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1/Makefile new file mode 100644 index 000000000..4eb9aa152 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1/Makefile @@ -0,0 +1,6 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_1_pulp.c + +PULP_CFLAGS = -O3 + +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1/obpmark_1_1_pulp.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1/obpmark_1_1_pulp.c new file mode 100644 index 000000000..b25a48955 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1/obpmark_1_1_pulp.c @@ -0,0 +1,306 @@ +/* + * OBPMark Benchmark #1.1 - Image Calibration and Correction + * Porting parallelo per il cluster integer PULP di Astral (8x RI5CY/cv32e40p, RV32) + * + * Pattern di programmazione ricalcato da matrixMul.c (regression_tests/astral/parMatrixMul8/), + * validato su questa stessa piattaforma: + * - testcase_t/run_suite per la gestione automatica del timer (bench_timer_start/stop) + * - synch_barrier() fornita dal runtime PULP (nessuna barriera custom necessaria, + * a differenza del porting sul domain host CVA6) + * - Tutti gli 8 core partono insieme al boot del cluster (nessun smp_resume() necessario) + * + * Dimensione: 32x32 (limite TCDM = 128 KB, dato 31 bytes/pixel necessari) + */ + +#include "pulp.h" +#include + +// ============================================================ +// Configurazione benchmark +// ============================================================ +#define IMG_W 32 +#define IMG_H 32 +#define NUM_FRAMES 8 +#define NUM_NEIGHBOURS 4 +#define TOTAL_FRAMES (NUM_FRAMES + NUM_NEIGHBOURS) // 12 +#define OFFSET_NEIGHBOURS 2 + +// ============================================================ +// Strutture dati (identiche a obpmark_image.h originale) +// ============================================================ +typedef struct { uint16_t *f; unsigned int w; unsigned int h; } frame16_t; +typedef struct { uint8_t *f; unsigned int w; unsigned int h; } frame8_t; +typedef struct { uint32_t *f; unsigned int w; unsigned int h; } frame32_t; + +#define PIXEL(frame, x, y) ((frame)->f[(y) * (frame)->w + (x)]) + +// ============================================================ +// Backing storage statico — risiede in TCDM (default per build cluster PULP) +// Condiviso tra tutti gli 8 core, cache-coherent a livello di TCDM +// ============================================================ +static uint16_t _frames_data [TOTAL_FRAMES][IMG_H * IMG_W]; +static uint16_t _offsets_data [IMG_H * IMG_W]; +static uint16_t _gains_data [IMG_H * IMG_W]; +static uint8_t _badpix_data [IMG_H * IMG_W]; +static uint32_t _output_data [(IMG_H/2) * (IMG_W/2)]; +static uint32_t _binned_data [(IMG_H/2) * (IMG_W/2)]; + +static frame16_t frames[TOTAL_FRAMES]; +static frame16_t offsets_frame; +static frame16_t gains_frame; +static frame8_t bad_pixels_frame; +static frame32_t output_frame; +static frame32_t binned_frame; + +unsigned int num_cores; + +static void init_frames(void) { + unsigned int i; + for (i = 0; i < TOTAL_FRAMES; i++) { + frames[i].f = _frames_data[i]; + frames[i].w = IMG_W; + frames[i].h = IMG_H; + } + offsets_frame.f = _offsets_data; offsets_frame.w = IMG_W; offsets_frame.h = IMG_H; + gains_frame.f = _gains_data; gains_frame.w = IMG_W; gains_frame.h = IMG_H; + bad_pixels_frame.f = _badpix_data; bad_pixels_frame.w = IMG_W; bad_pixels_frame.h = IMG_H; + output_frame.f = _output_data; output_frame.w = IMG_W/2; output_frame.h = IMG_H/2; + binned_frame.f = _binned_data; binned_frame.w = IMG_W/2; binned_frame.h = IMG_H/2; +} + +// ============================================================ +// Generazione dati sintetici — eseguita SOLO da core 0 +// ============================================================ +static unsigned int _rand_state; + +static unsigned int bm_rand(void) { + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} + +static void gen_rand_data(void) { + unsigned int f, x, y; + const unsigned int randmax = 65535u; + _rand_state = 21121993u; // BENCHMARK_RAND_SEED originale + + for (f = 0; f < TOTAL_FRAMES; f++) + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&frames[f], x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&offsets_frame, x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&gains_frame, x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&bad_pixels_frame, x, y) = ((uint8_t)(bm_rand() % randmax)) & 0x01u; + + for (x = 0; x < IMG_W/2; x++) + for (y = 0; y < IMG_H/2; y++) + PIXEL(&output_frame, x, y) = 0u; +} + +// ============================================================ +// Divisione del lavoro tra gli 8 core (stesso pattern di matrixMul.c) +// ============================================================ +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// KERNEL FUNCTIONS — identiche all'algoritmo originale, parametrizzate +// sul range [xs, xe) di colonne assegnato a ciascun core +// ============================================================ + +// [I] Bias offset correction +static void f_offset_range(frame16_t *frame, frame16_t *offsets, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) -= PIXEL(offsets, x, y); +} + +static uint32_t f_neighbour_masked_sum(frame16_t *frame, frame8_t *mask, + int x_mid, int y_mid) { + int x, y; + int x_start = -1, x_stop = 1, y_start = -1, y_stop = 1; + unsigned int n_sum = 0; + uint32_t sum = 0, mean; + + if (x_mid == 0) x_start = 0; + else if (x_mid == (int)(frame->w - 1)) x_stop = 0; + if (y_mid == 0) y_start = 0; + else if (y_mid == (int)(frame->h - 1)) y_stop = 0; + + for (x = x_start; x < (x_stop + 1); x++) + for (y = y_start; y < (y_stop + 1); y++) + if (PIXEL(mask, (x_mid+x), (y_mid+y)) == 0) { + sum += PIXEL(frame, (x_mid+x), (y_mid+y)); + n_sum++; + } + + mean = (n_sum == 0u) ? 0u : sum / n_sum; + return mean; +} + +// [II] Bad pixel correction (legge vicini 3x3 -> serve barrier prima, su tutta l'immagine) +static void f_mask_replace_range(frame16_t *frame, frame8_t *mask, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + if (PIXEL(mask, x, y) == 1u) + PIXEL(frame, x, y) = (uint16_t)f_neighbour_masked_sum( + frame, mask, (int)x, (int)y); +} + +// [III] Radiation scrubbing +static void f_scrub_range(frame16_t *frame, frame16_t *fs, unsigned int frame_i, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + static unsigned int num_neighbour = 4u; + uint32_t sum, mean, thr; + + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) { + sum = PIXEL(&fs[frame_i-2], x, y) + PIXEL(&fs[frame_i-1], x, y) + + PIXEL(&fs[frame_i+1], x, y) + PIXEL(&fs[frame_i+2], x, y); + mean = sum / num_neighbour; + thr = 2u * mean; + if (PIXEL(frame, x, y) > thr) + PIXEL(frame, x, y) = (uint16_t)mean; + } +} + +// [IV] Gain correction +static void f_gain_range(frame16_t *frame, frame16_t *gains, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) = (uint16_t)( + ((uint32_t)PIXEL(frame, x, y) * + (uint32_t)PIXEL(gains, x, y)) >> 16); +} + +// [V] 2x2 Spatial binning (indici a meta' risoluzione) +static void f_2x2_bin_range(frame16_t *frame, frame32_t *binned, + unsigned int xs2, unsigned int xe2) { + unsigned int x2, y2; + for (x2 = xs2; x2 < xe2; x2++) { + unsigned int x = x2 * 2u; + for (y2 = 0; y2 < binned->h; y2++) { + unsigned int y = y2 * 2u; + PIXEL(binned, x2, y2) = PIXEL(frame, x, y ) + + PIXEL(frame, (x+1u), y ) + + PIXEL(frame, x, (y+1u)) + + PIXEL(frame, (x+1u),(y+1u)); + } + } +} + +// [VI] Frame co-adding (meta' risoluzione) +static void f_coadd_range(frame32_t *sum_frame, frame32_t *add_frame, + unsigned int xs2, unsigned int xe2) { + unsigned int x, y; + for (x = xs2; x < xe2; x++) + for (y = 0; y < sum_frame->h; y++) + PIXEL(sum_frame, x, y) += PIXEL(add_frame, x, y); +} + +// ============================================================ +// Pipeline SPMD: ogni core calcola il proprio range, esegue il kernel, +// poi synch_barrier() (fornita dal runtime) prima del kernel successivo +// ============================================================ +static void prepare_image_frame_par(frame16_t *frame, unsigned int cid) { + unsigned int xs, xe; + split_range(IMG_W, cid, num_cores, &xs, &xe); + + f_offset_range(frame, &offsets_frame, xs, xe); // [I] + synch_barrier(); + f_mask_replace_range(frame, &bad_pixels_frame, xs, xe); // [II] + synch_barrier(); +} + +static void proc_image_frame_par(frame16_t *frame, unsigned int frame_i, + unsigned int cid) { + unsigned int xs, xe, xs2, xe2; + split_range(IMG_W, cid, num_cores, &xs, &xe); + split_range(IMG_W/2, cid, num_cores, &xs2, &xe2); + + f_scrub_range(frame, frames, frame_i, xs, xe); // [III] + synch_barrier(); + f_gain_range(frame, &gains_frame, xs, xe); // [IV] + synch_barrier(); + f_2x2_bin_range(frame, &binned_frame, xs2, xe2); // [V] + synch_barrier(); + f_coadd_range(&output_frame, &binned_frame, xs2, xe2); // [VI] + synch_barrier(); +} + +// ============================================================ +// Test case — invocata da run_suite(), che gestisce timer e stampa risultati +// ============================================================ +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_1_1", .test = check_obpmark }, + {0, 0} +}; + +int main() +{ + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + num_cores = get_core_num(); // ARCHI_CLUSTER_NB_PE = 8 su Astral + + if (rt_core_id() < num_cores) { + run_suite(testcases); + } + + synch_barrier(); + + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) +{ + unsigned int cid = get_core_id(); + + // Inizializzazione: una sola volta, dal core 0 + if (cid == 0) { + init_frames(); + gen_rand_data(); + } + if (num_cores != 1) synch_barrier(); + + // Inizio misurazione (gestita da bench_timer_start tramite il framework) + start(); + + unsigned int frame_i; + for (frame_i = OFFSET_NEIGHBOURS; + frame_i < TOTAL_FRAMES - OFFSET_NEIGHBOURS; + frame_i++) { + prepare_image_frame_par(&frames[frame_i + 2u], cid); + proc_image_frame_par(&frames[frame_i], frame_i, cid); + } + + if (num_cores != 1) synch_barrier(); + + stop(); // fine misurazione + + if (cid == 0) { + result->errors = 0; // nessuna verifica contro golden output (dati sintetici) + } +} \ No newline at end of file diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par/Makefile new file mode 100644 index 000000000..76694980c --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_1_pulp_par.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par/obpmark_1_1_pulp_par.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par/obpmark_1_1_pulp_par.c new file mode 100644 index 000000000..094f946db --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par/obpmark_1_1_pulp_par.c @@ -0,0 +1,290 @@ +/* + * Copyright 2024 ETH Zurich and University of Bologna. + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + * + * OBPMark Benchmark #1.1 - Image Calibration and Correction + * Parallel port for the PULP integer cluster of Astral (8 cores, RV32) + * + * SPMD: every core runs the same code on a distinct column range [xs, xe). + * Synchronisation uses the cluster hardware barrier (synch_barrier()). + * Data buffers live in .heapsram (TCDM) to keep them out of the ELF payload. + * Input: 32x32 pixels, 8 frames, synthetic data (LCG seed 21121993). + * Expected Output[0][0] = 125700 (matches sequential CVA6/PULP results). + */ + +#include "pulp.h" +#include +#include + +#define IMG_W 32 +#define IMG_H 32 +#define NUM_FRAMES 8 +#define NUM_NEIGHBOURS 4 +#define TOTAL_FRAMES (NUM_FRAMES + NUM_NEIGHBOURS) +#define OFFSET_NEIGHBOURS 2 + +#define EXPECTED_OUTPUT_0_0 125700u + +typedef struct { uint16_t *f; unsigned int w; unsigned int h; } frame16_t; +typedef struct { uint8_t *f; unsigned int w; unsigned int h; } frame8_t; +typedef struct { uint32_t *f; unsigned int w; unsigned int h; } frame32_t; + +#define PIXEL(frame, x, y) ((frame)->f[(y) * (frame)->w + (x)]) + +#define HEAPSRAM __attribute__((section(".heapsram"))) + +static HEAPSRAM uint16_t _frames_data [TOTAL_FRAMES][IMG_H * IMG_W]; +static HEAPSRAM uint16_t _offsets_data [IMG_H * IMG_W]; +static HEAPSRAM uint16_t _gains_data [IMG_H * IMG_W]; +static HEAPSRAM uint8_t _badpix_data [IMG_H * IMG_W]; +static HEAPSRAM uint32_t _output_data [(IMG_H/2) * (IMG_W/2)]; +static HEAPSRAM uint32_t _binned_data [(IMG_H/2) * (IMG_W/2)]; + +static HEAPSRAM frame16_t frames[TOTAL_FRAMES]; +static HEAPSRAM frame16_t offsets_frame; +static HEAPSRAM frame16_t gains_frame; +static HEAPSRAM frame8_t bad_pixels_frame; +static HEAPSRAM frame32_t output_frame; +static HEAPSRAM frame32_t binned_frame; + +static HEAPSRAM unsigned int num_cores; + +static void init_frames(void) { + unsigned int i; + for (i = 0; i < TOTAL_FRAMES; i++) { + frames[i].f = _frames_data[i]; + frames[i].w = IMG_W; + frames[i].h = IMG_H; + } + offsets_frame.f = _offsets_data; offsets_frame.w = IMG_W; offsets_frame.h = IMG_H; + gains_frame.f = _gains_data; gains_frame.w = IMG_W; gains_frame.h = IMG_H; + bad_pixels_frame.f = _badpix_data; bad_pixels_frame.w = IMG_W; bad_pixels_frame.h = IMG_H; + output_frame.f = _output_data; output_frame.w = IMG_W/2; output_frame.h = IMG_H/2; + binned_frame.f = _binned_data; binned_frame.w = IMG_W/2; binned_frame.h = IMG_H/2; +} + +static unsigned int _rand_state; + +static unsigned int bm_rand(void) { + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} + +static void gen_rand_data(void) { + unsigned int f, x, y; + _rand_state = 21121993u; + for (f = 0; f < TOTAL_FRAMES; f++) + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&frames[f], x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&offsets_frame, x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&gains_frame, x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&bad_pixels_frame, x, y) = ((uint8_t)(bm_rand() % 65535u)) & 0x01u; + for (x = 0; x < IMG_W/2; x++) + for (y = 0; y < IMG_H/2; y++) + PIXEL(&output_frame, x, y) = 0u; +} + +/* Static column partitioning, same policy as OpenMP schedule(static). */ +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +static void f_offset_range(frame16_t *frame, frame16_t *offsets, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) -= PIXEL(offsets, x, y); +} + +static uint32_t f_neighbour_masked_sum(frame16_t *frame, frame8_t *mask, + int x_mid, int y_mid) { + int x, y, x_start = -1, x_stop = 1, y_start = -1, y_stop = 1; + unsigned int n_sum = 0; + uint32_t sum = 0; + if (x_mid == 0) x_start = 0; + else if (x_mid == (int)(frame->w - 1)) x_stop = 0; + if (y_mid == 0) y_start = 0; + else if (y_mid == (int)(frame->h - 1)) y_stop = 0; + for (x = x_start; x < (x_stop + 1); x++) + for (y = y_start; y < (y_stop + 1); y++) + if (PIXEL(mask, (x_mid+x), (y_mid+y)) == 0) { + sum += PIXEL(frame, (x_mid+x), (y_mid+y)); + n_sum++; + } + return (n_sum == 0u) ? 0u : sum / n_sum; +} + +static void f_mask_replace_range(frame16_t *frame, frame8_t *mask, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + if (PIXEL(mask, x, y) == 1u) + PIXEL(frame, x, y) = (uint16_t)f_neighbour_masked_sum( + frame, mask, (int)x, (int)y); +} + +static void f_scrub_range(frame16_t *frame, frame16_t *fs, unsigned int frame_i, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + uint32_t sum, mean, thr; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) { + sum = PIXEL(&fs[frame_i-2], x, y) + PIXEL(&fs[frame_i-1], x, y) + + PIXEL(&fs[frame_i+1], x, y) + PIXEL(&fs[frame_i+2], x, y); + mean = sum / 4u; + thr = 2u * mean; + if (PIXEL(frame, x, y) > thr) + PIXEL(frame, x, y) = (uint16_t)mean; + } +} + +static void f_gain_range(frame16_t *frame, frame16_t *gains, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) = (uint16_t)( + ((uint32_t)PIXEL(frame, x, y) * + (uint32_t)PIXEL(gains, x, y)) >> 16); +} + +static void f_2x2_bin_range(frame16_t *frame, frame32_t *binned, + unsigned int xs2, unsigned int xe2) { + unsigned int x2, y2; + for (x2 = xs2; x2 < xe2; x2++) { + unsigned int x = x2 * 2u; + for (y2 = 0; y2 < binned->h; y2++) { + unsigned int y = y2 * 2u; + PIXEL(binned, x2, y2) = PIXEL(frame, x, y ) + + PIXEL(frame, (x+1u), y ) + + PIXEL(frame, x, (y+1u)) + + PIXEL(frame, (x+1u),(y+1u)); + } + } +} + +static void f_coadd_range(frame32_t *sum_frame, frame32_t *add_frame, + unsigned int xs2, unsigned int xe2) { + unsigned int x, y; + for (x = xs2; x < xe2; x++) + for (y = 0; y < sum_frame->h; y++) + PIXEL(sum_frame, x, y) += PIXEL(add_frame, x, y); +} + +/* Read the core id from hardware inside each pipeline function rather than + * trusting a passed-in parameter (same defensive approach as the CVA6 SMP + * port, where LTO corrupted the argument for the secondary hart). */ +static void prepare_image_frame_par(frame16_t *frame) { + unsigned int id = (unsigned int)get_core_id(); + unsigned int xs, xe; + split_range(IMG_W, id, num_cores, &xs, &xe); + + f_offset_range(frame, &offsets_frame, xs, xe); + synch_barrier(); + f_mask_replace_range(frame, &bad_pixels_frame, xs, xe); + synch_barrier(); +} + +static void proc_image_frame_par(frame16_t *frame, unsigned int frame_i) { + unsigned int id = (unsigned int)get_core_id(); + unsigned int xs, xe, xs2, xe2; + split_range(IMG_W, id, num_cores, &xs, &xe); + split_range(IMG_W/2, id, num_cores, &xs2, &xe2); + + f_scrub_range(frame, frames, frame_i, xs, xe); + synch_barrier(); + f_gain_range(frame, &gains_frame, xs, xe); + synch_barrier(); + f_2x2_bin_range(frame, &binned_frame, xs2, xe2); + synch_barrier(); + f_coadd_range(&output_frame, &binned_frame, xs2, xe2); + synch_barrier(); +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_1_1_par", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + num_cores = get_core_num(); + + if (rt_core_id() < num_cores) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int core_id = (unsigned int)get_core_id(); + + if (core_id == 0) { + init_frames(); + gen_rand_data(); + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + } + synch_barrier(); + + start(); + + unsigned int frame_i; + for (frame_i = OFFSET_NEIGHBOURS; + frame_i < TOTAL_FRAMES - OFFSET_NEIGHBOURS; + frame_i++) { + prepare_image_frame_par(&frames[frame_i + 2u]); + proc_image_frame_par(&frames[frame_i], frame_i); + } + + synch_barrier(); + stop(); + + if (core_id != 0) return; + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + unsigned int pixels = IMG_W * IMG_H * NUM_FRAMES; + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + unsigned int tp_int = (unsigned int)(((unsigned long long)pixels * 50ULL) / cycles); + unsigned int tp_frac = (unsigned int)((((unsigned long long)pixels * 50ULL) % cycles) * 100ULL / cycles); + + unsigned int out = (unsigned int)PIXEL(&output_frame, 0, 0); + + printf("OBPMark 1.1 - Image Calibration and Correction\n"); + printf("PULP cluster parallel (%u cores, RV32) - Astral @ 50 MHz\n", num_cores); + printf("Image: %dx%d px | Frames: %d\n", IMG_W, IMG_H, NUM_FRAMES); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Throughput : %u.%02u Mpixel/s\n", tp_int, tp_frac); + printf(" Output[0][0] : %u\n", out); + + if (out == EXPECTED_OUTPUT_0_0) { + result->errors = 0; + printf("RESULT: PASS\n"); + } else { + result->errors = 1; + printf("RESULT: FAIL (expected %u)\n", EXPECTED_OUTPUT_0_0); + } +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd/Makefile new file mode 100644 index 000000000..a3a1ce5f7 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_1_pulp_par_simd.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd/obpmark_1_1_pulp_par_simd.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd/obpmark_1_1_pulp_par_simd.c new file mode 100644 index 000000000..70555dc15 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd/obpmark_1_1_pulp_par_simd.c @@ -0,0 +1,321 @@ +/* + * Copyright 2024 ETH Zurich and University of Bologna. + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + * + * OBPMark Benchmark #1.1 - Image Calibration and Correction + * Parallel port for the PULP integer cluster of Astral (8 cores, RV32) + * + * SPMD: every core runs the same code on a distinct column range [xs, xe). + * Synchronisation uses the cluster hardware barrier (synch_barrier()). + * Data buffers live in .heapsram (TCDM) to keep them out of the ELF payload. + * Input: 32x32 pixels, 8 frames, synthetic data (LCG seed 21121993). + * Expected Output[0][0] = 125700 (matches sequential CVA6/PULP results). + */ + +#include "pulp.h" +#include +#include + +/* PULP 16-bit SIMD vector types (2x uint16 / int16 packed in one 32-bit word). + * Native operators (+, -) on these map to pv.add.h / pv.sub.h instructions + * when compiled with -march=rv32imcxgap9. */ +typedef unsigned short v2u __attribute__((vector_size(4))); +typedef signed short v2s __attribute__((vector_size(4))); + +#define IMG_W 32 +#define IMG_H 32 +#define NUM_FRAMES 8 +#define NUM_NEIGHBOURS 4 +#define TOTAL_FRAMES (NUM_FRAMES + NUM_NEIGHBOURS) +#define OFFSET_NEIGHBOURS 2 + +#define EXPECTED_OUTPUT_0_0 125700u + +typedef struct { uint16_t *f; unsigned int w; unsigned int h; } frame16_t; +typedef struct { uint8_t *f; unsigned int w; unsigned int h; } frame8_t; +typedef struct { uint32_t *f; unsigned int w; unsigned int h; } frame32_t; + +#define PIXEL(frame, x, y) ((frame)->f[(y) * (frame)->w + (x)]) + +#define HEAPSRAM __attribute__((section(".heapsram"))) + +static HEAPSRAM uint16_t _frames_data [TOTAL_FRAMES][IMG_H * IMG_W]; +static HEAPSRAM uint16_t _offsets_data [IMG_H * IMG_W]; +static HEAPSRAM uint16_t _gains_data [IMG_H * IMG_W]; +static HEAPSRAM uint8_t _badpix_data [IMG_H * IMG_W]; +static HEAPSRAM uint32_t _output_data [(IMG_H/2) * (IMG_W/2)]; +static HEAPSRAM uint32_t _binned_data [(IMG_H/2) * (IMG_W/2)]; + +static HEAPSRAM frame16_t frames[TOTAL_FRAMES]; +static HEAPSRAM frame16_t offsets_frame; +static HEAPSRAM frame16_t gains_frame; +static HEAPSRAM frame8_t bad_pixels_frame; +static HEAPSRAM frame32_t output_frame; +static HEAPSRAM frame32_t binned_frame; + +static HEAPSRAM unsigned int num_cores; + +static void init_frames(void) { + unsigned int i; + for (i = 0; i < TOTAL_FRAMES; i++) { + frames[i].f = _frames_data[i]; + frames[i].w = IMG_W; + frames[i].h = IMG_H; + } + offsets_frame.f = _offsets_data; offsets_frame.w = IMG_W; offsets_frame.h = IMG_H; + gains_frame.f = _gains_data; gains_frame.w = IMG_W; gains_frame.h = IMG_H; + bad_pixels_frame.f = _badpix_data; bad_pixels_frame.w = IMG_W; bad_pixels_frame.h = IMG_H; + output_frame.f = _output_data; output_frame.w = IMG_W/2; output_frame.h = IMG_H/2; + binned_frame.f = _binned_data; binned_frame.w = IMG_W/2; binned_frame.h = IMG_H/2; +} + +static unsigned int _rand_state; + +static unsigned int bm_rand(void) { + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} + +static void gen_rand_data(void) { + unsigned int f, x, y; + _rand_state = 21121993u; + for (f = 0; f < TOTAL_FRAMES; f++) + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&frames[f], x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&offsets_frame, x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&gains_frame, x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&bad_pixels_frame, x, y) = ((uint8_t)(bm_rand() % 65535u)) & 0x01u; + for (x = 0; x < IMG_W/2; x++) + for (y = 0; y < IMG_H/2; y++) + PIXEL(&output_frame, x, y) = 0u; +} + +/* Static column partitioning, same policy as OpenMP schedule(static). */ +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +/* SIMD-vectorized offset correction: processes 2 pixels per iteration. + * Each column x starts at byte offset y*w*2; the inner loop walks y, and for + * a fixed y two adjacent x values are contiguous in memory. We instead keep + * the column-partitioned layout (x outer) but pair up rows: for a fixed x, + * consecutive y values are w*2 bytes apart (NOT contiguous), so we cannot + * pack along y. We therefore pack along x: process columns in pairs. + * Alignment: row start y*w*2 is 4-byte aligned; x even => &frame[y*w+x] is + * 4-byte aligned. The per-core column range [xs,xe) always starts even + * (xs = id * (32/8) = id*4) and has even width, so all accesses are aligned. */ +static void f_offset_range(frame16_t *frame, frame16_t *offsets, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + unsigned int w = frame->w; + for (y = 0; y < frame->h; y++) { + uint16_t *frow = &frame->f[y * w]; + uint16_t *orow = &offsets->f[y * w]; + for (x = xs; x + 1u < xe; x += 2u) { + v2u fv = *(v2u *)&frow[x]; + v2u ov = *(v2u *)&orow[x]; + *(v2u *)&frow[x] = fv - ov; /* pv.sub.h: 2 pixels at once */ + } + if (x < xe) /* odd tail (not hit with 4-wide) */ + frow[x] -= orow[x]; + } +} + +static uint32_t f_neighbour_masked_sum(frame16_t *frame, frame8_t *mask, + int x_mid, int y_mid) { + int x, y, x_start = -1, x_stop = 1, y_start = -1, y_stop = 1; + unsigned int n_sum = 0; + uint32_t sum = 0; + if (x_mid == 0) x_start = 0; + else if (x_mid == (int)(frame->w - 1)) x_stop = 0; + if (y_mid == 0) y_start = 0; + else if (y_mid == (int)(frame->h - 1)) y_stop = 0; + for (x = x_start; x < (x_stop + 1); x++) + for (y = y_start; y < (y_stop + 1); y++) + if (PIXEL(mask, (x_mid+x), (y_mid+y)) == 0) { + sum += PIXEL(frame, (x_mid+x), (y_mid+y)); + n_sum++; + } + return (n_sum == 0u) ? 0u : sum / n_sum; +} + +static void f_mask_replace_range(frame16_t *frame, frame8_t *mask, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + if (PIXEL(mask, x, y) == 1u) + PIXEL(frame, x, y) = (uint16_t)f_neighbour_masked_sum( + frame, mask, (int)x, (int)y); +} + +static void f_scrub_range(frame16_t *frame, frame16_t *fs, unsigned int frame_i, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + uint32_t sum, mean, thr; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) { + sum = PIXEL(&fs[frame_i-2], x, y) + PIXEL(&fs[frame_i-1], x, y) + + PIXEL(&fs[frame_i+1], x, y) + PIXEL(&fs[frame_i+2], x, y); + mean = sum / 4u; + thr = 2u * mean; + if (PIXEL(frame, x, y) > thr) + PIXEL(frame, x, y) = (uint16_t)mean; + } +} + +static void f_gain_range(frame16_t *frame, frame16_t *gains, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) = (uint16_t)( + ((uint32_t)PIXEL(frame, x, y) * + (uint32_t)PIXEL(gains, x, y)) >> 16); +} + +/* SIMD-vectorized 2x2 binning. + * The two pixels of a 2x2 block on the same row are contiguous: a single v2u + * load fetches {p(x,y), p(x+1,y)}. Loading the two rows and adding them with + * pv.add.h gives {p(x,y)+p(x,y+1), p(x+1,y)+p(x+1,y+1)} in one instruction; + * a final scalar add of the two lanes yields the 4-pixel sum. + * Alignment: x = x2*2 is always even, so &frame[y*w + x] is 4-byte aligned. */ +static void f_2x2_bin_range(frame16_t *frame, frame32_t *binned, + unsigned int xs2, unsigned int xe2) { + unsigned int x2, y2; + unsigned int w = frame->w; + for (x2 = xs2; x2 < xe2; x2++) { + unsigned int x = x2 * 2u; + for (y2 = 0; y2 < binned->h; y2++) { + unsigned int y = y2 * 2u; + v2u top = *(v2u *)&frame->f[y * w + x]; /* {p(x,y), p(x+1,y)} */ + v2u bot = *(v2u *)&frame->f[(y + 1u) * w + x]; /* {p(x,y+1), p(x+1,y+1)} */ + v2u s = top + bot; /* pv.add.h: vertical sum */ + PIXEL(binned, x2, y2) = (uint32_t)s[0] + (uint32_t)s[1]; + } + } +} + +static void f_coadd_range(frame32_t *sum_frame, frame32_t *add_frame, + unsigned int xs2, unsigned int xe2) { + unsigned int x, y; + for (x = xs2; x < xe2; x++) + for (y = 0; y < sum_frame->h; y++) + PIXEL(sum_frame, x, y) += PIXEL(add_frame, x, y); +} + +/* Read the core id from hardware inside each pipeline function rather than + * trusting a passed-in parameter (same defensive approach as the CVA6 SMP + * port, where LTO corrupted the argument for the secondary hart). */ +static void prepare_image_frame_par(frame16_t *frame) { + unsigned int id = (unsigned int)get_core_id(); + unsigned int xs, xe; + split_range(IMG_W, id, num_cores, &xs, &xe); + + f_offset_range(frame, &offsets_frame, xs, xe); + synch_barrier(); + f_mask_replace_range(frame, &bad_pixels_frame, xs, xe); + synch_barrier(); +} + +static void proc_image_frame_par(frame16_t *frame, unsigned int frame_i) { + unsigned int id = (unsigned int)get_core_id(); + unsigned int xs, xe, xs2, xe2; + split_range(IMG_W, id, num_cores, &xs, &xe); + split_range(IMG_W/2, id, num_cores, &xs2, &xe2); + + f_scrub_range(frame, frames, frame_i, xs, xe); + synch_barrier(); + f_gain_range(frame, &gains_frame, xs, xe); + synch_barrier(); + f_2x2_bin_range(frame, &binned_frame, xs2, xe2); + synch_barrier(); + f_coadd_range(&output_frame, &binned_frame, xs2, xe2); + synch_barrier(); +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_1_1_par_simd", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + num_cores = get_core_num(); + + if (rt_core_id() < num_cores) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int core_id = (unsigned int)get_core_id(); + + if (core_id == 0) { + init_frames(); + gen_rand_data(); + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + } + synch_barrier(); + + start(); + + unsigned int frame_i; + for (frame_i = OFFSET_NEIGHBOURS; + frame_i < TOTAL_FRAMES - OFFSET_NEIGHBOURS; + frame_i++) { + prepare_image_frame_par(&frames[frame_i + 2u]); + proc_image_frame_par(&frames[frame_i], frame_i); + } + + synch_barrier(); + stop(); + + if (core_id != 0) return; + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + unsigned int pixels = IMG_W * IMG_H * NUM_FRAMES; + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + unsigned int tp_int = (unsigned int)(((unsigned long long)pixels * 50ULL) / cycles); + unsigned int tp_frac = (unsigned int)((((unsigned long long)pixels * 50ULL) % cycles) * 100ULL / cycles); + + unsigned int out = (unsigned int)PIXEL(&output_frame, 0, 0); + + printf("OBPMark 1.1 - Image Calibration and Correction\n"); + printf("PULP cluster parallel + SIMD16 (%u cores, RV32) - Astral @ 50 MHz\n", num_cores); + printf("Image: %dx%d px | Frames: %d\n", IMG_W, IMG_H, NUM_FRAMES); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Throughput : %u.%02u Mpixel/s\n", tp_int, tp_frac); + printf(" Output[0][0] : %u\n", out); + + if (out == EXPECTED_OUTPUT_0_0) { + result->errors = 0; + printf("RESULT: PASS\n"); + } else { + result->errors = 1; + printf("RESULT: FAIL (expected %u)\n", EXPECTED_OUTPUT_0_0); + } +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v2_prof/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v2_prof/Makefile new file mode 100644 index 000000000..17f9c1f64 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v2_prof/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_1_pulp_par_simd_v2_prof.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v2_prof/obpmark_1_1_pulp_par_simd_v2_prof.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v2_prof/obpmark_1_1_pulp_par_simd_v2_prof.c new file mode 100644 index 000000000..708e49152 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v2_prof/obpmark_1_1_pulp_par_simd_v2_prof.c @@ -0,0 +1,367 @@ +/* + * Copyright 2024 ETH Zurich and University of Bologna. + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + * + * OBPMark Benchmark #1.1 - Image Calibration and Correction + * Parallel port for the PULP integer cluster of Astral (8 cores, RV32) + * + * SPMD: every core runs the same code on a distinct column range [xs, xe). + * Synchronisation uses the cluster hardware barrier (synch_barrier()). + * Data buffers live in .heapsram (TCDM) to keep them out of the ELF payload. + * Input: 32x32 pixels, 8 frames, synthetic data (LCG seed 21121993). + * Expected Output[0][0] = 125700 (matches sequential CVA6/PULP results). + */ + +#include "pulp.h" +#include +#include + +typedef unsigned short v2u __attribute__((vector_size(4))); +typedef signed short v2s __attribute__((vector_size(4))); + +#define IMG_W 32 +#define IMG_H 32 +#define NUM_FRAMES 8 +#define NUM_NEIGHBOURS 4 +#define TOTAL_FRAMES (NUM_FRAMES + NUM_NEIGHBOURS) +#define OFFSET_NEIGHBOURS 2 + +#define EXPECTED_OUTPUT_0_0 125700u + +typedef struct { uint16_t *f; unsigned int w; unsigned int h; } frame16_t; +typedef struct { uint8_t *f; unsigned int w; unsigned int h; } frame8_t; +typedef struct { uint32_t *f; unsigned int w; unsigned int h; } frame32_t; + +#define PIXEL(frame, x, y) ((frame)->f[(y) * (frame)->w + (x)]) + +#define HEAPSRAM __attribute__((section(".heapsram"))) + +static HEAPSRAM uint16_t _frames_data [TOTAL_FRAMES][IMG_H * IMG_W]; +static HEAPSRAM uint16_t _offsets_data [IMG_H * IMG_W]; +static HEAPSRAM uint16_t _gains_data [IMG_H * IMG_W]; +static HEAPSRAM uint8_t _badpix_data [IMG_H * IMG_W]; +static HEAPSRAM uint32_t _output_data [(IMG_H/2) * (IMG_W/2)]; +static HEAPSRAM uint32_t _binned_data [(IMG_H/2) * (IMG_W/2)]; + +static HEAPSRAM frame16_t frames[TOTAL_FRAMES]; +static HEAPSRAM frame16_t offsets_frame; +static HEAPSRAM frame16_t gains_frame; +static HEAPSRAM frame8_t bad_pixels_frame; +static HEAPSRAM frame32_t output_frame; +static HEAPSRAM frame32_t binned_frame; + +static HEAPSRAM unsigned int num_cores; + +/* Per-kernel cycle accumulators (only core 0 writes them). Each kernel is + * timed across all 8 frame iterations; the barrier before each measurement + * ensures all cores have finished the previous stage, so the delta reflects + * the true wall-clock cost of that stage (the slowest core sets the pace). */ +static HEAPSRAM unsigned int cyc_offset; +static HEAPSRAM unsigned int cyc_mask; +static HEAPSRAM unsigned int cyc_scrub; +static HEAPSRAM unsigned int cyc_gain; +static HEAPSRAM unsigned int cyc_bin; +static HEAPSRAM unsigned int cyc_coadd; + +static void init_frames(void) { + unsigned int i; + for (i = 0; i < TOTAL_FRAMES; i++) { + frames[i].f = _frames_data[i]; + frames[i].w = IMG_W; + frames[i].h = IMG_H; + } + offsets_frame.f = _offsets_data; offsets_frame.w = IMG_W; offsets_frame.h = IMG_H; + gains_frame.f = _gains_data; gains_frame.w = IMG_W; gains_frame.h = IMG_H; + bad_pixels_frame.f = _badpix_data; bad_pixels_frame.w = IMG_W; bad_pixels_frame.h = IMG_H; + output_frame.f = _output_data; output_frame.w = IMG_W/2; output_frame.h = IMG_H/2; + binned_frame.f = _binned_data; binned_frame.w = IMG_W/2; binned_frame.h = IMG_H/2; +} + +static unsigned int _rand_state; + +static unsigned int bm_rand(void) { + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} + +static void gen_rand_data(void) { + unsigned int f, x, y; + _rand_state = 21121993u; + for (f = 0; f < TOTAL_FRAMES; f++) + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&frames[f], x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&offsets_frame, x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&gains_frame, x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&bad_pixels_frame, x, y) = ((uint8_t)(bm_rand() % 65535u)) & 0x01u; + for (x = 0; x < IMG_W/2; x++) + for (y = 0; y < IMG_H/2; y++) + PIXEL(&output_frame, x, y) = 0u; +} + +/* Static column partitioning, same policy as OpenMP schedule(static). */ +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +static void f_offset_range(frame16_t *frame, frame16_t *offsets, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + unsigned int w = frame->w; + for (y = 0; y < frame->h; y++) { + uint16_t *frow = &frame->f[y * w]; + uint16_t *orow = &offsets->f[y * w]; + for (x = xs; x + 1u < xe; x += 2u) { + v2u fv = *(v2u *)&frow[x]; + v2u ov = *(v2u *)&orow[x]; + *(v2u *)&frow[x] = fv - ov; /* pv.sub.h */ + } + if (x < xe) + frow[x] -= orow[x]; + } +} + +static uint32_t f_neighbour_masked_sum(frame16_t *frame, frame8_t *mask, + int x_mid, int y_mid) { + int x, y, x_start = -1, x_stop = 1, y_start = -1, y_stop = 1; + unsigned int n_sum = 0; + uint32_t sum = 0; + if (x_mid == 0) x_start = 0; + else if (x_mid == (int)(frame->w - 1)) x_stop = 0; + if (y_mid == 0) y_start = 0; + else if (y_mid == (int)(frame->h - 1)) y_stop = 0; + for (x = x_start; x < (x_stop + 1); x++) + for (y = y_start; y < (y_stop + 1); y++) + if (PIXEL(mask, (x_mid+x), (y_mid+y)) == 0) { + sum += PIXEL(frame, (x_mid+x), (y_mid+y)); + n_sum++; + } + return (n_sum == 0u) ? 0u : sum / n_sum; +} + +static void f_mask_replace_range(frame16_t *frame, frame8_t *mask, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + if (PIXEL(mask, x, y) == 1u) + PIXEL(frame, x, y) = (uint16_t)f_neighbour_masked_sum( + frame, mask, (int)x, (int)y); +} + +/* v2: partial vectorization. The 4-frame sum overflows 16 bit + * (4*65534 > 65535), so we keep SIMD only for the loads (4 v2u loads instead + * of 8 scalar loads) and do the accumulation + threshold in 32-bit scalar. */ +static void f_scrub_range(frame16_t *frame, frame16_t *fs, unsigned int frame_i, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + unsigned int w = frame->w; + uint16_t *fm2 = fs[frame_i - 2u].f; + uint16_t *fm1 = fs[frame_i - 1u].f; + uint16_t *fp1 = fs[frame_i + 1u].f; + uint16_t *fp2 = fs[frame_i + 2u].f; + uint16_t *fcur = frame->f; + + for (y = 0; y < frame->h; y++) { + unsigned int row = y * w; + for (x = xs; x + 1u < xe; x += 2u) { + unsigned int i = row + x; + v2u a = *(v2u *)&fm2[i]; + v2u b = *(v2u *)&fm1[i]; + v2u c = *(v2u *)&fp1[i]; + v2u d = *(v2u *)&fp2[i]; + uint32_t sum0 = (uint32_t)a[0] + (uint32_t)b[0] + (uint32_t)c[0] + (uint32_t)d[0]; + uint32_t sum1 = (uint32_t)a[1] + (uint32_t)b[1] + (uint32_t)c[1] + (uint32_t)d[1]; + uint32_t mean0 = sum0 >> 2; + uint32_t mean1 = sum1 >> 2; + if (fcur[i] > (mean0 << 1)) fcur[i] = (uint16_t)mean0; + if (fcur[i + 1u] > (mean1 << 1)) fcur[i + 1u] = (uint16_t)mean1; + } + if (x < xe) { + unsigned int i = row + x; + uint32_t sum = (uint32_t)fm2[i] + fm1[i] + fp1[i] + fp2[i]; + uint32_t mean = sum >> 2; + if (fcur[i] > (mean << 1)) fcur[i] = (uint16_t)mean; + } + } +} + +static void f_gain_range(frame16_t *frame, frame16_t *gains, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) = (uint16_t)( + ((uint32_t)PIXEL(frame, x, y) * + (uint32_t)PIXEL(gains, x, y)) >> 16); +} + +static void f_2x2_bin_range(frame16_t *frame, frame32_t *binned, + unsigned int xs2, unsigned int xe2) { + unsigned int x2, y2; + unsigned int w = frame->w; + for (x2 = xs2; x2 < xe2; x2++) { + unsigned int x = x2 * 2u; + for (y2 = 0; y2 < binned->h; y2++) { + unsigned int y = y2 * 2u; + v2u top = *(v2u *)&frame->f[y * w + x]; + v2u bot = *(v2u *)&frame->f[(y + 1u) * w + x]; + v2u s = top + bot; /* pv.add.h */ + PIXEL(binned, x2, y2) = (uint32_t)s[0] + (uint32_t)s[1]; + } + } +} + +static void f_coadd_range(frame32_t *sum_frame, frame32_t *add_frame, + unsigned int xs2, unsigned int xe2) { + unsigned int x, y; + for (x = xs2; x < xe2; x++) + for (y = 0; y < sum_frame->h; y++) + PIXEL(sum_frame, x, y) += PIXEL(add_frame, x, y); +} + +/* Read the core id from hardware inside each pipeline function rather than + * trusting a passed-in parameter (same defensive approach as the CVA6 SMP + * port, where LTO corrupted the argument for the secondary hart). */ +/* Timing helper: only core 0 reads the counter. A barrier before reading + * aligns all cores so the measured delta is the stage's full cost. */ +static inline void bar_and_mark(unsigned int *acc, unsigned int *last) { + synch_barrier(); + if (get_core_id() == 0) { + unsigned int now = cpu_perf_get(CSR_PCER_CYCLES); + *acc += now - *last; + *last = now; + } +} + +static void prepare_image_frame_par(frame16_t *frame) { + unsigned int id = (unsigned int)get_core_id(); + unsigned int xs, xe; + split_range(IMG_W, id, num_cores, &xs, &xe); + + unsigned int last = 0; + if (id == 0) last = cpu_perf_get(CSR_PCER_CYCLES); + + f_offset_range(frame, &offsets_frame, xs, xe); + bar_and_mark(&cyc_offset, &last); + f_mask_replace_range(frame, &bad_pixels_frame, xs, xe); + bar_and_mark(&cyc_mask, &last); +} + +static void proc_image_frame_par(frame16_t *frame, unsigned int frame_i) { + unsigned int id = (unsigned int)get_core_id(); + unsigned int xs, xe, xs2, xe2; + split_range(IMG_W, id, num_cores, &xs, &xe); + split_range(IMG_W/2, id, num_cores, &xs2, &xe2); + + unsigned int last = 0; + if (id == 0) last = cpu_perf_get(CSR_PCER_CYCLES); + + f_scrub_range(frame, frames, frame_i, xs, xe); + bar_and_mark(&cyc_scrub, &last); + f_gain_range(frame, &gains_frame, xs, xe); + bar_and_mark(&cyc_gain, &last); + f_2x2_bin_range(frame, &binned_frame, xs2, xe2); + bar_and_mark(&cyc_bin, &last); + f_coadd_range(&output_frame, &binned_frame, xs2, xe2); + bar_and_mark(&cyc_coadd, &last); +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_1_1_par_simd_v2_prof", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + num_cores = get_core_num(); + + if (rt_core_id() < num_cores) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int core_id = (unsigned int)get_core_id(); + + if (core_id == 0) { + init_frames(); + gen_rand_data(); + cyc_offset = cyc_mask = cyc_scrub = 0; + cyc_gain = cyc_bin = cyc_coadd = 0; + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + } + synch_barrier(); + + start(); + + unsigned int frame_i; + for (frame_i = OFFSET_NEIGHBOURS; + frame_i < TOTAL_FRAMES - OFFSET_NEIGHBOURS; + frame_i++) { + prepare_image_frame_par(&frames[frame_i + 2u]); + proc_image_frame_par(&frames[frame_i], frame_i); + } + + synch_barrier(); + stop(); + + if (core_id != 0) return; + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + unsigned int pixels = IMG_W * IMG_H * NUM_FRAMES; + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + unsigned int tp_int = (unsigned int)(((unsigned long long)pixels * 50ULL) / cycles); + unsigned int tp_frac = (unsigned int)((((unsigned long long)pixels * 50ULL) % cycles) * 100ULL / cycles); + + unsigned int out = (unsigned int)PIXEL(&output_frame, 0, 0); + + printf("OBPMark 1.1 - Image Calibration and Correction\n"); + printf("PULP cluster parallel + SIMD16 v2 + PROFILING (%u cores, RV32) - Astral @ 50 MHz\n", num_cores); + printf("Image: %dx%d px | Frames: %d\n", IMG_W, IMG_H, NUM_FRAMES); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Throughput : %u.%02u Mpixel/s\n", tp_int, tp_frac); + printf(" Output[0][0] : %u\n", out); + + unsigned int kernel_tot = cyc_offset + cyc_mask + cyc_scrub + + cyc_gain + cyc_bin + cyc_coadd; + printf("Per-kernel cycles (sum over 8 frames):\n"); + printf(" f_offset : %8u (%2u%%)\n", cyc_offset, kernel_tot ? cyc_offset*100u/kernel_tot : 0u); + printf(" f_mask_replace : %8u (%2u%%)\n", cyc_mask, kernel_tot ? cyc_mask*100u/kernel_tot : 0u); + printf(" f_scrub : %8u (%2u%%)\n", cyc_scrub, kernel_tot ? cyc_scrub*100u/kernel_tot : 0u); + printf(" f_gain : %8u (%2u%%)\n", cyc_gain, kernel_tot ? cyc_gain*100u/kernel_tot : 0u); + printf(" f_2x2_bin : %8u (%2u%%)\n", cyc_bin, kernel_tot ? cyc_bin*100u/kernel_tot : 0u); + printf(" f_coadd : %8u (%2u%%)\n", cyc_coadd, kernel_tot ? cyc_coadd*100u/kernel_tot : 0u); + printf(" kernels total : %8u (barriers/overhead: %u)\n", + kernel_tot, cycles > kernel_tot ? cycles - kernel_tot : 0u); + + if (out == EXPECTED_OUTPUT_0_0) { + result->errors = 0; + printf("RESULT: PASS\n"); + } else { + result->errors = 1; + printf("RESULT: FAIL (expected %u)\n", EXPECTED_OUTPUT_0_0); + } +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v3_prof/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v3_prof/Makefile new file mode 100644 index 000000000..68c57449c --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v3_prof/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_1_pulp_par_simd_v3_prof.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v3_prof/obpmark_1_1_pulp_par_simd_v3_prof.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v3_prof/obpmark_1_1_pulp_par_simd_v3_prof.c new file mode 100644 index 000000000..d97abc8a3 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_par_simd_v3_prof/obpmark_1_1_pulp_par_simd_v3_prof.c @@ -0,0 +1,373 @@ +/* + * Copyright 2024 ETH Zurich and University of Bologna. + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + * + * OBPMark Benchmark #1.1 - Image Calibration and Correction + * Parallel port for the PULP integer cluster of Astral (8 cores, RV32) + * + * SPMD: every core runs the same code on a distinct column range [xs, xe). + * Synchronisation uses the cluster hardware barrier (synch_barrier()). + * Data buffers live in .heapsram (TCDM) to keep them out of the ELF payload. + * Input: 32x32 pixels, 8 frames, synthetic data (LCG seed 21121993). + * Expected Output[0][0] = 125700 (matches sequential CVA6/PULP results). + */ + +#include "pulp.h" +#include +#include + +typedef unsigned short v2u __attribute__((vector_size(4))); +typedef signed short v2s __attribute__((vector_size(4))); + +#define IMG_W 32 +#define IMG_H 32 +#define NUM_FRAMES 8 +#define NUM_NEIGHBOURS 4 +#define TOTAL_FRAMES (NUM_FRAMES + NUM_NEIGHBOURS) +#define OFFSET_NEIGHBOURS 2 + +#define EXPECTED_OUTPUT_0_0 125700u + +typedef struct { uint16_t *f; unsigned int w; unsigned int h; } frame16_t; +typedef struct { uint8_t *f; unsigned int w; unsigned int h; } frame8_t; +typedef struct { uint32_t *f; unsigned int w; unsigned int h; } frame32_t; + +#define PIXEL(frame, x, y) ((frame)->f[(y) * (frame)->w + (x)]) + +#define HEAPSRAM __attribute__((section(".heapsram"))) + +static HEAPSRAM uint16_t _frames_data [TOTAL_FRAMES][IMG_H * IMG_W]; +static HEAPSRAM uint16_t _offsets_data [IMG_H * IMG_W]; +static HEAPSRAM uint16_t _gains_data [IMG_H * IMG_W]; +static HEAPSRAM uint8_t _badpix_data [IMG_H * IMG_W]; +static HEAPSRAM uint32_t _output_data [(IMG_H/2) * (IMG_W/2)]; +static HEAPSRAM uint32_t _binned_data [(IMG_H/2) * (IMG_W/2)]; + +static HEAPSRAM frame16_t frames[TOTAL_FRAMES]; +static HEAPSRAM frame16_t offsets_frame; +static HEAPSRAM frame16_t gains_frame; +static HEAPSRAM frame8_t bad_pixels_frame; +static HEAPSRAM frame32_t output_frame; +static HEAPSRAM frame32_t binned_frame; + +static HEAPSRAM unsigned int num_cores; + +/* Per-kernel cycle accumulators (only core 0 writes them). Each kernel is + * timed across all 8 frame iterations; the barrier before each measurement + * ensures all cores have finished the previous stage, so the delta reflects + * the true wall-clock cost of that stage (the slowest core sets the pace). */ +static HEAPSRAM unsigned int cyc_offset; +static HEAPSRAM unsigned int cyc_mask; +static HEAPSRAM unsigned int cyc_scrub; +static HEAPSRAM unsigned int cyc_gain; +static HEAPSRAM unsigned int cyc_bin; +static HEAPSRAM unsigned int cyc_coadd; + +static void init_frames(void) { + unsigned int i; + for (i = 0; i < TOTAL_FRAMES; i++) { + frames[i].f = _frames_data[i]; + frames[i].w = IMG_W; + frames[i].h = IMG_H; + } + offsets_frame.f = _offsets_data; offsets_frame.w = IMG_W; offsets_frame.h = IMG_H; + gains_frame.f = _gains_data; gains_frame.w = IMG_W; gains_frame.h = IMG_H; + bad_pixels_frame.f = _badpix_data; bad_pixels_frame.w = IMG_W; bad_pixels_frame.h = IMG_H; + output_frame.f = _output_data; output_frame.w = IMG_W/2; output_frame.h = IMG_H/2; + binned_frame.f = _binned_data; binned_frame.w = IMG_W/2; binned_frame.h = IMG_H/2; +} + +static unsigned int _rand_state; + +static unsigned int bm_rand(void) { + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} + +static void gen_rand_data(void) { + unsigned int f, x, y; + _rand_state = 21121993u; + for (f = 0; f < TOTAL_FRAMES; f++) + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&frames[f], x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&offsets_frame, x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&gains_frame, x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&bad_pixels_frame, x, y) = ((uint8_t)(bm_rand() % 65535u)) & 0x01u; + for (x = 0; x < IMG_W/2; x++) + for (y = 0; y < IMG_H/2; y++) + PIXEL(&output_frame, x, y) = 0u; +} + +/* Static column partitioning, same policy as OpenMP schedule(static). */ +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +static void f_offset_range(frame16_t *frame, frame16_t *offsets, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + unsigned int w = frame->w; + for (y = 0; y < frame->h; y++) { + uint16_t *frow = &frame->f[y * w]; + uint16_t *orow = &offsets->f[y * w]; + for (x = xs; x + 1u < xe; x += 2u) { + v2u fv = *(v2u *)&frow[x]; + v2u ov = *(v2u *)&orow[x]; + *(v2u *)&frow[x] = fv - ov; /* pv.sub.h */ + } + if (x < xe) + frow[x] -= orow[x]; + } +} + +static uint32_t f_neighbour_masked_sum(frame16_t *frame, frame8_t *mask, + int x_mid, int y_mid) { + int x, y, x_start = -1, x_stop = 1, y_start = -1, y_stop = 1; + unsigned int n_sum = 0; + uint32_t sum = 0; + if (x_mid == 0) x_start = 0; + else if (x_mid == (int)(frame->w - 1)) x_stop = 0; + if (y_mid == 0) y_start = 0; + else if (y_mid == (int)(frame->h - 1)) y_stop = 0; + for (x = x_start; x < (x_stop + 1); x++) + for (y = y_start; y < (y_stop + 1); y++) + if (PIXEL(mask, (x_mid+x), (y_mid+y)) == 0) { + sum += PIXEL(frame, (x_mid+x), (y_mid+y)); + n_sum++; + } + return (n_sum == 0u) ? 0u : sum / n_sum; +} + +static void f_mask_replace_range(frame16_t *frame, frame8_t *mask, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + if (PIXEL(mask, x, y) == 1u) + PIXEL(frame, x, y) = (uint16_t)f_neighbour_masked_sum( + frame, mask, (int)x, (int)y); +} + +/* v3 (solution D): fully SIMD 4-frame mean using add2div2. + * add2div2(x,y) computes (x+y)/2 per 16-bit lane WITHOUT intermediate + * overflow (the hardware sums at full precision then halves). Chaining: + * mean = ((fm2+fm1)/2 + (fp1+fp2)/2) / 2 = (fm2+fm1+fp1+fp2)/4 + * Both pixels of the pair are computed in parallel. NOTE: the two intermediate + * /2 truncate, so this mean can differ by a small rounding amount from the + * exact sum/4 used by the scalar reference. Whether Output[0][0] stays 125700 + * must be verified empirically. The threshold compare stays per-lane scalar. */ +static void f_scrub_range(frame16_t *frame, frame16_t *fs, unsigned int frame_i, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + unsigned int w = frame->w; + uint16_t *fm2 = fs[frame_i - 2u].f; + uint16_t *fm1 = fs[frame_i - 1u].f; + uint16_t *fp1 = fs[frame_i + 1u].f; + uint16_t *fp2 = fs[frame_i + 2u].f; + uint16_t *fcur = frame->f; + + for (y = 0; y < frame->h; y++) { + unsigned int row = y * w; + for (x = xs; x + 1u < xe; x += 2u) { + unsigned int i = row + x; + v2u a = *(v2u *)&fm2[i]; + v2u b = *(v2u *)&fm1[i]; + v2u c = *(v2u *)&fp1[i]; + v2u d = *(v2u *)&fp2[i]; + /* fully SIMD mean of the 4 frames, no overflow, both pixels at once */ + v2u ab = __builtin_pulp_add2div2(a, b); /* (a+b)/2 */ + v2u cd = __builtin_pulp_add2div2(c, d); /* (c+d)/2 */ + v2u mean = __builtin_pulp_add2div2(ab, cd); /* /2 again -> /4 total */ + v2u thr = mean + mean; /* 2*mean, per lane (pv.add.h) */ + if (fcur[i] > (unsigned int)thr[0]) fcur[i] = mean[0]; + if (fcur[i + 1u] > (unsigned int)thr[1]) fcur[i + 1u] = mean[1]; + } + if (x < xe) { + unsigned int i = row + x; + uint32_t sum = (uint32_t)fm2[i] + fm1[i] + fp1[i] + fp2[i]; + uint32_t mean = sum >> 2; + if (fcur[i] > (mean << 1)) fcur[i] = (uint16_t)mean; + } + } +} + +static void f_gain_range(frame16_t *frame, frame16_t *gains, + unsigned int xs, unsigned int xe) { + unsigned int x, y; + for (x = xs; x < xe; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) = (uint16_t)( + ((uint32_t)PIXEL(frame, x, y) * + (uint32_t)PIXEL(gains, x, y)) >> 16); +} + +static void f_2x2_bin_range(frame16_t *frame, frame32_t *binned, + unsigned int xs2, unsigned int xe2) { + unsigned int x2, y2; + unsigned int w = frame->w; + for (x2 = xs2; x2 < xe2; x2++) { + unsigned int x = x2 * 2u; + for (y2 = 0; y2 < binned->h; y2++) { + unsigned int y = y2 * 2u; + v2u top = *(v2u *)&frame->f[y * w + x]; + v2u bot = *(v2u *)&frame->f[(y + 1u) * w + x]; + v2u s = top + bot; /* pv.add.h */ + PIXEL(binned, x2, y2) = (uint32_t)s[0] + (uint32_t)s[1]; + } + } +} + +static void f_coadd_range(frame32_t *sum_frame, frame32_t *add_frame, + unsigned int xs2, unsigned int xe2) { + unsigned int x, y; + for (x = xs2; x < xe2; x++) + for (y = 0; y < sum_frame->h; y++) + PIXEL(sum_frame, x, y) += PIXEL(add_frame, x, y); +} + +/* Read the core id from hardware inside each pipeline function rather than + * trusting a passed-in parameter (same defensive approach as the CVA6 SMP + * port, where LTO corrupted the argument for the secondary hart). */ +/* Timing helper: only core 0 reads the counter. A barrier before reading + * aligns all cores so the measured delta is the stage's full cost. */ +static inline void bar_and_mark(unsigned int *acc, unsigned int *last) { + synch_barrier(); + if (get_core_id() == 0) { + unsigned int now = cpu_perf_get(CSR_PCER_CYCLES); + *acc += now - *last; + *last = now; + } +} + +static void prepare_image_frame_par(frame16_t *frame) { + unsigned int id = (unsigned int)get_core_id(); + unsigned int xs, xe; + split_range(IMG_W, id, num_cores, &xs, &xe); + + unsigned int last = 0; + if (id == 0) last = cpu_perf_get(CSR_PCER_CYCLES); + + f_offset_range(frame, &offsets_frame, xs, xe); + bar_and_mark(&cyc_offset, &last); + f_mask_replace_range(frame, &bad_pixels_frame, xs, xe); + bar_and_mark(&cyc_mask, &last); +} + +static void proc_image_frame_par(frame16_t *frame, unsigned int frame_i) { + unsigned int id = (unsigned int)get_core_id(); + unsigned int xs, xe, xs2, xe2; + split_range(IMG_W, id, num_cores, &xs, &xe); + split_range(IMG_W/2, id, num_cores, &xs2, &xe2); + + unsigned int last = 0; + if (id == 0) last = cpu_perf_get(CSR_PCER_CYCLES); + + f_scrub_range(frame, frames, frame_i, xs, xe); + bar_and_mark(&cyc_scrub, &last); + f_gain_range(frame, &gains_frame, xs, xe); + bar_and_mark(&cyc_gain, &last); + f_2x2_bin_range(frame, &binned_frame, xs2, xe2); + bar_and_mark(&cyc_bin, &last); + f_coadd_range(&output_frame, &binned_frame, xs2, xe2); + bar_and_mark(&cyc_coadd, &last); +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_1_1_par_simd_v3_prof", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + num_cores = get_core_num(); + + if (rt_core_id() < num_cores) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int core_id = (unsigned int)get_core_id(); + + if (core_id == 0) { + init_frames(); + gen_rand_data(); + cyc_offset = cyc_mask = cyc_scrub = 0; + cyc_gain = cyc_bin = cyc_coadd = 0; + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + } + synch_barrier(); + + start(); + + unsigned int frame_i; + for (frame_i = OFFSET_NEIGHBOURS; + frame_i < TOTAL_FRAMES - OFFSET_NEIGHBOURS; + frame_i++) { + prepare_image_frame_par(&frames[frame_i + 2u]); + proc_image_frame_par(&frames[frame_i], frame_i); + } + + synch_barrier(); + stop(); + + if (core_id != 0) return; + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + unsigned int pixels = IMG_W * IMG_H * NUM_FRAMES; + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + unsigned int tp_int = (unsigned int)(((unsigned long long)pixels * 50ULL) / cycles); + unsigned int tp_frac = (unsigned int)((((unsigned long long)pixels * 50ULL) % cycles) * 100ULL / cycles); + + unsigned int out = (unsigned int)PIXEL(&output_frame, 0, 0); + + printf("OBPMark 1.1 - Image Calibration and Correction\n"); + printf("PULP cluster parallel + SIMD16 v3 (add2div2) + PROFILING (%u cores, RV32) - Astral @ 50 MHz\n", num_cores); + printf("Image: %dx%d px | Frames: %d\n", IMG_W, IMG_H, NUM_FRAMES); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Throughput : %u.%02u Mpixel/s\n", tp_int, tp_frac); + printf(" Output[0][0] : %u\n", out); + + unsigned int kernel_tot = cyc_offset + cyc_mask + cyc_scrub + + cyc_gain + cyc_bin + cyc_coadd; + printf("Per-kernel cycles (sum over 8 frames):\n"); + printf(" f_offset : %8u (%2u%%)\n", cyc_offset, kernel_tot ? cyc_offset*100u/kernel_tot : 0u); + printf(" f_mask_replace : %8u (%2u%%)\n", cyc_mask, kernel_tot ? cyc_mask*100u/kernel_tot : 0u); + printf(" f_scrub : %8u (%2u%%)\n", cyc_scrub, kernel_tot ? cyc_scrub*100u/kernel_tot : 0u); + printf(" f_gain : %8u (%2u%%)\n", cyc_gain, kernel_tot ? cyc_gain*100u/kernel_tot : 0u); + printf(" f_2x2_bin : %8u (%2u%%)\n", cyc_bin, kernel_tot ? cyc_bin*100u/kernel_tot : 0u); + printf(" f_coadd : %8u (%2u%%)\n", cyc_coadd, kernel_tot ? cyc_coadd*100u/kernel_tot : 0u); + printf(" kernels total : %8u (barriers/overhead: %u)\n", + kernel_tot, cycles > kernel_tot ? cycles - kernel_tot : 0u); + + if (out == EXPECTED_OUTPUT_0_0) { + result->errors = 0; + printf("RESULT: PASS\n"); + } else { + result->errors = 1; + printf("RESULT: FAIL (expected %u)\n", EXPECTED_OUTPUT_0_0); + } +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_seq/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_seq/Makefile new file mode 100644 index 000000000..75ebac022 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_seq/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_1_pulp_seq.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_seq/obpmark_1_1_pulp_seq.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_seq/obpmark_1_1_pulp_seq.c new file mode 100644 index 000000000..e887404ac --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_1_seq/obpmark_1_1_pulp_seq.c @@ -0,0 +1,254 @@ +/* + * Copyright 2024 ETH Zurich and University of Bologna. + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + * + * OBPMark Benchmark #1.1 - Image Calibration and Correction + * Sequential port for the PULP integer cluster of Astral (1 core, RV32) + * + * Only core 0 runs the benchmark; all other cores return immediately. + * Pattern follows parMatrixMul8 from pulp_cluster regression tests. + * Data buffers use .heapsram section (same as parMatrixMul8 g_mA/g_mB/g_mC) + * which places them in TCDM without generating PT_LOAD payload — avoiding + * the large TCDM write sequence that breaks the cluster boot mechanism. + * Input: 32x32 pixels, 8 frames, synthetic data (LCG seed 21121993). + * Expected Output[0][0] = 125700 (matches sequential CVA6 host result). + */ + +#include "pulp.h" +#include +#include + +#define IMG_W 32 +#define IMG_H 32 +#define NUM_FRAMES 8 +#define NUM_NEIGHBOURS 4 +#define TOTAL_FRAMES (NUM_FRAMES + NUM_NEIGHBOURS) +#define OFFSET_NEIGHBOURS 2 + +#define EXPECTED_OUTPUT_0_0 125700u + +typedef struct { uint16_t *f; unsigned int w; unsigned int h; } frame16_t; +typedef struct { uint8_t *f; unsigned int w; unsigned int h; } frame8_t; +typedef struct { uint32_t *f; unsigned int w; unsigned int h; } frame32_t; + +#define PIXEL(frame, x, y) ((frame)->f[(y) * (frame)->w + (x)]) + +/* Place data buffers in .heapsram (TCDM) — same pattern as parMatrixMul8. + * This keeps them out of the ELF PT_LOAD payload, avoiding the large TCDM + * write sequence in load_binary() that breaks the cluster boot mechanism. */ +#define HEAPSRAM __attribute__((section(".heapsram"))) + +static HEAPSRAM uint16_t _frames_data [TOTAL_FRAMES][IMG_H * IMG_W]; +static HEAPSRAM uint16_t _offsets_data [IMG_H * IMG_W]; +static HEAPSRAM uint16_t _gains_data [IMG_H * IMG_W]; +static HEAPSRAM uint8_t _badpix_data [IMG_H * IMG_W]; +static HEAPSRAM uint32_t _output_data [(IMG_H/2) * (IMG_W/2)]; +static HEAPSRAM uint32_t _binned_data [(IMG_H/2) * (IMG_W/2)]; + +static HEAPSRAM frame16_t frames[TOTAL_FRAMES]; +static HEAPSRAM frame16_t offsets_frame; +static HEAPSRAM frame16_t gains_frame; +static HEAPSRAM frame8_t bad_pixels_frame; +static HEAPSRAM frame32_t output_frame; +static HEAPSRAM frame32_t binned_frame; + +static void init_frames(void) { + unsigned int i; + for (i = 0; i < TOTAL_FRAMES; i++) { + frames[i].f = _frames_data[i]; + frames[i].w = IMG_W; + frames[i].h = IMG_H; + } + offsets_frame.f = _offsets_data; offsets_frame.w = IMG_W; offsets_frame.h = IMG_H; + gains_frame.f = _gains_data; gains_frame.w = IMG_W; gains_frame.h = IMG_H; + bad_pixels_frame.f = _badpix_data; bad_pixels_frame.w = IMG_W; bad_pixels_frame.h = IMG_H; + output_frame.f = _output_data; output_frame.w = IMG_W/2; output_frame.h = IMG_H/2; + binned_frame.f = _binned_data; binned_frame.w = IMG_W/2; binned_frame.h = IMG_H/2; +} + +static unsigned int _rand_state; + +static unsigned int bm_rand(void) { + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} + +static void gen_rand_data(void) { + unsigned int f, x, y; + _rand_state = 21121993u; + for (f = 0; f < TOTAL_FRAMES; f++) + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&frames[f], x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&offsets_frame, x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&gains_frame, x, y) = (uint16_t)(bm_rand() % 65535u); + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&bad_pixels_frame, x, y) = ((uint8_t)(bm_rand() % 65535u)) & 0x01u; + for (x = 0; x < IMG_W/2; x++) + for (y = 0; y < IMG_H/2; y++) + PIXEL(&output_frame, x, y) = 0u; +} + +static void f_offset(frame16_t *frame, frame16_t *offsets) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) -= PIXEL(offsets, x, y); +} + +static uint32_t f_neighbour_masked_sum(frame16_t *frame, frame8_t *mask, + int x_mid, int y_mid) { + int x, y, x_start = -1, x_stop = 1, y_start = -1, y_stop = 1; + unsigned int n_sum = 0; + uint32_t sum = 0; + if (x_mid == 0) x_start = 0; + else if (x_mid == (int)(frame->w - 1)) x_stop = 0; + if (y_mid == 0) y_start = 0; + else if (y_mid == (int)(frame->h - 1)) y_stop = 0; + for (x = x_start; x < (x_stop + 1); x++) + for (y = y_start; y < (y_stop + 1); y++) + if (PIXEL(mask, (x_mid+x), (y_mid+y)) == 0) { + sum += PIXEL(frame, (x_mid+x), (y_mid+y)); + n_sum++; + } + return (n_sum == 0u) ? 0u : sum / n_sum; +} + +static void f_mask_replace(frame16_t *frame, frame8_t *mask) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + if (PIXEL(mask, x, y) == 1u) + PIXEL(frame, x, y) = (uint16_t)f_neighbour_masked_sum( + frame, mask, (int)x, (int)y); +} + +static void f_scrub(frame16_t *frame, frame16_t *fs, unsigned int frame_i) { + unsigned int x, y; + uint32_t sum, mean, thr; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) { + sum = PIXEL(&fs[frame_i-2], x, y) + PIXEL(&fs[frame_i-1], x, y) + + PIXEL(&fs[frame_i+1], x, y) + PIXEL(&fs[frame_i+2], x, y); + mean = sum / 4u; + thr = 2u * mean; + if (PIXEL(frame, x, y) > thr) + PIXEL(frame, x, y) = (uint16_t)mean; + } +} + +static void f_gain(frame16_t *frame, frame16_t *gains) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) = (uint16_t)( + ((uint32_t)PIXEL(frame, x, y) * + (uint32_t)PIXEL(gains, x, y)) >> 16); +} + +static void f_2x2_bin(frame16_t *frame, frame32_t *binned) { + unsigned int x, y; + for (x = 0; x < frame->w; x += 2) + for (y = 0; y < frame->h; y += 2) + PIXEL(binned, x/2, y/2) = PIXEL(frame, x, y ) + + PIXEL(frame, x+1, y ) + + PIXEL(frame, x, y+1 ) + + PIXEL(frame, x+1, y+1 ); +} + +static void f_coadd(frame32_t *sum_frame, frame32_t *add_frame) { + unsigned int x, y; + for (x = 0; x < sum_frame->w; x++) + for (y = 0; y < sum_frame->h; y++) + PIXEL(sum_frame, x, y) += PIXEL(add_frame, x, y); +} + +static void prepare_image_frame(frame16_t *frame) { + f_offset(frame, &offsets_frame); + f_mask_replace(frame, &bad_pixels_frame); +} + +static void proc_image_frame(frame16_t *frame, unsigned int frame_i) { + f_scrub(frame, frames, frame_i); + f_gain(frame, &gains_frame); + f_2x2_bin(frame, &binned_frame); + f_coadd(&output_frame, &binned_frame); +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_1_1_seq", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + if (get_core_id() == 0) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + if (get_core_id() != 0) return; + + init_frames(); + gen_rand_data(); + + /* Configure and start the cycle performance counter explicitly + * (bench_timer_start only calls perf_start() when PROFILE is defined). + * riscv_v5 API: conf_events selects which events, setall zeroes the + * counters, start(mask) activates counting. */ + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + + start(); + + unsigned int frame_i; + for (frame_i = OFFSET_NEIGHBOURS; + frame_i < TOTAL_FRAMES - OFFSET_NEIGHBOURS; + frame_i++) { + prepare_image_frame(&frames[frame_i + 2u]); + proc_image_frame(&frames[frame_i], frame_i); + } + + stop(); + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + /* Cluster runs at 50 MHz -> 20 ns per cycle */ + unsigned int pixels = IMG_W * IMG_H * NUM_FRAMES; + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + unsigned int tp_int = (unsigned int)(((unsigned long long)pixels * 50ULL) / cycles); + unsigned int tp_frac = (unsigned int)((((unsigned long long)pixels * 50ULL) % cycles) * 100ULL / cycles); + + unsigned int out = (unsigned int)PIXEL(&output_frame, 0, 0); + + printf("OBPMark 1.1 - Image Calibration and Correction\n"); + printf("PULP cluster sequential (1 core, RV32) - Astral @ 50 MHz\n"); + printf("Image: %dx%d px | Frames: %d\n", IMG_W, IMG_H, NUM_FRAMES); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Throughput : %u.%02u Mpixel/s\n", tp_int, tp_frac); + printf(" Output[0][0] : %u\n", out); + + if (out == EXPECTED_OUTPUT_0_0) { + result->errors = 0; + printf("RESULT: PASS\n"); + } else { + result->errors = 1; + printf("RESULT: FAIL (expected %u)\n", EXPECTED_OUTPUT_0_0); + } +} \ No newline at end of file From b54dcd3de398f800f10818b7076592d6a67620f5 Mon Sep 17 00:00:00 2001 From: Alessandro Piccini Date: Wed, 7 Oct 2026 16:47:41 +0200 Subject: [PATCH 2/2] Aggiunta porting OBPMark 1.1, 1.2, 2.1, 2.2 e 3.1 (host e cluster, seq/par) --- .../bare-metal/hostd/obpmark_1_1_host_seq.c | 291 ++++ .../hostd/obpmark_1_1_host_seq_256.c | 291 ++++ .../bare-metal/hostd/obpmark_1_2_host_seq.c | 587 ++++++++ .../hostd/obpmark_1_2_host_seq_datacheck.c | 596 ++++++++ .../hostd/obpmark_1_2_host_seq_full.c | 578 +++++++ .../hostd/obpmark_1_2_host_seq_small.c | 393 +++++ .../bare-metal/hostd/obpmark_1_2_host_smp.c | 947 ++++++++++++ .../hostd/obpmark_1_2_host_smp.c.bak | 947 ++++++++++++ .../hostd/obpmark_1_2_host_smp_datacheck.c | 948 ++++++++++++ .../hostd/obpmark_1_2_host_smp_diag.c | 899 +++++++++++ .../hostd/obpmark_1_2_host_smp_diag2.c | 885 +++++++++++ .../hostd/obpmark_1_2_host_smp_diag3.c | 874 +++++++++++ .../hostd/obpmark_1_2_host_smp_v2.c | 843 +++++++++++ .../hostd/obpmark_1_2_host_smp_v3.c | 852 +++++++++++ .../hostd/obpmark_1_2_host_smp_v4.c | 868 +++++++++++ .../hostd/obpmark_1_2_host_smp_v5.c | 892 +++++++++++ .../hostd/obpmark_1_2_host_smp_v6.c | 921 ++++++++++++ .../hostd/obpmark_1_2_host_smp_v7.c | 930 ++++++++++++ .../hostd/obpmark_1_2_host_smp_v8.c | 938 ++++++++++++ .../bare-metal/hostd/obpmark_2_1_host_seq.c | 493 ++++++ .../hostd/obpmark_2_1_host_seq_file.c | 494 ++++++ .../bare-metal/hostd/obpmark_2_1_host_smp.c | 548 +++++++ sw/tests/bare-metal/hostd/obpmark_2_1_input.h | 197 +++ .../bare-metal/hostd/obpmark_2_2_host_seq.c | 1258 ++++++++++++++++ .../hostd/obpmark_2_2_host_seq_float.c | 1212 +++++++++++++++ .../bare-metal/hostd/obpmark_2_2_host_smp.c | 1316 ++++++++++++++++ .../hostd/obpmark_2_2_host_smp_float.c | 1323 +++++++++++++++++ .../bare-metal/hostd/obpmark_3_1_host_seq.c | 349 +++++ .../bare-metal/hostd/obpmark_3_1_host_smp.c | 404 +++++ .../hostd/obpmark_3_1_host_smp.c.bak | 404 +++++ .../vers.1 32 x 32 bit/obpmark_1_1.c | 322 ++++ .../obpmark_1_2_pulpd_divtest/Makefile | 4 + .../obpmark_1_2_pulpd_divtest.c | 149 ++ .../obpmark_1_2_seq_diag/Makefile | 4 + .../obpmark_1_2_pulpd_seq_diag.c | 662 +++++++++ .../obpmark_1_2_seq_diag2/Makefile | 4 + .../obpmark_1_2_pulpd_seq_diag2.c | 692 +++++++++ .../obpmark_1_2_seq_v2/Makefile | 4 + .../obpmark_1_2_pulpd_seq_v2.c | 662 +++++++++ .../pulpd-benchmarks/obpmark_1_2_par/Makefile | 4 + .../obpmark_1_2_par/obpmark_1_2_pulpd_par.c | 836 +++++++++++ .../obpmark_1_2_pulpd_par/Makefile | 4 + .../obpmark_1_2_pulpd_par.c | 873 +++++++++++ .../obpmark_1_2_pulpd_seq/Makefile | 4 + .../obpmark_1_2_pulpd_seq.c | 660 ++++++++ .../pulpd-benchmarks/obpmark_1_2_seq/Makefile | 4 + .../obpmark_1_2_seq/obpmark_1_2_pulpd_seq.c | 623 ++++++++ .../pulpd-benchmarks/obpmark_2_1_par/Makefile | 4 + .../obpmark_2_1_par/obpmark_2_1_pulpd_par.c | 514 +++++++ .../pulpd-benchmarks/obpmark_2_1_seq/Makefile | 4 + .../obpmark_2_1_seq/obpmark_2_1_pulpd_seq.c | 474 ++++++ .../pulpd-benchmarks/obpmark_2_2_par/Makefile | 4 + .../obpmark_2_2_par/obpmark_2_2_pulpd_par.c | 1264 ++++++++++++++++ .../obpmark_2_2_par_float/Makefile | 4 + .../obpmark_2_2_pulpd_par_float.c | 1300 ++++++++++++++++ .../pulpd-benchmarks/obpmark_2_2_seq/Makefile | 4 + .../obpmark_2_2_seq/obpmark_2_2_pulpd_seq.c | 1195 +++++++++++++++ .../obpmark_2_2_seq_float/Makefile | 4 + .../obpmark_2_2_pulpd_seq_float.c | 1222 +++++++++++++++ .../pulpd-benchmarks/obpmark_3_1_par/Makefile | 4 + .../obpmark_3_1_par/obpmark_3_1_pulpd_par.c | 388 +++++ .../pulpd-benchmarks/obpmark_3_1_seq/Makefile | 4 + .../obpmark_3_1_seq/obpmark_3_1_pulpd_seq.c | 339 +++++ 63 files changed, 34717 insertions(+) create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_1_host_seq.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_1_host_seq_256.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_seq.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_seq_datacheck.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_seq_full.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_seq_small.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_smp.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_smp.c.bak create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_datacheck.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_diag.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_diag2.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_diag3.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v2.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v3.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v4.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v5.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v6.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v7.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v8.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_2_1_host_seq.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_2_1_host_seq_file.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_2_1_host_smp.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_2_1_input.h create mode 100644 sw/tests/bare-metal/hostd/obpmark_2_2_host_seq.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_2_2_host_seq_float.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_2_2_host_smp.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_2_2_host_smp_float.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_3_1_host_seq.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_3_1_host_smp.c create mode 100644 sw/tests/bare-metal/hostd/obpmark_3_1_host_smp.c.bak create mode 100644 sw/tests/bare-metal/hostd/old_obp_test/vers.1 32 x 32 bit/obpmark_1_1.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_pulpd_divtest/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_pulpd_divtest/obpmark_1_2_pulpd_divtest.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag/obpmark_1_2_pulpd_seq_diag.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag2/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag2/obpmark_1_2_pulpd_seq_diag2.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_v2/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_v2/obpmark_1_2_pulpd_seq_v2.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_par/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_par/obpmark_1_2_pulpd_par.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_par/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_par/obpmark_1_2_pulpd_par.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_seq/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_seq/obpmark_1_2_pulpd_seq.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_seq/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_seq/obpmark_1_2_pulpd_seq.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_par/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_par/obpmark_2_1_pulpd_par.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_seq/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_seq/obpmark_2_1_pulpd_seq.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par/obpmark_2_2_pulpd_par.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par_float/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par_float/obpmark_2_2_pulpd_par_float.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq/obpmark_2_2_pulpd_seq.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq_float/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq_float/obpmark_2_2_pulpd_seq_float.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_par/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_par/obpmark_3_1_pulpd_par.c create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_seq/Makefile create mode 100644 sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_seq/obpmark_3_1_pulpd_seq.c diff --git a/sw/tests/bare-metal/hostd/obpmark_1_1_host_seq.c b/sw/tests/bare-metal/hostd/obpmark_1_1_host_seq.c new file mode 100644 index 000000000..3ea66a482 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_1_host_seq.c @@ -0,0 +1,291 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.1 - Image Calibration and Correction +// Bare-metal SEQUENZIALE per Astral (1x CVA6, RV64) - dimensioni dell'originale +// +// Stessa pipeline e stesso reporting della versione 32x32, ma con dimensioni +// parametrizzabili e default uguali a quelle di scripts/run_1.1-image.sh +// dell'OBPMark originale (-w 1024 -h 1024 -f 8, dati random). +// Dimensioni alternative: -DIMG_W=256 -DIMG_H=256 [-DNUM_FRAMES=8] +// Buffer statici in DRAM (NON in L2): 1024x1024 richiede ~28 MB. +// Build di verifica su PC: gcc -O2 -DHOST_REF obpmark_1_1_host_seq.c + +#ifdef HOST_REF +#include +#include +#define car_init_start() ((void)0) +#else +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include +#endif + +// ============================================================ +// Configurazione benchmark +// ============================================================ +#ifndef IMG_W +#define IMG_W 1024 +#endif +#ifndef IMG_H +#define IMG_H 1024 +#endif +#ifndef NUM_FRAMES +#define NUM_FRAMES 8 +#endif +#define NUM_NEIGHBOURS 4 +#define TOTAL_FRAMES (NUM_FRAMES + NUM_NEIGHBOURS) +#define OFFSET_NEIGHBOURS 2 + +#define CVA6_CLK_PERIOD_NS 20ULL +#define CVA6_FREQ_HZ 50000000ULL + +// ============================================================ +// Strutture dati +// ============================================================ +typedef struct { uint16_t *f; unsigned int w; unsigned int h; } frame16_t; +typedef struct { uint8_t *f; unsigned int w; unsigned int h; } frame8_t; +typedef struct { uint32_t *f; unsigned int w; unsigned int h; } frame32_t; + +#define PIXEL(frame, x, y) ((frame)->f[(y) * (frame)->w + (x)]) + +// ============================================================ +// Backing storage statico +// ============================================================ +static uint16_t _frames_data [TOTAL_FRAMES][IMG_H * IMG_W]; +static uint16_t _offsets_data [IMG_H * IMG_W]; +static uint16_t _gains_data [IMG_H * IMG_W]; +static uint8_t _badpix_data [IMG_H * IMG_W]; +static uint32_t _output_data [(IMG_H/2) * (IMG_W/2)]; +static uint32_t _binned_data [(IMG_H/2) * (IMG_W/2)]; + +static frame16_t frames[TOTAL_FRAMES]; +static frame16_t offsets_frame; +static frame16_t gains_frame; +static frame8_t bad_pixels_frame; +static frame32_t output_frame; +static frame32_t binned_frame; + +static void init_frames(void) { + unsigned int i; + for (i = 0; i < TOTAL_FRAMES; i++) { + frames[i].f = _frames_data[i]; + frames[i].w = IMG_W; + frames[i].h = IMG_H; + } + offsets_frame.f = _offsets_data; offsets_frame.w = IMG_W; offsets_frame.h = IMG_H; + gains_frame.f = _gains_data; gains_frame.w = IMG_W; gains_frame.h = IMG_H; + bad_pixels_frame.f = _badpix_data; bad_pixels_frame.w = IMG_W; bad_pixels_frame.h = IMG_H; + output_frame.f = _output_data; output_frame.w = IMG_W/2; output_frame.h = IMG_H/2; + binned_frame.f = _binned_data; binned_frame.w = IMG_W/2; binned_frame.h = IMG_H/2; +} + +// ============================================================ +// Generazione dati sintetici (stesso seed delle versioni SMP) +// ============================================================ +static unsigned int _rand_state; + +static unsigned int bm_rand(void) { + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} + +static void gen_rand_data(void) { + unsigned int f, x, y; + const unsigned int randmax = 65535u; + _rand_state = 21121993u; + + for (f = 0; f < TOTAL_FRAMES; f++) + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&frames[f], x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&offsets_frame, x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&gains_frame, x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&bad_pixels_frame, x, y) = ((uint8_t)(bm_rand() % randmax)) & 0x01u; + + for (x = 0; x < IMG_W/2; x++) + for (y = 0; y < IMG_H/2; y++) + PIXEL(&output_frame, x, y) = 0u; +} + +// ============================================================ +// KERNEL FUNCTIONS (sequenziali, su tutta la larghezza) +// ============================================================ +static void f_offset(frame16_t *frame, frame16_t *offsets) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) -= PIXEL(offsets, x, y); +} + +static uint32_t f_neighbour_masked_sum(frame16_t *frame, frame8_t *mask, + int x_mid, int y_mid) { + int x, y; + int x_start = -1, x_stop = 1, y_start = -1, y_stop = 1; + unsigned int n_sum = 0; + uint32_t sum = 0, mean; + + if (x_mid == 0) x_start = 0; + else if (x_mid == (int)(frame->w - 1)) x_stop = 0; + if (y_mid == 0) y_start = 0; + else if (y_mid == (int)(frame->h - 1)) y_stop = 0; + + for (x = x_start; x < (x_stop + 1); x++) + for (y = y_start; y < (y_stop + 1); y++) + if (PIXEL(mask, (x_mid+x), (y_mid+y)) == 0) { + sum += PIXEL(frame, (x_mid+x), (y_mid+y)); + n_sum++; + } + + mean = (n_sum == 0u) ? 0u : sum / n_sum; + return mean; +} + +static void f_mask_replace(frame16_t *frame, frame8_t *mask) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + if (PIXEL(mask, x, y) == 1u) + PIXEL(frame, x, y) = (uint16_t)f_neighbour_masked_sum( + frame, mask, (int)x, (int)y); +} + +static void f_scrub(frame16_t *frame, frame16_t *fs, unsigned int frame_i) { + unsigned int x, y; + static unsigned int num_neighbour = 4u; + uint32_t sum, mean, thr; + + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) { + sum = PIXEL(&fs[frame_i-2], x, y) + PIXEL(&fs[frame_i-1], x, y) + + PIXEL(&fs[frame_i+1], x, y) + PIXEL(&fs[frame_i+2], x, y); + mean = sum / num_neighbour; + thr = 2u * mean; + if (PIXEL(frame, x, y) > thr) + PIXEL(frame, x, y) = (uint16_t)mean; + } +} + +static void f_gain(frame16_t *frame, frame16_t *gains) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) = (uint16_t)( + ((uint32_t)PIXEL(frame, x, y) * + (uint32_t)PIXEL(gains, x, y)) >> 16); +} + +static void f_2x2_bin(frame16_t *frame, frame32_t *binned) { + unsigned int x, y; + for (x = 0; x < frame->w; x += 2) + for (y = 0; y < frame->h; y += 2) + PIXEL(binned, x/2, y/2) = PIXEL(frame, x, y ) + + PIXEL(frame, (x+1u), y ) + + PIXEL(frame, x, (y+1u)) + + PIXEL(frame, (x+1u),(y+1u)); +} + +static void f_coadd(frame32_t *sum_frame, frame32_t *add_frame) { + unsigned int x, y; + for (x = 0; x < sum_frame->w; x++) + for (y = 0; y < sum_frame->h; y++) + PIXEL(sum_frame, x, y) += PIXEL(add_frame, x, y); +} + +// ============================================================ +// Pipeline sequenziale +// ============================================================ +static void prepare_image_frame(frame16_t *frame) { + f_offset(frame, &offsets_frame); + f_mask_replace(frame, &bad_pixels_frame); +} + +static void proc_image_frame(frame16_t *frame, unsigned int frame_i) { + f_scrub(frame, frames, frame_i); + f_gain(frame, &gains_frame); + f_2x2_bin(frame, &binned_frame); + f_coadd(&output_frame, &binned_frame); +} + +// ============================================================ +// Cycle counter +// ============================================================ +#ifdef HOST_REF +static inline uint64_t read_mcycle(void) { static uint64_t c = 1; return c += 1000; } +#else +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} +#endif + +static void print_results(uint64_t cycles) { + uint64_t time_ns = cycles * CVA6_CLK_PERIOD_NS; + uint64_t time_us = time_ns / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + uint64_t pixels = (uint64_t)IMG_W * (uint64_t)IMG_H * (uint64_t)NUM_FRAMES; + uint64_t tp_int = (pixels * 50ULL) / cycles; + uint64_t tp_frac = ((pixels * 50ULL) % cycles) * 100ULL / cycles; + + printf("Benchmark metrics (sequenziale, 1x CVA6):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n",(unsigned long long)time_ms_int, + (unsigned long long)time_ms_frac); + printf(" Throughput : %llu.%02llu Mpixel/s\r\n", + (unsigned long long)tp_int, (unsigned long long)tp_frac); +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + car_init_start(); + + printf("OBPMark #1.1 - Image Calibration and Correction\r\n"); + printf("Bare-metal sequenziale - Astral CVA6 RV64 @ 50 MHz\r\n"); + printf("Image: %dx%d px | Frames: %d\r\n", IMG_W, IMG_H, NUM_FRAMES); + + init_frames(); + gen_rand_data(); + + uint64_t t_start = read_mcycle(); + + unsigned int frame_i; + for (frame_i = OFFSET_NEIGHBOURS; + frame_i < TOTAL_FRAMES - OFFSET_NEIGHBOURS; + frame_i++) { + prepare_image_frame(&frames[frame_i + 2u]); + proc_image_frame(&frames[frame_i], frame_i); + } + + uint64_t t_end = read_mcycle(); + + printf("Done.\r\n"); + print_results(t_end - t_start); + printf("Output[0][0]: %u\r\n", (unsigned int)PIXEL(&output_frame, 0, 0)); + { // checksum su tutta l'immagine di output (piu' robusto di Output[0][0]) + uint32_t cs = 0; unsigned int i; + for (i = 0; i < (IMG_W/2)*(IMG_H/2); i++) cs = cs * 31u + _output_data[i]; + printf("Checksum : %u\r\n", (unsigned int)cs); + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_1_host_seq_256.c b/sw/tests/bare-metal/hostd/obpmark_1_1_host_seq_256.c new file mode 100644 index 000000000..5d24cf00d --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_1_host_seq_256.c @@ -0,0 +1,291 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.1 - Image Calibration and Correction +// Bare-metal SEQUENZIALE per Astral (1x CVA6, RV64) - dimensioni dell'originale +// +// Stessa pipeline e stesso reporting della versione 32x32, ma con dimensioni +// parametrizzabili e default uguali a quelle di scripts/run_1.1-image.sh +// dell'OBPMark originale (-w 1024 -h 1024 -f 8, dati random). +// Dimensioni alternative: -DIMG_W=256 -DIMG_H=256 [-DNUM_FRAMES=8] +// Buffer statici in DRAM (NON in L2): 1024x1024 richiede ~28 MB. +// Build di verifica su PC: gcc -O2 -DHOST_REF obpmark_1_1_host_seq.c + +#ifdef HOST_REF +#include +#include +#define car_init_start() ((void)0) +#else +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include +#endif + +// ============================================================ +// Configurazione benchmark +// ============================================================ +#ifndef IMG_W +#define IMG_W 256 +#endif +#ifndef IMG_H +#define IMG_H 256 +#endif +#ifndef NUM_FRAMES +#define NUM_FRAMES 8 +#endif +#define NUM_NEIGHBOURS 4 +#define TOTAL_FRAMES (NUM_FRAMES + NUM_NEIGHBOURS) +#define OFFSET_NEIGHBOURS 2 + +#define CVA6_CLK_PERIOD_NS 20ULL +#define CVA6_FREQ_HZ 50000000ULL + +// ============================================================ +// Strutture dati +// ============================================================ +typedef struct { uint16_t *f; unsigned int w; unsigned int h; } frame16_t; +typedef struct { uint8_t *f; unsigned int w; unsigned int h; } frame8_t; +typedef struct { uint32_t *f; unsigned int w; unsigned int h; } frame32_t; + +#define PIXEL(frame, x, y) ((frame)->f[(y) * (frame)->w + (x)]) + +// ============================================================ +// Backing storage statico +// ============================================================ +static uint16_t _frames_data [TOTAL_FRAMES][IMG_H * IMG_W]; +static uint16_t _offsets_data [IMG_H * IMG_W]; +static uint16_t _gains_data [IMG_H * IMG_W]; +static uint8_t _badpix_data [IMG_H * IMG_W]; +static uint32_t _output_data [(IMG_H/2) * (IMG_W/2)]; +static uint32_t _binned_data [(IMG_H/2) * (IMG_W/2)]; + +static frame16_t frames[TOTAL_FRAMES]; +static frame16_t offsets_frame; +static frame16_t gains_frame; +static frame8_t bad_pixels_frame; +static frame32_t output_frame; +static frame32_t binned_frame; + +static void init_frames(void) { + unsigned int i; + for (i = 0; i < TOTAL_FRAMES; i++) { + frames[i].f = _frames_data[i]; + frames[i].w = IMG_W; + frames[i].h = IMG_H; + } + offsets_frame.f = _offsets_data; offsets_frame.w = IMG_W; offsets_frame.h = IMG_H; + gains_frame.f = _gains_data; gains_frame.w = IMG_W; gains_frame.h = IMG_H; + bad_pixels_frame.f = _badpix_data; bad_pixels_frame.w = IMG_W; bad_pixels_frame.h = IMG_H; + output_frame.f = _output_data; output_frame.w = IMG_W/2; output_frame.h = IMG_H/2; + binned_frame.f = _binned_data; binned_frame.w = IMG_W/2; binned_frame.h = IMG_H/2; +} + +// ============================================================ +// Generazione dati sintetici (stesso seed delle versioni SMP) +// ============================================================ +static unsigned int _rand_state; + +static unsigned int bm_rand(void) { + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} + +static void gen_rand_data(void) { + unsigned int f, x, y; + const unsigned int randmax = 65535u; + _rand_state = 21121993u; + + for (f = 0; f < TOTAL_FRAMES; f++) + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&frames[f], x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&offsets_frame, x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&gains_frame, x, y) = (uint16_t)(bm_rand() % randmax); + + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&bad_pixels_frame, x, y) = ((uint8_t)(bm_rand() % randmax)) & 0x01u; + + for (x = 0; x < IMG_W/2; x++) + for (y = 0; y < IMG_H/2; y++) + PIXEL(&output_frame, x, y) = 0u; +} + +// ============================================================ +// KERNEL FUNCTIONS (sequenziali, su tutta la larghezza) +// ============================================================ +static void f_offset(frame16_t *frame, frame16_t *offsets) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) -= PIXEL(offsets, x, y); +} + +static uint32_t f_neighbour_masked_sum(frame16_t *frame, frame8_t *mask, + int x_mid, int y_mid) { + int x, y; + int x_start = -1, x_stop = 1, y_start = -1, y_stop = 1; + unsigned int n_sum = 0; + uint32_t sum = 0, mean; + + if (x_mid == 0) x_start = 0; + else if (x_mid == (int)(frame->w - 1)) x_stop = 0; + if (y_mid == 0) y_start = 0; + else if (y_mid == (int)(frame->h - 1)) y_stop = 0; + + for (x = x_start; x < (x_stop + 1); x++) + for (y = y_start; y < (y_stop + 1); y++) + if (PIXEL(mask, (x_mid+x), (y_mid+y)) == 0) { + sum += PIXEL(frame, (x_mid+x), (y_mid+y)); + n_sum++; + } + + mean = (n_sum == 0u) ? 0u : sum / n_sum; + return mean; +} + +static void f_mask_replace(frame16_t *frame, frame8_t *mask) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + if (PIXEL(mask, x, y) == 1u) + PIXEL(frame, x, y) = (uint16_t)f_neighbour_masked_sum( + frame, mask, (int)x, (int)y); +} + +static void f_scrub(frame16_t *frame, frame16_t *fs, unsigned int frame_i) { + unsigned int x, y; + static unsigned int num_neighbour = 4u; + uint32_t sum, mean, thr; + + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) { + sum = PIXEL(&fs[frame_i-2], x, y) + PIXEL(&fs[frame_i-1], x, y) + + PIXEL(&fs[frame_i+1], x, y) + PIXEL(&fs[frame_i+2], x, y); + mean = sum / num_neighbour; + thr = 2u * mean; + if (PIXEL(frame, x, y) > thr) + PIXEL(frame, x, y) = (uint16_t)mean; + } +} + +static void f_gain(frame16_t *frame, frame16_t *gains) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) + for (y = 0; y < frame->h; y++) + PIXEL(frame, x, y) = (uint16_t)( + ((uint32_t)PIXEL(frame, x, y) * + (uint32_t)PIXEL(gains, x, y)) >> 16); +} + +static void f_2x2_bin(frame16_t *frame, frame32_t *binned) { + unsigned int x, y; + for (x = 0; x < frame->w; x += 2) + for (y = 0; y < frame->h; y += 2) + PIXEL(binned, x/2, y/2) = PIXEL(frame, x, y ) + + PIXEL(frame, (x+1u), y ) + + PIXEL(frame, x, (y+1u)) + + PIXEL(frame, (x+1u),(y+1u)); +} + +static void f_coadd(frame32_t *sum_frame, frame32_t *add_frame) { + unsigned int x, y; + for (x = 0; x < sum_frame->w; x++) + for (y = 0; y < sum_frame->h; y++) + PIXEL(sum_frame, x, y) += PIXEL(add_frame, x, y); +} + +// ============================================================ +// Pipeline sequenziale +// ============================================================ +static void prepare_image_frame(frame16_t *frame) { + f_offset(frame, &offsets_frame); + f_mask_replace(frame, &bad_pixels_frame); +} + +static void proc_image_frame(frame16_t *frame, unsigned int frame_i) { + f_scrub(frame, frames, frame_i); + f_gain(frame, &gains_frame); + f_2x2_bin(frame, &binned_frame); + f_coadd(&output_frame, &binned_frame); +} + +// ============================================================ +// Cycle counter +// ============================================================ +#ifdef HOST_REF +static inline uint64_t read_mcycle(void) { static uint64_t c = 1; return c += 1000; } +#else +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} +#endif + +static void print_results(uint64_t cycles) { + uint64_t time_ns = cycles * CVA6_CLK_PERIOD_NS; + uint64_t time_us = time_ns / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + uint64_t pixels = (uint64_t)IMG_W * (uint64_t)IMG_H * (uint64_t)NUM_FRAMES; + uint64_t tp_int = (pixels * 50ULL) / cycles; + uint64_t tp_frac = ((pixels * 50ULL) % cycles) * 100ULL / cycles; + + printf("Benchmark metrics (sequenziale, 1x CVA6):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n",(unsigned long long)time_ms_int, + (unsigned long long)time_ms_frac); + printf(" Throughput : %llu.%02llu Mpixel/s\r\n", + (unsigned long long)tp_int, (unsigned long long)tp_frac); +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + car_init_start(); + + printf("OBPMark #1.1 - Image Calibration and Correction\r\n"); + printf("Bare-metal sequenziale - Astral CVA6 RV64 @ 50 MHz\r\n"); + printf("Image: %dx%d px | Frames: %d\r\n", IMG_W, IMG_H, NUM_FRAMES); + + init_frames(); + gen_rand_data(); + + uint64_t t_start = read_mcycle(); + + unsigned int frame_i; + for (frame_i = OFFSET_NEIGHBOURS; + frame_i < TOTAL_FRAMES - OFFSET_NEIGHBOURS; + frame_i++) { + prepare_image_frame(&frames[frame_i + 2u]); + proc_image_frame(&frames[frame_i], frame_i); + } + + uint64_t t_end = read_mcycle(); + + printf("Done.\r\n"); + print_results(t_end - t_start); + printf("Output[0][0]: %u\r\n", (unsigned int)PIXEL(&output_frame, 0, 0)); + { // checksum su tutta l'immagine di output (piu' robusto di Output[0][0]) + uint32_t cs = 0; unsigned int i; + for (i = 0; i < (IMG_W/2)*(IMG_H/2); i++) cs = cs * 31u + _output_data[i]; + printf("Checksum : %u\r\n", (unsigned int)cs); + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_seq.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_seq.c new file mode 100644 index 000000000..656e0d845 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_seq.c @@ -0,0 +1,587 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +static void SAR_focus(radar_params_t *params, float *out_max, float *out_min){ + *out_max = -1e30f; + *out_min = 1e30f; + + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + + for(uint32_t patch = 0; patch < params->npatch; patch++){ + SAR_range_compression(g_range_data, RANGE_H, RANGE_W, g_rrf); + complex_transpose(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->apatch, params->rvalid); + SAR_rcmc(g_azimuth_data, AZIM_H, AZIM_W, g_offsets); + SAR_azimuth_compression(g_azimuth_data, AZIM_H, AZIM_W, g_arf); + complex_transpose(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->rvalid, params->apatch); + SAR_multilook(g_range_data, g_ml_data, OUT_W, params, patch, out_max, out_min); + } + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main +// ============================================================ +int main(void) +{ + car_init_start(); + + printf("OBPMark #1.2 - Radar SAR (host seq)\r\n"); + printf("Host domain sequenziale - Astral CVA6 RV64 @ 50 MHz\r\n"); + + radar_params_t params; + params.lambda = 0.03f; + params.PRF = 1000.0f; + params.tau = 1e-5f; + params.fs = 5e7f; + params.vr = 7000.0f; + params.ro = 8e5f; + params.slope = 5e11f; + params.rsize = RSIZE; + params.asize = ASIZE; + + // rvalid = rsize - round(tau*fs), clamped to rsize if too small + float rvalid_f = (float)params.rsize - m_roundf(params.tau * params.fs); + uint32_t rvalid = (rvalid_f < (float)(params.rsize/2)) ? params.rsize : (uint32_t)rvalid_f; + params.rvalid = rvalid; + + params.avalid = (params.asize < 1024u) ? params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); // asize<1024 branch used here + // apatch = 2^ceil(log2(avalid+1)) + float lg = m_log2f((float)(params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + params.apatch = apatch; + params.npatch = params.asize / params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u\r\n", + (unsigned)params.rvalid, (unsigned)params.avalid, + (unsigned)params.apatch, (unsigned)params.npatch); + + gen_rand_radar_data(); + + uint64_t t_start = read_mcycle(); + + float vmax, vmin; + SAR_focus(¶ms, &vmax, &vmin); + + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + // Throughput: raw input complex samples (ASIZE*RSIZE) per second, in + // Ksample/s at 50 MHz (20 ns/cycle). Same fixed-point scheme as the 1.1 + // Mpixel/s metric; Ksample/s is used because with 256 input samples a + // Msample/s figure would be < 0.1 and lose all resolution. + uint64_t tp_samples = (uint64_t)ASIZE * (uint64_t)RSIZE; + uint64_t tp_int = (tp_samples * 50000ULL) / cycles; + uint64_t tp_frac = (((tp_samples * 50000ULL) % cycles) * 100ULL) / cycles; + printf(" Throughput : %llu.%02llu Ksample/s\r\n", + (unsigned long long)tp_int, (unsigned long long)tp_frac); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + printf(" Checksum : %u\r\n", (unsigned)checksum); + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_seq_datacheck.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_seq_datacheck.c new file mode 100644 index 000000000..1ecf1e1b1 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_seq_datacheck.c @@ -0,0 +1,596 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +static void SAR_focus(radar_params_t *params, float *out_max, float *out_min){ + *out_max = -1e30f; + *out_min = 1e30f; + + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + + for(uint32_t patch = 0; patch < params->npatch; patch++){ + SAR_range_compression(g_range_data, RANGE_H, RANGE_W, g_rrf); + complex_transpose(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->apatch, params->rvalid); + SAR_rcmc(g_azimuth_data, AZIM_H, AZIM_W, g_offsets); + SAR_azimuth_compression(g_azimuth_data, AZIM_H, AZIM_W, g_arf); + complex_transpose(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->rvalid, params->apatch); + SAR_multilook(g_range_data, g_ml_data, OUT_W, params, patch, out_max, out_min); + } + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main +// ============================================================ + +// DIAGNOSTIC (data comparison): sum of raw IEEE754 bit patterns of a float +// buffer - order-sensitive, catches ANY numerical difference between the +// sequential and SMP versions' synthetic input data. +static uint32_t buf_checksum_u32(const float *buf, unsigned int n){ + uint32_t s = 0; + for (unsigned int i = 0; i < n; i++){ + union { float f; uint32_t u; } v; v.f = buf[i]; + s += v.u; + } + return s; +} + +int main(void) +{ + car_init_start(); + + printf("OBPMark #1.2 - Radar SAR (SEQ, datacheck)\r\n"); + printf("Host domain sequenziale - Astral CVA6 RV64 @ 50 MHz\r\n"); + + radar_params_t params; + params.lambda = 0.03f; + params.PRF = 1000.0f; + params.tau = 1e-5f; + params.fs = 5e7f; + params.vr = 7000.0f; + params.ro = 8e5f; + params.slope = 5e11f; + params.rsize = RSIZE; + params.asize = ASIZE; + + // rvalid = rsize - round(tau*fs), clamped to rsize if too small + float rvalid_f = (float)params.rsize - m_roundf(params.tau * params.fs); + uint32_t rvalid = (rvalid_f < (float)(params.rsize/2)) ? params.rsize : (uint32_t)rvalid_f; + params.rvalid = rvalid; + + params.avalid = (params.asize < 1024u) ? params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); // asize<1024 branch used here + // apatch = 2^ceil(log2(avalid+1)) + float lg = m_log2f((float)(params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + params.apatch = apatch; + params.npatch = params.asize / params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u\r\n", + (unsigned)params.rvalid, (unsigned)params.avalid, + (unsigned)params.apatch, (unsigned)params.npatch); + + gen_rand_radar_data(); + + // DIAGNOSTIC: checksum of the raw synthetic input data, identical + // format in both the sequential and SMP versions, to verify they + // generate bit-identical input before comparing performance. + printf("DATA range_data_chk=%u\r\n", (unsigned)buf_checksum_u32(g_range_data, RANGE_H*RANGE_W)); + + uint64_t t_start = read_mcycle(); + + float vmax, vmin; + SAR_focus(¶ms, &vmax, &vmin); + + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + printf(" Checksum : %u\r\n", (unsigned)checksum); + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_seq_full.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_seq_full.c new file mode 100644 index 000000000..8f23a582d --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_seq_full.c @@ -0,0 +1,578 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +static void SAR_focus(radar_params_t *params, float *out_max, float *out_min){ + *out_max = -1e30f; + *out_min = 1e30f; + + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + + for(uint32_t patch = 0; patch < params->npatch; patch++){ + SAR_range_compression(g_range_data, RANGE_H, RANGE_W, g_rrf); + complex_transpose(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->apatch, params->rvalid); + SAR_rcmc(g_azimuth_data, AZIM_H, AZIM_W, g_offsets); + SAR_azimuth_compression(g_azimuth_data, AZIM_H, AZIM_W, g_arf); + complex_transpose(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->rvalid, params->apatch); + SAR_multilook(g_range_data, g_ml_data, OUT_W, params, patch, out_max, out_min); + } + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main +// ============================================================ +int main(void) +{ + car_init_start(); + + printf("OBPMark #1.2 - Radar SAR (FULL pipeline, small fixed input)\r\n"); + printf("Host domain sequenziale - Astral CVA6 RV64 @ 50 MHz\r\n"); + + radar_params_t params; + params.lambda = 0.03f; + params.PRF = 1000.0f; + params.tau = 1e-5f; + params.fs = 5e7f; + params.vr = 7000.0f; + params.ro = 8e5f; + params.slope = 5e11f; + params.rsize = RSIZE; + params.asize = ASIZE; + + // rvalid = rsize - round(tau*fs), clamped to rsize if too small + float rvalid_f = (float)params.rsize - m_roundf(params.tau * params.fs); + uint32_t rvalid = (rvalid_f < (float)(params.rsize/2)) ? params.rsize : (uint32_t)rvalid_f; + params.rvalid = rvalid; + + params.avalid = (params.asize < 1024u) ? params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); // asize<1024 branch used here + // apatch = 2^ceil(log2(avalid+1)) + float lg = m_log2f((float)(params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + params.apatch = apatch; + params.npatch = params.asize / params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u\r\n", + (unsigned)params.rvalid, (unsigned)params.avalid, + (unsigned)params.apatch, (unsigned)params.npatch); + + gen_rand_radar_data(); + + uint64_t t_start = read_mcycle(); + + float vmax, vmin; + SAR_focus(¶ms, &vmax, &vmin); + + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + printf(" Checksum : %u\r\n", (unsigned)checksum); + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_seq_small.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_seq_small.c new file mode 100644 index 000000000..27baef3ba --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_seq_small.c @@ -0,0 +1,393 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL port for Astral host domain (1x CVA6, RV64) +// +// Reduced but faithful pipeline of the reference SAR_focus (processing.c): +// range compression (FFT -> reference multiply -> IFFT) +// -> corner turn (complex transpose) +// -> azimuth compression (reference multiply -> IFFT) +// -> multilook (spatial binning + log2) +// -> quantize to 8 bit +// The Doppler-centroid estimation, RCMC table and squint geometry of the full +// reference are omitted: they add radar-geometry bookkeeping but do not change +// the computational character (FFT + elementwise complex ops), which is what +// this benchmark measures. +// +// Porting notes vs the reference C++ implementation: +// - std::complex replaced by an explicit {re,im} struct (bare-metal C, +// no STL). All complex ops (mul, conj, abs, polar) written by hand. +// - FFT is the iterative Cooley-Tukey kernel from the reference (GPU4S), +// operating in place on interleaved re/im float arrays. +// - sinf/cosf/sqrtf/log2f implemented in-file (no libm): the bare-metal +// toolchain has no hard-float libm and the soft-float one clashes with +// the lp64d ABI. The in-file versions use the hardware FPU. +// - malloc/free -> static buffers (no heap on bare-metal). +// - Synthetic input generated with an LCG (seed 21121993), like the 1.1 port. +// - Timing via mcycle CSR; ClkPeriod 20 ns (50 MHz). + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include + +// ============================================================ +// Self-contained math (no libm: the bare-metal toolchain lacks a +// hard-float libm, and linking the soft-float one clashes with the +// lp64d ABI). These use the hardware FPU and are accurate enough for +// the FFT twiddles and the multilook magnitude/log. +// ============================================================ +#define PI_F 3.14159265358979323846f + +// sqrtf: hardware instruction, exact +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +// reduce angle to [-pi, pi] +static inline float reduce_angle(float x){ + // x - 2*pi * round(x / (2*pi)) + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + // round to nearest + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} + +// sinf: minimax-style polynomial after range reduction +static float m_sinf(float x){ + x = reduce_angle(x); + // fold into [-pi/2, pi/2] using sin(pi - x) = sin(x) + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + // Taylor/minimax up to x^9 (good to ~1e-6 on the folded range) + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} + +// cosf via sinf +static inline float m_cosf(float x){ + return m_sinf(x + PI_F/2); +} + +// log2f: extract exponent from IEEE754 bits, polynomial on mantissa +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; // unbiased exponent + v.u = (v.u & 0x007FFFFF) | 0x3F800000; // mantissa in [1,2) + float m = v.f; + // log2(m) for m in [1,2): polynomial approximation + float t = m - 1.0f; + // minimax-ish; ln(1+t)/ln2 approximated + float p = t*(1.4426950408886f + t*(-0.7213475204444f + + t*(0.4808983469629f + t*(-0.3606737602222f + + t*0.2885390082f)))); + return (float)e + p; +} + +// ============================================================ +// Benchmark configuration (small, verifiable) +// ============================================================ +#define RANGE_SIZE 16 // samples per range line (power of two for FFT) +#define AZIMUTH_SIZE 16 // number of range lines (power of two) +#define IMG_W 8 // multilooked output width +#define IMG_H 8 // multilooked output height +#define CVA6_CLK_NS 20ULL + +#define PI 3.14159265358979323846f + +// ============================================================ +// Complex arithmetic (interleaved re/im in a float array) +// A "complex frame" is width*height complex numbers stored as +// 2*width*height floats: [re0,im0, re1,im1, ...]. +// ============================================================ +typedef struct { float re; float im; } cplx_t; + +static inline cplx_t c_add(cplx_t a, cplx_t b){ cplx_t r={a.re+b.re,a.im+b.im}; return r; } +static inline cplx_t c_mul(cplx_t a, cplx_t b){ + cplx_t r={a.re*b.re - a.im*b.im, a.re*b.im + a.im*b.re}; return r; +} +static inline cplx_t c_conj(cplx_t a){ cplx_t r={a.re,-a.im}; return r; } +static inline float c_abs(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline cplx_t c_polar(float mag, float phase){ + cplx_t r={mag*m_cosf(phase), mag*m_sinf(phase)}; return r; +} + +// ============================================================ +// Static buffers (bare-metal, no heap) +// radar data: AZIMUTH_SIZE lines x RANGE_SIZE complex samples +// ============================================================ +#define NSAMP (RANGE_SIZE * AZIMUTH_SIZE) + +static float radar_re[NSAMP]; +static float radar_im[NSAMP]; +static float aux_re[NSAMP]; // transpose scratch +static float aux_im[NSAMP]; +static float ref_range_re[RANGE_SIZE]; // range reference function (freq domain) +static float ref_range_im[RANGE_SIZE]; +static float ref_azimuth_re[AZIMUTH_SIZE]; +static float ref_azimuth_im[AZIMUTH_SIZE]; +static float ml_image[IMG_W * IMG_H]; // multilooked (float) +static uint8_t out_image[IMG_W * IMG_H]; // quantized 8-bit output + +// ============================================================ +// LCG for synthetic data (same generator as the 1.1 port) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// a float in [-1, 1) +static float rand_unit(void){ + return ((float)bm_rand() / 16384.0f) - 1.0f; +} + +static void gen_rand_data(void){ + _rand_state = 21121993u; + for(unsigned int i = 0; i < NSAMP; i++){ + radar_re[i] = rand_unit(); + radar_im[i] = rand_unit(); + } +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, in place on interleaved re/im). +// Adapted from the reference GPU4S fft_kernel. `data` holds nn complex +// numbers as 2*nn floats. isign=+1 forward, -1 inverse (unscaled). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; + wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} + +// forward FFT on one interleaved-complex row of `len` complex samples +static void fft_row(float *re, float *im, int len){ + // pack into interleaved buffer, transform, unpack + // (kept simple; uses a small stack buffer sized to RANGE/AZIMUTH max) + static float buf[2 * (RANGE_SIZE > AZIMUTH_SIZE ? RANGE_SIZE : AZIMUTH_SIZE)]; + for(int k = 0; k < len; k++){ buf[2*k] = re[k]; buf[2*k+1] = im[k]; } + fft_kernel(buf, len, 1); + for(int k = 0; k < len; k++){ re[k] = buf[2*k]; im[k] = buf[2*k+1]; } +} + +static void ifft_row(float *re, float *im, int len){ + static float buf[2 * (RANGE_SIZE > AZIMUTH_SIZE ? RANGE_SIZE : AZIMUTH_SIZE)]; + for(int k = 0; k < len; k++){ buf[2*k] = re[k]; buf[2*k+1] = im[k]; } + fft_kernel(buf, len, -1); + float inv = 1.0f / (float)len; + for(int k = 0; k < len; k++){ re[k] = buf[2*k]*inv; im[k] = buf[2*k+1]*inv; } +} + +// ============================================================ +// Reference functions: a chirp in time, transformed to frequency. +// Simplified (fc=0) chirp used as matched filter for both range and azimuth. +// ============================================================ +static void make_reference(float *re, float *im, int len, float slope){ + // chirp: exp(j * pi * slope * t^2), t centered + for(int k = 0; k < len; k++){ + float t = (float)(k - len/2); + float phase = PI * slope * t * t / (float)len; + cplx_t c = c_polar(1.0f, phase); + re[k] = c.re; im[k] = c.im; + } + // to frequency domain + fft_row(re, im, len); +} + +// multiply every row of the radar data by conj(reference) +static void reference_multiply_rows(float *dre, float *dim, + const float *rre, const float *rim, + int rows, int cols){ + for(int i = 0; i < rows; i++){ + for(int j = 0; j < cols; j++){ + int idx = i*cols + j; + cplx_t d = { dre[idx], dim[idx] }; + cplx_t r = { rre[j], rim[j] }; + cplx_t p = c_mul(d, c_conj(r)); + dre[idx] = p.re; dim[idx] = p.im; + } + } +} + +// ============================================================ +// Pipeline stages +// ============================================================ +static void range_compression(void){ + // FFT each range line + for(int i = 0; i < AZIMUTH_SIZE; i++) + fft_row(&radar_re[i*RANGE_SIZE], &radar_im[i*RANGE_SIZE], RANGE_SIZE); + // matched filter (multiply by conj of range reference) + reference_multiply_rows(radar_re, radar_im, ref_range_re, ref_range_im, + AZIMUTH_SIZE, RANGE_SIZE); + // back to time domain + for(int i = 0; i < AZIMUTH_SIZE; i++) + ifft_row(&radar_re[i*RANGE_SIZE], &radar_im[i*RANGE_SIZE], RANGE_SIZE); +} + +static void corner_turn(void){ + // transpose AZIMUTH_SIZE x RANGE_SIZE -> RANGE_SIZE x AZIMUTH_SIZE into aux + for(int i = 0; i < AZIMUTH_SIZE; i++) + for(int j = 0; j < RANGE_SIZE; j++){ + aux_re[j*AZIMUTH_SIZE + i] = radar_re[i*RANGE_SIZE + j]; + aux_im[j*AZIMUTH_SIZE + i] = radar_im[i*RANGE_SIZE + j]; + } + // copy back + for(int i = 0; i < NSAMP; i++){ radar_re[i] = aux_re[i]; radar_im[i] = aux_im[i]; } +} + +static void azimuth_compression(void){ + // now data is RANGE_SIZE rows x AZIMUTH_SIZE cols + reference_multiply_rows(radar_re, radar_im, ref_azimuth_re, ref_azimuth_im, + RANGE_SIZE, AZIMUTH_SIZE); + for(int i = 0; i < RANGE_SIZE; i++) + ifft_row(&radar_re[i*AZIMUTH_SIZE], &radar_im[i*AZIMUTH_SIZE], AZIMUTH_SIZE); +} + +static void multilook(float *out_max, float *out_min){ + // bin RANGE_SIZE x AZIMUTH_SIZE magnitude into IMG_H x IMG_W by averaging, + // then log2. Block sizes: + int bx = AZIMUTH_SIZE / IMG_W; // cols per output pixel + int by = RANGE_SIZE / IMG_H; // rows per output pixel + float vmax = -1e30f, vmin = 1e30f; + for(int i = 0; i < IMG_H; i++){ + for(int j = 0; j < IMG_W; j++){ + float sum = 0.0f; + for(int iy = 0; iy < by; iy++) + for(int jx = 0; jx < bx; jx++){ + int r = i*by + iy; + int c = j*bx + jx; + cplx_t v = { radar_re[r*AZIMUTH_SIZE + c], + radar_im[r*AZIMUTH_SIZE + c] }; + sum += c_abs(v); + } + float val = sum / (float)(bx*by); + val = (val <= 0.0f) ? 0.0f : m_log2f(val); + ml_image[i*IMG_W + j] = val; + if(val > vmax) vmax = val; + if(val < vmin) vmin = val; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(float vmax, float vmin){ + float scale = 255.0f / (vmax - vmin + 1e-30f); + for(int i = 0; i < IMG_W*IMG_H; i++){ + float q = scale * (ml_image[i] - vmin); + if(q < 0.0f) q = 0.0f; + if(q > 255.0f) q = 255.0f; + out_image[i] = (uint8_t)q; + } +} + +// ============================================================ +// SAR focus (reduced pipeline) +// ============================================================ +static void sar_focus(float *out_max, float *out_min){ + make_reference(ref_range_re, ref_range_im, RANGE_SIZE, 0.5f); + make_reference(ref_azimuth_re, ref_azimuth_im, AZIMUTH_SIZE, 0.25f); + + range_compression(); + corner_turn(); + azimuth_compression(); + multilook(out_max, out_min); + quantize(*out_max, *out_min); +} + +// ============================================================ +// Cycle counter (same as the 1.1 host port) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +// ============================================================ +// Main +// ============================================================ +int main(void) +{ + car_init_start(); + + printf("OBPMark #1.2 - Radar SAR (SMALL 16x16 for fast sim)\r\n"); + printf("Host domain sequenziale - Astral CVA6 RV64 @ 50 MHz\r\n"); + printf("Radar: %dx%d complex | Output: %dx%d px\r\n", + RANGE_SIZE, AZIMUTH_SIZE, IMG_W, IMG_H); + + gen_rand_data(); + + uint64_t t_start = read_mcycle(); + + float vmax, vmin; + sar_focus(&vmax, &vmin); + + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + + // deterministic scalar to verify correctness across ports + uint32_t checksum = 0; + for(int i = 0; i < IMG_W*IMG_H; i++) checksum += out_image[i]; + + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)out_image[0]); + printf(" Checksum : %u\r\n", (unsigned)checksum); + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp.c new file mode 100644 index 000000000..fb93e77d6 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp.c @@ -0,0 +1,947 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define NUM_HARTS 2 + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = atomic_add_w(&barrier_sense, 0u); + uint32_t arrived = atomic_add_w(&barrier_count, 1u) + 1u; + if (arrived == NUM_HARTS) { + atomic_add_w(&barrier_count, (uint32_t)(-(int)NUM_HARTS)); + asm volatile ("fence" ::: "memory"); + atomic_add_w(&barrier_sense, 1u); + } else { + while (atomic_add_w(&barrier_sense, 0u) == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// per-hart partial max/min for the SMP multilook reduction +static float g_partial_max[NUM_HARTS]; +static float g_partial_min[NUM_HARTS]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across harts gives a bit-identical +// result to the sequential version regardless of hart count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SMP variant: split the outer (nrows) loop across harts. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different harts get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SMP: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SMP: shift/remap part. NOT parallelized on purpose: it reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i. +// Splitting this by row across harts would be a genuine data race (one hart +// could read a row the other has already overwritten with a different +// value), which would make the SMP result depend on scheduling - i.e. not +// deterministic. Since this loop is cheap (rvalid*apatch = 512 iterations +// here) compared to the four FFT-heavy stages, it is kept sequential on +// hart 0. This keeps the SMP port's result bit-identical to the sequential +// full port (same execution order for this step), at a small cost in +// parallel coverage. +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SMP variant: split the outer (nfx) loop across harts. Each hart writes a +// disjoint set of output blocks (different i -> different image rows) and +// accumulates its own local max/min; hart 0 combines the two partials after +// the barrier. Bit-identical per-pixel values to the sequential version. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx_total = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// +// IMPORTANT: hart_id() is re-read at every single use point below (never +// cached in a parameter or a local that survives across a call/barrier), +// matching obpmark_1_1_smp_32.c's documented workaround: "Re-read hart_id() +// inside pipeline functions to avoid LTO register corruption". Caching it +// once in main() and passing it down as a parameter (as an earlier version +// of this file did) let LTO's cross-hart register allocation assumptions +// silently corrupt the value, producing wrong per-hart row ranges and a +// degenerate (uniform) result while still completing "successfully" - +// exactly the bug this version fixes. +// ============================================================ +static void SAR_focus_smp(radar_params_t *params, float *out_max, float *out_min){ + // Setup runs on hart 0 only, OUTSIDE the tight barrier loop below - + // same structure as obpmark_1_1_smp_32.c's init (init_frames/gen_rand_data + // run on hart 0 only, before the barrier-heavy per-frame loop begins). + if ((unsigned int)hart_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + barrier(); + + // From here on, EVERY hart executes the exact same sequence of calls in + // the exact same order, with no if(hart_id()==...) branches between + // barriers - matching obpmark_1_1_smp_32.c's pipeline functions, where + // both harts always call every f_*_range() together. Operations that + // are logically single-owner (RCMC shift, multilook max/min combine) + // are done REDUNDANTLY by both harts on the same read-only inputs + // instead of conditionally by one hart while the other waits - cheap + // enough here, and removes the asymmetric-branch structure that had + // no precedent in the working 1.1 SMP port. + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)hart_id(), NUM_HARTS, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + barrier(); + + // --- RCMC: FFT part split by AZIM_H rows --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + barrier(); + + // RCMC shift/remap: executed by hart 0 ALONE, exactly once. + // + // This loop reads data[j + offsets[ind]*width] - an arbitrary row of + // the same buffer it is writing - so it is NOT idempotent: running it + // twice over the buffer does not reproduce the single-pass result, + // because the second pass reads values the first pass already + // overwrote. A previous version ran it redundantly on both harts to + // remove the asymmetric branch; with identical input data + // (range_data_chk verified equal in both builds) that alone made the + // SMP result diverge from the sequential one (Output[0][0] 19 vs 89, + // checksum 1768 vs 2540). The asymmetry here is therefore mandatory, + // not a stylistic choice: the barriers around it keep both harts in + // lockstep, and the loop is cheap (rvalid*apatch iterations). + if ((unsigned int)hart_id() == 0) + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + barrier(); + + // --- azimuth compression: split AZIM_H rows --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)hart_id(), NUM_HARTS, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + barrier(); + + // --- multilook: split nfx blocks --- + float sx = (float)params->asize / (float)OUT_W; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + unsigned int ms, me; + unsigned int my_id = (unsigned int)hart_id(); + split_range(nfx, my_id, NUM_HARTS, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + barrier(); + + // Combine partials: redundant on both harts (cheap, NUM_HARTS=2 + // iterations), instead of hart-0-only + barrier. + { + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int h = 1; h < NUM_HARTS; h++){ + if (g_partial_max[h] > vmax) vmax = g_partial_max[h]; + if (g_partial_min[h] < vmin) vmin = g_partial_min[h]; + } + *out_max = vmax; *out_min = vmin; + } + barrier(); + } + // quantize is OUTSIDE the tight barrier loop, done by hart 0 only - + // same structure as 1.1_smp's final printf block after its last barrier. + if ((unsigned int)hart_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main (same SMP pattern as obpmark_1_1_smp_32.c: hart 0 does setup and +// calls smp_resume() to launch hart 1, which restarts at main() from the +// top and must skip the init block via the hid check) +// ============================================================ +static radar_params_t g_params; + +// Enable the FPU on the calling hart: set mstatus.FS to Initial (01). +// mstatus.FS occupies bits [14:13]; writing 0x2000 sets FS=01 (Initial), +// which is enough to make FP instructions legal. Hart 0 gets this via +// car_init_start(), but hart 1 is resumed straight into our code and may +// still have FS=Off (00), in which case ANY float instruction traps. Both +// harts call this before touching floating point. +static inline void enable_fpu(void){ + asm volatile ("csrs mstatus, %0" :: "r"(0x2000UL) : "memory"); +} + + +// ============================================================ +// All floating-point work is isolated in these two noinline functions. +// +// WHY: main()'s prologue saves whatever callee-saved registers the function +// body uses. If main uses ANY FP register, the compiler emits fsd fs0/fs1/... +// at the very top of main - executed by BOTH harts before anything else, +// including before mhartid is even read. Disassembly of the previous version +// showed exactly that: +// fsd fs0,168(sp) +// fsd fs1,160(sp) +// fsd fs2,152(sp) +// csrr s1,mhartid <- only now is the hart id read +// while obpmark_1_1_smp_32.c (which works) has an all-integer prologue. +// +// An FP instruction executed while mstatus.FS is Off raises an illegal +// instruction exception. Hart 1, resumed into main without having gone +// through car_init_start(), can have FS off - so those fsd's trap it +// immediately, which matches the trace: hart 1 never reaches program +// addresses and ends up spinning in a j pc-0 trap loop. +// +// Keeping every float out of main (and out of its inlining scope, hence +// noinline) makes main's prologue integer-only, so hart 1 can enter it. +// ============================================================ +static void __attribute__((noinline)) hart0_setup(void) +{ + enable_fpu(); + printf("OBPMark #1.2 - Radar SAR (host smp)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | Harts: %d\r\n", + (unsigned)g_params.rvalid, (unsigned)g_params.avalid, + (unsigned)g_params.apatch, (unsigned)g_params.npatch, NUM_HARTS); + + gen_rand_radar_data(); +} + +// Wrapper so the float locals vmax/vmin (and everything SAR_focus_smp needs) +// live here, not in main. Both harts call this, but only after both are +// already running and past a barrier. +static void __attribute__((noinline)) run_focus(void) +{ + enable_fpu(); + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_smp(&g_params, &vmax, &vmin); +} + +int main(void) +{ + // No cached hid here: every check below reads hart_id() fresh, on the + // spot. The previous version cached it once at the top in a local + // ("unsigned int hid = hart_id();") and reused that value across the + // whole function, including after run_focus() - a call/return boundary + // and several barriers away from where it was read. The v6 run showed + // both harts executing the final "if (hid==0)" print block (interleaved, + // garbled UART output, then a testbench crash from two concurrent + // writers), meaning hart 1 evaluated that stale cached hid as 0. Same + // root cause class as the SAR_focus_smp fix: don't let a hart-identity + // value survive across calls/barriers under LTO. + + if ((unsigned int)hart_id() == 0){ + car_init_start(); + smp_resume(); + } + + barrier(); + + if ((unsigned int)hart_id() == 0) + hart0_setup(); + + barrier(); // parameters and input data are now visible to both harts + + uint64_t t_start = ((unsigned int)hart_id() == 0) ? read_mcycle() : 0; + + run_focus(); + + barrier(); + + if ((unsigned int)hart_id() == 0){ + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + // Throughput: raw input complex samples (ASIZE*RSIZE) per second, in + // Ksample/s at 50 MHz (20 ns/cycle). Same fixed-point scheme as the 1.1 + // Mpixel/s metric; Ksample/s is used because with 256 input samples a + // Msample/s figure would be < 0.1 and lose all resolution. + uint64_t tp_samples = (uint64_t)ASIZE * (uint64_t)RSIZE; + uint64_t tp_int = (tp_samples * 50000ULL) / cycles; + uint64_t tp_frac = (((tp_samples * 50000ULL) % cycles) * 100ULL) / cycles; + printf(" Throughput : %llu.%02llu Ksample/s\r\n", + (unsigned long long)tp_int, (unsigned long long)tp_frac); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + + // Last line written by hand via uart_write_str+flush, not printf: + // the testbench triggers $finish as soon as uart_reading_byte==0, + // which can happen BETWEEN two consecutive printf calls before the + // final line is fully transmitted (same issue and same fix as + // obpmark_1_1_smp_32.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main(): returning triggers _exit(), + // which fires $finish before (or while) hart 0 is still writing its + // results to the UART. Same fix as obpmark_1_1_smp_32.c. + // + // Disable global interrupts (mstatus.MIE=0) before parking in wfi: + // without this, hart1 can be woken by spurious timer/software + // interrupts, re-check the while(1) condition, and re-enter wfi - + // each wake/sleep cycle costs real simulated time. With a much + // longer pipeline than obpmark_1_1_smp_32.c (many more barriers), + // this overhead was large enough to delay $finish substantially. + // With MIE cleared, hart1 stays parked in wfi for good. + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp.c.bak b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp.c.bak new file mode 100644 index 000000000..ab1fe0f27 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp.c.bak @@ -0,0 +1,947 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define NUM_HARTS 2 + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// per-hart partial max/min for the SMP multilook reduction +static float g_partial_max[NUM_HARTS]; +static float g_partial_min[NUM_HARTS]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across harts gives a bit-identical +// result to the sequential version regardless of hart count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SMP variant: split the outer (nrows) loop across harts. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different harts get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SMP: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SMP: shift/remap part. NOT parallelized on purpose: it reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i. +// Splitting this by row across harts would be a genuine data race (one hart +// could read a row the other has already overwritten with a different +// value), which would make the SMP result depend on scheduling - i.e. not +// deterministic. Since this loop is cheap (rvalid*apatch = 512 iterations +// here) compared to the four FFT-heavy stages, it is kept sequential on +// hart 0. This keeps the SMP port's result bit-identical to the sequential +// full port (same execution order for this step), at a small cost in +// parallel coverage. +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SMP variant: split the outer (nfx) loop across harts. Each hart writes a +// disjoint set of output blocks (different i -> different image rows) and +// accumulates its own local max/min; hart 0 combines the two partials after +// the barrier. Bit-identical per-pixel values to the sequential version. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx_total = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// +// IMPORTANT: hart_id() is re-read at every single use point below (never +// cached in a parameter or a local that survives across a call/barrier), +// matching obpmark_1_1_smp_32.c's documented workaround: "Re-read hart_id() +// inside pipeline functions to avoid LTO register corruption". Caching it +// once in main() and passing it down as a parameter (as an earlier version +// of this file did) let LTO's cross-hart register allocation assumptions +// silently corrupt the value, producing wrong per-hart row ranges and a +// degenerate (uniform) result while still completing "successfully" - +// exactly the bug this version fixes. +// ============================================================ +static void SAR_focus_smp(radar_params_t *params, float *out_max, float *out_min){ + // Setup runs on hart 0 only, OUTSIDE the tight barrier loop below - + // same structure as obpmark_1_1_smp_32.c's init (init_frames/gen_rand_data + // run on hart 0 only, before the barrier-heavy per-frame loop begins). + if ((unsigned int)hart_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + barrier(); + + // From here on, EVERY hart executes the exact same sequence of calls in + // the exact same order, with no if(hart_id()==...) branches between + // barriers - matching obpmark_1_1_smp_32.c's pipeline functions, where + // both harts always call every f_*_range() together. Operations that + // are logically single-owner (RCMC shift, multilook max/min combine) + // are done REDUNDANTLY by both harts on the same read-only inputs + // instead of conditionally by one hart while the other waits - cheap + // enough here, and removes the asymmetric-branch structure that had + // no precedent in the working 1.1 SMP port. + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)hart_id(), NUM_HARTS, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + barrier(); + + // --- RCMC: FFT part split by AZIM_H rows --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + barrier(); + + // RCMC shift/remap: executed by hart 0 ALONE, exactly once. + // + // This loop reads data[j + offsets[ind]*width] - an arbitrary row of + // the same buffer it is writing - so it is NOT idempotent: running it + // twice over the buffer does not reproduce the single-pass result, + // because the second pass reads values the first pass already + // overwrote. A previous version ran it redundantly on both harts to + // remove the asymmetric branch; with identical input data + // (range_data_chk verified equal in both builds) that alone made the + // SMP result diverge from the sequential one (Output[0][0] 19 vs 89, + // checksum 1768 vs 2540). The asymmetry here is therefore mandatory, + // not a stylistic choice: the barriers around it keep both harts in + // lockstep, and the loop is cheap (rvalid*apatch iterations). + if ((unsigned int)hart_id() == 0) + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + barrier(); + + // --- azimuth compression: split AZIM_H rows --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)hart_id(), NUM_HARTS, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + barrier(); + + // --- multilook: split nfx blocks --- + float sx = (float)params->asize / (float)OUT_W; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + unsigned int ms, me; + unsigned int my_id = (unsigned int)hart_id(); + split_range(nfx, my_id, NUM_HARTS, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + barrier(); + + // Combine partials: redundant on both harts (cheap, NUM_HARTS=2 + // iterations), instead of hart-0-only + barrier. + { + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int h = 1; h < NUM_HARTS; h++){ + if (g_partial_max[h] > vmax) vmax = g_partial_max[h]; + if (g_partial_min[h] < vmin) vmin = g_partial_min[h]; + } + *out_max = vmax; *out_min = vmin; + } + barrier(); + } + // quantize is OUTSIDE the tight barrier loop, done by hart 0 only - + // same structure as 1.1_smp's final printf block after its last barrier. + if ((unsigned int)hart_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main (same SMP pattern as obpmark_1_1_smp_32.c: hart 0 does setup and +// calls smp_resume() to launch hart 1, which restarts at main() from the +// top and must skip the init block via the hid check) +// ============================================================ +static radar_params_t g_params; + +// Enable the FPU on the calling hart: set mstatus.FS to Initial (01). +// mstatus.FS occupies bits [14:13]; writing 0x2000 sets FS=01 (Initial), +// which is enough to make FP instructions legal. Hart 0 gets this via +// car_init_start(), but hart 1 is resumed straight into our code and may +// still have FS=Off (00), in which case ANY float instruction traps. Both +// harts call this before touching floating point. +static inline void enable_fpu(void){ + asm volatile ("csrs mstatus, %0" :: "r"(0x2000UL) : "memory"); +} + + +// ============================================================ +// All floating-point work is isolated in these two noinline functions. +// +// WHY: main()'s prologue saves whatever callee-saved registers the function +// body uses. If main uses ANY FP register, the compiler emits fsd fs0/fs1/... +// at the very top of main - executed by BOTH harts before anything else, +// including before mhartid is even read. Disassembly of the previous version +// showed exactly that: +// fsd fs0,168(sp) +// fsd fs1,160(sp) +// fsd fs2,152(sp) +// csrr s1,mhartid <- only now is the hart id read +// while obpmark_1_1_smp_32.c (which works) has an all-integer prologue. +// +// An FP instruction executed while mstatus.FS is Off raises an illegal +// instruction exception. Hart 1, resumed into main without having gone +// through car_init_start(), can have FS off - so those fsd's trap it +// immediately, which matches the trace: hart 1 never reaches program +// addresses and ends up spinning in a j pc-0 trap loop. +// +// Keeping every float out of main (and out of its inlining scope, hence +// noinline) makes main's prologue integer-only, so hart 1 can enter it. +// ============================================================ +static void __attribute__((noinline)) hart0_setup(void) +{ + enable_fpu(); + printf("OBPMark #1.2 - Radar SAR (host smp)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | Harts: %d\r\n", + (unsigned)g_params.rvalid, (unsigned)g_params.avalid, + (unsigned)g_params.apatch, (unsigned)g_params.npatch, NUM_HARTS); + + gen_rand_radar_data(); +} + +// Wrapper so the float locals vmax/vmin (and everything SAR_focus_smp needs) +// live here, not in main. Both harts call this, but only after both are +// already running and past a barrier. +static void __attribute__((noinline)) run_focus(void) +{ + enable_fpu(); + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_smp(&g_params, &vmax, &vmin); +} + +int main(void) +{ + // No cached hid here: every check below reads hart_id() fresh, on the + // spot. The previous version cached it once at the top in a local + // ("unsigned int hid = hart_id();") and reused that value across the + // whole function, including after run_focus() - a call/return boundary + // and several barriers away from where it was read. The v6 run showed + // both harts executing the final "if (hid==0)" print block (interleaved, + // garbled UART output, then a testbench crash from two concurrent + // writers), meaning hart 1 evaluated that stale cached hid as 0. Same + // root cause class as the SAR_focus_smp fix: don't let a hart-identity + // value survive across calls/barriers under LTO. + + if ((unsigned int)hart_id() == 0){ + car_init_start(); + smp_resume(); + } + + barrier(); + + if ((unsigned int)hart_id() == 0) + hart0_setup(); + + barrier(); // parameters and input data are now visible to both harts + + uint64_t t_start = ((unsigned int)hart_id() == 0) ? read_mcycle() : 0; + + run_focus(); + + barrier(); + + if ((unsigned int)hart_id() == 0){ + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + // Throughput: raw input complex samples (ASIZE*RSIZE) per second, in + // Ksample/s at 50 MHz (20 ns/cycle). Same fixed-point scheme as the 1.1 + // Mpixel/s metric; Ksample/s is used because with 256 input samples a + // Msample/s figure would be < 0.1 and lose all resolution. + uint64_t tp_samples = (uint64_t)ASIZE * (uint64_t)RSIZE; + uint64_t tp_int = (tp_samples * 50000ULL) / cycles; + uint64_t tp_frac = (((tp_samples * 50000ULL) % cycles) * 100ULL) / cycles; + printf(" Throughput : %llu.%02llu Ksample/s\r\n", + (unsigned long long)tp_int, (unsigned long long)tp_frac); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + + // Last line written by hand via uart_write_str+flush, not printf: + // the testbench triggers $finish as soon as uart_reading_byte==0, + // which can happen BETWEEN two consecutive printf calls before the + // final line is fully transmitted (same issue and same fix as + // obpmark_1_1_smp_32.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main(): returning triggers _exit(), + // which fires $finish before (or while) hart 0 is still writing its + // results to the UART. Same fix as obpmark_1_1_smp_32.c. + // + // Disable global interrupts (mstatus.MIE=0) before parking in wfi: + // without this, hart1 can be woken by spurious timer/software + // interrupts, re-check the while(1) condition, and re-enter wfi - + // each wake/sleep cycle costs real simulated time. With a much + // longer pipeline than obpmark_1_1_smp_32.c (many more barriers), + // this overhead was large enough to delay $finish substantially. + // With MIE cleared, hart1 stays parked in wfi for good. + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_datacheck.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_datacheck.c new file mode 100644 index 000000000..cf0b25fba --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_datacheck.c @@ -0,0 +1,948 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define NUM_HARTS 2 + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// per-hart partial max/min for the SMP multilook reduction +static float g_partial_max[NUM_HARTS]; +static float g_partial_min[NUM_HARTS]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across harts gives a bit-identical +// result to the sequential version regardless of hart count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SMP variant: split the outer (nrows) loop across harts. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different harts get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SMP: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SMP: shift/remap part. NOT parallelized on purpose: it reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i. +// Splitting this by row across harts would be a genuine data race (one hart +// could read a row the other has already overwritten with a different +// value), which would make the SMP result depend on scheduling - i.e. not +// deterministic. Since this loop is cheap (rvalid*apatch = 512 iterations +// here) compared to the four FFT-heavy stages, it is kept sequential on +// hart 0. This keeps the SMP port's result bit-identical to the sequential +// full port (same execution order for this step), at a small cost in +// parallel coverage. +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SMP variant: split the outer (nfx) loop across harts. Each hart writes a +// disjoint set of output blocks (different i -> different image rows) and +// accumulates its own local max/min; hart 0 combines the two partials after +// the barrier. Bit-identical per-pixel values to the sequential version. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx_total = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// +// IMPORTANT: hart_id() is re-read at every single use point below (never +// cached in a parameter or a local that survives across a call/barrier), +// matching obpmark_1_1_smp_32.c's documented workaround: "Re-read hart_id() +// inside pipeline functions to avoid LTO register corruption". Caching it +// once in main() and passing it down as a parameter (as an earlier version +// of this file did) let LTO's cross-hart register allocation assumptions +// silently corrupt the value, producing wrong per-hart row ranges and a +// degenerate (uniform) result while still completing "successfully" - +// exactly the bug this version fixes. +// ============================================================ +static void SAR_focus_smp(radar_params_t *params, float *out_max, float *out_min){ + // Setup runs on hart 0 only, OUTSIDE the tight barrier loop below - + // same structure as obpmark_1_1_smp_32.c's init (init_frames/gen_rand_data + // run on hart 0 only, before the barrier-heavy per-frame loop begins). + if ((unsigned int)hart_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + barrier(); + + // From here on, EVERY hart executes the exact same sequence of calls in + // the exact same order, with no if(hart_id()==...) branches between + // barriers - matching obpmark_1_1_smp_32.c's pipeline functions, where + // both harts always call every f_*_range() together. Operations that + // are logically single-owner (RCMC shift, multilook max/min combine) + // are done REDUNDANTLY by both harts on the same read-only inputs + // instead of conditionally by one hart while the other waits - cheap + // enough here, and removes the asymmetric-branch structure that had + // no precedent in the working 1.1 SMP port. + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)hart_id(), NUM_HARTS, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + barrier(); + + // --- RCMC: FFT part split by AZIM_H rows --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + barrier(); + + // RCMC shift/remap: redundant on both harts (same deterministic + // read-only-then-write-own-copy semantics as the sequential + // version - see SAR_rcmc_shift's own comment on why it must not + // be row-split across harts). Both harts computing the identical + // result redundantly is wasteful but safe and removes the + // asymmetric branch. + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + barrier(); + + // --- azimuth compression: split AZIM_H rows --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)hart_id(), NUM_HARTS, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + barrier(); + + // --- multilook: split nfx blocks --- + float sx = (float)params->asize / (float)OUT_W; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + unsigned int ms, me; + unsigned int my_id = (unsigned int)hart_id(); + split_range(nfx, my_id, NUM_HARTS, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + barrier(); + + // Combine partials: redundant on both harts (cheap, NUM_HARTS=2 + // iterations), instead of hart-0-only + barrier. + { + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int h = 1; h < NUM_HARTS; h++){ + if (g_partial_max[h] > vmax) vmax = g_partial_max[h]; + if (g_partial_min[h] < vmin) vmin = g_partial_min[h]; + } + *out_max = vmax; *out_min = vmin; + } + barrier(); + } + // quantize is OUTSIDE the tight barrier loop, done by hart 0 only - + // same structure as 1.1_smp's final printf block after its last barrier. + if ((unsigned int)hart_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main (same SMP pattern as obpmark_1_1_smp_32.c: hart 0 does setup and +// calls smp_resume() to launch hart 1, which restarts at main() from the +// top and must skip the init block via the hid check) +// ============================================================ +static radar_params_t g_params; + +// Enable the FPU on the calling hart: set mstatus.FS to Initial (01). +// mstatus.FS occupies bits [14:13]; writing 0x2000 sets FS=01 (Initial), +// which is enough to make FP instructions legal. Hart 0 gets this via +// car_init_start(), but hart 1 is resumed straight into our code and may +// still have FS=Off (00), in which case ANY float instruction traps. Both +// harts call this before touching floating point. +static inline void enable_fpu(void){ + asm volatile ("csrs mstatus, %0" :: "r"(0x2000UL) : "memory"); +} + + +// ============================================================ +// All floating-point work is isolated in these two noinline functions. +// +// WHY: main()'s prologue saves whatever callee-saved registers the function +// body uses. If main uses ANY FP register, the compiler emits fsd fs0/fs1/... +// at the very top of main - executed by BOTH harts before anything else, +// including before mhartid is even read. Disassembly of the previous version +// showed exactly that: +// fsd fs0,168(sp) +// fsd fs1,160(sp) +// fsd fs2,152(sp) +// csrr s1,mhartid <- only now is the hart id read +// while obpmark_1_1_smp_32.c (which works) has an all-integer prologue. +// +// An FP instruction executed while mstatus.FS is Off raises an illegal +// instruction exception. Hart 1, resumed into main without having gone +// through car_init_start(), can have FS off - so those fsd's trap it +// immediately, which matches the trace: hart 1 never reaches program +// addresses and ends up spinning in a j pc-0 trap loop. +// +// Keeping every float out of main (and out of its inlining scope, hence +// noinline) makes main's prologue integer-only, so hart 1 can enter it. +// ============================================================ + +// DIAGNOSTIC (data comparison): sum of raw IEEE754 bit patterns of a float +// buffer - order-sensitive, catches ANY numerical difference between the +// sequential and SMP versions' synthetic input data. +static uint32_t buf_checksum_u32(const float *buf, unsigned int n){ + uint32_t s = 0; + for (unsigned int i = 0; i < n; i++){ + union { float f; uint32_t u; } v; v.f = buf[i]; + s += v.u; + } + return s; +} + +static void __attribute__((noinline)) hart0_setup(void) +{ + enable_fpu(); + printf("OBPMark #1.2 - Radar SAR (SMP, datacheck)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | Harts: %d\r\n", + (unsigned)g_params.rvalid, (unsigned)g_params.avalid, + (unsigned)g_params.apatch, (unsigned)g_params.npatch, NUM_HARTS); + + gen_rand_radar_data(); + + // DIAGNOSTIC: checksum of the raw synthetic input data, identical + // format in both the sequential and SMP versions, to verify they + // generate bit-identical input before comparing performance. + printf("DATA range_data_chk=%u\r\n", (unsigned)buf_checksum_u32(g_range_data, RANGE_H*RANGE_W)); +} + +// Wrapper so the float locals vmax/vmin (and everything SAR_focus_smp needs) +// live here, not in main. Both harts call this, but only after both are +// already running and past a barrier. +static void __attribute__((noinline)) run_focus(void) +{ + enable_fpu(); + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_smp(&g_params, &vmax, &vmin); +} + +int main(void) +{ + // No cached hid here: every check below reads hart_id() fresh, on the + // spot. The previous version cached it once at the top in a local + // ("unsigned int hid = hart_id();") and reused that value across the + // whole function, including after run_focus() - a call/return boundary + // and several barriers away from where it was read. The v6 run showed + // both harts executing the final "if (hid==0)" print block (interleaved, + // garbled UART output, then a testbench crash from two concurrent + // writers), meaning hart 1 evaluated that stale cached hid as 0. Same + // root cause class as the SAR_focus_smp fix: don't let a hart-identity + // value survive across calls/barriers under LTO. + + if ((unsigned int)hart_id() == 0){ + car_init_start(); + smp_resume(); + } + + barrier(); + + if ((unsigned int)hart_id() == 0) + hart0_setup(); + + barrier(); // parameters and input data are now visible to both harts + + uint64_t t_start = ((unsigned int)hart_id() == 0) ? read_mcycle() : 0; + + run_focus(); + + barrier(); + + if ((unsigned int)hart_id() == 0){ + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + + // Last line written by hand via uart_write_str+flush, not printf: + // the testbench triggers $finish as soon as uart_reading_byte==0, + // which can happen BETWEEN two consecutive printf calls before the + // final line is fully transmitted (same issue and same fix as + // obpmark_1_1_smp_32.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main(): returning triggers _exit(), + // which fires $finish before (or while) hart 0 is still writing its + // results to the UART. Same fix as obpmark_1_1_smp_32.c. + // + // Disable global interrupts (mstatus.MIE=0) before parking in wfi: + // without this, hart1 can be woken by spurious timer/software + // interrupts, re-check the while(1) condition, and re-enter wfi - + // each wake/sleep cycle costs real simulated time. With a much + // longer pipeline than obpmark_1_1_smp_32.c (many more barriers), + // this overhead was large enough to delay $finish substantially. + // With MIE cleared, hart1 stays parked in wfi for good. + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_diag.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_diag.c new file mode 100644 index 000000000..54421187e --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_diag.c @@ -0,0 +1,899 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define NUM_HARTS 2 + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// per-hart partial max/min for the SMP multilook reduction +static float g_partial_max[NUM_HARTS]; +static float g_partial_min[NUM_HARTS]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across harts gives a bit-identical +// result to the sequential version regardless of hart count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SMP variant: split the outer (nrows) loop across harts. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different harts get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SMP: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SMP: shift/remap part. NOT parallelized on purpose: it reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i. +// Splitting this by row across harts would be a genuine data race (one hart +// could read a row the other has already overwritten with a different +// value), which would make the SMP result depend on scheduling - i.e. not +// deterministic. Since this loop is cheap (rvalid*apatch = 512 iterations +// here) compared to the four FFT-heavy stages, it is kept sequential on +// hart 0. This keeps the SMP port's result bit-identical to the sequential +// full port (same execution order for this step), at a small cost in +// parallel coverage. +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SMP variant: split the outer (nfx) loop across harts. Each hart writes a +// disjoint set of output blocks (different i -> different image rows) and +// accumulates its own local max/min; hart 0 combines the two partials after +// the barrier. Bit-identical per-pixel values to the sequential version. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx_total = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// +// IMPORTANT: hart_id() is re-read at every single use point below (never +// cached in a parameter or a local that survives across a call/barrier), +// matching obpmark_1_1_smp_32.c's documented workaround: "Re-read hart_id() +// inside pipeline functions to avoid LTO register corruption". Caching it +// once in main() and passing it down as a parameter (as an earlier version +// of this file did) let LTO's cross-hart register allocation assumptions +// silently corrupt the value, producing wrong per-hart row ranges and a +// degenerate (uniform) result while still completing "successfully" - +// exactly the bug this version fixes. +// ============================================================ +static void SAR_focus_smp(radar_params_t *params, float *out_max, float *out_min){ + if ((unsigned int)hart_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + barrier(); // both harts see rrf/arf/offsets ready before using them + + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)hart_id(), NUM_HARTS, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + barrier(); + + // --- RCMC: FFT part split by AZIM_H rows, shift part hart 0 only --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + barrier(); + if ((unsigned int)hart_id() == 0) + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + barrier(); + + // --- azimuth compression: split AZIM_H rows (re-split, don't + // reuse fs/fe computed before the two barriers above - re-reading + // hart_id() and recomputing keeps every use point independent) --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)hart_id(), NUM_HARTS, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + barrier(); + + // --- multilook: split nfx blocks, combine local max/min --- + float sx = (float)params->asize / (float)OUT_W; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + unsigned int ms, me; + unsigned int my_id = (unsigned int)hart_id(); + split_range(nfx, my_id, NUM_HARTS, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + // publish partials, combine on hart 0 after barrier. my_id is used + // immediately here (no call/barrier in between since it was read), + // so it is safe as an array index. + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + barrier(); + if ((unsigned int)hart_id() == 0){ + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int h = 1; h < NUM_HARTS; h++){ + if (g_partial_max[h] > vmax) vmax = g_partial_max[h]; + if (g_partial_min[h] < vmin) vmin = g_partial_min[h]; + } + *out_max = vmax; *out_min = vmin; + } + barrier(); + } + + if ((unsigned int)hart_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); + barrier(); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// DIAGNOSTIC ONLY: sum of raw IEEE754 bit patterns of a float buffer - +// order-sensitive, catches any numerical difference between what each +// hart sees, no float-printf precision concerns. +static uint32_t buf_checksum_u32(const float *buf, unsigned int n){ + uint32_t s = 0; + for (unsigned int i = 0; i < n; i++){ + union { float f; uint32_t u; } v; v.f = buf[i]; + s += v.u; + } + return s; +} + +// ============================================================ +// Main (same SMP pattern as obpmark_1_1_smp_32.c: hart 0 does setup and +// calls smp_resume() to launch hart 1, which restarts at main() from the +// top and must skip the init block via the hid check) +// ============================================================ +static radar_params_t g_params; + +// ============================================================ +// DIAGNOSTIC ONLY (temporary): a trivial test-and-set spinlock so both +// harts can write raw UART bytes without interleaving/corrupting each +// other's output, used to prove liveness and data visibility across harts +// in a single simulation run instead of guessing across multiple runs. +// ============================================================ +static volatile uint32_t g_print_lock = 0; +static inline void diag_lock(void){ + uint32_t one = 1, old; + do { + asm volatile ("amoswap.w.aq %0, %1, (%2)" : "=r"(old) : "r"(one), "r"(&g_print_lock) : "memory"); + } while (old != 0); +} +static inline void diag_unlock(void){ + asm volatile ("amoswap.w.rl zero, zero, (%0)" :: "r"(&g_print_lock) : "memory"); +} +static void diag_print_u32(const char *label, uint32_t v){ + diag_lock(); + char buf[80]; char tmp[16]; int n = 0; char *p = buf; + const char *l = label; + while (*l) *p++ = *l++; + unsigned int q = v; + if (q == 0) tmp[n++] = '0'; + else while (q){ tmp[n++] = '0' + (q % 10); q /= 10; } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + diag_unlock(); +} + +int main(void) +{ + unsigned int hid = (unsigned int)hart_id(); + + if (hid == 0){ + car_init_start(); + printf("OBPMark #1.2 - Radar SAR (SMP DIAG)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | Harts: %d\r\n", + (unsigned)g_params.rvalid, (unsigned)g_params.apatch, + (unsigned)g_params.avalid, (unsigned)g_params.npatch, NUM_HARTS); + + // DIAGNOSTIC: what does the hardware say the hart count is? + uint32_t hw_num_harts = *reg32(&__base_regs, CHESHIRE_NUM_INT_HARTS_REG_OFFSET); + diag_print_u32("DIAG hw_num_harts=", hw_num_harts); + + gen_rand_radar_data(); + diag_print_u32("DIAG hart0 pre-resume range_data_chk=", buf_checksum_u32(g_range_data, RANGE_H*RANGE_W)); + + smp_resume(); + diag_print_u32("DIAG hart0 smp_resume returned, hid=", hid); + } + + barrier(); + + // DIAGNOSTIC: does hart1 actually reach this point? does it see the + // radar data hart0 generated before smp_resume()? Every hart prints + // its own hart_id (freshly read) and its own view of the checksum. + diag_print_u32("DIAG post-barrier1 hart_id=", (unsigned int)hart_id()); + diag_print_u32("DIAG post-barrier1 range_data_chk (this hart)=", buf_checksum_u32(g_range_data, RANGE_H*RANGE_W)); + + uint64_t t_start = (hid == 0) ? read_mcycle() : 0; + + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_smp(&g_params, &vmax, &vmin); + + barrier(); + + if (hid == 0){ + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + + // Last line written by hand via uart_write_str+flush, not printf: + // the testbench triggers $finish as soon as uart_reading_byte==0, + // which can happen BETWEEN two consecutive printf calls before the + // final line is fully transmitted (same issue and same fix as + // obpmark_1_1_smp_32.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main(): returning triggers _exit(), + // which fires $finish before (or while) hart 0 is still writing its + // results to the UART. Same fix as obpmark_1_1_smp_32.c. + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_diag2.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_diag2.c new file mode 100644 index 000000000..30443f5ef --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_diag2.c @@ -0,0 +1,885 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define NUM_HARTS 2 + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// per-hart partial max/min for the SMP multilook reduction +static float g_partial_max[NUM_HARTS]; +static float g_partial_min[NUM_HARTS]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across harts gives a bit-identical +// result to the sequential version regardless of hart count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SMP variant: split the outer (nrows) loop across harts. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different harts get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SMP: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SMP: shift/remap part. NOT parallelized on purpose: it reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i. +// Splitting this by row across harts would be a genuine data race (one hart +// could read a row the other has already overwritten with a different +// value), which would make the SMP result depend on scheduling - i.e. not +// deterministic. Since this loop is cheap (rvalid*apatch = 512 iterations +// here) compared to the four FFT-heavy stages, it is kept sequential on +// hart 0. This keeps the SMP port's result bit-identical to the sequential +// full port (same execution order for this step), at a small cost in +// parallel coverage. +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SMP variant: split the outer (nfx) loop across harts. Each hart writes a +// disjoint set of output blocks (different i -> different image rows) and +// accumulates its own local max/min; hart 0 combines the two partials after +// the barrier. Bit-identical per-pixel values to the sequential version. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx_total = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// +// IMPORTANT: hart_id() is re-read at every single use point below (never +// cached in a parameter or a local that survives across a call/barrier), +// matching obpmark_1_1_smp_32.c's documented workaround: "Re-read hart_id() +// inside pipeline functions to avoid LTO register corruption". Caching it +// once in main() and passing it down as a parameter (as an earlier version +// of this file did) let LTO's cross-hart register allocation assumptions +// silently corrupt the value, producing wrong per-hart row ranges and a +// degenerate (uniform) result while still completing "successfully" - +// exactly the bug this version fixes. +// ============================================================ +#define CVA6_CLK_NS 20ULL + +// DIAGNOSTIC ONLY: sum of raw IEEE754 bit patterns - order-sensitive, +// catches any numerical difference, no float-printf precision concerns. +static uint32_t buf_checksum_u32(const float *buf, unsigned int n){ + uint32_t s = 0; + for (unsigned int i = 0; i < n; i++){ + union { float f; uint32_t u; } v; v.f = buf[i]; + s += v.u; + } + return s; +} + +// ============================================================ +// Main (same SMP pattern as obpmark_1_1_smp_32.c: hart 0 does setup and +// calls smp_resume() to launch hart 1, which restarts at main() from the +// top and must skip the init block via the hid check) +// ============================================================ +static radar_params_t g_params; + + +static void SAR_focus_smp(radar_params_t *params, float *out_max, float *out_min){ + if ((unsigned int)hart_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + barrier(); // both harts see rrf/arf/offsets ready before using them + + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + barrier(); + + // DIAGNOSTIC (barrier-serialized): each hart reports its OWN split + // range [rs,re) and the FULL buffer checksum it now sees, right + // after the first parallel stage. This pinpoints whether the + // per-hart row split is correct AND whether both harts see the + // combined (both halves) result consistently. + if ((unsigned int)hart_id() == 0) + printf("DIAG hart0 stage1: rs=%u re=%u range_data_chk=%u\r\n", + rs, re, (unsigned)buf_checksum_u32(g_range_data, RANGE_H*RANGE_W)); + barrier(); + if ((unsigned int)hart_id() == 1) + printf("DIAG hart1 stage1: rs=%u re=%u range_data_chk=%u\r\n", + rs, re, (unsigned)buf_checksum_u32(g_range_data, RANGE_H*RANGE_W)); + barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)hart_id(), NUM_HARTS, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + barrier(); + + // --- RCMC: FFT part split by AZIM_H rows, shift part hart 0 only --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + barrier(); + if ((unsigned int)hart_id() == 0) + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + barrier(); + + // --- azimuth compression: split AZIM_H rows (re-split, don't + // reuse fs/fe computed before the two barriers above - re-reading + // hart_id() and recomputing keeps every use point independent) --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)hart_id(), NUM_HARTS, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + barrier(); + + // --- multilook: split nfx blocks, combine local max/min --- + float sx = (float)params->asize / (float)OUT_W; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + unsigned int ms, me; + unsigned int my_id = (unsigned int)hart_id(); + split_range(nfx, my_id, NUM_HARTS, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + // publish partials, combine on hart 0 after barrier. my_id is used + // immediately here (no call/barrier in between since it was read), + // so it is safe as an array index. + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + barrier(); + if ((unsigned int)hart_id() == 0){ + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int h = 1; h < NUM_HARTS; h++){ + if (g_partial_max[h] > vmax) vmax = g_partial_max[h]; + if (g_partial_min[h] < vmin) vmin = g_partial_min[h]; + } + *out_max = vmax; *out_min = vmin; + } + barrier(); + } + + if ((unsigned int)hart_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); + barrier(); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +int main(void) +{ + unsigned int hid = (unsigned int)hart_id(); + + if (hid == 0){ + car_init_start(); + printf("OBPMark #1.2 - Radar SAR (SMP DIAG2, barrier-serialized)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | Harts: %d\r\n", + (unsigned)g_params.rvalid, (unsigned)g_params.apatch, + (unsigned)g_params.avalid, (unsigned)g_params.npatch, NUM_HARTS); + + gen_rand_radar_data(); + smp_resume(); + } + + barrier(); + + // DIAGNOSTIC (barrier-serialized, no new lock mechanism): hart 0 + // prints first, then a barrier, then hart 1 prints, then a barrier. + // This uses ONLY the already-proven barrier() to avoid concurrent UART + // writes, instead of the untested spinlock from the previous diagnostic + // build (which may itself have deadlocked). + if ((unsigned int)hart_id() == 0){ + printf("DIAG hart0 post-barrier1: range_data_chk=%u\r\n", + (unsigned)buf_checksum_u32(g_range_data, RANGE_H*RANGE_W)); + } + barrier(); + if ((unsigned int)hart_id() == 1){ + printf("DIAG hart1 post-barrier1: range_data_chk=%u\r\n", + (unsigned)buf_checksum_u32(g_range_data, RANGE_H*RANGE_W)); + } + barrier(); + + uint64_t t_start = (hid == 0) ? read_mcycle() : 0; + + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_smp(&g_params, &vmax, &vmin); + + barrier(); + + if (hid == 0){ + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + + // Last line written by hand via uart_write_str+flush, not printf: + // the testbench triggers $finish as soon as uart_reading_byte==0, + // which can happen BETWEEN two consecutive printf calls before the + // final line is fully transmitted (same issue and same fix as + // obpmark_1_1_smp_32.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main(): returning triggers _exit(), + // which fires $finish before (or while) hart 0 is still writing its + // results to the UART. Same fix as obpmark_1_1_smp_32.c. + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_diag3.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_diag3.c new file mode 100644 index 000000000..77733ff89 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_diag3.c @@ -0,0 +1,874 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define NUM_HARTS 2 + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// per-hart partial max/min for the SMP multilook reduction +static float g_partial_max[NUM_HARTS]; +static float g_partial_min[NUM_HARTS]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across harts gives a bit-identical +// result to the sequential version regardless of hart count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SMP variant: split the outer (nrows) loop across harts. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different harts get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SMP: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SMP: shift/remap part. NOT parallelized on purpose: it reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i. +// Splitting this by row across harts would be a genuine data race (one hart +// could read a row the other has already overwritten with a different +// value), which would make the SMP result depend on scheduling - i.e. not +// deterministic. Since this loop is cheap (rvalid*apatch = 512 iterations +// here) compared to the four FFT-heavy stages, it is kept sequential on +// hart 0. This keeps the SMP port's result bit-identical to the sequential +// full port (same execution order for this step), at a small cost in +// parallel coverage. +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SMP variant: split the outer (nfx) loop across harts. Each hart writes a +// disjoint set of output blocks (different i -> different image rows) and +// accumulates its own local max/min; hart 0 combines the two partials after +// the barrier. Bit-identical per-pixel values to the sequential version. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx_total = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// +// IMPORTANT: hart_id() is re-read at every single use point below (never +// cached in a parameter or a local that survives across a call/barrier), +// matching obpmark_1_1_smp_32.c's documented workaround: "Re-read hart_id() +// inside pipeline functions to avoid LTO register corruption". Caching it +// once in main() and passing it down as a parameter (as an earlier version +// of this file did) let LTO's cross-hart register allocation assumptions +// silently corrupt the value, producing wrong per-hart row ranges and a +// degenerate (uniform) result while still completing "successfully" - +// exactly the bug this version fixes. +// ============================================================ +// ============================================================ +// DIAGNOSTIC (temporary): lightweight trace. No barriers, no locks - +// output from the two harts may interleave, but every line carries its +// own freshly-read hart id, so we can always tell who wrote what. The +// goal is to see EXACTLY how far hart 1 gets, in a single run. +// ============================================================ +static void trace(const char *tag){ + printf("TRACE h%u %s\r\n", (unsigned)hart_id(), tag); +} +static void trace2(const char *tag, uint32_t a, uint32_t b){ + printf("TRACE h%u %s %u %u\r\n", (unsigned)hart_id(), tag, (unsigned)a, (unsigned)b); +} + +static void SAR_focus_smp(radar_params_t *params, float *out_max, float *out_min){ + trace("enter_focus"); + if ((unsigned int)hart_id() == 0){ + trace("setup_begin"); + SAR_range_ref(g_rrf, params); + trace("after_range_ref"); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + trace("after_DCE"); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + trace("after_rcmc_table"); + SAR_azimuth_ref(g_arf, params, fDc); + trace("setup_end"); + } + trace("pre_barrier_setup"); + barrier(); // both harts see rrf/arf/offsets ready before using them + trace("post_barrier_setup"); + + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + trace2("stage1_split", rs, re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + trace("stage1_done"); + barrier(); + trace("stage1_post_barrier"); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)hart_id(), NUM_HARTS, &ts, &te); + trace2("stage2_split", ts, te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + trace("stage2_done"); + barrier(); + trace("stage2_post_barrier"); + + // --- RCMC: FFT part split by AZIM_H rows, shift part hart 0 only --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + barrier(); + if ((unsigned int)hart_id() == 0) + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + barrier(); + + // --- azimuth compression: split AZIM_H rows (re-split, don't + // reuse fs/fe computed before the two barriers above - re-reading + // hart_id() and recomputing keeps every use point independent) --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)hart_id(), NUM_HARTS, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + barrier(); + + // --- multilook: split nfx blocks, combine local max/min --- + float sx = (float)params->asize / (float)OUT_W; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + unsigned int ms, me; + unsigned int my_id = (unsigned int)hart_id(); + split_range(nfx, my_id, NUM_HARTS, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + // publish partials, combine on hart 0 after barrier. my_id is used + // immediately here (no call/barrier in between since it was read), + // so it is safe as an array index. + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + barrier(); + if ((unsigned int)hart_id() == 0){ + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int h = 1; h < NUM_HARTS; h++){ + if (g_partial_max[h] > vmax) vmax = g_partial_max[h]; + if (g_partial_min[h] < vmin) vmin = g_partial_min[h]; + } + *out_max = vmax; *out_min = vmin; + } + barrier(); + } + + if ((unsigned int)hart_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); + barrier(); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main (same SMP pattern as obpmark_1_1_smp_32.c: hart 0 does setup and +// calls smp_resume() to launch hart 1, which restarts at main() from the +// top and must skip the init block via the hid check) +// ============================================================ +static radar_params_t g_params; + +int main(void) +{ + unsigned int hid = (unsigned int)hart_id(); + + if (hid == 0){ + car_init_start(); + printf("OBPMark #1.2 - Radar SAR (SMP DIAG3, full trace)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | Harts: %d\r\n", + (unsigned)g_params.rvalid, (unsigned)g_params.apatch, + (unsigned)g_params.avalid, (unsigned)g_params.npatch, NUM_HARTS); + + gen_rand_radar_data(); + smp_resume(); + } + + trace("main_pre_barrier1"); + barrier(); + trace("main_post_barrier1"); + + uint64_t t_start = (hid == 0) ? read_mcycle() : 0; + + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_smp(&g_params, &vmax, &vmin); + trace("main_focus_returned"); + + barrier(); + trace("main_post_barrier_final"); + + if (hid == 0){ + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + + // Last line written by hand via uart_write_str+flush, not printf: + // the testbench triggers $finish as soon as uart_reading_byte==0, + // which can happen BETWEEN two consecutive printf calls before the + // final line is fully transmitted (same issue and same fix as + // obpmark_1_1_smp_32.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main(): returning triggers _exit(), + // which fires $finish before (or while) hart 0 is still writing its + // results to the UART. Same fix as obpmark_1_1_smp_32.c. + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v2.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v2.c new file mode 100644 index 000000000..d47b3fc68 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v2.c @@ -0,0 +1,843 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define NUM_HARTS 2 + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// per-hart partial max/min for the SMP multilook reduction +static float g_partial_max[NUM_HARTS]; +static float g_partial_min[NUM_HARTS]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across harts gives a bit-identical +// result to the sequential version regardless of hart count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SMP variant: split the outer (nrows) loop across harts. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different harts get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SMP: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SMP: shift/remap part. NOT parallelized on purpose: it reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i. +// Splitting this by row across harts would be a genuine data race (one hart +// could read a row the other has already overwritten with a different +// value), which would make the SMP result depend on scheduling - i.e. not +// deterministic. Since this loop is cheap (rvalid*apatch = 512 iterations +// here) compared to the four FFT-heavy stages, it is kept sequential on +// hart 0. This keeps the SMP port's result bit-identical to the sequential +// full port (same execution order for this step), at a small cost in +// parallel coverage. +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SMP variant: split the outer (nfx) loop across harts. Each hart writes a +// disjoint set of output blocks (different i -> different image rows) and +// accumulates its own local max/min; hart 0 combines the two partials after +// the barrier. Bit-identical per-pixel values to the sequential version. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx_total = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// +// IMPORTANT: hart_id() is re-read at every single use point below (never +// cached in a parameter or a local that survives across a call/barrier), +// matching obpmark_1_1_smp_32.c's documented workaround: "Re-read hart_id() +// inside pipeline functions to avoid LTO register corruption". Caching it +// once in main() and passing it down as a parameter (as an earlier version +// of this file did) let LTO's cross-hart register allocation assumptions +// silently corrupt the value, producing wrong per-hart row ranges and a +// degenerate (uniform) result while still completing "successfully" - +// exactly the bug this version fixes. +// ============================================================ +static void SAR_focus_smp(radar_params_t *params, float *out_max, float *out_min){ + if ((unsigned int)hart_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + barrier(); // both harts see rrf/arf/offsets ready before using them + + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)hart_id(), NUM_HARTS, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + barrier(); + + // --- RCMC: FFT part split by AZIM_H rows, shift part hart 0 only --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + barrier(); + if ((unsigned int)hart_id() == 0) + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + barrier(); + + // --- azimuth compression: split AZIM_H rows (re-split, don't + // reuse fs/fe computed before the two barriers above - re-reading + // hart_id() and recomputing keeps every use point independent) --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)hart_id(), NUM_HARTS, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + barrier(); + + // --- multilook: split nfx blocks, combine local max/min --- + float sx = (float)params->asize / (float)OUT_W; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + unsigned int ms, me; + unsigned int my_id = (unsigned int)hart_id(); + split_range(nfx, my_id, NUM_HARTS, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + // publish partials, combine on hart 0 after barrier. my_id is used + // immediately here (no call/barrier in between since it was read), + // so it is safe as an array index. + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + barrier(); + if ((unsigned int)hart_id() == 0){ + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int h = 1; h < NUM_HARTS; h++){ + if (g_partial_max[h] > vmax) vmax = g_partial_max[h]; + if (g_partial_min[h] < vmin) vmin = g_partial_min[h]; + } + *out_max = vmax; *out_min = vmin; + } + barrier(); + } + + if ((unsigned int)hart_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); + barrier(); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main (same SMP pattern as obpmark_1_1_smp_32.c: hart 0 does setup and +// calls smp_resume() to launch hart 1, which restarts at main() from the +// top and must skip the init block via the hid check) +// ============================================================ +static radar_params_t g_params; + +int main(void) +{ + unsigned int hid = (unsigned int)hart_id(); + + if (hid == 0){ + car_init_start(); + printf("OBPMark #1.2 - Radar SAR (SMP v2, fresh hart_id)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | Harts: %d\r\n", + (unsigned)g_params.rvalid, (unsigned)g_params.apatch, + (unsigned)g_params.avalid, (unsigned)g_params.npatch, NUM_HARTS); + + gen_rand_radar_data(); + smp_resume(); + } + + barrier(); + + uint64_t t_start = (hid == 0) ? read_mcycle() : 0; + + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_smp(&g_params, &vmax, &vmin); + + barrier(); + + if (hid == 0){ + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + + // Last line written by hand via uart_write_str+flush, not printf: + // the testbench triggers $finish as soon as uart_reading_byte==0, + // which can happen BETWEEN two consecutive printf calls before the + // final line is fully transmitted (same issue and same fix as + // obpmark_1_1_smp_32.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main(): returning triggers _exit(), + // which fires $finish before (or while) hart 0 is still writing its + // results to the UART. Same fix as obpmark_1_1_smp_32.c. + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v3.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v3.c new file mode 100644 index 000000000..f4e7b9c33 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v3.c @@ -0,0 +1,852 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define NUM_HARTS 2 + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// per-hart partial max/min for the SMP multilook reduction +static float g_partial_max[NUM_HARTS]; +static float g_partial_min[NUM_HARTS]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across harts gives a bit-identical +// result to the sequential version regardless of hart count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SMP variant: split the outer (nrows) loop across harts. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different harts get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SMP: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SMP: shift/remap part. NOT parallelized on purpose: it reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i. +// Splitting this by row across harts would be a genuine data race (one hart +// could read a row the other has already overwritten with a different +// value), which would make the SMP result depend on scheduling - i.e. not +// deterministic. Since this loop is cheap (rvalid*apatch = 512 iterations +// here) compared to the four FFT-heavy stages, it is kept sequential on +// hart 0. This keeps the SMP port's result bit-identical to the sequential +// full port (same execution order for this step), at a small cost in +// parallel coverage. +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SMP variant: split the outer (nfx) loop across harts. Each hart writes a +// disjoint set of output blocks (different i -> different image rows) and +// accumulates its own local max/min; hart 0 combines the two partials after +// the barrier. Bit-identical per-pixel values to the sequential version. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx_total = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// +// IMPORTANT: hart_id() is re-read at every single use point below (never +// cached in a parameter or a local that survives across a call/barrier), +// matching obpmark_1_1_smp_32.c's documented workaround: "Re-read hart_id() +// inside pipeline functions to avoid LTO register corruption". Caching it +// once in main() and passing it down as a parameter (as an earlier version +// of this file did) let LTO's cross-hart register allocation assumptions +// silently corrupt the value, producing wrong per-hart row ranges and a +// degenerate (uniform) result while still completing "successfully" - +// exactly the bug this version fixes. +// ============================================================ +static void SAR_focus_smp(radar_params_t *params, float *out_max, float *out_min){ + if ((unsigned int)hart_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + barrier(); // both harts see rrf/arf/offsets ready before using them + + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)hart_id(), NUM_HARTS, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + barrier(); + + // --- RCMC: FFT part split by AZIM_H rows, shift part hart 0 only --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + barrier(); + if ((unsigned int)hart_id() == 0) + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + barrier(); + + // --- azimuth compression: split AZIM_H rows (re-split, don't + // reuse fs/fe computed before the two barriers above - re-reading + // hart_id() and recomputing keeps every use point independent) --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)hart_id(), NUM_HARTS, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + barrier(); + + // --- multilook: split nfx blocks, combine local max/min --- + float sx = (float)params->asize / (float)OUT_W; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + unsigned int ms, me; + unsigned int my_id = (unsigned int)hart_id(); + split_range(nfx, my_id, NUM_HARTS, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + // publish partials, combine on hart 0 after barrier. my_id is used + // immediately here (no call/barrier in between since it was read), + // so it is safe as an array index. + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + barrier(); + if ((unsigned int)hart_id() == 0){ + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int h = 1; h < NUM_HARTS; h++){ + if (g_partial_max[h] > vmax) vmax = g_partial_max[h]; + if (g_partial_min[h] < vmin) vmin = g_partial_min[h]; + } + *out_max = vmax; *out_min = vmin; + } + barrier(); + } + + if ((unsigned int)hart_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); + barrier(); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main (same SMP pattern as obpmark_1_1_smp_32.c: hart 0 does setup and +// calls smp_resume() to launch hart 1, which restarts at main() from the +// top and must skip the init block via the hid check) +// ============================================================ +static radar_params_t g_params; + +int main(void) +{ + unsigned int hid = (unsigned int)hart_id(); + + if (hid == 0){ + car_init_start(); + printf("OBPMark #1.2 - Radar SAR (SMP v3, mie disabled on hart1 park)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | Harts: %d\r\n", + (unsigned)g_params.rvalid, (unsigned)g_params.apatch, + (unsigned)g_params.avalid, (unsigned)g_params.npatch, NUM_HARTS); + + gen_rand_radar_data(); + smp_resume(); + } + + barrier(); + + uint64_t t_start = (hid == 0) ? read_mcycle() : 0; + + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_smp(&g_params, &vmax, &vmin); + + barrier(); + + if (hid == 0){ + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + + // Last line written by hand via uart_write_str+flush, not printf: + // the testbench triggers $finish as soon as uart_reading_byte==0, + // which can happen BETWEEN two consecutive printf calls before the + // final line is fully transmitted (same issue and same fix as + // obpmark_1_1_smp_32.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main(): returning triggers _exit(), + // which fires $finish before (or while) hart 0 is still writing its + // results to the UART. Same fix as obpmark_1_1_smp_32.c. + // + // Disable global interrupts (mstatus.MIE=0) before parking in wfi: + // without this, hart1 can be woken by spurious timer/software + // interrupts, re-check the while(1) condition, and re-enter wfi - + // each wake/sleep cycle costs real simulated time. With a much + // longer pipeline than obpmark_1_1_smp_32.c (many more barriers), + // this overhead was large enough to delay $finish substantially. + // With MIE cleared, hart1 stays parked in wfi for good. + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v4.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v4.c new file mode 100644 index 000000000..ba62a6587 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v4.c @@ -0,0 +1,868 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define NUM_HARTS 2 + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// per-hart partial max/min for the SMP multilook reduction +static float g_partial_max[NUM_HARTS]; +static float g_partial_min[NUM_HARTS]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across harts gives a bit-identical +// result to the sequential version regardless of hart count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SMP variant: split the outer (nrows) loop across harts. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different harts get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SMP: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SMP: shift/remap part. NOT parallelized on purpose: it reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i. +// Splitting this by row across harts would be a genuine data race (one hart +// could read a row the other has already overwritten with a different +// value), which would make the SMP result depend on scheduling - i.e. not +// deterministic. Since this loop is cheap (rvalid*apatch = 512 iterations +// here) compared to the four FFT-heavy stages, it is kept sequential on +// hart 0. This keeps the SMP port's result bit-identical to the sequential +// full port (same execution order for this step), at a small cost in +// parallel coverage. +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SMP variant: split the outer (nfx) loop across harts. Each hart writes a +// disjoint set of output blocks (different i -> different image rows) and +// accumulates its own local max/min; hart 0 combines the two partials after +// the barrier. Bit-identical per-pixel values to the sequential version. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx_total = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// +// IMPORTANT: hart_id() is re-read at every single use point below (never +// cached in a parameter or a local that survives across a call/barrier), +// matching obpmark_1_1_smp_32.c's documented workaround: "Re-read hart_id() +// inside pipeline functions to avoid LTO register corruption". Caching it +// once in main() and passing it down as a parameter (as an earlier version +// of this file did) let LTO's cross-hart register allocation assumptions +// silently corrupt the value, producing wrong per-hart row ranges and a +// degenerate (uniform) result while still completing "successfully" - +// exactly the bug this version fixes. +// ============================================================ +static void SAR_focus_smp(radar_params_t *params, float *out_max, float *out_min){ + // Setup runs on hart 0 only, OUTSIDE the tight barrier loop below - + // same structure as obpmark_1_1_smp_32.c's init (init_frames/gen_rand_data + // run on hart 0 only, before the barrier-heavy per-frame loop begins). + if ((unsigned int)hart_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + barrier(); + + // From here on, EVERY hart executes the exact same sequence of calls in + // the exact same order, with no if(hart_id()==...) branches between + // barriers - matching obpmark_1_1_smp_32.c's pipeline functions, where + // both harts always call every f_*_range() together. Operations that + // are logically single-owner (RCMC shift, multilook max/min combine) + // are done REDUNDANTLY by both harts on the same read-only inputs + // instead of conditionally by one hart while the other waits - cheap + // enough here, and removes the asymmetric-branch structure that had + // no precedent in the working 1.1 SMP port. + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)hart_id(), NUM_HARTS, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + barrier(); + + // --- RCMC: FFT part split by AZIM_H rows --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + barrier(); + + // RCMC shift/remap: redundant on both harts (same deterministic + // read-only-then-write-own-copy semantics as the sequential + // version - see SAR_rcmc_shift's own comment on why it must not + // be row-split across harts). Both harts computing the identical + // result redundantly is wasteful but safe and removes the + // asymmetric branch. + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + barrier(); + + // --- azimuth compression: split AZIM_H rows --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)hart_id(), NUM_HARTS, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + barrier(); + + // --- multilook: split nfx blocks --- + float sx = (float)params->asize / (float)OUT_W; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + unsigned int ms, me; + unsigned int my_id = (unsigned int)hart_id(); + split_range(nfx, my_id, NUM_HARTS, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + barrier(); + + // Combine partials: redundant on both harts (cheap, NUM_HARTS=2 + // iterations), instead of hart-0-only + barrier. + { + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int h = 1; h < NUM_HARTS; h++){ + if (g_partial_max[h] > vmax) vmax = g_partial_max[h]; + if (g_partial_min[h] < vmin) vmin = g_partial_min[h]; + } + *out_max = vmax; *out_min = vmin; + } + barrier(); + } + // quantize is OUTSIDE the tight barrier loop, done by hart 0 only - + // same structure as 1.1_smp's final printf block after its last barrier. + if ((unsigned int)hart_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main (same SMP pattern as obpmark_1_1_smp_32.c: hart 0 does setup and +// calls smp_resume() to launch hart 1, which restarts at main() from the +// top and must skip the init block via the hid check) +// ============================================================ +static radar_params_t g_params; + +int main(void) +{ + unsigned int hid = (unsigned int)hart_id(); + + if (hid == 0){ + car_init_start(); + printf("OBPMark #1.2 - Radar SAR (SMP v4, symmetric barriers)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | Harts: %d\r\n", + (unsigned)g_params.rvalid, (unsigned)g_params.apatch, + (unsigned)g_params.avalid, (unsigned)g_params.npatch, NUM_HARTS); + + gen_rand_radar_data(); + smp_resume(); + } + + barrier(); + + uint64_t t_start = (hid == 0) ? read_mcycle() : 0; + + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_smp(&g_params, &vmax, &vmin); + + barrier(); + + if (hid == 0){ + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + + // Last line written by hand via uart_write_str+flush, not printf: + // the testbench triggers $finish as soon as uart_reading_byte==0, + // which can happen BETWEEN two consecutive printf calls before the + // final line is fully transmitted (same issue and same fix as + // obpmark_1_1_smp_32.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main(): returning triggers _exit(), + // which fires $finish before (or while) hart 0 is still writing its + // results to the UART. Same fix as obpmark_1_1_smp_32.c. + // + // Disable global interrupts (mstatus.MIE=0) before parking in wfi: + // without this, hart1 can be woken by spurious timer/software + // interrupts, re-check the while(1) condition, and re-enter wfi - + // each wake/sleep cycle costs real simulated time. With a much + // longer pipeline than obpmark_1_1_smp_32.c (many more barriers), + // this overhead was large enough to delay $finish substantially. + // With MIE cleared, hart1 stays parked in wfi for good. + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v5.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v5.c new file mode 100644 index 000000000..82d14ab79 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v5.c @@ -0,0 +1,892 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define NUM_HARTS 2 + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// per-hart partial max/min for the SMP multilook reduction +static float g_partial_max[NUM_HARTS]; +static float g_partial_min[NUM_HARTS]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across harts gives a bit-identical +// result to the sequential version regardless of hart count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SMP variant: split the outer (nrows) loop across harts. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different harts get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SMP: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SMP: shift/remap part. NOT parallelized on purpose: it reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i. +// Splitting this by row across harts would be a genuine data race (one hart +// could read a row the other has already overwritten with a different +// value), which would make the SMP result depend on scheduling - i.e. not +// deterministic. Since this loop is cheap (rvalid*apatch = 512 iterations +// here) compared to the four FFT-heavy stages, it is kept sequential on +// hart 0. This keeps the SMP port's result bit-identical to the sequential +// full port (same execution order for this step), at a small cost in +// parallel coverage. +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SMP variant: split the outer (nfx) loop across harts. Each hart writes a +// disjoint set of output blocks (different i -> different image rows) and +// accumulates its own local max/min; hart 0 combines the two partials after +// the barrier. Bit-identical per-pixel values to the sequential version. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx_total = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// +// IMPORTANT: hart_id() is re-read at every single use point below (never +// cached in a parameter or a local that survives across a call/barrier), +// matching obpmark_1_1_smp_32.c's documented workaround: "Re-read hart_id() +// inside pipeline functions to avoid LTO register corruption". Caching it +// once in main() and passing it down as a parameter (as an earlier version +// of this file did) let LTO's cross-hart register allocation assumptions +// silently corrupt the value, producing wrong per-hart row ranges and a +// degenerate (uniform) result while still completing "successfully" - +// exactly the bug this version fixes. +// ============================================================ +static void SAR_focus_smp(radar_params_t *params, float *out_max, float *out_min){ + // Setup runs on hart 0 only, OUTSIDE the tight barrier loop below - + // same structure as obpmark_1_1_smp_32.c's init (init_frames/gen_rand_data + // run on hart 0 only, before the barrier-heavy per-frame loop begins). + if ((unsigned int)hart_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + barrier(); + + // From here on, EVERY hart executes the exact same sequence of calls in + // the exact same order, with no if(hart_id()==...) branches between + // barriers - matching obpmark_1_1_smp_32.c's pipeline functions, where + // both harts always call every f_*_range() together. Operations that + // are logically single-owner (RCMC shift, multilook max/min combine) + // are done REDUNDANTLY by both harts on the same read-only inputs + // instead of conditionally by one hart while the other waits - cheap + // enough here, and removes the asymmetric-branch structure that had + // no precedent in the working 1.1 SMP port. + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)hart_id(), NUM_HARTS, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + barrier(); + + // --- RCMC: FFT part split by AZIM_H rows --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + barrier(); + + // RCMC shift/remap: redundant on both harts (same deterministic + // read-only-then-write-own-copy semantics as the sequential + // version - see SAR_rcmc_shift's own comment on why it must not + // be row-split across harts). Both harts computing the identical + // result redundantly is wasteful but safe and removes the + // asymmetric branch. + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + barrier(); + + // --- azimuth compression: split AZIM_H rows --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)hart_id(), NUM_HARTS, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + barrier(); + + // --- multilook: split nfx blocks --- + float sx = (float)params->asize / (float)OUT_W; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + unsigned int ms, me; + unsigned int my_id = (unsigned int)hart_id(); + split_range(nfx, my_id, NUM_HARTS, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + barrier(); + + // Combine partials: redundant on both harts (cheap, NUM_HARTS=2 + // iterations), instead of hart-0-only + barrier. + { + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int h = 1; h < NUM_HARTS; h++){ + if (g_partial_max[h] > vmax) vmax = g_partial_max[h]; + if (g_partial_min[h] < vmin) vmin = g_partial_min[h]; + } + *out_max = vmax; *out_min = vmin; + } + barrier(); + } + // quantize is OUTSIDE the tight barrier loop, done by hart 0 only - + // same structure as 1.1_smp's final printf block after its last barrier. + if ((unsigned int)hart_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main (same SMP pattern as obpmark_1_1_smp_32.c: hart 0 does setup and +// calls smp_resume() to launch hart 1, which restarts at main() from the +// top and must skip the init block via the hid check) +// ============================================================ +static radar_params_t g_params; + +int main(void) +{ + unsigned int hid = (unsigned int)hart_id(); + + // Hart 0 does the absolute minimum before releasing hart 1: init the + // platform, then smp_resume() immediately. Everything else (FPU math + // for the derived parameters, printf, synthetic data generation) is + // moved AFTER the first barrier. + // + // Rationale: in obpmark_1_1_smp_32.c (which works) nothing touches the + // FPU before smp_resume(); in the previous version of this file the + // derived-parameter block called m_roundf/m_log2f/m_ceilf - i.e. it + // enabled and used the FPU, changing mstatus.FS - before releasing + // hart 1. Since the wake-up path itself manipulates mstatus (MIE/MSIE, + // see set_msie/set_mie in util.h and smp_pause in smp.c), doing FPU + // work first is the one structural difference we had not eliminated. + // Tracing showed hart 1 never leaving the bootrom (only 0x020000xx + // addresses, then a j pc-0 spin) while in 1.1_smp it reaches program + // addresses (0x80001dxx), so the wake-up is what fails, not our + // pipeline. This version tests that hypothesis directly. + if (hid == 0){ + car_init_start(); + smp_resume(); + } + + barrier(); + + // From here both harts are running. Hart 0 now does the setup work + // that used to happen before the resume; hart 1 waits at the next + // barrier until the parameters and input data are ready. + if ((unsigned int)hart_id() == 0){ + printf("OBPMark #1.2 - Radar SAR (SMP v5, minimal pre-resume)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | Harts: %d\r\n", + (unsigned)g_params.rvalid, (unsigned)g_params.avalid, + (unsigned)g_params.apatch, (unsigned)g_params.npatch, NUM_HARTS); + + gen_rand_radar_data(); + } + + barrier(); // parameters and input data are now visible to both harts + + uint64_t t_start = (hid == 0) ? read_mcycle() : 0; + + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_smp(&g_params, &vmax, &vmin); + + barrier(); + + if (hid == 0){ + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + + // Last line written by hand via uart_write_str+flush, not printf: + // the testbench triggers $finish as soon as uart_reading_byte==0, + // which can happen BETWEEN two consecutive printf calls before the + // final line is fully transmitted (same issue and same fix as + // obpmark_1_1_smp_32.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main(): returning triggers _exit(), + // which fires $finish before (or while) hart 0 is still writing its + // results to the UART. Same fix as obpmark_1_1_smp_32.c. + // + // Disable global interrupts (mstatus.MIE=0) before parking in wfi: + // without this, hart1 can be woken by spurious timer/software + // interrupts, re-check the while(1) condition, and re-enter wfi - + // each wake/sleep cycle costs real simulated time. With a much + // longer pipeline than obpmark_1_1_smp_32.c (many more barriers), + // this overhead was large enough to delay $finish substantially. + // With MIE cleared, hart1 stays parked in wfi for good. + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v6.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v6.c new file mode 100644 index 000000000..3f5224262 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v6.c @@ -0,0 +1,921 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define NUM_HARTS 2 + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// per-hart partial max/min for the SMP multilook reduction +static float g_partial_max[NUM_HARTS]; +static float g_partial_min[NUM_HARTS]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across harts gives a bit-identical +// result to the sequential version regardless of hart count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SMP variant: split the outer (nrows) loop across harts. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different harts get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SMP: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SMP: shift/remap part. NOT parallelized on purpose: it reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i. +// Splitting this by row across harts would be a genuine data race (one hart +// could read a row the other has already overwritten with a different +// value), which would make the SMP result depend on scheduling - i.e. not +// deterministic. Since this loop is cheap (rvalid*apatch = 512 iterations +// here) compared to the four FFT-heavy stages, it is kept sequential on +// hart 0. This keeps the SMP port's result bit-identical to the sequential +// full port (same execution order for this step), at a small cost in +// parallel coverage. +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SMP variant: split the outer (nfx) loop across harts. Each hart writes a +// disjoint set of output blocks (different i -> different image rows) and +// accumulates its own local max/min; hart 0 combines the two partials after +// the barrier. Bit-identical per-pixel values to the sequential version. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx_total = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// +// IMPORTANT: hart_id() is re-read at every single use point below (never +// cached in a parameter or a local that survives across a call/barrier), +// matching obpmark_1_1_smp_32.c's documented workaround: "Re-read hart_id() +// inside pipeline functions to avoid LTO register corruption". Caching it +// once in main() and passing it down as a parameter (as an earlier version +// of this file did) let LTO's cross-hart register allocation assumptions +// silently corrupt the value, producing wrong per-hart row ranges and a +// degenerate (uniform) result while still completing "successfully" - +// exactly the bug this version fixes. +// ============================================================ +static void SAR_focus_smp(radar_params_t *params, float *out_max, float *out_min){ + // Setup runs on hart 0 only, OUTSIDE the tight barrier loop below - + // same structure as obpmark_1_1_smp_32.c's init (init_frames/gen_rand_data + // run on hart 0 only, before the barrier-heavy per-frame loop begins). + if ((unsigned int)hart_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + barrier(); + + // From here on, EVERY hart executes the exact same sequence of calls in + // the exact same order, with no if(hart_id()==...) branches between + // barriers - matching obpmark_1_1_smp_32.c's pipeline functions, where + // both harts always call every f_*_range() together. Operations that + // are logically single-owner (RCMC shift, multilook max/min combine) + // are done REDUNDANTLY by both harts on the same read-only inputs + // instead of conditionally by one hart while the other waits - cheap + // enough here, and removes the asymmetric-branch structure that had + // no precedent in the working 1.1 SMP port. + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)hart_id(), NUM_HARTS, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + barrier(); + + // --- RCMC: FFT part split by AZIM_H rows --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + barrier(); + + // RCMC shift/remap: redundant on both harts (same deterministic + // read-only-then-write-own-copy semantics as the sequential + // version - see SAR_rcmc_shift's own comment on why it must not + // be row-split across harts). Both harts computing the identical + // result redundantly is wasteful but safe and removes the + // asymmetric branch. + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + barrier(); + + // --- azimuth compression: split AZIM_H rows --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)hart_id(), NUM_HARTS, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + barrier(); + + // --- multilook: split nfx blocks --- + float sx = (float)params->asize / (float)OUT_W; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + unsigned int ms, me; + unsigned int my_id = (unsigned int)hart_id(); + split_range(nfx, my_id, NUM_HARTS, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + barrier(); + + // Combine partials: redundant on both harts (cheap, NUM_HARTS=2 + // iterations), instead of hart-0-only + barrier. + { + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int h = 1; h < NUM_HARTS; h++){ + if (g_partial_max[h] > vmax) vmax = g_partial_max[h]; + if (g_partial_min[h] < vmin) vmin = g_partial_min[h]; + } + *out_max = vmax; *out_min = vmin; + } + barrier(); + } + // quantize is OUTSIDE the tight barrier loop, done by hart 0 only - + // same structure as 1.1_smp's final printf block after its last barrier. + if ((unsigned int)hart_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main (same SMP pattern as obpmark_1_1_smp_32.c: hart 0 does setup and +// calls smp_resume() to launch hart 1, which restarts at main() from the +// top and must skip the init block via the hid check) +// ============================================================ +static radar_params_t g_params; + +// Enable the FPU on the calling hart: set mstatus.FS to Initial (01). +// mstatus.FS occupies bits [14:13]; writing 0x2000 sets FS=01 (Initial), +// which is enough to make FP instructions legal. Hart 0 gets this via +// car_init_start(), but hart 1 is resumed straight into our code and may +// still have FS=Off (00), in which case ANY float instruction traps. Both +// harts call this before touching floating point. +static inline void enable_fpu(void){ + asm volatile ("csrs mstatus, %0" :: "r"(0x2000UL) : "memory"); +} + + +// ============================================================ +// All floating-point work is isolated in these two noinline functions. +// +// WHY: main()'s prologue saves whatever callee-saved registers the function +// body uses. If main uses ANY FP register, the compiler emits fsd fs0/fs1/... +// at the very top of main - executed by BOTH harts before anything else, +// including before mhartid is even read. Disassembly of the previous version +// showed exactly that: +// fsd fs0,168(sp) +// fsd fs1,160(sp) +// fsd fs2,152(sp) +// csrr s1,mhartid <- only now is the hart id read +// while obpmark_1_1_smp_32.c (which works) has an all-integer prologue. +// +// An FP instruction executed while mstatus.FS is Off raises an illegal +// instruction exception. Hart 1, resumed into main without having gone +// through car_init_start(), can have FS off - so those fsd's trap it +// immediately, which matches the trace: hart 1 never reaches program +// addresses and ends up spinning in a j pc-0 trap loop. +// +// Keeping every float out of main (and out of its inlining scope, hence +// noinline) makes main's prologue integer-only, so hart 1 can enter it. +// ============================================================ +static void __attribute__((noinline)) hart0_setup(void) +{ + enable_fpu(); + printf("OBPMark #1.2 - Radar SAR (SMP v6, FP isolated from main)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | Harts: %d\r\n", + (unsigned)g_params.rvalid, (unsigned)g_params.avalid, + (unsigned)g_params.apatch, (unsigned)g_params.npatch, NUM_HARTS); + + gen_rand_radar_data(); +} + +// Wrapper so the float locals vmax/vmin (and everything SAR_focus_smp needs) +// live here, not in main. Both harts call this, but only after both are +// already running and past a barrier. +static void __attribute__((noinline)) run_focus(void) +{ + enable_fpu(); + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_smp(&g_params, &vmax, &vmin); +} + +int main(void) +{ + unsigned int hid = (unsigned int)hart_id(); + + if (hid == 0){ + car_init_start(); + smp_resume(); + } + + barrier(); + + if ((unsigned int)hart_id() == 0) + hart0_setup(); + + barrier(); // parameters and input data are now visible to both harts + + uint64_t t_start = (hid == 0) ? read_mcycle() : 0; + + run_focus(); + + barrier(); + + if (hid == 0){ + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + + // Last line written by hand via uart_write_str+flush, not printf: + // the testbench triggers $finish as soon as uart_reading_byte==0, + // which can happen BETWEEN two consecutive printf calls before the + // final line is fully transmitted (same issue and same fix as + // obpmark_1_1_smp_32.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main(): returning triggers _exit(), + // which fires $finish before (or while) hart 0 is still writing its + // results to the UART. Same fix as obpmark_1_1_smp_32.c. + // + // Disable global interrupts (mstatus.MIE=0) before parking in wfi: + // without this, hart1 can be woken by spurious timer/software + // interrupts, re-check the while(1) condition, and re-enter wfi - + // each wake/sleep cycle costs real simulated time. With a much + // longer pipeline than obpmark_1_1_smp_32.c (many more barriers), + // this overhead was large enough to delay $finish substantially. + // With MIE cleared, hart1 stays parked in wfi for good. + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v7.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v7.c new file mode 100644 index 000000000..4bf919ccd --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v7.c @@ -0,0 +1,930 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define NUM_HARTS 2 + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// per-hart partial max/min for the SMP multilook reduction +static float g_partial_max[NUM_HARTS]; +static float g_partial_min[NUM_HARTS]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across harts gives a bit-identical +// result to the sequential version regardless of hart count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SMP variant: split the outer (nrows) loop across harts. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different harts get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SMP: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SMP: shift/remap part. NOT parallelized on purpose: it reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i. +// Splitting this by row across harts would be a genuine data race (one hart +// could read a row the other has already overwritten with a different +// value), which would make the SMP result depend on scheduling - i.e. not +// deterministic. Since this loop is cheap (rvalid*apatch = 512 iterations +// here) compared to the four FFT-heavy stages, it is kept sequential on +// hart 0. This keeps the SMP port's result bit-identical to the sequential +// full port (same execution order for this step), at a small cost in +// parallel coverage. +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SMP variant: split the outer (nfx) loop across harts. Each hart writes a +// disjoint set of output blocks (different i -> different image rows) and +// accumulates its own local max/min; hart 0 combines the two partials after +// the barrier. Bit-identical per-pixel values to the sequential version. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx_total = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// +// IMPORTANT: hart_id() is re-read at every single use point below (never +// cached in a parameter or a local that survives across a call/barrier), +// matching obpmark_1_1_smp_32.c's documented workaround: "Re-read hart_id() +// inside pipeline functions to avoid LTO register corruption". Caching it +// once in main() and passing it down as a parameter (as an earlier version +// of this file did) let LTO's cross-hart register allocation assumptions +// silently corrupt the value, producing wrong per-hart row ranges and a +// degenerate (uniform) result while still completing "successfully" - +// exactly the bug this version fixes. +// ============================================================ +static void SAR_focus_smp(radar_params_t *params, float *out_max, float *out_min){ + // Setup runs on hart 0 only, OUTSIDE the tight barrier loop below - + // same structure as obpmark_1_1_smp_32.c's init (init_frames/gen_rand_data + // run on hart 0 only, before the barrier-heavy per-frame loop begins). + if ((unsigned int)hart_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + barrier(); + + // From here on, EVERY hart executes the exact same sequence of calls in + // the exact same order, with no if(hart_id()==...) branches between + // barriers - matching obpmark_1_1_smp_32.c's pipeline functions, where + // both harts always call every f_*_range() together. Operations that + // are logically single-owner (RCMC shift, multilook max/min combine) + // are done REDUNDANTLY by both harts on the same read-only inputs + // instead of conditionally by one hart while the other waits - cheap + // enough here, and removes the asymmetric-branch structure that had + // no precedent in the working 1.1 SMP port. + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)hart_id(), NUM_HARTS, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + barrier(); + + // --- RCMC: FFT part split by AZIM_H rows --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + barrier(); + + // RCMC shift/remap: redundant on both harts (same deterministic + // read-only-then-write-own-copy semantics as the sequential + // version - see SAR_rcmc_shift's own comment on why it must not + // be row-split across harts). Both harts computing the identical + // result redundantly is wasteful but safe and removes the + // asymmetric branch. + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + barrier(); + + // --- azimuth compression: split AZIM_H rows --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)hart_id(), NUM_HARTS, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + barrier(); + + // --- multilook: split nfx blocks --- + float sx = (float)params->asize / (float)OUT_W; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + unsigned int ms, me; + unsigned int my_id = (unsigned int)hart_id(); + split_range(nfx, my_id, NUM_HARTS, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + barrier(); + + // Combine partials: redundant on both harts (cheap, NUM_HARTS=2 + // iterations), instead of hart-0-only + barrier. + { + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int h = 1; h < NUM_HARTS; h++){ + if (g_partial_max[h] > vmax) vmax = g_partial_max[h]; + if (g_partial_min[h] < vmin) vmin = g_partial_min[h]; + } + *out_max = vmax; *out_min = vmin; + } + barrier(); + } + // quantize is OUTSIDE the tight barrier loop, done by hart 0 only - + // same structure as 1.1_smp's final printf block after its last barrier. + if ((unsigned int)hart_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main (same SMP pattern as obpmark_1_1_smp_32.c: hart 0 does setup and +// calls smp_resume() to launch hart 1, which restarts at main() from the +// top and must skip the init block via the hid check) +// ============================================================ +static radar_params_t g_params; + +// Enable the FPU on the calling hart: set mstatus.FS to Initial (01). +// mstatus.FS occupies bits [14:13]; writing 0x2000 sets FS=01 (Initial), +// which is enough to make FP instructions legal. Hart 0 gets this via +// car_init_start(), but hart 1 is resumed straight into our code and may +// still have FS=Off (00), in which case ANY float instruction traps. Both +// harts call this before touching floating point. +static inline void enable_fpu(void){ + asm volatile ("csrs mstatus, %0" :: "r"(0x2000UL) : "memory"); +} + + +// ============================================================ +// All floating-point work is isolated in these two noinline functions. +// +// WHY: main()'s prologue saves whatever callee-saved registers the function +// body uses. If main uses ANY FP register, the compiler emits fsd fs0/fs1/... +// at the very top of main - executed by BOTH harts before anything else, +// including before mhartid is even read. Disassembly of the previous version +// showed exactly that: +// fsd fs0,168(sp) +// fsd fs1,160(sp) +// fsd fs2,152(sp) +// csrr s1,mhartid <- only now is the hart id read +// while obpmark_1_1_smp_32.c (which works) has an all-integer prologue. +// +// An FP instruction executed while mstatus.FS is Off raises an illegal +// instruction exception. Hart 1, resumed into main without having gone +// through car_init_start(), can have FS off - so those fsd's trap it +// immediately, which matches the trace: hart 1 never reaches program +// addresses and ends up spinning in a j pc-0 trap loop. +// +// Keeping every float out of main (and out of its inlining scope, hence +// noinline) makes main's prologue integer-only, so hart 1 can enter it. +// ============================================================ +static void __attribute__((noinline)) hart0_setup(void) +{ + enable_fpu(); + printf("OBPMark #1.2 - Radar SAR (SMP v7, fresh hart_id everywhere)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | Harts: %d\r\n", + (unsigned)g_params.rvalid, (unsigned)g_params.avalid, + (unsigned)g_params.apatch, (unsigned)g_params.npatch, NUM_HARTS); + + gen_rand_radar_data(); +} + +// Wrapper so the float locals vmax/vmin (and everything SAR_focus_smp needs) +// live here, not in main. Both harts call this, but only after both are +// already running and past a barrier. +static void __attribute__((noinline)) run_focus(void) +{ + enable_fpu(); + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_smp(&g_params, &vmax, &vmin); +} + +int main(void) +{ + // No cached hid here: every check below reads hart_id() fresh, on the + // spot. The previous version cached it once at the top in a local + // ("unsigned int hid = hart_id();") and reused that value across the + // whole function, including after run_focus() - a call/return boundary + // and several barriers away from where it was read. The v6 run showed + // both harts executing the final "if (hid==0)" print block (interleaved, + // garbled UART output, then a testbench crash from two concurrent + // writers), meaning hart 1 evaluated that stale cached hid as 0. Same + // root cause class as the SAR_focus_smp fix: don't let a hart-identity + // value survive across calls/barriers under LTO. + + if ((unsigned int)hart_id() == 0){ + car_init_start(); + smp_resume(); + } + + barrier(); + + if ((unsigned int)hart_id() == 0) + hart0_setup(); + + barrier(); // parameters and input data are now visible to both harts + + uint64_t t_start = ((unsigned int)hart_id() == 0) ? read_mcycle() : 0; + + run_focus(); + + barrier(); + + if ((unsigned int)hart_id() == 0){ + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + + // Last line written by hand via uart_write_str+flush, not printf: + // the testbench triggers $finish as soon as uart_reading_byte==0, + // which can happen BETWEEN two consecutive printf calls before the + // final line is fully transmitted (same issue and same fix as + // obpmark_1_1_smp_32.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main(): returning triggers _exit(), + // which fires $finish before (or while) hart 0 is still writing its + // results to the UART. Same fix as obpmark_1_1_smp_32.c. + // + // Disable global interrupts (mstatus.MIE=0) before parking in wfi: + // without this, hart1 can be woken by spurious timer/software + // interrupts, re-check the while(1) condition, and re-enter wfi - + // each wake/sleep cycle costs real simulated time. With a much + // longer pipeline than obpmark_1_1_smp_32.c (many more barriers), + // this overhead was large enough to delay $finish substantially. + // With MIE cleared, hart1 stays parked in wfi for good. + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v8.c b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v8.c new file mode 100644 index 000000000..738a47d05 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_1_2_host_smp_v8.c @@ -0,0 +1,938 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL host port for Astral (1x CVA6, RV64) - FULL pipeline +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository: +// - device.h (sequential): radar_params_t, radar_data_t +// - device.c (sequential): device_memory_init buffer sizing formulas +// - processing.c: SAR_focus, SAR_range_ref, SAR_DCE, SAR_rcmc_table, +// SAR_azimuth_ref, SAR_range_compression, SAR_rcmc, +// SAR_azimuth_compression, SAR_multilook, quantize, ref_func, +// reference_multiplication, complex_transpose, fft/ifft (GPU4S kernel) +// +// Differences vs the reference (all forced by the bare-metal target, none +// change the computation performed): +// - std::complex -> explicit {re,im} cplx_t (no C++ STL) +// - malloc/free -> static buffers, sized at compile time from the SAME +// formulas as device_memory_init (verified by hand, see the size +// derivation comment block below) +// - radar_params_t generated with FIXED physical values instead of +// gen_params()'s rand()-based ones (bare-metal has no reliable libc +// rand(); fixed values keep every derived formula - rvalid, avalid, +// apatch, npatch - identical in kind to what gen_params would produce) +// - synthetic complex input generated with the project's LCG (seed +// 21121993, as in 1.1/1.2-reduced), same amplitude range as the +// reference gen_data() ([-15.5, 15.5]), with the same "rows beyond +// asize are zero" padding that copy_memory_to_device implicitly +// applies via the datagen's line-copy bound check +// - sinf/cosf/sqrtf/log2f/atan2f/floorf/ceilf/roundf implemented +// in-file (Step 1, validated against real libm to < 1e-4 error) +// - dimensions chosen small (see block below) so every derived formula +// from the reference still holds, but the whole pipeline runs fast in +// RTL simulation +// +// Size derivation (worked by hand, matches device_memory_init exactly): +// lambda=0.03 PRF=1000 tau=1e-5 fs=5e7 vr=7000 ro=8e5 slope=5e11 +// rsize=16 (range samples), asize=16 (azimuth lines) +// rvalid = rsize - round(tau*fs) = 16-500 -> clamps to rsize = 16 +// avalid = asize (asize<1024) = 16 +// apatch = 2^ceil(log2(avalid+1)) = 2^ceil(log2(17)) = 32 +// npatch = asize/avalid = 1 +// next_power_of2(rsize) = 16 (already a power of two) +// range_data: h=apatch=32, w=next_pow2(rsize)*2=32 floats (1024 total) +// azimuth_data: h=rvalid=16, w=apatch*2=64 floats (1024 total) +// rrf: next_pow2(rsize)*2 = 32 floats (16 complex) +// arf: apatch*2 = 64 floats (32 complex) +// aux: rsize*2 = 32 floats (16 complex) +// offsets: rvalid*apatch = 512 uint32 +// output: out_w=4, out_h=nfx=4 -> 16 pixels (see SAR_multilook block) +// Every FFT length used (16 or 32) is a power of two: valid for the +// iterative Cooley-Tukey kernel. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +#define NUM_HARTS 2 + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c). sqrtf uses the +// hardware fsqrt.s instruction (exact); the rest are the accuracy-checked +// polynomial/range-reduction implementations. +// ============================================================ +#define PI_F 3.14159265358979323846f + +static inline float m_sqrtf(float x){ + float r; + asm volatile ("fsqrt.s %0, %1" : "=f"(r) : "f"(x)); + return r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = (m - 1.0f) / (m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? ay/ax : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? ax/ay : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -(2.0f * PI_F / (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] /= (float)nn; data[2*i+1] /= (float)nn; } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap) +// ============================================================ +static float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static float g_rrf[RRF_LEN]; +static float g_arf[ARF_LEN]; +static float g_aux[AUX_LEN]; +static uint32_t g_offsets[OFFS_LEN]; +static float g_ml_data[OUT_W * OUT_H]; +static uint8_t g_output_image[OUT_W * OUT_H]; + +// per-hart partial max/min for the SMP multilook reduction +static float g_partial_max[NUM_HARTS]; +static float g_partial_min[NUM_HARTS]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * ((float)bm_rand() / 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * (1.0f/fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = mean / (float)width; + return mean * params->PRF / (2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * (params->PRF/(float)params->avalid) + fDc; + float ratio = params->lambda * delta / (2.0f*params->vr); + float offset = (1.0f/m_sqrtf(1.0f - ratio*ratio) - 1.0f) + * (params->ro + (float)i * (c_light/(2.0f*params->fs))); + offset = m_roundf(offset / (c_light/(2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*(c_light/(2.0f*params->fs)); + float ratio = params->lambda*fDc/(2.0f*params->vr); + float rdc = rnge / m_sqrtf(1.0f - ratio*ratio); + float tauz = (rdc*(params->lambda/10.0f) * 0.8f) / params->vr; + float chirp = -(2.0f*params->vr*params->vr)/params->lambda/rdc; + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across harts gives a bit-identical +// result to the sequential version regardless of hart count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SMP variant: split the outer (nrows) loop across harts. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different harts get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SMP: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SMP: shift/remap part. NOT parallelized on purpose: it reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i. +// Splitting this by row across harts would be a genuine data race (one hart +// could read a row the other has already overwritten with a different +// value), which would make the SMP result depend on scheduling - i.e. not +// deterministic. Since this loop is cheap (rvalid*apatch = 512 iterations +// here) compared to the four FFT-heavy stages, it is kept sequential on +// hart 0. This keeps the SMP port's result bit-identical to the sequential +// full port (same execution order for this step), at a small cost in +// parallel coverage. +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SMP variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SMP variant: split the outer (nfx) loop across harts. Each hart writes a +// disjoint set of output blocks (different i -> different image rows) and +// accumulates its own local max/min; hart 0 combines the two partials after +// the barrier. Bit-identical per-pixel values to the sequential version. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = (float)params->asize / (float)image_w; + float sy = (float)params->rvalid / (float)image_w; + uint32_t nfx_total = (uint32_t)m_floorf((float)params->avalid / sx); + uint32_t nfy = (uint32_t)m_floorf((float)params->rvalid / sy); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = fimg / (float)(isx*isy); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = 256.0f / (vmax - vmin + 1e-30f); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// ============================================================ +// SAR_focus_smp: same stage order as the sequential SAR_focus, but the +// four FFT-heavy stages and the multilook are split by row/block range +// across the two harts, with a barrier between stages (each stage reads +// the full output of the previous one). Setup (range_ref, DCE, rcmc_table, +// azimuth_ref) and the RCMC shift/remap run on hart 0 only - see the +// comment on SAR_rcmc_shift for why that one loop is intentionally kept +// sequential. +// +// IMPORTANT: hart_id() is re-read at every single use point below (never +// cached in a parameter or a local that survives across a call/barrier), +// matching obpmark_1_1_smp_32.c's documented workaround: "Re-read hart_id() +// inside pipeline functions to avoid LTO register corruption". Caching it +// once in main() and passing it down as a parameter (as an earlier version +// of this file did) let LTO's cross-hart register allocation assumptions +// silently corrupt the value, producing wrong per-hart row ranges and a +// degenerate (uniform) result while still completing "successfully" - +// exactly the bug this version fixes. +// ============================================================ +static void SAR_focus_smp(radar_params_t *params, float *out_max, float *out_min){ + // Setup runs on hart 0 only, OUTSIDE the tight barrier loop below - + // same structure as obpmark_1_1_smp_32.c's init (init_frames/gen_rand_data + // run on hart 0 only, before the barrier-heavy per-frame loop begins). + if ((unsigned int)hart_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + barrier(); + + // From here on, EVERY hart executes the exact same sequence of calls in + // the exact same order, with no if(hart_id()==...) branches between + // barriers - matching obpmark_1_1_smp_32.c's pipeline functions, where + // both harts always call every f_*_range() together. Operations that + // are logically single-owner (RCMC shift, multilook max/min combine) + // are done REDUNDANTLY by both harts on the same read-only inputs + // instead of conditionally by one hart while the other waits - cheap + // enough here, and removes the asymmetric-branch structure that had + // no precedent in the working 1.1 SMP port. + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)hart_id(), NUM_HARTS, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + barrier(); + + // --- RCMC: FFT part split by AZIM_H rows --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + barrier(); + + // RCMC shift/remap: executed by hart 0 ALONE, exactly once. + // + // This loop reads data[j + offsets[ind]*width] - an arbitrary row of + // the same buffer it is writing - so it is NOT idempotent: running it + // twice over the buffer does not reproduce the single-pass result, + // because the second pass reads values the first pass already + // overwrote. A previous version ran it redundantly on both harts to + // remove the asymmetric branch; with identical input data + // (range_data_chk verified equal in both builds) that alone made the + // SMP result diverge from the sequential one (Output[0][0] 19 vs 89, + // checksum 1768 vs 2540). The asymmetry here is therefore mandatory, + // not a stylistic choice: the barriers around it keep both harts in + // lockstep, and the loop is cheap (rvalid*apatch iterations). + if ((unsigned int)hart_id() == 0) + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + barrier(); + + // --- azimuth compression: split AZIM_H rows --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)hart_id(), NUM_HARTS, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)hart_id(), NUM_HARTS, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + barrier(); + + // --- multilook: split nfx blocks --- + float sx = (float)params->asize / (float)OUT_W; + uint32_t nfx = (uint32_t)m_floorf((float)params->avalid / sx); + unsigned int ms, me; + unsigned int my_id = (unsigned int)hart_id(); + split_range(nfx, my_id, NUM_HARTS, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + barrier(); + + // Combine partials: redundant on both harts (cheap, NUM_HARTS=2 + // iterations), instead of hart-0-only + barrier. + { + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int h = 1; h < NUM_HARTS; h++){ + if (g_partial_max[h] > vmax) vmax = g_partial_max[h]; + if (g_partial_min[h] < vmin) vmin = g_partial_min[h]; + } + *out_max = vmax; *out_min = vmin; + } + barrier(); + } + // quantize is OUTSIDE the tight barrier loop, done by hart 0 only - + // same structure as 1.1_smp's final printf block after its last barrier. + if ((unsigned int)hart_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void){ + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +#define CVA6_CLK_NS 20ULL + +// ============================================================ +// Main (same SMP pattern as obpmark_1_1_smp_32.c: hart 0 does setup and +// calls smp_resume() to launch hart 1, which restarts at main() from the +// top and must skip the init block via the hid check) +// ============================================================ +static radar_params_t g_params; + +// Enable the FPU on the calling hart: set mstatus.FS to Initial (01). +// mstatus.FS occupies bits [14:13]; writing 0x2000 sets FS=01 (Initial), +// which is enough to make FP instructions legal. Hart 0 gets this via +// car_init_start(), but hart 1 is resumed straight into our code and may +// still have FS=Off (00), in which case ANY float instruction traps. Both +// harts call this before touching floating point. +static inline void enable_fpu(void){ + asm volatile ("csrs mstatus, %0" :: "r"(0x2000UL) : "memory"); +} + + +// ============================================================ +// All floating-point work is isolated in these two noinline functions. +// +// WHY: main()'s prologue saves whatever callee-saved registers the function +// body uses. If main uses ANY FP register, the compiler emits fsd fs0/fs1/... +// at the very top of main - executed by BOTH harts before anything else, +// including before mhartid is even read. Disassembly of the previous version +// showed exactly that: +// fsd fs0,168(sp) +// fsd fs1,160(sp) +// fsd fs2,152(sp) +// csrr s1,mhartid <- only now is the hart id read +// while obpmark_1_1_smp_32.c (which works) has an all-integer prologue. +// +// An FP instruction executed while mstatus.FS is Off raises an illegal +// instruction exception. Hart 1, resumed into main without having gone +// through car_init_start(), can have FS off - so those fsd's trap it +// immediately, which matches the trace: hart 1 never reaches program +// addresses and ends up spinning in a j pc-0 trap loop. +// +// Keeping every float out of main (and out of its inlining scope, hence +// noinline) makes main's prologue integer-only, so hart 1 can enter it. +// ============================================================ +static void __attribute__((noinline)) hart0_setup(void) +{ + enable_fpu(); + printf("OBPMark #1.2 - Radar SAR (SMP v8, rcmc shift on hart0 only)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | Harts: %d\r\n", + (unsigned)g_params.rvalid, (unsigned)g_params.avalid, + (unsigned)g_params.apatch, (unsigned)g_params.npatch, NUM_HARTS); + + gen_rand_radar_data(); +} + +// Wrapper so the float locals vmax/vmin (and everything SAR_focus_smp needs) +// live here, not in main. Both harts call this, but only after both are +// already running and past a barrier. +static void __attribute__((noinline)) run_focus(void) +{ + enable_fpu(); + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_smp(&g_params, &vmax, &vmin); +} + +int main(void) +{ + // No cached hid here: every check below reads hart_id() fresh, on the + // spot. The previous version cached it once at the top in a local + // ("unsigned int hid = hart_id();") and reused that value across the + // whole function, including after run_focus() - a call/return boundary + // and several barriers away from where it was read. The v6 run showed + // both harts executing the final "if (hid==0)" print block (interleaved, + // garbled UART output, then a testbench crash from two concurrent + // writers), meaning hart 1 evaluated that stale cached hid as 0. Same + // root cause class as the SAR_focus_smp fix: don't let a hart-identity + // value survive across calls/barriers under LTO. + + if ((unsigned int)hart_id() == 0){ + car_init_start(); + smp_resume(); + } + + barrier(); + + if ((unsigned int)hart_id() == 0) + hart0_setup(); + + barrier(); // parameters and input data are now visible to both harts + + uint64_t t_start = ((unsigned int)hart_id() == 0) ? read_mcycle() : 0; + + run_focus(); + + barrier(); + + if ((unsigned int)hart_id() == 0){ + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_NS) / 1000ULL; + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("Done.\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu us\r\n", (unsigned long long)time_us); + printf(" Output[0][0] : %u\r\n", (unsigned)g_output_image[0]); + + // Last line written by hand via uart_write_str+flush, not printf: + // the testbench triggers $finish as soon as uart_reading_byte==0, + // which can happen BETWEEN two consecutive printf calls before the + // final line is fully transmitted (same issue and same fix as + // obpmark_1_1_smp_32.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++]='0'+(q%10); q/=10; } } + for (int i = n-1; i >= 0; i--) *p++ = tmp[i]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main(): returning triggers _exit(), + // which fires $finish before (or while) hart 0 is still writing its + // results to the UART. Same fix as obpmark_1_1_smp_32.c. + // + // Disable global interrupts (mstatus.MIE=0) before parking in wfi: + // without this, hart1 can be woken by spurious timer/software + // interrupts, re-check the while(1) condition, and re-enter wfi - + // each wake/sleep cycle costs real simulated time. With a much + // longer pipeline than obpmark_1_1_smp_32.c (many more barriers), + // this overhead was large enough to delay $finish substantially. + // With MIE cleared, hart1 stays parked in wfi for good. + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_2_1_host_seq.c b/sw/tests/bare-metal/hostd/obpmark_2_1_host_seq.c new file mode 100644 index 000000000..5121108fe --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_2_1_host_seq.c @@ -0,0 +1,493 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #2.1 - CCSDS 121.0 Data Compression +// Bare-metal SEQUENZIALE per Astral (1x CVA6, RV64) +// +// Porting dal riferimento CPU OBPMark: +// src/2.1-data_compression/src/ports/cpu/{processing.c,processing.h} +// src/2.1-data_compression/src/output_format_utils.c +// src/2.1-data_compression/include/{benchmark.h,device.h,output_format_utils.h} +// +// Allineato all'ambiente bare-metal gia' usato per il benchmark #1.1 +// (car_init_start, read_mcycle, printf.h): niente libc (no , +// , ), buffer statici, nessuna allocazione dinamica +// (FCompressedData.data e' a dimensione fissa J_BLOCKSIZE, al posto di +// malloc/calloc/free usati dal riferimento). +// +// Naming convention: obpmark_2_1_host_seq (obpmark___). + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include + +// ============================================================ +// Configurazione benchmark (compile-time, al posto del parsing CLI +// util_arg.c del riferimento; nessun file di input/verifica: il target +// bare-metal non ha filesystem) +// ============================================================ +#define N_BITS 16u // ampiezza campione in bit +#define J_BLOCKSIZE 8u // campioni per blocco (JBLOCKSIZE1) +#define R_SAMPLES_INTERVAL 8u // blocchi per intervallo di riferimento +#define STEPS 2u // numero di intervalli di riferimento +#define PREPROCESSOR_ACTIVE 1 // predittore CCSDS 121.0 attivo + +#define SAMPLES_PER_STEP (R_SAMPLES_INTERVAL * J_BLOCKSIZE) +#define TOTAL_SAMPLES (SAMPLES_PER_STEP * STEPS) + +// Caso peggiore: ogni blocco non compresso, n_bits/campione, piu' header per blocco. +#define OUTPUT_BUFFER_BYTES 512u + +#define CVA6_CLK_PERIOD_NS 20ULL +#define CVA6_FREQ_HZ 50000000ULL + +// ============================================================ +// Identificatori interni CCSDS 121.0 (benchmark.h) +// ============================================================ +#define ZERO_BLOCK_ID 0u +#define FUNDAMENTAL_SEQUENCE_ID 1u +#define SECOND_EXTENSION_ID 2u +#define SAMPLE_SPLITTING_ID 3u +#define NO_COMPRESSION_ID 32u + +#define MIN_MACRO(x, y) (((x) < (y)) ? (x) : (y)) + +// ============================================================ +// Cycle counter (RV64, come nel porting sequenziale del benchmark #1.1) +// ============================================================ +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +// ============================================================ +// Output bit stream (output_format_utils.h) +// ============================================================ +typedef struct { + uint8_t OutputBitStream[OUTPUT_BUFFER_BYTES]; + unsigned int num_bits; + unsigned int num_total_bytes; +} OutputBitStream_t; + +static void writeWord(OutputBitStream_t *status, unsigned int word, int number_bits) { + int i; + for (i = number_bits - 1; i >= 0; --i) { + status->OutputBitStream[status->num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1u) << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { + status->num_bits = 0u; + status->num_total_bytes++; + } + } +} + +static void writeWordChar(OutputBitStream_t *status, uint8_t word, int number_bits) { + int i; + for (i = number_bits - 1; i >= 0; --i) { + status->OutputBitStream[status->num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1u) << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { + status->num_bits = 0u; + status->num_total_bytes++; + } + } +} + +static void writeValue(OutputBitStream_t *status, uint8_t value, unsigned int number_bits) { + unsigned int i; + if (value != 0u && value != 1u) return; + value &= 0x1u; + for (i = 0; i < number_bits; ++i) { + status->OutputBitStream[status->num_total_bytes] |= + (uint8_t)(value << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { + status->num_bits = 0u; + status->num_total_bytes++; + } + } +} + +static void ZeroBlockWriter(OutputBitStream_t *status, unsigned int size) { + writeValue(status, 0u, size - 1u); + writeValue(status, 1u, 1u); +} + +static void NoCompressionWriter(OutputBitStream_t *status, unsigned int j_blocksize, + unsigned int n_bits, unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) writeWord(status, data[i], (int)n_bits); +} + +static void FundamentalSequenceWriter(OutputBitStream_t *status, unsigned int j_blocksize, + unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) { + writeValue(status, 0u, data[i]); + writeValue(status, 1u, 1u); + } +} + +static void SecondExtensionWriter(OutputBitStream_t *status, unsigned int half_blocksize, + unsigned int *data) { + unsigned int i; + for (i = 0; i < half_blocksize; ++i) writeWord(status, data[i], 32); +} + +static void SampleSplittingWriter(OutputBitStream_t *status, unsigned int j_blocksize, + unsigned int k, unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) { + writeValue(status, 0u, data[i] >> k); + writeValue(status, 1u, 1u); + } + for (i = 0; i < j_blocksize; ++i) writeWord(status, data[i], (int)k); +} + +// ============================================================ +// Preprocessor: predittore unit-delay + error mapper CCSDS 121.0 +// ============================================================ +static int DelayedStack = 0; // persiste tra blocchi/step, come in cpu/processing.c + +static int UnitDelayPredictor(int data_sample) { + const int cached = DelayedStack; + DelayedStack = data_sample; + return cached; +} + +static int PredictorErrorMapper(int predicted_value, int prediction_error, unsigned int n_bits) { + const int x_min = 0; + const int x_max = (int)((1u << n_bits) - 1u); + const int theta = MIN_MACRO(predicted_value - x_min, x_max - predicted_value); + + int preprocessed = theta + (prediction_error < 0 ? -prediction_error : prediction_error); + + if (prediction_error >= 0 && prediction_error <= theta) + preprocessed = 2 * prediction_error; + else if (prediction_error < 0 && -theta <= prediction_error) + preprocessed = (2 * (-prediction_error)) - 1; + + return preprocessed; +} + +static int Preprocessor(int x, unsigned int n_bits) { + const int predicted = UnitDelayPredictor(x); + const int error = x - predicted; + return PredictorErrorMapper(predicted, error, n_bits); +} + +// ============================================================ +// Adaptive entropy encoder (candidati a dimensione fissa, niente heap) +// ============================================================ +typedef struct { + unsigned int size; + unsigned int data[J_BLOCKSIZE]; + uint8_t CompressionIdentifier; + unsigned int CompressionIdentifierInternal; +} FCompressedData_t; + +static void CopyCandidate(FCompressedData_t *dst, const FCompressedData_t *src) { + unsigned int i; + dst->size = src->size; + dst->CompressionIdentifier = src->CompressionIdentifier; + dst->CompressionIdentifierInternal = src->CompressionIdentifierInternal; + for (i = 0; i < J_BLOCKSIZE; ++i) dst->data[i] = src->data[i]; +} + +// Equivalente della MIN() del riferimento: mantiene il candidato piu' piccolo. +static void KeepBest(FCompressedData_t *best, const FCompressedData_t *candidate) { + if (candidate->size < best->size) CopyCandidate(best, candidate); +} + +static void NoCompression(FCompressedData_t *out, unsigned int n_bits, + unsigned int j_blocksize, unsigned int *samples) { + unsigned int i; + out->CompressionIdentifierInternal = NO_COMPRESSION_ID; + out->size = n_bits * j_blocksize; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; + + if (n_bits < 3u) out->CompressionIdentifier = 0x1u; + else if (n_bits < 5u) out->CompressionIdentifier = 0x3u; + else if (n_bits <= 8u) out->CompressionIdentifier = 0x7u; + else if (n_bits <= 16u) out->CompressionIdentifier = 0xFu; + else out->CompressionIdentifier = 0x1Fu; +} + +static unsigned int GetSizeSecondExtension(unsigned int half_blocksize, unsigned int *halved) { + unsigned int size = 0, i; + for (i = 0; i < half_blocksize; ++i) size += halved[i] + 1u; + return size; +} + +static void SecondExtension(FCompressedData_t *out, unsigned int j_blocksize, unsigned int *samples) { + const unsigned int half_blocksize = j_blocksize / 2u; + unsigned int halved[J_BLOCKSIZE / 2u]; + unsigned int i, compressed_size; + + // Algoritmo opzione SE, vedi CCSDS 121.0-B-2 + for (i = 0; i < half_blocksize; ++i) + halved[i] = (((samples[2u*i] + samples[2u*i+1u]) * (samples[2u*i] + samples[2u*i+1u] + 1u)) / 2u) + samples[2u*i+1u]; + + out->CompressionIdentifier = SECOND_EXTENSION_ID - 1u; + out->size = j_blocksize * 32u; + + compressed_size = GetSizeSecondExtension(half_blocksize, halved); + if (compressed_size > j_blocksize * 32u) return; + + out->size = compressed_size; + out->CompressionIdentifierInternal = SECOND_EXTENSION_ID; + for (i = 0; i < half_blocksize; ++i) out->data[i] = halved[i]; +} + +static unsigned int GetSizeFundamentalSequence(unsigned int j_blocksize, unsigned int *samples) { + unsigned int size = 0, i; + for (i = 0; i < j_blocksize; ++i) size += samples[i] + 1u; + return size; +} + +static void FundamentalSequence(FCompressedData_t *out, unsigned int j_blocksize, unsigned int *samples) { + unsigned int i, compressed_size; + out->CompressionIdentifier = FUNDAMENTAL_SEQUENCE_ID; + out->size = j_blocksize * 32u; + + compressed_size = GetSizeFundamentalSequence(j_blocksize, samples); + if (compressed_size > j_blocksize * 32u) return; + + out->size = compressed_size; + out->CompressionIdentifierInternal = FUNDAMENTAL_SEQUENCE_ID; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; +} + +static unsigned int GetSizeSampleSplitting(unsigned int j_blocksize, unsigned int *samples, unsigned int k) { + unsigned int size = 0, i; + for (i = 0; i < j_blocksize; ++i) size += (k + (samples[i] >> k) + 1u); + return size; +} + +static void SampleSplitting(FCompressedData_t *out, unsigned int n_bits, unsigned int j_blocksize, + unsigned int *samples, unsigned int k) { + unsigned int i, compressed_size; + out->size = j_blocksize * 32u; + + if (k >= n_bits - 2u) { + out->CompressionIdentifier = (uint8_t)(SAMPLE_SPLITTING_ID + k); + return; + } + if (k == 0u) { + out->CompressionIdentifier = 1u; + FundamentalSequence(out, j_blocksize, samples); + return; + } + + compressed_size = GetSizeSampleSplitting(j_blocksize, samples, k); + if (compressed_size > j_blocksize * 32u) { + out->CompressionIdentifier = (uint8_t)(SAMPLE_SPLITTING_ID + k); + return; + } + + out->CompressionIdentifier = (uint8_t)(k + 1u); + out->size = compressed_size; + out->CompressionIdentifierInternal = SAMPLE_SPLITTING_ID + k; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; +} + +static void ZeroBlock(FCompressedData_t *out, unsigned int j_blocksize, unsigned int number_of_zeros) { + unsigned int i; + out->size = number_of_zeros + 1u; + for (i = 0; i < j_blocksize; ++i) out->data[i] = 0u; + out->data[0] = 1u; + out->CompressionIdentifier = ZERO_BLOCK_ID; + out->CompressionIdentifierInternal = ZERO_BLOCK_ID; +} + +// Sceglie la codifica piu' piccola per un blocco e la serializza subito, +// come fa il riferimento cpu/processing.c (nessun array intermedio per +// blocco, a differenza dei porting OpenMP/CUDA). +static void AdaptativeEntropyEncoder(OutputBitStream_t *out_stream, unsigned int n_bits, + unsigned int j_blocksize, unsigned int *samples, + int number_of_zeros) { + FCompressedData_t best; + FCompressedData_t candidate; + unsigned int id_size, k; + + if (number_of_zeros == -1) { + NoCompression(&best, n_bits, j_blocksize, samples); + + SecondExtension(&candidate, j_blocksize, samples); + KeepBest(&best, &candidate); + + for (k = 0; k < n_bits; ++k) { + SampleSplitting(&candidate, n_bits, j_blocksize, samples, k); + KeepBest(&best, &candidate); + } + } else { + ZeroBlock(&best, j_blocksize, (unsigned int)number_of_zeros); + } + + // Larghezza del campo identificatore di tecnica, per CCSDS 121.0 + if (n_bits < 3u) id_size = 1u; + else if (n_bits < 5u) id_size = 2u; + else if (n_bits <= 8u) id_size = 3u; + else if (n_bits <= 16u) id_size = 4u; + else id_size = 5u; + + if (best.CompressionIdentifierInternal == ZERO_BLOCK_ID || + best.CompressionIdentifierInternal == SECOND_EXTENSION_ID) + id_size += 1u; + + writeWordChar(out_stream, best.CompressionIdentifier, (int)id_size); + + if (best.CompressionIdentifierInternal == ZERO_BLOCK_ID) + ZeroBlockWriter(out_stream, best.size); + else if (best.CompressionIdentifierInternal == NO_COMPRESSION_ID) + NoCompressionWriter(out_stream, j_blocksize, n_bits, best.data); + else if (best.CompressionIdentifierInternal == FUNDAMENTAL_SEQUENCE_ID) + FundamentalSequenceWriter(out_stream, j_blocksize, best.data); + else if (best.CompressionIdentifierInternal == SECOND_EXTENSION_ID) + SecondExtensionWriter(out_stream, j_blocksize / 2u, best.data); + else if (best.CompressionIdentifierInternal >= SAMPLE_SPLITTING_ID) + SampleSplittingWriter(out_stream, j_blocksize, + best.CompressionIdentifierInternal - SAMPLE_SPLITTING_ID, best.data); +} + +// ============================================================ +// Bookkeeping run di zero-block (strutture di processing.h) +// ============================================================ +typedef struct { + unsigned int counter; + int position; +} ZeroBlockCounter_t; + +typedef struct { + int NumberOfZeros; +} ZeroBlockProcessed_t; + +// ============================================================ +// Buffer statici (niente heap) +// ============================================================ +static unsigned int input_data[TOTAL_SAMPLES]; +static unsigned int preprocessed[SAMPLES_PER_STEP]; +static OutputBitStream_t output_stream; + +// Flusso sintetico deterministico: i blocchi 0-1 dello step 0 sono a +// valore costante, cosi' il blocco 1 (il cui predittore e' inizializzato +// dall'ultimo campione del blocco 0) si preprocessa a tutto zero, +// esercitando il percorso Zero Block; i blocchi restanti variano, +// esercitando No-Compression / Second-Extension / Sample-Splitting. +static void generate_input(void) { + unsigned int s, b, i, idx = 0; + for (s = 0; s < STEPS; ++s) { + for (b = 0; b < R_SAMPLES_INTERVAL; ++b) { + for (i = 0; i < J_BLOCKSIZE; ++i) { + if (s == 0u && (b == 0u || b == 1u)) + input_data[idx] = 100u; + else + input_data[idx] = (idx * 37u + s * 13u + 5u) % 4096u; + idx++; + } + } + } +} + +static void print_results(uint64_t cycles, unsigned int checksum) { + uint64_t time_ns = cycles * CVA6_CLK_PERIOD_NS; + uint64_t time_us = time_ns / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + printf("Benchmark metrics (sequenziale, 1x CVA6):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n", + (unsigned long long)time_ms_int, (unsigned long long)time_ms_frac); + printf(" Total output bytes : %u\r\n", output_stream.num_total_bytes); + printf(" Checksum : %u\r\n", checksum); +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + unsigned int step, block, i, checksum; + uint64_t t_start, t_end; + + car_init_start(); + + printf("OBPMark #2.1 - CCSDS 121.0 Data Compression\r\n"); + printf("Bare-metal sequenziale - Astral CVA6 RV64 @ 50 MHz\r\n"); + printf("n_bits=%u j_blocksize=%u r_samplesInterval=%u steps=%u\r\n", + N_BITS, J_BLOCKSIZE, R_SAMPLES_INTERVAL, STEPS); + + generate_input(); + for (i = 0; i < OUTPUT_BUFFER_BYTES; ++i) output_stream.OutputBitStream[i] = 0u; + output_stream.num_bits = 0u; + output_stream.num_total_bytes = 0u; + + t_start = read_mcycle(); + + for (step = 0; step < STEPS; ++step) { + ZeroBlockCounter_t zero_counter[R_SAMPLES_INTERVAL]; + ZeroBlockProcessed_t zb_processed[R_SAMPLES_INTERVAL]; + unsigned int zero_counter_pos = 0u; + unsigned int number_of_consecutive_zero_blocks = 0u; + + for (i = 0; i < R_SAMPLES_INTERVAL; ++i) { zero_counter[i].counter = 0u; zero_counter[i].position = -1; } + for (i = 0; i < R_SAMPLES_INTERVAL; ++i) zb_processed[i].NumberOfZeros = -1; + + // --- preprocess_data --- + for (block = 0; block < R_SAMPLES_INTERVAL; ++block) { + int all_zeros_in_block = 1; + for (i = 0; i < J_BLOCKSIZE; ++i) { + const unsigned int in_idx = step * SAMPLES_PER_STEP + block * J_BLOCKSIZE + i; + unsigned int value; +#if PREPROCESSOR_ACTIVE + value = (unsigned int)Preprocessor((int)input_data[in_idx], N_BITS); +#else + value = input_data[in_idx]; +#endif + preprocessed[block * J_BLOCKSIZE + i] = value; + if (value != 0u) all_zeros_in_block = 0; + } + + if (all_zeros_in_block) { + zero_counter[zero_counter_pos].counter = number_of_consecutive_zero_blocks; + zero_counter[zero_counter_pos].position = (int)block; + zero_counter_pos++; + number_of_consecutive_zero_blocks++; + } else { + number_of_consecutive_zero_blocks = 0u; + } + } + + // --- process_zeroblock --- + for (i = 0; i < zero_counter_pos; ++i) + zb_processed[zero_counter[i].position].NumberOfZeros = (int)zero_counter[i].counter; + + // --- process_blocks --- + for (block = 0; block < R_SAMPLES_INTERVAL; ++block) { + AdaptativeEntropyEncoder(&output_stream, N_BITS, J_BLOCKSIZE, + &preprocessed[block * J_BLOCKSIZE], + zb_processed[block].NumberOfZeros); + } + } + + t_end = read_mcycle(); + + checksum = 0u; + for (i = 0; i < output_stream.num_total_bytes + 1u; ++i) + checksum += output_stream.OutputBitStream[i]; + + printf("Done.\r\n"); + print_results(t_end - t_start, checksum); + printf("Output[0]: %u\r\n", (unsigned int)output_stream.OutputBitStream[0]); + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_2_1_host_seq_file.c b/sw/tests/bare-metal/hostd/obpmark_2_1_host_seq_file.c new file mode 100644 index 000000000..16fb02ab5 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_2_1_host_seq_file.c @@ -0,0 +1,494 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #2.1 - CCSDS 121.0 Data Compression +// Bare-metal SEQUENZIALE per Astral (1x CVA6, RV64) - input di riferimento +// +// Variante di obpmark_2_1_host_seq.c che sostituisce il flusso sintetico +// generato in codice con i campioni reali del file di input di riferimento +// OBPMark (data/input_data/2.1-compression/1024/...), convertiti in header +// C da gen_obpmark_2_1_input.py: il target bare-metal non ha filesystem, +// quindi load_data_from_file() del riferimento non e' utilizzabile e i dati +// vengono incorporati in .rodata. +// +// Configurazione, dimensioni dei buffer e valori attesi arrivano tutti +// dall'header generato, cosi' che cambiare taglia richieda solo di +// rigenerarlo. L'algoritmo e' invariato rispetto alla versione gia' +// validata (Output[0]=64, 130 byte, Checksum=13073 con input sintetico +// N_BITS=16 J=8 R=8 STEPS=2). +// +// Differenze rispetto a obpmark_2_1_host_seq.c, oltre alla sorgente dati: +// - zero_counter[] e zb_processed[] spostati da stack a storage statico: +// con r_samplesInterval grande le due VLA-equivalenti supererebbero lo +// stack (stesso problema gia' incontrato nel porting SMP di #1.1). +// - verdetto PASS/FAIL contro i valori attesi calcolati dal modello di +// riferimento, come nei porting per cluster. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include + +#include "obpmark_2_1_input.h" + +// ============================================================ +// Configurazione, derivata interamente dall'header generato +// ============================================================ +#define N_BITS OBPMARK_2_1_N_BITS +#define J_BLOCKSIZE OBPMARK_2_1_J_BLOCKSIZE +#define R_SAMPLES_INTERVAL OBPMARK_2_1_R_SAMPLES_INTERVAL +#define STEPS OBPMARK_2_1_STEPS +#define PREPROCESSOR_ACTIVE 1 + +#define SAMPLES_PER_STEP (R_SAMPLES_INTERVAL * J_BLOCKSIZE) +#define TOTAL_SAMPLES OBPMARK_2_1_TOTAL_SAMPLES +#define OUTPUT_BUFFER_BYTES OBPMARK_2_1_OUTPUT_BUFFER_BYTES + +#define EXPECTED_OUTPUT_0 OBPMARK_2_1_EXPECTED_OUTPUT_0 +#define EXPECTED_TOTAL_BYTES OBPMARK_2_1_EXPECTED_TOTAL_BYTES +#define EXPECTED_CHECKSUM OBPMARK_2_1_EXPECTED_CHECKSUM + +#define CVA6_CLK_PERIOD_NS 20ULL + +// ============================================================ +// Identificatori interni CCSDS 121.0 (benchmark.h) +// ============================================================ +#define ZERO_BLOCK_ID 0u +#define FUNDAMENTAL_SEQUENCE_ID 1u +#define SECOND_EXTENSION_ID 2u +#define SAMPLE_SPLITTING_ID 3u +#define NO_COMPRESSION_ID 32u + +#define MIN_MACRO(x, y) (((x) < (y)) ? (x) : (y)) + +// ============================================================ +// Cycle counter (RV64, come negli altri porting host) +// ============================================================ +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +// ============================================================ +// Output bit stream (output_format_utils.h) +// ============================================================ +typedef struct { + uint8_t OutputBitStream[OUTPUT_BUFFER_BYTES]; + unsigned int num_bits; + unsigned int num_total_bytes; +} OutputBitStream_t; + +static void writeWord(OutputBitStream_t *status, unsigned int word, int number_bits) { + int i; + for (i = number_bits - 1; i >= 0; --i) { + status->OutputBitStream[status->num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1u) << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { status->num_bits = 0u; status->num_total_bytes++; } + } +} + +static void writeWordChar(OutputBitStream_t *status, uint8_t word, int number_bits) { + int i; + for (i = number_bits - 1; i >= 0; --i) { + status->OutputBitStream[status->num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1u) << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { status->num_bits = 0u; status->num_total_bytes++; } + } +} + +static void writeValue(OutputBitStream_t *status, uint8_t value, unsigned int number_bits) { + unsigned int i; + if (value != 0u && value != 1u) return; + value &= 0x1u; + for (i = 0; i < number_bits; ++i) { + status->OutputBitStream[status->num_total_bytes] |= + (uint8_t)(value << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { status->num_bits = 0u; status->num_total_bytes++; } + } +} + +static void ZeroBlockWriter(OutputBitStream_t *status, unsigned int size) { + writeValue(status, 0u, size - 1u); + writeValue(status, 1u, 1u); +} + +static void NoCompressionWriter(OutputBitStream_t *status, unsigned int j_blocksize, + unsigned int n_bits, unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) writeWord(status, data[i], (int)n_bits); +} + +static void FundamentalSequenceWriter(OutputBitStream_t *status, unsigned int j_blocksize, + unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) { writeValue(status, 0u, data[i]); writeValue(status, 1u, 1u); } +} + +static void SecondExtensionWriter(OutputBitStream_t *status, unsigned int half_blocksize, + unsigned int *data) { + unsigned int i; + for (i = 0; i < half_blocksize; ++i) writeWord(status, data[i], 32); +} + +static void SampleSplittingWriter(OutputBitStream_t *status, unsigned int j_blocksize, + unsigned int k, unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) { writeValue(status, 0u, data[i] >> k); writeValue(status, 1u, 1u); } + for (i = 0; i < j_blocksize; ++i) writeWord(status, data[i], (int)k); +} + +// ============================================================ +// Preprocessor: predittore unit-delay + error mapper CCSDS 121.0 +// ============================================================ +static int DelayedStack = 0; // persiste tra blocchi/step, come in cpu/processing.c + +static int UnitDelayPredictor(int data_sample) { + const int cached = DelayedStack; + DelayedStack = data_sample; + return cached; +} + +static int PredictorErrorMapper(int predicted_value, int prediction_error, unsigned int n_bits) { + const int x_min = 0; + const int x_max = (int)((1u << n_bits) - 1u); + const int theta = MIN_MACRO(predicted_value - x_min, x_max - predicted_value); + + int preprocessed = theta + (prediction_error < 0 ? -prediction_error : prediction_error); + + if (prediction_error >= 0 && prediction_error <= theta) + preprocessed = 2 * prediction_error; + else if (prediction_error < 0 && -theta <= prediction_error) + preprocessed = (2 * (-prediction_error)) - 1; + + return preprocessed; +} + +static int Preprocessor(int x, unsigned int n_bits) { + const int predicted = UnitDelayPredictor(x); + const int error = x - predicted; + return PredictorErrorMapper(predicted, error, n_bits); +} + +// ============================================================ +// Adaptive entropy encoder (candidati a dimensione fissa, niente heap) +// ============================================================ +typedef struct { + unsigned int size; + unsigned int data[J_BLOCKSIZE]; + uint8_t CompressionIdentifier; + unsigned int CompressionIdentifierInternal; +} FCompressedData_t; + +static void CopyCandidate(FCompressedData_t *dst, const FCompressedData_t *src) { + unsigned int i; + dst->size = src->size; + dst->CompressionIdentifier = src->CompressionIdentifier; + dst->CompressionIdentifierInternal = src->CompressionIdentifierInternal; + for (i = 0; i < J_BLOCKSIZE; ++i) dst->data[i] = src->data[i]; +} + +static void KeepBest(FCompressedData_t *best, const FCompressedData_t *candidate) { + if (candidate->size < best->size) CopyCandidate(best, candidate); +} + +static void NoCompression(FCompressedData_t *out, unsigned int n_bits, + unsigned int j_blocksize, unsigned int *samples) { + unsigned int i; + out->CompressionIdentifierInternal = NO_COMPRESSION_ID; + out->size = n_bits * j_blocksize; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; + + if (n_bits < 3u) out->CompressionIdentifier = 0x1u; + else if (n_bits < 5u) out->CompressionIdentifier = 0x3u; + else if (n_bits <= 8u) out->CompressionIdentifier = 0x7u; + else if (n_bits <= 16u) out->CompressionIdentifier = 0xFu; + else out->CompressionIdentifier = 0x1Fu; +} + +static unsigned int GetSizeSecondExtension(unsigned int half_blocksize, unsigned int *halved) { + unsigned int size = 0, i; + for (i = 0; i < half_blocksize; ++i) size += halved[i] + 1u; + return size; +} + +static void SecondExtension(FCompressedData_t *out, unsigned int j_blocksize, unsigned int *samples) { + const unsigned int half_blocksize = j_blocksize / 2u; + unsigned int halved[J_BLOCKSIZE / 2u]; + unsigned int i, compressed_size; + + // Algoritmo opzione SE, vedi CCSDS 121.0-B-2. Con campioni reali a 16 bit + // il prodotto puo' eccedere i 32 bit e wrappa: comportamento identico al + // riferimento, che usa la stessa aritmetica su unsigned int. + for (i = 0; i < half_blocksize; ++i) + halved[i] = (((samples[2u*i] + samples[2u*i+1u]) * (samples[2u*i] + samples[2u*i+1u] + 1u)) / 2u) + samples[2u*i+1u]; + + out->CompressionIdentifier = SECOND_EXTENSION_ID - 1u; + out->size = j_blocksize * 32u; + + compressed_size = GetSizeSecondExtension(half_blocksize, halved); + if (compressed_size > j_blocksize * 32u) return; + + out->size = compressed_size; + out->CompressionIdentifierInternal = SECOND_EXTENSION_ID; + for (i = 0; i < half_blocksize; ++i) out->data[i] = halved[i]; +} + +static unsigned int GetSizeFundamentalSequence(unsigned int j_blocksize, unsigned int *samples) { + unsigned int size = 0, i; + for (i = 0; i < j_blocksize; ++i) size += samples[i] + 1u; + return size; +} + +static void FundamentalSequence(FCompressedData_t *out, unsigned int j_blocksize, unsigned int *samples) { + unsigned int i, compressed_size; + out->CompressionIdentifier = FUNDAMENTAL_SEQUENCE_ID; + out->size = j_blocksize * 32u; + + compressed_size = GetSizeFundamentalSequence(j_blocksize, samples); + if (compressed_size > j_blocksize * 32u) return; + + out->size = compressed_size; + out->CompressionIdentifierInternal = FUNDAMENTAL_SEQUENCE_ID; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; +} + +static unsigned int GetSizeSampleSplitting(unsigned int j_blocksize, unsigned int *samples, unsigned int k) { + unsigned int size = 0, i; + for (i = 0; i < j_blocksize; ++i) size += (k + (samples[i] >> k) + 1u); + return size; +} + +static void SampleSplitting(FCompressedData_t *out, unsigned int n_bits, unsigned int j_blocksize, + unsigned int *samples, unsigned int k) { + unsigned int i, compressed_size; + out->size = j_blocksize * 32u; + + if (k >= n_bits - 2u) { + out->CompressionIdentifier = (uint8_t)(SAMPLE_SPLITTING_ID + k); + return; + } + if (k == 0u) { + out->CompressionIdentifier = 1u; + FundamentalSequence(out, j_blocksize, samples); + return; + } + + compressed_size = GetSizeSampleSplitting(j_blocksize, samples, k); + if (compressed_size > j_blocksize * 32u) { + out->CompressionIdentifier = (uint8_t)(SAMPLE_SPLITTING_ID + k); + return; + } + + out->CompressionIdentifier = (uint8_t)(k + 1u); + out->size = compressed_size; + out->CompressionIdentifierInternal = SAMPLE_SPLITTING_ID + k; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; +} + +static void ZeroBlock(FCompressedData_t *out, unsigned int j_blocksize, unsigned int number_of_zeros) { + unsigned int i; + out->size = number_of_zeros + 1u; + for (i = 0; i < j_blocksize; ++i) out->data[i] = 0u; + out->data[0] = 1u; + out->CompressionIdentifier = ZERO_BLOCK_ID; + out->CompressionIdentifierInternal = ZERO_BLOCK_ID; +} + +// I due candidati sono statici e non locali: con J_BLOCKSIZE=64 due +// FCompressedData_t sullo stack sono gia' mezzo kilobyte per chiamata. +static FCompressedData_t g_best; +static FCompressedData_t g_candidate; + +static void AdaptativeEntropyEncoder(OutputBitStream_t *out_stream, unsigned int n_bits, + unsigned int j_blocksize, unsigned int *samples, + int number_of_zeros) { + unsigned int id_size, k; + + if (number_of_zeros == -1) { + NoCompression(&g_best, n_bits, j_blocksize, samples); + + SecondExtension(&g_candidate, j_blocksize, samples); + KeepBest(&g_best, &g_candidate); + + for (k = 0; k < n_bits; ++k) { + SampleSplitting(&g_candidate, n_bits, j_blocksize, samples, k); + KeepBest(&g_best, &g_candidate); + } + } else { + ZeroBlock(&g_best, j_blocksize, (unsigned int)number_of_zeros); + } + + if (n_bits < 3u) id_size = 1u; + else if (n_bits < 5u) id_size = 2u; + else if (n_bits <= 8u) id_size = 3u; + else if (n_bits <= 16u) id_size = 4u; + else id_size = 5u; + + if (g_best.CompressionIdentifierInternal == ZERO_BLOCK_ID || + g_best.CompressionIdentifierInternal == SECOND_EXTENSION_ID) + id_size += 1u; + + writeWordChar(out_stream, g_best.CompressionIdentifier, (int)id_size); + + if (g_best.CompressionIdentifierInternal == ZERO_BLOCK_ID) + ZeroBlockWriter(out_stream, g_best.size); + else if (g_best.CompressionIdentifierInternal == NO_COMPRESSION_ID) + NoCompressionWriter(out_stream, j_blocksize, n_bits, g_best.data); + else if (g_best.CompressionIdentifierInternal == FUNDAMENTAL_SEQUENCE_ID) + FundamentalSequenceWriter(out_stream, j_blocksize, g_best.data); + else if (g_best.CompressionIdentifierInternal == SECOND_EXTENSION_ID) + SecondExtensionWriter(out_stream, j_blocksize / 2u, g_best.data); + else if (g_best.CompressionIdentifierInternal >= SAMPLE_SPLITTING_ID) + SampleSplittingWriter(out_stream, j_blocksize, + g_best.CompressionIdentifierInternal - SAMPLE_SPLITTING_ID, + g_best.data); +} + +// ============================================================ +// Bookkeeping run di zero-block (strutture di processing.h), in storage +// statico: con r_samplesInterval grande non starebbero sullo stack +// ============================================================ +typedef struct { + unsigned int counter; + int position; +} ZeroBlockCounter_t; + +typedef struct { + int NumberOfZeros; +} ZeroBlockProcessed_t; + +static ZeroBlockCounter_t zero_counter[R_SAMPLES_INTERVAL]; +static ZeroBlockProcessed_t zb_processed[R_SAMPLES_INTERVAL]; + +// ============================================================ +// Buffer statici (niente heap). I campioni di ingresso restano in .rodata +// nell'header generato: non ne viene fatta copia in RAM. +// ============================================================ +static unsigned int preprocessed[SAMPLES_PER_STEP]; +static OutputBitStream_t output_stream; + +static void print_results(uint64_t cycles, unsigned int checksum) { + uint64_t time_us = (cycles * CVA6_CLK_PERIOD_NS) / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + // Throughput in campioni/s * 100, aritmetica intera: f_clk = 50 MHz. + uint64_t tp_int = ((uint64_t)TOTAL_SAMPLES * 50000000ULL) / cycles; + // Rapporto di compressione * 100 rispetto ai TOTAL_SAMPLES * N_BITS bit grezzi. + uint64_t raw_bytes = ((uint64_t)TOTAL_SAMPLES * (uint64_t)N_BITS) / 8ULL; + uint64_t cr_int = (raw_bytes * 100ULL) / (uint64_t)output_stream.num_total_bytes; + + printf("Benchmark metrics (sequenziale, 1x CVA6):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n", + (unsigned long long)time_ms_int, (unsigned long long)time_ms_frac); + printf(" Throughput : %llu samples/s\r\n", (unsigned long long)tp_int); + printf(" Raw bytes : %llu\r\n", (unsigned long long)raw_bytes); + printf(" Total output bytes : %u\r\n", output_stream.num_total_bytes); + printf(" Compression ratio : %llu.%02llu\r\n", + (unsigned long long)(cr_int / 100ULL), (unsigned long long)(cr_int % 100ULL)); + printf(" Checksum : %u\r\n", checksum); +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + unsigned int step, block, i, checksum; + uint64_t t_start, t_end; + + car_init_start(); + + printf("OBPMark #2.1 - CCSDS 121.0 Data Compression\r\n"); + printf("Bare-metal sequenziale - Astral CVA6 RV64 @ 50 MHz\r\n"); + printf("Input: file di riferimento OBPMark (%u campioni)\r\n", TOTAL_SAMPLES); + printf("n_bits=%u j_blocksize=%u r_samplesInterval=%u steps=%u\r\n", + N_BITS, J_BLOCKSIZE, R_SAMPLES_INTERVAL, STEPS); + + for (i = 0; i < OUTPUT_BUFFER_BYTES; ++i) output_stream.OutputBitStream[i] = 0u; + output_stream.num_bits = 0u; + output_stream.num_total_bytes = 0u; + DelayedStack = 0; + + t_start = read_mcycle(); + + for (step = 0; step < STEPS; ++step) { + unsigned int zero_counter_pos = 0u; + unsigned int number_of_consecutive_zero_blocks = 0u; + + for (i = 0; i < R_SAMPLES_INTERVAL; ++i) { zero_counter[i].counter = 0u; zero_counter[i].position = -1; } + for (i = 0; i < R_SAMPLES_INTERVAL; ++i) zb_processed[i].NumberOfZeros = -1; + + // --- preprocess_data --- + for (block = 0; block < R_SAMPLES_INTERVAL; ++block) { + int all_zeros_in_block = 1; + for (i = 0; i < J_BLOCKSIZE; ++i) { + const unsigned int in_idx = step * SAMPLES_PER_STEP + block * J_BLOCKSIZE + i; + unsigned int value; +#if PREPROCESSOR_ACTIVE + value = (unsigned int)Preprocessor((int)obpmark_2_1_input_data[in_idx], N_BITS); +#else + value = (unsigned int)obpmark_2_1_input_data[in_idx]; +#endif + preprocessed[block * J_BLOCKSIZE + i] = value; + if (value != 0u) all_zeros_in_block = 0; + } + + if (all_zeros_in_block) { + zero_counter[zero_counter_pos].counter = number_of_consecutive_zero_blocks; + zero_counter[zero_counter_pos].position = (int)block; + zero_counter_pos++; + number_of_consecutive_zero_blocks++; + } else { + number_of_consecutive_zero_blocks = 0u; + } + } + + // --- process_zeroblock --- + for (i = 0; i < zero_counter_pos; ++i) + zb_processed[zero_counter[i].position].NumberOfZeros = (int)zero_counter[i].counter; + + // --- process_blocks --- + for (block = 0; block < R_SAMPLES_INTERVAL; ++block) { + AdaptativeEntropyEncoder(&output_stream, N_BITS, J_BLOCKSIZE, + &preprocessed[block * J_BLOCKSIZE], + zb_processed[block].NumberOfZeros); + } + } + + t_end = read_mcycle(); + + checksum = 0u; + for (i = 0; i < output_stream.num_total_bytes + 1u; ++i) + checksum += output_stream.OutputBitStream[i]; + + printf("Done.\r\n"); + print_results(t_end - t_start, checksum); + printf("Output[0] : %u\r\n", (unsigned int)output_stream.OutputBitStream[0]); + + // Ultima riga scritta a mano via uart_write_str+flush, non printf: il + // testbench innesca $finish appena uart_reading_byte==0, cosa che puo' + // accadere tra due printf consecutivi prima che l'ultima riga sia stata + // trasmessa per intero (stesso accorgimento dei porting SMP). + { + int pass = (output_stream.OutputBitStream[0] == EXPECTED_OUTPUT_0) && + (output_stream.num_total_bytes == EXPECTED_TOTAL_BYTES) && + (checksum == EXPECTED_CHECKSUM); + const char *msg = pass ? "RESULT: PASS\r\n" : "RESULT: FAIL\r\n"; + int n = 0; + while (msg[n]) n++; + uart_write_str(&__base_uart, (char *)msg, n); + uart_write_flush(&__base_uart); + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_2_1_host_smp.c b/sw/tests/bare-metal/hostd/obpmark_2_1_host_smp.c new file mode 100644 index 000000000..e24bf023d --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_2_1_host_smp.c @@ -0,0 +1,548 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #2.1 - CCSDS 121.0 Data Compression +// Bare-metal SMP port for Astral host domain (2x CVA6, RV64) +// +// Same algorithm and compile-time configuration as obpmark_2_1_host_seq.c +// (N_BITS=16, J_BLOCKSIZE=8, R_SAMPLES_INTERVAL=8, STEPS=2), so the result +// is expected to be bit-identical to the sequential run already validated +// on this platform (Output[0]=64, Total output bytes=130, Checksum=13073), +// the same validation approach used for obpmark_1_2_host_smp_v8.c against +// its sequential counterpart. +// +// Parallelization strategy, per step: +// Phase 1 (PARALLEL, split by block range): preprocessing. Unlike the +// sequential port's persistent `DelayedStack`, the unit-delay predictor +// here is recomputed directly from input_data[idx-1] (the predictor +// only ever needs the previous RAW sample, never a previously +// preprocessed value), so every sample is an independent, embarrassingly +// parallel computation - no cross-hart dependency at all. +// Phase 2 (SEQUENTIAL, hart 0 only): zero-block run-length counting. +// Genuinely sequential: each zero block's recorded counter depends on +// the running count of *all* preceding consecutive zero blocks in the +// same step, the same kind of true cross-element dependency that keeps +// SAR_rcmc_shift() sequential on hart 0 in obpmark_1_2_host_smp_v8.c. +// Cheap (R_SAMPLES_INTERVAL iterations), so not worth parallelizing. +// Phase 3 (PARALLEL, split by block range): adaptive entropy encoder +// candidate selection (NoCompression / SecondExtension / SampleSplitting +// / ZeroBlock -> smallest wins). Each block's candidates only read that +// block's own preprocessed samples, so this is independent per block - +// it is also the most expensive phase (up to n_bits SampleSplitting +// trials per block), making it the main parallelization target. +// Phase 4 (SEQUENTIAL, hart 0 only): bitstream serialization. Bit-level +// packing carries state (current byte, current bit offset) across +// blocks, so - unlike the sequential/cpu reference port, which writes +// directly inside AdaptativeEntropyEncoder - this SMP port follows the +// reference OpenMP/CUDA ports' structure instead: Phase 3 only selects +// and stores each block's winning candidate (mirroring their +// compresion_identifier/size_block/data_in_blocks arrays), and Phase 4 +// alone writes them to the shared OutputBitStream, in block order. +// +// hart_id() is re-read at every use point, never cached in a variable that +// survives a barrier or a call boundary - the documented workaround from +// obpmark_1_1_smp_32.c and obpmark_1_2_host_smp_v8.c ("Re-read hart_id() +// ... to avoid LTO register corruption"). No floating point is used in +// this benchmark, so the enable_fpu()/noinline split used in 1.2's SMP +// port for FPU-state safety is not needed here. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +// ============================================================ +// Compile-time configuration (identical to obpmark_2_1_host_seq.c) +// ============================================================ +#define N_BITS 16u +#define J_BLOCKSIZE 8u +#define R_SAMPLES_INTERVAL 8u +#define STEPS 2u +#define PREPROCESSOR_ACTIVE 1 + +#define SAMPLES_PER_STEP (R_SAMPLES_INTERVAL * J_BLOCKSIZE) +#define TOTAL_SAMPLES (SAMPLES_PER_STEP * STEPS) + +#define OUTPUT_BUFFER_BYTES 512u + +#define NUM_HARTS 2 +#define CVA6_CLK_PERIOD_NS 20ULL + +// ============================================================ +// CCSDS 121.0 internal identifiers (benchmark.h) +// ============================================================ +#define ZERO_BLOCK_ID 0u +#define FUNDAMENTAL_SEQUENCE_ID 1u +#define SECOND_EXTENSION_ID 2u +#define SAMPLE_SPLITTING_ID 3u +#define NO_COMPRESSION_ID 32u + +#define MIN_MACRO(x, y) (((x) < (y)) ? (x) : (y)) + +// ============================================================ +// Sense-reversing barrier for 2 harts (identical to obpmark_1_1_smp_32.c +// and obpmark_1_2_host_smp_v8.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// Cycle counter (same as the other host ports) +// ============================================================ +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +// ============================================================ +// Output bit stream (output_format_utils.h) - written by hart 0 only +// ============================================================ +typedef struct { + uint8_t OutputBitStream[OUTPUT_BUFFER_BYTES]; + unsigned int num_bits; + unsigned int num_total_bytes; +} OutputBitStream_t; + +static void writeWord(OutputBitStream_t *status, unsigned int word, int number_bits) { + int i; + for (i = number_bits - 1; i >= 0; --i) { + status->OutputBitStream[status->num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1u) << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { status->num_bits = 0u; status->num_total_bytes++; } + } +} + +static void writeWordChar(OutputBitStream_t *status, uint8_t word, int number_bits) { + int i; + for (i = number_bits - 1; i >= 0; --i) { + status->OutputBitStream[status->num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1u) << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { status->num_bits = 0u; status->num_total_bytes++; } + } +} + +static void writeValue(OutputBitStream_t *status, uint8_t value, unsigned int number_bits) { + unsigned int i; + if (value != 0u && value != 1u) return; + value &= 0x1u; + for (i = 0; i < number_bits; ++i) { + status->OutputBitStream[status->num_total_bytes] |= (uint8_t)(value << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { status->num_bits = 0u; status->num_total_bytes++; } + } +} + +static void ZeroBlockWriter(OutputBitStream_t *status, unsigned int size) { + writeValue(status, 0u, size - 1u); + writeValue(status, 1u, 1u); +} + +static void NoCompressionWriter(OutputBitStream_t *status, unsigned int j_blocksize, + unsigned int n_bits, unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) writeWord(status, data[i], (int)n_bits); +} + +static void FundamentalSequenceWriter(OutputBitStream_t *status, unsigned int j_blocksize, unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) { writeValue(status, 0u, data[i]); writeValue(status, 1u, 1u); } +} + +static void SecondExtensionWriter(OutputBitStream_t *status, unsigned int half_blocksize, unsigned int *data) { + unsigned int i; + for (i = 0; i < half_blocksize; ++i) writeWord(status, data[i], 32); +} + +static void SampleSplittingWriter(OutputBitStream_t *status, unsigned int j_blocksize, + unsigned int k, unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) { writeValue(status, 0u, data[i] >> k); writeValue(status, 1u, 1u); } + for (i = 0; i < j_blocksize; ++i) writeWord(status, data[i], (int)k); +} + +// ============================================================ +// Preprocessor: unit-delay predictor + CCSDS 121.0 error mapper. +// Stateless / parallel-safe: predicted value is always the previous RAW +// input sample, read directly from input_data - no dependency on any +// other sample's preprocessing result. +// ============================================================ +static int PredictorErrorMapper(int predicted_value, int prediction_error, unsigned int n_bits) { + const int x_min = 0; + const int x_max = (int)((1u << n_bits) - 1u); + const int theta = MIN_MACRO(predicted_value - x_min, x_max - predicted_value); + + int preprocessed = theta + (prediction_error < 0 ? -prediction_error : prediction_error); + + if (prediction_error >= 0 && prediction_error <= theta) + preprocessed = 2 * prediction_error; + else if (prediction_error < 0 && -theta <= prediction_error) + preprocessed = (2 * (-prediction_error)) - 1; + + return preprocessed; +} + +// ============================================================ +// Adaptive entropy encoder: candidate structure and selection functions. +// Fixed-size, no heap - same design as obpmark_2_1_host_seq.c. +// ============================================================ +typedef struct { + unsigned int size; + unsigned int data[J_BLOCKSIZE]; + uint8_t CompressionIdentifier; + unsigned int CompressionIdentifierInternal; +} FCompressedData_t; + +static void CopyCandidate(FCompressedData_t *dst, const FCompressedData_t *src) { + unsigned int i; + dst->size = src->size; + dst->CompressionIdentifier = src->CompressionIdentifier; + dst->CompressionIdentifierInternal = src->CompressionIdentifierInternal; + for (i = 0; i < J_BLOCKSIZE; ++i) dst->data[i] = src->data[i]; +} + +static void KeepBest(FCompressedData_t *best, const FCompressedData_t *candidate) { + if (candidate->size < best->size) CopyCandidate(best, candidate); +} + +static void NoCompression(FCompressedData_t *out, unsigned int n_bits, + unsigned int j_blocksize, unsigned int *samples) { + unsigned int i; + out->CompressionIdentifierInternal = NO_COMPRESSION_ID; + out->size = n_bits * j_blocksize; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; + + if (n_bits < 3u) out->CompressionIdentifier = 0x1u; + else if (n_bits < 5u) out->CompressionIdentifier = 0x3u; + else if (n_bits <= 8u) out->CompressionIdentifier = 0x7u; + else if (n_bits <= 16u) out->CompressionIdentifier = 0xFu; + else out->CompressionIdentifier = 0x1Fu; +} + +static unsigned int GetSizeSecondExtension(unsigned int half_blocksize, unsigned int *halved) { + unsigned int size = 0, i; + for (i = 0; i < half_blocksize; ++i) size += halved[i] + 1u; + return size; +} + +static void SecondExtension(FCompressedData_t *out, unsigned int j_blocksize, unsigned int *samples) { + const unsigned int half_blocksize = j_blocksize / 2u; + unsigned int halved[J_BLOCKSIZE / 2u]; + unsigned int i, compressed_size; + + for (i = 0; i < half_blocksize; ++i) + halved[i] = (((samples[2u*i] + samples[2u*i+1u]) * (samples[2u*i] + samples[2u*i+1u] + 1u)) / 2u) + samples[2u*i+1u]; + + out->CompressionIdentifier = SECOND_EXTENSION_ID - 1u; + out->size = j_blocksize * 32u; + + compressed_size = GetSizeSecondExtension(half_blocksize, halved); + if (compressed_size > j_blocksize * 32u) return; + + out->size = compressed_size; + out->CompressionIdentifierInternal = SECOND_EXTENSION_ID; + for (i = 0; i < half_blocksize; ++i) out->data[i] = halved[i]; +} + +static unsigned int GetSizeFundamentalSequence(unsigned int j_blocksize, unsigned int *samples) { + unsigned int size = 0, i; + for (i = 0; i < j_blocksize; ++i) size += samples[i] + 1u; + return size; +} + +static void FundamentalSequence(FCompressedData_t *out, unsigned int j_blocksize, unsigned int *samples) { + unsigned int i, compressed_size; + out->CompressionIdentifier = FUNDAMENTAL_SEQUENCE_ID; + out->size = j_blocksize * 32u; + + compressed_size = GetSizeFundamentalSequence(j_blocksize, samples); + if (compressed_size > j_blocksize * 32u) return; + + out->size = compressed_size; + out->CompressionIdentifierInternal = FUNDAMENTAL_SEQUENCE_ID; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; +} + +static unsigned int GetSizeSampleSplitting(unsigned int j_blocksize, unsigned int *samples, unsigned int k) { + unsigned int size = 0, i; + for (i = 0; i < j_blocksize; ++i) size += (k + (samples[i] >> k) + 1u); + return size; +} + +static void SampleSplitting(FCompressedData_t *out, unsigned int n_bits, unsigned int j_blocksize, + unsigned int *samples, unsigned int k) { + unsigned int i, compressed_size; + out->size = j_blocksize * 32u; + + if (k >= n_bits - 2u) { + out->CompressionIdentifier = (uint8_t)(SAMPLE_SPLITTING_ID + k); + return; + } + if (k == 0u) { + out->CompressionIdentifier = 1u; + FundamentalSequence(out, j_blocksize, samples); + return; + } + + compressed_size = GetSizeSampleSplitting(j_blocksize, samples, k); + if (compressed_size > j_blocksize * 32u) { + out->CompressionIdentifier = (uint8_t)(SAMPLE_SPLITTING_ID + k); + return; + } + + out->CompressionIdentifier = (uint8_t)(k + 1u); + out->size = compressed_size; + out->CompressionIdentifierInternal = SAMPLE_SPLITTING_ID + k; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; +} + +static void ZeroBlock(FCompressedData_t *out, unsigned int j_blocksize, unsigned int number_of_zeros) { + unsigned int i; + out->size = number_of_zeros + 1u; + for (i = 0; i < j_blocksize; ++i) out->data[i] = 0u; + out->data[0] = 1u; + out->CompressionIdentifier = ZERO_BLOCK_ID; + out->CompressionIdentifierInternal = ZERO_BLOCK_ID; +} + +// Phase 3 worker (PARALLEL): selects the smallest candidate for one block, +// writes it into *out. No shared mutable state besides *out itself, which +// is a distinct array slot per block - safe across harts. +static void SelectBestForBlock(FCompressedData_t *out, unsigned int n_bits, unsigned int j_blocksize, + unsigned int *samples, int number_of_zeros) { + FCompressedData_t candidate; + unsigned int k; + + if (number_of_zeros == -1) { + NoCompression(out, n_bits, j_blocksize, samples); + + SecondExtension(&candidate, j_blocksize, samples); + KeepBest(out, &candidate); + + for (k = 0; k < n_bits; ++k) { + SampleSplitting(&candidate, n_bits, j_blocksize, samples, k); + KeepBest(out, &candidate); + } + } else { + ZeroBlock(out, j_blocksize, (unsigned int)number_of_zeros); + } +} + +// Phase 4 worker (SEQUENTIAL, hart 0 only): serializes one already-selected +// block into the shared OutputBitStream. +static void SerializeBlock(OutputBitStream_t *out_stream, unsigned int n_bits, + unsigned int j_blocksize, const FCompressedData_t *best) { + unsigned int id_size; + + if (n_bits < 3u) id_size = 1u; + else if (n_bits < 5u) id_size = 2u; + else if (n_bits <= 8u) id_size = 3u; + else if (n_bits <= 16u) id_size = 4u; + else id_size = 5u; + + if (best->CompressionIdentifierInternal == ZERO_BLOCK_ID || + best->CompressionIdentifierInternal == SECOND_EXTENSION_ID) + id_size += 1u; + + writeWordChar(out_stream, best->CompressionIdentifier, (int)id_size); + + if (best->CompressionIdentifierInternal == ZERO_BLOCK_ID) + ZeroBlockWriter(out_stream, best->size); + else if (best->CompressionIdentifierInternal == NO_COMPRESSION_ID) + NoCompressionWriter(out_stream, j_blocksize, n_bits, (unsigned int *)best->data); + else if (best->CompressionIdentifierInternal == FUNDAMENTAL_SEQUENCE_ID) + FundamentalSequenceWriter(out_stream, j_blocksize, (unsigned int *)best->data); + else if (best->CompressionIdentifierInternal == SECOND_EXTENSION_ID) + SecondExtensionWriter(out_stream, j_blocksize / 2u, (unsigned int *)best->data); + else if (best->CompressionIdentifierInternal >= SAMPLE_SPLITTING_ID) + SampleSplittingWriter(out_stream, j_blocksize, + best->CompressionIdentifierInternal - SAMPLE_SPLITTING_ID, + (unsigned int *)best->data); +} + +// ============================================================ +// Static buffers (no heap; shared across harts, partitioned by block +// range per phase so distinct harts always write distinct indices) +// ============================================================ +static unsigned int input_data[TOTAL_SAMPLES]; +static unsigned int preprocessed[SAMPLES_PER_STEP]; +static unsigned int all_zeros_flag[R_SAMPLES_INTERVAL]; +static int number_of_zeros[R_SAMPLES_INTERVAL]; +static FCompressedData_t best_block[R_SAMPLES_INTERVAL]; +static OutputBitStream_t output_stream; + +// Same deterministic synthetic stream as obpmark_2_1_host_seq.c, generated +// by hart 0 only before the timed region. +static void generate_input(void) { + unsigned int s, b, i, idx = 0; + for (s = 0; s < STEPS; ++s) + for (b = 0; b < R_SAMPLES_INTERVAL; ++b) + for (i = 0; i < J_BLOCKSIZE; ++i) { + if (s == 0u && (b == 0u || b == 1u)) input_data[idx] = 100u; + else input_data[idx] = (idx * 37u + s * 13u + 5u) % 4096u; + idx++; + } +} + +static void __attribute__((noinline)) hart0_setup(void) { + unsigned int i; + printf("OBPMark #2.1 - CCSDS 121.0 Data Compression (SMP)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + printf("n_bits=%u j_blocksize=%u r_samplesInterval=%u steps=%u | Harts: %d\r\n", + N_BITS, J_BLOCKSIZE, R_SAMPLES_INTERVAL, STEPS, NUM_HARTS); + + generate_input(); + for (i = 0; i < OUTPUT_BUFFER_BYTES; ++i) output_stream.OutputBitStream[i] = 0u; + output_stream.num_bits = 0u; + output_stream.num_total_bytes = 0u; +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + if ((unsigned int)hart_id() == 0) { + car_init_start(); + smp_resume(); + } + barrier(); + + if ((unsigned int)hart_id() == 0) hart0_setup(); + barrier(); // input data and cleared output_stream now visible to both harts + + uint64_t t_start = ((unsigned int)hart_id() == 0) ? read_mcycle() : 0; + + unsigned int step; + for (step = 0; step < STEPS; ++step) { + unsigned int bs, be, block, i; + + // --- Phase 1 (PARALLEL): preprocessing --- + split_range(R_SAMPLES_INTERVAL, (unsigned int)hart_id(), NUM_HARTS, &bs, &be); + for (block = bs; block < be; ++block) { + unsigned int az = 1u; + for (i = 0; i < J_BLOCKSIZE; ++i) { + const unsigned int idx = step * SAMPLES_PER_STEP + block * J_BLOCKSIZE + i; + int predicted, error, value; +#if PREPROCESSOR_ACTIVE + predicted = (idx == 0u) ? 0 : (int)input_data[idx - 1u]; + error = (int)input_data[idx] - predicted; + value = PredictorErrorMapper(predicted, error, N_BITS); +#else + value = (int)input_data[idx]; +#endif + preprocessed[block * J_BLOCKSIZE + i] = (unsigned int)value; + if (value != 0) az = 0u; + } + all_zeros_flag[block] = az; + } + barrier(); + + // --- Phase 2 (SEQUENTIAL, hart 0 only): zero-run counting --- + if ((unsigned int)hart_id() == 0) { + unsigned int run = 0u; + for (block = 0; block < R_SAMPLES_INTERVAL; ++block) { + if (all_zeros_flag[block]) { number_of_zeros[block] = (int)run; run++; } + else { number_of_zeros[block] = -1; run = 0u; } + } + } + barrier(); + + // --- Phase 3 (PARALLEL): candidate selection --- + split_range(R_SAMPLES_INTERVAL, (unsigned int)hart_id(), NUM_HARTS, &bs, &be); + for (block = bs; block < be; ++block) { + SelectBestForBlock(&best_block[block], N_BITS, J_BLOCKSIZE, + &preprocessed[block * J_BLOCKSIZE], number_of_zeros[block]); + } + barrier(); + + // --- Phase 4 (SEQUENTIAL, hart 0 only): bitstream serialization --- + if ((unsigned int)hart_id() == 0) { + for (block = 0; block < R_SAMPLES_INTERVAL; ++block) + SerializeBlock(&output_stream, N_BITS, J_BLOCKSIZE, &best_block[block]); + } + barrier(); + } + + if ((unsigned int)hart_id() == 0) { + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_PERIOD_NS) / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + unsigned int checksum = 0u, i; + for (i = 0; i < output_stream.num_total_bytes + 1u; ++i) + checksum += output_stream.OutputBitStream[i]; + + printf("Done.\r\n"); + printf("Benchmark metrics (2x CVA6 SMP):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n", + (unsigned long long)time_ms_int, (unsigned long long)time_ms_frac); + printf(" Total output bytes : %u\r\n", output_stream.num_total_bytes); + printf(" Output[0] : %u\r\n", (unsigned int)output_stream.OutputBitStream[0]); + + // Last line written by hand via uart_write_str+flush, not printf: the + // testbench triggers $finish as soon as uart_reading_byte==0, which can + // happen between two consecutive printf calls before the final line is + // fully transmitted (same issue and same fix as obpmark_1_1_smp_32.c + // and obpmark_1_2_host_smp_v8.c). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0u) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++] = (char)('0' + (q % 10u)); q /= 10u; } } + for (int j = n - 1; j >= 0; j--) *p++ = tmp[j]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 must NOT return from main() (_exit() would trigger $finish + // before hart 0 finishes printing), and must stay parked in wfi with + // interrupts disabled so a spurious wake doesn't delay $finish - same + // fix as obpmark_1_2_host_smp_v8.c. + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_2_1_input.h b/sw/tests/bare-metal/hostd/obpmark_2_1_input.h new file mode 100644 index 000000000..ed3ecaa84 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_2_1_input.h @@ -0,0 +1,197 @@ +/* Generato da gen_obpmark_2_1_input.py - non modificare a mano. */ +/* Sorgente : 2.1-compression-data_1024x1024.bin */ +/* Campioni : 2048 (offset 0), letti come uint16 little-endian, */ +/* come load_data_from_file() del riferimento. */ + +#ifndef OBPMARK_2_1_INPUT_H_ +#define OBPMARK_2_1_INPUT_H_ + +#include + +#define OBPMARK_2_1_N_BITS 16u +#define OBPMARK_2_1_J_BLOCKSIZE 64u +#define OBPMARK_2_1_R_SAMPLES_INTERVAL 16u +#define OBPMARK_2_1_STEPS 2u +#define OBPMARK_2_1_TOTAL_SAMPLES 2048u +#define OBPMARK_2_1_OUTPUT_BUFFER_BYTES 8192u + +/* Valori attesi, calcolati dal modello di riferimento in Python. */ +#define OBPMARK_2_1_EXPECTED_OUTPUT_0 144u +#define OBPMARK_2_1_EXPECTED_TOTAL_BYTES 2666u +#define OBPMARK_2_1_EXPECTED_CHECKSUM 128741u + +static const uint16_t obpmark_2_1_input_data[OBPMARK_2_1_TOTAL_SAMPLES] = { + 3598, 3598, 3598, 3598, 3598, 3855, 4112, 4112, 4112, 4112, 4112, 3855, + 3855, 3855, 4112, 4112, 4112, 4369, 4369, 4626, 4883, 4883, 5140, 5397, + 5397, 5397, 5397, 5654, 5654, 5911, 6168, 6425, 6939, 7453, 8224, 8738, + 9252, 10023, 10537, 11051, 11565, 11565, 11565, 11565, 11308, 11308, 11308, 11308, + 11565, 12079, 12336, 12593, 12593, 12850, 12850, 13107, 12850, 13107, 13364, 13107, + 12850, 12593, 12593, 12336, 12336, 12336, 12336, 12336, 12593, 13107, 13621, 14135, + 14906, 15677, 16448, 16962, 17733, 18761, 19532, 20303, 21074, 21588, 22359, 23387, + 23644, 23901, 24672, 25443, 26471, 26985, 27499, 27756, 28270, 28784, 29298, 29555, + 29555, 29555, 29298, 28527, 27499, 26985, 25700, 25186, 25186, 25186, 25700, 26728, + 28784, 30840, 32896, 33410, 33667, 33667, 33410, 32896, 32125, 31611, 31097, 30583, + 30069, 29555, 29041, 28784, 28527, 28270, 28013, 28013, 28013, 28013, 28013, 28270, + 28270, 28270, 28013, 28013, 27756, 27756, 27756, 27756, 27756, 27499, 27242, 27242, + 26985, 26471, 26471, 26471, 26728, 27242, 28527, 29812, 31097, 31097, 31097, 30326, + 29812, 29555, 29555, 29555, 29812, 30326, 30840, 31097, 31354, 31611, 31868, 32382, + 32896, 33153, 33410, 33667, 33667, 33410, 33153, 32639, 31868, 31611, 31097, 30583, + 30326, 29812, 29298, 29041, 28527, 28270, 28013, 27499, 26728, 26471, 26214, 25700, + 25443, 25186, 25443, 25443, 25700, 25700, 25700, 26214, 26471, 26728, 27242, 27499, + 28527, 28784, 28784, 29041, 29041, 29298, 29298, 29298, 29555, 29812, 30069, 30326, + 30583, 30583, 30840, 31097, 31097, 31097, 31097, 31097, 31097, 30840, 30326, 30069, + 29812, 29555, 29298, 28527, 28527, 28527, 28784, 29041, 28784, 28013, 27756, 27756, + 28013, 28013, 28013, 28013, 28270, 28527, 28270, 27756, 26985, 26728, 26214, 25957, + 25443, 24672, 24415, 24672, 24929, 25443, 26728, 27756, 28784, 29555, 29812, 29555, + 28527, 28013, 27499, 27756, 27756, 27756, 28013, 28013, 28784, 29298, 30326, 30840, + 31097, 31097, 30840, 29812, 28013, 26214, 25186, 23644, 22873, 22359, 21588, 21074, + 20817, 19789, 19532, 19018, 18761, 18504, 18247, 17990, 17733, 17733, 17990, 17990, + 18504, 18761, 18761, 19018, 19018, 19275, 19532, 19789, 20046, 19789, 19789, 19532, + 19018, 18761, 18247, 17990, 17733, 17219, 17219, 17219, 16962, 16962, 17219, 16962, + 16962, 16705, 16705, 16448, 16191, 16191, 15934, 15677, 15677, 15677, 15677, 15934, + 15934, 16448, 16962, 17733, 19532, 20560, 21845, 22359, 22616, 22359, 21845, 20817, + 19789, 19275, 19018, 19018, 19018, 19275, 19532, 19789, 19789, 20046, 19789, 19789, + 19532, 19018, 18761, 18504, 17990, 17733, 17476, 17219, 16705, 16448, 16191, 16191, + 16448, 16705, 17476, 17990, 19275, 20817, 22102, 23901, 25186, 25700, 26214, 25957, + 25957, 25700, 25700, 25957, 26471, 26728, 27242, 27499, 27756, 27756, 27499, 26985, + 26214, 25700, 25443, 25443, 25443, 25700, 26471, 27242, 28013, 28527, 29041, 29298, + 29555, 29298, 29041, 29041, 29041, 29041, 29298, 29298, 28784, 28013, 27499, 25957, + 25443, 24672, 24158, 23901, 23387, 22873, 22102, 21588, 20560, 19275, 17990, 17476, + 16962, 16191, 15934, 15677, 15163, 14906, 14649, 14392, 14135, 14135, 14135, 14135, + 14135, 14135, 13878, 13621, 13364, 13364, 13621, 13621, 13878, 13878, 13878, 13878, + 13878, 13878, 14135, 14135, 14135, 14649, 14649, 14392, 14649, 14649, 14649, 14392, + 14392, 14135, 14135, 13621, 13364, 13364, 13364, 13364, 13107, 12850, 12593, 12336, + 11822, 11308, 10537, 10280, 9766, 9509, 9252, 9252, 9252, 9252, 9252, 9252, + 9252, 8738, 8738, 8481, 8224, 8224, 7967, 7967, 7967, 7967, 7967, 7967, + 8224, 8224, 8481, 8738, 8995, 8995, 8995, 8995, 9252, 8995, 8738, 8481, + 8481, 8224, 8224, 7967, 7967, 7710, 7710, 7453, 7453, 7196, 6939, 6682, + 6682, 6682, 6425, 6425, 6682, 6682, 6939, 6939, 6682, 6682, 6425, 6425, + 6168, 5911, 5911, 5654, 5397, 5397, 5140, 5140, 5140, 5397, 5397, 5654, + 5911, 5911, 5911, 6425, 6425, 6682, 6682, 6682, 6939, 6939, 6939, 6939, + 6939, 6939, 6939, 6682, 6425, 6168, 5911, 5911, 5654, 5654, 5654, 5654, + 5654, 5654, 5397, 5397, 5397, 5397, 5654, 5654, 5654, 5911, 6168, 6425, + 6682, 7196, 7196, 7453, 7710, 7710, 7967, 7967, 7967, 7967, 7967, 7967, + 7967, 7967, 7967, 7967, 7710, 7453, 7196, 6939, 6682, 6682, 6682, 6425, + 6425, 6425, 6425, 6168, 6168, 6168, 6168, 6168, 6168, 6168, 6168, 6168, + 5911, 5911, 5911, 5911, 5654, 5654, 5654, 5654, 5654, 5654, 5654, 5654, + 5911, 5911, 5911, 5911, 6168, 6168, 6168, 5911, 5911, 6168, 6168, 5911, + 5911, 5654, 5654, 5397, 5397, 5397, 5397, 5397, 5397, 5654, 5654, 5911, + 5654, 5654, 5654, 5397, 5397, 4883, 4626, 4369, 4112, 3855, 3855, 3855, + 3855, 3598, 3598, 3341, 3084, 3084, 3084, 3084, 2827, 2827, 2827, 2827, + 2570, 2570, 2570, 2570, 2570, 2570, 2570, 2570, 2313, 2313, 2313, 2313, + 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2056, 2056, 2056, 2056, + 2056, 2056, 2056, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, + 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, + 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2570, 2570, 2570, 2570, 2570, + 2570, 2570, 2570, 2827, 2827, 3084, 3084, 3084, 3084, 3084, 3084, 3084, + 3084, 3084, 3084, 3341, 3084, 3341, 3341, 3341, 3598, 3598, 3598, 3598, + 3598, 3598, 3598, 3598, 3598, 3598, 3598, 3598, 3598, 3598, 3855, 3855, + 3855, 3855, 3855, 3855, 3855, 4112, 3855, 3855, 3855, 4112, 4112, 4112, + 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4369, 4369, 4626, 4626, 4626, + 4626, 4626, 4626, 4883, 4883, 4883, 4883, 4883, 5140, 5140, 5140, 5140, + 5140, 5140, 5397, 5397, 5397, 5397, 5654, 5911, 6168, 6168, 6425, 6939, + 6939, 7196, 7453, 7453, 7710, 7710, 7710, 7967, 7967, 7967, 7967, 7967, + 7967, 7967, 7967, 8224, 8224, 8481, 8481, 8481, 8738, 8738, 8995, 8995, + 9252, 9252, 9509, 9509, 9766, 10023, 10023, 10023, 10537, 10794, 11308, 11565, + 12079, 12593, 13107, 13621, 13621, 13878, 14135, 14135, 14135, 14392, 14135, 14392, + 14392, 14649, 14649, 14649, 14649, 14906, 14906, 14906, 14906, 14906, 14649, 14649, + 14649, 14392, 14135, 14135, 13878, 14135, 14135, 14135, 14392, 14392, 14649, 14649, + 14649, 14392, 14135, 13878, 13878, 13621, 13621, 13878, 13878, 13621, 13621, 13364, + 13107, 12850, 12593, 12593, 12593, 12336, 12336, 12336, 12336, 12336, 12336, 12079, + 12079, 12079, 12079, 11822, 11308, 11051, 10537, 10280, 10023, 9766, 9252, 8995, + 8738, 8481, 8224, 7967, 7710, 7710, 7710, 7710, 7453, 7453, 7453, 7453, + 7196, 6939, 6939, 6425, 6425, 6168, 6168, 5911, 5911, 5654, 5654, 5397, + 5397, 5397, 5397, 5397, 5397, 5397, 5140, 5140, 5140, 5140, 5140, 5140, + 5140, 5140, 5397, 5397, 5654, 5654, 5911, 5911, 5911, 5911, 5911, 5654, + 5654, 5140, 4883, 4626, 4369, 4112, 4112, 3855, 3855, 3598, 3341, 3084, + 3084, 3084, 3084, 3084, 3084, 3084, 3341, 3598, 3855, 3855, 3855, 3598, + 3341, 3084, 2827, 2570, 2313, 2313, 2313, 2313, 2056, 2056, 2056, 2056, + 2056, 2056, 2313, 2313, 3341, 3341, 3598, 3598, 3598, 3855, 3855, 4112, + 4112, 4112, 3855, 3855, 3855, 3855, 4112, 4112, 4112, 4369, 4626, 4626, + 4883, 5140, 5397, 5397, 5397, 5654, 5654, 5654, 5911, 6168, 6425, 6939, + 7710, 8224, 8738, 8995, 9252, 10023, 10537, 10794, 11308, 11565, 11565, 11565, + 11565, 11308, 11308, 11308, 11565, 11822, 11822, 12336, 12593, 12336, 12593, 12593, + 12593, 12593, 12593, 12593, 12336, 12336, 12079, 12079, 11822, 12079, 12336, 12336, + 12593, 12850, 13107, 14135, 14906, 15677, 16705, 17476, 18247, 19275, 20046, 20817, + 21845, 22616, 23644, 24158, 24415, 25443, 26214, 26728, 26985, 27756, 28527, 29041, + 29555, 29812, 29812, 30069, 30326, 30069, 29555, 28784, 28013, 27242, 26728, 25957, + 25700, 25443, 25957, 26728, 29041, 30840, 32382, 33924, 34181, 34438, 33924, 33410, + 32639, 31868, 31354, 30583, 30069, 29555, 29041, 28527, 28013, 28013, 28013, 27756, + 27756, 27756, 27756, 28013, 27756, 28013, 27756, 27756, 27756, 27756, 27756, 27756, + 27499, 27756, 27499, 27242, 26985, 26471, 26471, 26214, 26728, 27499, 29298, 30840, + 31868, 32382, 32125, 31868, 31097, 30583, 30069, 30326, 30840, 31097, 31097, 31097, + 31354, 31611, 31868, 32125, 32639, 33153, 33924, 34181, 34181, 33924, 33410, 33410, + 32382, 31611, 31097, 30583, 30069, 29555, 28784, 28527, 28013, 27756, 27242, 26985, + 26728, 26471, 25957, 25700, 25700, 25443, 25700, 25700, 25957, 25957, 26214, 26471, + 26471, 26728, 27242, 27499, 28270, 28784, 28784, 28527, 28784, 28527, 28527, 28784, + 29041, 29041, 29041, 29555, 30069, 30326, 30583, 30840, 30840, 30840, 30840, 30583, + 30069, 29555, 29298, 29041, 28784, 28270, 27756, 27242, 27242, 27756, 27242, 26728, + 26728, 26728, 26985, 27242, 27242, 26985, 26985, 27242, 27756, 27499, 26985, 26728, + 26214, 25700, 24929, 24415, 23901, 23644, 23644, 23901, 24415, 25186, 26471, 27756, + 29041, 29812, 30069, 29298, 28784, 28013, 27499, 27242, 27499, 27499, 27756, 27756, + 28270, 28784, 29555, 30326, 30840, 30840, 30326, 29555, 28270, 26471, 25186, 24158, + 22616, 22359, 21588, 21074, 20817, 20046, 19789, 19275, 19018, 18504, 18504, 17990, + 17990, 17990, 18247, 18504, 18504, 18761, 18761, 19018, 19275, 19532, 19789, 19789, + 19789, 19789, 19532, 19275, 18761, 18761, 18247, 17733, 17733, 17476, 17219, 17219, + 17219, 17219, 17219, 17219, 17219, 16962, 16705, 16705, 16448, 16191, 15934, 15934, + 15934, 15677, 15677, 15934, 15934, 15934, 16705, 17219, 18761, 19275, 19789, 20560, + 21588, 21845, 21331, 20303, 19532, 19018, 19018, 19018, 19275, 19275, 19532, 19789, + 19789, 19789, 19789, 19789, 19275, 19018, 19018, 18761, 18504, 17990, 17476, 17219, + 16705, 16448, 16448, 16448, 16705, 16705, 17219, 17733, 18761, 20046, 21845, 23130, + 25186, 25700, 25957, 26214, 26471, 26214, 25957, 26214, 26471, 26985, 27242, 27756, + 27756, 27756, 27499, 26728, 26214, 25700, 25443, 25700, 25957, 26214, 26728, 27242, + 27756, 28270, 28784, 29041, 29298, 29298, 29298, 29298, 29298, 29298, 29555, 29555, + 29555, 28784, 28270, 27242, 26214, 25700, 24415, 23901, 23387, 22616, 22102, 21331, + 20303, 19018, 18247, 17219, 16705, 16191, 15677, 15677, 15163, 14649, 14135, 14135, + 14135, 13878, 13878, 13878, 13878, 13878, 13878, 13878, 13621, 13621, 13621, 13621, + 13878, 13878, 13878, 13878, 13878, 14135, 14135, 14392, 14392, 14649, 14649, 14392, + 14392, 14135, 14135, 14135, 14135, 13878, 13364, 12850, 12850, 12850, 12593, 12593, + 12593, 12336, 12079, 11822, 11308, 11051, 10280, 10023, 9766, 9252, 9252, 9252, + 9252, 8995, 9252, 8995, 8995, 8738, 8481, 8481, 8224, 8224, 8224, 7967, + 7967, 7967, 7967, 7967, 8224, 8481, 8481, 8738, 8995, 8995, 8995, 8995, + 8995, 8738, 8738, 8738, 8481, 7967, 7967, 7710, 7710, 7710, 7453, 7453, + 7453, 7196, 7196, 6682, 6682, 6682, 6682, 6682, 6682, 6682, 6939, 6939, + 6682, 6682, 6425, 6168, 6168, 6168, 5911, 5654, 5397, 5397, 5397, 5397, + 5397, 5397, 5654, 5911, 5911, 6168, 5911, 6168, 6425, 6425, 6682, 6682, + 6682, 6939, 6939, 6939, 6682, 6682, 6425, 6425, 6425, 6168, 5911, 5911, + 5654, 5654, 5654, 5654, 5654, 5654, 5397, 5397, 5397, 5397, 5397, 5397, + 5654, 5911, 6168, 6682, 6682, 7196, 7453, 7453, 7453, 7710, 7710, 7967, + 7967, 7967, 7967, 7967, 7967, 7967, 7967, 7967, 7710, 7453, 7196, 6939, + 6939, 6939, 6682, 6682, 6425, 6425, 6425, 6425, 6425, 6168, 6168, 6168, + 6168, 6168, 6168, 6168, 5911, 5911, 5911, 5911, 5654, 5654, 5654, 5654, + 5654, 5654, 5654, 5654, 5911, 5911, 5911, 5911, 5911, 5911, 5911, 5911, + 5911, 5911, 5911, 5654, 5654, 5397, 5397, 5397, 5397, 5397, 5397, 5397, + 5654, 5654, 5654, 5654, 5654, 5654, 5654, 5397, 5140, 4883, 4626, 4112, + 4112, 3855, 3855, 3855, 3855, 3855, 3598, 3341, 3084, 3084, 3084, 3084, + 2827, 2827, 2827, 2827, 2570, 2570, 2570, 2570, 2570, 2570, 2570, 2313, + 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, + 2313, 2056, 2056, 2056, 2056, 2056, 2313, 2313, 2313, 2313, 2313, 2313, + 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, + 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2313, 2570, + 2570, 2570, 2570, 2570, 2570, 2570, 2570, 2827, 3084, 3084, 3084, 3084, + 3084, 3084, 3084, 3341, 3341, 3084, 3341, 3084, 3084, 3341, 3341, 3341, + 3598, 3598, 3598, 3598, 3598, 3598, 3598, 3598, 3598, 3598, 3598, 3855, + 3598, 3855, 3855, 3855, 3855, 3855, 3855, 3855, 3855, 3855, 3855, 3855, + 3855, 3855, 4112, 4112, 4112, 4112, 4369, 4369, 4369, 4112, 4369, 4369, + 4369, 4626, 4626, 4626, 4626, 4883, 4883, 4883, 4883, 4883, 4883, 5140, + 5140, 5140, 5140, 5140, 5140, 5140, 5140, 5397, 5397, 5397, 5911, 5911, + 6168, 6168, 6682, 6939, 6939, 7453, 7453, 7453, 7710, 7967, 7967, 7967, + 7967, 7967, 7967, 7967, 7967, 7967, 7967, 8224, 8224, 8481, 8738, 8738, + 8995, 8995, 9252, 9252, 9509, 9509, 9766, 10023, 10280, 10280, 10280, 10794, + 11051, 11308, 11565, 11822, 12593, 12850, 13364, 13878, 13878, 14135, 14135, 14392, + 14392, 14392, 14392, 14392, 14649, 14649, 14649, 14649, 14906, 15163, 15163, 15163, + 15163, 15163, 14906, 14906, 14906, 14649, 14649, 14392, 14392, 14392, 14392, 14649, + 14649, 14906, 14906, 15163, 14906, 14649, 14392, 14135, 13878, 13878, 13878, 13878, + 13878, 13878, 13621, 13621, 13364, 13107, 13107, 12850, 12593, 12593, 12593, 12593, + 12336, 12336, 12336, 12079, 12079, 12079, 12079, 11822, 11565, 11051, 10794, 10280, + 10023, 9766, 9509, 9252, 8995, 8738, 8481, 8481, 8224, 8224, 7967, 7967, + 7967, 7967, 7710, 7453, 7196, 7196, 7196, 6939, 6682, 6425, 6168, 5911, + 5911, 5654, 5654, 5397, 5397, 5397, 5397, 5397, 5397, 5140, 5140, 5140, + 5140, 5140, 5140, 5140, 5140, 5140, 5397, 5397, 5654, 5654, 5654, 5911, + 6168, 6168, 6168, 5911, 5911, 5397, 5140, 5140, 4883, 4369, 4369, 3855, + 3855, 3598, 3084, 3084, 3084, 2827, 2827, 2827, 3084, 3084, 3341, 3855, + 4112, 4369, 4112, 3855, 3341, 3084, 2827, 2570, 2313, 2313, 2056, 2056, + 2056, 2056, 2056, 2056, 2056, 2056, 2056, 2056, +}; + +#endif /* OBPMARK_2_1_INPUT_H_ */ diff --git a/sw/tests/bare-metal/hostd/obpmark_2_2_host_seq.c b/sw/tests/bare-metal/hostd/obpmark_2_2_host_seq.c new file mode 100644 index 000000000..b62fb3740 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_2_2_host_seq.c @@ -0,0 +1,1258 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #2.2 - CCSDS 122.0 Image Compression +// Bare-metal SEQUENZIALE per Astral (1x CVA6, RV64) +// Stage 1: DWT integer (3 livelli) + coefficient scaling + coefficient +// regrouping + block_string [VALIDATO su RTL: PASS, 201460 cicli] +// Stage 2a: header di segmento + DC encoding [VALIDATO su RTL: PASS, 235680 cicli] +// Stage 2b: AC bit-plane encoding (bit_plane_encoding, stages_encoding, +// coding_options/rice_coding/gaggle_pass, ref_bit_end_encode) - +// porting fedele da src/2.2-image_compression/src/ports/cpu/processing.c +// dato dall'utente, specializzato per SEGMENT_SIZE == GAGGLE_SIZE == 16 +// (un solo gaggle per stage, nessun reminder_gaggle) e percorso integer. +// +// Porting dal riferimento CPU OBPMark: +// src/2.2-image_compression/src/ports/cpu/{processing.c,processing.h} +// src/2.2-image_compression/include/benchmark.h +// src/2.2-image_compression/include/output_format_utils.h +// +// Allineato all'ambiente bare-metal gia' usato per i benchmark #1.1/#2.1 +// (car_init_start, read_mcycle, printf.h): niente libc, buffer statici, +// nessuna allocazione dinamica (block_data / output bitstream a dimensione +// fissa, al posto di malloc/calloc e della lista concatenata +// SegmentBitStream del riferimento). Nessuna floating point / libm: sia la +// DWT (Stage 1) sia log2/ceil (Stage 2a) sono riscritte in aritmetica +// intera esatta. +// +// Naming convention: obpmark_2_2_host_seq (obpmark___). + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include +#include + +// ============================================================ +// Configurazione benchmark (compile-time) +// ============================================================ +#define IMAGE_WIDTH 32u +#define IMAGE_HEIGHT 32u +#define BIT_SIZE 16u +#define LEVELS_DWT 3u +#define BLOCKSIZEIMAGE 8u +#define SEGMENT_SIZE 16u +#define GAGGLE_SIZE 16u +#define UNCODED_VALUE 0xFFu +#define BITLL3 3u + +#define MAX_SYMBOLS_IN_BLOCK 22u +#define SIZE_TYPE 4u + +#define ENUM_NONE 0 +#define ENUM_TYPE_P 1 +#define ENUM_TYPE_TRAN_B 2 +#define ENUM_TYPE_TRAN_D 3 +#define ENUM_TYPE_CI 4 +#define ENUM_TRAN_GI 5 +#define ENUM_TRAN_HI 6 +#define ENUM_TYPE_HIJ 7 + +#define BITHH1 0 +#define BITLH1 1 +#define BITHL1 1 +#define BITHH2 1 +#define BITLH2 2 +#define BITHL2 2 +#define BITHH3 2 +#define BITHL3 3 +#define BITLH3 3 + +#define ABSOLUTE(x) ((x) < 0 ? -(x) : (x)) +#define SIGN(x) ((x) < 0 ? 1 : 0) + +static const int bit2_pattern[4] = {0, 2, 1, 3}; +static const int bit3_pattern[8] = {1, 4, 0, 5, 2, 6, 3, 7}; +static const int bit3_pattern_TranD[8] = {0, 3, 0, 4, 1, 5, 2, 6}; +static const int bit4_pattern_TypeCi[16] = {10, 1, 3, 6, 2, 5, 9, 12, 0, 8, 7, 13, 4, 14, 11, 15}; +static const int bit4_pattern_TypeHij_TranHi[16] = {0, 1, 3, 6, 2, 5, 9, 11, 0, 8, 7, 12, 4, 13, 10, 14}; + +static const int type_of_compression = 0; // 0 = integer path + +#define W_PADDED IMAGE_WIDTH +#define H_PADDED IMAGE_HEIGHT +#define TOTAL_BLOCKS ((W_PADDED / BLOCKSIZEIMAGE) * (H_PADDED / BLOCKSIZEIMAGE)) // 16 +#define NUM_SEGMENTS 1u // TOTAL_BLOCKS % SEGMENT_SIZE == 0 + +#define OUTPUT_BUFFER_BYTES 512u + +#define CVA6_CLK_PERIOD_NS 20ULL +#define CVA6_FREQ_HZ 50000000ULL + +// ============================================================ +// Cycle counter (RV64) +// ============================================================ +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +// ============================================================ +// Buffer statici Stage 1 (niente heap) +// ============================================================ +static int image_data[H_PADDED][W_PADDED]; +static int transformed_image[H_PADDED][W_PADDED]; +static int block_string[TOTAL_BLOCKS][BLOCKSIZEIMAGE * BLOCKSIZEIMAGE]; + +static int aux_row[H_PADDED][W_PADDED]; +static int aux_col_in[H_PADDED]; +static int aux_col_out[H_PADDED]; +static int regroup_tmp[H_PADDED][W_PADDED]; + +// ============================================================ +// Divisione intera con arrotondamento verso -infinito (Stage 1) +// ============================================================ +static inline int floor_div(int a, int b) { + int q = a / b; + int r = a % b; + if (r != 0 && ((r < 0) != (b < 0))) q--; + return q; +} + +// ============================================================ +// DWT 1D/2D/3-livelli reversibile a interi (Stage 1) +// ============================================================ +static void ccsds_wavelet_transform_1D_int(const int *A, int *B, int size) { + int full = 2 * size; + int i; + for (i = 0; i < size; ++i) { + int s, val; + if (i == 0) { + s = 9 * (A[0] + A[2]) - (A[2] + A[4]); + val = A[1] - floor_div(s + 8, 16); + } else if (i == size - 2) { + s = 9 * (A[full-4] + A[full-2]) - (A[full-6] + A[full-2]); + val = A[full-3] - floor_div(s + 8, 16); + } else if (i == size - 1) { + s = 9 * A[full-2] - A[full-4]; + val = A[full-1] - floor_div(s + 4, 8); + } else { + s = 9 * (A[2*i] + A[2*i+2]) - (A[2*i-2] + A[2*i+4]); + val = A[2*i+1] - floor_div(s + 8, 16); + } + B[size + i] = val; + } + for (i = 0; i < size; ++i) { + int val; + if (i == 0) + val = A[0] - floor_div(1 - B[size], 2); + else + val = A[2*i] - floor_div(2 - (B[i+size-1] + B[i+size]), 4); + B[i] = val; + } +} + +static void ccsds_wavelet_transform_2D_int(int A[H_PADDED][W_PADDED], int w, int h) { + int i, x; + for (i = 0; i < h; ++i) + ccsds_wavelet_transform_1D_int(A[i], aux_row[i], w / 2); + for (i = 0; i < w; ++i) { + for (x = 0; x < h; ++x) aux_col_in[x] = aux_row[x][i]; + ccsds_wavelet_transform_1D_int(aux_col_in, aux_col_out, h / 2); + for (x = 0; x < h; ++x) A[x][i] = aux_col_out[x]; + } +} + +static void dwt2D_compression_computation_integer(void) { + int level_size = W_PADDED; + int lvl, i, j; + for (lvl = 0; lvl < LEVELS_DWT; ++lvl) { + ccsds_wavelet_transform_2D_int(image_data, level_size, level_size); + level_size /= 2; + } + for (i = 0; i < H_PADDED; ++i) + for (j = 0; j < W_PADDED; ++j) + transformed_image[i][j] = image_data[i][j]; +} + +static void coefficient_scaling(void) { + const int hh_1 = 1, hl_lh = 2, hh_2 = 2, hl_lh_2 = 4, hh_3 = 4, hl_lh_3 = 8, ll_4 = 8; + int i, j; + for (i = H_PADDED>>1; i < H_PADDED; i++) + for (j = W_PADDED>>1; j < W_PADDED; j++) transformed_image[i][j] *= hh_1; + for (i = 0; i < H_PADDED>>1; i++) + for (j = W_PADDED>>1; j < W_PADDED; j++) transformed_image[i][j] *= hl_lh; + for (i = H_PADDED>>1; i < H_PADDED; i++) + for (j = 0; j < W_PADDED>>1; j++) transformed_image[i][j] *= hl_lh; + for (i = H_PADDED>>2; i < H_PADDED>>1; i++) + for (j = W_PADDED>>2; j < W_PADDED>>1; j++) transformed_image[i][j] *= hh_2; + for (i = 0; i < H_PADDED>>2; i++) + for (j = W_PADDED>>2; j < W_PADDED>>1; j++) transformed_image[i][j] *= hl_lh_2; + for (i = H_PADDED>>2; i < H_PADDED>>1; i++) + for (j = 0; j < W_PADDED>>2; j++) transformed_image[i][j] *= hl_lh_2; + for (i = H_PADDED>>3; i < H_PADDED>>2; i++) + for (j = W_PADDED>>3; j < W_PADDED>>2; j++) transformed_image[i][j] *= hh_3; + for (i = 0; i < H_PADDED>>3; i++) + for (j = W_PADDED>>3; j < W_PADDED>>2; j++) transformed_image[i][j] *= hl_lh_3; + for (i = H_PADDED>>3; i < H_PADDED>>2; i++) + for (j = 0; j < W_PADDED>>3; j++) transformed_image[i][j] *= hl_lh_3; + for (i = 0; i < H_PADDED>>3; i++) + for (j = 0; j < W_PADDED>>3; j++) transformed_image[i][j] *= ll_4; +} + +static void coeff_regroup(void) { + int i, j, p, k, x, y, xb; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) regroup_tmp[i][j] = 0; + + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 4; p < 8; ++p) + for (k = 4; k < 8; ++k) + regroup_tmp[x+p][y+k] = transformed_image[i+p-4][j+k-4]; + } + for (i = 0; i < H_PADDED>>1; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = i << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 0; p < 4; ++p) + for (k = 4; k < 8; ++k) + regroup_tmp[x+p][y+k] = transformed_image[i+p][j+k-4]; + } + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = 0; j < W_PADDED>>1; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = j << 1; + for (p = 4; p < 8; ++p) + for (k = 0; k < 4; ++k) + regroup_tmp[x+p][y+k] = transformed_image[i+p-4][j+k]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = (j - (W_PADDED>>2)) << 2; + regroup_tmp[x+2][y+2] = transformed_image[i][j]; + regroup_tmp[x+2][y+3] = transformed_image[i][j+1]; + regroup_tmp[x+3][y+2] = transformed_image[i+1][j]; + regroup_tmp[x+3][y+3] = transformed_image[i+1][j+1]; + } + for (i = 0; i < H_PADDED>>2; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = i << 2; y = (j - (W_PADDED>>2)) << 2; + regroup_tmp[x][y+2] = transformed_image[i][j]; + regroup_tmp[x][y+3] = transformed_image[i][j+1]; + regroup_tmp[x+1][y+2] = transformed_image[i+1][j]; + regroup_tmp[x+1][y+3] = transformed_image[i+1][j+1]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = 0; j < W_PADDED>>2; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = j << 2; + regroup_tmp[x+2][y] = transformed_image[i][j]; + regroup_tmp[x+2][y+1] = transformed_image[i][j+1]; + regroup_tmp[x+3][y] = transformed_image[i+1][j]; + regroup_tmp[x+3][y+1] = transformed_image[i+1][j+1]; + } + xb = H_PADDED>>3; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + regroup_tmp[((i-xb)<<3)+1][((j-(W_PADDED>>3))<<3)+1] = transformed_image[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + regroup_tmp[i<<3][((j-(W_PADDED>>3))<<3)+1] = transformed_image[i][j]; + xb = H_PADDED>>3; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + regroup_tmp[((i-xb)<<3)+1][j<<3] = transformed_image[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + regroup_tmp[i<<3][j<<3] = transformed_image[i][j]; + + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + transformed_image[i][j] = regroup_tmp[i][j]; +} + +static void build_block_string(void) { + int block_h = H_PADDED / BLOCKSIZEIMAGE; + int block_w = W_PADDED / BLOCKSIZEIMAGE; + int i, j, x, y, counter = 0; + for (i = 0; i < block_h; ++i) + for (j = 0; j < block_w; ++j) { + for (x = 0; x < BLOCKSIZEIMAGE; ++x) + for (y = 0; y < BLOCKSIZEIMAGE; ++y) + block_string[counter][x*BLOCKSIZEIMAGE + y] = + transformed_image[i*BLOCKSIZEIMAGE + x][j*BLOCKSIZEIMAGE + y]; + ++counter; + } +} + +static void generate_input(void) { + unsigned int i, j; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + image_data[i][j] = (int)((i * 173u + j * 91u + 13u) & 0xFFFFu); +} + +// ============================================================ +// Output bitstream statico (sostituisce la lista concatenata +// SegmentBitStream del riferimento). Scrittura MSB-first. +// ============================================================ +typedef struct { + uint8_t OutputBitStream[OUTPUT_BUFFER_BYTES]; + unsigned int num_bits; + unsigned int num_total_bytes; +} OutputBitStream_t; + +static OutputBitStream_t output_stream; + +static void write_to_the_output_segment(long word, unsigned int length) { + int i; + for (i = (int)length - 1; i >= 0; --i) { + output_stream.OutputBitStream[output_stream.num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1) << (7 - output_stream.num_bits)); + output_stream.num_bits++; + if (output_stream.num_bits >= 8u) { + output_stream.num_bits = 0u; + output_stream.num_total_bytes++; + } + } +} + +// ============================================================ +// ceil(log2(x)) in aritmetica intera esatta (Stage 2a, no libm) +// ============================================================ +static unsigned int ceil_log2(unsigned long x) { + unsigned int r = 0; + unsigned long v = 1; + while (v < x) { v <<= 1; r++; } + return r; +} + +static unsigned int bit_size_with_pow2_adjust(long v) { + unsigned long av = (unsigned long)(v < 0 ? -v : v); + if (av == 0) return 0; + unsigned int b = ceil_log2(av); + if ((av & (av - 1)) == 0) b += 1; + return b; +} + +static unsigned long conv_to_twos_complement(int value, unsigned int num_bits) { + unsigned long temp, complement; + short i; + if (num_bits == 1) return 0; + if (value >= 0) return (unsigned long) value; + complement = ~(unsigned long)(-value); + temp = 0; + for (i = 0; i < (short)num_bits; i++) { temp <<= 1; temp++; } + complement &= temp; + complement++; + return complement; +} + +// ============================================================ +// Strutture Stage 2a/2b +// ============================================================ +typedef struct { + unsigned int symbol_val; + unsigned int symbol_len; + unsigned int symbol_mapped_pattern; + unsigned int sign; + unsigned int type; +} str_symbol_details_t; + +typedef struct { + long shifted_dc; + unsigned int dc_reminder; + unsigned long mapped_dc; + long max_ac_bit_size; + unsigned long mapped_ac; + unsigned int type_p; + unsigned int tran_b; + unsigned int tran_d; + unsigned int tran_gi; + unsigned int parent_ref_symbol; + unsigned int parent_sym_len; + unsigned int children_ref_symbol; + unsigned int children_sym_len; + unsigned int type_ci[3]; + unsigned int tran_hi[3]; + unsigned int grand_children_ref_symbol[3]; + unsigned int grand_children_sym_len[3]; + unsigned int type_hi[12]; + str_symbol_details_t symbol_block[MAX_SYMBOLS_IN_BLOCK]; +} block_data_t; + +typedef struct { + int start_img_flag; + int end_img_flag; + unsigned int segment_count; + unsigned int bit_depth_dc; + unsigned int bit_depth_ac; + int part_2_flag; + int part_3_flag; + int part_4_flag; + unsigned int pad_rows; +} header_data_t; + +static block_data_t block_data[SEGMENT_SIZE]; +static header_data_t header_data; + +// ============================================================ +// header_output +// ============================================================ +static void header_output(void) { + write_to_the_output_segment(header_data.start_img_flag, 1); + write_to_the_output_segment(header_data.end_img_flag, 1); + write_to_the_output_segment(header_data.segment_count, 8); + write_to_the_output_segment(header_data.bit_depth_dc, 5); + write_to_the_output_segment(header_data.bit_depth_ac, 5); + write_to_the_output_segment(0, 1); // RESERVED + write_to_the_output_segment(header_data.part_2_flag, 1); + write_to_the_output_segment(header_data.part_3_flag, 1); + write_to_the_output_segment(header_data.part_4_flag, 1); + if (header_data.end_img_flag) { + write_to_the_output_segment(header_data.pad_rows, 3); + write_to_the_output_segment(0, 5); // RESERVED + } +} + +// ============================================================ +// coding_quantized_coefficients: sign-extend shifted_dc a N bit, poi +// DPCM + folding -> mapped_dc +// ============================================================ +static void coding_quantized_coefficients(unsigned int N) { + long x_min = -(1L << (N - 1)); + long x_max = (1L << (N - 1)) - 1; + unsigned int number_bits = 0, i; + long theta, gamma; + + for (i = 0; i < N - 1; i++) number_bits = (number_bits << 1) + 1; + + if ((block_data[0].shifted_dc & (1L << (N - 1))) > 0) + block_data[0].shifted_dc = -(long)(((block_data[0].shifted_dc ^ number_bits) & number_bits) + 1); + block_data[0].mapped_dc = (unsigned long) block_data[0].shifted_dc; + + for (i = 1; i < SEGMENT_SIZE; i++) { + if ((block_data[i].shifted_dc & (1L << (N - 1))) > 0) + block_data[i].shifted_dc = -(long)(((block_data[i].shifted_dc ^ number_bits) & number_bits) + 1); + + theta = (block_data[i-1].shifted_dc - x_min) < (x_max - block_data[i-1].shifted_dc) + ? (block_data[i-1].shifted_dc - x_min) : (x_max - block_data[i-1].shifted_dc); + gamma = block_data[i].shifted_dc - block_data[i-1].shifted_dc; + + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_dc = (unsigned long)(2 * gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_dc = (unsigned long)(2 * (-gamma) - 1); + else + block_data[i].mapped_dc = (unsigned long)(theta + (gamma < 0 ? -gamma : gamma)); + } +} + +// ============================================================ +// dc_encoder: gaggle coding DC (brute-force, 1 solo gaggle) +// ============================================================ +static void dc_encoder(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + unsigned int total_bits, min_bits = 0xFFFFu; + unsigned int k, i; + + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position > 1 ? start_position : 1); i < end_position; i++) + total_bits += (unsigned int)((block_data[i].mapped_dc >> k) + 1) + k; + if (total_bits < min_bits && total_bits < N * GAGGLE_SIZE) { + min_bits = total_bits; + encode_selection = k; + } + } + + write_to_the_output_segment((long)encode_selection, id_length); + + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_dc, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_dc >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position > 1 ? start_position : 1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_dc, encode_selection); + } +} + +static void dc_entropy_coding(unsigned int N, unsigned int quantization_factor) { + unsigned int id_length = 0, max_k = 0, i; + + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 8) { max_k = 6; id_length = 3; } + else { max_k = 8; id_length = 4; } + + dc_encoder(max_k, id_length, N); + + if (header_data.bit_depth_ac < quantization_factor) { + unsigned int num_additional_bit_planes = quantization_factor - header_data.bit_depth_ac; + for (i = 0; i < num_additional_bit_planes; i++) { + unsigned int kk; + for (kk = 0; kk < SEGMENT_SIZE; kk++) + write_to_the_output_segment( + (block_data[kk].dc_reminder >> (quantization_factor - i - 1)), 1); + } + } +} + +// ============================================================ +// dc_encoding: quantizzazione DC + calcolo bit_depth_ac preliminare +// ============================================================ +static unsigned int dc_encoding(void) { + long max_ac_value = 0; + unsigned int quantization_factor; + unsigned int i, j, k_mask; + + header_data.bit_depth_dc = 0; + + for (i = 0; i < SEGMENT_SIZE; ++i) { + long max_ac_value_block = 0; + int dc_value = block_string[i][0]; + unsigned int dc_bit_size; + + if (dc_value < 0) + dc_bit_size = ceil_log2((unsigned long)(-dc_value) + 1UL); + else + dc_bit_size = ceil_log2((unsigned long)(1 + dc_value)) + 1; + + if (dc_bit_size > header_data.bit_depth_dc) header_data.bit_depth_dc = dc_bit_size; + + for (j = 1; j < BLOCKSIZEIMAGE * BLOCKSIZEIMAGE; ++j) { + long v = block_string[i][j]; + long av = v < 0 ? -v : v; + if (av > (max_ac_value < 0 ? -max_ac_value : max_ac_value)) max_ac_value = v; + if (av > (max_ac_value_block < 0 ? -max_ac_value_block : max_ac_value_block)) max_ac_value_block = v; + } + block_data[i].max_ac_bit_size = (long) bit_size_with_pow2_adjust(max_ac_value_block); + } + + header_data.bit_depth_ac = bit_size_with_pow2_adjust(max_ac_value); + + header_output(); + + if (header_data.bit_depth_dc <= 3) + quantization_factor = 0; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) <= 1) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 3; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) > 10) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 10; + else + quantization_factor = 1 + (header_data.bit_depth_ac >> 1); + + k_mask = (1u << quantization_factor) - 1u; + for (i = 0; i < SEGMENT_SIZE; ++i) { + unsigned long new_value_twos = conv_to_twos_complement(block_string[i][0], header_data.bit_depth_dc); + block_data[i].shifted_dc = (long)(new_value_twos >> quantization_factor); + block_data[i].dc_reminder = (unsigned int)(new_value_twos & k_mask); + } + + { + unsigned int N = header_data.bit_depth_dc > quantization_factor + ? header_data.bit_depth_dc - quantization_factor : 1; + if (N == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].shifted_dc, 1); + } else { + coding_quantized_coefficients(N); + dc_entropy_coding(N, quantization_factor); + } + } + return quantization_factor; +} + +// ============================================================ +// Stage 2b: AC bit-plane encoding +// ============================================================ +static void dpcm_ac_mapper(unsigned int N) { + long x_min = 0; + long x_max = (1L << N) - 1; + long theta, gamma; + unsigned int i; + + block_data[0].mapped_ac = (unsigned long) block_data[0].max_ac_bit_size; + for (i = 1; i < SEGMENT_SIZE; i++) { + theta = (block_data[i-1].max_ac_bit_size - x_min) < (x_max - block_data[i-1].max_ac_bit_size) + ? (block_data[i-1].max_ac_bit_size - x_min) : (x_max - block_data[i-1].max_ac_bit_size); + gamma = block_data[i].max_ac_bit_size - block_data[i-1].max_ac_bit_size; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_ac = (unsigned long)(2*gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_ac = (unsigned long)(2*(-gamma) - 1); + else + block_data[i].mapped_ac = (unsigned long)(theta + (gamma<0?-gamma:gamma)); + } +} + +static void ac_gaggle_encode(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + long total_bits, min_bits = 0xFFFF; + unsigned int k, i; + + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position>1?start_position:1); i < end_position; i++) + total_bits += (long)((block_data[i].mapped_ac >> k) + 1) + k; + if (total_bits < min_bits && total_bits < (long)(N * GAGGLE_SIZE)) { + min_bits = total_bits; + encode_selection = k; + } + } + + write_to_the_output_segment((long)encode_selection, id_length); + + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_ac, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_ac >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position>1?start_position:1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_ac, encode_selection); + } +} + +static void ac_depth_encoder(void) { + unsigned int N = 0, id_length, max_k; + while ((header_data.bit_depth_ac >> N) > 0) N++; + dpcm_ac_mapper(N); + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 5) { max_k = 6; id_length = 3; } + else { max_k = 0; id_length = 0; } + ac_gaggle_encode(max_k, id_length, N); +} + +static void bit_plane_encoding(unsigned int bit_plane_number) { + unsigned int temp_x, temp_y, block_num; + unsigned char symbol; + int temp_value; + unsigned int final_position; + int bit_set_plane = (1 << (bit_plane_number - 1)); + + for (block_num = 0; block_num < SEGMENT_SIZE; ++block_num) { + symbol = 0; + final_position = block_num; + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + + for (int j = 0; j < 3; ++j) { + switch (j) { + case 0: temp_y = 1; break; + case 1: temp_y = BLOCKSIZEIMAGE; break; + case 2: temp_y = BLOCKSIZEIMAGE + 1; break; + default: temp_y = 0; break; + } + temp_x = final_position; + if (!type_of_compression) { + if (((j==0)&&(BITHL3>=bit_plane_number)) || ((j==1)&&(BITLH3>=bit_plane_number)) || ((j==2)&&(BITHH3>=bit_plane_number))) + continue; + } + if ((block_data[block_num].type_p & (1 << (2-j))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_P; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + if (ABSOLUTE(block_string[temp_x][temp_y]) >= (1 << (bit_plane_number-1)) && + ABSOLUTE(block_string[temp_x][temp_y]) < (1 << bit_plane_number)) { + block_data[block_num].type_p += (1 << (2-j)); + block_data[block_num].symbol_block[symbol].symbol_val += 1; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[temp_x][temp_y]); + } + } else { + temp_value = (ABSOLUTE(block_string[temp_x][temp_y]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((j==0)&&(BITHL3=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = true; break; + } + if (break_flag) break; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = true; break; + } + if (break_flag) break; + } + } + + if (block_data[block_num].tran_b == 0) { + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 0; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) ++symbol; + + if (block_data[block_num].tran_b == 1) { + for (int k = 0; k < 3; k++) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)&&(BITHL1>=bit_plane_number)) + || ((k==1)&&(BITLH2>=bit_plane_number)&&(BITLH1>=bit_plane_number)) + || ((k==2)&&(BITHH2>=bit_plane_number)&&(BITHH1>=bit_plane_number))) + continue; + } + bool break_flag = false; + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + bool jump_flag = false; + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + jump_flag = true; + } + if (!jump_flag) { + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = true; break; + } + } + if (!break_flag) { + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = true; break; + } + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + } + + if (block_data[block_num].tran_d == 0) { + // porzione senza AC significativi in questo bit-plane per questo blocco + } + + for (unsigned int k = 0; k < 3; ++k) { + if ((block_data[block_num].tran_d & (1 << (2-k))) != 0) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + int p = 1; + for (unsigned int i = 0; i < 4; ++i) + if ((block_data[block_num].type_ci[k] & (1 << (3-i))) == 0) { p = 0; break; } + if (p == 0) { + unsigned int counter = 0; + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_CI; + for (unsigned int i = temp_x; i < temp_x+2; ++i) { + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) { + if ((block_data[block_num].type_ci[k] & (1 << (3-counter))) == 0) { + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].type_ci[k] += (1 << (3-counter)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (((block_data[block_num].tran_d & (1 << (2-k))) != 0) && ((block_data[block_num].tran_gi & (1 << (2-k))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_GI; + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + bool break_flag = false; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].tran_gi += (1 << (2-k)); + break_flag = true; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if (((block_data[block_num].tran_gi & (1 << (2-i3))) != 0) && ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_HI; + bool break_flag = false; + for (unsigned int k = temp_x; k < temp_x+2 && !break_flag; k++) + for (unsigned int p = temp_y; p < temp_y+2; p++) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].tran_hi[i3] += (1 << (3-jx)); + break_flag = true; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) != 0) { + unsigned int counter = 0; + for (unsigned int k = 0; k < 4; ++k) + if ((block_data[block_num].type_hi[i3*SIZE_TYPE+jx] & (1 << (3-k))) == 0) counter++; + + if (counter == 0) { + for (unsigned int k = temp_x; k < temp_x+2; k++) { + for (unsigned int p = temp_y; p < temp_y+2; p++) { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1 0) { + block_data[block_num].type_hi[i3*SIZE_TYPE+jx] += (1 << (3-t)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1symbol_len) { + case 0: return; + case 1: s->symbol_mapped_pattern = s->symbol_val; break; + case 2: s->symbol_mapped_pattern = bit2_pattern[s->symbol_val]; break; + case 3: + s->symbol_mapped_pattern = (s->type == ENUM_TYPE_TRAN_D) ? bit3_pattern_TranD[s->symbol_val] : bit3_pattern[s->symbol_val]; + break; + case 4: + if (s->type == ENUM_TYPE_CI) s->symbol_mapped_pattern = bit4_pattern_TypeCi[s->symbol_val]; + else if (s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ) s->symbol_mapped_pattern = bit4_pattern_TypeHij_TranHi[s->symbol_val]; + break; + default: break; + } +} + +static void coding_options(unsigned int bit_plane_number, unsigned char *code_option_gaggle) { + unsigned int bit_counter_2bits[2] = {0}; + unsigned int bit_counter_3bits[3] = {0}; + unsigned int bit_counter_4bits[4] = {0}; + unsigned int block_num, symbol_id; + + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + if (s->type == ENUM_NONE) continue; + if (s->symbol_len == 1) { pattern_mapping(s); continue; } + pattern_mapping(s); + if (s->symbol_len == 2) { + switch (s->symbol_mapped_pattern) { + case 0: bit_counter_2bits[0] += 1; break; + case 1: bit_counter_2bits[0] += 2; break; + default: bit_counter_2bits[0] += 3; break; + } + bit_counter_2bits[1] += 2; + } else if (s->symbol_len == 3) { + if (s->symbol_mapped_pattern <= 2) bit_counter_3bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 5) bit_counter_3bits[0] += 5; + else bit_counter_3bits[0] += 6; + if (s->symbol_mapped_pattern <= 1) bit_counter_3bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_3bits[1] += 3; + else bit_counter_3bits[1] += 4; + bit_counter_3bits[2] += 3; + } else if (s->symbol_len == 4) { + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[0] += 7; + else bit_counter_4bits[0] += 8; + if (s->symbol_mapped_pattern <= 1) bit_counter_4bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[1] += 3; + else if (s->symbol_mapped_pattern <= 5) bit_counter_4bits[1] += 4; + else if (s->symbol_mapped_pattern <= 11) bit_counter_4bits[1] += 6; + else bit_counter_4bits[1] += 7; + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[2] += 3; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[2] += 4; + else bit_counter_4bits[2] += 5; + bit_counter_4bits[3] += 4; + } + } + } + code_option_gaggle[0] = (bit_counter_2bits[0] < bit_counter_2bits[1]) ? 0 : 1; + if (bit_counter_3bits[2] <= bit_counter_3bits[0] && bit_counter_3bits[2] <= bit_counter_3bits[1]) code_option_gaggle[1] = 3; + else if (bit_counter_3bits[0] <= bit_counter_3bits[1]) code_option_gaggle[1] = 0; + else code_option_gaggle[1] = 1; + if (bit_counter_4bits[3] <= bit_counter_4bits[1] && bit_counter_4bits[3] <= bit_counter_4bits[0] && bit_counter_4bits[3] <= bit_counter_4bits[2]) code_option_gaggle[2] = 3; + else if (bit_counter_4bits[0] <= bit_counter_4bits[1] && bit_counter_4bits[0] <= bit_counter_4bits[2]) code_option_gaggle[2] = 0; + else if (bit_counter_4bits[1] <= bit_counter_4bits[2]) code_option_gaggle[2] = 1; + else code_option_gaggle[2] = 2; +} + +static void rice_coding(long input_value, unsigned int bit_length, unsigned char *code_option) { + switch (bit_length) { + case 0: return; + case 1: write_to_the_output_segment(input_value, 1); break; + case 2: + if (code_option[0] == 1) write_to_the_output_segment(input_value, 2); + else { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1, 1); } + else write_to_the_output_segment(0, 3); + } + break; + case 3: + if (code_option[1] == 0) { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 5) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-3,2); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,3); } + } else if (code_option[1] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else { + write_to_the_output_segment(0,2); + switch (input_value) { + case 4: write_to_the_output_segment(2,2); break; + case 5: write_to_the_output_segment(3,2); break; + case 6: write_to_the_output_segment(0,2); break; + case 7: write_to_the_output_segment(1,2); break; + } + } + } else write_to_the_output_segment(input_value, 3); + break; + case 4: + if (code_option[2] == 0) { + if (input_value <= 3) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 7) { write_to_the_output_segment(0,5); write_to_the_output_segment(input_value-4,2); } + else { write_to_the_output_segment(0,4); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else if (input_value <= 5) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-2,2); } + else if (input_value <= 11) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-6,3); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 2) { + if (input_value <= 3) write_to_the_output_segment(input_value+4, 3); + else if (input_value <= 7) write_to_the_output_segment(input_value, 4); + else if (input_value <= 11) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-4,3); } + else write_to_the_output_segment(input_value-12, 5); + } else write_to_the_output_segment(input_value, 4); + break; + } +} + +static void gaggle_pass(unsigned int bit_plane_number, unsigned char *code_option_gaggle, bool *hit_flag, int pass) { + unsigned int block_num, symbol_id; + (void) bit_plane_number; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + int match = (pass == 1 && s->type == ENUM_TYPE_P) + || (pass == 2 && (s->type == ENUM_TYPE_TRAN_B || s->type == ENUM_TYPE_TRAN_D || s->type == ENUM_TYPE_CI)) + || (pass == 3 && (s->type == ENUM_TRAN_GI || s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ)); + if (!match) continue; + if (s->symbol_len > 1 && !hit_flag[s->symbol_len - 2]) { + hit_flag[s->symbol_len - 2] = true; + if (s->symbol_len == 2) write_to_the_output_segment(code_option_gaggle[0], 1); + else if (s->symbol_len == 3) write_to_the_output_segment(code_option_gaggle[1], 2); + else if (s->symbol_len == 4) write_to_the_output_segment(code_option_gaggle[2], 2); + } + rice_coding(s->symbol_mapped_pattern, s->symbol_len, code_option_gaggle); + if (s->type == ENUM_TYPE_CI || s->type == ENUM_TYPE_HIJ) { + unsigned int counter = 0, i; + for (i = 0; i < s->symbol_len; ++i) if ((s->symbol_val & (1 << i)) > 0) counter++; + write_to_the_output_segment(s->sign, counter); + } + s->symbol_val = 0; s->symbol_len = 0; s->symbol_mapped_pattern = 0; s->sign = 0; s->type = 0; + } + } +} + +static void ref_bit_end_encode(void) { + unsigned int block_num, i; + for (block_num = 0; block_num < SEGMENT_SIZE; ++block_num) { + if (block_data[block_num].parent_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].parent_ref_symbol, block_data[block_num].parent_sym_len); + block_data[block_num].parent_ref_symbol = 0; block_data[block_num].parent_sym_len = 0; + } + if (block_data[block_num].children_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].children_ref_symbol, block_data[block_num].children_sym_len); + block_data[block_num].children_ref_symbol = 0; block_data[block_num].children_sym_len = 0; + } + for (i = 0; i < 3; ++i) { + if (block_data[block_num].grand_children_sym_len[i] > 0) + write_to_the_output_segment(block_data[block_num].grand_children_ref_symbol[i], block_data[block_num].grand_children_sym_len[i]); + block_data[block_num].grand_children_sym_len[i] = 0; + block_data[block_num].grand_children_ref_symbol[i] = 0; + } + } +} + +static void stages_encoding(unsigned int bit_plane_number) { + unsigned char code_option_gaggle[3] = {0,0,0}; + bool hit_flag[3] = {false,false,false}; + coding_options(bit_plane_number, code_option_gaggle); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 1); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 2); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 3); + ref_bit_end_encode(); +} + +static void ac_encoding(unsigned int quantization_factor) { + unsigned int bit_plane, i; + if (header_data.bit_depth_ac == 0) return; + + if (header_data.bit_depth_ac == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].max_ac_bit_size, 1); + } else { + ac_depth_encoder(); + } + + for (bit_plane = header_data.bit_depth_ac; bit_plane > 0; bit_plane--) { + if ((bit_plane <= quantization_factor) && + (type_of_compression || (quantization_factor > BITLL3 && BITLL3 < bit_plane))) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment((block_data[i].dc_reminder >> (bit_plane - 1)) & 0x01, 1); + } + bit_plane_encoding(bit_plane); + stages_encoding(bit_plane); + } +} + +static void print_results(uint64_t cycles, unsigned int checksum) { + uint64_t time_ns = cycles * CVA6_CLK_PERIOD_NS; + uint64_t time_us = time_ns / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + printf("Benchmark metrics (sequenziale, 1x CVA6):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n", + (unsigned long long)time_ms_int, (unsigned long long)time_ms_frac); + printf(" bit_depth_dc : %u\r\n", header_data.bit_depth_dc); + printf(" bit_depth_ac : %u\r\n", header_data.bit_depth_ac); + printf(" Output bytes : %u\r\n", output_stream.num_total_bytes); + printf(" Checksum : %u\r\n", checksum); +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + unsigned int checksum, i, qf; + uint64_t t_start, t_end; + + car_init_start(); + + printf("OBPMark #2.2 - CCSDS 122.0 Image Compression (Stage 2b: header+DC+AC)\r\n"); + printf("Bare-metal sequenziale - Astral CVA6 RV64 @ 50 MHz\r\n"); + printf("w_size=%u h_size=%u bit_size=%u segment_size=%u\r\n", + IMAGE_WIDTH, IMAGE_HEIGHT, BIT_SIZE, SEGMENT_SIZE); + + generate_input(); + + for (i = 0; i < OUTPUT_BUFFER_BYTES; ++i) output_stream.OutputBitStream[i] = 0u; + output_stream.num_bits = 0u; + output_stream.num_total_bytes = 0u; + + header_data.start_img_flag = 1; + header_data.end_img_flag = 1; + header_data.segment_count = 0; + header_data.part_2_flag = 0; + header_data.part_3_flag = 0; + header_data.part_4_flag = 0; + header_data.pad_rows = 0; + + t_start = read_mcycle(); + dwt2D_compression_computation_integer(); + coefficient_scaling(); + coeff_regroup(); + build_block_string(); + qf = dc_encoding(); + ac_encoding(qf); + t_end = read_mcycle(); + + checksum = 0u; + for (i = 0; i < output_stream.num_total_bytes + 1u; ++i) + checksum += output_stream.OutputBitStream[i]; + + printf("Done.\r\n"); + print_results(t_end - t_start, checksum); + printf("Output[0]: %u\r\n", (unsigned int)output_stream.OutputBitStream[0]); + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_2_2_host_seq_float.c b/sw/tests/bare-metal/hostd/obpmark_2_2_host_seq_float.c new file mode 100644 index 000000000..600f08672 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_2_2_host_seq_float.c @@ -0,0 +1,1212 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #2.2 - CCSDS 122.0 Image Compression - percorso FLOAT +// Bare-metal SEQUENZIALE per Astral (1x CVA6, RV64) +// +// Porting dal riferimento CPU OBPMark (type_of_compression = true): +// src/2.2-image_compression/src/ports/cpu/processing.c +// ccsds_wavelet_transform_1D(const float*, float*, int) +// ccsds_wavelet_transform_2D(float**, int, int) +// dwt2D_compression_computation_float() +// src/2.2-image_compression/include/benchmark.h +// lowpass_filter_cpu[9], highpass_filter_cpu[7] (valori verbatim) +// +// Differenze rispetto al percorso integer (obpmark_2_2_host_seq.c): +// - DWT 9/7 irreversibile in singola precisione (convoluzione con +// estensione simmetrica), 3 livelli senza arrotondamenti intermedi, +// arrotondamento finale a intero half-away-from-zero; +// - nessun coefficient_scaling (chiamato solo nel ramo integer); +// - type_of_compression = 1: nel BPE tutte le sottobande partecipano a +// tutti i piani di bit (nessuna esclusione BITxx) e i bit residui del +// DC sono emessi per ogni bit_plane <= quantization_factor. +// +// Fedelta' numerica: +// - il riferimento arrotonda con (int)(x + 0.5), dove 0.5 e' double: +// la somma e' esatta. round_half_away() riproduce lo stesso risultato +// in sola singola precisione (x - trunc(x) e' esatto in float), +// quindi e' portabile anche sul cluster privo di FPU double; +// - fp-contract=off: nessuna fusione moltiplicazione-somma (fmadd.s), +// valutazione IEEE operazione per operazione come nel riferimento. +// +// Nessun valore atteso noto a priori: questa variante fissa il +// riferimento per SMP host e cluster. +// +// Naming convention: obpmark_2_2_host_seq_float + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include +#include + +#pragma GCC optimize ("fp-contract=off") + +// ============================================================ +// Configurazione benchmark (compile-time) +// ============================================================ +#define IMAGE_WIDTH 32u +#define IMAGE_HEIGHT 32u +#define BIT_SIZE 16u +#define LEVELS_DWT 3u +#define BLOCKSIZEIMAGE 8u +#define SEGMENT_SIZE 16u +#define GAGGLE_SIZE 16u +#define UNCODED_VALUE 0xFFu +#define BITLL3 3u + +#define MAX_SYMBOLS_IN_BLOCK 22u +#define SIZE_TYPE 4u + +#define ENUM_NONE 0 +#define ENUM_TYPE_P 1 +#define ENUM_TYPE_TRAN_B 2 +#define ENUM_TYPE_TRAN_D 3 +#define ENUM_TYPE_CI 4 +#define ENUM_TRAN_GI 5 +#define ENUM_TRAN_HI 6 +#define ENUM_TYPE_HIJ 7 + +#define BITHH1 0 +#define BITLH1 1 +#define BITHL1 1 +#define BITHH2 1 +#define BITLH2 2 +#define BITHL2 2 +#define BITHH3 2 +#define BITHL3 3 +#define BITLH3 3 + +#define ABSOLUTE(x) ((x) < 0 ? -(x) : (x)) +#define SIGN(x) ((x) < 0 ? 1 : 0) + +#define HIGHPASSFILTERSIZE 7 +#define LOWPASSFILTERSIZE 9 + +// verbatim da src/2.2-image_compression/include/benchmark.h +static const float lowpass_filter_cpu[LOWPASSFILTERSIZE] = {0.037828455507,-0.023849465020,-0.110624404418,0.377402855613, 0.852698679009,0.377402855613, -0.110624404418,-0.023849465020, 0.037828455507}; +static const float highpass_filter_cpu[HIGHPASSFILTERSIZE] = {-0.064538882629, 0.040689417609, 0.418092273222,-0.788485616406,0.418092273222,0.040689417609,-0.064538882629}; + +static const int bit2_pattern[4] = {0, 2, 1, 3}; +static const int bit3_pattern[8] = {1, 4, 0, 5, 2, 6, 3, 7}; +static const int bit3_pattern_TranD[8] = {0, 3, 0, 4, 1, 5, 2, 6}; +static const int bit4_pattern_TypeCi[16] = {10, 1, 3, 6, 2, 5, 9, 12, 0, 8, 7, 13, 4, 14, 11, 15}; +static const int bit4_pattern_TypeHij_TranHi[16] = {0, 1, 3, 6, 2, 5, 9, 11, 0, 8, 7, 12, 4, 13, 10, 14}; + +static const int type_of_compression = 1; // 1 = float path + +#define W_PADDED IMAGE_WIDTH +#define H_PADDED IMAGE_HEIGHT +#define TOTAL_BLOCKS ((W_PADDED / BLOCKSIZEIMAGE) * (H_PADDED / BLOCKSIZEIMAGE)) // 16 + +#define OUTPUT_BUFFER_BYTES 8192u + +#define CVA6_CLK_PERIOD_NS 20ULL + +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +// ============================================================ +// Buffer statici (niente heap) +// ============================================================ +static int image_data[H_PADDED][W_PADDED]; +static int transformed_image[H_PADDED][W_PADDED]; +static int block_string[TOTAL_BLOCKS][BLOCKSIZEIMAGE * BLOCKSIZEIMAGE]; +static int regroup_tmp[H_PADDED][W_PADDED]; + +static float img_f[H_PADDED][W_PADDED]; // aux_data del riferimento +static float row_f[H_PADDED][W_PADDED]; // auxiliar (uscita row-pass) +static float col_in_f[H_PADDED]; +static float col_out_f[H_PADDED]; + +// ============================================================ +// DWT 1D float (CCSDS 122.0 9/7 irreversibile), fedele al riferimento: +// A ha 2*size elementi, B[0..size-1] passa-basso, B[size..] passa-alto +// ============================================================ +static void ccsds_wavelet_transform_1D_float(const float *A, float *B, int size) { + int full_size = size * 2; + int hi_start = -(LOWPASSFILTERSIZE / 2); + int hi_end = LOWPASSFILTERSIZE / 2; + int gi_start = -(HIGHPASSFILTERSIZE / 2); + int gi_end = HIGHPASSFILTERSIZE / 2; + int i; + + for (i = 0; i < size; ++i) { + float sum_value_low = 0.0f; + for (int hi = hi_start; hi < hi_end + 1; ++hi) { + int x_position = (2 * i) + hi; + if (x_position < 0) + x_position = -x_position; + else if (x_position > full_size - 1) + x_position = full_size - 1 - (x_position - (full_size - 1)); + sum_value_low += lowpass_filter_cpu[hi + hi_end] * A[x_position]; + } + B[i] = sum_value_low; + + float sum_value_high = 0.0f; + for (int gi = gi_start; gi < gi_end + 1; ++gi) { + int x_position = (2 * i) + gi + 1; + if (x_position < 0) + x_position = -x_position; + else if (x_position > full_size - 1) + x_position = full_size - 1 - (x_position - (full_size - 1)); + sum_value_high += highpass_filter_cpu[gi + gi_end] * A[x_position]; + } + B[i + size] = sum_value_high; + } +} + +// DWT 2D float sul sotto-blocco w x h in alto a sinistra di img_f +static void ccsds_wavelet_transform_2D_float(int w, int h) { + int i, x; + for (i = 0; i < h; ++i) + ccsds_wavelet_transform_1D_float(img_f[i], row_f[i], w / 2); + for (i = 0; i < w; ++i) { + for (x = 0; x < h; ++x) col_in_f[x] = row_f[x][i]; + ccsds_wavelet_transform_1D_float(col_in_f, col_out_f, h / 2); + for (x = 0; x < h; ++x) img_f[x][i] = col_out_f[x]; + } +} + +// Arrotondamento identico a (int)((double)x + 0.5) / (int)((double)x - 0.5) +// del riferimento, calcolato solo in singola precisione. +static inline int round_half_away(float x) { + int t = (int)x; // troncamento verso zero + float f = x - (float)t; // esatto in float + if (x >= 0.0f) return t + (f >= 0.5f ? 1 : 0); + else return t - (f <= -0.5f ? 1 : 0); +} + +// Equivalente di dwt2D_compression_computation_float() +static void dwt2D_compression_computation_float(void) { + int level_size = W_PADDED; // immagine quadrata + int lvl, i, j; + + for (i = 0; i < H_PADDED; ++i) + for (j = 0; j < W_PADDED; ++j) + img_f[i][j] = (float)image_data[i][j]; + + for (lvl = 0; lvl < (int)LEVELS_DWT; ++lvl) { + ccsds_wavelet_transform_2D_float(level_size, level_size); + level_size /= 2; + } + + for (i = 0; i < H_PADDED; ++i) + for (j = 0; j < W_PADDED; ++j) + transformed_image[i][j] = round_half_away(img_f[i][j]); +} + +// ============================================================ +// Riorganizzazione dei coefficienti (invariata rispetto all'integer) +// ============================================================ +static void coeff_regroup(void) { + int i, j, p, k, x, y, xb; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) regroup_tmp[i][j] = 0; + + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 4; p < 8; ++p) + for (k = 4; k < 8; ++k) + regroup_tmp[x+p][y+k] = transformed_image[i+p-4][j+k-4]; + } + for (i = 0; i < H_PADDED>>1; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = i << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 0; p < 4; ++p) + for (k = 4; k < 8; ++k) + regroup_tmp[x+p][y+k] = transformed_image[i+p][j+k-4]; + } + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = 0; j < W_PADDED>>1; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = j << 1; + for (p = 4; p < 8; ++p) + for (k = 0; k < 4; ++k) + regroup_tmp[x+p][y+k] = transformed_image[i+p-4][j+k]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = (j - (W_PADDED>>2)) << 2; + regroup_tmp[x+2][y+2] = transformed_image[i][j]; + regroup_tmp[x+2][y+3] = transformed_image[i][j+1]; + regroup_tmp[x+3][y+2] = transformed_image[i+1][j]; + regroup_tmp[x+3][y+3] = transformed_image[i+1][j+1]; + } + for (i = 0; i < H_PADDED>>2; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = i << 2; y = (j - (W_PADDED>>2)) << 2; + regroup_tmp[x][y+2] = transformed_image[i][j]; + regroup_tmp[x][y+3] = transformed_image[i][j+1]; + regroup_tmp[x+1][y+2] = transformed_image[i+1][j]; + regroup_tmp[x+1][y+3] = transformed_image[i+1][j+1]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = 0; j < W_PADDED>>2; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = j << 2; + regroup_tmp[x+2][y] = transformed_image[i][j]; + regroup_tmp[x+2][y+1] = transformed_image[i][j+1]; + regroup_tmp[x+3][y] = transformed_image[i+1][j]; + regroup_tmp[x+3][y+1] = transformed_image[i+1][j+1]; + } + xb = H_PADDED>>3; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + regroup_tmp[((i-xb)<<3)+1][((j-(W_PADDED>>3))<<3)+1] = transformed_image[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + regroup_tmp[i<<3][((j-(W_PADDED>>3))<<3)+1] = transformed_image[i][j]; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + regroup_tmp[((i-xb)<<3)+1][j<<3] = transformed_image[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + regroup_tmp[i<<3][j<<3] = transformed_image[i][j]; + + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + transformed_image[i][j] = regroup_tmp[i][j]; +} + +static void build_block_string(void) { + int block_h = H_PADDED / BLOCKSIZEIMAGE; + int block_w = W_PADDED / BLOCKSIZEIMAGE; + int i, j, x, y, counter = 0; + for (i = 0; i < block_h; ++i) + for (j = 0; j < block_w; ++j) { + for (x = 0; x < (int)BLOCKSIZEIMAGE; ++x) + for (y = 0; y < (int)BLOCKSIZEIMAGE; ++y) + block_string[counter][x*BLOCKSIZEIMAGE + y] = + transformed_image[i*BLOCKSIZEIMAGE + x][j*BLOCKSIZEIMAGE + y]; + ++counter; + } +} + +// Stesso pattern sintetico del percorso integer +static void generate_input(void) { + unsigned int i, j; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + image_data[i][j] = (int)((i * 173u + j * 91u + 13u) & 0xFFFFu); +} + +// ============================================================ +// Output bitstream statico con controllo di overflow +// ============================================================ +typedef struct { + uint8_t OutputBitStream[OUTPUT_BUFFER_BYTES]; + unsigned int num_bits; + unsigned int num_total_bytes; + unsigned int overflow; +} OutputBitStream_t; + +static OutputBitStream_t output_stream; + +static void write_to_the_output_segment(long word, unsigned int length) { + int i; + if (output_stream.num_total_bytes + (length >> 3) + 2u > OUTPUT_BUFFER_BYTES) { + output_stream.overflow = 1u; + return; + } + for (i = (int)length - 1; i >= 0; --i) { + output_stream.OutputBitStream[output_stream.num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1) << (7 - output_stream.num_bits)); + output_stream.num_bits++; + if (output_stream.num_bits >= 8u) { + output_stream.num_bits = 0u; + output_stream.num_total_bytes++; + } + } +} + +static unsigned int ceil_log2(unsigned long x) { + unsigned int r = 0; + unsigned long v = 1; + while (v < x) { v <<= 1; r++; } + return r; +} + +static unsigned int bit_size_with_pow2_adjust(long v) { + unsigned long av = (unsigned long)(v < 0 ? -v : v); + if (av == 0) return 0; + unsigned int b = ceil_log2(av); + if ((av & (av - 1)) == 0) b += 1; + return b; +} + +static unsigned long conv_to_twos_complement(int value, unsigned int num_bits) { + unsigned long temp, complement; + short i; + if (num_bits == 1) return 0; + if (value >= 0) return (unsigned long) value; + complement = ~(unsigned long)(-value); + temp = 0; + for (i = 0; i < (short)num_bits; i++) { temp <<= 1; temp++; } + complement &= temp; + complement++; + return complement; +} + +typedef struct { + unsigned int symbol_val; + unsigned int symbol_len; + unsigned int symbol_mapped_pattern; + unsigned int sign; + unsigned int type; +} str_symbol_details_t; + +typedef struct { + long shifted_dc; + unsigned int dc_reminder; + unsigned long mapped_dc; + long max_ac_bit_size; + unsigned long mapped_ac; + unsigned int type_p; + unsigned int tran_b; + unsigned int tran_d; + unsigned int tran_gi; + unsigned int parent_ref_symbol; + unsigned int parent_sym_len; + unsigned int children_ref_symbol; + unsigned int children_sym_len; + unsigned int type_ci[3]; + unsigned int tran_hi[3]; + unsigned int grand_children_ref_symbol[3]; + unsigned int grand_children_sym_len[3]; + unsigned int type_hi[12]; + str_symbol_details_t symbol_block[MAX_SYMBOLS_IN_BLOCK]; +} block_data_t; + +typedef struct { + int start_img_flag; + int end_img_flag; + unsigned int segment_count; + unsigned int bit_depth_dc; + unsigned int bit_depth_ac; + int part_2_flag; + int part_3_flag; + int part_4_flag; + unsigned int pad_rows; +} header_data_t; + +static block_data_t block_data[SEGMENT_SIZE]; +static header_data_t header_data; + +static void header_output(void) { + write_to_the_output_segment(header_data.start_img_flag, 1); + write_to_the_output_segment(header_data.end_img_flag, 1); + write_to_the_output_segment(header_data.segment_count, 8); + write_to_the_output_segment(header_data.bit_depth_dc, 5); + write_to_the_output_segment(header_data.bit_depth_ac, 5); + write_to_the_output_segment(0, 1); + write_to_the_output_segment(header_data.part_2_flag, 1); + write_to_the_output_segment(header_data.part_3_flag, 1); + write_to_the_output_segment(header_data.part_4_flag, 1); + if (header_data.end_img_flag) { + write_to_the_output_segment(header_data.pad_rows, 3); + write_to_the_output_segment(0, 5); + } +} + +static void coding_quantized_coefficients(unsigned int N) { + long x_min = -(1L << (N - 1)); + long x_max = (1L << (N - 1)) - 1; + unsigned int number_bits = 0, i; + long theta, gamma; + for (i = 0; i < N - 1; i++) number_bits = (number_bits << 1) + 1; + if ((block_data[0].shifted_dc & (1L << (N - 1))) > 0) + block_data[0].shifted_dc = -(long)(((block_data[0].shifted_dc ^ number_bits) & number_bits) + 1); + block_data[0].mapped_dc = (unsigned long) block_data[0].shifted_dc; + for (i = 1; i < SEGMENT_SIZE; i++) { + if ((block_data[i].shifted_dc & (1L << (N - 1))) > 0) + block_data[i].shifted_dc = -(long)(((block_data[i].shifted_dc ^ number_bits) & number_bits) + 1); + theta = (block_data[i-1].shifted_dc - x_min) < (x_max - block_data[i-1].shifted_dc) + ? (block_data[i-1].shifted_dc - x_min) : (x_max - block_data[i-1].shifted_dc); + gamma = block_data[i].shifted_dc - block_data[i-1].shifted_dc; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_dc = (unsigned long)(2 * gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_dc = (unsigned long)(2 * (-gamma) - 1); + else + block_data[i].mapped_dc = (unsigned long)(theta + (gamma < 0 ? -gamma : gamma)); + } +} + +static void dc_encoder(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + unsigned int total_bits, min_bits = 0xFFFFu; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position > 1 ? start_position : 1); i < end_position; i++) + total_bits += (unsigned int)((block_data[i].mapped_dc >> k) + 1) + k; + if (total_bits < min_bits && total_bits < N * GAGGLE_SIZE) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_dc, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_dc >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position > 1 ? start_position : 1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_dc, encode_selection); + } +} + +static void dc_entropy_coding(unsigned int N, unsigned int quantization_factor) { + unsigned int id_length = 0, max_k = 0, i; + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 8) { max_k = 6; id_length = 3; } + else { max_k = 8; id_length = 4; } + dc_encoder(max_k, id_length, N); + if (header_data.bit_depth_ac < quantization_factor) { + unsigned int num_additional_bit_planes = quantization_factor - header_data.bit_depth_ac; + for (i = 0; i < num_additional_bit_planes; i++) { + unsigned int kk; + for (kk = 0; kk < SEGMENT_SIZE; kk++) + write_to_the_output_segment( + (block_data[kk].dc_reminder >> (quantization_factor - i - 1)), 1); + } + } +} + +static unsigned int dc_encoding(void) { + long max_ac_value = 0; + unsigned int quantization_factor; + unsigned int i, j, k_mask; + header_data.bit_depth_dc = 0; + for (i = 0; i < SEGMENT_SIZE; ++i) { + long max_ac_value_block = 0; + int dc_value = block_string[i][0]; + unsigned int dc_bit_size; + if (dc_value < 0) + dc_bit_size = ceil_log2((unsigned long)(-dc_value) + 1UL); + else + dc_bit_size = ceil_log2((unsigned long)(1 + dc_value)) + 1; + if (dc_bit_size > header_data.bit_depth_dc) header_data.bit_depth_dc = dc_bit_size; + for (j = 1; j < BLOCKSIZEIMAGE * BLOCKSIZEIMAGE; ++j) { + long v = block_string[i][j]; + long av = v < 0 ? -v : v; + if (av > (max_ac_value < 0 ? -max_ac_value : max_ac_value)) max_ac_value = v; + if (av > (max_ac_value_block < 0 ? -max_ac_value_block : max_ac_value_block)) max_ac_value_block = v; + } + block_data[i].max_ac_bit_size = (long) bit_size_with_pow2_adjust(max_ac_value_block); + } + header_data.bit_depth_ac = bit_size_with_pow2_adjust(max_ac_value); + header_output(); + if (header_data.bit_depth_dc <= 3) + quantization_factor = 0; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) <= 1) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 3; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) > 10) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 10; + else + quantization_factor = 1 + (header_data.bit_depth_ac >> 1); + k_mask = (1u << quantization_factor) - 1u; + for (i = 0; i < SEGMENT_SIZE; ++i) { + unsigned long new_value_twos = conv_to_twos_complement(block_string[i][0], header_data.bit_depth_dc); + block_data[i].shifted_dc = (long)(new_value_twos >> quantization_factor); + block_data[i].dc_reminder = (unsigned int)(new_value_twos & k_mask); + } + { + unsigned int N = header_data.bit_depth_dc > quantization_factor + ? header_data.bit_depth_dc - quantization_factor : 1; + if (N == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].shifted_dc, 1); + } else { + coding_quantized_coefficients(N); + dc_entropy_coding(N, quantization_factor); + } + } + return quantization_factor; +} + +static void dpcm_ac_mapper(unsigned int N) { + long x_min = 0; + long x_max = (1L << N) - 1; + long theta, gamma; + unsigned int i; + block_data[0].mapped_ac = (unsigned long) block_data[0].max_ac_bit_size; + for (i = 1; i < SEGMENT_SIZE; i++) { + theta = (block_data[i-1].max_ac_bit_size - x_min) < (x_max - block_data[i-1].max_ac_bit_size) + ? (block_data[i-1].max_ac_bit_size - x_min) : (x_max - block_data[i-1].max_ac_bit_size); + gamma = block_data[i].max_ac_bit_size - block_data[i-1].max_ac_bit_size; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_ac = (unsigned long)(2*gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_ac = (unsigned long)(2*(-gamma) - 1); + else + block_data[i].mapped_ac = (unsigned long)(theta + (gamma<0?-gamma:gamma)); + } +} + +static void ac_gaggle_encode(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + long total_bits, min_bits = 0xFFFF; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position>1?start_position:1); i < end_position; i++) + total_bits += (long)((block_data[i].mapped_ac >> k) + 1) + k; + if (total_bits < min_bits && total_bits < (long)(N * GAGGLE_SIZE)) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_ac, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_ac >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position>1?start_position:1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_ac, encode_selection); + } +} + +static void ac_depth_encoder(void) { + unsigned int N = 0, id_length, max_k; + while ((header_data.bit_depth_ac >> N) > 0) N++; + dpcm_ac_mapper(N); + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 5) { max_k = 6; id_length = 3; } + else { max_k = 0; id_length = 0; } + ac_gaggle_encode(max_k, id_length, N); +} + +static void bit_plane_encoding(unsigned int bit_plane_number) { + unsigned int temp_x, temp_y, block_num; + unsigned char symbol; + int temp_value; + unsigned int final_position; + int bit_set_plane = (1 << (bit_plane_number - 1)); + + for (block_num = 0; block_num < SEGMENT_SIZE; ++block_num) { + symbol = 0; + final_position = block_num; + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + + for (int j = 0; j < 3; ++j) { + switch (j) { + case 0: temp_y = 1; break; + case 1: temp_y = BLOCKSIZEIMAGE; break; + case 2: temp_y = BLOCKSIZEIMAGE + 1; break; + default: temp_y = 0; break; + } + temp_x = final_position; + if (!type_of_compression) { + if (((j==0)&&(BITHL3>=bit_plane_number)) || ((j==1)&&(BITLH3>=bit_plane_number)) || ((j==2)&&(BITHH3>=bit_plane_number))) + continue; + } + if ((block_data[block_num].type_p & (1 << (2-j))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_P; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + if (ABSOLUTE(block_string[temp_x][temp_y]) >= (1 << (bit_plane_number-1)) && + ABSOLUTE(block_string[temp_x][temp_y]) < (1 << bit_plane_number)) { + block_data[block_num].type_p += (1 << (2-j)); + block_data[block_num].symbol_block[symbol].symbol_val += 1; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[temp_x][temp_y]); + } + } else { + temp_value = (ABSOLUTE(block_string[temp_x][temp_y]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((j==0)&&(BITHL3=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = true; break; + } + if (break_flag) break; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = true; break; + } + if (break_flag) break; + } + } + + if (block_data[block_num].tran_b == 0) { + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 0; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) ++symbol; + + if (block_data[block_num].tran_b == 1) { + for (int k = 0; k < 3; k++) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)&&(BITHL1>=bit_plane_number)) + || ((k==1)&&(BITLH2>=bit_plane_number)&&(BITLH1>=bit_plane_number)) + || ((k==2)&&(BITHH2>=bit_plane_number)&&(BITHH1>=bit_plane_number))) + continue; + } + bool break_flag = false; + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + bool jump_flag = false; + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + jump_flag = true; + } + if (!jump_flag) { + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = true; break; + } + } + if (!break_flag) { + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = true; break; + } + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + } + + for (unsigned int k = 0; k < 3; ++k) { + if ((block_data[block_num].tran_d & (1 << (2-k))) != 0) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + int p = 1; + for (unsigned int i = 0; i < 4; ++i) + if ((block_data[block_num].type_ci[k] & (1 << (3-i))) == 0) { p = 0; break; } + if (p == 0) { + unsigned int counter = 0; + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_CI; + for (unsigned int i = temp_x; i < temp_x+2; ++i) { + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) { + if ((block_data[block_num].type_ci[k] & (1 << (3-counter))) == 0) { + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].type_ci[k] += (1 << (3-counter)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (((block_data[block_num].tran_d & (1 << (2-k))) != 0) && ((block_data[block_num].tran_gi & (1 << (2-k))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_GI; + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + bool break_flag = false; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].tran_gi += (1 << (2-k)); + break_flag = true; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if (((block_data[block_num].tran_gi & (1 << (2-i3))) != 0) && ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_HI; + bool break_flag = false; + for (unsigned int k = temp_x; k < temp_x+2 && !break_flag; k++) + for (unsigned int p = temp_y; p < temp_y+2; p++) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].tran_hi[i3] += (1 << (3-jx)); + break_flag = true; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) != 0) { + unsigned int counter = 0; + for (unsigned int k = 0; k < 4; ++k) + if ((block_data[block_num].type_hi[i3*SIZE_TYPE+jx] & (1 << (3-k))) == 0) counter++; + if (counter == 0) { + for (unsigned int k = temp_x; k < temp_x+2; k++) { + for (unsigned int p = temp_y; p < temp_y+2; p++) { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1 0) { + block_data[block_num].type_hi[i3*SIZE_TYPE+jx] += (1 << (3-t)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1symbol_len) { + case 0: return; + case 1: s->symbol_mapped_pattern = s->symbol_val; break; + case 2: s->symbol_mapped_pattern = bit2_pattern[s->symbol_val]; break; + case 3: + s->symbol_mapped_pattern = (s->type == ENUM_TYPE_TRAN_D) ? bit3_pattern_TranD[s->symbol_val] : bit3_pattern[s->symbol_val]; + break; + case 4: + if (s->type == ENUM_TYPE_CI) s->symbol_mapped_pattern = bit4_pattern_TypeCi[s->symbol_val]; + else if (s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ) s->symbol_mapped_pattern = bit4_pattern_TypeHij_TranHi[s->symbol_val]; + break; + default: break; + } +} + +static void coding_options(unsigned int bit_plane_number, unsigned char *code_option_gaggle) { + unsigned int bit_counter_2bits[2] = {0}; + unsigned int bit_counter_3bits[3] = {0}; + unsigned int bit_counter_4bits[4] = {0}; + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + if (s->type == ENUM_NONE) continue; + if (s->symbol_len == 1) { pattern_mapping(s); continue; } + pattern_mapping(s); + if (s->symbol_len == 2) { + switch (s->symbol_mapped_pattern) { + case 0: bit_counter_2bits[0] += 1; break; + case 1: bit_counter_2bits[0] += 2; break; + default: bit_counter_2bits[0] += 3; break; + } + bit_counter_2bits[1] += 2; + } else if (s->symbol_len == 3) { + if (s->symbol_mapped_pattern <= 2) bit_counter_3bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 5) bit_counter_3bits[0] += 5; + else bit_counter_3bits[0] += 6; + if (s->symbol_mapped_pattern <= 1) bit_counter_3bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_3bits[1] += 3; + else bit_counter_3bits[1] += 4; + bit_counter_3bits[2] += 3; + } else if (s->symbol_len == 4) { + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[0] += 7; + else bit_counter_4bits[0] += 8; + if (s->symbol_mapped_pattern <= 1) bit_counter_4bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[1] += 3; + else if (s->symbol_mapped_pattern <= 5) bit_counter_4bits[1] += 4; + else if (s->symbol_mapped_pattern <= 11) bit_counter_4bits[1] += 6; + else bit_counter_4bits[1] += 7; + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[2] += 3; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[2] += 4; + else bit_counter_4bits[2] += 5; + bit_counter_4bits[3] += 4; + } + } + } + code_option_gaggle[0] = (bit_counter_2bits[0] < bit_counter_2bits[1]) ? 0 : 1; + if (bit_counter_3bits[2] <= bit_counter_3bits[0] && bit_counter_3bits[2] <= bit_counter_3bits[1]) code_option_gaggle[1] = 3; + else if (bit_counter_3bits[0] <= bit_counter_3bits[1]) code_option_gaggle[1] = 0; + else code_option_gaggle[1] = 1; + if (bit_counter_4bits[3] <= bit_counter_4bits[1] && bit_counter_4bits[3] <= bit_counter_4bits[0] && bit_counter_4bits[3] <= bit_counter_4bits[2]) code_option_gaggle[2] = 3; + else if (bit_counter_4bits[0] <= bit_counter_4bits[1] && bit_counter_4bits[0] <= bit_counter_4bits[2]) code_option_gaggle[2] = 0; + else if (bit_counter_4bits[1] <= bit_counter_4bits[2]) code_option_gaggle[2] = 1; + else code_option_gaggle[2] = 2; +} + +static void rice_coding(long input_value, unsigned int bit_length, unsigned char *code_option) { + switch (bit_length) { + case 0: return; + case 1: write_to_the_output_segment(input_value, 1); break; + case 2: + if (code_option[0] == 1) write_to_the_output_segment(input_value, 2); + else { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1, 1); } + else write_to_the_output_segment(0, 3); + } + break; + case 3: + if (code_option[1] == 0) { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 5) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-3,2); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,3); } + } else if (code_option[1] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else { + write_to_the_output_segment(0,2); + switch (input_value) { + case 4: write_to_the_output_segment(2,2); break; + case 5: write_to_the_output_segment(3,2); break; + case 6: write_to_the_output_segment(0,2); break; + case 7: write_to_the_output_segment(1,2); break; + } + } + } else write_to_the_output_segment(input_value, 3); + break; + case 4: + if (code_option[2] == 0) { + if (input_value <= 3) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 7) { write_to_the_output_segment(0,5); write_to_the_output_segment(input_value-4,2); } + else { write_to_the_output_segment(0,4); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else if (input_value <= 5) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-2,2); } + else if (input_value <= 11) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-6,3); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 2) { + if (input_value <= 3) write_to_the_output_segment(input_value+4, 3); + else if (input_value <= 7) write_to_the_output_segment(input_value, 4); + else if (input_value <= 11) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-4,3); } + else write_to_the_output_segment(input_value-12, 5); + } else write_to_the_output_segment(input_value, 4); + break; + } +} + +static void gaggle_pass(unsigned int bit_plane_number, unsigned char *code_option_gaggle, bool *hit_flag, int pass) { + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + int match = (pass == 1 && s->type == ENUM_TYPE_P) + || (pass == 2 && (s->type == ENUM_TYPE_TRAN_B || s->type == ENUM_TYPE_TRAN_D || s->type == ENUM_TYPE_CI)) + || (pass == 3 && (s->type == ENUM_TRAN_GI || s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ)); + if (!match) continue; + if (s->symbol_len > 1 && !hit_flag[s->symbol_len - 2]) { + hit_flag[s->symbol_len - 2] = true; + if (s->symbol_len == 2) write_to_the_output_segment(code_option_gaggle[0], 1); + else if (s->symbol_len == 3) write_to_the_output_segment(code_option_gaggle[1], 2); + else if (s->symbol_len == 4) write_to_the_output_segment(code_option_gaggle[2], 2); + } + rice_coding(s->symbol_mapped_pattern, s->symbol_len, code_option_gaggle); + if (s->type == ENUM_TYPE_CI || s->type == ENUM_TYPE_HIJ) { + unsigned int counter = 0, i; + for (i = 0; i < s->symbol_len; ++i) if ((s->symbol_val & (1 << i)) > 0) counter++; + write_to_the_output_segment(s->sign, counter); + } + s->symbol_val = 0; s->symbol_len = 0; s->symbol_mapped_pattern = 0; s->sign = 0; s->type = 0; + } + } +} + +static void ref_bit_end_encode(void) { + unsigned int block_num, i; + for (block_num = 0; block_num < SEGMENT_SIZE; ++block_num) { + if (block_data[block_num].parent_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].parent_ref_symbol, block_data[block_num].parent_sym_len); + block_data[block_num].parent_ref_symbol = 0; block_data[block_num].parent_sym_len = 0; + } + if (block_data[block_num].children_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].children_ref_symbol, block_data[block_num].children_sym_len); + block_data[block_num].children_ref_symbol = 0; block_data[block_num].children_sym_len = 0; + } + for (i = 0; i < 3; ++i) { + if (block_data[block_num].grand_children_sym_len[i] > 0) + write_to_the_output_segment(block_data[block_num].grand_children_ref_symbol[i], block_data[block_num].grand_children_sym_len[i]); + block_data[block_num].grand_children_sym_len[i] = 0; + block_data[block_num].grand_children_ref_symbol[i] = 0; + } + } +} + +static void stages_encoding(unsigned int bit_plane_number) { + unsigned char code_option_gaggle[3] = {0,0,0}; + bool hit_flag[3] = {false,false,false}; + coding_options(bit_plane_number, code_option_gaggle); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 1); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 2); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 3); + ref_bit_end_encode(); +} + +static void ac_encoding(unsigned int quantization_factor) { + unsigned int bit_plane, i; + if (header_data.bit_depth_ac == 0) return; + if (header_data.bit_depth_ac == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].max_ac_bit_size, 1); + } else { + ac_depth_encoder(); + } + for (bit_plane = header_data.bit_depth_ac; bit_plane > 0; bit_plane--) { + if ((bit_plane <= quantization_factor) && + (type_of_compression || (quantization_factor > BITLL3 && BITLL3 < bit_plane))) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment((block_data[i].dc_reminder >> (bit_plane - 1)) & 0x01, 1); + } + bit_plane_encoding(bit_plane); + stages_encoding(bit_plane); + } +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + unsigned int checksum, i, qf; + long coeff_checksum; + uint64_t t_start, t_end; + + car_init_start(); + + printf("OBPMark #2.2 - CCSDS 122.0 Image Compression (FLOAT)\r\n"); + printf("Bare-metal sequenziale - Astral CVA6 RV64 @ 50 MHz\r\n"); + printf("w_size=%u h_size=%u bit_size=%u segment_size=%u\r\n", + IMAGE_WIDTH, IMAGE_HEIGHT, BIT_SIZE, SEGMENT_SIZE); + + generate_input(); + + for (i = 0; i < OUTPUT_BUFFER_BYTES; ++i) output_stream.OutputBitStream[i] = 0u; + output_stream.num_bits = 0u; + output_stream.num_total_bytes = 0u; + output_stream.overflow = 0u; + + header_data.start_img_flag = 1; + header_data.end_img_flag = 1; + header_data.segment_count = 0; + header_data.part_2_flag = 0; + header_data.part_3_flag = 0; + header_data.part_4_flag = 0; + header_data.pad_rows = 0; + + t_start = read_mcycle(); + dwt2D_compression_computation_float(); + coeff_regroup(); + build_block_string(); + qf = dc_encoding(); + ac_encoding(qf); + t_end = read_mcycle(); + + coeff_checksum = 0; + for (i = 0; i < TOTAL_BLOCKS; ++i) + for (unsigned int k = 0; k < BLOCKSIZEIMAGE * BLOCKSIZEIMAGE; ++k) + coeff_checksum += block_string[i][k]; + + checksum = 0u; + for (i = 0; i < output_stream.num_total_bytes + 1u && i < OUTPUT_BUFFER_BYTES; ++i) + checksum += output_stream.OutputBitStream[i]; + + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_PERIOD_NS) / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + printf("Done.\r\n"); + printf("Benchmark metrics (sequenziale, 1x CVA6):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n", + (unsigned long long)time_ms_int, (unsigned long long)time_ms_frac); + printf(" Coeff checksum : %ld\r\n", coeff_checksum); + printf(" Block[0] DC : %d\r\n", block_string[0][0]); + printf(" bit_depth_dc : %u\r\n", header_data.bit_depth_dc); + printf(" bit_depth_ac : %u\r\n", header_data.bit_depth_ac); + printf(" quant. factor : %u\r\n", qf); + printf(" Output bytes : %u\r\n", output_stream.num_total_bytes); + printf(" Overflow : %u\r\n", output_stream.overflow); + printf(" Checksum : %u\r\n", checksum); + printf(" Output[0] : %u\r\n", (unsigned int)output_stream.OutputBitStream[0]); + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_2_2_host_smp.c b/sw/tests/bare-metal/hostd/obpmark_2_2_host_smp.c new file mode 100644 index 000000000..27296798e --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_2_2_host_smp.c @@ -0,0 +1,1316 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #2.2 - CCSDS 122.0 Image Compression +// Bare-metal SMP port for Astral host domain (2x CVA6, RV64) +// +// Stesso algoritmo e configurazione compile-time di obpmark_2_2_host_seq.c +// (w_size=h_size=32, bit_size=16, segment_size=16, percorso integer), quindi +// il risultato atteso e' bit-identico alla versione sequenziale gia' +// validata (Elapsed cycles=1141900, bit_depth_dc=17, bit_depth_ac=13, +// Output bytes=301, Checksum=35042) - stesso criterio di verifica usato +// per obpmark_2_1_host_smp.c. +// +// Strategia di parallelizzazione: +// DWT (Stage 1): row-pass e column-pass della DWT 2D, a ciascuno dei 3 +// livelli, sono genuinamente indipendenti per riga/colonna - split per +// range tra i due hart, con barriera tra row-pass e column-pass (la +// column-pass legge l'intero output della row-pass) e tra un livello +// e il successivo (il livello k+1 opera sul sotto-blocco LL prodotto +// dal livello k). La column-pass usa buffer scratch LOCALI (stack), +// non statici condivisi: due hart che processano colonne diverse in +// parallelo scriverebbero altrimenti sullo stesso buffer. +// coefficient_scaling / coeff_regroup (Stage 1): costo minimo, pattern +// di scatter con troppi casi sovrapposti per uno split sicuro senza +// rischio - sequenziali su hart 0, stessa natura di SAR_rcmc_shift in +// obpmark_1_2_host_smp_v8.c. +// build_block_string (Stage 1): indipendente per blocco - split per +// range di blocco. +// dc_encoding (Stage 2a): contiene riduzioni globali su tutto il +// segmento (bit_depth_dc, bit_depth_ac), l'header, e coding_quantized_ +// coefficients ha una catena DPCM sequenziale sul singolo gaggle - +// sequenziale su hart 0, stessa natura del conteggio zero-run in +// obpmark_2_1_host_smp.c. +// bit_plane_encoding (Stage 2b, per bit-plane): indipendente per blocco +// (ogni blocco scrive solo i propri campi in block_data[]) - la fase +// piu' onerosa dell'intero benchmark, split per range di blocco: il +// target primario della parallelizzazione. +// stages_encoding (Stage 2b, per bit-plane): coding_options calcola +// statistiche aggregate sull'intero gaggle, i tre gaggle_pass scrivono +// il bitstream condiviso in ordine stretto dipendente da stato +// (hit_flag) - sequenziale su hart 0. +// +// hart_id() riletto a ogni punto di decisione, mai cache-ato attraverso +// barriere/call - stesso workaround documentato per obpmark_1_1_smp_32.c +// e obpmark_1_2_host_smp_v8.c. + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include +#include + +// ============================================================ +// Configurazione (identica a obpmark_2_2_host_seq.c) +// ============================================================ +#define IMAGE_WIDTH 32u +#define IMAGE_HEIGHT 32u +#define BIT_SIZE 16u +#define LEVELS_DWT 3u +#define BLOCKSIZEIMAGE 8u +#define SEGMENT_SIZE 16u +#define GAGGLE_SIZE 16u +#define UNCODED_VALUE 0xFFu +#define BITLL3 3u + +#define MAX_SYMBOLS_IN_BLOCK 22u +#define SIZE_TYPE 4u + +#define ENUM_NONE 0 +#define ENUM_TYPE_P 1 +#define ENUM_TYPE_TRAN_B 2 +#define ENUM_TYPE_TRAN_D 3 +#define ENUM_TYPE_CI 4 +#define ENUM_TRAN_GI 5 +#define ENUM_TRAN_HI 6 +#define ENUM_TYPE_HIJ 7 + +#define BITHH1 0 +#define BITLH1 1 +#define BITHL1 1 +#define BITHH2 1 +#define BITLH2 2 +#define BITHL2 2 +#define BITHH3 2 +#define BITHL3 3 +#define BITLH3 3 + +#define ABSOLUTE(x) ((x) < 0 ? -(x) : (x)) +#define SIGN(x) ((x) < 0 ? 1 : 0) + +static const int bit2_pattern[4] = {0, 2, 1, 3}; +static const int bit3_pattern[8] = {1, 4, 0, 5, 2, 6, 3, 7}; +static const int bit3_pattern_TranD[8] = {0, 3, 0, 4, 1, 5, 2, 6}; +static const int bit4_pattern_TypeCi[16] = {10, 1, 3, 6, 2, 5, 9, 12, 0, 8, 7, 13, 4, 14, 11, 15}; +static const int bit4_pattern_TypeHij_TranHi[16] = {0, 1, 3, 6, 2, 5, 9, 11, 0, 8, 7, 12, 4, 13, 10, 14}; + +static const int type_of_compression = 0; // 0 = integer path + +#define W_PADDED IMAGE_WIDTH +#define H_PADDED IMAGE_HEIGHT +#define TOTAL_BLOCKS ((W_PADDED / BLOCKSIZEIMAGE) * (H_PADDED / BLOCKSIZEIMAGE)) // 16 +#define NUM_SEGMENTS 1u + +#define OUTPUT_BUFFER_BYTES 512u +#define NUM_HARTS 2 +#define CVA6_CLK_PERIOD_NS 20ULL + +// ============================================================ +// Barriera sense-reversing a 2 hart (identica a obpmark_2_1_host_smp.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +// ============================================================ +// Buffer statici Stage 1, condivisi tra hart +// ============================================================ +static int image_data[H_PADDED][W_PADDED]; +static int transformed_image[H_PADDED][W_PADDED]; +static int block_string[TOTAL_BLOCKS][BLOCKSIZEIMAGE * BLOCKSIZEIMAGE]; +static int aux_row[H_PADDED][W_PADDED]; // scritto per riga: indici distinti tra hart, sicuro +static int regroup_tmp[H_PADDED][W_PADDED]; + +static inline int floor_div(int a, int b) { + int q = a / b; + int r = a % b; + if (r != 0 && ((r < 0) != (b < 0))) q--; + return q; +} + +static void ccsds_wavelet_transform_1D_int(const int *A, int *B, int size) { + int full = 2 * size; + int i; + for (i = 0; i < size; ++i) { + int s, val; + if (i == 0) { + s = 9 * (A[0] + A[2]) - (A[2] + A[4]); + val = A[1] - floor_div(s + 8, 16); + } else if (i == size - 2) { + s = 9 * (A[full-4] + A[full-2]) - (A[full-6] + A[full-2]); + val = A[full-3] - floor_div(s + 8, 16); + } else if (i == size - 1) { + s = 9 * A[full-2] - A[full-4]; + val = A[full-1] - floor_div(s + 4, 8); + } else { + s = 9 * (A[2*i] + A[2*i+2]) - (A[2*i-2] + A[2*i+4]); + val = A[2*i+1] - floor_div(s + 8, 16); + } + B[size + i] = val; + } + for (i = 0; i < size; ++i) { + int val; + if (i == 0) + val = A[0] - floor_div(1 - B[size], 2); + else + val = A[2*i] - floor_div(2 - (B[i+size-1] + B[i+size]), 4); + B[i] = val; + } +} + +// Row-pass (PARALLELO, split per riga [rstart,rend)): ogni hart scrive +// solo aux_row[riga_propria] - nessuna sovrapposizione. +static void dwt_row_pass_worker(int w, unsigned int rstart, unsigned int rend) { + unsigned int i; + for (i = rstart; i < rend; ++i) + ccsds_wavelet_transform_1D_int(image_data[i], aux_row[i], w / 2); +} + +// Column-pass (PARALLELO, split per colonna [cstart,cend)): buffer +// scratch LOCALI (stack), privati per hart - evita la race sul buffer +// condiviso se due hart processano colonne diverse in parallelo. +static void dwt_col_pass_worker(int w, int h, unsigned int cstart, unsigned int cend) { + int local_in[H_PADDED]; + int local_out[H_PADDED]; + unsigned int i, x; + (void) w; + for (i = cstart; i < cend; ++i) { + for (x = 0; x < (unsigned int)h; ++x) local_in[x] = aux_row[x][i]; + ccsds_wavelet_transform_1D_int(local_in, local_out, h / 2); + for (x = 0; x < (unsigned int)h; ++x) image_data[x][i] = local_out[x]; + } +} + +static void coefficient_scaling(void) { + const int hh_1 = 1, hl_lh = 2, hh_2 = 2, hl_lh_2 = 4, hh_3 = 4, hl_lh_3 = 8, ll_4 = 8; + int i, j; + for (i = H_PADDED>>1; i < H_PADDED; i++) + for (j = W_PADDED>>1; j < W_PADDED; j++) transformed_image[i][j] *= hh_1; + for (i = 0; i < H_PADDED>>1; i++) + for (j = W_PADDED>>1; j < W_PADDED; j++) transformed_image[i][j] *= hl_lh; + for (i = H_PADDED>>1; i < H_PADDED; i++) + for (j = 0; j < W_PADDED>>1; j++) transformed_image[i][j] *= hl_lh; + for (i = H_PADDED>>2; i < H_PADDED>>1; i++) + for (j = W_PADDED>>2; j < W_PADDED>>1; j++) transformed_image[i][j] *= hh_2; + for (i = 0; i < H_PADDED>>2; i++) + for (j = W_PADDED>>2; j < W_PADDED>>1; j++) transformed_image[i][j] *= hl_lh_2; + for (i = H_PADDED>>2; i < H_PADDED>>1; i++) + for (j = 0; j < W_PADDED>>2; j++) transformed_image[i][j] *= hl_lh_2; + for (i = H_PADDED>>3; i < H_PADDED>>2; i++) + for (j = W_PADDED>>3; j < W_PADDED>>2; j++) transformed_image[i][j] *= hh_3; + for (i = 0; i < H_PADDED>>3; i++) + for (j = W_PADDED>>3; j < W_PADDED>>2; j++) transformed_image[i][j] *= hl_lh_3; + for (i = H_PADDED>>3; i < H_PADDED>>2; i++) + for (j = 0; j < W_PADDED>>3; j++) transformed_image[i][j] *= hl_lh_3; + for (i = 0; i < H_PADDED>>3; i++) + for (j = 0; j < W_PADDED>>3; j++) transformed_image[i][j] *= ll_4; +} + +static void coeff_regroup(void) { + int i, j, p, k, x, y, xb; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) regroup_tmp[i][j] = 0; + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 4; p < 8; ++p) + for (k = 4; k < 8; ++k) + regroup_tmp[x+p][y+k] = transformed_image[i+p-4][j+k-4]; + } + for (i = 0; i < H_PADDED>>1; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = i << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 0; p < 4; ++p) + for (k = 4; k < 8; ++k) + regroup_tmp[x+p][y+k] = transformed_image[i+p][j+k-4]; + } + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = 0; j < W_PADDED>>1; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = j << 1; + for (p = 4; p < 8; ++p) + for (k = 0; k < 4; ++k) + regroup_tmp[x+p][y+k] = transformed_image[i+p-4][j+k]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = (j - (W_PADDED>>2)) << 2; + regroup_tmp[x+2][y+2] = transformed_image[i][j]; + regroup_tmp[x+2][y+3] = transformed_image[i][j+1]; + regroup_tmp[x+3][y+2] = transformed_image[i+1][j]; + regroup_tmp[x+3][y+3] = transformed_image[i+1][j+1]; + } + for (i = 0; i < H_PADDED>>2; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = i << 2; y = (j - (W_PADDED>>2)) << 2; + regroup_tmp[x][y+2] = transformed_image[i][j]; + regroup_tmp[x][y+3] = transformed_image[i][j+1]; + regroup_tmp[x+1][y+2] = transformed_image[i+1][j]; + regroup_tmp[x+1][y+3] = transformed_image[i+1][j+1]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = 0; j < W_PADDED>>2; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = j << 2; + regroup_tmp[x+2][y] = transformed_image[i][j]; + regroup_tmp[x+2][y+1] = transformed_image[i][j+1]; + regroup_tmp[x+3][y] = transformed_image[i+1][j]; + regroup_tmp[x+3][y+1] = transformed_image[i+1][j+1]; + } + xb = H_PADDED>>3; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + regroup_tmp[((i-xb)<<3)+1][((j-(W_PADDED>>3))<<3)+1] = transformed_image[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + regroup_tmp[i<<3][((j-(W_PADDED>>3))<<3)+1] = transformed_image[i][j]; + xb = H_PADDED>>3; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + regroup_tmp[((i-xb)<<3)+1][j<<3] = transformed_image[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + regroup_tmp[i<<3][j<<3] = transformed_image[i][j]; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + transformed_image[i][j] = regroup_tmp[i][j]; +} + +// build_block_string (PARALLELO, split per blocco [bstart,bend)) +static void build_block_string_worker(unsigned int bstart, unsigned int bend) { + int block_w = W_PADDED / BLOCKSIZEIMAGE; + unsigned int bidx; + for (bidx = bstart; bidx < bend; ++bidx) { + int i = (int)bidx / block_w; + int j = (int)bidx % block_w; + int x, y; + for (x = 0; x < (int)BLOCKSIZEIMAGE; ++x) + for (y = 0; y < (int)BLOCKSIZEIMAGE; ++y) + block_string[bidx][x*BLOCKSIZEIMAGE + y] = + transformed_image[i*BLOCKSIZEIMAGE + x][j*BLOCKSIZEIMAGE + y]; + } +} + +static void generate_input(void) { + unsigned int i, j; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + image_data[i][j] = (int)((i * 173u + j * 91u + 13u) & 0xFFFFu); +} + +// ============================================================ +// Output bitstream statico - scritto solo da hart 0 +// ============================================================ +typedef struct { + uint8_t OutputBitStream[OUTPUT_BUFFER_BYTES]; + unsigned int num_bits; + unsigned int num_total_bytes; +} OutputBitStream_t; + +static OutputBitStream_t output_stream; + +static void write_to_the_output_segment(long word, unsigned int length) { + int i; + for (i = (int)length - 1; i >= 0; --i) { + output_stream.OutputBitStream[output_stream.num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1) << (7 - output_stream.num_bits)); + output_stream.num_bits++; + if (output_stream.num_bits >= 8u) { + output_stream.num_bits = 0u; + output_stream.num_total_bytes++; + } + } +} + +static unsigned int ceil_log2(unsigned long x) { + unsigned int r = 0; + unsigned long v = 1; + while (v < x) { v <<= 1; r++; } + return r; +} + +static unsigned int bit_size_with_pow2_adjust(long v) { + unsigned long av = (unsigned long)(v < 0 ? -v : v); + if (av == 0) return 0; + unsigned int b = ceil_log2(av); + if ((av & (av - 1)) == 0) b += 1; + return b; +} + +static unsigned long conv_to_twos_complement(int value, unsigned int num_bits) { + unsigned long temp, complement; + short i; + if (num_bits == 1) return 0; + if (value >= 0) return (unsigned long) value; + complement = ~(unsigned long)(-value); + temp = 0; + for (i = 0; i < (short)num_bits; i++) { temp <<= 1; temp++; } + complement &= temp; + complement++; + return complement; +} + +typedef struct { + unsigned int symbol_val; + unsigned int symbol_len; + unsigned int symbol_mapped_pattern; + unsigned int sign; + unsigned int type; +} str_symbol_details_t; + +typedef struct { + long shifted_dc; + unsigned int dc_reminder; + unsigned long mapped_dc; + long max_ac_bit_size; + unsigned long mapped_ac; + unsigned int type_p; + unsigned int tran_b; + unsigned int tran_d; + unsigned int tran_gi; + unsigned int parent_ref_symbol; + unsigned int parent_sym_len; + unsigned int children_ref_symbol; + unsigned int children_sym_len; + unsigned int type_ci[3]; + unsigned int tran_hi[3]; + unsigned int grand_children_ref_symbol[3]; + unsigned int grand_children_sym_len[3]; + unsigned int type_hi[12]; + str_symbol_details_t symbol_block[MAX_SYMBOLS_IN_BLOCK]; +} block_data_t; + +typedef struct { + int start_img_flag; + int end_img_flag; + unsigned int segment_count; + unsigned int bit_depth_dc; + unsigned int bit_depth_ac; + int part_2_flag; + int part_3_flag; + int part_4_flag; + unsigned int pad_rows; +} header_data_t; + +static block_data_t block_data[SEGMENT_SIZE]; +static header_data_t header_data; + +static void header_output(void) { + write_to_the_output_segment(header_data.start_img_flag, 1); + write_to_the_output_segment(header_data.end_img_flag, 1); + write_to_the_output_segment(header_data.segment_count, 8); + write_to_the_output_segment(header_data.bit_depth_dc, 5); + write_to_the_output_segment(header_data.bit_depth_ac, 5); + write_to_the_output_segment(0, 1); + write_to_the_output_segment(header_data.part_2_flag, 1); + write_to_the_output_segment(header_data.part_3_flag, 1); + write_to_the_output_segment(header_data.part_4_flag, 1); + if (header_data.end_img_flag) { + write_to_the_output_segment(header_data.pad_rows, 3); + write_to_the_output_segment(0, 5); + } +} + +static void coding_quantized_coefficients(unsigned int N) { + long x_min = -(1L << (N - 1)); + long x_max = (1L << (N - 1)) - 1; + unsigned int number_bits = 0, i; + long theta, gamma; + for (i = 0; i < N - 1; i++) number_bits = (number_bits << 1) + 1; + if ((block_data[0].shifted_dc & (1L << (N - 1))) > 0) + block_data[0].shifted_dc = -(long)(((block_data[0].shifted_dc ^ number_bits) & number_bits) + 1); + block_data[0].mapped_dc = (unsigned long) block_data[0].shifted_dc; + for (i = 1; i < SEGMENT_SIZE; i++) { + if ((block_data[i].shifted_dc & (1L << (N - 1))) > 0) + block_data[i].shifted_dc = -(long)(((block_data[i].shifted_dc ^ number_bits) & number_bits) + 1); + theta = (block_data[i-1].shifted_dc - x_min) < (x_max - block_data[i-1].shifted_dc) + ? (block_data[i-1].shifted_dc - x_min) : (x_max - block_data[i-1].shifted_dc); + gamma = block_data[i].shifted_dc - block_data[i-1].shifted_dc; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_dc = (unsigned long)(2 * gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_dc = (unsigned long)(2 * (-gamma) - 1); + else + block_data[i].mapped_dc = (unsigned long)(theta + (gamma < 0 ? -gamma : gamma)); + } +} + +static void dc_encoder(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + unsigned int total_bits, min_bits = 0xFFFFu; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position > 1 ? start_position : 1); i < end_position; i++) + total_bits += (unsigned int)((block_data[i].mapped_dc >> k) + 1) + k; + if (total_bits < min_bits && total_bits < N * GAGGLE_SIZE) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_dc, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_dc >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position > 1 ? start_position : 1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_dc, encode_selection); + } +} + +static void dc_entropy_coding(unsigned int N, unsigned int quantization_factor) { + unsigned int id_length = 0, max_k = 0, i; + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 8) { max_k = 6; id_length = 3; } + else { max_k = 8; id_length = 4; } + dc_encoder(max_k, id_length, N); + if (header_data.bit_depth_ac < quantization_factor) { + unsigned int num_additional_bit_planes = quantization_factor - header_data.bit_depth_ac; + for (i = 0; i < num_additional_bit_planes; i++) { + unsigned int kk; + for (kk = 0; kk < SEGMENT_SIZE; kk++) + write_to_the_output_segment( + (block_data[kk].dc_reminder >> (quantization_factor - i - 1)), 1); + } + } +} + +static unsigned int dc_encoding(void) { + long max_ac_value = 0; + unsigned int quantization_factor; + unsigned int i, j, k_mask; + header_data.bit_depth_dc = 0; + for (i = 0; i < SEGMENT_SIZE; ++i) { + long max_ac_value_block = 0; + int dc_value = block_string[i][0]; + unsigned int dc_bit_size; + if (dc_value < 0) + dc_bit_size = ceil_log2((unsigned long)(-dc_value) + 1UL); + else + dc_bit_size = ceil_log2((unsigned long)(1 + dc_value)) + 1; + if (dc_bit_size > header_data.bit_depth_dc) header_data.bit_depth_dc = dc_bit_size; + for (j = 1; j < BLOCKSIZEIMAGE * BLOCKSIZEIMAGE; ++j) { + long v = block_string[i][j]; + long av = v < 0 ? -v : v; + if (av > (max_ac_value < 0 ? -max_ac_value : max_ac_value)) max_ac_value = v; + if (av > (max_ac_value_block < 0 ? -max_ac_value_block : max_ac_value_block)) max_ac_value_block = v; + } + block_data[i].max_ac_bit_size = (long) bit_size_with_pow2_adjust(max_ac_value_block); + } + header_data.bit_depth_ac = bit_size_with_pow2_adjust(max_ac_value); + header_output(); + if (header_data.bit_depth_dc <= 3) + quantization_factor = 0; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) <= 1) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 3; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) > 10) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 10; + else + quantization_factor = 1 + (header_data.bit_depth_ac >> 1); + k_mask = (1u << quantization_factor) - 1u; + for (i = 0; i < SEGMENT_SIZE; ++i) { + unsigned long new_value_twos = conv_to_twos_complement(block_string[i][0], header_data.bit_depth_dc); + block_data[i].shifted_dc = (long)(new_value_twos >> quantization_factor); + block_data[i].dc_reminder = (unsigned int)(new_value_twos & k_mask); + } + { + unsigned int N = header_data.bit_depth_dc > quantization_factor + ? header_data.bit_depth_dc - quantization_factor : 1; + if (N == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].shifted_dc, 1); + } else { + coding_quantized_coefficients(N); + dc_entropy_coding(N, quantization_factor); + } + } + return quantization_factor; +} + +static void dpcm_ac_mapper(unsigned int N) { + long x_min = 0; + long x_max = (1L << N) - 1; + long theta, gamma; + unsigned int i; + block_data[0].mapped_ac = (unsigned long) block_data[0].max_ac_bit_size; + for (i = 1; i < SEGMENT_SIZE; i++) { + theta = (block_data[i-1].max_ac_bit_size - x_min) < (x_max - block_data[i-1].max_ac_bit_size) + ? (block_data[i-1].max_ac_bit_size - x_min) : (x_max - block_data[i-1].max_ac_bit_size); + gamma = block_data[i].max_ac_bit_size - block_data[i-1].max_ac_bit_size; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_ac = (unsigned long)(2*gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_ac = (unsigned long)(2*(-gamma) - 1); + else + block_data[i].mapped_ac = (unsigned long)(theta + (gamma<0?-gamma:gamma)); + } +} + +static void ac_gaggle_encode(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + long total_bits, min_bits = 0xFFFF; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position>1?start_position:1); i < end_position; i++) + total_bits += (long)((block_data[i].mapped_ac >> k) + 1) + k; + if (total_bits < min_bits && total_bits < (long)(N * GAGGLE_SIZE)) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_ac, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_ac >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position>1?start_position:1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_ac, encode_selection); + } +} + +static void ac_depth_encoder(void) { + unsigned int N = 0, id_length, max_k; + while ((header_data.bit_depth_ac >> N) > 0) N++; + dpcm_ac_mapper(N); + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 5) { max_k = 6; id_length = 3; } + else { max_k = 0; id_length = 0; } + ac_gaggle_encode(max_k, id_length, N); +} + +// bit_plane_encoding (PARALLELO, split per blocco [bstart,bend)) +static void bit_plane_encoding_worker(unsigned int bit_plane_number, unsigned int bstart, unsigned int bend) { + unsigned int temp_x, temp_y, block_num; + unsigned char symbol; + int temp_value; + unsigned int final_position; + int bit_set_plane = (1 << (bit_plane_number - 1)); + + for (block_num = bstart; block_num < bend; ++block_num) { + symbol = 0; + final_position = block_num; + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + + for (int j = 0; j < 3; ++j) { + switch (j) { + case 0: temp_y = 1; break; + case 1: temp_y = BLOCKSIZEIMAGE; break; + case 2: temp_y = BLOCKSIZEIMAGE + 1; break; + default: temp_y = 0; break; + } + temp_x = final_position; + if (!type_of_compression) { + if (((j==0)&&(BITHL3>=bit_plane_number)) || ((j==1)&&(BITLH3>=bit_plane_number)) || ((j==2)&&(BITHH3>=bit_plane_number))) + continue; + } + if ((block_data[block_num].type_p & (1 << (2-j))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_P; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + if (ABSOLUTE(block_string[temp_x][temp_y]) >= (1 << (bit_plane_number-1)) && + ABSOLUTE(block_string[temp_x][temp_y]) < (1 << bit_plane_number)) { + block_data[block_num].type_p += (1 << (2-j)); + block_data[block_num].symbol_block[symbol].symbol_val += 1; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[temp_x][temp_y]); + } + } else { + temp_value = (ABSOLUTE(block_string[temp_x][temp_y]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((j==0)&&(BITHL3=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = true; break; + } + if (break_flag) break; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = true; break; + } + if (break_flag) break; + } + } + + if (block_data[block_num].tran_b == 0) { + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 0; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) ++symbol; + + if (block_data[block_num].tran_b == 1) { + for (int k = 0; k < 3; k++) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)&&(BITHL1>=bit_plane_number)) + || ((k==1)&&(BITLH2>=bit_plane_number)&&(BITLH1>=bit_plane_number)) + || ((k==2)&&(BITHH2>=bit_plane_number)&&(BITHH1>=bit_plane_number))) + continue; + } + bool break_flag = false; + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + bool jump_flag = false; + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + jump_flag = true; + } + if (!jump_flag) { + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = true; break; + } + } + if (!break_flag) { + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = true; break; + } + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + } + + for (unsigned int k = 0; k < 3; ++k) { + if ((block_data[block_num].tran_d & (1 << (2-k))) != 0) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + int p = 1; + for (unsigned int i = 0; i < 4; ++i) + if ((block_data[block_num].type_ci[k] & (1 << (3-i))) == 0) { p = 0; break; } + if (p == 0) { + unsigned int counter = 0; + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_CI; + for (unsigned int i = temp_x; i < temp_x+2; ++i) { + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) { + if ((block_data[block_num].type_ci[k] & (1 << (3-counter))) == 0) { + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].type_ci[k] += (1 << (3-counter)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (((block_data[block_num].tran_d & (1 << (2-k))) != 0) && ((block_data[block_num].tran_gi & (1 << (2-k))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_GI; + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + bool break_flag = false; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].tran_gi += (1 << (2-k)); + break_flag = true; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if (((block_data[block_num].tran_gi & (1 << (2-i3))) != 0) && ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_HI; + bool break_flag = false; + for (unsigned int k = temp_x; k < temp_x+2 && !break_flag; k++) + for (unsigned int p = temp_y; p < temp_y+2; p++) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].tran_hi[i3] += (1 << (3-jx)); + break_flag = true; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) != 0) { + unsigned int counter = 0; + for (unsigned int k = 0; k < 4; ++k) + if ((block_data[block_num].type_hi[i3*SIZE_TYPE+jx] & (1 << (3-k))) == 0) counter++; + if (counter == 0) { + for (unsigned int k = temp_x; k < temp_x+2; k++) { + for (unsigned int p = temp_y; p < temp_y+2; p++) { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1 0) { + block_data[block_num].type_hi[i3*SIZE_TYPE+jx] += (1 << (3-t)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1symbol_len) { + case 0: return; + case 1: s->symbol_mapped_pattern = s->symbol_val; break; + case 2: s->symbol_mapped_pattern = bit2_pattern[s->symbol_val]; break; + case 3: + s->symbol_mapped_pattern = (s->type == ENUM_TYPE_TRAN_D) ? bit3_pattern_TranD[s->symbol_val] : bit3_pattern[s->symbol_val]; + break; + case 4: + if (s->type == ENUM_TYPE_CI) s->symbol_mapped_pattern = bit4_pattern_TypeCi[s->symbol_val]; + else if (s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ) s->symbol_mapped_pattern = bit4_pattern_TypeHij_TranHi[s->symbol_val]; + break; + default: break; + } +} + +static void coding_options(unsigned int bit_plane_number, unsigned char *code_option_gaggle) { + unsigned int bit_counter_2bits[2] = {0}; + unsigned int bit_counter_3bits[3] = {0}; + unsigned int bit_counter_4bits[4] = {0}; + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + if (s->type == ENUM_NONE) continue; + if (s->symbol_len == 1) { pattern_mapping(s); continue; } + pattern_mapping(s); + if (s->symbol_len == 2) { + switch (s->symbol_mapped_pattern) { + case 0: bit_counter_2bits[0] += 1; break; + case 1: bit_counter_2bits[0] += 2; break; + default: bit_counter_2bits[0] += 3; break; + } + bit_counter_2bits[1] += 2; + } else if (s->symbol_len == 3) { + if (s->symbol_mapped_pattern <= 2) bit_counter_3bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 5) bit_counter_3bits[0] += 5; + else bit_counter_3bits[0] += 6; + if (s->symbol_mapped_pattern <= 1) bit_counter_3bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_3bits[1] += 3; + else bit_counter_3bits[1] += 4; + bit_counter_3bits[2] += 3; + } else if (s->symbol_len == 4) { + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[0] += 7; + else bit_counter_4bits[0] += 8; + if (s->symbol_mapped_pattern <= 1) bit_counter_4bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[1] += 3; + else if (s->symbol_mapped_pattern <= 5) bit_counter_4bits[1] += 4; + else if (s->symbol_mapped_pattern <= 11) bit_counter_4bits[1] += 6; + else bit_counter_4bits[1] += 7; + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[2] += 3; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[2] += 4; + else bit_counter_4bits[2] += 5; + bit_counter_4bits[3] += 4; + } + } + } + code_option_gaggle[0] = (bit_counter_2bits[0] < bit_counter_2bits[1]) ? 0 : 1; + if (bit_counter_3bits[2] <= bit_counter_3bits[0] && bit_counter_3bits[2] <= bit_counter_3bits[1]) code_option_gaggle[1] = 3; + else if (bit_counter_3bits[0] <= bit_counter_3bits[1]) code_option_gaggle[1] = 0; + else code_option_gaggle[1] = 1; + if (bit_counter_4bits[3] <= bit_counter_4bits[1] && bit_counter_4bits[3] <= bit_counter_4bits[0] && bit_counter_4bits[3] <= bit_counter_4bits[2]) code_option_gaggle[2] = 3; + else if (bit_counter_4bits[0] <= bit_counter_4bits[1] && bit_counter_4bits[0] <= bit_counter_4bits[2]) code_option_gaggle[2] = 0; + else if (bit_counter_4bits[1] <= bit_counter_4bits[2]) code_option_gaggle[2] = 1; + else code_option_gaggle[2] = 2; +} + +static void rice_coding(long input_value, unsigned int bit_length, unsigned char *code_option) { + switch (bit_length) { + case 0: return; + case 1: write_to_the_output_segment(input_value, 1); break; + case 2: + if (code_option[0] == 1) write_to_the_output_segment(input_value, 2); + else { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1, 1); } + else write_to_the_output_segment(0, 3); + } + break; + case 3: + if (code_option[1] == 0) { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 5) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-3,2); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,3); } + } else if (code_option[1] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else { + write_to_the_output_segment(0,2); + switch (input_value) { + case 4: write_to_the_output_segment(2,2); break; + case 5: write_to_the_output_segment(3,2); break; + case 6: write_to_the_output_segment(0,2); break; + case 7: write_to_the_output_segment(1,2); break; + } + } + } else write_to_the_output_segment(input_value, 3); + break; + case 4: + if (code_option[2] == 0) { + if (input_value <= 3) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 7) { write_to_the_output_segment(0,5); write_to_the_output_segment(input_value-4,2); } + else { write_to_the_output_segment(0,4); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else if (input_value <= 5) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-2,2); } + else if (input_value <= 11) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-6,3); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 2) { + if (input_value <= 3) write_to_the_output_segment(input_value+4, 3); + else if (input_value <= 7) write_to_the_output_segment(input_value, 4); + else if (input_value <= 11) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-4,3); } + else write_to_the_output_segment(input_value-12, 5); + } else write_to_the_output_segment(input_value, 4); + break; + } +} + +static void gaggle_pass(unsigned int bit_plane_number, unsigned char *code_option_gaggle, bool *hit_flag, int pass) { + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + int match = (pass == 1 && s->type == ENUM_TYPE_P) + || (pass == 2 && (s->type == ENUM_TYPE_TRAN_B || s->type == ENUM_TYPE_TRAN_D || s->type == ENUM_TYPE_CI)) + || (pass == 3 && (s->type == ENUM_TRAN_GI || s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ)); + if (!match) continue; + if (s->symbol_len > 1 && !hit_flag[s->symbol_len - 2]) { + hit_flag[s->symbol_len - 2] = true; + if (s->symbol_len == 2) write_to_the_output_segment(code_option_gaggle[0], 1); + else if (s->symbol_len == 3) write_to_the_output_segment(code_option_gaggle[1], 2); + else if (s->symbol_len == 4) write_to_the_output_segment(code_option_gaggle[2], 2); + } + rice_coding(s->symbol_mapped_pattern, s->symbol_len, code_option_gaggle); + if (s->type == ENUM_TYPE_CI || s->type == ENUM_TYPE_HIJ) { + unsigned int counter = 0, i; + for (i = 0; i < s->symbol_len; ++i) if ((s->symbol_val & (1 << i)) > 0) counter++; + write_to_the_output_segment(s->sign, counter); + } + s->symbol_val = 0; s->symbol_len = 0; s->symbol_mapped_pattern = 0; s->sign = 0; s->type = 0; + } + } +} + +static void ref_bit_end_encode(void) { + unsigned int block_num, i; + for (block_num = 0; block_num < SEGMENT_SIZE; ++block_num) { + if (block_data[block_num].parent_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].parent_ref_symbol, block_data[block_num].parent_sym_len); + block_data[block_num].parent_ref_symbol = 0; block_data[block_num].parent_sym_len = 0; + } + if (block_data[block_num].children_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].children_ref_symbol, block_data[block_num].children_sym_len); + block_data[block_num].children_ref_symbol = 0; block_data[block_num].children_sym_len = 0; + } + for (i = 0; i < 3; ++i) { + if (block_data[block_num].grand_children_sym_len[i] > 0) + write_to_the_output_segment(block_data[block_num].grand_children_ref_symbol[i], block_data[block_num].grand_children_sym_len[i]); + block_data[block_num].grand_children_sym_len[i] = 0; + block_data[block_num].grand_children_ref_symbol[i] = 0; + } + } +} + +static void stages_encoding(unsigned int bit_plane_number) { + unsigned char code_option_gaggle[3] = {0,0,0}; + bool hit_flag[3] = {false,false,false}; + coding_options(bit_plane_number, code_option_gaggle); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 1); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 2); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 3); + ref_bit_end_encode(); +} + +static void __attribute__((noinline)) hart0_setup(void) { + unsigned int i; + printf("OBPMark #2.2 - CCSDS 122.0 Image Compression (SMP)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + printf("w_size=%u h_size=%u bit_size=%u segment_size=%u | Harts: %d\r\n", + IMAGE_WIDTH, IMAGE_HEIGHT, BIT_SIZE, SEGMENT_SIZE, NUM_HARTS); + + generate_input(); + for (i = 0; i < OUTPUT_BUFFER_BYTES; ++i) output_stream.OutputBitStream[i] = 0u; + output_stream.num_bits = 0u; + output_stream.num_total_bytes = 0u; + + header_data.start_img_flag = 1; + header_data.end_img_flag = 1; + header_data.segment_count = 0; + header_data.part_2_flag = 0; + header_data.part_3_flag = 0; + header_data.part_4_flag = 0; + header_data.pad_rows = 0; +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + if ((unsigned int)hart_id() == 0) { + car_init_start(); + smp_resume(); + } + barrier(); + + if ((unsigned int)hart_id() == 0) hart0_setup(); + barrier(); + + uint64_t t_start = ((unsigned int)hart_id() == 0) ? read_mcycle() : 0; + + // ---- Stage 1: DWT (PARALLELA per livello) ---- + { + int level_size = W_PADDED; + int lvl; + for (lvl = 0; lvl < (int)LEVELS_DWT; ++lvl) { + unsigned int rs, re, cs, ce; + split_range((unsigned int)level_size, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + dwt_row_pass_worker(level_size, rs, re); + barrier(); + split_range((unsigned int)level_size, (unsigned int)hart_id(), NUM_HARTS, &cs, &ce); + dwt_col_pass_worker(level_size, level_size, cs, ce); + barrier(); + level_size /= 2; + } + if ((unsigned int)hart_id() == 0) { + int i, j; + for (i = 0; i < H_PADDED; ++i) + for (j = 0; j < W_PADDED; ++j) + transformed_image[i][j] = image_data[i][j]; + } + barrier(); + } + + // ---- coefficient_scaling / coeff_regroup (sequenziali, hart 0) ---- + if ((unsigned int)hart_id() == 0) { + coefficient_scaling(); + coeff_regroup(); + } + barrier(); + + // ---- build_block_string (PARALLELA per blocco) ---- + { + unsigned int bs, be; + split_range(TOTAL_BLOCKS, (unsigned int)hart_id(), NUM_HARTS, &bs, &be); + build_block_string_worker(bs, be); + } + barrier(); + + // ---- Stage 2a: DC encoding (sequenziale, hart 0) ---- + unsigned int qf = 0; + if ((unsigned int)hart_id() == 0) qf = dc_encoding(); + barrier(); + + // ---- Stage 2b: AC bit-plane encoding ---- + if ((unsigned int)hart_id() == 0) { + if (header_data.bit_depth_ac > 0) { + if (header_data.bit_depth_ac == 1) { + unsigned int i; + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].max_ac_bit_size, 1); + } else { + ac_depth_encoder(); + } + } + } + barrier(); + + if (header_data.bit_depth_ac > 0) { + unsigned int bit_plane; + for (bit_plane = header_data.bit_depth_ac; bit_plane > 0; bit_plane--) { + if ((unsigned int)hart_id() == 0) { + if ((bit_plane <= qf) && + (type_of_compression || (qf > BITLL3 && BITLL3 < bit_plane))) { + unsigned int i; + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment((block_data[i].dc_reminder >> (bit_plane - 1)) & 0x01, 1); + } + } + barrier(); + + // bit_plane_encoding (PARALLELA per blocco) + { + unsigned int bs, be; + split_range(SEGMENT_SIZE, (unsigned int)hart_id(), NUM_HARTS, &bs, &be); + bit_plane_encoding_worker(bit_plane, bs, be); + } + barrier(); + + // stages_encoding (sequenziale, hart 0) + if ((unsigned int)hart_id() == 0) stages_encoding(bit_plane); + barrier(); + } + } + + if ((unsigned int)hart_id() == 0) { + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_PERIOD_NS) / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + unsigned int checksum = 0u, i; + for (i = 0; i < output_stream.num_total_bytes + 1u; ++i) + checksum += output_stream.OutputBitStream[i]; + + printf("Done.\r\n"); + printf("Benchmark metrics (2x CVA6 SMP):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n", + (unsigned long long)time_ms_int, (unsigned long long)time_ms_frac); + printf(" bit_depth_dc : %u\r\n", header_data.bit_depth_dc); + printf(" bit_depth_ac : %u\r\n", header_data.bit_depth_ac); + printf(" Output bytes : %u\r\n", output_stream.num_total_bytes); + printf(" Output[0] : %u\r\n", (unsigned int)output_stream.OutputBitStream[0]); + + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0u) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++] = (char)('0' + (q % 10u)); q /= 10u; } } + for (int j = n - 1; j >= 0; j--) *p++ = tmp[j]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_2_2_host_smp_float.c b/sw/tests/bare-metal/hostd/obpmark_2_2_host_smp_float.c new file mode 100644 index 000000000..4bb047e0e --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_2_2_host_smp_float.c @@ -0,0 +1,1323 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #2.2 - CCSDS 122.0 Image Compression - percorso FLOAT +// Bare-metal SMP per Astral host domain (2x CVA6, RV64) +// +// Stesso algoritmo di obpmark_2_2_host_seq_float.c: DWT 9/7 irreversibile +// in singola precisione, niente coefficient_scaling, type_of_compression=1. +// Riferimento sequenziale gia' validato: 1.645.333 cicli, bit_depth_dc=17, +// bit_depth_ac=12, quantization_factor=7, Output bytes=336, Checksum=42236, +// Output[0]=192, Coeff checksum=398521. +// +// Correzione FPU/SMP (dalla porta host_smp di 1.2, primo benchmark float su +// questo target): hart 1 riprende l'esecuzione dentro main() con +// mstatus.FS=Off. Se main() ha variabili locali float, il prologo emette +// fsd sullo stack e il primo accesso FP genera illegal instruction. Fix: +// - nessuna variabile float in main(); +// - ogni funzione che tocca float e' __attribute__((noinline)), mai +// inlineabile dentro main(); +// - enable_fpu() (mstatus.FS=Initial) chiamata da entrambi gli hart prima +// di qualunque codice float. +// +// Decomposizione (identica a obpmark_2_2_host_smp.c integer): +// conversione a float + DWT (row-pass/column-pass per livello) e +// arrotondamento finale: parallela, split per riga/colonna, scratch +// column-pass privato per hart (col_in_f/col_out_f[core]); +// coeff_regroup: hart 0; +// build_block_string: parallela, split per blocco; +// dc_encoding + ac_depth_encoder: hart 0; +// bit_plane_encoding: parallela, split per blocco; stages_encoding: hart 0. +// +// fp-contract=off: nessuna fusione moltiplicazione-somma, stessa +// valutazione IEEE della versione sequenziale. +// +// Naming convention: obpmark_2_2_host_smp_float + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include +#include + +#pragma GCC optimize ("fp-contract=off") + +// ============================================================ +// Configurazione (identica a obpmark_2_2_host_seq_float.c) +// ============================================================ +#define IMAGE_WIDTH 32u +#define IMAGE_HEIGHT 32u +#define BIT_SIZE 16u +#define LEVELS_DWT 3u +#define BLOCKSIZEIMAGE 8u +#define SEGMENT_SIZE 16u +#define GAGGLE_SIZE 16u +#define UNCODED_VALUE 0xFFu +#define BITLL3 3u + +#define MAX_SYMBOLS_IN_BLOCK 22u +#define SIZE_TYPE 4u + +#define ENUM_NONE 0 +#define ENUM_TYPE_P 1 +#define ENUM_TYPE_TRAN_B 2 +#define ENUM_TYPE_TRAN_D 3 +#define ENUM_TYPE_CI 4 +#define ENUM_TRAN_GI 5 +#define ENUM_TRAN_HI 6 +#define ENUM_TYPE_HIJ 7 + +#define BITHH1 0 +#define BITLH1 1 +#define BITHL1 1 +#define BITHH2 1 +#define BITLH2 2 +#define BITHL2 2 +#define BITHH3 2 +#define BITHL3 3 +#define BITLH3 3 + +#define ABSOLUTE(x) ((x) < 0 ? -(x) : (x)) +#define SIGN(x) ((x) < 0 ? 1 : 0) + +#define HIGHPASSFILTERSIZE 7 +#define LOWPASSFILTERSIZE 9 + +static const float lowpass_filter_cpu[LOWPASSFILTERSIZE] = {0.037828455507,-0.023849465020,-0.110624404418,0.377402855613, 0.852698679009,0.377402855613, -0.110624404418,-0.023849465020, 0.037828455507}; +static const float highpass_filter_cpu[HIGHPASSFILTERSIZE] = {-0.064538882629, 0.040689417609, 0.418092273222,-0.788485616406,0.418092273222,0.040689417609,-0.064538882629}; + +static const int bit2_pattern[4] = {0, 2, 1, 3}; +static const int bit3_pattern[8] = {1, 4, 0, 5, 2, 6, 3, 7}; +static const int bit3_pattern_TranD[8] = {0, 3, 0, 4, 1, 5, 2, 6}; +static const int bit4_pattern_TypeCi[16] = {10, 1, 3, 6, 2, 5, 9, 12, 0, 8, 7, 13, 4, 14, 11, 15}; +static const int bit4_pattern_TypeHij_TranHi[16] = {0, 1, 3, 6, 2, 5, 9, 11, 0, 8, 7, 12, 4, 13, 10, 14}; + +static const int type_of_compression = 1; // 1 = float path + +#define W_PADDED IMAGE_WIDTH +#define H_PADDED IMAGE_HEIGHT +#define TOTAL_BLOCKS ((W_PADDED / BLOCKSIZEIMAGE) * (H_PADDED / BLOCKSIZEIMAGE)) // 16 + +#define OUTPUT_BUFFER_BYTES 8192u +#define NUM_HARTS 2 +#define CVA6_CLK_PERIOD_NS 20ULL + +// ============================================================ +// FPU enable (vedi nota in testa al file) +// ============================================================ +static inline void enable_fpu(void) { + asm volatile ("csrs mstatus, %0" :: "r"(0x2000UL) : "memory"); +} + +// ============================================================ +// Barriera sense-reversing a 2 hart +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +// ============================================================ +// Buffer statici, condivisi tra hart +// ============================================================ +static int image_data[H_PADDED][W_PADDED]; +static int transformed_image[H_PADDED][W_PADDED]; +static int block_string[TOTAL_BLOCKS][BLOCKSIZEIMAGE * BLOCKSIZEIMAGE]; +static int regroup_tmp[H_PADDED][W_PADDED]; + +static float img_f[H_PADDED][W_PADDED]; +static float row_f[H_PADDED][W_PADDED]; // scritto per riga: indici distinti tra hart, sicuro + +// ============================================================ +// DWT 1D/2D float (identica alla sequenziale) +// ============================================================ +static void ccsds_wavelet_transform_1D_float(const float *A, float *B, int size) { + int full_size = size * 2; + int hi_start = -(LOWPASSFILTERSIZE / 2); + int hi_end = LOWPASSFILTERSIZE / 2; + int gi_start = -(HIGHPASSFILTERSIZE / 2); + int gi_end = HIGHPASSFILTERSIZE / 2; + int i; + + for (i = 0; i < size; ++i) { + float sum_value_low = 0.0f; + for (int hi = hi_start; hi < hi_end + 1; ++hi) { + int x_position = (2 * i) + hi; + if (x_position < 0) + x_position = -x_position; + else if (x_position > full_size - 1) + x_position = full_size - 1 - (x_position - (full_size - 1)); + sum_value_low += lowpass_filter_cpu[hi + hi_end] * A[x_position]; + } + B[i] = sum_value_low; + + float sum_value_high = 0.0f; + for (int gi = gi_start; gi < gi_end + 1; ++gi) { + int x_position = (2 * i) + gi + 1; + if (x_position < 0) + x_position = -x_position; + else if (x_position > full_size - 1) + x_position = full_size - 1 - (x_position - (full_size - 1)); + sum_value_high += highpass_filter_cpu[gi + gi_end] * A[x_position]; + } + B[i + size] = sum_value_high; + } +} + +// Row-pass (PARALLELA): ogni hart scrive solo le proprie righe di row_f +static void __attribute__((noinline)) dwt_row_pass_worker(int w, unsigned int rstart, unsigned int rend) { + unsigned int i; + for (i = rstart; i < rend; ++i) + ccsds_wavelet_transform_1D_float(img_f[i], row_f[i], w / 2); +} + +// Column-pass (PARALLELA): scratch LOCALE (stack), privato per hart +static void __attribute__((noinline)) dwt_col_pass_worker(int h, unsigned int cstart, unsigned int cend) { + float col_in[H_PADDED]; + float col_out[H_PADDED]; + unsigned int i, x; + for (i = cstart; i < cend; ++i) { + for (x = 0; x < (unsigned int)h; ++x) col_in[x] = row_f[x][i]; + ccsds_wavelet_transform_1D_float(col_in, col_out, h / 2); + for (x = 0; x < (unsigned int)h; ++x) img_f[x][i] = col_out[x]; + } +} + +static inline int round_half_away(float x) { + int t = (int)x; + float f = x - (float)t; + if (x >= 0.0f) return t + (f >= 0.5f ? 1 : 0); + else return t - (f <= -0.5f ? 1 : 0); +} + +// Conversione intero->float (PARALLELA, split per riga) +static void __attribute__((noinline)) dwt_convert_in_worker(unsigned int rstart, unsigned int rend) { + unsigned int i, j; + for (i = rstart; i < rend; ++i) + for (j = 0; j < W_PADDED; ++j) + img_f[i][j] = (float)image_data[i][j]; +} + +// Arrotondamento finale float->intero (PARALLELA, split per riga) +static void __attribute__((noinline)) dwt_round_out_worker(unsigned int rstart, unsigned int rend) { + unsigned int i, j; + for (i = rstart; i < rend; ++i) + for (j = 0; j < W_PADDED; ++j) + transformed_image[i][j] = round_half_away(img_f[i][j]); +} + +// ============================================================ +// Riorganizzazione dei coefficienti (invariata, nessun float coinvolto) +// ============================================================ +static void coeff_regroup(void) { + int i, j, p, k, x, y, xb; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) regroup_tmp[i][j] = 0; + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 4; p < 8; ++p) + for (k = 4; k < 8; ++k) + regroup_tmp[x+p][y+k] = transformed_image[i+p-4][j+k-4]; + } + for (i = 0; i < H_PADDED>>1; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = i << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 0; p < 4; ++p) + for (k = 4; k < 8; ++k) + regroup_tmp[x+p][y+k] = transformed_image[i+p][j+k-4]; + } + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = 0; j < W_PADDED>>1; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = j << 1; + for (p = 4; p < 8; ++p) + for (k = 0; k < 4; ++k) + regroup_tmp[x+p][y+k] = transformed_image[i+p-4][j+k]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = (j - (W_PADDED>>2)) << 2; + regroup_tmp[x+2][y+2] = transformed_image[i][j]; + regroup_tmp[x+2][y+3] = transformed_image[i][j+1]; + regroup_tmp[x+3][y+2] = transformed_image[i+1][j]; + regroup_tmp[x+3][y+3] = transformed_image[i+1][j+1]; + } + for (i = 0; i < H_PADDED>>2; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = i << 2; y = (j - (W_PADDED>>2)) << 2; + regroup_tmp[x][y+2] = transformed_image[i][j]; + regroup_tmp[x][y+3] = transformed_image[i][j+1]; + regroup_tmp[x+1][y+2] = transformed_image[i+1][j]; + regroup_tmp[x+1][y+3] = transformed_image[i+1][j+1]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = 0; j < W_PADDED>>2; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = j << 2; + regroup_tmp[x+2][y] = transformed_image[i][j]; + regroup_tmp[x+2][y+1] = transformed_image[i][j+1]; + regroup_tmp[x+3][y] = transformed_image[i+1][j]; + regroup_tmp[x+3][y+1] = transformed_image[i+1][j+1]; + } + xb = H_PADDED>>3; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + regroup_tmp[((i-xb)<<3)+1][((j-(W_PADDED>>3))<<3)+1] = transformed_image[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + regroup_tmp[i<<3][((j-(W_PADDED>>3))<<3)+1] = transformed_image[i][j]; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + regroup_tmp[((i-xb)<<3)+1][j<<3] = transformed_image[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + regroup_tmp[i<<3][j<<3] = transformed_image[i][j]; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + transformed_image[i][j] = regroup_tmp[i][j]; +} + +// build_block_string (PARALLELA, split per blocco) +static void __attribute__((noinline)) build_block_string_worker(unsigned int bstart, unsigned int bend) { + int block_w = W_PADDED / BLOCKSIZEIMAGE; + unsigned int bidx; + for (bidx = bstart; bidx < bend; ++bidx) { + int i = (int)bidx / block_w; + int j = (int)bidx % block_w; + int x, y; + for (x = 0; x < (int)BLOCKSIZEIMAGE; ++x) + for (y = 0; y < (int)BLOCKSIZEIMAGE; ++y) + block_string[bidx][x*BLOCKSIZEIMAGE + y] = + transformed_image[i*BLOCKSIZEIMAGE + x][j*BLOCKSIZEIMAGE + y]; + } +} + +static void generate_input(void) { + unsigned int i, j; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + image_data[i][j] = (int)((i * 173u + j * 91u + 13u) & 0xFFFFu); +} + +// ============================================================ +// Output bitstream statico con controllo di overflow, hart 0 soltanto +// ============================================================ +typedef struct { + uint8_t OutputBitStream[OUTPUT_BUFFER_BYTES]; + unsigned int num_bits; + unsigned int num_total_bytes; + unsigned int overflow; +} OutputBitStream_t; + +static OutputBitStream_t output_stream; + +static void write_to_the_output_segment(long word, unsigned int length) { + int i; + if (output_stream.num_total_bytes + (length >> 3) + 2u > OUTPUT_BUFFER_BYTES) { + output_stream.overflow = 1u; + return; + } + for (i = (int)length - 1; i >= 0; --i) { + output_stream.OutputBitStream[output_stream.num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1) << (7 - output_stream.num_bits)); + output_stream.num_bits++; + if (output_stream.num_bits >= 8u) { + output_stream.num_bits = 0u; + output_stream.num_total_bytes++; + } + } +} + +static unsigned int ceil_log2(unsigned long x) { + unsigned int r = 0; + unsigned long v = 1; + while (v < x) { v <<= 1; r++; } + return r; +} + +static unsigned int bit_size_with_pow2_adjust(long v) { + unsigned long av = (unsigned long)(v < 0 ? -v : v); + if (av == 0) return 0; + unsigned int b = ceil_log2(av); + if ((av & (av - 1)) == 0) b += 1; + return b; +} + +static unsigned long conv_to_twos_complement(int value, unsigned int num_bits) { + unsigned long temp, complement; + short i; + if (num_bits == 1) return 0; + if (value >= 0) return (unsigned long) value; + complement = ~(unsigned long)(-value); + temp = 0; + for (i = 0; i < (short)num_bits; i++) { temp <<= 1; temp++; } + complement &= temp; + complement++; + return complement; +} + +typedef struct { + unsigned int symbol_val; + unsigned int symbol_len; + unsigned int symbol_mapped_pattern; + unsigned int sign; + unsigned int type; +} str_symbol_details_t; + +typedef struct { + long shifted_dc; + unsigned int dc_reminder; + unsigned long mapped_dc; + long max_ac_bit_size; + unsigned long mapped_ac; + unsigned int type_p; + unsigned int tran_b; + unsigned int tran_d; + unsigned int tran_gi; + unsigned int parent_ref_symbol; + unsigned int parent_sym_len; + unsigned int children_ref_symbol; + unsigned int children_sym_len; + unsigned int type_ci[3]; + unsigned int tran_hi[3]; + unsigned int grand_children_ref_symbol[3]; + unsigned int grand_children_sym_len[3]; + unsigned int type_hi[12]; + str_symbol_details_t symbol_block[MAX_SYMBOLS_IN_BLOCK]; +} block_data_t; + +typedef struct { + int start_img_flag; + int end_img_flag; + unsigned int segment_count; + unsigned int bit_depth_dc; + unsigned int bit_depth_ac; + int part_2_flag; + int part_3_flag; + int part_4_flag; + unsigned int pad_rows; +} header_data_t; + +static block_data_t block_data[SEGMENT_SIZE]; +static header_data_t header_data; + +static void header_output(void) { + write_to_the_output_segment(header_data.start_img_flag, 1); + write_to_the_output_segment(header_data.end_img_flag, 1); + write_to_the_output_segment(header_data.segment_count, 8); + write_to_the_output_segment(header_data.bit_depth_dc, 5); + write_to_the_output_segment(header_data.bit_depth_ac, 5); + write_to_the_output_segment(0, 1); + write_to_the_output_segment(header_data.part_2_flag, 1); + write_to_the_output_segment(header_data.part_3_flag, 1); + write_to_the_output_segment(header_data.part_4_flag, 1); + if (header_data.end_img_flag) { + write_to_the_output_segment(header_data.pad_rows, 3); + write_to_the_output_segment(0, 5); + } +} + +static void coding_quantized_coefficients(unsigned int N) { + long x_min = -(1L << (N - 1)); + long x_max = (1L << (N - 1)) - 1; + unsigned int number_bits = 0, i; + long theta, gamma; + for (i = 0; i < N - 1; i++) number_bits = (number_bits << 1) + 1; + if ((block_data[0].shifted_dc & (1L << (N - 1))) > 0) + block_data[0].shifted_dc = -(long)(((block_data[0].shifted_dc ^ number_bits) & number_bits) + 1); + block_data[0].mapped_dc = (unsigned long) block_data[0].shifted_dc; + for (i = 1; i < SEGMENT_SIZE; i++) { + if ((block_data[i].shifted_dc & (1L << (N - 1))) > 0) + block_data[i].shifted_dc = -(long)(((block_data[i].shifted_dc ^ number_bits) & number_bits) + 1); + theta = (block_data[i-1].shifted_dc - x_min) < (x_max - block_data[i-1].shifted_dc) + ? (block_data[i-1].shifted_dc - x_min) : (x_max - block_data[i-1].shifted_dc); + gamma = block_data[i].shifted_dc - block_data[i-1].shifted_dc; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_dc = (unsigned long)(2 * gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_dc = (unsigned long)(2 * (-gamma) - 1); + else + block_data[i].mapped_dc = (unsigned long)(theta + (gamma < 0 ? -gamma : gamma)); + } +} + +static void dc_encoder(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + unsigned int total_bits, min_bits = 0xFFFFu; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position > 1 ? start_position : 1); i < end_position; i++) + total_bits += (unsigned int)((block_data[i].mapped_dc >> k) + 1) + k; + if (total_bits < min_bits && total_bits < N * GAGGLE_SIZE) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_dc, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_dc >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position > 1 ? start_position : 1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_dc, encode_selection); + } +} + +static void dc_entropy_coding(unsigned int N, unsigned int quantization_factor) { + unsigned int id_length = 0, max_k = 0, i; + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 8) { max_k = 6; id_length = 3; } + else { max_k = 8; id_length = 4; } + dc_encoder(max_k, id_length, N); + if (header_data.bit_depth_ac < quantization_factor) { + unsigned int num_additional_bit_planes = quantization_factor - header_data.bit_depth_ac; + for (i = 0; i < num_additional_bit_planes; i++) { + unsigned int kk; + for (kk = 0; kk < SEGMENT_SIZE; kk++) + write_to_the_output_segment( + (block_data[kk].dc_reminder >> (quantization_factor - i - 1)), 1); + } + } +} + +static unsigned int dc_encoding(void) { + long max_ac_value = 0; + unsigned int quantization_factor; + unsigned int i, j, k_mask; + header_data.bit_depth_dc = 0; + for (i = 0; i < SEGMENT_SIZE; ++i) { + long max_ac_value_block = 0; + int dc_value = block_string[i][0]; + unsigned int dc_bit_size; + if (dc_value < 0) + dc_bit_size = ceil_log2((unsigned long)(-dc_value) + 1UL); + else + dc_bit_size = ceil_log2((unsigned long)(1 + dc_value)) + 1; + if (dc_bit_size > header_data.bit_depth_dc) header_data.bit_depth_dc = dc_bit_size; + for (j = 1; j < BLOCKSIZEIMAGE * BLOCKSIZEIMAGE; ++j) { + long v = block_string[i][j]; + long av = v < 0 ? -v : v; + if (av > (max_ac_value < 0 ? -max_ac_value : max_ac_value)) max_ac_value = v; + if (av > (max_ac_value_block < 0 ? -max_ac_value_block : max_ac_value_block)) max_ac_value_block = v; + } + block_data[i].max_ac_bit_size = (long) bit_size_with_pow2_adjust(max_ac_value_block); + } + header_data.bit_depth_ac = bit_size_with_pow2_adjust(max_ac_value); + header_output(); + if (header_data.bit_depth_dc <= 3) + quantization_factor = 0; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) <= 1) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 3; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) > 10) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 10; + else + quantization_factor = 1 + (header_data.bit_depth_ac >> 1); + k_mask = (1u << quantization_factor) - 1u; + for (i = 0; i < SEGMENT_SIZE; ++i) { + unsigned long new_value_twos = conv_to_twos_complement(block_string[i][0], header_data.bit_depth_dc); + block_data[i].shifted_dc = (long)(new_value_twos >> quantization_factor); + block_data[i].dc_reminder = (unsigned int)(new_value_twos & k_mask); + } + { + unsigned int N = header_data.bit_depth_dc > quantization_factor + ? header_data.bit_depth_dc - quantization_factor : 1; + if (N == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].shifted_dc, 1); + } else { + coding_quantized_coefficients(N); + dc_entropy_coding(N, quantization_factor); + } + } + return quantization_factor; +} + +static void dpcm_ac_mapper(unsigned int N) { + long x_min = 0; + long x_max = (1L << N) - 1; + long theta, gamma; + unsigned int i; + block_data[0].mapped_ac = (unsigned long) block_data[0].max_ac_bit_size; + for (i = 1; i < SEGMENT_SIZE; i++) { + theta = (block_data[i-1].max_ac_bit_size - x_min) < (x_max - block_data[i-1].max_ac_bit_size) + ? (block_data[i-1].max_ac_bit_size - x_min) : (x_max - block_data[i-1].max_ac_bit_size); + gamma = block_data[i].max_ac_bit_size - block_data[i-1].max_ac_bit_size; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_ac = (unsigned long)(2*gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_ac = (unsigned long)(2*(-gamma) - 1); + else + block_data[i].mapped_ac = (unsigned long)(theta + (gamma<0?-gamma:gamma)); + } +} + +static void ac_gaggle_encode(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + long total_bits, min_bits = 0xFFFF; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position>1?start_position:1); i < end_position; i++) + total_bits += (long)((block_data[i].mapped_ac >> k) + 1) + k; + if (total_bits < min_bits && total_bits < (long)(N * GAGGLE_SIZE)) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_ac, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_ac >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position>1?start_position:1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_ac, encode_selection); + } +} + +static void ac_depth_encoder(void) { + unsigned int N = 0, id_length, max_k; + while ((header_data.bit_depth_ac >> N) > 0) N++; + dpcm_ac_mapper(N); + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 5) { max_k = 6; id_length = 3; } + else { max_k = 0; id_length = 0; } + ac_gaggle_encode(max_k, id_length, N); +} + +// bit_plane_encoding (PARALLELA, split per blocco [bstart,bend)) +static void __attribute__((noinline)) bit_plane_encoding_worker(unsigned int bit_plane_number, unsigned int bstart, unsigned int bend) { + unsigned int temp_x, temp_y, block_num; + unsigned char symbol; + int temp_value; + unsigned int final_position; + int bit_set_plane = (1 << (bit_plane_number - 1)); + + for (block_num = bstart; block_num < bend; ++block_num) { + symbol = 0; + final_position = block_num; + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + + for (int j = 0; j < 3; ++j) { + switch (j) { + case 0: temp_y = 1; break; + case 1: temp_y = BLOCKSIZEIMAGE; break; + case 2: temp_y = BLOCKSIZEIMAGE + 1; break; + default: temp_y = 0; break; + } + temp_x = final_position; + if (!type_of_compression) { + if (((j==0)&&(BITHL3>=bit_plane_number)) || ((j==1)&&(BITLH3>=bit_plane_number)) || ((j==2)&&(BITHH3>=bit_plane_number))) + continue; + } + if ((block_data[block_num].type_p & (1 << (2-j))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_P; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + if (ABSOLUTE(block_string[temp_x][temp_y]) >= (1 << (bit_plane_number-1)) && + ABSOLUTE(block_string[temp_x][temp_y]) < (1 << bit_plane_number)) { + block_data[block_num].type_p += (1 << (2-j)); + block_data[block_num].symbol_block[symbol].symbol_val += 1; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[temp_x][temp_y]); + } + } else { + temp_value = (ABSOLUTE(block_string[temp_x][temp_y]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((j==0)&&(BITHL3=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = true; break; + } + if (break_flag) break; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = true; break; + } + if (break_flag) break; + } + } + + if (block_data[block_num].tran_b == 0) { + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 0; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) ++symbol; + + if (block_data[block_num].tran_b == 1) { + for (int k = 0; k < 3; k++) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)&&(BITHL1>=bit_plane_number)) + || ((k==1)&&(BITLH2>=bit_plane_number)&&(BITLH1>=bit_plane_number)) + || ((k==2)&&(BITHH2>=bit_plane_number)&&(BITHH1>=bit_plane_number))) + continue; + } + bool break_flag = false; + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + bool jump_flag = false; + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + jump_flag = true; + } + if (!jump_flag) { + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = true; break; + } + } + if (!break_flag) { + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = true; break; + } + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + } + + for (unsigned int k = 0; k < 3; ++k) { + if ((block_data[block_num].tran_d & (1 << (2-k))) != 0) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + int p = 1; + for (unsigned int i = 0; i < 4; ++i) + if ((block_data[block_num].type_ci[k] & (1 << (3-i))) == 0) { p = 0; break; } + if (p == 0) { + unsigned int counter = 0; + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_CI; + for (unsigned int i = temp_x; i < temp_x+2; ++i) { + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) { + if ((block_data[block_num].type_ci[k] & (1 << (3-counter))) == 0) { + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].type_ci[k] += (1 << (3-counter)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (((block_data[block_num].tran_d & (1 << (2-k))) != 0) && ((block_data[block_num].tran_gi & (1 << (2-k))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_GI; + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + bool break_flag = false; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].tran_gi += (1 << (2-k)); + break_flag = true; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if (((block_data[block_num].tran_gi & (1 << (2-i3))) != 0) && ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_HI; + bool break_flag = false; + for (unsigned int k = temp_x; k < temp_x+2 && !break_flag; k++) + for (unsigned int p = temp_y; p < temp_y+2; p++) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].tran_hi[i3] += (1 << (3-jx)); + break_flag = true; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) != 0) { + unsigned int counter = 0; + for (unsigned int k = 0; k < 4; ++k) + if ((block_data[block_num].type_hi[i3*SIZE_TYPE+jx] & (1 << (3-k))) == 0) counter++; + if (counter == 0) { + for (unsigned int k = temp_x; k < temp_x+2; k++) { + for (unsigned int p = temp_y; p < temp_y+2; p++) { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1 0) { + block_data[block_num].type_hi[i3*SIZE_TYPE+jx] += (1 << (3-t)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1symbol_len) { + case 0: return; + case 1: s->symbol_mapped_pattern = s->symbol_val; break; + case 2: s->symbol_mapped_pattern = bit2_pattern[s->symbol_val]; break; + case 3: + s->symbol_mapped_pattern = (s->type == ENUM_TYPE_TRAN_D) ? bit3_pattern_TranD[s->symbol_val] : bit3_pattern[s->symbol_val]; + break; + case 4: + if (s->type == ENUM_TYPE_CI) s->symbol_mapped_pattern = bit4_pattern_TypeCi[s->symbol_val]; + else if (s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ) s->symbol_mapped_pattern = bit4_pattern_TypeHij_TranHi[s->symbol_val]; + break; + default: break; + } +} + +static void coding_options(unsigned int bit_plane_number, unsigned char *code_option_gaggle) { + unsigned int bit_counter_2bits[2] = {0}; + unsigned int bit_counter_3bits[3] = {0}; + unsigned int bit_counter_4bits[4] = {0}; + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + if (s->type == ENUM_NONE) continue; + if (s->symbol_len == 1) { pattern_mapping(s); continue; } + pattern_mapping(s); + if (s->symbol_len == 2) { + switch (s->symbol_mapped_pattern) { + case 0: bit_counter_2bits[0] += 1; break; + case 1: bit_counter_2bits[0] += 2; break; + default: bit_counter_2bits[0] += 3; break; + } + bit_counter_2bits[1] += 2; + } else if (s->symbol_len == 3) { + if (s->symbol_mapped_pattern <= 2) bit_counter_3bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 5) bit_counter_3bits[0] += 5; + else bit_counter_3bits[0] += 6; + if (s->symbol_mapped_pattern <= 1) bit_counter_3bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_3bits[1] += 3; + else bit_counter_3bits[1] += 4; + bit_counter_3bits[2] += 3; + } else if (s->symbol_len == 4) { + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[0] += 7; + else bit_counter_4bits[0] += 8; + if (s->symbol_mapped_pattern <= 1) bit_counter_4bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[1] += 3; + else if (s->symbol_mapped_pattern <= 5) bit_counter_4bits[1] += 4; + else if (s->symbol_mapped_pattern <= 11) bit_counter_4bits[1] += 6; + else bit_counter_4bits[1] += 7; + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[2] += 3; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[2] += 4; + else bit_counter_4bits[2] += 5; + bit_counter_4bits[3] += 4; + } + } + } + code_option_gaggle[0] = (bit_counter_2bits[0] < bit_counter_2bits[1]) ? 0 : 1; + if (bit_counter_3bits[2] <= bit_counter_3bits[0] && bit_counter_3bits[2] <= bit_counter_3bits[1]) code_option_gaggle[1] = 3; + else if (bit_counter_3bits[0] <= bit_counter_3bits[1]) code_option_gaggle[1] = 0; + else code_option_gaggle[1] = 1; + if (bit_counter_4bits[3] <= bit_counter_4bits[1] && bit_counter_4bits[3] <= bit_counter_4bits[0] && bit_counter_4bits[3] <= bit_counter_4bits[2]) code_option_gaggle[2] = 3; + else if (bit_counter_4bits[0] <= bit_counter_4bits[1] && bit_counter_4bits[0] <= bit_counter_4bits[2]) code_option_gaggle[2] = 0; + else if (bit_counter_4bits[1] <= bit_counter_4bits[2]) code_option_gaggle[2] = 1; + else code_option_gaggle[2] = 2; +} + +static void rice_coding(long input_value, unsigned int bit_length, unsigned char *code_option) { + switch (bit_length) { + case 0: return; + case 1: write_to_the_output_segment(input_value, 1); break; + case 2: + if (code_option[0] == 1) write_to_the_output_segment(input_value, 2); + else { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1, 1); } + else write_to_the_output_segment(0, 3); + } + break; + case 3: + if (code_option[1] == 0) { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 5) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-3,2); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,3); } + } else if (code_option[1] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else { + write_to_the_output_segment(0,2); + switch (input_value) { + case 4: write_to_the_output_segment(2,2); break; + case 5: write_to_the_output_segment(3,2); break; + case 6: write_to_the_output_segment(0,2); break; + case 7: write_to_the_output_segment(1,2); break; + } + } + } else write_to_the_output_segment(input_value, 3); + break; + case 4: + if (code_option[2] == 0) { + if (input_value <= 3) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 7) { write_to_the_output_segment(0,5); write_to_the_output_segment(input_value-4,2); } + else { write_to_the_output_segment(0,4); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else if (input_value <= 5) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-2,2); } + else if (input_value <= 11) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-6,3); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 2) { + if (input_value <= 3) write_to_the_output_segment(input_value+4, 3); + else if (input_value <= 7) write_to_the_output_segment(input_value, 4); + else if (input_value <= 11) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-4,3); } + else write_to_the_output_segment(input_value-12, 5); + } else write_to_the_output_segment(input_value, 4); + break; + } +} + +static void gaggle_pass(unsigned int bit_plane_number, unsigned char *code_option_gaggle, bool *hit_flag, int pass) { + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + int match = (pass == 1 && s->type == ENUM_TYPE_P) + || (pass == 2 && (s->type == ENUM_TYPE_TRAN_B || s->type == ENUM_TYPE_TRAN_D || s->type == ENUM_TYPE_CI)) + || (pass == 3 && (s->type == ENUM_TRAN_GI || s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ)); + if (!match) continue; + if (s->symbol_len > 1 && !hit_flag[s->symbol_len - 2]) { + hit_flag[s->symbol_len - 2] = true; + if (s->symbol_len == 2) write_to_the_output_segment(code_option_gaggle[0], 1); + else if (s->symbol_len == 3) write_to_the_output_segment(code_option_gaggle[1], 2); + else if (s->symbol_len == 4) write_to_the_output_segment(code_option_gaggle[2], 2); + } + rice_coding(s->symbol_mapped_pattern, s->symbol_len, code_option_gaggle); + if (s->type == ENUM_TYPE_CI || s->type == ENUM_TYPE_HIJ) { + unsigned int counter = 0, i; + for (i = 0; i < s->symbol_len; ++i) if ((s->symbol_val & (1 << i)) > 0) counter++; + write_to_the_output_segment(s->sign, counter); + } + s->symbol_val = 0; s->symbol_len = 0; s->symbol_mapped_pattern = 0; s->sign = 0; s->type = 0; + } + } +} + +static void ref_bit_end_encode(void) { + unsigned int block_num, i; + for (block_num = 0; block_num < SEGMENT_SIZE; ++block_num) { + if (block_data[block_num].parent_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].parent_ref_symbol, block_data[block_num].parent_sym_len); + block_data[block_num].parent_ref_symbol = 0; block_data[block_num].parent_sym_len = 0; + } + if (block_data[block_num].children_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].children_ref_symbol, block_data[block_num].children_sym_len); + block_data[block_num].children_ref_symbol = 0; block_data[block_num].children_sym_len = 0; + } + for (i = 0; i < 3; ++i) { + if (block_data[block_num].grand_children_sym_len[i] > 0) + write_to_the_output_segment(block_data[block_num].grand_children_ref_symbol[i], block_data[block_num].grand_children_sym_len[i]); + block_data[block_num].grand_children_sym_len[i] = 0; + block_data[block_num].grand_children_ref_symbol[i] = 0; + } + } +} + +static void stages_encoding(unsigned int bit_plane_number) { + unsigned char code_option_gaggle[3] = {0,0,0}; + bool hit_flag[3] = {false,false,false}; + coding_options(bit_plane_number, code_option_gaggle); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 1); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 2); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 3); + ref_bit_end_encode(); +} + +static void __attribute__((noinline)) hart0_setup(void) { + unsigned int i; + printf("OBPMark #2.2 - CCSDS 122.0 Image Compression (FLOAT, SMP)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + printf("w_size=%u h_size=%u bit_size=%u segment_size=%u | Harts: %d\r\n", + IMAGE_WIDTH, IMAGE_HEIGHT, BIT_SIZE, SEGMENT_SIZE, NUM_HARTS); + + generate_input(); + for (i = 0; i < OUTPUT_BUFFER_BYTES; ++i) output_stream.OutputBitStream[i] = 0u; + output_stream.num_bits = 0u; + output_stream.num_total_bytes = 0u; + output_stream.overflow = 0u; + + header_data.start_img_flag = 1; + header_data.end_img_flag = 1; + header_data.segment_count = 0; + header_data.part_2_flag = 0; + header_data.part_3_flag = 0; + header_data.part_4_flag = 0; + header_data.pad_rows = 0; +} + +static void __attribute__((noinline)) print_results_and_checksum(uint64_t cycles) { + unsigned int checksum = 0u, i; + long coeff_checksum = 0; + for (i = 0; i < TOTAL_BLOCKS; ++i) + for (unsigned int k = 0; k < BLOCKSIZEIMAGE * BLOCKSIZEIMAGE; ++k) + coeff_checksum += block_string[i][k]; + for (i = 0; i < output_stream.num_total_bytes + 1u && i < OUTPUT_BUFFER_BYTES; ++i) + checksum += output_stream.OutputBitStream[i]; + + uint64_t time_us = (cycles * CVA6_CLK_PERIOD_NS) / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + printf("Done.\r\n"); + printf("Benchmark metrics (SMP, 2x CVA6):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n", + (unsigned long long)time_ms_int, (unsigned long long)time_ms_frac); + printf(" Coeff checksum : %ld\r\n", coeff_checksum); + printf(" Block[0] DC : %d\r\n", block_string[0][0]); + printf(" bit_depth_dc : %u\r\n", header_data.bit_depth_dc); + printf(" bit_depth_ac : %u\r\n", header_data.bit_depth_ac); + printf(" Output bytes : %u\r\n", output_stream.num_total_bytes); + printf(" Overflow : %u\r\n", output_stream.overflow); + printf(" Checksum : %u\r\n", checksum); + printf(" Output[0] : %u\r\n", (unsigned int)output_stream.OutputBitStream[0]); +} + +// ============================================================ +// Main: nessuna variabile float qui dentro (vedi nota fix FPU/SMP) +// ============================================================ +int main(void) { + if ((unsigned int)hart_id() == 0) { + car_init_start(); + smp_resume(); + } + barrier(); + + enable_fpu(); + + if ((unsigned int)hart_id() == 0) hart0_setup(); + barrier(); + + uint64_t t_start = ((unsigned int)hart_id() == 0) ? read_mcycle() : 0; + + // ---- conversione a float (PARALLELA per riga) ---- + { + unsigned int rs, re; + split_range(H_PADDED, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + dwt_convert_in_worker(rs, re); + } + barrier(); + + // ---- DWT (PARALLELA per livello) ---- + { + int level_size = W_PADDED; + int lvl; + for (lvl = 0; lvl < (int)LEVELS_DWT; ++lvl) { + unsigned int rs, re, cs, ce; + split_range((unsigned int)level_size, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + dwt_row_pass_worker(level_size, rs, re); + barrier(); + split_range((unsigned int)level_size, (unsigned int)hart_id(), NUM_HARTS, &cs, &ce); + dwt_col_pass_worker(level_size, cs, ce); + barrier(); + level_size /= 2; + } + } + + // ---- arrotondamento finale (PARALLELA per riga) ---- + { + unsigned int rs, re; + split_range(H_PADDED, (unsigned int)hart_id(), NUM_HARTS, &rs, &re); + dwt_round_out_worker(rs, re); + } + barrier(); + + // ---- coeff_regroup (sequenziale, hart 0) ---- + if ((unsigned int)hart_id() == 0) coeff_regroup(); + barrier(); + + // ---- build_block_string (PARALLELA per blocco) ---- + { + unsigned int bs, be; + split_range(TOTAL_BLOCKS, (unsigned int)hart_id(), NUM_HARTS, &bs, &be); + build_block_string_worker(bs, be); + } + barrier(); + + // ---- DC encoding (sequenziale, hart 0) ---- + unsigned int qf = 0; + if ((unsigned int)hart_id() == 0) qf = dc_encoding(); + barrier(); + + // ---- AC depth encoding (sequenziale, hart 0) ---- + if ((unsigned int)hart_id() == 0) { + if (header_data.bit_depth_ac == 1) { + unsigned int i; + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].max_ac_bit_size, 1); + } else if (header_data.bit_depth_ac > 1) { + ac_depth_encoder(); + } + } + barrier(); + + // ---- AC bit-plane encoding ---- + if (header_data.bit_depth_ac > 0) { + unsigned int bit_depth_ac = header_data.bit_depth_ac; + unsigned int bit_plane, bs, be; + split_range(SEGMENT_SIZE, (unsigned int)hart_id(), NUM_HARTS, &bs, &be); + + for (bit_plane = bit_depth_ac; bit_plane > 0; bit_plane--) { + if ((unsigned int)hart_id() == 0 && + (bit_plane <= qf) && + (type_of_compression || (qf > BITLL3 && BITLL3 < bit_plane))) { + unsigned int i; + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment((block_data[i].dc_reminder >> (bit_plane - 1)) & 0x01, 1); + } + + bit_plane_encoding_worker(bit_plane, bs, be); + barrier(); + + if ((unsigned int)hart_id() == 0) stages_encoding(bit_plane); + barrier(); + } + } + + if ((unsigned int)hart_id() == 0) { + uint64_t t_end = read_mcycle(); + print_results_and_checksum(t_end - t_start); + } else { + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_3_1_host_seq.c b/sw/tests/bare-metal/hostd/obpmark_3_1_host_seq.c new file mode 100644 index 000000000..6549499f8 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_3_1_host_seq.c @@ -0,0 +1,349 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #3.1 - AES Encryption (AES-CTR mode) +// Bare-metal SEQUENZIALE per Astral (1x CVA6, RV64) +// +// Porting dal riferimento CPU OBPMark: +// src/3.1-aes_encryption/src/ports/cpu/{processing.c,processing.h,device.c} +// src/3.1-aes_encryption/include/{benchmark.h,device.h} +// +// Allineato all'ambiente bare-metal gia' usato per i benchmark precedenti +// (car_init_start, read_mcycle, printf.h): niente libc (no , +// , ), buffer statici, nessuna allocazione dinamica +// (AES_key_t/AES_data_t del riferimento allocavano key/plaintext/cyphertext/ +// iv/sbox/rcon/expanded_key con malloc: qui sono array statici a dimensione +// fissa), configurazione a compile-time al posto del parsing CLI +// (util_arg.c) e nessun file di input/verifica (il target bare-metal non +// ha filesystem). +// +// Modalita' operativa: il riferimento CPU implementa AES in modalita' CTR. +// AES_encrypt() cifra il contatore corrente (iv) con AES a blocco singolo +// per produrre il keystream, lo XORa col plaintext, poi incrementa l'intero +// contatore a 128 bit (counter_add, propagazione di riporto su tutti e 16 +// i byte, non solo la meta' bassa). +// +// Riguardo l'endianness: il riferimento CPU carica ogni word della chiave +// espansa con `*((uint32_t*)&key->value[4*i])` e la rilegge byte-a-byte per +// AddRoundKey: questo presuppone macchina little-endian (vero per CVA6/ +// RISC-V). Qui la stessa cosa e' fatta esplicitamente byte-a-byte (word = +// b0 | b1<<8 | b2<<16 | b3<<24) cosi' da non dipendere da un cast di +// puntatore non allineato, restando pero' bit-per-bit equivalente +// all'originale su target little-endian. +// +// Validazione: key/IV/plaintext sono il vettore di test NIST SP 800-38A +// Appendice F.5.1 "CTR-AES128.Encrypt" (AES-128, 4 blocchi da 16 byte). +// Ciphertext atteso (4 blocchi): +// 874d6191b620e3261bef6864990db6ce +// 9806f66b7970fdff8617187bb9fffdff +// 5ae4df3edbd5d35e5b4f09020db03eab +// 1e031dda2fbe03d1792170a0f3009cee +// Checksum atteso (somma di tutti i 64 byte di ciphertext, come unsigned +// int): 8196. Output[0] atteso: 0x87 (135). +// +// Naming convention: obpmark_3_1_host_seq (obpmark___). + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include + +// ============================================================ +// Configurazione benchmark (compile-time, al posto del parsing CLI +// util_arg.c del riferimento). AES-128: Nk=4, Nr=10; Nb=4 sempre +// (device.c imposta key->Nb=4 indipendentemente dalla key size). +// ============================================================ +#define AES_NB 4u +#define AES_NK 4u /* AES-128 -> Nk=4 */ +#define AES_NR 10u /* AES-128 -> Nr=10 */ +#define DATA_LENGTH 64u /* byte di plaintext = 4 blocchi da 16 byte */ +#define N_BLOCKS (DATA_LENGTH / 16u) +#define EXPKEY_WORDS (AES_NB * (AES_NR + 1u)) /* 44 word per AES-128 */ + +#define CVA6_CLK_PERIOD_NS 20ULL + +// ============================================================ +// Cycle counter (RV64, come nel porting sequenziale dei benchmark precedenti) +// ============================================================ +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +// ============================================================ +// Tabelle costanti AES (benchmark.h: SBOX_INIT / RCON_INIT, FIPS-197) +// ============================================================ +static const uint8_t sbox[256] = { + 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, + 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, + 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, + 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, + 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, + 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, + 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, + 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, + 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, + 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, + 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, + 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, + 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, + 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, + 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, + 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16 +}; + +static const uint8_t rcon[11] = { + 0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36 +}; + +// ============================================================ +// Buffer statici (niente heap: al posto di AES_key_t/AES_data_t allocati +// con malloc nel riferimento). Vettore di test NIST SP 800-38A F.5.1. +// ============================================================ +static const uint8_t key[16] = { + 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c +}; + +static uint8_t iv[16] = { + 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff +}; + +static const uint8_t plaintext[DATA_LENGTH] = { + 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a, + 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51, + 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef, + 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10 +}; + +static uint8_t cyphertext[DATA_LENGTH]; +static uint32_t expanded_key[EXPKEY_WORDS]; + +// ============================================================ +// AES core (src/3.1-aes_encryption/src/ports/cpu/processing.c) +// ============================================================ +static inline uint32_t AES_RotWord(uint32_t n) { + return (n >> 8) | (n << 24); +} + +static uint32_t AES_SubWord(uint32_t word) { + return ((uint32_t)sbox[(word >> 24) & 0xFFu] << 24) | + ((uint32_t)sbox[(word >> 16) & 0xFFu] << 16) | + ((uint32_t)sbox[(word >> 8) & 0xFFu] << 8) | + ((uint32_t)sbox[word & 0xFFu]); +} + +static void AES_KeyExpansion(void) { + uint32_t temp; + unsigned int i = 0u; + while (i < AES_NK) { + expanded_key[i] = (uint32_t)key[4u*i] | + ((uint32_t)key[4u*i + 1u] << 8) | + ((uint32_t)key[4u*i + 2u] << 16) | + ((uint32_t)key[4u*i + 3u] << 24); + i++; + } + for (; i < EXPKEY_WORDS; i++) { + temp = expanded_key[i - 1u]; + if (i % AES_NK == 0u) { + temp = AES_SubWord(AES_RotWord(temp)) ^ (uint32_t)rcon[i / AES_NK]; + } + /* Nk>6 branch (SubWord every 4th word) omitted: AES-128 only, AES_NK==4 */ + expanded_key[i] = expanded_key[i - AES_NK] ^ temp; + } +} + +// byte `row` (0..3) of the round-key word for round `round`, column `col` +static inline uint8_t roundkey_byte(unsigned int round, unsigned int col, unsigned int row) { + unsigned int idx = round * AES_NB * 4u + col * AES_NB + row; + return (uint8_t)(expanded_key[idx >> 2] >> ((idx & 3u) * 8u)); +} + +static void AES_AddRoundKey(const uint8_t in_state[4][4], uint8_t state[4][4], unsigned int round) { + unsigned int i, j; + for (i = 0; i < AES_NB; i++) { + for (j = 0; j < 4u; j++) { + state[i][j] = in_state[i][j] ^ roundkey_byte(round, i, j); + } + } +} + +static void AES_SubBytes(uint8_t state[4][4]) { + unsigned int i, j; + for (i = 0; i < 4u; i++) { + for (j = 0; j < 4u; j++) { + state[i][j] = sbox[state[i][j]]; + } + } +} + +static void AES_ShiftRows(uint8_t state[4][4]) { + uint8_t temp; + /* No shift on 1st row */ + /* Shift 2nd row 1 byte */ + temp = state[0][1]; + state[0][1] = state[1][1]; + state[1][1] = state[2][1]; + state[2][1] = state[3][1]; + state[3][1] = temp; + + /* Shift 3rd row 2 bytes */ + temp = state[0][2]; + state[0][2] = state[2][2]; + state[2][2] = temp; + temp = state[1][2]; + state[1][2] = state[3][2]; + state[3][2] = temp; + + /* Shift 4th row 3 bytes (same as 1 byte right shift) */ + temp = state[3][3]; + state[3][3] = state[2][3]; + state[2][3] = state[1][3]; + state[1][3] = state[0][3]; + state[0][3] = temp; +} + +static inline uint8_t xtime(uint8_t x) { + return (uint8_t)((x << 1) ^ (((x >> 7) & 1u) * 0x1bu)); +} + +static void AES_MixColumns(uint8_t state[4][4]) { + uint8_t Tmp, Tm, t; + unsigned int i; + for (i = 0; i < 4u; i++) { + t = state[i][0]; /* valore originale, serve all'ultima colonna */ + Tmp = state[i][0] ^ state[i][1] ^ state[i][2] ^ state[i][3]; + + Tm = xtime((uint8_t)(state[i][0] ^ state[i][1])); + state[i][0] ^= Tm ^ Tmp; + + Tm = xtime((uint8_t)(state[i][1] ^ state[i][2])); + state[i][1] ^= Tm ^ Tmp; + + Tm = xtime((uint8_t)(state[i][2] ^ state[i][3])); + state[i][2] ^= Tm ^ Tmp; + + Tm = xtime((uint8_t)(state[i][3] ^ t)); + state[i][3] ^= Tm ^ Tmp; + } +} + +static void AES_encrypt_state(const uint8_t in_state[4][4], uint8_t state[4][4], unsigned int num_rounds) { + unsigned int roundi; + AES_AddRoundKey(in_state, state, 0u); + + for (roundi = 1u; roundi < num_rounds; roundi++) { + AES_SubBytes(state); + AES_ShiftRows(state); + AES_MixColumns(state); + AES_AddRoundKey(state, state, roundi); + } + /* Last round without MixColumns */ + AES_SubBytes(state); + AES_ShiftRows(state); + AES_AddRoundKey(state, state, num_rounds); +} + +// Adds 1 to the 128-bit big-endian counter (iv[15] = LSB), full carry +// propagation across all 16 bytes: iterative equivalent of the reference +// counter_add(iv, 1, 15) tail recursion. +static void counter_increment(uint8_t *ivptr) { + int id = 15; + uint64_t block = 1u; + for (;;) { + uint64_t carry = ivptr[id] + block; + ivptr[id] = (uint8_t)carry; + if (carry <= 255u || id == 0) break; + block = carry >> 8; + id--; + } +} + +static void AES_encrypt_block(unsigned int b) { + uint8_t counter_state[4][4]; + uint8_t out_state[4][4]; + unsigned int col, row; + + /* flat 16-byte counter -> 4x4 state, column-major (state[col][row] = flat[col*4+row]) */ + for (col = 0; col < 4u; col++) { + for (row = 0; row < 4u; row++) { + counter_state[col][row] = iv[col*4u + row]; + } + } + + AES_encrypt_state(counter_state, out_state, AES_NR); + + for (col = 0; col < 4u; col++) { + for (row = 0; row < 4u; row++) { + cyphertext[b*16u + col*4u + row] = plaintext[b*16u + col*4u + row] ^ out_state[col][row]; + } + } + + counter_increment(iv); +} + +// ============================================================ +// Reporting +// ============================================================ +static void print_ciphertext_hex(void) { + unsigned int i; + printf("0x"); + for (i = 0; i < DATA_LENGTH; i++) { + printf("%02x", cyphertext[i]); + } + printf("\r\n"); +} + +static void print_results(uint64_t cycles, unsigned int checksum) { + uint64_t time_us = (cycles * CVA6_CLK_PERIOD_NS) / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + printf("Benchmark metrics (sequenziale, 1x CVA6):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n", + (unsigned long long)time_ms_int, (unsigned long long)time_ms_frac); + printf(" Ciphertext : "); + print_ciphertext_hex(); + printf(" Output[0] : %u\r\n", (unsigned int)cyphertext[0]); + printf(" Checksum : %u\r\n", checksum); +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + unsigned int b, i, checksum; + uint64_t t_start, t_end; + + car_init_start(); + + printf("OBPMark #3.1 - AES Encryption (CTR mode)\r\n"); + printf("Bare-metal sequenziale - Astral CVA6 RV64 @ 50 MHz\r\n"); + printf("key_size=128 bit Nb=%u Nk=%u Nr=%u data_length=%u byte (%u blocchi)\r\n", + AES_NB, AES_NK, AES_NR, DATA_LENGTH, N_BLOCKS); + + t_start = read_mcycle(); + + AES_KeyExpansion(); + for (b = 0; b < N_BLOCKS; b++) { + AES_encrypt_block(b); + } + + t_end = read_mcycle(); + + checksum = 0u; + for (i = 0; i < DATA_LENGTH; i++) { + checksum += cyphertext[i]; + } + + printf("Done.\r\n"); + print_results(t_end - t_start, checksum); + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_3_1_host_smp.c b/sw/tests/bare-metal/hostd/obpmark_3_1_host_smp.c new file mode 100644 index 000000000..e2aedb056 --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_3_1_host_smp.c @@ -0,0 +1,404 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #3.1 - AES Encryption (AES-CTR mode) +// Bare-metal SMP per Astral (2x CVA6, RV64) +// +// Porting dal riferimento OBPMark: +// src/3.1-aes_encryption/src/ports/cpu/{processing.c,device.c} +// src/3.1-aes_encryption/src/ports/openmp/device.c (schema di parallelizzazione) +// src/3.1-aes_encryption/include/{benchmark.h,device.h} +// +// Stessa configurazione, stesso vettore di test e stesso algoritmo di +// obpmark_3_1_host_seq.c: l'output deve essere bit-identico. +// +// Strategia di parallelizzazione: +// Fase 1 (SEQUENZIALE, hart 0): AES_KeyExpansion. Ricorrenza genuina +// (w[i] dipende da w[i-1] e w[i-Nk]), costo fisso e piccolo (44 word per +// AES-128). Una barriera rende expanded_key visibile a entrambi gli hart. +// Fase 2 (PARALLELA, per range contiguo di blocchi): cifratura CTR. +// Nel riferimento CPU sequenziale il contatore e' un unico iv condiviso, +// incrementato dopo ogni blocco: questa e' l'unica dipendenza tra +// blocchi. Come nel porting OpenMP di riferimento (che replica l'iv per +// ogni blocco), qui la dipendenza si elimina calcolando direttamente il +// contatore del blocco b come iv0 + b (addizione a 128 bit big-endian con +// propagazione completa del riporto, equivalente a b chiamate successive +// di counter_add(iv, 1)). Ogni hart lavora su un contatore privato sullo +// stack e scrive slot disgiunti di cyphertext: nessuno stato condiviso +// in scrittura, nessuna barriera interna. +// +// Il valore finale dell'iv globale (iv0 + N_BLOCKS nel riferimento) non fa +// parte dell'output del benchmark e non viene aggiornato. +// +// hart_id() e' riletto a ogni punto d'uso, mai memorizzato in una variabile +// che attraversa una barriera o una chiamata (workaround documentato in +// obpmark_1_1_smp_32.c e obpmark_1_2_host_smp_v8.c contro la corruzione dei +// registri con LTO). Nessuna operazione floating point. +// +// Validazione: vettore NIST SP 800-38A F.5.1 "CTR-AES128.Encrypt" sui primi +// 64 byte (si veda l'intestazione di obpmark_3_1_host_seq.c per il +// ciphertext atteso). Con DATA_LENGTH > 64 i byte oltre il 64-esimo sono +// generati in modo deterministico da init_plaintext(): in CTR ogni blocco +// dipende solo dal proprio contatore, quindi i primi 4 blocchi restano +// comunque confrontabili con i valori NIST. +// +// Naming convention: obpmark_3_1_host_smp (obpmark___). + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +// ============================================================ +// Configurazione (identica a obpmark_3_1_host_seq.c) +// ============================================================ +#define AES_NB 4u +#define AES_NK 4u /* AES-128 */ +#define AES_NR 10u +#define DATA_LENGTH 64u /* multiplo di 16 */ +#define N_BLOCKS (DATA_LENGTH / 16u) +#define EXPKEY_WORDS (AES_NB * (AES_NR + 1u)) + +#define NIST_LENGTH 64u + +#define NUM_HARTS 2 +#define CVA6_CLK_PERIOD_NS 20ULL + +// ============================================================ +// Cycle counter +// ============================================================ +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +// ============================================================ +// Barriera sense-reversing per 2 hart (identica a obpmark_1_1_smp_32.c, +// obpmark_1_2_host_smp_v8.c, obpmark_2_1_host_smp.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = atomic_add_w(&barrier_sense, 0u); + uint32_t arrived = atomic_add_w(&barrier_count, 1u) + 1u; + if (arrived == NUM_HARTS) { + atomic_add_w(&barrier_count, (uint32_t)(-(int)NUM_HARTS)); + asm volatile ("fence" ::: "memory"); + atomic_add_w(&barrier_sense, 1u); + } else { + while (atomic_add_w(&barrier_sense, 0u) == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + unsigned int rem = total % n; + *start = id * chunk + (id < rem ? id : rem); + *end = *start + chunk + (id < rem ? 1u : 0u); +} + +// ============================================================ +// Tabelle costanti AES (benchmark.h: SBOX_INIT / RCON_INIT) +// ============================================================ +static const uint8_t sbox[256] = { + 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, + 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, + 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, + 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, + 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, + 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, + 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, + 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, + 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, + 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, + 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, + 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, + 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, + 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, + 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, + 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16 +}; + +static const uint8_t rcon[11] = { + 0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36 +}; + +// ============================================================ +// Dati (vettore NIST SP 800-38A F.5.1) +// ============================================================ +static const uint8_t key[16] = { + 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c +}; + +static const uint8_t iv0[16] = { + 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff +}; + +static const uint8_t nist_plaintext[NIST_LENGTH] = { + 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a, + 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51, + 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef, + 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10 +}; + +static uint8_t plaintext[DATA_LENGTH]; +static uint8_t cyphertext[DATA_LENGTH]; +static uint32_t expanded_key[EXPKEY_WORDS]; + +// ============================================================ +// AES core (identico a obpmark_3_1_host_seq.c) +// ============================================================ +static inline uint32_t AES_RotWord(uint32_t n) { + return (n >> 8) | (n << 24); +} + +static uint32_t AES_SubWord(uint32_t word) { + return ((uint32_t)sbox[(word >> 24) & 0xFFu] << 24) | + ((uint32_t)sbox[(word >> 16) & 0xFFu] << 16) | + ((uint32_t)sbox[(word >> 8) & 0xFFu] << 8) | + ((uint32_t)sbox[word & 0xFFu]); +} + +static void AES_KeyExpansion(void) { + uint32_t temp; + unsigned int i = 0u; + while (i < AES_NK) { + expanded_key[i] = (uint32_t)key[4u*i] | + ((uint32_t)key[4u*i + 1u] << 8) | + ((uint32_t)key[4u*i + 2u] << 16) | + ((uint32_t)key[4u*i + 3u] << 24); + i++; + } + for (; i < EXPKEY_WORDS; i++) { + temp = expanded_key[i - 1u]; + if (i % AES_NK == 0u) { + temp = AES_SubWord(AES_RotWord(temp)) ^ (uint32_t)rcon[i / AES_NK]; + } + expanded_key[i] = expanded_key[i - AES_NK] ^ temp; + } +} + +static inline uint8_t roundkey_byte(unsigned int round, unsigned int col, unsigned int row) { + unsigned int idx = round * AES_NB * 4u + col * AES_NB + row; + return (uint8_t)(expanded_key[idx >> 2] >> ((idx & 3u) * 8u)); +} + +static void AES_AddRoundKey(const uint8_t in_state[4][4], uint8_t state[4][4], unsigned int round) { + unsigned int i, j; + for (i = 0; i < AES_NB; i++) + for (j = 0; j < 4u; j++) + state[i][j] = in_state[i][j] ^ roundkey_byte(round, i, j); +} + +static void AES_SubBytes(uint8_t state[4][4]) { + unsigned int i, j; + for (i = 0; i < 4u; i++) + for (j = 0; j < 4u; j++) + state[i][j] = sbox[state[i][j]]; +} + +static void AES_ShiftRows(uint8_t state[4][4]) { + uint8_t temp; + temp = state[0][1]; + state[0][1] = state[1][1]; + state[1][1] = state[2][1]; + state[2][1] = state[3][1]; + state[3][1] = temp; + + temp = state[0][2]; + state[0][2] = state[2][2]; + state[2][2] = temp; + temp = state[1][2]; + state[1][2] = state[3][2]; + state[3][2] = temp; + + temp = state[3][3]; + state[3][3] = state[2][3]; + state[2][3] = state[1][3]; + state[1][3] = state[0][3]; + state[0][3] = temp; +} + +static inline uint8_t xtime(uint8_t x) { + return (uint8_t)((x << 1) ^ (((x >> 7) & 1u) * 0x1bu)); +} + +static void AES_MixColumns(uint8_t state[4][4]) { + uint8_t Tmp, Tm, t; + unsigned int i; + for (i = 0; i < 4u; i++) { + t = state[i][0]; /* valore originale, serve all'ultima colonna */ + Tmp = state[i][0] ^ state[i][1] ^ state[i][2] ^ state[i][3]; + Tm = xtime((uint8_t)(state[i][0] ^ state[i][1])); state[i][0] ^= Tm ^ Tmp; + Tm = xtime((uint8_t)(state[i][1] ^ state[i][2])); state[i][1] ^= Tm ^ Tmp; + Tm = xtime((uint8_t)(state[i][2] ^ state[i][3])); state[i][2] ^= Tm ^ Tmp; + Tm = xtime((uint8_t)(state[i][3] ^ t)); state[i][3] ^= Tm ^ Tmp; + } +} + +static void AES_encrypt_state(const uint8_t in_state[4][4], uint8_t state[4][4], unsigned int num_rounds) { + unsigned int roundi; + AES_AddRoundKey(in_state, state, 0u); + for (roundi = 1u; roundi < num_rounds; roundi++) { + AES_SubBytes(state); + AES_ShiftRows(state); + AES_MixColumns(state); + AES_AddRoundKey(state, state, roundi); + } + AES_SubBytes(state); + AES_ShiftRows(state); + AES_AddRoundKey(state, state, num_rounds); +} + +// ============================================================ +// CTR: contatore del blocco b = iv0 + b (128 bit big-endian, iv[15] = LSB), +// propagazione completa del riporto. Equivale a b applicazioni successive +// di counter_add(iv, 1) del riferimento. +// ============================================================ +static void counter_for_block(uint8_t ctr[16], unsigned int b) { + uint64_t carry = (uint64_t)b; + int id; + for (id = 15; id >= 0; id--) { + uint64_t s = (uint64_t)iv0[id] + carry; + ctr[id] = (uint8_t)s; + carry = s >> 8; + } +} + +static void AES_encrypt_block(unsigned int b) { + uint8_t ctr[16]; + uint8_t counter_state[4][4]; + uint8_t out_state[4][4]; + unsigned int col, row; + + counter_for_block(ctr, b); + + for (col = 0; col < 4u; col++) + for (row = 0; row < 4u; row++) + counter_state[col][row] = ctr[col*4u + row]; + + AES_encrypt_state(counter_state, out_state, AES_NR); + + for (col = 0; col < 4u; col++) + for (row = 0; row < 4u; row++) + cyphertext[b*16u + col*4u + row] = + plaintext[b*16u + col*4u + row] ^ out_state[col][row]; +} + +// ============================================================ +// Setup (hart 0) +// ============================================================ +static void init_plaintext(void) { + unsigned int i; + for (i = 0; i < DATA_LENGTH; i++) { + plaintext[i] = (i < NIST_LENGTH) ? nist_plaintext[i] + : (uint8_t)((i * 37u + 11u) & 0xFFu); + } +} + +static void __attribute__((noinline)) hart0_setup(void) { + printf("OBPMark #3.1 - AES Encryption (CTR mode, SMP)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + printf("key_size=128 bit Nb=%u Nk=%u Nr=%u data_length=%u byte (%u blocchi) | Harts: %d\r\n", + AES_NB, AES_NK, AES_NR, DATA_LENGTH, N_BLOCKS, NUM_HARTS); + init_plaintext(); +} + +static void print_ciphertext_hex(void) { + unsigned int i; + printf("0x"); + for (i = 0; i < DATA_LENGTH; i++) printf("%02x", cyphertext[i]); + printf("\r\n"); +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + if ((unsigned int)hart_id() == 0) { + car_init_start(); + smp_resume(); + } + barrier(); + + if ((unsigned int)hart_id() == 0) hart0_setup(); + barrier(); // plaintext visibile a entrambi gli hart + + uint64_t t_start = ((unsigned int)hart_id() == 0) ? read_mcycle() : 0; + + // --- Fase 1 (SEQUENZIALE, hart 0): key expansion --- + if ((unsigned int)hart_id() == 0) AES_KeyExpansion(); + barrier(); // expanded_key visibile a entrambi gli hart + + // --- Fase 2 (PARALLELA): cifratura CTR per range di blocchi --- + { + unsigned int bs, be, b; + split_range(N_BLOCKS, (unsigned int)hart_id(), NUM_HARTS, &bs, &be); + for (b = bs; b < be; b++) AES_encrypt_block(b); + } + barrier(); // cyphertext completo + + if ((unsigned int)hart_id() == 0) { + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_PERIOD_NS) / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + unsigned int checksum = 0u, i; + for (i = 0; i < DATA_LENGTH; i++) checksum += cyphertext[i]; + + printf("Done.\r\n"); + printf("Benchmark metrics (2x CVA6 SMP):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n", + (unsigned long long)time_ms_int, (unsigned long long)time_ms_frac); + printf(" Ciphertext : "); + print_ciphertext_hex(); + printf(" Output[0] : %u\r\n", (unsigned int)cyphertext[0]); + + // Ultima riga scritta a mano via uart_write_str+flush, non printf: + // il testbench esegue $finish appena uart_reading_byte==0, cosa che + // puo' avvenire tra due printf consecutive prima che la riga finale + // sia trasmessa (stesso problema e stessa soluzione dei port SMP + // precedenti). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0u) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++] = (char)('0' + (q % 10u)); q /= 10u; } } + for (int j = n - 1; j >= 0; j--) *p++ = tmp[j]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 non deve uscire da main() (_exit() provocherebbe $finish + // prima che hart 0 termini le stampe) e resta parcheggiato in wfi + // con interrupt disabilitati. + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/obpmark_3_1_host_smp.c.bak b/sw/tests/bare-metal/hostd/obpmark_3_1_host_smp.c.bak new file mode 100644 index 000000000..b970637bb --- /dev/null +++ b/sw/tests/bare-metal/hostd/obpmark_3_1_host_smp.c.bak @@ -0,0 +1,404 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #3.1 - AES Encryption (AES-CTR mode) +// Bare-metal SMP per Astral (2x CVA6, RV64) +// +// Porting dal riferimento OBPMark: +// src/3.1-aes_encryption/src/ports/cpu/{processing.c,device.c} +// src/3.1-aes_encryption/src/ports/openmp/device.c (schema di parallelizzazione) +// src/3.1-aes_encryption/include/{benchmark.h,device.h} +// +// Stessa configurazione, stesso vettore di test e stesso algoritmo di +// obpmark_3_1_host_seq.c: l'output deve essere bit-identico. +// +// Strategia di parallelizzazione: +// Fase 1 (SEQUENZIALE, hart 0): AES_KeyExpansion. Ricorrenza genuina +// (w[i] dipende da w[i-1] e w[i-Nk]), costo fisso e piccolo (44 word per +// AES-128). Una barriera rende expanded_key visibile a entrambi gli hart. +// Fase 2 (PARALLELA, per range contiguo di blocchi): cifratura CTR. +// Nel riferimento CPU sequenziale il contatore e' un unico iv condiviso, +// incrementato dopo ogni blocco: questa e' l'unica dipendenza tra +// blocchi. Come nel porting OpenMP di riferimento (che replica l'iv per +// ogni blocco), qui la dipendenza si elimina calcolando direttamente il +// contatore del blocco b come iv0 + b (addizione a 128 bit big-endian con +// propagazione completa del riporto, equivalente a b chiamate successive +// di counter_add(iv, 1)). Ogni hart lavora su un contatore privato sullo +// stack e scrive slot disgiunti di cyphertext: nessuno stato condiviso +// in scrittura, nessuna barriera interna. +// +// Il valore finale dell'iv globale (iv0 + N_BLOCKS nel riferimento) non fa +// parte dell'output del benchmark e non viene aggiornato. +// +// hart_id() e' riletto a ogni punto d'uso, mai memorizzato in una variabile +// che attraversa una barriera o una chiamata (workaround documentato in +// obpmark_1_1_smp_32.c e obpmark_1_2_host_smp_v8.c contro la corruzione dei +// registri con LTO). Nessuna operazione floating point. +// +// Validazione: vettore NIST SP 800-38A F.5.1 "CTR-AES128.Encrypt" sui primi +// 64 byte (si veda l'intestazione di obpmark_3_1_host_seq.c per il +// ciphertext atteso). Con DATA_LENGTH > 64 i byte oltre il 64-esimo sono +// generati in modo deterministico da init_plaintext(): in CTR ogni blocco +// dipende solo dal proprio contatore, quindi i primi 4 blocchi restano +// comunque confrontabili con i valori NIST. +// +// Naming convention: obpmark_3_1_host_smp (obpmark___). + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "smp.h" +#include "printf.h" +#include + +// ============================================================ +// Configurazione (identica a obpmark_3_1_host_seq.c) +// ============================================================ +#define AES_NB 4u +#define AES_NK 4u /* AES-128 */ +#define AES_NR 10u +#define DATA_LENGTH 64u /* multiplo di 16 */ +#define N_BLOCKS (DATA_LENGTH / 16u) +#define EXPKEY_WORDS (AES_NB * (AES_NR + 1u)) + +#define NIST_LENGTH 64u + +#define NUM_HARTS 2 +#define CVA6_CLK_PERIOD_NS 20ULL + +// ============================================================ +// Cycle counter +// ============================================================ +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +// ============================================================ +// Barriera sense-reversing per 2 hart (identica a obpmark_1_1_smp_32.c, +// obpmark_1_2_host_smp_v8.c, obpmark_2_1_host_smp.c) +// ============================================================ +static volatile uint32_t barrier_count = 0; +static volatile uint32_t barrier_sense = 0; + +static inline uint32_t atomic_add_w(volatile uint32_t *addr, uint32_t val) { + uint32_t old; + asm volatile ("amoadd.w.aqrl %0, %2, (%1)" + : "=r"(old) : "r"(addr), "r"(val) : "memory"); + return old; +} + +static void barrier(void) { + uint32_t local_sense = barrier_sense; + uint32_t arrived = atomic_add_w(&barrier_count, 1) + 1u; + if (arrived == NUM_HARTS) { + barrier_count = 0; + asm volatile ("fence" ::: "memory"); + barrier_sense = !local_sense; + } else { + while (barrier_sense == local_sense) { } + asm volatile ("fence" ::: "memory"); + } +} + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + unsigned int rem = total % n; + *start = id * chunk + (id < rem ? id : rem); + *end = *start + chunk + (id < rem ? 1u : 0u); +} + +// ============================================================ +// Tabelle costanti AES (benchmark.h: SBOX_INIT / RCON_INIT) +// ============================================================ +static const uint8_t sbox[256] = { + 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, + 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, + 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, + 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, + 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, + 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, + 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, + 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, + 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, + 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, + 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, + 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, + 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, + 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, + 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, + 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16 +}; + +static const uint8_t rcon[11] = { + 0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36 +}; + +// ============================================================ +// Dati (vettore NIST SP 800-38A F.5.1) +// ============================================================ +static const uint8_t key[16] = { + 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c +}; + +static const uint8_t iv0[16] = { + 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff +}; + +static const uint8_t nist_plaintext[NIST_LENGTH] = { + 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a, + 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51, + 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef, + 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10 +}; + +static uint8_t plaintext[DATA_LENGTH]; +static uint8_t cyphertext[DATA_LENGTH]; +static uint32_t expanded_key[EXPKEY_WORDS]; + +// ============================================================ +// AES core (identico a obpmark_3_1_host_seq.c) +// ============================================================ +static inline uint32_t AES_RotWord(uint32_t n) { + return (n >> 8) | (n << 24); +} + +static uint32_t AES_SubWord(uint32_t word) { + return ((uint32_t)sbox[(word >> 24) & 0xFFu] << 24) | + ((uint32_t)sbox[(word >> 16) & 0xFFu] << 16) | + ((uint32_t)sbox[(word >> 8) & 0xFFu] << 8) | + ((uint32_t)sbox[word & 0xFFu]); +} + +static void AES_KeyExpansion(void) { + uint32_t temp; + unsigned int i = 0u; + while (i < AES_NK) { + expanded_key[i] = (uint32_t)key[4u*i] | + ((uint32_t)key[4u*i + 1u] << 8) | + ((uint32_t)key[4u*i + 2u] << 16) | + ((uint32_t)key[4u*i + 3u] << 24); + i++; + } + for (; i < EXPKEY_WORDS; i++) { + temp = expanded_key[i - 1u]; + if (i % AES_NK == 0u) { + temp = AES_SubWord(AES_RotWord(temp)) ^ (uint32_t)rcon[i / AES_NK]; + } + expanded_key[i] = expanded_key[i - AES_NK] ^ temp; + } +} + +static inline uint8_t roundkey_byte(unsigned int round, unsigned int col, unsigned int row) { + unsigned int idx = round * AES_NB * 4u + col * AES_NB + row; + return (uint8_t)(expanded_key[idx >> 2] >> ((idx & 3u) * 8u)); +} + +static void AES_AddRoundKey(const uint8_t in_state[4][4], uint8_t state[4][4], unsigned int round) { + unsigned int i, j; + for (i = 0; i < AES_NB; i++) + for (j = 0; j < 4u; j++) + state[i][j] = in_state[i][j] ^ roundkey_byte(round, i, j); +} + +static void AES_SubBytes(uint8_t state[4][4]) { + unsigned int i, j; + for (i = 0; i < 4u; i++) + for (j = 0; j < 4u; j++) + state[i][j] = sbox[state[i][j]]; +} + +static void AES_ShiftRows(uint8_t state[4][4]) { + uint8_t temp; + temp = state[0][1]; + state[0][1] = state[1][1]; + state[1][1] = state[2][1]; + state[2][1] = state[3][1]; + state[3][1] = temp; + + temp = state[0][2]; + state[0][2] = state[2][2]; + state[2][2] = temp; + temp = state[1][2]; + state[1][2] = state[3][2]; + state[3][2] = temp; + + temp = state[3][3]; + state[3][3] = state[2][3]; + state[2][3] = state[1][3]; + state[1][3] = state[0][3]; + state[0][3] = temp; +} + +static inline uint8_t xtime(uint8_t x) { + return (uint8_t)((x << 1) ^ (((x >> 7) & 1u) * 0x1bu)); +} + +static void AES_MixColumns(uint8_t state[4][4]) { + uint8_t Tmp, Tm, t; + unsigned int i; + for (i = 0; i < 4u; i++) { + t = state[i][0]; /* valore originale, serve all'ultima colonna */ + Tmp = state[i][0] ^ state[i][1] ^ state[i][2] ^ state[i][3]; + Tm = xtime((uint8_t)(state[i][0] ^ state[i][1])); state[i][0] ^= Tm ^ Tmp; + Tm = xtime((uint8_t)(state[i][1] ^ state[i][2])); state[i][1] ^= Tm ^ Tmp; + Tm = xtime((uint8_t)(state[i][2] ^ state[i][3])); state[i][2] ^= Tm ^ Tmp; + Tm = xtime((uint8_t)(state[i][3] ^ t)); state[i][3] ^= Tm ^ Tmp; + } +} + +static void AES_encrypt_state(const uint8_t in_state[4][4], uint8_t state[4][4], unsigned int num_rounds) { + unsigned int roundi; + AES_AddRoundKey(in_state, state, 0u); + for (roundi = 1u; roundi < num_rounds; roundi++) { + AES_SubBytes(state); + AES_ShiftRows(state); + AES_MixColumns(state); + AES_AddRoundKey(state, state, roundi); + } + AES_SubBytes(state); + AES_ShiftRows(state); + AES_AddRoundKey(state, state, num_rounds); +} + +// ============================================================ +// CTR: contatore del blocco b = iv0 + b (128 bit big-endian, iv[15] = LSB), +// propagazione completa del riporto. Equivale a b applicazioni successive +// di counter_add(iv, 1) del riferimento. +// ============================================================ +static void counter_for_block(uint8_t ctr[16], unsigned int b) { + uint64_t carry = (uint64_t)b; + int id; + for (id = 15; id >= 0; id--) { + uint64_t s = (uint64_t)iv0[id] + carry; + ctr[id] = (uint8_t)s; + carry = s >> 8; + } +} + +static void AES_encrypt_block(unsigned int b) { + uint8_t ctr[16]; + uint8_t counter_state[4][4]; + uint8_t out_state[4][4]; + unsigned int col, row; + + counter_for_block(ctr, b); + + for (col = 0; col < 4u; col++) + for (row = 0; row < 4u; row++) + counter_state[col][row] = ctr[col*4u + row]; + + AES_encrypt_state(counter_state, out_state, AES_NR); + + for (col = 0; col < 4u; col++) + for (row = 0; row < 4u; row++) + cyphertext[b*16u + col*4u + row] = + plaintext[b*16u + col*4u + row] ^ out_state[col][row]; +} + +// ============================================================ +// Setup (hart 0) +// ============================================================ +static void init_plaintext(void) { + unsigned int i; + for (i = 0; i < DATA_LENGTH; i++) { + plaintext[i] = (i < NIST_LENGTH) ? nist_plaintext[i] + : (uint8_t)((i * 37u + 11u) & 0xFFu); + } +} + +static void __attribute__((noinline)) hart0_setup(void) { + printf("OBPMark #3.1 - AES Encryption (CTR mode, SMP)\r\n"); + printf("Bare-metal SMP - Astral 2x CVA6 RV64 @ 50 MHz\r\n"); + printf("key_size=128 bit Nb=%u Nk=%u Nr=%u data_length=%u byte (%u blocchi) | Harts: %d\r\n", + AES_NB, AES_NK, AES_NR, DATA_LENGTH, N_BLOCKS, NUM_HARTS); + init_plaintext(); +} + +static void print_ciphertext_hex(void) { + unsigned int i; + printf("0x"); + for (i = 0; i < DATA_LENGTH; i++) printf("%02x", cyphertext[i]); + printf("\r\n"); +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + if ((unsigned int)hart_id() == 0) { + car_init_start(); + smp_resume(); + } + barrier(); + + if ((unsigned int)hart_id() == 0) hart0_setup(); + barrier(); // plaintext visibile a entrambi gli hart + + uint64_t t_start = ((unsigned int)hart_id() == 0) ? read_mcycle() : 0; + + // --- Fase 1 (SEQUENZIALE, hart 0): key expansion --- + if ((unsigned int)hart_id() == 0) AES_KeyExpansion(); + barrier(); // expanded_key visibile a entrambi gli hart + + // --- Fase 2 (PARALLELA): cifratura CTR per range di blocchi --- + { + unsigned int bs, be, b; + split_range(N_BLOCKS, (unsigned int)hart_id(), NUM_HARTS, &bs, &be); + for (b = bs; b < be; b++) AES_encrypt_block(b); + } + barrier(); // cyphertext completo + + if ((unsigned int)hart_id() == 0) { + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + uint64_t time_us = (cycles * CVA6_CLK_PERIOD_NS) / 1000ULL; + uint64_t time_ms_int = time_us / 1000ULL; + uint64_t time_ms_frac = time_us % 1000ULL; + + unsigned int checksum = 0u, i; + for (i = 0; i < DATA_LENGTH; i++) checksum += cyphertext[i]; + + printf("Done.\r\n"); + printf("Benchmark metrics (2x CVA6 SMP):\r\n"); + printf(" Elapsed cycles : %llu\r\n", (unsigned long long)cycles); + printf(" Elapsed time : %llu.%03llu ms\r\n", + (unsigned long long)time_ms_int, (unsigned long long)time_ms_frac); + printf(" Ciphertext : "); + print_ciphertext_hex(); + printf(" Output[0] : %u\r\n", (unsigned int)cyphertext[0]); + + // Ultima riga scritta a mano via uart_write_str+flush, non printf: + // il testbench esegue $finish appena uart_reading_byte==0, cosa che + // puo' avvenire tra due printf consecutive prima che la riga finale + // sia trasmessa (stesso problema e stessa soluzione dei port SMP + // precedenti). + { + char buf[64]; char tmp[16]; int n = 0; char *p = buf; + *p++='C';*p++='h';*p++='e';*p++='c';*p++='k';*p++='s';*p++='u';*p++='m'; + *p++=':';*p++=' '; + unsigned int v = checksum; + if (v == 0u) { tmp[n++] = '0'; } + else { unsigned int q = v; while (q) { tmp[n++] = (char)('0' + (q % 10u)); q /= 10u; } } + for (int j = n - 1; j >= 0; j--) *p++ = tmp[j]; + *p++ = '\r'; *p++ = '\n'; + uart_write_str(&__base_uart, buf, (int)(p - buf)); + uart_write_flush(&__base_uart); + } + } else { + // Hart 1 non deve uscire da main() (_exit() provocherebbe $finish + // prima che hart 0 termini le stampe) e resta parcheggiato in wfi + // con interrupt disabilitati. + set_mie(0); + while (1) { asm volatile ("wfi" ::: "memory"); } + } + + return 0; +} diff --git a/sw/tests/bare-metal/hostd/old_obp_test/vers.1 32 x 32 bit/obpmark_1_1.c b/sw/tests/bare-metal/hostd/old_obp_test/vers.1 32 x 32 bit/obpmark_1_1.c new file mode 100644 index 000000000..a4fd8020e --- /dev/null +++ b/sw/tests/bare-metal/hostd/old_obp_test/vers.1 32 x 32 bit/obpmark_1_1.c @@ -0,0 +1,322 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.1 - Image Calibration and Correction +// Bare-metal port for Astral/Carfield (CVA6, RV64) +// +// Porting notes: +// - Algorithm (processing.c) copiato identico dall'originale +// - malloc/free sostituiti con array statici globali +// - File I/O sostituito con generazione sintetica (da util_data_rand.c, seed 21121993) +// - clock() sostituito con CSR mcycle (RISC-V cycle counter) +// - Dimensione immagine ridotta a IMG_W x IMG_H per simulazione RTL + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "params.h" +#include "util.h" +#include "car_util.h" +#include "printf.h" +#include + +// ============================================================ +// Configurazione benchmark +// ============================================================ +#define IMG_W 32 // larghezza immagine +#define IMG_H 32 // altezza immagine +#define NUM_FRAMES 8 // frame di dati +#define NUM_NEIGHBOURS 4 // frame vicini per scrubbing (2 prima + 2 dopo) +#define TOTAL_FRAMES (NUM_FRAMES + NUM_NEIGHBOURS) // 12 frame totali +#define OFFSET_NEIGHBOURS 2 // offset iniziale loop (come nell'originale) + +// ============================================================ +// Strutture dati (identiche all'originale, senza malloc) +// ============================================================ + +// Backing storage statico per tutti i frame 16-bit +static uint16_t _frames_data [TOTAL_FRAMES][IMG_H * IMG_W]; +static uint16_t _offsets_data [IMG_H * IMG_W]; +static uint16_t _gains_data [IMG_H * IMG_W]; +static uint8_t _badpix_data [IMG_H * IMG_W]; +static uint32_t _output_data [(IMG_H/2) * (IMG_W/2)]; +static uint32_t _binned_data [(IMG_H/2) * (IMG_W/2)]; + +// Struct frame (come nell'originale obpmark_image.h) +typedef struct { uint16_t *f; unsigned int w; unsigned int h; } frame16_t; +typedef struct { uint8_t *f; unsigned int w; unsigned int h; } frame8_t; +typedef struct { uint32_t *f; unsigned int w; unsigned int h; } frame32_t; + +// Array di frame (come nell'originale) +static frame16_t frames [TOTAL_FRAMES]; +static frame16_t offsets_frame; +static frame16_t gains_frame; +static frame8_t bad_pixels_frame; +static frame32_t output_frame; +static frame32_t binned_frame; + +// PIXEL macro identica all'originale (accesso flat 1D: x*h + y) +#define PIXEL(frame, x, y) ((frame)->f[(x) * (frame)->h + (y)]) + +// ============================================================ +// Inizializzazione strutture (sostituisce malloc + memset) +// ============================================================ +static void init_frames(void) { + unsigned int i; + for (i = 0; i < TOTAL_FRAMES; i++) { + frames[i].f = _frames_data[i]; + frames[i].w = IMG_W; + frames[i].h = IMG_H; + } + offsets_frame.f = _offsets_data; + offsets_frame.w = IMG_W; + offsets_frame.h = IMG_H; + + gains_frame.f = _gains_data; + gains_frame.w = IMG_W; + gains_frame.h = IMG_H; + + bad_pixels_frame.f = _badpix_data; + bad_pixels_frame.w = IMG_W; + bad_pixels_frame.h = IMG_H; + + output_frame.f = _output_data; + output_frame.w = IMG_W / 2; + output_frame.h = IMG_H / 2; + + binned_frame.f = _binned_data; + binned_frame.w = IMG_W / 2; + binned_frame.h = IMG_H / 2; +} + +// ============================================================ +// Generazione dati sintetici (da util_data_rand.c originale) +// Seed identico: 21121993 +// ============================================================ +static unsigned int _rand_state; + +// LCG compatibile con rand() della glibc +static unsigned int bm_rand(void) { + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} + +static void gen_rand_data(void) { + unsigned int f, x, y; + unsigned int randnumber = 65535; + + _rand_state = 21121993u; // stesso seed di util_data_rand.c + + // Frame di input (identico a benchmark_gen_rand_data) + for (f = 0; f < TOTAL_FRAMES; f++) { + for (x = 0; x < IMG_W; x++) { + for (y = 0; y < IMG_H; y++) { + PIXEL(&frames[f], x, y) = (uint16_t)(bm_rand() % randnumber); + } + } + } + + // Offset map + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&offsets_frame, x, y) = (uint16_t)(bm_rand() % randnumber); + + // Gain map + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&gains_frame, x, y) = (uint16_t)(bm_rand() % randnumber); + + // Bad pixel map + for (x = 0; x < IMG_W; x++) + for (y = 0; y < IMG_H; y++) + PIXEL(&bad_pixels_frame, x, y) = ((uint8_t)(bm_rand() % randnumber)) & 0x01u; + + // Azzera output + for (x = 0; x < IMG_W/2; x++) + for (y = 0; y < IMG_H/2; y++) + PIXEL(&output_frame, x, y) = 0u; +} + +// ============================================================ +// KERNEL FUNCTIONS - copiate identiche da processing.c +// ============================================================ + +// [I] Bias offset correction +static void f_offset(frame16_t *frame, frame16_t *offsets) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) { + for (y = 0; y < frame->h; y++) { + PIXEL(frame, x, y) -= PIXEL(offsets, x, y); + } + } +} + +// Helper per f_mask_replace +static uint32_t f_neighbour_masked_sum(frame16_t *frame, frame8_t *mask, + int x_mid, int y_mid) { + int x, y; + int x_start = -1, x_stop = 1; + int y_start = -1, y_stop = 1; + unsigned int n_sum = 0; + uint32_t sum = 0, mean; + + if (x_mid == 0) x_start = 0; + else if (x_mid == (int)(frame->w - 1)) x_stop = 0; + if (y_mid == 0) y_start = 0; + else if (y_mid == (int)(frame->h - 1)) y_stop = 0; + + for (x = x_start; x < (x_stop + 1); x++) { + for (y = y_start; y < (y_stop + 1); y++) { + if (PIXEL(mask, (x_mid + x), (y_mid + y)) == 0) { + sum += PIXEL(frame, (x_mid + x), (y_mid + y)); + n_sum++; + } + } + } + mean = (n_sum == 0) ? 0u : sum / n_sum; + return mean; +} + +// [II] Bad pixel correction +static void f_mask_replace(frame16_t *frame, frame8_t *mask) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) { + for (y = 0; y < frame->h; y++) { + if (PIXEL(mask, x, y) == 1) { + PIXEL(frame, x, y) = (uint16_t)f_neighbour_masked_sum( + frame, mask, (int)x, (int)y); + } + } + } +} + +// [III] Radiation scrubbing +static void f_scrub(frame16_t *frame, frame16_t *fs, unsigned int frame_i) { + unsigned int x, y; + static unsigned int num_neighbour = 4; + uint32_t sum, mean, thr; + + for (x = 0; x < frame->w; x++) { + for (y = 0; y < frame->h; y++) { + sum = PIXEL(&fs[frame_i-2], x, y) + + PIXEL(&fs[frame_i-1], x, y) + + PIXEL(&fs[frame_i+1], x, y) + + PIXEL(&fs[frame_i+2], x, y); + mean = sum / num_neighbour; + thr = 2 * mean; + if (PIXEL(frame, x, y) > thr) { + PIXEL(frame, x, y) = (uint16_t)mean; + } + } + } +} + +// [IV] Gain correction +static void f_gain(frame16_t *frame, frame16_t *gains) { + unsigned int x, y; + for (x = 0; x < frame->w; x++) { + for (y = 0; y < frame->h; y++) { + PIXEL(frame, x, y) = (uint16_t)( + ((uint32_t)PIXEL(frame, x, y) * + (uint32_t)PIXEL(gains, x, y)) >> 16); + } + } +} + +// [V] 2x2 Spatial binning +static void f_2x2_bin(frame16_t *frame, frame32_t *binned) { + unsigned int x, y, x2, y2; + x2 = 0; + for (x = 0; x < frame->w; x += 2) { + y2 = 0; + for (y = 0; y < frame->h; y += 2) { + PIXEL(binned, x2, y2) = PIXEL(frame, x, y ) + + PIXEL(frame, (x+1), y ) + + PIXEL(frame, x, (y+1)) + + PIXEL(frame, (x+1),(y+1)); + ++y2; + } + ++x2; + } +} + +// [VI] Co-adding frames +static void f_coadd(frame32_t *sum_frame, frame32_t *add_frame) { + unsigned int x, y; + for (x = 0; x < sum_frame->w; x++) { + for (y = 0; y < sum_frame->h; y++) { + PIXEL(sum_frame, x, y) += PIXEL(add_frame, x, y); + } + } +} + +// ============================================================ +// Pipeline identica a process_benchmark() di device.c +// ============================================================ +static void prepare_image_frame(frame16_t *frame) { + f_offset(frame, &offsets_frame); // [I] + f_mask_replace(frame, &bad_pixels_frame); // [II] +} + +static void proc_image_frame(frame16_t *frame, unsigned int frame_i) { + f_scrub(frame, frames, frame_i); // [III] + f_gain(frame, &gains_frame); // [IV] + f_2x2_bin(frame, &binned_frame); // [V] + f_coadd(&output_frame, &binned_frame); // [VI] +} + +// ============================================================ +// Cycle counter RISC-V (sostituisce clock()) +// ============================================================ +static inline uint64_t read_mcycle(void) { + uint64_t cycles; + asm volatile ("csrr %0, mcycle" : "=r"(cycles)); + return cycles; +} + +// ============================================================ +// Main +// ============================================================ +int main(void) { + // Metti a dormire gli hart secondari (come in helloworld) + if (hart_id() != 0) wfi(); + + // Inizializza HW Carfield/Astral + car_init_start(); + + printf("OBPMark #1.1 - Image Calibration and Correction\r\n"); + printf("Bare-metal port - Astral (CVA6, RV64)\r\n"); + printf("Image: %dx%d pixels, %d frames\r\n", IMG_W, IMG_H, NUM_FRAMES); + + // Inizializza strutture frame (puntatori ai buffer statici) + init_frames(); + + // Genera dati sintetici (identico a -r nell'originale, seed 21121993) + gen_rand_data(); + + // ------------------------------------------------------- + // BENCHMARK: loop identico a process_benchmark() in device.c + // ------------------------------------------------------- + uint64_t t_start = read_mcycle(); + + unsigned int frame_i; + for (frame_i = OFFSET_NEIGHBOURS; + frame_i < TOTAL_FRAMES - OFFSET_NEIGHBOURS; + frame_i++) { + // Prepara frame frame_i+2 (look-ahead, come nell'originale) + prepare_image_frame(&frames[frame_i + 2]); + // Processa frame frame_i + proc_image_frame(&frames[frame_i], frame_i); + } + + uint64_t t_end = read_mcycle(); + uint64_t cycles = t_end - t_start; + // ------------------------------------------------------- + + printf("Done.\r\n"); + printf("Elapsed cycles: %llu\r\n", (unsigned long long)cycles); + printf("Output[0][0]: %u\r\n", (unsigned int)PIXEL(&output_frame, 0, 0)); + + return 0; +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_pulpd_divtest/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_pulpd_divtest/Makefile new file mode 100644 index 000000000..b9e8bd208 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_pulpd_divtest/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_2_pulpd_divtest.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_pulpd_divtest/obpmark_1_2_pulpd_divtest.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_pulpd_divtest/obpmark_1_2_pulpd_divtest.c new file mode 100644 index 000000000..1d4147807 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_pulpd_divtest/obpmark_1_2_pulpd_divtest.c @@ -0,0 +1,149 @@ +/* + * Isolated runtime test of the float primitives behind m_divf() in + * obpmark_1_2_pulpd_seq.c. + * + * Context: checkpoint tracing on the cluster showed m_divf(16.0f, 4.0f) + * returning +Inf (0x7F800000) inside SAR_multilook. This test reproduces + * the computation step by step with VOLATILE inputs, so that nothing can + * be constant-folded at compile time (a call with literal arguments would + * be evaluated by the compiler and never touch the cluster FPU), and + * prints the raw bit pattern of every intermediate value (%u only: the + * runtime's minimal printf has no float support). + * + * The same source builds on the host with -DHOST_REF to produce the + * expected output (the operations used are plain IEEE-754 add/sub/mul and + * bit moves, deterministic across targets unless fused multiply-add + * contraction changes a rounding). Diff the cluster output against it. + * No native float division is used anywhere: this core has no div/sqrt + * unit and fdiv.s would trap. + */ +#ifdef HOST_REF +#include +#include +#include +#else +#include "pulp.h" +#include +#include +#include +#endif + +static inline uint32_t f_to_bits(float x){ uint32_t u; memcpy(&u, &x, sizeof(u)); return u; } +static inline float bits_to_f(uint32_t u){ float x; memcpy(&x, &u, sizeof(x)); return x; } + +/* exact copy of m_divf from obpmark_1_2_pulpd_seq.c */ +static float m_divf(float a, float b){ + if (b == 0.0f) return 0.0f; + uint32_t u = f_to_bits(b); + u = 0x7EF127EA - u; + float r = bits_to_f(u); + r = r * (2.0f - b * r); + r = r * (2.0f - b * r); + return a * r; +} + +/* same body, forced out of line (the empty asm keeps the two functions + * from being merged by identical-code folding) */ +static float __attribute__((noinline)) m_divf_ni(float a, float b){ + __asm__ volatile ("" ::: "memory"); + if (b == 0.0f) return 0.0f; + uint32_t u = f_to_bits(b); + u = 0x7EF127EA - u; + float r = bits_to_f(u); + r = r * (2.0f - b * r); + r = r * (2.0f - b * r); + return a * r; +} + +/* exact copy of m_sqrtf from obpmark_1_2_pulpd_seq.c */ +static float m_sqrtf(float x){ + if (x <= 0.0f) return 0.0f; + uint32_t u = f_to_bits(x); + u = 0x5f3759df - (u >> 1); + float y = bits_to_f(u); + y = y * (1.5f - 0.5f*x*y*y); + y = y * (1.5f - 0.5f*x*y*y); + return x * y; +} + +static void divtest_body(void){ + volatile uint32_t v_a = 16u, v_w = 4u; /* runtime integers */ + volatile float v_fa = 16.0f, v_fb = 4.0f; /* runtime floats */ + + /* integer -> float conversion, as done on params->asize / image_w */ + float fa = (float)v_a; + float fw = (float)v_w; + printf("T01 (float)16u bits=%u\n", f_to_bits(fa)); + printf("T02 (float)4u bits=%u\n", f_to_bits(fw)); + + /* basic FPU operations on runtime values */ + float one = 1.0f, two = v_fb * 0.5f; + printf("T03 1+2 bits=%u\n", f_to_bits(one + (two))); + printf("T04 4*0.25 bits=%u\n", f_to_bits(v_fb * 0.25f)); + printf("T05 (int)4.0f=%u\n", (unsigned)(int)v_fb); + + /* m_divf(16,4) step by step, nothing foldable */ + float a = v_fa, b = v_fb; + uint32_t bb = f_to_bits(b); + printf("T06 bits(b)=%u\n", bb); + uint32_t u0 = 0x7EF127EAu - bb; + printf("T07 u0=%u\n", u0); + float r0 = bits_to_f(u0); + printf("T08 r0 bits=%u\n", f_to_bits(r0)); + float t1 = b * r0; + printf("T09 b*r0 bits=%u\n", f_to_bits(t1)); + float r1 = r0 * (2.0f - t1); + printf("T10 r1 bits=%u\n", f_to_bits(r1)); + float t2 = b * r1; + printf("T11 b*r1 bits=%u\n", f_to_bits(t2)); + float r2 = r1 * (2.0f - t2); + printf("T12 r2 bits=%u\n", f_to_bits(r2)); + printf("T13 a*r2 bits=%u\n", f_to_bits(a * r2)); + + /* the functions as used by the benchmark, same runtime inputs */ + printf("T14 m_divf(16,4) bits=%u\n", f_to_bits(m_divf(fa, fw))); + printf("T15 m_divf_ni(16,4) bits=%u\n", f_to_bits(m_divf_ni(fa, fw))); + + /* several pairs, read through volatile pointers */ + static const float A[] = {16.0f, 8.0f, 1.0f, 100.0f, 256.0f, 6.2831855f}; + static const float B[] = { 4.0f, 4.0f, 16.0f, 7.0f, 3.0f, 32.0f}; + const volatile float *pa = A, *pb = B; + for (unsigned i = 0; i < 6; i++){ + float x = pa[i], y = pb[i]; + printf("P%u m_divf=%u m_divf_ni=%u\n", i, + f_to_bits(m_divf(x, y)), f_to_bits(m_divf_ni(x, y))); + } + + /* software sqrt */ + float s16 = v_fa; + printf("S1 m_sqrtf(16) bits=%u\n", f_to_bits(m_sqrtf(s16))); + printf("S2 m_sqrtf(2) bits=%u\n", f_to_bits(m_sqrtf(v_fb * 0.5f))); +} + +#ifdef HOST_REF +int main(void){ divtest_body(); return 0; } +#else +void check_divtest(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_1_2_pulpd_divtest", .test = check_divtest }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + if (get_core_id() == 0) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_divtest(testresult_t *result, void (*start)(), void (*stop)()) { + if (get_core_id() != 0) return; + divtest_body(); + result->errors = 0; +} +#endif diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag/Makefile new file mode 100644 index 000000000..a907ed504 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_2_pulpd_seq_diag.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag/obpmark_1_2_pulpd_seq_diag.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag/obpmark_1_2_pulpd_seq_diag.c new file mode 100644 index 000000000..d18b9c145 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag/obpmark_1_2_pulpd_seq_diag.c @@ -0,0 +1,662 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL port for the Astral PULP cluster (1x RV32 core) - +// FULL pipeline, ported from obpmark_1_2_host_seq_full.c (CVA6/RV64 host). +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository - see the host +// sequential port's header comment for the full derivation and size +// calculations, unchanged here (same RSIZE/ASIZE/APATCH/RVALID/NPATCH, +// same LCG seed, same fixed physical parameters). +// +// ============================================================ +// CLUSTER-SPECIFIC DIFFERENCE FROM THE HOST PORT: floating-point division +// and square root. +// +// Verified on this project's actual hardware configuration (not assumed): +// - hw/carfield.sv sets EnablePrivateFpu: 1 for the PULP cluster cores - +// add/sub/mul in hardware (confirmed by compiling `a+b`/`a*b` at +// -march=rv32imfcxgap9 -mabi=ilp32f: emits fadd.s/fmul.s directly). +// - hw/carfield.sv sets EnablePrivateFpDivSqrt: 0 and NumSharedFpu: 0 - +// the div/sqrt unit is NOT synthesized in any core's FPU +// (pulp_cluster.sv instantiates core_region with +// .FP_DIVSQRT(Cfg.EnablePrivateFpDivSqrt), which is 0). The compiler +// is unaware of this at -march=rv32imfcxgap9 and happily emits +// fdiv.s/fsqrt.s (confirmed: `a/b` compiles straight to fdiv.s) - +// those would trap as illegal instructions on this cluster at runtime. +// +// Consequence: every `/` on floats in the computational code below is +// replaced by a call to m_divf() (software reciprocal-refine division, +// never emits fdiv.s), and sqrt uses the Step-1-validated software m_sqrtf +// (bit-hack + Newton-Raphson) instead of the host port's inline fsqrt.s +// asm, which does not exist on this ISA at all. Plain +, -, * on floats +// are left as native operators: they compile to real hardware +// fadd.s/fsub.s/fmul.s on this core, unlike on the host's rv32imcxgap9 +// cluster tests seen elsewhere in this project (no `f` in march there). +// ============================================================ +// +// PULP-RUNTIME STRUCTURE: verified directly against this project's actual +// obpmark_1_1_pulp_seq.c. Same testcase_t/run_suite framework, same +// rt_cluster_id()/get_core_id()/synch_barrier() bring-up, same cpu_perf_* +// cycle counter (CSR_PCER_CYCLES), same .heapsram placement for all data +// buffers (avoids the large TCDM PT_LOAD write sequence that breaks the +// cluster boot mechanism, per that file's own comment). + +#include "pulp.h" +#include +#include + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c), adapted for the +// cluster: sqrtf is the SOFTWARE bit-hack+Newton version (no fsqrt.s on +// this core), and a new m_divf replaces the `/` operator wherever the +// computational code needs floating-point division (no fdiv.s either). +// sinf/cosf/log2f/atan2f/floorf/ceilf/roundf are unchanged from the host +// port: none of them use hardware division or sqrt internally. +// ============================================================ +#define PI_F 3.14159265358979323846f + +// Software sqrt (Step 1: bit-hack initial guess + 2x Newton-Raphson, +// ~4.6e-6 relative error measured against real libm). No fsqrt.s: this +// core's FPU has no synthesized div/sqrt unit. +static float m_sqrtf(float x){ + if (x <= 0.0f) return 0.0f; + union { float f; uint32_t u; } v; v.f = x; + v.u = 0x5f3759df - (v.u >> 1); + float y = v.f; + y = y * (1.5f - 0.5f*x*y*y); + y = y * (1.5f - 0.5f*x*y*y); + return x * y; +} + +// Software division: Newton-Raphson refinement of a bit-hack reciprocal +// estimate, then one final multiply. Never emits fdiv.s. Accuracy is +// exercised implicitly through the whole SAR pipeline; if higher precision +// is ever needed, add one more Newton iteration below (cheap: one more +// fmul.s/fsub.s pair, both hardware on this core). +static float m_divf(float a, float b){ + if (b == 0.0f) return 0.0f; + union { float f; uint32_t u; } v; v.f = b; + v.u = 0x7EF127EA - v.u; // initial reciprocal estimate + float r = v.f; + r = r * (2.0f - b * r); // Newton-Raphson iteration 1 + r = r * (2.0f - b * r); // Newton-Raphson iteration 2 + return a * r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = m_divf(m - 1.0f, m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? m_divf(ay, ax) : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? m_divf(ax, ay) : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -m_divf(2.0f * PI_F, (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] = m_divf(data[2*i], (float)nn); data[2*i+1] = m_divf(data[2*i+1], (float)nn); } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap). Placed in .heapsram (TCDM), same +// pattern as obpmark_1_1_pulp_seq.c: this keeps them out of the ELF +// PT_LOAD payload, avoiding the large TCDM write sequence in +// load_binary() that breaks the cluster boot mechanism. +// ============================================================ +#define HEAPSRAM __attribute__((section(".heapsram"))) + +static HEAPSRAM float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static HEAPSRAM float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static HEAPSRAM float g_rrf[RRF_LEN]; +static HEAPSRAM float g_arf[ARF_LEN]; +static HEAPSRAM float g_aux[AUX_LEN]; +static HEAPSRAM uint32_t g_offsets[OFFS_LEN]; +static HEAPSRAM float g_ml_data[OUT_W * OUT_H]; +static HEAPSRAM uint8_t g_output_image[OUT_W * OUT_H]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * m_divf((float)bm_rand(), 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * m_divf(1.0f, fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = m_divf(mean, (float)width); + return m_divf(mean * params->PRF, 2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * m_divf(params->PRF, (float)params->avalid) + fDc; + float ratio = m_divf(params->lambda * delta, 2.0f*params->vr); + float offset = (m_divf(1.0f, m_sqrtf(1.0f - ratio*ratio)) - 1.0f) + * (params->ro + (float)i * m_divf(c_light, 2.0f*params->fs)); + offset = m_roundf(m_divf(offset, m_divf(c_light, 2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*m_divf(c_light, 2.0f*params->fs); + float ratio = m_divf(params->lambda*fDc, 2.0f*params->vr); + float rdc = m_divf(rnge, m_sqrtf(1.0f - ratio*ratio)); + float tauz = m_divf(rdc*m_divf(params->lambda, 10.0f) * 0.8f, params->vr); + float chirp = m_divf(m_divf(-(2.0f*params->vr*params->vr), params->lambda), rdc); + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = m_divf((float)params->asize, (float)image_w); + float sy = m_divf((float)params->rvalid, (float)image_w); + uint32_t nfx = (uint32_t)m_floorf(m_divf((float)params->avalid, sx)); + uint32_t nfy = (uint32_t)m_floorf(m_divf((float)params->rvalid, sy)); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = m_divf(fimg, (float)(isx*isy)); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = m_divf(256.0f, (vmax - vmin + 1e-30f)); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// DIAGNOSTIC: sum of raw IEEE754 bit patterns of a float buffer - +// order-sensitive, catches any numerical difference, prints via %u since +// this runtime's minimal printf does not support %f (confirmed: every +// existing printf in this project's cluster/host ports uses %u/%d only). +static uint32_t buf_checksum_u32(const float *buf, unsigned int n){ + uint32_t s = 0; + for (unsigned int i = 0; i < n; i++){ + union { float f; uint32_t u; } v; v.f = buf[i]; + s += v.u; + } + return s; +} +static uint32_t f_bits(float x){ + union { float f; uint32_t u; } v; v.f = x; + return v.u; +} + +static void SAR_focus(radar_params_t *params, float *out_max, float *out_min){ + *out_max = -1e30f; + *out_min = 1e30f; + + SAR_range_ref(g_rrf, params); + printf("CHK after_range_ref rrf=%u\n", buf_checksum_u32(g_rrf, RRF_LEN)); + + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + printf("CHK after_DCE fDc_bits=%u\n", f_bits(fDc)); + + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + { uint32_t s=0; for(unsigned i=0;inpatch; patch++){ + SAR_range_compression(g_range_data, RANGE_H, RANGE_W, g_rrf); + printf("CHK after_range_compr range_data=%u\n", buf_checksum_u32(g_range_data, RANGE_H*RANGE_W)); + + complex_transpose(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->apatch, params->rvalid); + printf("CHK after_transpose1 azimuth_data=%u\n", buf_checksum_u32(g_azimuth_data, AZIM_H*AZIM_W)); + + SAR_rcmc(g_azimuth_data, AZIM_H, AZIM_W, g_offsets); + printf("CHK after_rcmc azimuth_data=%u\n", buf_checksum_u32(g_azimuth_data, AZIM_H*AZIM_W)); + + SAR_azimuth_compression(g_azimuth_data, AZIM_H, AZIM_W, g_arf); + printf("CHK after_azimuth_compr azimuth_data=%u\n", buf_checksum_u32(g_azimuth_data, AZIM_H*AZIM_W)); + + complex_transpose(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->rvalid, params->apatch); + printf("CHK after_transpose2 range_data=%u\n", buf_checksum_u32(g_range_data, RANGE_H*RANGE_W)); + + SAR_multilook(g_range_data, g_ml_data, OUT_W, params, patch, out_max, out_min); + printf("CHK after_multilook ml_data=%u max_bits=%u min_bits=%u\n", + buf_checksum_u32(g_ml_data, OUT_W*OUT_H), f_bits(*out_max), f_bits(*out_min)); + } + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); + { uint32_t s=0; for(unsigned i=0;i 20 ns per cycle */ + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("OBPMark #1.2 - Radar SAR (PULP cluster seq, DIAGNOSTIC)\n"); + printf("PULP cluster sequential (1 core, RV32) - Astral @ 50 MHz\n"); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Output[0][0] : %u\n", (unsigned)g_output_image[0]); + printf(" Checksum : %u\n", (unsigned)checksum); + + // No fixed expected value here (unlike 1.1's EXPECTED_OUTPUT_0_0): + // this is a new port with no prior cluster run to compare against. + // Cross-check this Output[0][0]/checksum against the host sequential + // port's values (obpmark_1_2_host_seq_full.c) instead - they must + // match exactly, since both compute the identical algorithm on + // identical synthetic input (same LCG seed). + result->errors = 0; +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag2/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag2/Makefile new file mode 100644 index 000000000..e3540bffa --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag2/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_2_pulpd_seq_diag2.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag2/obpmark_1_2_pulpd_seq_diag2.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag2/obpmark_1_2_pulpd_seq_diag2.c new file mode 100644 index 000000000..5ca2b481a --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_diag2/obpmark_1_2_pulpd_seq_diag2.c @@ -0,0 +1,692 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL port for the Astral PULP cluster (1x RV32 core) - +// FULL pipeline, ported from obpmark_1_2_host_seq_full.c (CVA6/RV64 host). +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository - see the host +// sequential port's header comment for the full derivation and size +// calculations, unchanged here (same RSIZE/ASIZE/APATCH/RVALID/NPATCH, +// same LCG seed, same fixed physical parameters). +// +// ============================================================ +// CLUSTER-SPECIFIC DIFFERENCE FROM THE HOST PORT: floating-point division +// and square root. +// +// Verified on this project's actual hardware configuration (not assumed): +// - hw/carfield.sv sets EnablePrivateFpu: 1 for the PULP cluster cores - +// add/sub/mul in hardware (confirmed by compiling `a+b`/`a*b` at +// -march=rv32imfcxgap9 -mabi=ilp32f: emits fadd.s/fmul.s directly). +// - hw/carfield.sv sets EnablePrivateFpDivSqrt: 0 and NumSharedFpu: 0 - +// the div/sqrt unit is NOT synthesized in any core's FPU +// (pulp_cluster.sv instantiates core_region with +// .FP_DIVSQRT(Cfg.EnablePrivateFpDivSqrt), which is 0). The compiler +// is unaware of this at -march=rv32imfcxgap9 and happily emits +// fdiv.s/fsqrt.s (confirmed: `a/b` compiles straight to fdiv.s) - +// those would trap as illegal instructions on this cluster at runtime. +// +// Consequence: every `/` on floats in the computational code below is +// replaced by a call to m_divf() (software reciprocal-refine division, +// never emits fdiv.s), and sqrt uses the Step-1-validated software m_sqrtf +// (bit-hack + Newton-Raphson) instead of the host port's inline fsqrt.s +// asm, which does not exist on this ISA at all. Plain +, -, * on floats +// are left as native operators: they compile to real hardware +// fadd.s/fsub.s/fmul.s on this core, unlike on the host's rv32imcxgap9 +// cluster tests seen elsewhere in this project (no `f` in march there). +// ============================================================ +// +// PULP-RUNTIME STRUCTURE: verified directly against this project's actual +// obpmark_1_1_pulp_seq.c. Same testcase_t/run_suite framework, same +// rt_cluster_id()/get_core_id()/synch_barrier() bring-up, same cpu_perf_* +// cycle counter (CSR_PCER_CYCLES), same .heapsram placement for all data +// buffers (avoids the large TCDM PT_LOAD write sequence that breaks the +// cluster boot mechanism, per that file's own comment). + +#include "pulp.h" +#include +#include + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c), adapted for the +// cluster: sqrtf is the SOFTWARE bit-hack+Newton version (no fsqrt.s on +// this core), and a new m_divf replaces the `/` operator wherever the +// computational code needs floating-point division (no fdiv.s either). +// sinf/cosf/log2f/atan2f/floorf/ceilf/roundf are unchanged from the host +// port: none of them use hardware division or sqrt internally. +// ============================================================ +#define PI_F 3.14159265358979323846f + +// Software sqrt (Step 1: bit-hack initial guess + 2x Newton-Raphson, +// ~4.6e-6 relative error measured against real libm). No fsqrt.s: this +// core's FPU has no synthesized div/sqrt unit. +static float m_sqrtf(float x){ + if (x <= 0.0f) return 0.0f; + union { float f; uint32_t u; } v; v.f = x; + v.u = 0x5f3759df - (v.u >> 1); + float y = v.f; + y = y * (1.5f - 0.5f*x*y*y); + y = y * (1.5f - 0.5f*x*y*y); + return x * y; +} + +// Software division: Newton-Raphson refinement of a bit-hack reciprocal +// estimate, then one final multiply. Never emits fdiv.s. Accuracy is +// exercised implicitly through the whole SAR pipeline; if higher precision +// is ever needed, add one more Newton iteration below (cheap: one more +// fmul.s/fsub.s pair, both hardware on this core). +static float m_divf(float a, float b){ + if (b == 0.0f) return 0.0f; + union { float f; uint32_t u; } v; v.f = b; + v.u = 0x7EF127EA - v.u; // initial reciprocal estimate + float r = v.f; + r = r * (2.0f - b * r); // Newton-Raphson iteration 1 + r = r * (2.0f - b * r); // Newton-Raphson iteration 2 + return a * r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = m_divf(m - 1.0f, m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? m_divf(ay, ax) : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? m_divf(ax, ay) : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -m_divf(2.0f * PI_F, (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] = m_divf(data[2*i], (float)nn); data[2*i+1] = m_divf(data[2*i+1], (float)nn); } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap). Placed in .heapsram (TCDM), same +// pattern as obpmark_1_1_pulp_seq.c: this keeps them out of the ELF +// PT_LOAD payload, avoiding the large TCDM write sequence in +// load_binary() that breaks the cluster boot mechanism. +// ============================================================ +#define HEAPSRAM __attribute__((section(".heapsram"))) + +static HEAPSRAM float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static HEAPSRAM float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static HEAPSRAM float g_rrf[RRF_LEN]; +static HEAPSRAM float g_arf[ARF_LEN]; +static HEAPSRAM float g_aux[AUX_LEN]; +static HEAPSRAM uint32_t g_offsets[OFFS_LEN]; +static HEAPSRAM float g_ml_data[OUT_W * OUT_H]; +static HEAPSRAM uint8_t g_output_image[OUT_W * OUT_H]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * m_divf((float)bm_rand(), 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * m_divf(1.0f, fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = m_divf(mean, (float)width); + return m_divf(mean * params->PRF, 2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * m_divf(params->PRF, (float)params->avalid) + fDc; + float ratio = m_divf(params->lambda * delta, 2.0f*params->vr); + float offset = (m_divf(1.0f, m_sqrtf(1.0f - ratio*ratio)) - 1.0f) + * (params->ro + (float)i * m_divf(c_light, 2.0f*params->fs)); + offset = m_roundf(m_divf(offset, m_divf(c_light, 2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*m_divf(c_light, 2.0f*params->fs); + float ratio = m_divf(params->lambda*fDc, 2.0f*params->vr); + float rdc = m_divf(rnge, m_sqrtf(1.0f - ratio*ratio)); + float tauz = m_divf(rdc*m_divf(params->lambda, 10.0f) * 0.8f, params->vr); + float chirp = m_divf(m_divf(-(2.0f*params->vr*params->vr), params->lambda), rdc); + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = m_divf((float)params->asize, (float)image_w); + float sy = m_divf((float)params->rvalid, (float)image_w); + uint32_t nfx = (uint32_t)m_floorf(m_divf((float)params->avalid, sx)); + uint32_t nfy = (uint32_t)m_floorf(m_divf((float)params->rvalid, sy)); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + // DIAGNOSTIC: print every derived parameter that controls the loop + // bounds. isx/isy are the prime suspect: if either is 0, the inner + // accumulation loop (ix vmax) vmax = value; + if (value < vmin) vmin = value; + + // DIAGNOSTIC: raw bits of every individual pixel value, plus + // the accumulated fimg before the final division/log2, so we + // can tell whether fimg itself is zero (loop never ran) or + // whether it's the divide/log2 step that degenerates it. + { union { float f; uint32_t u; } uf, uv; + uf.f = fimg; uv.f = value; + printf("MLK pixel i=%u j=%u fimg_bits=%u value_bits=%u\n", i, j, uf.u, uv.u); } + } + } + *out_max = vmax; *out_min = vmin; + + // DIAGNOSTIC: also print vmax/vmin bits right after the loop, and the + // scale computed inside quantize's own logic replicated here for + // visibility (quantize itself is called separately by SAR_focus, but + // seeing vmax/vmin here already tells us if they are both exactly 0, + // which is the condition that makes quantize's scale computation + // degenerate). + { union { float f; uint32_t u; } uma, umi; + uma.f = vmax; umi.f = vmin; + printf("MLK final vmax_bits=%u vmin_bits=%u\n", uma.u, umi.u); } +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = m_divf(256.0f, (vmax - vmin + 1e-30f)); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +// DIAGNOSTIC: sum of raw IEEE754 bit patterns of a float buffer - +// order-sensitive, catches any numerical difference, prints via %u since +// this runtime's minimal printf does not support %f (confirmed: every +// existing printf in this project's cluster/host ports uses %u/%d only). +static uint32_t buf_checksum_u32(const float *buf, unsigned int n){ + uint32_t s = 0; + for (unsigned int i = 0; i < n; i++){ + union { float f; uint32_t u; } v; v.f = buf[i]; + s += v.u; + } + return s; +} +static uint32_t f_bits(float x){ + union { float f; uint32_t u; } v; v.f = x; + return v.u; +} + +static void SAR_focus(radar_params_t *params, float *out_max, float *out_min){ + *out_max = -1e30f; + *out_min = 1e30f; + + SAR_range_ref(g_rrf, params); + printf("CHK after_range_ref rrf=%u\n", buf_checksum_u32(g_rrf, RRF_LEN)); + + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + printf("CHK after_DCE fDc_bits=%u\n", f_bits(fDc)); + + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + { uint32_t s=0; for(unsigned i=0;inpatch; patch++){ + SAR_range_compression(g_range_data, RANGE_H, RANGE_W, g_rrf); + printf("CHK after_range_compr range_data=%u\n", buf_checksum_u32(g_range_data, RANGE_H*RANGE_W)); + + complex_transpose(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->apatch, params->rvalid); + printf("CHK after_transpose1 azimuth_data=%u\n", buf_checksum_u32(g_azimuth_data, AZIM_H*AZIM_W)); + + SAR_rcmc(g_azimuth_data, AZIM_H, AZIM_W, g_offsets); + printf("CHK after_rcmc azimuth_data=%u\n", buf_checksum_u32(g_azimuth_data, AZIM_H*AZIM_W)); + + SAR_azimuth_compression(g_azimuth_data, AZIM_H, AZIM_W, g_arf); + printf("CHK after_azimuth_compr azimuth_data=%u\n", buf_checksum_u32(g_azimuth_data, AZIM_H*AZIM_W)); + + complex_transpose(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->rvalid, params->apatch); + printf("CHK after_transpose2 range_data=%u\n", buf_checksum_u32(g_range_data, RANGE_H*RANGE_W)); + + SAR_multilook(g_range_data, g_ml_data, OUT_W, params, patch, out_max, out_min); + printf("CHK after_multilook ml_data=%u max_bits=%u min_bits=%u\n", + buf_checksum_u32(g_ml_data, OUT_W*OUT_H), f_bits(*out_max), f_bits(*out_min)); + } + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); + { uint32_t s=0; for(unsigned i=0;i 20 ns per cycle */ + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("OBPMark #1.2 - Radar SAR (PULP cluster seq, DIAG2 multilook)\n"); + printf("PULP cluster sequential (1 core, RV32) - Astral @ 50 MHz\n"); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Output[0][0] : %u\n", (unsigned)g_output_image[0]); + printf(" Checksum : %u\n", (unsigned)checksum); + + // No fixed expected value here (unlike 1.1's EXPECTED_OUTPUT_0_0): + // this is a new port with no prior cluster run to compare against. + // Cross-check this Output[0][0]/checksum against the host sequential + // port's values (obpmark_1_2_host_seq_full.c) instead - they must + // match exactly, since both compute the identical algorithm on + // identical synthetic input (same LCG seed). + result->errors = 0; +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_v2/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_v2/Makefile new file mode 100644 index 000000000..b6b50cce6 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_v2/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_2_pulpd_seq_v2.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_v2/obpmark_1_2_pulpd_seq_v2.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_v2/obpmark_1_2_pulpd_seq_v2.c new file mode 100644 index 000000000..7863e4062 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_old_versions/obpmark_1_2_seq_v2/obpmark_1_2_pulpd_seq_v2.c @@ -0,0 +1,662 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL port for the Astral PULP cluster (1x RV32 core) - +// FULL pipeline, ported from obpmark_1_2_host_seq_full.c (CVA6/RV64 host). +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository - see the host +// sequential port's header comment for the full derivation and size +// calculations, unchanged here (same RSIZE/ASIZE/APATCH/RVALID/NPATCH, +// same LCG seed, same fixed physical parameters). +// +// ============================================================ +// CLUSTER-SPECIFIC DIFFERENCE FROM THE HOST PORT: floating-point division +// and square root. +// +// Verified on this project's actual hardware configuration (not assumed): +// - hw/carfield.sv sets EnablePrivateFpu: 1 for the PULP cluster cores - +// add/sub/mul in hardware (confirmed by compiling `a+b`/`a*b` at +// -march=rv32imfcxgap9 -mabi=ilp32f: emits fadd.s/fmul.s directly). +// - hw/carfield.sv sets EnablePrivateFpDivSqrt: 0 and NumSharedFpu: 0 - +// the div/sqrt unit is NOT synthesized in any core's FPU +// (pulp_cluster.sv instantiates core_region with +// .FP_DIVSQRT(Cfg.EnablePrivateFpDivSqrt), which is 0). The compiler +// is unaware of this at -march=rv32imfcxgap9 and happily emits +// fdiv.s/fsqrt.s (confirmed: `a/b` compiles straight to fdiv.s) - +// those would trap as illegal instructions on this cluster at runtime. +// +// Consequence: every `/` on floats in the computational code below is +// replaced by a call to m_divf() (software reciprocal-refine division, +// never emits fdiv.s), and sqrt uses the Step-1-validated software m_sqrtf +// (bit-hack + Newton-Raphson) instead of the host port's inline fsqrt.s +// asm, which does not exist on this ISA at all. Plain +, -, * on floats +// are left as native operators: they compile to real hardware +// fadd.s/fsub.s/fmul.s on this core, unlike on the host's rv32imcxgap9 +// cluster tests seen elsewhere in this project (no `f` in march there). +// ============================================================ +// +// PULP-RUNTIME STRUCTURE: verified directly against this project's actual +// obpmark_1_1_pulp_seq.c. Same testcase_t/run_suite framework, same +// rt_cluster_id()/get_core_id()/synch_barrier() bring-up, same cpu_perf_* +// cycle counter (CSR_PCER_CYCLES), same .heapsram placement for all data +// buffers (avoids the large TCDM PT_LOAD write sequence that breaks the +// cluster boot mechanism, per that file's own comment). + +#include "pulp.h" +#include +#include +#include + +// ============================================================ +// BUGFIX (v2, replacing the earlier union-based float<->uint32 bit +// reinterpretation used in m_sqrtf/m_divf/m_log2f): diagnostic tracing +// on the real cluster (obpmark_1_2_pulpd_seq_diag2.c) showed +// m_divf(16.0f, 4.0f) - a division between two exactly representable +// integers with no possible rounding ambiguity - returning +Infinity +// (bit pattern 0x7F800000) inside SAR_multilook, which then propagated +// into isx/isy via an undefined-behavior float-to-uint32_t cast of that +// infinity (observed as isx=isy=0x80000000), degenerating the whole +// multilook output to zero. This is consistent with a strict-aliasing +// miscompilation of the `union { float f; uint32_t u; } v; v.f = x; ...v.u` +// idiom under -O3: the union write and the subsequent read of the other +// member are not guaranteed by the C standard to be ordered relative to +// each other unless accessed through a character type or memcpy, and +// aggressive optimization can reorder or stale-read across that boundary. +// This bug was NOT present in the host-side validation harness (a +// different compiler, x86-64 gcc, happened not to miscompile the same +// pattern), which is why the sequential/parallel PULP results (127/2032) +// looked internally consistent with each other but could not be +// reconciled with the host CVA6 result (89/2540) even after accounting +// for div/sqrt precision alone - the checkpoint trace pinpointed the +// actual divergence to this single call site. +// +// Fix: memcpy-based reinterpretation, the only idiom the C standard +// guarantees is respected by any compiler at any optimization level, +// with no aliasing ambiguity. +// ============================================================ +static inline uint32_t f_to_bits(float x){ + uint32_t u; + memcpy(&u, &x, sizeof(u)); + return u; +} +static inline float bits_to_f(uint32_t u){ + float x; + memcpy(&x, &u, sizeof(x)); + return x; +} + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c), adapted for the +// cluster: sqrtf is the SOFTWARE bit-hack+Newton version (no fsqrt.s on +// this core), and a new m_divf replaces the `/` operator wherever the +// computational code needs floating-point division (no fdiv.s either). +// sinf/cosf/log2f/atan2f/floorf/ceilf/roundf are unchanged from the host +// port: none of them use hardware division or sqrt internally. +// ============================================================ +#define PI_F 3.14159265358979323846f + +// Software sqrt (Step 1: bit-hack initial guess + 2x Newton-Raphson, +// ~4.6e-6 relative error measured against real libm). No fsqrt.s: this +// core's FPU has no synthesized div/sqrt unit. +static float m_sqrtf(float x){ + if (x <= 0.0f) return 0.0f; + uint32_t u = f_to_bits(x); + u = 0x5f3759df - (u >> 1); + float y = bits_to_f(u); + y = y * (1.5f - 0.5f*x*y*y); + y = y * (1.5f - 0.5f*x*y*y); + return x * y; +} + +// Software division: Newton-Raphson refinement of a bit-hack reciprocal +// estimate, then one final multiply. Never emits fdiv.s. Accuracy is +// exercised implicitly through the whole SAR pipeline; if higher precision +// is ever needed, add one more Newton iteration below (cheap: one more +// fmul.s/fsub.s pair, both hardware on this core). +static float m_divf(float a, float b){ + if (b == 0.0f) return 0.0f; + uint32_t u = f_to_bits(b); + u = 0x7EF127EA - u; // initial reciprocal estimate + float r = bits_to_f(u); + r = r * (2.0f - b * r); // Newton-Raphson iteration 1 + r = r * (2.0f - b * r); // Newton-Raphson iteration 2 + return a * r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + uint32_t bits = f_to_bits(x); + int e = (int)((bits >> 23) & 0xFF) - 127; + bits = (bits & 0x007FFFFF) | 0x3F800000; + float m = bits_to_f(bits); + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = m_divf(m - 1.0f, m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? m_divf(ay, ax) : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? m_divf(ax, ay) : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -m_divf(2.0f * PI_F, (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] = m_divf(data[2*i], (float)nn); data[2*i+1] = m_divf(data[2*i+1], (float)nn); } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap). Placed in .heapsram (TCDM), same +// pattern as obpmark_1_1_pulp_seq.c: this keeps them out of the ELF +// PT_LOAD payload, avoiding the large TCDM write sequence in +// load_binary() that breaks the cluster boot mechanism. +// ============================================================ +#define HEAPSRAM __attribute__((section(".heapsram"))) + +static HEAPSRAM float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static HEAPSRAM float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static HEAPSRAM float g_rrf[RRF_LEN]; +static HEAPSRAM float g_arf[ARF_LEN]; +static HEAPSRAM float g_aux[AUX_LEN]; +static HEAPSRAM uint32_t g_offsets[OFFS_LEN]; +static HEAPSRAM float g_ml_data[OUT_W * OUT_H]; +static HEAPSRAM uint8_t g_output_image[OUT_W * OUT_H]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * m_divf((float)bm_rand(), 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * m_divf(1.0f, fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = m_divf(mean, (float)width); + return m_divf(mean * params->PRF, 2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * m_divf(params->PRF, (float)params->avalid) + fDc; + float ratio = m_divf(params->lambda * delta, 2.0f*params->vr); + float offset = (m_divf(1.0f, m_sqrtf(1.0f - ratio*ratio)) - 1.0f) + * (params->ro + (float)i * m_divf(c_light, 2.0f*params->fs)); + offset = m_roundf(m_divf(offset, m_divf(c_light, 2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*m_divf(c_light, 2.0f*params->fs); + float ratio = m_divf(params->lambda*fDc, 2.0f*params->vr); + float rdc = m_divf(rnge, m_sqrtf(1.0f - ratio*ratio)); + float tauz = m_divf(rdc*m_divf(params->lambda, 10.0f) * 0.8f, params->vr); + float chirp = m_divf(m_divf(-(2.0f*params->vr*params->vr), params->lambda), rdc); + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = m_divf((float)params->asize, (float)image_w); + float sy = m_divf((float)params->rvalid, (float)image_w); + uint32_t nfx = (uint32_t)m_floorf(m_divf((float)params->avalid, sx)); + uint32_t nfy = (uint32_t)m_floorf(m_divf((float)params->rvalid, sy)); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = m_divf(fimg, (float)(isx*isy)); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = m_divf(256.0f, (vmax - vmin + 1e-30f)); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +static void SAR_focus(radar_params_t *params, float *out_max, float *out_min){ + *out_max = -1e30f; + *out_min = 1e30f; + + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + + for(uint32_t patch = 0; patch < params->npatch; patch++){ + SAR_range_compression(g_range_data, RANGE_H, RANGE_W, g_rrf); + complex_transpose(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->apatch, params->rvalid); + SAR_rcmc(g_azimuth_data, AZIM_H, AZIM_W, g_offsets); + SAR_azimuth_compression(g_azimuth_data, AZIM_H, AZIM_W, g_arf); + complex_transpose(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->rvalid, params->apatch); + SAR_multilook(g_range_data, g_ml_data, OUT_W, params, patch, out_max, out_min); + } + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Test framework glue, main(), and check_obpmark(): structured exactly +// like obpmark_1_1_pulp_seq.c (verified against that file directly) - +// same testcase_t/run_suite pattern, same rt_cluster_id()/get_core_id()/ +// synch_barrier() bring-up, same cpu_perf_* cycle counter API and event +// (CSR_PCER_CYCLES), same 50 MHz / 20 ns clock assumption. Only the +// benchmark body (radar_params_t setup, gen_rand_radar_data, SAR_focus, +// checksum) is specific to 1.2. +// ============================================================ +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_1_2_seq", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + if (get_core_id() == 0) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + if (get_core_id() != 0) return; + + radar_params_t params; + params.lambda = 0.03f; + params.PRF = 1000.0f; + params.tau = 1e-5f; + params.fs = 5e7f; + params.vr = 7000.0f; + params.ro = 8e5f; + params.slope = 5e11f; + params.rsize = RSIZE; + params.asize = ASIZE; + + // rvalid = rsize - round(tau*fs), clamped to rsize if too small + float rvalid_f = (float)params.rsize - m_roundf(params.tau * params.fs); + uint32_t rvalid = (rvalid_f < (float)(params.rsize/2)) ? params.rsize : (uint32_t)rvalid_f; + params.rvalid = rvalid; + + params.avalid = (params.asize < 1024u) ? params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); // asize<1024 branch used here + // apatch = 2^ceil(log2(avalid+1)) + float lg = m_log2f((float)(params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + params.apatch = apatch; + params.npatch = params.asize / params.avalid; // integer division, hardware (m ext.) + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u\n", + (unsigned)params.rvalid, (unsigned)params.avalid, + (unsigned)params.apatch, (unsigned)params.npatch); + + gen_rand_radar_data(); + + /* Configure and start the cycle performance counter explicitly + * (bench_timer_start only calls perf_start() when PROFILE is defined). + * riscv_v5 API: conf_events selects which events, setall zeroes the + * counters, start(mask) activates counting. */ + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + + start(); + + float vmax, vmin; + SAR_focus(¶ms, &vmax, &vmin); + + stop(); + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + /* Cluster runs at 50 MHz -> 20 ns per cycle */ + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("OBPMark #1.2 - Radar SAR (PULP cluster seq, v2 memcpy fix)\n"); + printf("PULP cluster sequential (1 core, RV32) - Astral @ 50 MHz\n"); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Output[0][0] : %u\n", (unsigned)g_output_image[0]); + printf(" Checksum : %u\n", (unsigned)checksum); + + // No fixed expected value here (unlike 1.1's EXPECTED_OUTPUT_0_0): + // this is a new port with no prior cluster run to compare against. + // Cross-check this Output[0][0]/checksum against the host sequential + // port's values (obpmark_1_2_host_seq_full.c) instead - they must + // match exactly, since both compute the identical algorithm on + // identical synthetic input (same LCG seed). + result->errors = 0; +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_par/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_par/Makefile new file mode 100644 index 000000000..f035332cf --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_par/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_2_pulpd_par.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_par/obpmark_1_2_pulpd_par.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_par/obpmark_1_2_pulpd_par.c new file mode 100644 index 000000000..a6ddd85c2 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_par/obpmark_1_2_pulpd_par.c @@ -0,0 +1,836 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal PARALLEL (SPMD) port for the Astral PULP cluster (up to 8x +// RV32 cores) - FULL pipeline, built on obpmark_1_2_pulpd_seq.c (1-core +// port) and structured exactly like obpmark_1_1_pulp_par.c (verified +// against that file): synch_barrier() hardware barrier, get_core_id() +// re-read fresh inside every pipeline function (same defensive pattern as +// the CVA6 SMP port, where a cached hart-identity value survived across a +// call/barrier boundary under LTO and silently misidentified the calling +// core), num_cores determined at runtime via get_core_num() rather than a +// compile-time constant. +// +// LOAD IMBALANCE NOTE: split_range(total, id, n, ...) degenerates when +// n (num_cores, up to 8) exceeds total (the row/block count of a given +// stage). With the reduced dimensions used here (RVALID=8, nfx=2), stages +// with fewer units of work than active cores give every core except the +// last an EMPTY range (chunk = total/n truncates to 0), and the last core +// alone does that stage's entire workload. This is still correct +// (deterministic, no race - see the SAR_rcmc_shift note below for the one +// stage that is exclusive to core 0 for a different reason), but it means +// parallel speedup on the smallest stages is limited by core count vs. +// problem size, not by the parallelization logic itself - worth reporting +// as measured, not working around silently. +// FULL pipeline, ported from obpmark_1_2_host_seq_full.c (CVA6/RV64 host). +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository - see the host +// sequential port's header comment for the full derivation and size +// calculations, unchanged here (same RSIZE/ASIZE/APATCH/RVALID/NPATCH, +// same LCG seed, same fixed physical parameters). +// +// ============================================================ +// CLUSTER-SPECIFIC DIFFERENCE FROM THE HOST PORT: floating-point division +// and square root. +// +// Verified on this project's actual hardware configuration (not assumed): +// - hw/carfield.sv sets EnablePrivateFpu: 1 for the PULP cluster cores - +// add/sub/mul in hardware (confirmed by compiling `a+b`/`a*b` at +// -march=rv32imfcxgap9 -mabi=ilp32f: emits fadd.s/fmul.s directly). +// - hw/carfield.sv sets EnablePrivateFpDivSqrt: 0 and NumSharedFpu: 0 - +// the div/sqrt unit is NOT synthesized in any core's FPU +// (pulp_cluster.sv instantiates core_region with +// .FP_DIVSQRT(Cfg.EnablePrivateFpDivSqrt), which is 0). The compiler +// is unaware of this at -march=rv32imfcxgap9 and happily emits +// fdiv.s/fsqrt.s (confirmed: `a/b` compiles straight to fdiv.s) - +// those would trap as illegal instructions on this cluster at runtime. +// +// Consequence: every `/` on floats in the computational code below is +// replaced by a call to m_divf() (software reciprocal-refine division, +// never emits fdiv.s), and sqrt uses the Step-1-validated software m_sqrtf +// (bit-hack + Newton-Raphson) instead of the host port's inline fsqrt.s +// asm, which does not exist on this ISA at all. Plain +, -, * on floats +// are left as native operators: they compile to real hardware +// fadd.s/fsub.s/fmul.s on this core, unlike on the host's rv32imcxgap9 +// cluster tests seen elsewhere in this project (no `f` in march there). +// ============================================================ +// +// PULP-RUNTIME STRUCTURE: verified directly against this project's actual +// obpmark_1_1_pulp_seq.c. Same testcase_t/run_suite framework, same +// rt_cluster_id()/get_core_id()/synch_barrier() bring-up, same cpu_perf_* +// cycle counter (CSR_PCER_CYCLES), same .heapsram placement for all data +// buffers (avoids the large TCDM PT_LOAD write sequence that breaks the +// cluster boot mechanism, per that file's own comment). + +#include "pulp.h" +#include +#include + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c), adapted for the +// cluster: sqrtf is the SOFTWARE bit-hack+Newton version (no fsqrt.s on +// this core), and a new m_divf replaces the `/` operator wherever the +// computational code needs floating-point division (no fdiv.s either). +// sinf/cosf/log2f/atan2f/floorf/ceilf/roundf are unchanged from the host +// port: none of them use hardware division or sqrt internally. +// ============================================================ +#define PI_F 3.14159265358979323846f + +// Software sqrt (Step 1: bit-hack initial guess + 2x Newton-Raphson, +// ~4.6e-6 relative error measured against real libm). No fsqrt.s: this +// core's FPU has no synthesized div/sqrt unit. +static float m_sqrtf(float x){ + if (x <= 0.0f) return 0.0f; + union { float f; uint32_t u; } v; v.f = x; + v.u = 0x5f3759df - (v.u >> 1); + float y = v.f; + y = y * (1.5f - 0.5f*x*y*y); + y = y * (1.5f - 0.5f*x*y*y); + return x * y; +} + +// Software division: Newton-Raphson refinement of a bit-hack reciprocal +// estimate, then one final multiply. Never emits fdiv.s. Accuracy is +// exercised implicitly through the whole SAR pipeline; if higher precision +// is ever needed, add one more Newton iteration below (cheap: one more +// fmul.s/fsub.s pair, both hardware on this core). +static float m_divf(float a, float b){ + if (b == 0.0f) return 0.0f; + union { float f; uint32_t u; } v; v.f = b; + v.u = 0x7EF127EA - v.u; // initial reciprocal estimate + float r = v.f; + r = r * (2.0f - b * r); // Newton-Raphson iteration 1 + r = r * (2.0f - b * r); // Newton-Raphson iteration 2 + return a * r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = m_divf(m - 1.0f, m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? m_divf(ay, ax) : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? m_divf(ax, ay) : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -m_divf(2.0f * PI_F, (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] = m_divf(data[2*i], (float)nn); data[2*i+1] = m_divf(data[2*i+1], (float)nn); } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap). Placed in .heapsram (TCDM), same +// pattern as obpmark_1_1_pulp_seq.c: this keeps them out of the ELF +// PT_LOAD payload, avoiding the large TCDM write sequence in +// load_binary() that breaks the cluster boot mechanism. +// ============================================================ +#define HEAPSRAM __attribute__((section(".heapsram"))) + +static HEAPSRAM float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static HEAPSRAM float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static HEAPSRAM float g_rrf[RRF_LEN]; +static HEAPSRAM float g_arf[ARF_LEN]; +static HEAPSRAM float g_aux[AUX_LEN]; +static HEAPSRAM uint32_t g_offsets[OFFS_LEN]; +static HEAPSRAM float g_ml_data[OUT_W * OUT_H]; +static HEAPSRAM uint8_t g_output_image[OUT_W * OUT_H]; + +static HEAPSRAM unsigned int num_cores; + +#define MAX_CORES 8 // this cluster's core count (8x RV32, per project spec) +static HEAPSRAM float g_partial_max[MAX_CORES]; +static HEAPSRAM float g_partial_min[MAX_CORES]; + +/* Static range partitioning, same policy as OpenMP schedule(static) and + * as obpmark_1_1_pulp_par.c's split_range(). See the load-imbalance note + * at the top of this file for what happens when n > total. */ +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * m_divf((float)bm_rand(), 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * m_divf(1.0f, fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = m_divf(mean, (float)width); + return m_divf(mean * params->PRF, 2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * m_divf(params->PRF, (float)params->avalid) + fDc; + float ratio = m_divf(params->lambda * delta, 2.0f*params->vr); + float offset = (m_divf(1.0f, m_sqrtf(1.0f - ratio*ratio)) - 1.0f) + * (params->ro + (float)i * m_divf(c_light, 2.0f*params->fs)); + offset = m_roundf(m_divf(offset, m_divf(c_light, 2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*m_divf(c_light, 2.0f*params->fs); + float ratio = m_divf(params->lambda*fDc, 2.0f*params->vr); + float rdc = m_divf(rnge, m_sqrtf(1.0f - ratio*ratio)); + float tauz = m_divf(rdc*m_divf(params->lambda, 10.0f) * 0.8f, params->vr); + float chirp = m_divf(m_divf(-(2.0f*params->vr*params->vr), params->lambda), rdc); + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SPMD variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across cores gives a bit-identical +// result to the sequential version regardless of core count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SPMD variant: split the outer (nrows) loop across cores. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different cores get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SPMD: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SPMD: shift/remap part. NOT parallelized on purpose - see the top-of-file +// note and the host SMP port's identical reasoning: this loop reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i, +// so it is not idempotent and must run exactly once, on core 0 alone, with +// barriers on both sides to keep every core in lockstep. Cheap regardless +// (rvalid*apatch iterations). +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SPMD variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = m_divf((float)params->asize, (float)image_w); + float sy = m_divf((float)params->rvalid, (float)image_w); + uint32_t nfx = (uint32_t)m_floorf(m_divf((float)params->avalid, sx)); + uint32_t nfy = (uint32_t)m_floorf(m_divf((float)params->rvalid, sy)); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = m_divf(fimg, (float)(isx*isy)); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SPMD variant: split the outer (nfx) loop across cores. Each core writes a +// disjoint set of output blocks and accumulates its own local max/min; +// core 0 combines all partials after a barrier. See the load-imbalance +// note at the top of this file for what happens when num_cores > nfx. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = m_divf((float)params->asize, (float)image_w); + float sy = m_divf((float)params->rvalid, (float)image_w); + uint32_t nfx_total = (uint32_t)m_floorf(m_divf((float)params->avalid, sx)); + uint32_t nfy = (uint32_t)m_floorf(m_divf((float)params->rvalid, sy)); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = m_divf(fimg, (float)(isx*isy)); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = m_divf(256.0f, (vmax - vmin + 1e-30f)); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus_par: SPMD orchestration of the full pipeline. Every core runs +// this same function; get_core_id() is re-read fresh at every decision +// point rather than cached in a parameter (see the top-of-file note on +// why - lesson learned from the CVA6 SMP port). Setup (range_ref, DCE, +// rcmc_table, azimuth_ref) and the RCMC shift/remap run on core 0 only; +// synch_barrier() (hardware) replaces the CVA6 port's software +// sense-reversing barrier entirely. +// ============================================================ +static void SAR_focus_par(radar_params_t *params, float *out_max, float *out_min){ + if ((unsigned int)get_core_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + synch_barrier(); + + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)get_core_id(), num_cores, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + synch_barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)get_core_id(), num_cores, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + synch_barrier(); + + // --- RCMC: FFT part split by AZIM_H rows --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)get_core_id(), num_cores, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + synch_barrier(); + + // RCMC shift/remap: core 0 only, not idempotent (see comment on + // SAR_rcmc_shift above). + if ((unsigned int)get_core_id() == 0) + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + synch_barrier(); + + // --- azimuth compression: split AZIM_H rows --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)get_core_id(), num_cores, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + synch_barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)get_core_id(), num_cores, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + synch_barrier(); + + // --- multilook: split nfx blocks, combine local max/min --- + float sx = m_divf((float)params->asize, (float)OUT_W); + uint32_t nfx = (uint32_t)m_floorf(m_divf((float)params->avalid, sx)); + unsigned int ms, me; + unsigned int my_id = (unsigned int)get_core_id(); + split_range(nfx, my_id, num_cores, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + if (my_id < MAX_CORES){ + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + } + synch_barrier(); + + if ((unsigned int)get_core_id() == 0){ + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int c = 1; c < num_cores && c < MAX_CORES; c++){ + if (g_partial_max[c] > vmax) vmax = g_partial_max[c]; + if (g_partial_min[c] < vmin) vmin = g_partial_min[c]; + } + *out_max = vmax; *out_min = vmin; + } + synch_barrier(); + } + // quantize is core 0 only, outside the tight barrier loop. + if ((unsigned int)get_core_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Test framework glue, main(), and check_obpmark(): structured exactly +// like obpmark_1_1_pulp_par.c (verified against that file directly) - +// same testcase_t/run_suite pattern, same rt_cluster_id()/get_core_num()/ +// synch_barrier() bring-up (num_cores determined at runtime, not a +// compile-time constant), same core-0-only init/perf-counter guard +// before the parallel region, same cpu_perf_* cycle counter API +// (CSR_PCER_CYCLES). Only the benchmark body (radar_params_t setup, +// gen_rand_radar_data, SAR_focus_par, checksum) is specific to 1.2. +// ============================================================ +static HEAPSRAM radar_params_t g_params; + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_1_2_par", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + num_cores = get_core_num(); + + if (rt_core_id() < num_cores) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int core_id = (unsigned int)get_core_id(); + + if (core_id == 0){ + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + // rvalid = rsize - round(tau*fs), clamped to rsize if too small + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + // apatch = 2^ceil(log2(avalid+1)) + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; // integer division, hardware (m ext.) + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | cores=%u\n", + (unsigned)g_params.rvalid, (unsigned)g_params.avalid, + (unsigned)g_params.apatch, (unsigned)g_params.npatch, num_cores); + + gen_rand_radar_data(); + + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + } + synch_barrier(); // params, input data, and perf counters are ready + + start(); + + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_par(&g_params, &vmax, &vmin); + + synch_barrier(); + stop(); + + if (core_id != 0) return; + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + /* Cluster runs at 50 MHz -> 20 ns per cycle */ + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("OBPMark #1.2 - Radar SAR (FULL pipeline, PULP cluster par)\n"); + printf("PULP cluster parallel (%u cores, RV32) - Astral @ 50 MHz\n", num_cores); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Output[0][0] : %u\n", (unsigned)g_output_image[0]); + printf(" Checksum : %u\n", (unsigned)checksum); + + // No fixed expected value here (unlike 1.1's EXPECTED_OUTPUT_0_0): this + // is a new port with no prior cluster run to compare against. Cross- + // check this Output[0][0]/checksum against BOTH the host sequential + // port (obpmark_1_2_host_seq_full.c) AND the sequential PULP port + // (obpmark_1_2_pulpd_seq.c) - all three must match exactly, since all + // three compute the identical algorithm on identical synthetic input + // (same LCG seed). A mismatch against the sequential PULP port + // specifically (rather than the host one) would point at the + // parallelization logic itself, not at the software div/sqrt vs. + // hardware fdiv.s/fsqrt.s precision difference already expected + // between cluster and host. + result->errors = 0; +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_par/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_par/Makefile new file mode 100644 index 000000000..f035332cf --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_par/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_2_pulpd_par.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_par/obpmark_1_2_pulpd_par.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_par/obpmark_1_2_pulpd_par.c new file mode 100644 index 000000000..259ce549c --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_par/obpmark_1_2_pulpd_par.c @@ -0,0 +1,873 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal PARALLEL (SPMD) port for the Astral PULP cluster (up to 8x +// RV32 cores) - FULL pipeline, built on obpmark_1_2_pulpd_seq.c (1-core +// port) and structured exactly like obpmark_1_1_pulp_par.c (verified +// against that file): synch_barrier() hardware barrier, get_core_id() +// re-read fresh inside every pipeline function (same defensive pattern as +// the CVA6 SMP port, where a cached hart-identity value survived across a +// call/barrier boundary under LTO and silently misidentified the calling +// core), num_cores determined at runtime via get_core_num() rather than a +// compile-time constant. +// +// LOAD IMBALANCE NOTE: split_range(total, id, n, ...) degenerates when +// n (num_cores, up to 8) exceeds total (the row/block count of a given +// stage). With the reduced dimensions used here (RVALID=8, nfx=2), stages +// with fewer units of work than active cores give every core except the +// last an EMPTY range (chunk = total/n truncates to 0), and the last core +// alone does that stage's entire workload. This is still correct +// (deterministic, no race - see the SAR_rcmc_shift note below for the one +// stage that is exclusive to core 0 for a different reason), but it means +// parallel speedup on the smallest stages is limited by core count vs. +// problem size, not by the parallelization logic itself - worth reporting +// as measured, not working around silently. +// FULL pipeline, ported from obpmark_1_2_host_seq_full.c (CVA6/RV64 host). +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository - see the host +// sequential port's header comment for the full derivation and size +// calculations, unchanged here (same RSIZE/ASIZE/APATCH/RVALID/NPATCH, +// same LCG seed, same fixed physical parameters). +// +// ============================================================ +// CLUSTER-SPECIFIC DIFFERENCE FROM THE HOST PORT: floating-point division +// and square root. +// +// Verified on this project's actual hardware configuration (not assumed): +// - hw/carfield.sv sets EnablePrivateFpu: 1 for the PULP cluster cores - +// add/sub/mul in hardware (confirmed by compiling `a+b`/`a*b` at +// -march=rv32imfcxgap9 -mabi=ilp32f: emits fadd.s/fmul.s directly). +// - hw/carfield.sv sets EnablePrivateFpDivSqrt: 0 and NumSharedFpu: 0 - +// the div/sqrt unit is NOT synthesized in any core's FPU +// (pulp_cluster.sv instantiates core_region with +// .FP_DIVSQRT(Cfg.EnablePrivateFpDivSqrt), which is 0). The compiler +// is unaware of this at -march=rv32imfcxgap9 and happily emits +// fdiv.s/fsqrt.s (confirmed: `a/b` compiles straight to fdiv.s) - +// those would trap as illegal instructions on this cluster at runtime. +// +// Consequence: every `/` on floats in the computational code below is +// replaced by a call to m_divf() (software reciprocal-refine division, +// never emits fdiv.s), and sqrt uses the Step-1-validated software m_sqrtf +// (bit-hack + Newton-Raphson) instead of the host port's inline fsqrt.s +// asm, which does not exist on this ISA at all. Plain +, -, * on floats +// are left as native operators: they compile to real hardware +// fadd.s/fsub.s/fmul.s on this core, unlike on the host's rv32imcxgap9 +// cluster tests seen elsewhere in this project (no `f` in march there). +// ============================================================ +// +// PULP-RUNTIME STRUCTURE: verified directly against this project's actual +// obpmark_1_1_pulp_seq.c. Same testcase_t/run_suite framework, same +// rt_cluster_id()/get_core_id()/synch_barrier() bring-up, same cpu_perf_* +// cycle counter (CSR_PCER_CYCLES), same .heapsram placement for all data +// buffers (avoids the large TCDM PT_LOAD write sequence that breaks the +// cluster boot mechanism, per that file's own comment). + +#include "pulp.h" +#include +#include +#include + +// ============================================================ +// Float <-> uint32_t bit reinterpretation uses memcpy, the idiom the C +// standard guarantees at any optimization level (no aliasing ambiguity), +// instead of a union. Tested on the cluster against the earlier union-based +// version: identical cycle count (821038) and identical result, i.e. a +// portability choice with no effect on the behavior described below. +// +// KNOWN OPEN ISSUE (cluster only): checkpoint tracing on the real cluster +// showed m_divf(16.0f, 4.0f) returning +Inf (0x7F800000) inside +// SAR_multilook (sx_bits=2139095040), which makes nfx=nfy=0 and degenerates +// the multilook output. The reported Output[0][0]=127 / Checksum=2032 and +// the cycle/throughput figures are therefore NOT valid until this is +// resolved; the host ports (89/2540) are unaffected. Next step: an isolated +// m_divf(16.0f, 4.0f) test with hard-coded literals, to tell whether the +// inputs or m_divf itself are at fault. +// ============================================================ +static inline uint32_t f_to_bits(float x){ + uint32_t u; + memcpy(&u, &x, sizeof(u)); + return u; +} +static inline float bits_to_f(uint32_t u){ + float x; + memcpy(&x, &u, sizeof(x)); + return x; +} + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c), adapted for the +// cluster: sqrtf is the SOFTWARE bit-hack+Newton version (no fsqrt.s on +// this core), and a new m_divf replaces the `/` operator wherever the +// computational code needs floating-point division (no fdiv.s either). +// sinf/cosf/log2f/atan2f/floorf/ceilf/roundf are unchanged from the host +// port: none of them use hardware division or sqrt internally. +// ============================================================ +#define PI_F 3.14159265358979323846f + +// Software sqrt (Step 1: bit-hack initial guess + 2x Newton-Raphson, +// ~4.6e-6 relative error measured against real libm). No fsqrt.s: this +// core's FPU has no synthesized div/sqrt unit. +static float m_sqrtf(float x){ + if (x <= 0.0f) return 0.0f; + uint32_t u = f_to_bits(x); + u = 0x5f3759df - (u >> 1); + float y = bits_to_f(u); + y = y * (1.5f - 0.5f*x*y*y); + y = y * (1.5f - 0.5f*x*y*y); + return x * y; +} + +// Software division: Newton-Raphson refinement of a bit-hack reciprocal +// estimate, then one final multiply. Never emits fdiv.s. Accuracy is +// exercised implicitly through the whole SAR pipeline; if higher precision +// is ever needed, add one more Newton iteration below (cheap: one more +// fmul.s/fsub.s pair, both hardware on this core). +static float m_divf(float a, float b){ + if (b == 0.0f) return 0.0f; + uint32_t u = f_to_bits(b); + u = 0x7EF127EA - u; // initial reciprocal estimate + float r = bits_to_f(u); + r = r * (2.0f - b * r); // Newton-Raphson iteration 1 + r = r * (2.0f - b * r); // Newton-Raphson iteration 2 + return a * r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + uint32_t bits = f_to_bits(x); + int e = (int)((bits >> 23) & 0xFF) - 127; + bits = (bits & 0x007FFFFF) | 0x3F800000; + float m = bits_to_f(bits); + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = m_divf(m - 1.0f, m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? m_divf(ay, ax) : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? m_divf(ax, ay) : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -m_divf(2.0f * PI_F, (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] = m_divf(data[2*i], (float)nn); data[2*i+1] = m_divf(data[2*i+1], (float)nn); } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap). Placed in .heapsram (TCDM), same +// pattern as obpmark_1_1_pulp_seq.c: this keeps them out of the ELF +// PT_LOAD payload, avoiding the large TCDM write sequence in +// load_binary() that breaks the cluster boot mechanism. +// ============================================================ +#define HEAPSRAM __attribute__((section(".heapsram"))) + +static HEAPSRAM float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static HEAPSRAM float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static HEAPSRAM float g_rrf[RRF_LEN]; +static HEAPSRAM float g_arf[ARF_LEN]; +static HEAPSRAM float g_aux[AUX_LEN]; +static HEAPSRAM uint32_t g_offsets[OFFS_LEN]; +static HEAPSRAM float g_ml_data[OUT_W * OUT_H]; +static HEAPSRAM uint8_t g_output_image[OUT_W * OUT_H]; + +static HEAPSRAM unsigned int num_cores; + +#define MAX_CORES 8 // this cluster's core count (8x RV32, per project spec) +static HEAPSRAM float g_partial_max[MAX_CORES]; +static HEAPSRAM float g_partial_min[MAX_CORES]; + +/* Static range partitioning, same policy as OpenMP schedule(static) and + * as obpmark_1_1_pulp_par.c's split_range(). See the load-imbalance note + * at the top of this file for what happens when n > total. */ +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * m_divf((float)bm_rand(), 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * m_divf(1.0f, fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = m_divf(mean, (float)width); + return m_divf(mean * params->PRF, 2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * m_divf(params->PRF, (float)params->avalid) + fDc; + float ratio = m_divf(params->lambda * delta, 2.0f*params->vr); + float offset = (m_divf(1.0f, m_sqrtf(1.0f - ratio*ratio)) - 1.0f) + * (params->ro + (float)i * m_divf(c_light, 2.0f*params->fs)); + offset = m_roundf(m_divf(offset, m_divf(c_light, 2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*m_divf(c_light, 2.0f*params->fs); + float ratio = m_divf(params->lambda*fDc, 2.0f*params->vr); + float rdc = m_divf(rnge, m_sqrtf(1.0f - ratio*ratio)); + float tauz = m_divf(rdc*m_divf(params->lambda, 10.0f) * 0.8f, params->vr); + float chirp = m_divf(m_divf(-(2.0f*params->vr*params->vr), params->lambda), rdc); + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SPMD variant: identical math, restricted to row range [rs,re). Each row's +// fft->refmul->ifft is fully independent of every other row, so splitting +// [0,dh) into disjoint contiguous ranges across cores gives a bit-identical +// result to the sequential version regardless of core count. +static void SAR_range_compression_range(float *data, uint32_t dw_floats, + const float *rrf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(rrf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +// SPMD variant: split the outer (nrows) loop across cores. For a fixed i, +// every write out[j*b_rw+i] only depends on i - different cores get disjoint +// i-ranges, hence disjoint output columns: no data race, bit-identical result. +static void complex_transpose_range(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t ncols, uint32_t rs, uint32_t re){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = rs; i < re; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +// SPMD: FFT part only, restricted to a row range - safe (each row independent). +static void SAR_rcmc_fft_range(float *data, uint32_t dw_floats, + uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) fft(&data[k*dw_floats], (int)width); +} + +// SPMD: shift/remap part. NOT parallelized on purpose - see the top-of-file +// note and the host SMP port's identical reasoning: this loop reads +// data[j + offsets[ind]*width] from an ARBITRARY row while writing row i, +// so it is not idempotent and must run exactly once, on core 0 alone, with +// barriers on both sides to keep every core in lockstep. Cheap regardless +// (rvalid*apatch iterations). +static void SAR_rcmc_shift(float *data, uint32_t dh, uint32_t width, + const uint32_t *offsets){ + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// SPMD variant: same reasoning as range_compression - each row independent. +static void SAR_azimuth_compression_range(float *data, uint32_t dw_floats, + const float *arf, uint32_t rs, uint32_t re){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = rs; k < re; k++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = k*width + j; + cst(data, idx, cmul(cld(data, idx), cconj(cld(arf, j)))); + } + for(uint32_t k = rs; k < re; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = m_divf((float)params->asize, (float)image_w); + float sy = m_divf((float)params->rvalid, (float)image_w); + uint32_t nfx = (uint32_t)m_floorf(m_divf((float)params->avalid, sx)); + uint32_t nfy = (uint32_t)m_floorf(m_divf((float)params->rvalid, sy)); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = m_divf(fimg, (float)(isx*isy)); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +// SPMD variant: split the outer (nfx) loop across cores. Each core writes a +// disjoint set of output blocks and accumulates its own local max/min; +// core 0 combines all partials after a barrier. See the load-imbalance +// note at the top of this file for what happens when num_cores > nfx. +static void SAR_multilook_range(const float *radar_data, float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + uint32_t is, uint32_t ie, + float *out_max, float *out_min){ + float sx = m_divf((float)params->asize, (float)image_w); + float sy = m_divf((float)params->rvalid, (float)image_w); + uint32_t nfx_total = (uint32_t)m_floorf(m_divf((float)params->avalid, sx)); + uint32_t nfy = (uint32_t)m_floorf(m_divf((float)params->rvalid, sy)); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); + uint32_t offset = patch * nfx_total * nfy; + float vmax = -1e30f, vmin = 1e30f; + + for(uint32_t i = is; i < ie; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = m_divf(fimg, (float)(isx*isy)); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = m_divf(256.0f, (vmax - vmin + 1e-30f)); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus_par: SPMD orchestration of the full pipeline. Every core runs +// this same function; get_core_id() is re-read fresh at every decision +// point rather than cached in a parameter (see the top-of-file note on +// why - lesson learned from the CVA6 SMP port). Setup (range_ref, DCE, +// rcmc_table, azimuth_ref) and the RCMC shift/remap run on core 0 only; +// synch_barrier() (hardware) replaces the CVA6 port's software +// sense-reversing barrier entirely. +// ============================================================ +static void SAR_focus_par(radar_params_t *params, float *out_max, float *out_min){ + if ((unsigned int)get_core_id() == 0){ + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + } + synch_barrier(); + + for(uint32_t patch = 0; patch < params->npatch; patch++){ + // --- range compression: split RANGE_H rows --- + unsigned int rs, re; + split_range(RANGE_H, (unsigned int)get_core_id(), num_cores, &rs, &re); + SAR_range_compression_range(g_range_data, RANGE_W, g_rrf, rs, re); + synch_barrier(); + + // --- transpose range -> azimuth: split by APATCH (nrows) --- + unsigned int ts, te; + split_range(params->apatch, (unsigned int)get_core_id(), num_cores, &ts, &te); + complex_transpose_range(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->rvalid, ts, te); + synch_barrier(); + + // --- RCMC: FFT part split by AZIM_H rows --- + unsigned int fs, fe; + split_range(AZIM_H, (unsigned int)get_core_id(), num_cores, &fs, &fe); + SAR_rcmc_fft_range(g_azimuth_data, AZIM_W, fs, fe); + synch_barrier(); + + // RCMC shift/remap: core 0 only, not idempotent (see comment on + // SAR_rcmc_shift above). + if ((unsigned int)get_core_id() == 0) + SAR_rcmc_shift(g_azimuth_data, AZIM_H, AZIM_W>>1, g_offsets); + synch_barrier(); + + // --- azimuth compression: split AZIM_H rows --- + unsigned int fs2, fe2; + split_range(AZIM_H, (unsigned int)get_core_id(), num_cores, &fs2, &fe2); + SAR_azimuth_compression_range(g_azimuth_data, AZIM_W, g_arf, fs2, fe2); + synch_barrier(); + + // --- transpose azimuth -> range: split by RVALID (nrows) --- + unsigned int t2s, t2e; + split_range(params->rvalid, (unsigned int)get_core_id(), num_cores, &t2s, &t2e); + complex_transpose_range(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->apatch, t2s, t2e); + synch_barrier(); + + // --- multilook: split nfx blocks, combine local max/min --- + float sx = m_divf((float)params->asize, (float)OUT_W); + uint32_t nfx = (uint32_t)m_floorf(m_divf((float)params->avalid, sx)); + unsigned int ms, me; + unsigned int my_id = (unsigned int)get_core_id(); + split_range(nfx, my_id, num_cores, &ms, &me); + float lmax, lmin; + SAR_multilook_range(g_range_data, g_ml_data, OUT_W, params, patch, + ms, me, &lmax, &lmin); + if (my_id < MAX_CORES){ + g_partial_max[my_id] = lmax; + g_partial_min[my_id] = lmin; + } + synch_barrier(); + + if ((unsigned int)get_core_id() == 0){ + float vmax = g_partial_max[0], vmin = g_partial_min[0]; + for (unsigned int c = 1; c < num_cores && c < MAX_CORES; c++){ + if (g_partial_max[c] > vmax) vmax = g_partial_max[c]; + if (g_partial_min[c] < vmin) vmin = g_partial_min[c]; + } + *out_max = vmax; *out_min = vmin; + } + synch_barrier(); + } + // quantize is core 0 only, outside the tight barrier loop. + if ((unsigned int)get_core_id() == 0) + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Test framework glue, main(), and check_obpmark(): structured exactly +// like obpmark_1_1_pulp_par.c (verified against that file directly) - +// same testcase_t/run_suite pattern, same rt_cluster_id()/get_core_num()/ +// synch_barrier() bring-up (num_cores determined at runtime, not a +// compile-time constant), same core-0-only init/perf-counter guard +// before the parallel region, same cpu_perf_* cycle counter API +// (CSR_PCER_CYCLES). Only the benchmark body (radar_params_t setup, +// gen_rand_radar_data, SAR_focus_par, checksum) is specific to 1.2. +// ============================================================ +static HEAPSRAM radar_params_t g_params; + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_1_2_pulpd_par", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + num_cores = get_core_num(); + + if (rt_core_id() < num_cores) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int core_id = (unsigned int)get_core_id(); + + if (core_id == 0){ + g_params.lambda = 0.03f; + g_params.PRF = 1000.0f; + g_params.tau = 1e-5f; + g_params.fs = 5e7f; + g_params.vr = 7000.0f; + g_params.ro = 8e5f; + g_params.slope = 5e11f; + g_params.rsize = RSIZE; + g_params.asize = ASIZE; + + // rvalid = rsize - round(tau*fs), clamped to rsize if too small + float rvalid_f = (float)g_params.rsize - m_roundf(g_params.tau * g_params.fs); + uint32_t rvalid = (rvalid_f < (float)(g_params.rsize/2)) ? g_params.rsize : (uint32_t)rvalid_f; + g_params.rvalid = rvalid; + + g_params.avalid = (g_params.asize < 1024u) ? g_params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); + // apatch = 2^ceil(log2(avalid+1)) + float lg = m_log2f((float)(g_params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + g_params.apatch = apatch; + g_params.npatch = g_params.asize / g_params.avalid; // integer division, hardware (m ext.) + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u | cores=%u\n", + (unsigned)g_params.rvalid, (unsigned)g_params.avalid, + (unsigned)g_params.apatch, (unsigned)g_params.npatch, num_cores); + + gen_rand_radar_data(); + + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + } + synch_barrier(); // params, input data, and perf counters are ready + + start(); + + float vmax = -1e30f, vmin = 1e30f; + SAR_focus_par(&g_params, &vmax, &vmin); + + synch_barrier(); + stop(); + + if (core_id != 0) return; + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + /* Cluster runs at 50 MHz -> 20 ns per cycle */ + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("OBPMark #1.2 - Radar SAR (PULP cluster par)\n"); + printf("PULP cluster parallel (%u cores, RV32) - Astral @ 50 MHz\n", num_cores); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + /* Throughput: raw input complex samples (ASIZE*RSIZE) per second, in + * Ksample/s at 50 MHz (20 ns/cycle). Same fixed-point scheme as the 1.1 + * Mpixel/s metric (64-bit intermediates, %u-only printf); Ksample/s is + * used because with 256 input samples a Msample/s figure would be < 0.1 + * and lose all resolution. */ + unsigned int tp_samples = ASIZE * RSIZE; + unsigned int tp_int = (unsigned int)(((unsigned long long)tp_samples * 50000ULL) / cycles); + unsigned int tp_frac = (unsigned int)((((unsigned long long)tp_samples * 50000ULL) % cycles) * 100ULL / cycles); + printf(" Throughput : %u.%02u Ksample/s\n", tp_int, tp_frac); + printf(" Output[0][0] : %u\n", (unsigned)g_output_image[0]); + printf(" Checksum : %u\n", (unsigned)checksum); + + // No fixed expected value here (unlike 1.1's EXPECTED_OUTPUT_0_0): this + // is a new port with no prior cluster run to compare against. Cross- + // check this Output[0][0]/checksum against BOTH the host sequential + // port (obpmark_1_2_host_seq_full.c) AND the sequential PULP port + // (obpmark_1_2_pulpd_seq.c) - all three must match exactly, since all + // three compute the identical algorithm on identical synthetic input + // (same LCG seed). A mismatch against the sequential PULP port + // specifically (rather than the host one) would point at the + // parallelization logic itself, not at the software div/sqrt vs. + // hardware fdiv.s/fsqrt.s precision difference already expected + // between cluster and host. + result->errors = 0; +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_seq/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_seq/Makefile new file mode 100644 index 000000000..f9113ede8 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_seq/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_2_pulpd_seq.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_seq/obpmark_1_2_pulpd_seq.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_seq/obpmark_1_2_pulpd_seq.c new file mode 100644 index 000000000..841c03990 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_pulpd_seq/obpmark_1_2_pulpd_seq.c @@ -0,0 +1,660 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL port for the Astral PULP cluster (1x RV32 core) - +// FULL pipeline, ported from obpmark_1_2_host_seq_full.c (CVA6/RV64 host). +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository - see the host +// sequential port's header comment for the full derivation and size +// calculations, unchanged here (same RSIZE/ASIZE/APATCH/RVALID/NPATCH, +// same LCG seed, same fixed physical parameters). +// +// ============================================================ +// CLUSTER-SPECIFIC DIFFERENCE FROM THE HOST PORT: floating-point division +// and square root. +// +// Verified on this project's actual hardware configuration (not assumed): +// - hw/carfield.sv sets EnablePrivateFpu: 1 for the PULP cluster cores - +// add/sub/mul in hardware (confirmed by compiling `a+b`/`a*b` at +// -march=rv32imfcxgap9 -mabi=ilp32f: emits fadd.s/fmul.s directly). +// - hw/carfield.sv sets EnablePrivateFpDivSqrt: 0 and NumSharedFpu: 0 - +// the div/sqrt unit is NOT synthesized in any core's FPU +// (pulp_cluster.sv instantiates core_region with +// .FP_DIVSQRT(Cfg.EnablePrivateFpDivSqrt), which is 0). The compiler +// is unaware of this at -march=rv32imfcxgap9 and happily emits +// fdiv.s/fsqrt.s (confirmed: `a/b` compiles straight to fdiv.s) - +// those would trap as illegal instructions on this cluster at runtime. +// +// Consequence: every `/` on floats in the computational code below is +// replaced by a call to m_divf() (software reciprocal-refine division, +// never emits fdiv.s), and sqrt uses the Step-1-validated software m_sqrtf +// (bit-hack + Newton-Raphson) instead of the host port's inline fsqrt.s +// asm, which does not exist on this ISA at all. Plain +, -, * on floats +// are left as native operators: they compile to real hardware +// fadd.s/fsub.s/fmul.s on this core, unlike on the host's rv32imcxgap9 +// cluster tests seen elsewhere in this project (no `f` in march there). +// ============================================================ +// +// PULP-RUNTIME STRUCTURE: verified directly against this project's actual +// obpmark_1_1_pulp_seq.c. Same testcase_t/run_suite framework, same +// rt_cluster_id()/get_core_id()/synch_barrier() bring-up, same cpu_perf_* +// cycle counter (CSR_PCER_CYCLES), same .heapsram placement for all data +// buffers (avoids the large TCDM PT_LOAD write sequence that breaks the +// cluster boot mechanism, per that file's own comment). + +#include "pulp.h" +#include +#include +#include + +// ============================================================ +// Float <-> uint32_t bit reinterpretation uses memcpy, the idiom the C +// standard guarantees at any optimization level (no aliasing ambiguity), +// instead of a union. Tested on the cluster against the earlier union-based +// version: identical cycle count (821038) and identical result, i.e. a +// portability choice with no effect on the behavior described below. +// +// KNOWN OPEN ISSUE (cluster only): checkpoint tracing on the real cluster +// showed m_divf(16.0f, 4.0f) returning +Inf (0x7F800000) inside +// SAR_multilook (sx_bits=2139095040), which makes nfx=nfy=0 and degenerates +// the multilook output. The reported Output[0][0]=127 / Checksum=2032 and +// the cycle/throughput figures are therefore NOT valid until this is +// resolved; the host ports (89/2540) are unaffected. Next step: an isolated +// m_divf(16.0f, 4.0f) test with hard-coded literals, to tell whether the +// inputs or m_divf itself are at fault. +// ============================================================ +static inline uint32_t f_to_bits(float x){ + uint32_t u; + memcpy(&u, &x, sizeof(u)); + return u; +} +static inline float bits_to_f(uint32_t u){ + float x; + memcpy(&x, &u, sizeof(x)); + return x; +} + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c), adapted for the +// cluster: sqrtf is the SOFTWARE bit-hack+Newton version (no fsqrt.s on +// this core), and a new m_divf replaces the `/` operator wherever the +// computational code needs floating-point division (no fdiv.s either). +// sinf/cosf/log2f/atan2f/floorf/ceilf/roundf are unchanged from the host +// port: none of them use hardware division or sqrt internally. +// ============================================================ +#define PI_F 3.14159265358979323846f + +// Software sqrt (Step 1: bit-hack initial guess + 2x Newton-Raphson, +// ~4.6e-6 relative error measured against real libm). No fsqrt.s: this +// core's FPU has no synthesized div/sqrt unit. +static float m_sqrtf(float x){ + if (x <= 0.0f) return 0.0f; + uint32_t u = f_to_bits(x); + u = 0x5f3759df - (u >> 1); + float y = bits_to_f(u); + y = y * (1.5f - 0.5f*x*y*y); + y = y * (1.5f - 0.5f*x*y*y); + return x * y; +} + +// Software division: Newton-Raphson refinement of a bit-hack reciprocal +// estimate, then one final multiply. Never emits fdiv.s. Accuracy is +// exercised implicitly through the whole SAR pipeline; if higher precision +// is ever needed, add one more Newton iteration below (cheap: one more +// fmul.s/fsub.s pair, both hardware on this core). +static float m_divf(float a, float b){ + if (b == 0.0f) return 0.0f; + uint32_t u = f_to_bits(b); + u = 0x7EF127EA - u; // initial reciprocal estimate + float r = bits_to_f(u); + r = r * (2.0f - b * r); // Newton-Raphson iteration 1 + r = r * (2.0f - b * r); // Newton-Raphson iteration 2 + return a * r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + uint32_t bits = f_to_bits(x); + int e = (int)((bits >> 23) & 0xFF) - 127; + bits = (bits & 0x007FFFFF) | 0x3F800000; + float m = bits_to_f(bits); + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = m_divf(m - 1.0f, m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? m_divf(ay, ax) : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? m_divf(ax, ay) : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -m_divf(2.0f * PI_F, (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] = m_divf(data[2*i], (float)nn); data[2*i+1] = m_divf(data[2*i+1], (float)nn); } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap). Placed in .heapsram (TCDM), same +// pattern as obpmark_1_1_pulp_seq.c: this keeps them out of the ELF +// PT_LOAD payload, avoiding the large TCDM write sequence in +// load_binary() that breaks the cluster boot mechanism. +// ============================================================ +#define HEAPSRAM __attribute__((section(".heapsram"))) + +static HEAPSRAM float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static HEAPSRAM float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static HEAPSRAM float g_rrf[RRF_LEN]; +static HEAPSRAM float g_arf[ARF_LEN]; +static HEAPSRAM float g_aux[AUX_LEN]; +static HEAPSRAM uint32_t g_offsets[OFFS_LEN]; +static HEAPSRAM float g_ml_data[OUT_W * OUT_H]; +static HEAPSRAM uint8_t g_output_image[OUT_W * OUT_H]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * m_divf((float)bm_rand(), 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * m_divf(1.0f, fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = m_divf(mean, (float)width); + return m_divf(mean * params->PRF, 2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * m_divf(params->PRF, (float)params->avalid) + fDc; + float ratio = m_divf(params->lambda * delta, 2.0f*params->vr); + float offset = (m_divf(1.0f, m_sqrtf(1.0f - ratio*ratio)) - 1.0f) + * (params->ro + (float)i * m_divf(c_light, 2.0f*params->fs)); + offset = m_roundf(m_divf(offset, m_divf(c_light, 2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*m_divf(c_light, 2.0f*params->fs); + float ratio = m_divf(params->lambda*fDc, 2.0f*params->vr); + float rdc = m_divf(rnge, m_sqrtf(1.0f - ratio*ratio)); + float tauz = m_divf(rdc*m_divf(params->lambda, 10.0f) * 0.8f, params->vr); + float chirp = m_divf(m_divf(-(2.0f*params->vr*params->vr), params->lambda), rdc); + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = m_divf((float)params->asize, (float)image_w); + float sy = m_divf((float)params->rvalid, (float)image_w); + uint32_t nfx = (uint32_t)m_floorf(m_divf((float)params->avalid, sx)); + uint32_t nfy = (uint32_t)m_floorf(m_divf((float)params->rvalid, sy)); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = m_divf(fimg, (float)(isx*isy)); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = m_divf(256.0f, (vmax - vmin + 1e-30f)); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +static void SAR_focus(radar_params_t *params, float *out_max, float *out_min){ + *out_max = -1e30f; + *out_min = 1e30f; + + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + + for(uint32_t patch = 0; patch < params->npatch; patch++){ + SAR_range_compression(g_range_data, RANGE_H, RANGE_W, g_rrf); + complex_transpose(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->apatch, params->rvalid); + SAR_rcmc(g_azimuth_data, AZIM_H, AZIM_W, g_offsets); + SAR_azimuth_compression(g_azimuth_data, AZIM_H, AZIM_W, g_arf); + complex_transpose(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->rvalid, params->apatch); + SAR_multilook(g_range_data, g_ml_data, OUT_W, params, patch, out_max, out_min); + } + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Test framework glue, main(), and check_obpmark(): structured exactly +// like obpmark_1_1_pulp_seq.c (verified against that file directly) - +// same testcase_t/run_suite pattern, same rt_cluster_id()/get_core_id()/ +// synch_barrier() bring-up, same cpu_perf_* cycle counter API and event +// (CSR_PCER_CYCLES), same 50 MHz / 20 ns clock assumption. Only the +// benchmark body (radar_params_t setup, gen_rand_radar_data, SAR_focus, +// checksum) is specific to 1.2. +// ============================================================ +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_1_2_pulpd_seq", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + if (get_core_id() == 0) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + if (get_core_id() != 0) return; + + radar_params_t params; + params.lambda = 0.03f; + params.PRF = 1000.0f; + params.tau = 1e-5f; + params.fs = 5e7f; + params.vr = 7000.0f; + params.ro = 8e5f; + params.slope = 5e11f; + params.rsize = RSIZE; + params.asize = ASIZE; + + // rvalid = rsize - round(tau*fs), clamped to rsize if too small + float rvalid_f = (float)params.rsize - m_roundf(params.tau * params.fs); + uint32_t rvalid = (rvalid_f < (float)(params.rsize/2)) ? params.rsize : (uint32_t)rvalid_f; + params.rvalid = rvalid; + + params.avalid = (params.asize < 1024u) ? params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); // asize<1024 branch used here + // apatch = 2^ceil(log2(avalid+1)) + float lg = m_log2f((float)(params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + params.apatch = apatch; + params.npatch = params.asize / params.avalid; // integer division, hardware (m ext.) + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u\n", + (unsigned)params.rvalid, (unsigned)params.avalid, + (unsigned)params.apatch, (unsigned)params.npatch); + + gen_rand_radar_data(); + + /* Configure and start the cycle performance counter explicitly + * (bench_timer_start only calls perf_start() when PROFILE is defined). + * riscv_v5 API: conf_events selects which events, setall zeroes the + * counters, start(mask) activates counting. */ + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + + start(); + + float vmax, vmin; + SAR_focus(¶ms, &vmax, &vmin); + + stop(); + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + /* Cluster runs at 50 MHz -> 20 ns per cycle */ + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("OBPMark #1.2 - Radar SAR (PULP cluster seq)\n"); + printf("PULP cluster sequential (1 core, RV32) - Astral @ 50 MHz\n"); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + /* Throughput: raw input complex samples (ASIZE*RSIZE) per second, in + * Ksample/s at 50 MHz (20 ns/cycle). Same fixed-point scheme as the 1.1 + * Mpixel/s metric (64-bit intermediates, %u-only printf); Ksample/s is + * used because with 256 input samples a Msample/s figure would be < 0.1 + * and lose all resolution. */ + unsigned int tp_samples = ASIZE * RSIZE; + unsigned int tp_int = (unsigned int)(((unsigned long long)tp_samples * 50000ULL) / cycles); + unsigned int tp_frac = (unsigned int)((((unsigned long long)tp_samples * 50000ULL) % cycles) * 100ULL / cycles); + printf(" Throughput : %u.%02u Ksample/s\n", tp_int, tp_frac); + printf(" Output[0][0] : %u\n", (unsigned)g_output_image[0]); + printf(" Checksum : %u\n", (unsigned)checksum); + + // No fixed expected value here (unlike 1.1's EXPECTED_OUTPUT_0_0): + // this is a new port with no prior cluster run to compare against. + // Cross-check this Output[0][0]/checksum against the host sequential + // port's values (obpmark_1_2_host_seq_full.c) instead - they must + // match exactly, since both compute the identical algorithm on + // identical synthetic input (same LCG seed). + result->errors = 0; +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_seq/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_seq/Makefile new file mode 100644 index 000000000..f9113ede8 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_seq/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_1_2_pulpd_seq.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_seq/obpmark_1_2_pulpd_seq.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_seq/obpmark_1_2_pulpd_seq.c new file mode 100644 index 000000000..539ae758c --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_1_2_seq/obpmark_1_2_pulpd_seq.c @@ -0,0 +1,623 @@ +// Copyright 2024 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// OBPMark Benchmark #1.2 - Radar Image Processing (Range-Doppler SAR) +// Bare-metal SEQUENTIAL port for the Astral PULP cluster (1x RV32 core) - +// FULL pipeline, ported from obpmark_1_2_host_seq_full.c (CVA6/RV64 host). +// +// This is a FAITHFUL port of the reference SAR_focus() and all its callees +// (processing.c, src/1.2-radar), matching function names, structures and +// buffer layouts 1:1 against the original repository - see the host +// sequential port's header comment for the full derivation and size +// calculations, unchanged here (same RSIZE/ASIZE/APATCH/RVALID/NPATCH, +// same LCG seed, same fixed physical parameters). +// +// ============================================================ +// CLUSTER-SPECIFIC DIFFERENCE FROM THE HOST PORT: floating-point division +// and square root. +// +// Verified on this project's actual hardware configuration (not assumed): +// - hw/carfield.sv sets EnablePrivateFpu: 1 for the PULP cluster cores - +// add/sub/mul in hardware (confirmed by compiling `a+b`/`a*b` at +// -march=rv32imfcxgap9 -mabi=ilp32f: emits fadd.s/fmul.s directly). +// - hw/carfield.sv sets EnablePrivateFpDivSqrt: 0 and NumSharedFpu: 0 - +// the div/sqrt unit is NOT synthesized in any core's FPU +// (pulp_cluster.sv instantiates core_region with +// .FP_DIVSQRT(Cfg.EnablePrivateFpDivSqrt), which is 0). The compiler +// is unaware of this at -march=rv32imfcxgap9 and happily emits +// fdiv.s/fsqrt.s (confirmed: `a/b` compiles straight to fdiv.s) - +// those would trap as illegal instructions on this cluster at runtime. +// +// Consequence: every `/` on floats in the computational code below is +// replaced by a call to m_divf() (software reciprocal-refine division, +// never emits fdiv.s), and sqrt uses the Step-1-validated software m_sqrtf +// (bit-hack + Newton-Raphson) instead of the host port's inline fsqrt.s +// asm, which does not exist on this ISA at all. Plain +, -, * on floats +// are left as native operators: they compile to real hardware +// fadd.s/fsub.s/fmul.s on this core, unlike on the host's rv32imcxgap9 +// cluster tests seen elsewhere in this project (no `f` in march there). +// ============================================================ +// +// PULP-RUNTIME STRUCTURE: verified directly against this project's actual +// obpmark_1_1_pulp_seq.c. Same testcase_t/run_suite framework, same +// rt_cluster_id()/get_core_id()/synch_barrier() bring-up, same cpu_perf_* +// cycle counter (CSR_PCER_CYCLES), same .heapsram placement for all data +// buffers (avoids the large TCDM PT_LOAD write sequence that breaks the +// cluster boot mechanism, per that file's own comment). + +#include "pulp.h" +#include +#include + +// ============================================================ +// Step 1 math (validated: see step1_math_functions.c), adapted for the +// cluster: sqrtf is the SOFTWARE bit-hack+Newton version (no fsqrt.s on +// this core), and a new m_divf replaces the `/` operator wherever the +// computational code needs floating-point division (no fdiv.s either). +// sinf/cosf/log2f/atan2f/floorf/ceilf/roundf are unchanged from the host +// port: none of them use hardware division or sqrt internally. +// ============================================================ +#define PI_F 3.14159265358979323846f + +// Software sqrt (Step 1: bit-hack initial guess + 2x Newton-Raphson, +// ~4.6e-6 relative error measured against real libm). No fsqrt.s: this +// core's FPU has no synthesized div/sqrt unit. +static float m_sqrtf(float x){ + if (x <= 0.0f) return 0.0f; + union { float f; uint32_t u; } v; v.f = x; + v.u = 0x5f3759df - (v.u >> 1); + float y = v.f; + y = y * (1.5f - 0.5f*x*y*y); + y = y * (1.5f - 0.5f*x*y*y); + return x * y; +} + +// Software division: Newton-Raphson refinement of a bit-hack reciprocal +// estimate, then one final multiply. Never emits fdiv.s. Accuracy is +// exercised implicitly through the whole SAR pipeline; if higher precision +// is ever needed, add one more Newton iteration below (cheap: one more +// fmul.s/fsub.s pair, both hardware on this core). +static float m_divf(float a, float b){ + if (b == 0.0f) return 0.0f; + union { float f; uint32_t u; } v; v.f = b; + v.u = 0x7EF127EA - v.u; // initial reciprocal estimate + float r = v.f; + r = r * (2.0f - b * r); // Newton-Raphson iteration 1 + r = r * (2.0f - b * r); // Newton-Raphson iteration 2 + return a * r; +} + +static inline float reduce_angle(float x){ + const float two_pi = 2.0f * PI_F; + const float inv_two_pi = 1.0f / two_pi; + float k = x * inv_two_pi; + float kr = (k >= 0.0f) ? (float)(int)(k + 0.5f) : (float)(int)(k - 0.5f); + return x - two_pi * kr; +} +static float m_sinf(float x){ + x = reduce_angle(x); + if (x > PI_F/2) x = PI_F - x; + if (x < -PI_F/2) x = -PI_F - x; + float x2 = x*x; + return x * (1.0f + x2*(-1.0f/6.0f + x2*(1.0f/120.0f + + x2*(-1.0f/5040.0f + x2*(1.0f/362880.0f))))); +} +static inline float m_cosf(float x){ return m_sinf(x + PI_F/2); } + +static float m_floorf(float x){ + float xi = (float)(int)x; + return (x < xi) ? xi - 1.0f : xi; +} +static float m_ceilf(float x){ + float xi = (float)(int)x; + return (x > xi) ? xi + 1.0f : xi; +} +static float m_roundf(float x){ + return (x >= 0.0f) ? m_floorf(x + 0.5f) : m_ceilf(x - 0.5f); +} + +static float m_log2f(float x){ + if (x <= 0.0f) return -1e30f; + union { float f; uint32_t u; } v; v.f = x; + int e = (int)((v.u >> 23) & 0xFF) - 127; + v.u = (v.u & 0x007FFFFF) | 0x3F800000; + float m = v.f; + if (m > 1.41421356f){ m = m * 0.5f; e = e + 1; } + float u = m_divf(m - 1.0f, m + 1.0f); + float u2 = u*u; + float atanh_u = u * (1.0f + u2*(1.0f/3.0f + u2*(1.0f/5.0f + u2*(1.0f/7.0f)))); + float log2_m = 2.88539008f * atanh_u; + return (float)e + log2_m; +} + +static float m_atanf_01(float t){ + float t2 = t*t; + return t*(0.9998660f + t2*(-0.3302995f + t2*(0.1801410f + + t2*(-0.0851330f + t2*0.0208351f)))); +} +static float m_atan2f(float y, float x){ + if (x == 0.0f && y == 0.0f) return 0.0f; + float ax = (x < 0.0f) ? -x : x; + float ay = (y < 0.0f) ? -y : y; + float t, r; + if (ax >= ay){ + t = (ax > 0.0f) ? m_divf(ay, ax) : 0.0f; + r = m_atanf_01(t); + } else { + t = (ay > 0.0f) ? m_divf(ax, ay) : 0.0f; + r = PI_F/2.0f - m_atanf_01(t); + } + if (x < 0.0f) r = PI_F - r; + if (y < 0.0f) r = -r; + return r; +} + +// ============================================================ +// Complex arithmetic on interleaved float buffers (same memory layout +// as the reference's std::complex* reinterpret_cast: re,im,re,im..) +// ============================================================ +typedef struct { float re, im; } cplx_t; + +static inline cplx_t cld(const float *p, int idx){ + cplx_t c; c.re = p[2*idx]; c.im = p[2*idx+1]; return c; +} +static inline void cst(float *p, int idx, cplx_t v){ + p[2*idx] = v.re; p[2*idx+1] = v.im; +} +static inline cplx_t cmul(cplx_t a, cplx_t b){ + cplx_t r; r.re = a.re*b.re - a.im*b.im; r.im = a.re*b.im + a.im*b.re; return r; +} +static inline cplx_t cconj(cplx_t a){ cplx_t r; r.re = a.re; r.im = -a.im; return r; } +static inline cplx_t cadd(cplx_t a, cplx_t b){ cplx_t r; r.re=a.re+b.re; r.im=a.im+b.im; return r; } +static inline float cabsf_(cplx_t a){ return m_sqrtf(a.re*a.re + a.im*a.im); } +static inline float cargf_(cplx_t a){ return m_atan2f(a.im, a.re); } +static inline cplx_t cpolar(float mag, float phase){ + cplx_t r; r.re = mag*m_cosf(phase); r.im = mag*m_sinf(phase); return r; +} + +// ============================================================ +// FFT (iterative Cooley-Tukey, interleaved re/im, in place). Same kernel +// as the reference (adapted from GPU4S_benchmarks fft_kernel). +// ============================================================ +static void fft_kernel(float *data, int nn, int isign){ + int n, mmax, m, j, istep, i; + float wtemp, wr, wpr, wpi, wi, theta; + float tempr, tempi; + + n = nn << 1; + j = 1; + for(i = 1; i < n; i += 2){ + if(j > i){ + float t; + t = data[j-1]; data[j-1] = data[i-1]; data[i-1] = t; + t = data[j]; data[j] = data[i]; data[i] = t; + } + m = nn; + while(m >= 2 && j > m){ j -= m; m >>= 1; } + j += m; + } + mmax = 2; + while(n > mmax){ + istep = mmax << 1; + theta = -m_divf(2.0f * PI_F, (float)(mmax * isign)); + wtemp = m_sinf(0.5f * theta); + wpr = -2.0f * wtemp * wtemp; + wpi = m_sinf(theta); + wr = 1.0f; wi = 0.0f; + for(m = 1; m < mmax; m += 2){ + for(i = m; i <= n; i += istep){ + j = i + mmax; + tempr = wr * data[j-1] - wi * data[j]; + tempi = wr * data[j] + wi * data[j-1]; + data[j-1] = data[i-1] - tempr; + data[j] = data[i] - tempi; + data[i-1] += tempr; + data[i] += tempi; + } + wtemp = wr; + wr += wr * wpr - wi * wpi; + wi += wi * wpr + wtemp * wpi; + } + mmax = istep; + } +} +static void fft(float *data, int nn){ fft_kernel(data, nn, 1); } +static void ifft(float *data, int nn){ + fft_kernel(data, nn, -1); + for(int i = 0; i < nn; i++){ data[2*i] = m_divf(data[2*i], (float)nn); data[2*i+1] = m_divf(data[2*i+1], (float)nn); } +} + +static unsigned int next_power_of2(unsigned int n){ + unsigned int v = n; + v--; v |= v>>1; v |= v>>2; v |= v>>4; v |= v>>8; v |= v>>16; v++; + return v; +} + +// ============================================================ +// radar_params_t (identical fields to device.h) +// ============================================================ +typedef struct { + float lambda, PRF, tau, fs, vr, ro, slope; + uint32_t asize, avalid, apatch, rsize, rvalid, npatch; +} radar_params_t; + +// ============================================================ +// Frame descriptors (same layout as obpmark_image.h, 1D indexing) +// ============================================================ +typedef struct { float *f; unsigned int w, h; } framefp_t; +typedef struct { uint8_t *f; unsigned int w, h; } frame8_t; + +// ============================================================ +// Compile-time sizes (derived by hand above, matches device_memory_init) +// ============================================================ +#define RSIZE 16u +#define ASIZE 16u +#define AVALID 16u +#define APATCH 32u +#define RVALID 16u +#define NPATCH 1u +#define NEXT_POW2_RS 16u // next_power_of2(RSIZE) + +#define RANGE_W (NEXT_POW2_RS*2u) // 32 floats/row +#define RANGE_H APATCH // 32 rows +#define AZIM_W (APATCH*2u) // 64 floats/row +#define AZIM_H RVALID // 16 rows +#define RRF_LEN (NEXT_POW2_RS*2u) // 32 floats (16 complex) +#define ARF_LEN (APATCH*2u) // 64 floats (32 complex) +#define AUX_LEN (RSIZE*2u) // 32 floats (16 complex) +#define OFFS_LEN (RVALID*APATCH) // 512 + +#define OUT_W 4u +#define OUT_H 4u // = nfx, with npatch=1 (see SAR_multilook derivation) + +// ============================================================ +// Static buffers (bare-metal, no heap). Placed in .heapsram (TCDM), same +// pattern as obpmark_1_1_pulp_seq.c: this keeps them out of the ELF +// PT_LOAD payload, avoiding the large TCDM write sequence in +// load_binary() that breaks the cluster boot mechanism. +// ============================================================ +#define HEAPSRAM __attribute__((section(".heapsram"))) + +static HEAPSRAM float g_range_data [RANGE_H * RANGE_W]; // h=APATCH,w=RANGE_W +static HEAPSRAM float g_azimuth_data[AZIM_H * AZIM_W]; // h=RVALID,w=AZIM_W +static HEAPSRAM float g_rrf[RRF_LEN]; +static HEAPSRAM float g_arf[ARF_LEN]; +static HEAPSRAM float g_aux[AUX_LEN]; +static HEAPSRAM uint32_t g_offsets[OFFS_LEN]; +static HEAPSRAM float g_ml_data[OUT_W * OUT_H]; +static HEAPSRAM uint8_t g_output_image[OUT_W * OUT_H]; + +// ============================================================ +// LCG synthetic data generator (same generator as 1.1 / 1.2-reduced) +// ============================================================ +static unsigned int _rand_state; +static unsigned int bm_rand(void){ + _rand_state = _rand_state * 1103515245u + 12345u; + return (_rand_state >> 16u) & 0x7FFFu; +} +// uniform in [-15.5, 15.5], matching the reference gen_data() amplitude +static float rand_amp(void){ + return -15.5f + 31.0f * m_divf((float)bm_rand(), 32768.0f); +} + +// Fill only the first ASIZE rows of range_data with random complex data; +// rows ASIZE..APATCH-1 stay zero. This matches the reference datagen's +// line-copy bound check (ind/(width<<1) < asize), which zero-pads the +// azimuth lines beyond asize within each apatch-sized patch. +static void gen_rand_radar_data(void){ + _rand_state = 21121993u; + for(unsigned int row = 0; row < ASIZE; row++){ + for(unsigned int k = 0; k < NEXT_POW2_RS; k++){ + unsigned int idx = row * (RANGE_W/2) + k; + cplx_t v; + v.re = rand_amp(); + v.im = rand_amp(); + cst(g_range_data, idx, v); + } + } + // rows ASIZE..APATCH-1 of g_range_data are left at zero (static init) +} + +// ============================================================ +// ref_func: builds a chirp in time, transforms to frequency (matches +// the reference exactly, including the nit=floor(tau*fs) truncation). +// ============================================================ +static void ref_func(float *ref, float fc, float slope, float tau, float fs, + uint32_t length, uint32_t fftlen){ + uint32_t nit = (uint32_t)m_floorf(tau * fs); + float phase; + for(uint32_t i = 0; i < nit && i < length; i++){ + phase = (-((float)nit)/2.0f + (float)i) * m_divf(1.0f, fs); + phase = 2.0f*PI_F*fc*phase + PI_F*slope*phase*phase; + cst(ref, i, cpolar(1.0f, phase)); + } + fft(ref, (int)fftlen); +} + +static void SAR_range_ref(float *rrf, const radar_params_t *params){ + ref_func(rrf, 0.0f, params->slope, params->tau, params->fs, + params->rsize, next_power_of2(params->rsize)); +} + +// ============================================================ +// SAR_DCE - Doppler Centroid Estimation. Exercises m_atan2f in situ. +// ============================================================ +static float SAR_DCE(float *aux, const float *data, uint32_t data_h, + const radar_params_t *params){ + uint32_t width = params->rsize; + uint32_t off = next_power_of2(width); + float mean = 0.0f; + for(uint32_t i = 0; i < data_h - 1; i++){ + for(uint32_t j = 0; j < width; j++){ + cplx_t a = cld(aux, j); + cplx_t p = cmul(cconj(cld(data, i*off+j)), cld(data, (i+1)*off+j)); + a = cadd(a, p); + cst(aux, j, a); + if (i == data_h - 2) mean += cargf_(a); + } + } + mean = m_divf(mean, (float)width); + return m_divf(mean * params->PRF, 2.0f * PI_F); +} + +static void SAR_rcmc_table(const radar_params_t *params, uint32_t *offsets, + float fDc, uint32_t width){ + const float c_light = 2.99792458e8f; + for(uint32_t i = 0; i < params->rvalid; i++){ + for(uint32_t j = 0; j < width; j++){ + float delta = (float)j * m_divf(params->PRF, (float)params->avalid) + fDc; + float ratio = m_divf(params->lambda * delta, 2.0f*params->vr); + float offset = (m_divf(1.0f, m_sqrtf(1.0f - ratio*ratio)) - 1.0f) + * (params->ro + (float)i * m_divf(c_light, 2.0f*params->fs)); + offset = m_roundf(m_divf(offset, m_divf(c_light, 2.0f*params->fs))); + uint32_t ind = i*width + j; + offsets[ind] = (uint32_t)((int)offset + (int)i); + } + } +} + +static void SAR_azimuth_ref(float *arf, const radar_params_t *params, float fDc){ + const float c_light = 2.99792458e8f; + float rnge = params->ro + ((float)params->rvalid/2.0f)*m_divf(c_light, 2.0f*params->fs); + float ratio = m_divf(params->lambda*fDc, 2.0f*params->vr); + float rdc = m_divf(rnge, m_sqrtf(1.0f - ratio*ratio)); + float tauz = m_divf(rdc*m_divf(params->lambda, 10.0f) * 0.8f, params->vr); + float chirp = m_divf(m_divf(-(2.0f*params->vr*params->vr), params->lambda), rdc); + ref_func(arf, fDc, chirp, tauz, params->PRF, params->apatch, params->apatch); +} + +// ============================================================ +// reference_multiplication: data *= conj(ref), row by row +// ============================================================ +static void reference_multiply_rows(float *data, uint32_t dh, uint32_t dw_floats, + const float *ref){ + uint32_t width = dw_floats >> 1; + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t idx = i*width + j; + cplx_t d = cld(data, idx); + cplx_t r = cld(ref, j); + cst(data, idx, cmul(d, cconj(r))); + } +} + +static void SAR_range_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *rrf){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + reference_multiply_rows(data, dh, dw_floats, rrf); + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +static void complex_transpose(const float *in, uint32_t in_w_floats, + float *out, uint32_t out_w_floats, + uint32_t nrows, uint32_t ncols){ + uint32_t a_rw = in_w_floats >> 1; + uint32_t b_rw = out_w_floats >> 1; + for(uint32_t i = 0; i < nrows; i++) + for(uint32_t j = 0; j < ncols; j++) + cst(out, j*b_rw + i, cld(in, i*a_rw + j)); +} + +static void SAR_rcmc(float *data, uint32_t dh, uint32_t dw_floats, + const uint32_t *offsets){ + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) fft(&data[k*dw_floats], (int)width); + for(uint32_t i = 0; i < dh; i++) + for(uint32_t j = 0; j < width; j++){ + uint32_t ind = i*width + j; + if (offsets[ind] < dh) + cst(data, ind, cld(data, j + offsets[ind]*width)); + else { + cplx_t z = {0.0f,0.0f}; + cst(data, ind, z); + } + } +} + +static void SAR_azimuth_compression(float *data, uint32_t dh, uint32_t dw_floats, + const float *arf){ + reference_multiply_rows(data, dh, dw_floats, arf); + uint32_t width = dw_floats >> 1; + for(uint32_t k = 0; k < dh; k++) ifft(&data[k*dw_floats], (int)width); +} + +// ============================================================ +// SAR_multilook: spatial binning of |complex| magnitude + log2. +// See the size-derivation comment block at the top: out_w=4 -> sx=sy=4, +// isx=isy=4, nfx=nfy=4, out_h=nfx=4 (npatch=1). +// ============================================================ +static void SAR_multilook(const float *radar_data, /* range_data after 2nd transpose */ + float *image, uint32_t image_w, + const radar_params_t *params, uint32_t patch, + float *out_max, float *out_min){ + float sx = m_divf((float)params->asize, (float)image_w); + float sy = m_divf((float)params->rvalid, (float)image_w); + uint32_t nfx = (uint32_t)m_floorf(m_divf((float)params->avalid, sx)); + uint32_t nfy = (uint32_t)m_floorf(m_divf((float)params->rvalid, sy)); + uint32_t isx = (uint32_t)m_floorf(sx); + uint32_t isy = (uint32_t)m_floorf(sy); + uint32_t stride = next_power_of2(params->rsize); // complex samples/row in range_data + uint32_t offset = patch * nfx * nfy; + float vmax = *out_max, vmin = *out_min; + + for(uint32_t i = 0; i < nfx; i++){ + for(uint32_t j = 0; j < nfy; j++){ + float fimg = 0.0f; + for(uint32_t ix = 0; ix < isx; ix++) + for(uint32_t jy = 0; jy < isy; jy++){ + uint32_t idx = (i*isx+ix)*stride + (j*isy+jy); + fimg += cabsf_(cld(radar_data, idx)); + } + float value = m_divf(fimg, (float)(isx*isy)); + value = (value <= 0.0f) ? 0.0f : m_log2f(value); + image[offset + i*image_w + j] = value; + if (value > vmax) vmax = value; + if (value < vmin) vmin = value; + } + } + *out_max = vmax; *out_min = vmin; +} + +static void quantize(const float *data, uint8_t *image, uint32_t w, uint32_t h, + float vmax, float vmin){ + float scale = m_divf(256.0f, (vmax - vmin + 1e-30f)); + for(uint32_t i = 0; i < h; i++){ + for(uint32_t j = 0; j < w; j++){ + float q = scale * (data[i*w+j] - vmin); + if (q > 255.0f) q = 255.0f; + if (q < 0.0f) q = 0.0f; + image[i*w+j] = (uint8_t)m_floorf(q); + } + } +} + +// ============================================================ +// SAR_focus: orchestrates the full pipeline, matching the reference +// function-for-function (npatch loop kept for fidelity even though +// NPATCH==1 here). +// ============================================================ +static void SAR_focus(radar_params_t *params, float *out_max, float *out_min){ + *out_max = -1e30f; + *out_min = 1e30f; + + SAR_range_ref(g_rrf, params); + float fDc = SAR_DCE(g_aux, g_range_data, RANGE_H, params); + SAR_rcmc_table(params, g_offsets, fDc, params->apatch); + SAR_azimuth_ref(g_arf, params, fDc); + + for(uint32_t patch = 0; patch < params->npatch; patch++){ + SAR_range_compression(g_range_data, RANGE_H, RANGE_W, g_rrf); + complex_transpose(g_range_data, RANGE_W, g_azimuth_data, AZIM_W, + params->apatch, params->rvalid); + SAR_rcmc(g_azimuth_data, AZIM_H, AZIM_W, g_offsets); + SAR_azimuth_compression(g_azimuth_data, AZIM_H, AZIM_W, g_arf); + complex_transpose(g_azimuth_data, AZIM_W, g_range_data, RANGE_W, + params->rvalid, params->apatch); + SAR_multilook(g_range_data, g_ml_data, OUT_W, params, patch, out_max, out_min); + } + quantize(g_ml_data, g_output_image, OUT_W, OUT_H, *out_max, *out_min); +} + +// ============================================================ +// Test framework glue, main(), and check_obpmark(): structured exactly +// like obpmark_1_1_pulp_seq.c (verified against that file directly) - +// same testcase_t/run_suite pattern, same rt_cluster_id()/get_core_id()/ +// synch_barrier() bring-up, same cpu_perf_* cycle counter API and event +// (CSR_PCER_CYCLES), same 50 MHz / 20 ns clock assumption. Only the +// benchmark body (radar_params_t setup, gen_rand_radar_data, SAR_focus, +// checksum) is specific to 1.2. +// ============================================================ +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_1_2_seq", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + if (get_core_id() == 0) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + if (get_core_id() != 0) return; + + radar_params_t params; + params.lambda = 0.03f; + params.PRF = 1000.0f; + params.tau = 1e-5f; + params.fs = 5e7f; + params.vr = 7000.0f; + params.ro = 8e5f; + params.slope = 5e11f; + params.rsize = RSIZE; + params.asize = ASIZE; + + // rvalid = rsize - round(tau*fs), clamped to rsize if too small + float rvalid_f = (float)params.rsize - m_roundf(params.tau * params.fs); + uint32_t rvalid = (rvalid_f < (float)(params.rsize/2)) ? params.rsize : (uint32_t)rvalid_f; + params.rvalid = rvalid; + + params.avalid = (params.asize < 1024u) ? params.asize + : (uint32_t)(2048.0f - m_ceilf(0.0f)); // asize<1024 branch used here + // apatch = 2^ceil(log2(avalid+1)) + float lg = m_log2f((float)(params.avalid + 1u)); + uint32_t exp_bits = (uint32_t)m_ceilf(lg); + uint32_t apatch = 1u; + for (uint32_t b = 0; b < exp_bits; b++) apatch <<= 1; + params.apatch = apatch; + params.npatch = params.asize / params.avalid; // integer division, hardware (m ext.) + + printf("Derived: rvalid=%u avalid=%u apatch=%u npatch=%u\n", + (unsigned)params.rvalid, (unsigned)params.avalid, + (unsigned)params.apatch, (unsigned)params.npatch); + + gen_rand_radar_data(); + + /* Configure and start the cycle performance counter explicitly + * (bench_timer_start only calls perf_start() when PROFILE is defined). + * riscv_v5 API: conf_events selects which events, setall zeroes the + * counters, start(mask) activates counting. */ + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + + start(); + + float vmax, vmin; + SAR_focus(¶ms, &vmax, &vmin); + + stop(); + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + /* Cluster runs at 50 MHz -> 20 ns per cycle */ + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + + uint32_t checksum = 0; + for(unsigned int i = 0; i < OUT_W*OUT_H; i++) checksum += g_output_image[i]; + + printf("OBPMark #1.2 - Radar SAR (FULL pipeline, PULP cluster seq)\n"); + printf("PULP cluster sequential (1 core, RV32) - Astral @ 50 MHz\n"); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Output[0][0] : %u\n", (unsigned)g_output_image[0]); + printf(" Checksum : %u\n", (unsigned)checksum); + + // No fixed expected value here (unlike 1.1's EXPECTED_OUTPUT_0_0): + // this is a new port with no prior cluster run to compare against. + // Cross-check this Output[0][0]/checksum against the host sequential + // port's values (obpmark_1_2_host_seq_full.c) instead - they must + // match exactly, since both compute the identical algorithm on + // identical synthetic input (same LCG seed). + result->errors = 0; +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_par/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_par/Makefile new file mode 100644 index 000000000..ac5349123 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_par/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_2_1_pulpd_par.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_par/obpmark_2_1_pulpd_par.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_par/obpmark_2_1_pulpd_par.c new file mode 100644 index 000000000..ba2f4afcb --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_par/obpmark_2_1_pulpd_par.c @@ -0,0 +1,514 @@ +/* + * Copyright 2024 ETH Zurich and University of Bologna. + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + * + * OBPMark Benchmark #2.1 - CCSDS 121.0 Data Compression + * Parallel port for the PULP integer cluster of Astral (8 cores, RV32) + * + * SPMD: every core runs the same code on a distinct block range within each + * reference interval. Synchronisation uses the cluster hardware barrier + * (synch_barrier()). Data buffers live in .heapsram (TCDM) to keep them out + * of the ELF payload. Same algorithm and compile-time configuration as + * obpmark_2_1_pulpd_seq.c and obpmark_2_1_host_seq.c (N_BITS=16, + * J_BLOCKSIZE=8, R_SAMPLES_INTERVAL=8, STEPS=2). Being purely integer + * arithmetic, the result is expected to be bit-identical to the already + * validated host-domain sequential run: Output[0]=64, Total output + * bytes=130, Checksum=13073 - used here as the PASS/FAIL criterion, as + * EXPECTED_OUTPUT_0_0 is in the parallel cluster port of OBPMark #1.1. + * + * Phase structure per reference interval (same decomposition validated on + * the CVA6 host SMP port): + * Phase 1 (PARALLEL, by block range): preprocessing. The unit-delay + * predictor is recomputed directly from input_data[idx-1] - the raw + * previous sample - rather than carried in a persistent DelayedStack as + * the sequential port does, so every sample is independent and the phase + * is embarrassingly parallel. + * Phase 2 (SEQUENTIAL, core 0): zero-block run-length counting. A genuine + * recurrence (each zero block's counter depends on the running count of + * all preceding consecutive zero blocks), analogous to the RCMC shift + * kept sequential in the 1.2 SMP port. Cheap: R_SAMPLES_INTERVAL + * iterations. + * Phase 3 (PARALLEL, by block range): adaptive entropy encoder candidate + * selection. Each block reads only its own preprocessed samples, and + * this is the dominant cost (up to n_bits SampleSplitting trials per + * block), hence the main parallelisation target. + * Phase 4 (SEQUENTIAL, core 0): bitstream serialization. Bit-level packing + * carries state (current byte, current bit offset) across blocks of + * data-dependent length, so - unlike the sequential port, which writes + * directly inside AdaptativeEntropyEncoder - Phase 3 only stores each + * block's winning candidate and Phase 4 writes them in block order, + * mirroring the reference OpenMP/CUDA ports' structure. + * + * Core id is read from hardware inside each phase rather than cached across + * barriers, the same defensive approach adopted in the 1.1 parallel cluster + * port and the CVA6 SMP ports. + * + * NOTE ON EXPECTED SPEEDUP: with R_SAMPLES_INTERVAL=8 and 8 cores, Phase 3 + * gives exactly one block per core, so barrier overhead is likely to + * dominate - the same effect already measured on the 2-hart host SMP port + * (1.14x over sequential). Raising R_SAMPLES_INTERVAL and/or STEPS amortises + * the four barriers per interval over more work; note that changing either + * invalidates the EXPECTED_* constants below, which must then be re-derived + * from a sequential run at the new configuration. + */ + +#include "pulp.h" +#include +#include + +#define HEAPSRAM __attribute__((section(".heapsram"))) + +/* ============================================================ + * Compile-time configuration (identical to obpmark_2_1_pulpd_seq.c) + * ============================================================ */ +#define N_BITS 16u +#define J_BLOCKSIZE 8u +#define R_SAMPLES_INTERVAL 8u +#define STEPS 2u +#define PREPROCESSOR_ACTIVE 1 + +#define SAMPLES_PER_STEP (R_SAMPLES_INTERVAL * J_BLOCKSIZE) +#define TOTAL_SAMPLES (SAMPLES_PER_STEP * STEPS) + +#define OUTPUT_BUFFER_BYTES 512u + +#define EXPECTED_OUTPUT_0 64u +#define EXPECTED_CHECKSUM 13073u +#define EXPECTED_TOTAL_BYTES 130u + +/* ============================================================ + * CCSDS 121.0 internal identifiers (benchmark.h) + * ============================================================ */ +#define ZERO_BLOCK_ID 0u +#define FUNDAMENTAL_SEQUENCE_ID 1u +#define SECOND_EXTENSION_ID 2u +#define SAMPLE_SPLITTING_ID 3u +#define NO_COMPRESSION_ID 32u + +#define MIN_MACRO(x, y) (((x) < (y)) ? (x) : (y)) + +/* Static block partitioning, same policy as OpenMP schedule(static) and as + * split_range() in the 1.1 parallel cluster port. */ +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +/* ============================================================ + * Output bit stream (output_format_utils.h) - written by core 0 only + * ============================================================ */ +typedef struct { + uint8_t OutputBitStream[OUTPUT_BUFFER_BYTES]; + unsigned int num_bits; + unsigned int num_total_bytes; +} OutputBitStream_t; + +static void writeWord(OutputBitStream_t *status, unsigned int word, int number_bits) { + int i; + for (i = number_bits - 1; i >= 0; --i) { + status->OutputBitStream[status->num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1u) << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { status->num_bits = 0u; status->num_total_bytes++; } + } +} + +static void writeWordChar(OutputBitStream_t *status, uint8_t word, int number_bits) { + int i; + for (i = number_bits - 1; i >= 0; --i) { + status->OutputBitStream[status->num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1u) << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { status->num_bits = 0u; status->num_total_bytes++; } + } +} + +static void writeValue(OutputBitStream_t *status, uint8_t value, unsigned int number_bits) { + unsigned int i; + if (value != 0u && value != 1u) return; + value &= 0x1u; + for (i = 0; i < number_bits; ++i) { + status->OutputBitStream[status->num_total_bytes] |= (uint8_t)(value << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { status->num_bits = 0u; status->num_total_bytes++; } + } +} + +static void ZeroBlockWriter(OutputBitStream_t *status, unsigned int size) { + writeValue(status, 0u, size - 1u); + writeValue(status, 1u, 1u); +} + +static void NoCompressionWriter(OutputBitStream_t *status, unsigned int j_blocksize, + unsigned int n_bits, unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) writeWord(status, data[i], (int)n_bits); +} + +static void FundamentalSequenceWriter(OutputBitStream_t *status, unsigned int j_blocksize, unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) { writeValue(status, 0u, data[i]); writeValue(status, 1u, 1u); } +} + +static void SecondExtensionWriter(OutputBitStream_t *status, unsigned int half_blocksize, unsigned int *data) { + unsigned int i; + for (i = 0; i < half_blocksize; ++i) writeWord(status, data[i], 32); +} + +static void SampleSplittingWriter(OutputBitStream_t *status, unsigned int j_blocksize, + unsigned int k, unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) { writeValue(status, 0u, data[i] >> k); writeValue(status, 1u, 1u); } + for (i = 0; i < j_blocksize; ++i) writeWord(status, data[i], (int)k); +} + +/* ============================================================ + * Preprocessor: CCSDS 121.0 error mapper. Parallel-safe: the predicted + * value is always the previous RAW input sample, read directly from + * input_data, so no cross-core state is carried. + * ============================================================ */ +static int PredictorErrorMapper(int predicted_value, int prediction_error, unsigned int n_bits) { + const int x_min = 0; + const int x_max = (int)((1u << n_bits) - 1u); + const int theta = MIN_MACRO(predicted_value - x_min, x_max - predicted_value); + + int preprocessed = theta + (prediction_error < 0 ? -prediction_error : prediction_error); + + if (prediction_error >= 0 && prediction_error <= theta) + preprocessed = 2 * prediction_error; + else if (prediction_error < 0 && -theta <= prediction_error) + preprocessed = (2 * (-prediction_error)) - 1; + + return preprocessed; +} + +/* ============================================================ + * Adaptive entropy encoder: candidate structure and selection + * ============================================================ */ +typedef struct { + unsigned int size; + unsigned int data[J_BLOCKSIZE]; + uint8_t CompressionIdentifier; + unsigned int CompressionIdentifierInternal; +} FCompressedData_t; + +static void CopyCandidate(FCompressedData_t *dst, const FCompressedData_t *src) { + unsigned int i; + dst->size = src->size; + dst->CompressionIdentifier = src->CompressionIdentifier; + dst->CompressionIdentifierInternal = src->CompressionIdentifierInternal; + for (i = 0; i < J_BLOCKSIZE; ++i) dst->data[i] = src->data[i]; +} + +static void KeepBest(FCompressedData_t *best, const FCompressedData_t *candidate) { + if (candidate->size < best->size) CopyCandidate(best, candidate); +} + +static void NoCompression(FCompressedData_t *out, unsigned int n_bits, + unsigned int j_blocksize, unsigned int *samples) { + unsigned int i; + out->CompressionIdentifierInternal = NO_COMPRESSION_ID; + out->size = n_bits * j_blocksize; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; + + if (n_bits < 3u) out->CompressionIdentifier = 0x1u; + else if (n_bits < 5u) out->CompressionIdentifier = 0x3u; + else if (n_bits <= 8u) out->CompressionIdentifier = 0x7u; + else if (n_bits <= 16u) out->CompressionIdentifier = 0xFu; + else out->CompressionIdentifier = 0x1Fu; +} + +static unsigned int GetSizeSecondExtension(unsigned int half_blocksize, unsigned int *halved) { + unsigned int size = 0, i; + for (i = 0; i < half_blocksize; ++i) size += halved[i] + 1u; + return size; +} + +static void SecondExtension(FCompressedData_t *out, unsigned int j_blocksize, unsigned int *samples) { + const unsigned int half_blocksize = j_blocksize / 2u; + unsigned int halved[J_BLOCKSIZE / 2u]; + unsigned int i, compressed_size; + + for (i = 0; i < half_blocksize; ++i) + halved[i] = (((samples[2u*i] + samples[2u*i+1u]) * (samples[2u*i] + samples[2u*i+1u] + 1u)) / 2u) + samples[2u*i+1u]; + + out->CompressionIdentifier = SECOND_EXTENSION_ID - 1u; + out->size = j_blocksize * 32u; + + compressed_size = GetSizeSecondExtension(half_blocksize, halved); + if (compressed_size > j_blocksize * 32u) return; + + out->size = compressed_size; + out->CompressionIdentifierInternal = SECOND_EXTENSION_ID; + for (i = 0; i < half_blocksize; ++i) out->data[i] = halved[i]; +} + +static unsigned int GetSizeFundamentalSequence(unsigned int j_blocksize, unsigned int *samples) { + unsigned int size = 0, i; + for (i = 0; i < j_blocksize; ++i) size += samples[i] + 1u; + return size; +} + +static void FundamentalSequence(FCompressedData_t *out, unsigned int j_blocksize, unsigned int *samples) { + unsigned int i, compressed_size; + out->CompressionIdentifier = FUNDAMENTAL_SEQUENCE_ID; + out->size = j_blocksize * 32u; + + compressed_size = GetSizeFundamentalSequence(j_blocksize, samples); + if (compressed_size > j_blocksize * 32u) return; + + out->size = compressed_size; + out->CompressionIdentifierInternal = FUNDAMENTAL_SEQUENCE_ID; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; +} + +static unsigned int GetSizeSampleSplitting(unsigned int j_blocksize, unsigned int *samples, unsigned int k) { + unsigned int size = 0, i; + for (i = 0; i < j_blocksize; ++i) size += (k + (samples[i] >> k) + 1u); + return size; +} + +static void SampleSplitting(FCompressedData_t *out, unsigned int n_bits, unsigned int j_blocksize, + unsigned int *samples, unsigned int k) { + unsigned int i, compressed_size; + out->size = j_blocksize * 32u; + + if (k >= n_bits - 2u) { + out->CompressionIdentifier = (uint8_t)(SAMPLE_SPLITTING_ID + k); + return; + } + if (k == 0u) { + out->CompressionIdentifier = 1u; + FundamentalSequence(out, j_blocksize, samples); + return; + } + + compressed_size = GetSizeSampleSplitting(j_blocksize, samples, k); + if (compressed_size > j_blocksize * 32u) { + out->CompressionIdentifier = (uint8_t)(SAMPLE_SPLITTING_ID + k); + return; + } + + out->CompressionIdentifier = (uint8_t)(k + 1u); + out->size = compressed_size; + out->CompressionIdentifierInternal = SAMPLE_SPLITTING_ID + k; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; +} + +static void ZeroBlock(FCompressedData_t *out, unsigned int j_blocksize, unsigned int number_of_zeros) { + unsigned int i; + out->size = number_of_zeros + 1u; + for (i = 0; i < j_blocksize; ++i) out->data[i] = 0u; + out->data[0] = 1u; + out->CompressionIdentifier = ZERO_BLOCK_ID; + out->CompressionIdentifierInternal = ZERO_BLOCK_ID; +} + +/* Phase 3 worker (PARALLEL): selects the smallest candidate for one block. + * The only shared mutable state written is *out, a distinct array slot per + * block, so distinct cores always write distinct addresses. */ +static void SelectBestForBlock(FCompressedData_t *out, unsigned int n_bits, unsigned int j_blocksize, + unsigned int *samples, int number_of_zeros) { + FCompressedData_t candidate; + unsigned int k; + + if (number_of_zeros == -1) { + NoCompression(out, n_bits, j_blocksize, samples); + + SecondExtension(&candidate, j_blocksize, samples); + KeepBest(out, &candidate); + + for (k = 0; k < n_bits; ++k) { + SampleSplitting(&candidate, n_bits, j_blocksize, samples, k); + KeepBest(out, &candidate); + } + } else { + ZeroBlock(out, j_blocksize, (unsigned int)number_of_zeros); + } +} + +/* Phase 4 worker (SEQUENTIAL, core 0): serializes one already-selected + * block into the shared OutputBitStream. */ +static void SerializeBlock(OutputBitStream_t *out_stream, unsigned int n_bits, + unsigned int j_blocksize, FCompressedData_t *best) { + unsigned int id_size; + + if (n_bits < 3u) id_size = 1u; + else if (n_bits < 5u) id_size = 2u; + else if (n_bits <= 8u) id_size = 3u; + else if (n_bits <= 16u) id_size = 4u; + else id_size = 5u; + + if (best->CompressionIdentifierInternal == ZERO_BLOCK_ID || + best->CompressionIdentifierInternal == SECOND_EXTENSION_ID) + id_size += 1u; + + writeWordChar(out_stream, best->CompressionIdentifier, (int)id_size); + + if (best->CompressionIdentifierInternal == ZERO_BLOCK_ID) + ZeroBlockWriter(out_stream, best->size); + else if (best->CompressionIdentifierInternal == NO_COMPRESSION_ID) + NoCompressionWriter(out_stream, j_blocksize, n_bits, best->data); + else if (best->CompressionIdentifierInternal == FUNDAMENTAL_SEQUENCE_ID) + FundamentalSequenceWriter(out_stream, j_blocksize, best->data); + else if (best->CompressionIdentifierInternal == SECOND_EXTENSION_ID) + SecondExtensionWriter(out_stream, j_blocksize / 2u, best->data); + else if (best->CompressionIdentifierInternal >= SAMPLE_SPLITTING_ID) + SampleSplittingWriter(out_stream, j_blocksize, + best->CompressionIdentifierInternal - SAMPLE_SPLITTING_ID, best->data); +} + +/* ============================================================ + * Shared buffers in TCDM (.heapsram), partitioned by block range per phase + * so distinct cores always write distinct indices + * ============================================================ */ +static HEAPSRAM unsigned int input_data[TOTAL_SAMPLES]; +static HEAPSRAM unsigned int preprocessed[SAMPLES_PER_STEP]; +static HEAPSRAM unsigned int all_zeros_flag[R_SAMPLES_INTERVAL]; +static HEAPSRAM int number_of_zeros[R_SAMPLES_INTERVAL]; +static HEAPSRAM FCompressedData_t best_block[R_SAMPLES_INTERVAL]; +static HEAPSRAM OutputBitStream_t output_stream; + +static HEAPSRAM unsigned int num_cores; + +/* Same deterministic synthetic stream as the sequential ports. */ +static void generate_input(void) { + unsigned int s, b, i, idx = 0; + for (s = 0; s < STEPS; ++s) + for (b = 0; b < R_SAMPLES_INTERVAL; ++b) + for (i = 0; i < J_BLOCKSIZE; ++i) { + if (s == 0u && (b == 0u || b == 1u)) input_data[idx] = 100u; + else input_data[idx] = (idx * 37u + s * 13u + 5u) % 4096u; + idx++; + } +} + +/* ============================================================ + * Cluster test harness + * ============================================================ */ +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_2_1_par", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + num_cores = get_core_num(); + + if (rt_core_id() < num_cores) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int core_id = (unsigned int)get_core_id(); + unsigned int step, block, i; + + if (core_id == 0) { + generate_input(); + for (i = 0; i < OUTPUT_BUFFER_BYTES; ++i) output_stream.OutputBitStream[i] = 0u; + output_stream.num_bits = 0u; + output_stream.num_total_bytes = 0u; + + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + } + synch_barrier(); + + start(); + + for (step = 0; step < STEPS; ++step) { + unsigned int bs, be; + + /* --- Phase 1 (PARALLEL): preprocessing --- */ + split_range(R_SAMPLES_INTERVAL, (unsigned int)get_core_id(), num_cores, &bs, &be); + for (block = bs; block < be; ++block) { + unsigned int az = 1u; + for (i = 0; i < J_BLOCKSIZE; ++i) { + const unsigned int idx = step * SAMPLES_PER_STEP + block * J_BLOCKSIZE + i; + int predicted, error, value; +#if PREPROCESSOR_ACTIVE + predicted = (idx == 0u) ? 0 : (int)input_data[idx - 1u]; + error = (int)input_data[idx] - predicted; + value = PredictorErrorMapper(predicted, error, N_BITS); +#else + value = (int)input_data[idx]; +#endif + preprocessed[block * J_BLOCKSIZE + i] = (unsigned int)value; + if (value != 0) az = 0u; + } + all_zeros_flag[block] = az; + } + synch_barrier(); + + /* --- Phase 2 (SEQUENTIAL, core 0): zero-run counting --- */ + if ((unsigned int)get_core_id() == 0) { + unsigned int run = 0u; + for (block = 0; block < R_SAMPLES_INTERVAL; ++block) { + if (all_zeros_flag[block]) { number_of_zeros[block] = (int)run; run++; } + else { number_of_zeros[block] = -1; run = 0u; } + } + } + synch_barrier(); + + /* --- Phase 3 (PARALLEL): candidate selection --- */ + split_range(R_SAMPLES_INTERVAL, (unsigned int)get_core_id(), num_cores, &bs, &be); + for (block = bs; block < be; ++block) + SelectBestForBlock(&best_block[block], N_BITS, J_BLOCKSIZE, + &preprocessed[block * J_BLOCKSIZE], number_of_zeros[block]); + synch_barrier(); + + /* --- Phase 4 (SEQUENTIAL, core 0): bitstream serialization --- */ + if ((unsigned int)get_core_id() == 0) { + for (block = 0; block < R_SAMPLES_INTERVAL; ++block) + SerializeBlock(&output_stream, N_BITS, J_BLOCKSIZE, &best_block[block]); + } + synch_barrier(); + } + + synch_barrier(); + stop(); + + if (core_id != 0) return; + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + unsigned int checksum = 0u; + for (i = 0; i < output_stream.num_total_bytes + 1u; ++i) + checksum += output_stream.OutputBitStream[i]; + + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + unsigned int out0 = (unsigned int)output_stream.OutputBitStream[0]; + + printf("OBPMark 2.1 - CCSDS 121.0 Data Compression\n"); + printf("PULP cluster parallel (%u cores, RV32) - Astral @ 50 MHz\n", num_cores); + printf("n_bits=%u j_blocksize=%u r_samplesInterval=%u steps=%u\n", + N_BITS, J_BLOCKSIZE, R_SAMPLES_INTERVAL, STEPS); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Total output bytes : %u\n", output_stream.num_total_bytes); + printf(" Checksum : %u\n", checksum); + printf(" Output[0] : %u\n", out0); + + if (out0 == EXPECTED_OUTPUT_0 && checksum == EXPECTED_CHECKSUM && + output_stream.num_total_bytes == EXPECTED_TOTAL_BYTES) { + result->errors = 0; + printf("RESULT: PASS\n"); + } else { + result->errors = 1; + printf("RESULT: FAIL (expected Output[0]=%u Checksum=%u bytes=%u)\n", + EXPECTED_OUTPUT_0, EXPECTED_CHECKSUM, EXPECTED_TOTAL_BYTES); + } +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_seq/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_seq/Makefile new file mode 100644 index 000000000..0b4320d62 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_seq/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_2_1_pulpd_seq.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_seq/obpmark_2_1_pulpd_seq.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_seq/obpmark_2_1_pulpd_seq.c new file mode 100644 index 000000000..c0f24c8d2 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_1_seq/obpmark_2_1_pulpd_seq.c @@ -0,0 +1,474 @@ +/* + * Copyright 2024 ETH Zurich and University of Bologna. + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + * + * OBPMark Benchmark #2.1 - CCSDS 121.0 Data Compression + * Sequential (single-core) port for the PULP integer cluster of Astral + * (core 0 only, RV32) + * + * Same algorithm and compile-time configuration as obpmark_2_1_host_seq.c + * (N_BITS=16, J_BLOCKSIZE=8, R_SAMPLES_INTERVAL=8, STEPS=2). Being purely + * integer arithmetic (no floating point), the result is expected to be + * bit-identical to the host-domain sequential run already validated on + * this platform: Output[0]=64, Total output bytes=130, Checksum=13073. + * + * Cluster test harness (testcase_t / run_suite / check_obpmark / + * bench_cluster_forward / cpu_perf_*), HEAPSRAM buffer placement and + * cycle-counting mechanism follow the same conventions as the parallel + * cluster port of OBPMark #1.1. Only core 0 enters run_suite(): this is a + * single-core port, not an 8-core SPMD one, so no split_range()/ + * synch_barrier() is used anywhere. + */ + +#include "pulp.h" +#include +#include + +#define HEAPSRAM __attribute__((section(".heapsram"))) + +// ============================================================ +// Compile-time configuration (identical to obpmark_2_1_host_seq.c) +// ============================================================ +#define N_BITS 16u +#define J_BLOCKSIZE 8u +#define R_SAMPLES_INTERVAL 8u +#define STEPS 2u +#define PREPROCESSOR_ACTIVE 1 + +#define SAMPLES_PER_STEP (R_SAMPLES_INTERVAL * J_BLOCKSIZE) +#define TOTAL_SAMPLES (SAMPLES_PER_STEP * STEPS) + +#define OUTPUT_BUFFER_BYTES 512u + +#define EXPECTED_OUTPUT_0 64u +#define EXPECTED_CHECKSUM 13073u +#define EXPECTED_TOTAL_BYTES 130u + +// ============================================================ +// CCSDS 121.0 internal identifiers (benchmark.h) +// ============================================================ +#define ZERO_BLOCK_ID 0u +#define FUNDAMENTAL_SEQUENCE_ID 1u +#define SECOND_EXTENSION_ID 2u +#define SAMPLE_SPLITTING_ID 3u +#define NO_COMPRESSION_ID 32u + +#define MIN_MACRO(x, y) (((x) < (y)) ? (x) : (y)) + +// ============================================================ +// Output bit stream (output_format_utils.h) +// ============================================================ +typedef struct { + uint8_t OutputBitStream[OUTPUT_BUFFER_BYTES]; + unsigned int num_bits; + unsigned int num_total_bytes; +} OutputBitStream_t; + +static void writeWord(OutputBitStream_t *status, unsigned int word, int number_bits) { + int i; + for (i = number_bits - 1; i >= 0; --i) { + status->OutputBitStream[status->num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1u) << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { status->num_bits = 0u; status->num_total_bytes++; } + } +} + +static void writeWordChar(OutputBitStream_t *status, uint8_t word, int number_bits) { + int i; + for (i = number_bits - 1; i >= 0; --i) { + status->OutputBitStream[status->num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1u) << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { status->num_bits = 0u; status->num_total_bytes++; } + } +} + +static void writeValue(OutputBitStream_t *status, uint8_t value, unsigned int number_bits) { + unsigned int i; + if (value != 0u && value != 1u) return; + value &= 0x1u; + for (i = 0; i < number_bits; ++i) { + status->OutputBitStream[status->num_total_bytes] |= (uint8_t)(value << (7 - status->num_bits)); + status->num_bits++; + if (status->num_bits >= 8u) { status->num_bits = 0u; status->num_total_bytes++; } + } +} + +static void ZeroBlockWriter(OutputBitStream_t *status, unsigned int size) { + writeValue(status, 0u, size - 1u); + writeValue(status, 1u, 1u); +} + +static void NoCompressionWriter(OutputBitStream_t *status, unsigned int j_blocksize, + unsigned int n_bits, unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) writeWord(status, data[i], (int)n_bits); +} + +static void FundamentalSequenceWriter(OutputBitStream_t *status, unsigned int j_blocksize, unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) { writeValue(status, 0u, data[i]); writeValue(status, 1u, 1u); } +} + +static void SecondExtensionWriter(OutputBitStream_t *status, unsigned int half_blocksize, unsigned int *data) { + unsigned int i; + for (i = 0; i < half_blocksize; ++i) writeWord(status, data[i], 32); +} + +static void SampleSplittingWriter(OutputBitStream_t *status, unsigned int j_blocksize, + unsigned int k, unsigned int *data) { + unsigned int i; + for (i = 0; i < j_blocksize; ++i) { writeValue(status, 0u, data[i] >> k); writeValue(status, 1u, 1u); } + for (i = 0; i < j_blocksize; ++i) writeWord(status, data[i], (int)k); +} + +// ============================================================ +// Preprocessor: unit-delay predictor + CCSDS 121.0 error mapper +// ============================================================ +static int DelayedStack = 0; // persists across blocks/steps, as in cpu/processing.c + +static int UnitDelayPredictor(int data_sample) { + const int cached = DelayedStack; + DelayedStack = data_sample; + return cached; +} + +static int PredictorErrorMapper(int predicted_value, int prediction_error, unsigned int n_bits) { + const int x_min = 0; + const int x_max = (int)((1u << n_bits) - 1u); + const int theta = MIN_MACRO(predicted_value - x_min, x_max - predicted_value); + + int preprocessed = theta + (prediction_error < 0 ? -prediction_error : prediction_error); + + if (prediction_error >= 0 && prediction_error <= theta) + preprocessed = 2 * prediction_error; + else if (prediction_error < 0 && -theta <= prediction_error) + preprocessed = (2 * (-prediction_error)) - 1; + + return preprocessed; +} + +static int Preprocessor(int x, unsigned int n_bits) { + const int predicted = UnitDelayPredictor(x); + const int error = x - predicted; + return PredictorErrorMapper(predicted, error, n_bits); +} + +// ============================================================ +// Adaptive entropy encoder (fixed-size candidates, no heap) +// ============================================================ +typedef struct { + unsigned int size; + unsigned int data[J_BLOCKSIZE]; + uint8_t CompressionIdentifier; + unsigned int CompressionIdentifierInternal; +} FCompressedData_t; + +static void CopyCandidate(FCompressedData_t *dst, const FCompressedData_t *src) { + unsigned int i; + dst->size = src->size; + dst->CompressionIdentifier = src->CompressionIdentifier; + dst->CompressionIdentifierInternal = src->CompressionIdentifierInternal; + for (i = 0; i < J_BLOCKSIZE; ++i) dst->data[i] = src->data[i]; +} + +static void KeepBest(FCompressedData_t *best, const FCompressedData_t *candidate) { + if (candidate->size < best->size) CopyCandidate(best, candidate); +} + +static void NoCompression(FCompressedData_t *out, unsigned int n_bits, + unsigned int j_blocksize, unsigned int *samples) { + unsigned int i; + out->CompressionIdentifierInternal = NO_COMPRESSION_ID; + out->size = n_bits * j_blocksize; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; + + if (n_bits < 3u) out->CompressionIdentifier = 0x1u; + else if (n_bits < 5u) out->CompressionIdentifier = 0x3u; + else if (n_bits <= 8u) out->CompressionIdentifier = 0x7u; + else if (n_bits <= 16u) out->CompressionIdentifier = 0xFu; + else out->CompressionIdentifier = 0x1Fu; +} + +static unsigned int GetSizeSecondExtension(unsigned int half_blocksize, unsigned int *halved) { + unsigned int size = 0, i; + for (i = 0; i < half_blocksize; ++i) size += halved[i] + 1u; + return size; +} + +static void SecondExtension(FCompressedData_t *out, unsigned int j_blocksize, unsigned int *samples) { + const unsigned int half_blocksize = j_blocksize / 2u; + unsigned int halved[J_BLOCKSIZE / 2u]; + unsigned int i, compressed_size; + + for (i = 0; i < half_blocksize; ++i) + halved[i] = (((samples[2u*i] + samples[2u*i+1u]) * (samples[2u*i] + samples[2u*i+1u] + 1u)) / 2u) + samples[2u*i+1u]; + + out->CompressionIdentifier = SECOND_EXTENSION_ID - 1u; + out->size = j_blocksize * 32u; + + compressed_size = GetSizeSecondExtension(half_blocksize, halved); + if (compressed_size > j_blocksize * 32u) return; + + out->size = compressed_size; + out->CompressionIdentifierInternal = SECOND_EXTENSION_ID; + for (i = 0; i < half_blocksize; ++i) out->data[i] = halved[i]; +} + +static unsigned int GetSizeFundamentalSequence(unsigned int j_blocksize, unsigned int *samples) { + unsigned int size = 0, i; + for (i = 0; i < j_blocksize; ++i) size += samples[i] + 1u; + return size; +} + +static void FundamentalSequence(FCompressedData_t *out, unsigned int j_blocksize, unsigned int *samples) { + unsigned int i, compressed_size; + out->CompressionIdentifier = FUNDAMENTAL_SEQUENCE_ID; + out->size = j_blocksize * 32u; + + compressed_size = GetSizeFundamentalSequence(j_blocksize, samples); + if (compressed_size > j_blocksize * 32u) return; + + out->size = compressed_size; + out->CompressionIdentifierInternal = FUNDAMENTAL_SEQUENCE_ID; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; +} + +static unsigned int GetSizeSampleSplitting(unsigned int j_blocksize, unsigned int *samples, unsigned int k) { + unsigned int size = 0, i; + for (i = 0; i < j_blocksize; ++i) size += (k + (samples[i] >> k) + 1u); + return size; +} + +static void SampleSplitting(FCompressedData_t *out, unsigned int n_bits, unsigned int j_blocksize, + unsigned int *samples, unsigned int k) { + unsigned int i, compressed_size; + out->size = j_blocksize * 32u; + + if (k >= n_bits - 2u) { + out->CompressionIdentifier = (uint8_t)(SAMPLE_SPLITTING_ID + k); + return; + } + if (k == 0u) { + out->CompressionIdentifier = 1u; + FundamentalSequence(out, j_blocksize, samples); + return; + } + + compressed_size = GetSizeSampleSplitting(j_blocksize, samples, k); + if (compressed_size > j_blocksize * 32u) { + out->CompressionIdentifier = (uint8_t)(SAMPLE_SPLITTING_ID + k); + return; + } + + out->CompressionIdentifier = (uint8_t)(k + 1u); + out->size = compressed_size; + out->CompressionIdentifierInternal = SAMPLE_SPLITTING_ID + k; + for (i = 0; i < j_blocksize; ++i) out->data[i] = samples[i]; +} + +static void ZeroBlock(FCompressedData_t *out, unsigned int j_blocksize, unsigned int number_of_zeros) { + unsigned int i; + out->size = number_of_zeros + 1u; + for (i = 0; i < j_blocksize; ++i) out->data[i] = 0u; + out->data[0] = 1u; + out->CompressionIdentifier = ZERO_BLOCK_ID; + out->CompressionIdentifierInternal = ZERO_BLOCK_ID; +} + +// Selects the smallest encoding for one block and serializes it directly, +// as the reference cpu/processing.c AdaptativeEntropyEncoder() does (no +// intermediate per-block storage - single core, no parallel phase split +// is needed here). +static void AdaptativeEntropyEncoder(OutputBitStream_t *out_stream, unsigned int n_bits, + unsigned int j_blocksize, unsigned int *samples, + int number_of_zeros) { + FCompressedData_t best; + FCompressedData_t candidate; + unsigned int id_size, k; + + if (number_of_zeros == -1) { + NoCompression(&best, n_bits, j_blocksize, samples); + + SecondExtension(&candidate, j_blocksize, samples); + KeepBest(&best, &candidate); + + for (k = 0; k < n_bits; ++k) { + SampleSplitting(&candidate, n_bits, j_blocksize, samples, k); + KeepBest(&best, &candidate); + } + } else { + ZeroBlock(&best, j_blocksize, (unsigned int)number_of_zeros); + } + + if (n_bits < 3u) id_size = 1u; + else if (n_bits < 5u) id_size = 2u; + else if (n_bits <= 8u) id_size = 3u; + else if (n_bits <= 16u) id_size = 4u; + else id_size = 5u; + + if (best.CompressionIdentifierInternal == ZERO_BLOCK_ID || + best.CompressionIdentifierInternal == SECOND_EXTENSION_ID) + id_size += 1u; + + writeWordChar(out_stream, best.CompressionIdentifier, (int)id_size); + + if (best.CompressionIdentifierInternal == ZERO_BLOCK_ID) + ZeroBlockWriter(out_stream, best.size); + else if (best.CompressionIdentifierInternal == NO_COMPRESSION_ID) + NoCompressionWriter(out_stream, j_blocksize, n_bits, best.data); + else if (best.CompressionIdentifierInternal == FUNDAMENTAL_SEQUENCE_ID) + FundamentalSequenceWriter(out_stream, j_blocksize, best.data); + else if (best.CompressionIdentifierInternal == SECOND_EXTENSION_ID) + SecondExtensionWriter(out_stream, j_blocksize / 2u, best.data); + else if (best.CompressionIdentifierInternal >= SAMPLE_SPLITTING_ID) + SampleSplittingWriter(out_stream, j_blocksize, + best.CompressionIdentifierInternal - SAMPLE_SPLITTING_ID, best.data); +} + +// ============================================================ +// Zero-block run bookkeeping (processing.h structs) +// ============================================================ +typedef struct { + unsigned int counter; + int position; +} ZeroBlockCounter_t; + +typedef struct { + int NumberOfZeros; +} ZeroBlockProcessed_t; + +// ============================================================ +// Static buffers in TCDM (.heapsram), to keep them out of the ELF payload +// ============================================================ +static HEAPSRAM unsigned int input_data[TOTAL_SAMPLES]; +static HEAPSRAM unsigned int preprocessed[SAMPLES_PER_STEP]; +static HEAPSRAM OutputBitStream_t output_stream; + +// Same deterministic synthetic stream as obpmark_2_1_host_seq.c. +static void generate_input(void) { + unsigned int s, b, i, idx = 0; + for (s = 0; s < STEPS; ++s) + for (b = 0; b < R_SAMPLES_INTERVAL; ++b) + for (i = 0; i < J_BLOCKSIZE; ++i) { + if (s == 0u && (b == 0u || b == 1u)) input_data[idx] = 100u; + else input_data[idx] = (idx * 37u + s * 13u + 5u) % 4096u; + idx++; + } +} + +// ============================================================ +// Cluster test harness (same pattern as the parallel OBPMark #1.1 +// cluster port; here only core 0 ever calls check_obpmark) +// ============================================================ +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_2_1_seq", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + if (rt_core_id() == 0) + run_suite(testcases); + + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int step, block, i, checksum; + + generate_input(); + for (i = 0; i < OUTPUT_BUFFER_BYTES; ++i) output_stream.OutputBitStream[i] = 0u; + output_stream.num_bits = 0u; + output_stream.num_total_bytes = 0u; + DelayedStack = 0; + + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + + start(); + + for (step = 0; step < STEPS; ++step) { + ZeroBlockCounter_t zero_counter[R_SAMPLES_INTERVAL]; + ZeroBlockProcessed_t zb_processed[R_SAMPLES_INTERVAL]; + unsigned int zero_counter_pos = 0u; + unsigned int number_of_consecutive_zero_blocks = 0u; + + for (i = 0; i < R_SAMPLES_INTERVAL; ++i) { zero_counter[i].counter = 0u; zero_counter[i].position = -1; } + for (i = 0; i < R_SAMPLES_INTERVAL; ++i) zb_processed[i].NumberOfZeros = -1; + + // --- preprocess_data --- + for (block = 0; block < R_SAMPLES_INTERVAL; ++block) { + int all_zeros_in_block = 1; + for (i = 0; i < J_BLOCKSIZE; ++i) { + const unsigned int in_idx = step * SAMPLES_PER_STEP + block * J_BLOCKSIZE + i; + unsigned int value; +#if PREPROCESSOR_ACTIVE + value = (unsigned int)Preprocessor((int)input_data[in_idx], N_BITS); +#else + value = input_data[in_idx]; +#endif + preprocessed[block * J_BLOCKSIZE + i] = value; + if (value != 0u) all_zeros_in_block = 0; + } + + if (all_zeros_in_block) { + zero_counter[zero_counter_pos].counter = number_of_consecutive_zero_blocks; + zero_counter[zero_counter_pos].position = (int)block; + zero_counter_pos++; + number_of_consecutive_zero_blocks++; + } else { + number_of_consecutive_zero_blocks = 0u; + } + } + + // --- process_zeroblock --- + for (i = 0; i < zero_counter_pos; ++i) + zb_processed[zero_counter[i].position].NumberOfZeros = (int)zero_counter[i].counter; + + // --- process_blocks --- + for (block = 0; block < R_SAMPLES_INTERVAL; ++block) { + AdaptativeEntropyEncoder(&output_stream, N_BITS, J_BLOCKSIZE, + &preprocessed[block * J_BLOCKSIZE], + zb_processed[block].NumberOfZeros); + } + } + + stop(); + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + checksum = 0u; + for (i = 0; i < output_stream.num_total_bytes + 1u; ++i) + checksum += output_stream.OutputBitStream[i]; + + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + unsigned int out0 = (unsigned int)output_stream.OutputBitStream[0]; + + printf("OBPMark 2.1 - CCSDS 121.0 Data Compression\n"); + printf("PULP cluster sequential (1 core, RV32) - Astral @ 50 MHz\n"); + printf("n_bits=%u j_blocksize=%u r_samplesInterval=%u steps=%u\n", + N_BITS, J_BLOCKSIZE, R_SAMPLES_INTERVAL, STEPS); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Total output bytes : %u\n", output_stream.num_total_bytes); + printf(" Checksum : %u\n", checksum); + printf(" Output[0] : %u\n", out0); + + if (out0 == EXPECTED_OUTPUT_0 && checksum == EXPECTED_CHECKSUM && + output_stream.num_total_bytes == EXPECTED_TOTAL_BYTES) { + result->errors = 0; + printf("RESULT: PASS\n"); + } else { + result->errors = 1; + printf("RESULT: FAIL (expected Output[0]=%u Checksum=%u bytes=%u)\n", + EXPECTED_OUTPUT_0, EXPECTED_CHECKSUM, EXPECTED_TOTAL_BYTES); + } +} \ No newline at end of file diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par/Makefile new file mode 100644 index 000000000..aef3ef7ec --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_2_2_pulpd_par.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par/obpmark_2_2_pulpd_par.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par/obpmark_2_2_pulpd_par.c new file mode 100644 index 000000000..e5ce2edb6 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par/obpmark_2_2_pulpd_par.c @@ -0,0 +1,1264 @@ +/* + * Copyright 2024 ETH Zurich and University of Bologna. + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + * + * OBPMark Benchmark #2.2 - CCSDS 122.0 Image Compression + * Parallel (8-core SPMD) port for the PULP integer cluster of Astral (RV32) + * + * Stesso algoritmo e configurazione di obpmark_2_2_pulpd_seq.c + * (w_size=h_size=32, bit_size=16, segment_size=16, percorso integer): + * risultato atteso bit-identico a tutte le varianti gia' validate + * (bit_depth_dc=17, bit_depth_ac=13, Output bytes=301, Checksum=35042, + * Output[0]=192). + * + * Decomposizione (stessa della versione SMP host obpmark_2_2_host_smp.c): + * DWT: row-pass e column-pass di ciascun livello, split per riga/colonna, + * barriera tra row-pass e column-pass e tra livelli successivi. + * Scratch della column-pass privato per core (col_in/col_out[core]). + * coefficient_scaling / coeff_regroup: core 0. + * build_block_string: split per blocco. + * dc_encoding + ac_depth_encoder: core 0 (riduzioni globali, header, + * catene DPCM, scrittura bitstream). + * per ogni bit-plane: bit_plane_encoding split per blocco (ogni blocco + * scrive solo block_data[blocco]); stages_encoding su core 0 + * (statistiche sull'intero gaggle, bitstream in ordine stretto). + * + * Vincoli ereditati dal sequenziale cluster: + * - text entro i primi 64 KiB della L2 cluster (0x78000000-0x7800FFFF): + * il codice nella regione l2_priv1 (>= 0x78010000) produce illegal + * instruction. Da qui il pragma O2 (limitato a questo file: PULP_CFLAGS + * resta -O3 per pulp-runtime/kernel/hmr_synch.c) e il noinline su + * write_to_the_output_segment. + * - footprint dati ridotto: nessun transformed_image separato, + * aux_row/regroup_tmp unificati in scratch_2d. + */ + +#include "pulp.h" +#include +#include + +#pragma GCC optimize ("O2") + +#define HEAPSRAM __attribute__((section(".heapsram"))) + +#define IMAGE_WIDTH 32u +#define IMAGE_HEIGHT 32u +#define BIT_SIZE 16u +#define LEVELS_DWT 3u +#define BLOCKSIZEIMAGE 8u +#define SEGMENT_SIZE 16u +#define GAGGLE_SIZE 16u +#define UNCODED_VALUE 0xFFu +#define BITLL3 3u + +#define MAX_SYMBOLS_IN_BLOCK 22u +#define SIZE_TYPE 4u + +#define ENUM_NONE 0 +#define ENUM_TYPE_P 1 +#define ENUM_TYPE_TRAN_B 2 +#define ENUM_TYPE_TRAN_D 3 +#define ENUM_TYPE_CI 4 +#define ENUM_TRAN_GI 5 +#define ENUM_TRAN_HI 6 +#define ENUM_TYPE_HIJ 7 + +#define BITHH1 0 +#define BITLH1 1 +#define BITHL1 1 +#define BITHH2 1 +#define BITLH2 2 +#define BITHL2 2 +#define BITHH3 2 +#define BITHL3 3 +#define BITLH3 3 + +#define ABSOLUTE(x) ((x) < 0 ? -(x) : (x)) +#define SIGN(x) ((x) < 0 ? 1 : 0) + +static const int bit2_pattern[4] = {0, 2, 1, 3}; +static const int bit3_pattern[8] = {1, 4, 0, 5, 2, 6, 3, 7}; +static const int bit3_pattern_TranD[8] = {0, 3, 0, 4, 1, 5, 2, 6}; +static const int bit4_pattern_TypeCi[16] = {10, 1, 3, 6, 2, 5, 9, 12, 0, 8, 7, 13, 4, 14, 11, 15}; +static const int bit4_pattern_TypeHij_TranHi[16] = {0, 1, 3, 6, 2, 5, 9, 11, 0, 8, 7, 12, 4, 13, 10, 14}; + +static const int type_of_compression = 0; // 0 = integer path + +#define W_PADDED IMAGE_WIDTH +#define H_PADDED IMAGE_HEIGHT +#define TOTAL_BLOCKS ((W_PADDED / BLOCKSIZEIMAGE) * (H_PADDED / BLOCKSIZEIMAGE)) // 16 + +#define OUTPUT_BUFFER_BYTES 512u +#define MAX_CORES 8u + +#define EXPECTED_OUTPUT_0 192u +#define EXPECTED_CHECKSUM 35042u +#define EXPECTED_TOTAL_BYTES 301u +#define EXPECTED_BIT_DEPTH_DC 17u +#define EXPECTED_BIT_DEPTH_AC 13u + +// ============================================================ +// Stato condiviso tra core +// ============================================================ +static unsigned int g_num_cores = 1u; +static HEAPSRAM unsigned int g_qf; + +static HEAPSRAM int image_data[H_PADDED][W_PADDED]; +static HEAPSRAM int block_string[TOTAL_BLOCKS][BLOCKSIZEIMAGE * BLOCKSIZEIMAGE]; +static HEAPSRAM int scratch_2d[H_PADDED][W_PADDED]; // aux_row (DWT) e regroup_tmp (coeff_regroup), fasi non sovrapposte +static HEAPSRAM int col_in[MAX_CORES][H_PADDED]; // scratch column-pass, privato per core +static HEAPSRAM int col_out[MAX_CORES][H_PADDED]; + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +static inline int floor_div(int a, int b) { + int q = a / b; + int r = a % b; + if (r != 0 && ((r < 0) != (b < 0))) q--; + return q; +} + +static void ccsds_wavelet_transform_1D_int(const int *A, int *B, int size) { + int full = 2 * size; + int i; + for (i = 0; i < size; ++i) { + int s, val; + if (i == 0) { + s = 9 * (A[0] + A[2]) - (A[2] + A[4]); + val = A[1] - floor_div(s + 8, 16); + } else if (i == size - 2) { + s = 9 * (A[full-4] + A[full-2]) - (A[full-6] + A[full-2]); + val = A[full-3] - floor_div(s + 8, 16); + } else if (i == size - 1) { + s = 9 * A[full-2] - A[full-4]; + val = A[full-1] - floor_div(s + 4, 8); + } else { + s = 9 * (A[2*i] + A[2*i+2]) - (A[2*i-2] + A[2*i+4]); + val = A[2*i+1] - floor_div(s + 8, 16); + } + B[size + i] = val; + } + for (i = 0; i < size; ++i) { + int val; + if (i == 0) + val = A[0] - floor_div(1 - B[size], 2); + else + val = A[2*i] - floor_div(2 - (B[i+size-1] + B[i+size]), 4); + B[i] = val; + } +} + +// Row-pass (parallela): ogni core scrive solo le proprie righe di scratch_2d +static void dwt_row_pass_worker(int w, unsigned int rstart, unsigned int rend) { + unsigned int i; + for (i = rstart; i < rend; ++i) + ccsds_wavelet_transform_1D_int(image_data[i], scratch_2d[i], w / 2); +} + +// Column-pass (parallela): scratch privato col_in/col_out[core] +static void dwt_col_pass_worker(int h, unsigned int cstart, unsigned int cend, unsigned int core) { + unsigned int i, x; + for (i = cstart; i < cend; ++i) { + for (x = 0; x < (unsigned int)h; ++x) col_in[core][x] = scratch_2d[x][i]; + ccsds_wavelet_transform_1D_int(col_in[core], col_out[core], h / 2); + for (x = 0; x < (unsigned int)h; ++x) image_data[x][i] = col_out[core][x]; + } +} + +static void coefficient_scaling(void) { + const int hh_1 = 1, hl_lh = 2, hh_2 = 2, hl_lh_2 = 4, hh_3 = 4, hl_lh_3 = 8, ll_4 = 8; + int i, j; + for (i = H_PADDED>>1; i < H_PADDED; i++) + for (j = W_PADDED>>1; j < W_PADDED; j++) image_data[i][j] *= hh_1; + for (i = 0; i < H_PADDED>>1; i++) + for (j = W_PADDED>>1; j < W_PADDED; j++) image_data[i][j] *= hl_lh; + for (i = H_PADDED>>1; i < H_PADDED; i++) + for (j = 0; j < W_PADDED>>1; j++) image_data[i][j] *= hl_lh; + for (i = H_PADDED>>2; i < H_PADDED>>1; i++) + for (j = W_PADDED>>2; j < W_PADDED>>1; j++) image_data[i][j] *= hh_2; + for (i = 0; i < H_PADDED>>2; i++) + for (j = W_PADDED>>2; j < W_PADDED>>1; j++) image_data[i][j] *= hl_lh_2; + for (i = H_PADDED>>2; i < H_PADDED>>1; i++) + for (j = 0; j < W_PADDED>>2; j++) image_data[i][j] *= hl_lh_2; + for (i = H_PADDED>>3; i < H_PADDED>>2; i++) + for (j = W_PADDED>>3; j < W_PADDED>>2; j++) image_data[i][j] *= hh_3; + for (i = 0; i < H_PADDED>>3; i++) + for (j = W_PADDED>>3; j < W_PADDED>>2; j++) image_data[i][j] *= hl_lh_3; + for (i = H_PADDED>>3; i < H_PADDED>>2; i++) + for (j = 0; j < W_PADDED>>3; j++) image_data[i][j] *= hl_lh_3; + for (i = 0; i < H_PADDED>>3; i++) + for (j = 0; j < W_PADDED>>3; j++) image_data[i][j] *= ll_4; +} + +static void coeff_regroup(void) { + int i, j, p, k, x, y, xb; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) scratch_2d[i][j] = 0; + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 4; p < 8; ++p) + for (k = 4; k < 8; ++k) + scratch_2d[x+p][y+k] = image_data[i+p-4][j+k-4]; + } + for (i = 0; i < H_PADDED>>1; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = i << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 0; p < 4; ++p) + for (k = 4; k < 8; ++k) + scratch_2d[x+p][y+k] = image_data[i+p][j+k-4]; + } + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = 0; j < W_PADDED>>1; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = j << 1; + for (p = 4; p < 8; ++p) + for (k = 0; k < 4; ++k) + scratch_2d[x+p][y+k] = image_data[i+p-4][j+k]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = (j - (W_PADDED>>2)) << 2; + scratch_2d[x+2][y+2] = image_data[i][j]; + scratch_2d[x+2][y+3] = image_data[i][j+1]; + scratch_2d[x+3][y+2] = image_data[i+1][j]; + scratch_2d[x+3][y+3] = image_data[i+1][j+1]; + } + for (i = 0; i < H_PADDED>>2; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = i << 2; y = (j - (W_PADDED>>2)) << 2; + scratch_2d[x][y+2] = image_data[i][j]; + scratch_2d[x][y+3] = image_data[i][j+1]; + scratch_2d[x+1][y+2] = image_data[i+1][j]; + scratch_2d[x+1][y+3] = image_data[i+1][j+1]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = 0; j < W_PADDED>>2; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = j << 2; + scratch_2d[x+2][y] = image_data[i][j]; + scratch_2d[x+2][y+1] = image_data[i][j+1]; + scratch_2d[x+3][y] = image_data[i+1][j]; + scratch_2d[x+3][y+1] = image_data[i+1][j+1]; + } + xb = H_PADDED>>3; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + scratch_2d[((i-xb)<<3)+1][((j-(W_PADDED>>3))<<3)+1] = image_data[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + scratch_2d[i<<3][((j-(W_PADDED>>3))<<3)+1] = image_data[i][j]; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + scratch_2d[((i-xb)<<3)+1][j<<3] = image_data[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + scratch_2d[i<<3][j<<3] = image_data[i][j]; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + image_data[i][j] = scratch_2d[i][j]; +} + +// build_block_string (parallela, split per blocco) +static void build_block_string_worker(unsigned int bstart, unsigned int bend) { + unsigned int block_w = W_PADDED / BLOCKSIZEIMAGE; + unsigned int bidx, x, y; + for (bidx = bstart; bidx < bend; ++bidx) { + unsigned int i = bidx / block_w; + unsigned int j = bidx % block_w; + for (x = 0; x < BLOCKSIZEIMAGE; ++x) + for (y = 0; y < BLOCKSIZEIMAGE; ++y) + block_string[bidx][x*BLOCKSIZEIMAGE + y] = + image_data[i*BLOCKSIZEIMAGE + x][j*BLOCKSIZEIMAGE + y]; + } +} + +static void generate_input(void) { + unsigned int i, j; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + image_data[i][j] = (int)((i * 173u + j * 91u + 13u) & 0xFFFFu); +} + +// ============================================================ +// Bitstream di output (scritto solo da core 0) +// ============================================================ +typedef struct { + uint8_t OutputBitStream[OUTPUT_BUFFER_BYTES]; + unsigned int num_bits; + unsigned int num_total_bytes; +} OutputBitStream_t; + +static HEAPSRAM OutputBitStream_t output_stream; + +static void __attribute__((noinline)) write_to_the_output_segment(long word, unsigned int length) { + int i; + for (i = (int)length - 1; i >= 0; --i) { + output_stream.OutputBitStream[output_stream.num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1) << (7 - output_stream.num_bits)); + output_stream.num_bits++; + if (output_stream.num_bits >= 8u) { + output_stream.num_bits = 0u; + output_stream.num_total_bytes++; + } + } +} + +static unsigned int ceil_log2(unsigned long x) { + unsigned int r = 0; + unsigned long v = 1; + while (v < x) { v <<= 1; r++; } + return r; +} + +static unsigned int bit_size_with_pow2_adjust(long v) { + unsigned long av = (unsigned long)(v < 0 ? -v : v); + if (av == 0) return 0; + unsigned int b = ceil_log2(av); + if ((av & (av - 1)) == 0) b += 1; + return b; +} + +static unsigned long conv_to_twos_complement(int value, unsigned int num_bits) { + unsigned long temp, complement; + short i; + if (num_bits == 1) return 0; + if (value >= 0) return (unsigned long) value; + complement = ~(unsigned long)(-value); + temp = 0; + for (i = 0; i < (short)num_bits; i++) { temp <<= 1; temp++; } + complement &= temp; + complement++; + return complement; +} + +typedef struct { + unsigned int symbol_val; + unsigned int symbol_len; + unsigned int symbol_mapped_pattern; + unsigned int sign; + unsigned int type; +} str_symbol_details_t; + +typedef struct { + long shifted_dc; + unsigned int dc_reminder; + unsigned long mapped_dc; + long max_ac_bit_size; + unsigned long mapped_ac; + unsigned int type_p; + unsigned int tran_b; + unsigned int tran_d; + unsigned int tran_gi; + unsigned int parent_ref_symbol; + unsigned int parent_sym_len; + unsigned int children_ref_symbol; + unsigned int children_sym_len; + unsigned int type_ci[3]; + unsigned int tran_hi[3]; + unsigned int grand_children_ref_symbol[3]; + unsigned int grand_children_sym_len[3]; + unsigned int type_hi[12]; + str_symbol_details_t symbol_block[MAX_SYMBOLS_IN_BLOCK]; +} block_data_t; + +typedef struct { + int start_img_flag; + int end_img_flag; + unsigned int segment_count; + unsigned int bit_depth_dc; + unsigned int bit_depth_ac; + int part_2_flag; + int part_3_flag; + int part_4_flag; + unsigned int pad_rows; +} header_data_t; + +static HEAPSRAM block_data_t block_data[SEGMENT_SIZE]; +static HEAPSRAM header_data_t header_data; + +static void header_output(void) { + write_to_the_output_segment(header_data.start_img_flag, 1); + write_to_the_output_segment(header_data.end_img_flag, 1); + write_to_the_output_segment(header_data.segment_count, 8); + write_to_the_output_segment(header_data.bit_depth_dc, 5); + write_to_the_output_segment(header_data.bit_depth_ac, 5); + write_to_the_output_segment(0, 1); + write_to_the_output_segment(header_data.part_2_flag, 1); + write_to_the_output_segment(header_data.part_3_flag, 1); + write_to_the_output_segment(header_data.part_4_flag, 1); + if (header_data.end_img_flag) { + write_to_the_output_segment(header_data.pad_rows, 3); + write_to_the_output_segment(0, 5); + } +} + +static void coding_quantized_coefficients(unsigned int N) { + long x_min = -(1L << (N - 1)); + long x_max = (1L << (N - 1)) - 1; + unsigned int number_bits = 0, i; + long theta, gamma; + for (i = 0; i < N - 1; i++) number_bits = (number_bits << 1) + 1; + if ((block_data[0].shifted_dc & (1L << (N - 1))) > 0) + block_data[0].shifted_dc = -(long)(((block_data[0].shifted_dc ^ number_bits) & number_bits) + 1); + block_data[0].mapped_dc = (unsigned long) block_data[0].shifted_dc; + for (i = 1; i < SEGMENT_SIZE; i++) { + if ((block_data[i].shifted_dc & (1L << (N - 1))) > 0) + block_data[i].shifted_dc = -(long)(((block_data[i].shifted_dc ^ number_bits) & number_bits) + 1); + theta = (block_data[i-1].shifted_dc - x_min) < (x_max - block_data[i-1].shifted_dc) + ? (block_data[i-1].shifted_dc - x_min) : (x_max - block_data[i-1].shifted_dc); + gamma = block_data[i].shifted_dc - block_data[i-1].shifted_dc; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_dc = (unsigned long)(2 * gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_dc = (unsigned long)(2 * (-gamma) - 1); + else + block_data[i].mapped_dc = (unsigned long)(theta + (gamma < 0 ? -gamma : gamma)); + } +} + +static void dc_encoder(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + unsigned int total_bits, min_bits = 0xFFFFu; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position > 1 ? start_position : 1); i < end_position; i++) + total_bits += (unsigned int)((block_data[i].mapped_dc >> k) + 1) + k; + if (total_bits < min_bits && total_bits < N * GAGGLE_SIZE) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_dc, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_dc >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position > 1 ? start_position : 1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_dc, encode_selection); + } +} + +static void dc_entropy_coding(unsigned int N, unsigned int quantization_factor) { + unsigned int id_length = 0, max_k = 0, i; + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 8) { max_k = 6; id_length = 3; } + else { max_k = 8; id_length = 4; } + dc_encoder(max_k, id_length, N); + if (header_data.bit_depth_ac < quantization_factor) { + unsigned int num_additional_bit_planes = quantization_factor - header_data.bit_depth_ac; + for (i = 0; i < num_additional_bit_planes; i++) { + unsigned int kk; + for (kk = 0; kk < SEGMENT_SIZE; kk++) + write_to_the_output_segment( + (block_data[kk].dc_reminder >> (quantization_factor - i - 1)), 1); + } + } +} + +static unsigned int dc_encoding(void) { + long max_ac_value = 0; + unsigned int quantization_factor; + unsigned int i, j, k_mask; + header_data.bit_depth_dc = 0; + for (i = 0; i < SEGMENT_SIZE; ++i) { + long max_ac_value_block = 0; + int dc_value = block_string[i][0]; + unsigned int dc_bit_size; + if (dc_value < 0) + dc_bit_size = ceil_log2((unsigned long)(-dc_value) + 1UL); + else + dc_bit_size = ceil_log2((unsigned long)(1 + dc_value)) + 1; + if (dc_bit_size > header_data.bit_depth_dc) header_data.bit_depth_dc = dc_bit_size; + for (j = 1; j < BLOCKSIZEIMAGE * BLOCKSIZEIMAGE; ++j) { + long v = block_string[i][j]; + long av = v < 0 ? -v : v; + if (av > (max_ac_value < 0 ? -max_ac_value : max_ac_value)) max_ac_value = v; + if (av > (max_ac_value_block < 0 ? -max_ac_value_block : max_ac_value_block)) max_ac_value_block = v; + } + block_data[i].max_ac_bit_size = (long) bit_size_with_pow2_adjust(max_ac_value_block); + } + header_data.bit_depth_ac = bit_size_with_pow2_adjust(max_ac_value); + header_output(); + if (header_data.bit_depth_dc <= 3) + quantization_factor = 0; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) <= 1) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 3; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) > 10) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 10; + else + quantization_factor = 1 + (header_data.bit_depth_ac >> 1); + k_mask = (1u << quantization_factor) - 1u; + for (i = 0; i < SEGMENT_SIZE; ++i) { + unsigned long new_value_twos = conv_to_twos_complement(block_string[i][0], header_data.bit_depth_dc); + block_data[i].shifted_dc = (long)(new_value_twos >> quantization_factor); + block_data[i].dc_reminder = (unsigned int)(new_value_twos & k_mask); + } + { + unsigned int N = header_data.bit_depth_dc > quantization_factor + ? header_data.bit_depth_dc - quantization_factor : 1; + if (N == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].shifted_dc, 1); + } else { + coding_quantized_coefficients(N); + dc_entropy_coding(N, quantization_factor); + } + } + return quantization_factor; +} + +static void dpcm_ac_mapper(unsigned int N) { + long x_min = 0; + long x_max = (1L << N) - 1; + long theta, gamma; + unsigned int i; + block_data[0].mapped_ac = (unsigned long) block_data[0].max_ac_bit_size; + for (i = 1; i < SEGMENT_SIZE; i++) { + theta = (block_data[i-1].max_ac_bit_size - x_min) < (x_max - block_data[i-1].max_ac_bit_size) + ? (block_data[i-1].max_ac_bit_size - x_min) : (x_max - block_data[i-1].max_ac_bit_size); + gamma = block_data[i].max_ac_bit_size - block_data[i-1].max_ac_bit_size; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_ac = (unsigned long)(2*gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_ac = (unsigned long)(2*(-gamma) - 1); + else + block_data[i].mapped_ac = (unsigned long)(theta + (gamma<0?-gamma:gamma)); + } +} + +static void ac_gaggle_encode(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + long total_bits, min_bits = 0xFFFF; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position>1?start_position:1); i < end_position; i++) + total_bits += (long)((block_data[i].mapped_ac >> k) + 1) + k; + if (total_bits < min_bits && total_bits < (long)(N * GAGGLE_SIZE)) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_ac, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_ac >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position>1?start_position:1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_ac, encode_selection); + } +} + +static void ac_depth_encoder(void) { + unsigned int N = 0, id_length, max_k; + while ((header_data.bit_depth_ac >> N) > 0) N++; + dpcm_ac_mapper(N); + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 5) { max_k = 6; id_length = 3; } + else { max_k = 0; id_length = 0; } + ac_gaggle_encode(max_k, id_length, N); +} + +// bit_plane_encoding (parallela, split per blocco [bstart,bend)): +// ogni blocco scrive solo block_data[blocco], block_string e' in sola lettura +static void bit_plane_encoding_worker(unsigned int bit_plane_number, unsigned int bstart, unsigned int bend) { + unsigned int temp_x, temp_y, block_num; + unsigned char symbol; + int temp_value; + unsigned int final_position; + int bit_set_plane = (1 << (bit_plane_number - 1)); + + for (block_num = bstart; block_num < bend; ++block_num) { + symbol = 0; + final_position = block_num; + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + + for (int j = 0; j < 3; ++j) { + switch (j) { + case 0: temp_y = 1; break; + case 1: temp_y = BLOCKSIZEIMAGE; break; + case 2: temp_y = BLOCKSIZEIMAGE + 1; break; + default: temp_y = 0; break; + } + temp_x = final_position; + if (!type_of_compression) { + if (((j==0)&&(BITHL3>=bit_plane_number)) || ((j==1)&&(BITLH3>=bit_plane_number)) || ((j==2)&&(BITHH3>=bit_plane_number))) + continue; + } + if ((block_data[block_num].type_p & (1 << (2-j))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_P; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + if (ABSOLUTE(block_string[temp_x][temp_y]) >= (1 << (bit_plane_number-1)) && + ABSOLUTE(block_string[temp_x][temp_y]) < (1 << bit_plane_number)) { + block_data[block_num].type_p += (1 << (2-j)); + block_data[block_num].symbol_block[symbol].symbol_val += 1; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[temp_x][temp_y]); + } + } else { + temp_value = (ABSOLUTE(block_string[temp_x][temp_y]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((j==0)&&(BITHL3=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = 1; break; + } + if (break_flag) break; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = 1; break; + } + if (break_flag) break; + } + } + + if (block_data[block_num].tran_b == 0) { + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 0; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) ++symbol; + + if (block_data[block_num].tran_b == 1) { + for (int k = 0; k < 3; k++) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)&&(BITHL1>=bit_plane_number)) + || ((k==1)&&(BITLH2>=bit_plane_number)&&(BITLH1>=bit_plane_number)) + || ((k==2)&&(BITHH2>=bit_plane_number)&&(BITHH1>=bit_plane_number))) + continue; + } + int break_flag = 0; + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + int jump_flag = 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + jump_flag = 1; + } + if (!jump_flag) { + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = 1; break; + } + } + if (!break_flag) { + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = 1; break; + } + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + } + + for (unsigned int k = 0; k < 3; ++k) { + if ((block_data[block_num].tran_d & (1 << (2-k))) != 0) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + int p = 1; + for (unsigned int i = 0; i < 4; ++i) + if ((block_data[block_num].type_ci[k] & (1 << (3-i))) == 0) { p = 0; break; } + if (p == 0) { + unsigned int counter = 0; + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_CI; + for (unsigned int i = temp_x; i < temp_x+2; ++i) { + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) { + if ((block_data[block_num].type_ci[k] & (1 << (3-counter))) == 0) { + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].type_ci[k] += (1 << (3-counter)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (((block_data[block_num].tran_d & (1 << (2-k))) != 0) && ((block_data[block_num].tran_gi & (1 << (2-k))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_GI; + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + int break_flag = 0; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].tran_gi += (1 << (2-k)); + break_flag = 1; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if (((block_data[block_num].tran_gi & (1 << (2-i3))) != 0) && ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_HI; + int break_flag = 0; + for (unsigned int k = temp_x; k < temp_x+2 && !break_flag; k++) + for (unsigned int p = temp_y; p < temp_y+2; p++) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].tran_hi[i3] += (1 << (3-jx)); + break_flag = 1; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) != 0) { + unsigned int counter = 0; + for (unsigned int k = 0; k < 4; ++k) + if ((block_data[block_num].type_hi[i3*SIZE_TYPE+jx] & (1 << (3-k))) == 0) counter++; + if (counter == 0) { + for (unsigned int k = temp_x; k < temp_x+2; k++) { + for (unsigned int p = temp_y; p < temp_y+2; p++) { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1 0) { + block_data[block_num].type_hi[i3*SIZE_TYPE+jx] += (1 << (3-t)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1symbol_len) { + case 0: return; + case 1: s->symbol_mapped_pattern = s->symbol_val; break; + case 2: s->symbol_mapped_pattern = bit2_pattern[s->symbol_val]; break; + case 3: + s->symbol_mapped_pattern = (s->type == ENUM_TYPE_TRAN_D) ? bit3_pattern_TranD[s->symbol_val] : bit3_pattern[s->symbol_val]; + break; + case 4: + if (s->type == ENUM_TYPE_CI) s->symbol_mapped_pattern = bit4_pattern_TypeCi[s->symbol_val]; + else if (s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ) s->symbol_mapped_pattern = bit4_pattern_TypeHij_TranHi[s->symbol_val]; + break; + default: break; + } +} + +static void coding_options(unsigned int bit_plane_number, unsigned char *code_option_gaggle) { + unsigned int bit_counter_2bits[2] = {0}; + unsigned int bit_counter_3bits[3] = {0}; + unsigned int bit_counter_4bits[4] = {0}; + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + if (s->type == ENUM_NONE) continue; + if (s->symbol_len == 1) { pattern_mapping(s); continue; } + pattern_mapping(s); + if (s->symbol_len == 2) { + switch (s->symbol_mapped_pattern) { + case 0: bit_counter_2bits[0] += 1; break; + case 1: bit_counter_2bits[0] += 2; break; + default: bit_counter_2bits[0] += 3; break; + } + bit_counter_2bits[1] += 2; + } else if (s->symbol_len == 3) { + if (s->symbol_mapped_pattern <= 2) bit_counter_3bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 5) bit_counter_3bits[0] += 5; + else bit_counter_3bits[0] += 6; + if (s->symbol_mapped_pattern <= 1) bit_counter_3bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_3bits[1] += 3; + else bit_counter_3bits[1] += 4; + bit_counter_3bits[2] += 3; + } else if (s->symbol_len == 4) { + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[0] += 7; + else bit_counter_4bits[0] += 8; + if (s->symbol_mapped_pattern <= 1) bit_counter_4bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[1] += 3; + else if (s->symbol_mapped_pattern <= 5) bit_counter_4bits[1] += 4; + else if (s->symbol_mapped_pattern <= 11) bit_counter_4bits[1] += 6; + else bit_counter_4bits[1] += 7; + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[2] += 3; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[2] += 4; + else bit_counter_4bits[2] += 5; + bit_counter_4bits[3] += 4; + } + } + } + code_option_gaggle[0] = (bit_counter_2bits[0] < bit_counter_2bits[1]) ? 0 : 1; + if (bit_counter_3bits[2] <= bit_counter_3bits[0] && bit_counter_3bits[2] <= bit_counter_3bits[1]) code_option_gaggle[1] = 3; + else if (bit_counter_3bits[0] <= bit_counter_3bits[1]) code_option_gaggle[1] = 0; + else code_option_gaggle[1] = 1; + if (bit_counter_4bits[3] <= bit_counter_4bits[1] && bit_counter_4bits[3] <= bit_counter_4bits[0] && bit_counter_4bits[3] <= bit_counter_4bits[2]) code_option_gaggle[2] = 3; + else if (bit_counter_4bits[0] <= bit_counter_4bits[1] && bit_counter_4bits[0] <= bit_counter_4bits[2]) code_option_gaggle[2] = 0; + else if (bit_counter_4bits[1] <= bit_counter_4bits[2]) code_option_gaggle[2] = 1; + else code_option_gaggle[2] = 2; +} + +static void rice_coding(long input_value, unsigned int bit_length, unsigned char *code_option) { + switch (bit_length) { + case 0: return; + case 1: write_to_the_output_segment(input_value, 1); break; + case 2: + if (code_option[0] == 1) write_to_the_output_segment(input_value, 2); + else { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1, 1); } + else write_to_the_output_segment(0, 3); + } + break; + case 3: + if (code_option[1] == 0) { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 5) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-3,2); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,3); } + } else if (code_option[1] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else { + write_to_the_output_segment(0,2); + switch (input_value) { + case 4: write_to_the_output_segment(2,2); break; + case 5: write_to_the_output_segment(3,2); break; + case 6: write_to_the_output_segment(0,2); break; + case 7: write_to_the_output_segment(1,2); break; + } + } + } else write_to_the_output_segment(input_value, 3); + break; + case 4: + if (code_option[2] == 0) { + if (input_value <= 3) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 7) { write_to_the_output_segment(0,5); write_to_the_output_segment(input_value-4,2); } + else { write_to_the_output_segment(0,4); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else if (input_value <= 5) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-2,2); } + else if (input_value <= 11) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-6,3); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 2) { + if (input_value <= 3) write_to_the_output_segment(input_value+4, 3); + else if (input_value <= 7) write_to_the_output_segment(input_value, 4); + else if (input_value <= 11) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-4,3); } + else write_to_the_output_segment(input_value-12, 5); + } else write_to_the_output_segment(input_value, 4); + break; + } +} + +static void gaggle_pass(unsigned int bit_plane_number, unsigned char *code_option_gaggle, int *hit_flag, int pass) { + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + int match = (pass == 1 && s->type == ENUM_TYPE_P) + || (pass == 2 && (s->type == ENUM_TYPE_TRAN_B || s->type == ENUM_TYPE_TRAN_D || s->type == ENUM_TYPE_CI)) + || (pass == 3 && (s->type == ENUM_TRAN_GI || s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ)); + if (!match) continue; + if (s->symbol_len > 1 && !hit_flag[s->symbol_len - 2]) { + hit_flag[s->symbol_len - 2] = 1; + if (s->symbol_len == 2) write_to_the_output_segment(code_option_gaggle[0], 1); + else if (s->symbol_len == 3) write_to_the_output_segment(code_option_gaggle[1], 2); + else if (s->symbol_len == 4) write_to_the_output_segment(code_option_gaggle[2], 2); + } + rice_coding(s->symbol_mapped_pattern, s->symbol_len, code_option_gaggle); + if (s->type == ENUM_TYPE_CI || s->type == ENUM_TYPE_HIJ) { + unsigned int counter = 0, i; + for (i = 0; i < s->symbol_len; ++i) if ((s->symbol_val & (1 << i)) > 0) counter++; + write_to_the_output_segment(s->sign, counter); + } + s->symbol_val = 0; s->symbol_len = 0; s->symbol_mapped_pattern = 0; s->sign = 0; s->type = 0; + } + } +} + +static void ref_bit_end_encode(void) { + unsigned int block_num, i; + for (block_num = 0; block_num < SEGMENT_SIZE; ++block_num) { + if (block_data[block_num].parent_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].parent_ref_symbol, block_data[block_num].parent_sym_len); + block_data[block_num].parent_ref_symbol = 0; block_data[block_num].parent_sym_len = 0; + } + if (block_data[block_num].children_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].children_ref_symbol, block_data[block_num].children_sym_len); + block_data[block_num].children_ref_symbol = 0; block_data[block_num].children_sym_len = 0; + } + for (i = 0; i < 3; ++i) { + if (block_data[block_num].grand_children_sym_len[i] > 0) + write_to_the_output_segment(block_data[block_num].grand_children_ref_symbol[i], block_data[block_num].grand_children_sym_len[i]); + block_data[block_num].grand_children_sym_len[i] = 0; + block_data[block_num].grand_children_ref_symbol[i] = 0; + } + } +} + +static void stages_encoding(unsigned int bit_plane_number) { + unsigned char code_option_gaggle[3] = {0,0,0}; + int hit_flag[3] = {0,0,0}; + coding_options(bit_plane_number, code_option_gaggle); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 1); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 2); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 3); + ref_bit_end_encode(); +} + +// ============================================================ +// Cluster test harness (stesso schema di obpmark_2_1_pulpd_par.c) +// ============================================================ +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_2_2_par", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + g_num_cores = get_core_num(); + + if (rt_core_id() < g_num_cores) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int core_id = (unsigned int)get_core_id(); + unsigned int n = g_num_cores; + unsigned int i; + + if (core_id == 0) { + generate_input(); + for (i = 0; i < OUTPUT_BUFFER_BYTES; ++i) output_stream.OutputBitStream[i] = 0u; + output_stream.num_bits = 0u; + output_stream.num_total_bytes = 0u; + + header_data.start_img_flag = 1; + header_data.end_img_flag = 1; + header_data.segment_count = 0; + header_data.part_2_flag = 0; + header_data.part_3_flag = 0; + header_data.part_4_flag = 0; + header_data.pad_rows = 0; + + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + } + synch_barrier(); + + start(); + + // ---- DWT a 3 livelli (parallela) ---- + { + int level_size = W_PADDED; + unsigned int lvl, s, e; + for (lvl = 0; lvl < LEVELS_DWT; ++lvl) { + split_range((unsigned int)level_size, core_id, n, &s, &e); + dwt_row_pass_worker(level_size, s, e); + synch_barrier(); + split_range((unsigned int)level_size, core_id, n, &s, &e); + dwt_col_pass_worker(level_size, s, e, core_id); + synch_barrier(); + level_size /= 2; + } + } + + // ---- scaling + regroup (core 0) ---- + if (core_id == 0) { + coefficient_scaling(); + coeff_regroup(); + } + synch_barrier(); + + // ---- build_block_string (parallela) ---- + { + unsigned int bs, be; + split_range(TOTAL_BLOCKS, core_id, n, &bs, &be); + build_block_string_worker(bs, be); + } + synch_barrier(); + + // ---- DC encoding + AC depth encoding (core 0) ---- + if (core_id == 0) { + g_qf = dc_encoding(); + if (header_data.bit_depth_ac == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].max_ac_bit_size, 1); + } else if (header_data.bit_depth_ac > 1) { + ac_depth_encoder(); + } + } + synch_barrier(); + + // ---- AC bit-plane encoding ---- + { + unsigned int bit_depth_ac = header_data.bit_depth_ac; + unsigned int qf = g_qf; + unsigned int bit_plane, bs, be; + split_range(SEGMENT_SIZE, core_id, n, &bs, &be); + + for (bit_plane = bit_depth_ac; bit_plane > 0; bit_plane--) { + // bit residui del DC: solo core 0, prima della sua quota di + // bit_plane_encoding (non tocca i campi scritti dagli altri core) + if (core_id == 0 && + (bit_plane <= qf) && + (type_of_compression || (qf > BITLL3 && BITLL3 < bit_plane))) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment((block_data[i].dc_reminder >> (bit_plane - 1)) & 0x01, 1); + } + + bit_plane_encoding_worker(bit_plane, bs, be); + synch_barrier(); + + if (core_id == 0) stages_encoding(bit_plane); + synch_barrier(); + } + } + + stop(); + + if (core_id != 0) return; + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + unsigned int checksum = 0u; + for (i = 0; i < output_stream.num_total_bytes + 1u; ++i) + checksum += output_stream.OutputBitStream[i]; + + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + unsigned int out0 = (unsigned int)output_stream.OutputBitStream[0]; + + printf("OBPMark 2.2 - CCSDS 122.0 Image Compression\n"); + printf("PULP cluster parallel (%u cores, RV32) - Astral @ 50 MHz\n", n); + printf("w_size=%u h_size=%u bit_size=%u segment_size=%u\n", + IMAGE_WIDTH, IMAGE_HEIGHT, BIT_SIZE, SEGMENT_SIZE); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" bit_depth_dc : %u\n", header_data.bit_depth_dc); + printf(" bit_depth_ac : %u\n", header_data.bit_depth_ac); + printf(" Output bytes : %u\n", output_stream.num_total_bytes); + printf(" Checksum : %u\n", checksum); + printf(" Output[0] : %u\n", out0); + + if (n <= MAX_CORES && + out0 == EXPECTED_OUTPUT_0 && checksum == EXPECTED_CHECKSUM && + output_stream.num_total_bytes == EXPECTED_TOTAL_BYTES && + header_data.bit_depth_dc == EXPECTED_BIT_DEPTH_DC && + header_data.bit_depth_ac == EXPECTED_BIT_DEPTH_AC) { + result->errors = 0; + printf("RESULT: PASS\n"); + } else { + result->errors = 1; + printf("RESULT: FAIL (expected Output[0]=%u Checksum=%u bytes=%u dc=%u ac=%u, cores<=%u)\n", + EXPECTED_OUTPUT_0, EXPECTED_CHECKSUM, EXPECTED_TOTAL_BYTES, + EXPECTED_BIT_DEPTH_DC, EXPECTED_BIT_DEPTH_AC, MAX_CORES); + } +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par_float/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par_float/Makefile new file mode 100644 index 000000000..e19535903 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par_float/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_2_2_pulpd_par_float.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par_float/obpmark_2_2_pulpd_par_float.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par_float/obpmark_2_2_pulpd_par_float.c new file mode 100644 index 000000000..c82463063 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_par_float/obpmark_2_2_pulpd_par_float.c @@ -0,0 +1,1300 @@ +/* + * Copyright 2024 ETH Zurich and University of Bologna. + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + * + * OBPMark Benchmark #2.2 - CCSDS 122.0 Image Compression - percorso FLOAT + * Parallel (8-core SPMD) port for the PULP integer cluster of Astral (RV32) + * + * Stesso algoritmo di obpmark_2_2_pulpd_seq_float.c; valori attesi dal + * riferimento float (host seq, host SMP e cluster seq, bit-identici): + * Coeff checksum=398521, Block[0] DC=4232, bit_depth_dc=17, + * bit_depth_ac=12, Output bytes=336, Checksum=42236, Output[0]=192. + * + * Decomposizione (come obpmark_2_2_pulpd_par.c): + * conversione int->float, row-pass/column-pass per livello e + * arrotondamento float->int: split per riga/colonna, barriere tra le + * fasi; scratch column-pass privato per core; + * coeff_regroup: core 0 (scr passa da float a int solo dopo barriera); + * build_block_string: split per blocco; + * dc_encoding + ac_depth_encoder: core 0; + * per ogni bit-plane: bit_plane_encoding split per blocco, + * stages_encoding su core 0. + * + * Vincoli del cluster: pragma O2 + fp-contract=off + no-strict-aliasing + * limitato al file (PULP_CFLAGS resta -O3), noinline su + * write_to_the_output_segment, text entro i primi 64 KiB della L2. + * + * Misura aggiuntiva: cicli della sola DWT (lettura del contatore su + * core 0 dopo la barriera che chiude l'arrotondamento). + */ + +#include "pulp.h" +#include +#include + +#pragma GCC optimize ("O2", "fp-contract=off", "no-strict-aliasing") + +#define HEAPSRAM __attribute__((section(".heapsram"))) + +#define IMAGE_WIDTH 32u +#define IMAGE_HEIGHT 32u +#define BIT_SIZE 16u +#define LEVELS_DWT 3u +#define BLOCKSIZEIMAGE 8u +#define SEGMENT_SIZE 16u +#define GAGGLE_SIZE 16u +#define UNCODED_VALUE 0xFFu +#define BITLL3 3u + +#define MAX_SYMBOLS_IN_BLOCK 22u +#define SIZE_TYPE 4u + +#define ENUM_NONE 0 +#define ENUM_TYPE_P 1 +#define ENUM_TYPE_TRAN_B 2 +#define ENUM_TYPE_TRAN_D 3 +#define ENUM_TYPE_CI 4 +#define ENUM_TRAN_GI 5 +#define ENUM_TRAN_HI 6 +#define ENUM_TYPE_HIJ 7 + +#define BITHH1 0 +#define BITLH1 1 +#define BITHL1 1 +#define BITHH2 1 +#define BITLH2 2 +#define BITHL2 2 +#define BITHH3 2 +#define BITHL3 3 +#define BITLH3 3 + +#define ABSOLUTE(x) ((x) < 0 ? -(x) : (x)) +#define SIGN(x) ((x) < 0 ? 1 : 0) + +#define HIGHPASSFILTERSIZE 7 +#define LOWPASSFILTERSIZE 9 + +// verbatim da src/2.2-image_compression/include/benchmark.h +static const float lowpass_filter_cpu[LOWPASSFILTERSIZE] = {0.037828455507,-0.023849465020,-0.110624404418,0.377402855613, 0.852698679009,0.377402855613, -0.110624404418,-0.023849465020, 0.037828455507}; +static const float highpass_filter_cpu[HIGHPASSFILTERSIZE] = {-0.064538882629, 0.040689417609, 0.418092273222,-0.788485616406,0.418092273222,0.040689417609,-0.064538882629}; + +static const int bit2_pattern[4] = {0, 2, 1, 3}; +static const int bit3_pattern[8] = {1, 4, 0, 5, 2, 6, 3, 7}; +static const int bit3_pattern_TranD[8] = {0, 3, 0, 4, 1, 5, 2, 6}; +static const int bit4_pattern_TypeCi[16] = {10, 1, 3, 6, 2, 5, 9, 12, 0, 8, 7, 13, 4, 14, 11, 15}; +static const int bit4_pattern_TypeHij_TranHi[16] = {0, 1, 3, 6, 2, 5, 9, 11, 0, 8, 7, 12, 4, 13, 10, 14}; + +static const int type_of_compression = 1; // 1 = float path + +#define W_PADDED IMAGE_WIDTH +#define H_PADDED IMAGE_HEIGHT +#define TOTAL_BLOCKS ((W_PADDED / BLOCKSIZEIMAGE) * (H_PADDED / BLOCKSIZEIMAGE)) // 16 + +#define OUTPUT_BUFFER_BYTES 1024u +#define MAX_CORES 8u + +#define EXPECTED_COEFF_CHECKSUM 398521 +#define EXPECTED_BLOCK0_DC 4232 +#define EXPECTED_OUTPUT_0 192u +#define EXPECTED_CHECKSUM 42236u +#define EXPECTED_TOTAL_BYTES 336u +#define EXPECTED_BIT_DEPTH_DC 17u +#define EXPECTED_BIT_DEPTH_AC 12u + +// ============================================================ +// Stato condiviso tra core +// ============================================================ +typedef union { + int i[H_PADDED][W_PADDED]; + float f[H_PADDED][W_PADDED]; +} buf2d_t; + +static unsigned int g_num_cores = 1u; +static HEAPSRAM unsigned int g_qf; +static HEAPSRAM unsigned int g_dwt_cycles; + +static HEAPSRAM buf2d_t img; // immagine -> campioni float -> coefficienti +static HEAPSRAM buf2d_t scr; // uscita row-pass (float) / scratch coeff_regroup (int) +static HEAPSRAM int block_string[TOTAL_BLOCKS][BLOCKSIZEIMAGE * BLOCKSIZEIMAGE]; +static HEAPSRAM float col_in_f[MAX_CORES][H_PADDED]; // scratch column-pass, privato per core +static HEAPSRAM float col_out_f[MAX_CORES][H_PADDED]; + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + *start = id * chunk; + *end = (id == n - 1u) ? total : (*start + chunk); +} + +// ============================================================ +// DWT float +// ============================================================ +static void ccsds_wavelet_transform_1D_float(const float *A, float *B, int size) { + int full_size = size * 2; + int hi_start = -(LOWPASSFILTERSIZE / 2); + int hi_end = LOWPASSFILTERSIZE / 2; + int gi_start = -(HIGHPASSFILTERSIZE / 2); + int gi_end = HIGHPASSFILTERSIZE / 2; + int i; + + for (i = 0; i < size; ++i) { + float sum_value_low = 0.0f; + for (int hi = hi_start; hi < hi_end + 1; ++hi) { + int x_position = (2 * i) + hi; + if (x_position < 0) + x_position = -x_position; + else if (x_position > full_size - 1) + x_position = full_size - 1 - (x_position - (full_size - 1)); + sum_value_low += lowpass_filter_cpu[hi + hi_end] * A[x_position]; + } + B[i] = sum_value_low; + + float sum_value_high = 0.0f; + for (int gi = gi_start; gi < gi_end + 1; ++gi) { + int x_position = (2 * i) + gi + 1; + if (x_position < 0) + x_position = -x_position; + else if (x_position > full_size - 1) + x_position = full_size - 1 - (x_position - (full_size - 1)); + sum_value_high += highpass_filter_cpu[gi + gi_end] * A[x_position]; + } + B[i + size] = sum_value_high; + } +} + +static inline int round_half_away(float x) { + int t = (int)x; + float f = x - (float)t; + if (x >= 0.0f) return t + (f >= 0.5f ? 1 : 0); + else return t - (f <= -0.5f ? 1 : 0); +} + +// conversione int -> float in place (parallela per riga) +static void dwt_convert_in_worker(unsigned int rstart, unsigned int rend) { + unsigned int i, j; + for (i = rstart; i < rend; ++i) + for (j = 0; j < W_PADDED; ++j) { + int v = img.i[i][j]; + img.f[i][j] = (float)v; + } +} + +// row-pass (parallela): ogni core scrive solo le proprie righe di scr.f +static void dwt_row_pass_worker(int w, unsigned int rstart, unsigned int rend) { + unsigned int i; + for (i = rstart; i < rend; ++i) + ccsds_wavelet_transform_1D_float(img.f[i], scr.f[i], w / 2); +} + +// column-pass (parallela): scratch privato col_in_f/col_out_f[core] +static void dwt_col_pass_worker(int h, unsigned int cstart, unsigned int cend, unsigned int core) { + unsigned int i, x; + for (i = cstart; i < cend; ++i) { + for (x = 0; x < (unsigned int)h; ++x) col_in_f[core][x] = scr.f[x][i]; + ccsds_wavelet_transform_1D_float(col_in_f[core], col_out_f[core], h / 2); + for (x = 0; x < (unsigned int)h; ++x) img.f[x][i] = col_out_f[core][x]; + } +} + +// arrotondamento float -> int in place (parallelo per riga) +static void dwt_round_out_worker(unsigned int rstart, unsigned int rend) { + unsigned int i, j; + for (i = rstart; i < rend; ++i) + for (j = 0; j < W_PADDED; ++j) { + float v = img.f[i][j]; + img.i[i][j] = round_half_away(v); + } +} + +// ============================================================ +// Riorganizzazione dei coefficienti (core 0) +// ============================================================ +static void coeff_regroup(void) { + int i, j, p, k, x, y, xb; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) scr.i[i][j] = 0; + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 4; p < 8; ++p) + for (k = 4; k < 8; ++k) + scr.i[x+p][y+k] = img.i[i+p-4][j+k-4]; + } + for (i = 0; i < H_PADDED>>1; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = i << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 0; p < 4; ++p) + for (k = 4; k < 8; ++k) + scr.i[x+p][y+k] = img.i[i+p][j+k-4]; + } + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = 0; j < W_PADDED>>1; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = j << 1; + for (p = 4; p < 8; ++p) + for (k = 0; k < 4; ++k) + scr.i[x+p][y+k] = img.i[i+p-4][j+k]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = (j - (W_PADDED>>2)) << 2; + scr.i[x+2][y+2] = img.i[i][j]; + scr.i[x+2][y+3] = img.i[i][j+1]; + scr.i[x+3][y+2] = img.i[i+1][j]; + scr.i[x+3][y+3] = img.i[i+1][j+1]; + } + for (i = 0; i < H_PADDED>>2; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = i << 2; y = (j - (W_PADDED>>2)) << 2; + scr.i[x][y+2] = img.i[i][j]; + scr.i[x][y+3] = img.i[i][j+1]; + scr.i[x+1][y+2] = img.i[i+1][j]; + scr.i[x+1][y+3] = img.i[i+1][j+1]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = 0; j < W_PADDED>>2; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = j << 2; + scr.i[x+2][y] = img.i[i][j]; + scr.i[x+2][y+1] = img.i[i][j+1]; + scr.i[x+3][y] = img.i[i+1][j]; + scr.i[x+3][y+1] = img.i[i+1][j+1]; + } + xb = H_PADDED>>3; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + scr.i[((i-xb)<<3)+1][((j-(W_PADDED>>3))<<3)+1] = img.i[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + scr.i[i<<3][((j-(W_PADDED>>3))<<3)+1] = img.i[i][j]; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + scr.i[((i-xb)<<3)+1][j<<3] = img.i[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + scr.i[i<<3][j<<3] = img.i[i][j]; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + img.i[i][j] = scr.i[i][j]; +} + +// build_block_string (parallela, split per blocco) +static void build_block_string_worker(unsigned int bstart, unsigned int bend) { + unsigned int block_w = W_PADDED / BLOCKSIZEIMAGE; + unsigned int bidx, x, y; + for (bidx = bstart; bidx < bend; ++bidx) { + unsigned int i = bidx / block_w; + unsigned int j = bidx % block_w; + for (x = 0; x < BLOCKSIZEIMAGE; ++x) + for (y = 0; y < BLOCKSIZEIMAGE; ++y) + block_string[bidx][x*BLOCKSIZEIMAGE + y] = + img.i[i*BLOCKSIZEIMAGE + x][j*BLOCKSIZEIMAGE + y]; + } +} + +static void generate_input(void) { + unsigned int i, j; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + img.i[i][j] = (int)((i * 173u + j * 91u + 13u) & 0xFFFFu); +} + +// ============================================================ +// Bitstream di output (solo core 0) con controllo di overflow +// ============================================================ +typedef struct { + uint8_t OutputBitStream[OUTPUT_BUFFER_BYTES]; + unsigned int num_bits; + unsigned int num_total_bytes; + unsigned int overflow; +} OutputBitStream_t; + +static HEAPSRAM OutputBitStream_t output_stream; + +static void __attribute__((noinline)) write_to_the_output_segment(long word, unsigned int length) { + int i; + if (output_stream.num_total_bytes + (length >> 3) + 2u > OUTPUT_BUFFER_BYTES) { + output_stream.overflow = 1u; + return; + } + for (i = (int)length - 1; i >= 0; --i) { + output_stream.OutputBitStream[output_stream.num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1) << (7 - output_stream.num_bits)); + output_stream.num_bits++; + if (output_stream.num_bits >= 8u) { + output_stream.num_bits = 0u; + output_stream.num_total_bytes++; + } + } +} + +static unsigned int ceil_log2(unsigned long x) { + unsigned int r = 0; + unsigned long v = 1; + while (v < x) { v <<= 1; r++; } + return r; +} + +static unsigned int bit_size_with_pow2_adjust(long v) { + unsigned long av = (unsigned long)(v < 0 ? -v : v); + if (av == 0) return 0; + unsigned int b = ceil_log2(av); + if ((av & (av - 1)) == 0) b += 1; + return b; +} + +static unsigned long conv_to_twos_complement(int value, unsigned int num_bits) { + unsigned long temp, complement; + short i; + if (num_bits == 1) return 0; + if (value >= 0) return (unsigned long) value; + complement = ~(unsigned long)(-value); + temp = 0; + for (i = 0; i < (short)num_bits; i++) { temp <<= 1; temp++; } + complement &= temp; + complement++; + return complement; +} + +typedef struct { + unsigned int symbol_val; + unsigned int symbol_len; + unsigned int symbol_mapped_pattern; + unsigned int sign; + unsigned int type; +} str_symbol_details_t; + +typedef struct { + long shifted_dc; + unsigned int dc_reminder; + unsigned long mapped_dc; + long max_ac_bit_size; + unsigned long mapped_ac; + unsigned int type_p; + unsigned int tran_b; + unsigned int tran_d; + unsigned int tran_gi; + unsigned int parent_ref_symbol; + unsigned int parent_sym_len; + unsigned int children_ref_symbol; + unsigned int children_sym_len; + unsigned int type_ci[3]; + unsigned int tran_hi[3]; + unsigned int grand_children_ref_symbol[3]; + unsigned int grand_children_sym_len[3]; + unsigned int type_hi[12]; + str_symbol_details_t symbol_block[MAX_SYMBOLS_IN_BLOCK]; +} block_data_t; + +typedef struct { + int start_img_flag; + int end_img_flag; + unsigned int segment_count; + unsigned int bit_depth_dc; + unsigned int bit_depth_ac; + int part_2_flag; + int part_3_flag; + int part_4_flag; + unsigned int pad_rows; +} header_data_t; + +static HEAPSRAM block_data_t block_data[SEGMENT_SIZE]; +static HEAPSRAM header_data_t header_data; + +static void header_output(void) { + write_to_the_output_segment(header_data.start_img_flag, 1); + write_to_the_output_segment(header_data.end_img_flag, 1); + write_to_the_output_segment(header_data.segment_count, 8); + write_to_the_output_segment(header_data.bit_depth_dc, 5); + write_to_the_output_segment(header_data.bit_depth_ac, 5); + write_to_the_output_segment(0, 1); + write_to_the_output_segment(header_data.part_2_flag, 1); + write_to_the_output_segment(header_data.part_3_flag, 1); + write_to_the_output_segment(header_data.part_4_flag, 1); + if (header_data.end_img_flag) { + write_to_the_output_segment(header_data.pad_rows, 3); + write_to_the_output_segment(0, 5); + } +} + +static void coding_quantized_coefficients(unsigned int N) { + long x_min = -(1L << (N - 1)); + long x_max = (1L << (N - 1)) - 1; + unsigned int number_bits = 0, i; + long theta, gamma; + for (i = 0; i < N - 1; i++) number_bits = (number_bits << 1) + 1; + if ((block_data[0].shifted_dc & (1L << (N - 1))) > 0) + block_data[0].shifted_dc = -(long)(((block_data[0].shifted_dc ^ number_bits) & number_bits) + 1); + block_data[0].mapped_dc = (unsigned long) block_data[0].shifted_dc; + for (i = 1; i < SEGMENT_SIZE; i++) { + if ((block_data[i].shifted_dc & (1L << (N - 1))) > 0) + block_data[i].shifted_dc = -(long)(((block_data[i].shifted_dc ^ number_bits) & number_bits) + 1); + theta = (block_data[i-1].shifted_dc - x_min) < (x_max - block_data[i-1].shifted_dc) + ? (block_data[i-1].shifted_dc - x_min) : (x_max - block_data[i-1].shifted_dc); + gamma = block_data[i].shifted_dc - block_data[i-1].shifted_dc; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_dc = (unsigned long)(2 * gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_dc = (unsigned long)(2 * (-gamma) - 1); + else + block_data[i].mapped_dc = (unsigned long)(theta + (gamma < 0 ? -gamma : gamma)); + } +} + +static void dc_encoder(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + unsigned int total_bits, min_bits = 0xFFFFu; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position > 1 ? start_position : 1); i < end_position; i++) + total_bits += (unsigned int)((block_data[i].mapped_dc >> k) + 1) + k; + if (total_bits < min_bits && total_bits < N * GAGGLE_SIZE) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_dc, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_dc >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position > 1 ? start_position : 1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_dc, encode_selection); + } +} + +static void dc_entropy_coding(unsigned int N, unsigned int quantization_factor) { + unsigned int id_length = 0, max_k = 0, i; + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 8) { max_k = 6; id_length = 3; } + else { max_k = 8; id_length = 4; } + dc_encoder(max_k, id_length, N); + if (header_data.bit_depth_ac < quantization_factor) { + unsigned int num_additional_bit_planes = quantization_factor - header_data.bit_depth_ac; + for (i = 0; i < num_additional_bit_planes; i++) { + unsigned int kk; + for (kk = 0; kk < SEGMENT_SIZE; kk++) + write_to_the_output_segment( + (block_data[kk].dc_reminder >> (quantization_factor - i - 1)), 1); + } + } +} + +static unsigned int dc_encoding(void) { + long max_ac_value = 0; + unsigned int quantization_factor; + unsigned int i, j, k_mask; + header_data.bit_depth_dc = 0; + for (i = 0; i < SEGMENT_SIZE; ++i) { + long max_ac_value_block = 0; + int dc_value = block_string[i][0]; + unsigned int dc_bit_size; + if (dc_value < 0) + dc_bit_size = ceil_log2((unsigned long)(-dc_value) + 1UL); + else + dc_bit_size = ceil_log2((unsigned long)(1 + dc_value)) + 1; + if (dc_bit_size > header_data.bit_depth_dc) header_data.bit_depth_dc = dc_bit_size; + for (j = 1; j < BLOCKSIZEIMAGE * BLOCKSIZEIMAGE; ++j) { + long v = block_string[i][j]; + long av = v < 0 ? -v : v; + if (av > (max_ac_value < 0 ? -max_ac_value : max_ac_value)) max_ac_value = v; + if (av > (max_ac_value_block < 0 ? -max_ac_value_block : max_ac_value_block)) max_ac_value_block = v; + } + block_data[i].max_ac_bit_size = (long) bit_size_with_pow2_adjust(max_ac_value_block); + } + header_data.bit_depth_ac = bit_size_with_pow2_adjust(max_ac_value); + header_output(); + if (header_data.bit_depth_dc <= 3) + quantization_factor = 0; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) <= 1) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 3; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) > 10) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 10; + else + quantization_factor = 1 + (header_data.bit_depth_ac >> 1); + k_mask = (1u << quantization_factor) - 1u; + for (i = 0; i < SEGMENT_SIZE; ++i) { + unsigned long new_value_twos = conv_to_twos_complement(block_string[i][0], header_data.bit_depth_dc); + block_data[i].shifted_dc = (long)(new_value_twos >> quantization_factor); + block_data[i].dc_reminder = (unsigned int)(new_value_twos & k_mask); + } + { + unsigned int N = header_data.bit_depth_dc > quantization_factor + ? header_data.bit_depth_dc - quantization_factor : 1; + if (N == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].shifted_dc, 1); + } else { + coding_quantized_coefficients(N); + dc_entropy_coding(N, quantization_factor); + } + } + return quantization_factor; +} + +static void dpcm_ac_mapper(unsigned int N) { + long x_min = 0; + long x_max = (1L << N) - 1; + long theta, gamma; + unsigned int i; + block_data[0].mapped_ac = (unsigned long) block_data[0].max_ac_bit_size; + for (i = 1; i < SEGMENT_SIZE; i++) { + theta = (block_data[i-1].max_ac_bit_size - x_min) < (x_max - block_data[i-1].max_ac_bit_size) + ? (block_data[i-1].max_ac_bit_size - x_min) : (x_max - block_data[i-1].max_ac_bit_size); + gamma = block_data[i].max_ac_bit_size - block_data[i-1].max_ac_bit_size; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_ac = (unsigned long)(2*gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_ac = (unsigned long)(2*(-gamma) - 1); + else + block_data[i].mapped_ac = (unsigned long)(theta + (gamma<0?-gamma:gamma)); + } +} + +static void ac_gaggle_encode(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + long total_bits, min_bits = 0xFFFF; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position>1?start_position:1); i < end_position; i++) + total_bits += (long)((block_data[i].mapped_ac >> k) + 1) + k; + if (total_bits < min_bits && total_bits < (long)(N * GAGGLE_SIZE)) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_ac, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_ac >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position>1?start_position:1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_ac, encode_selection); + } +} + +static void ac_depth_encoder(void) { + unsigned int N = 0, id_length, max_k; + while ((header_data.bit_depth_ac >> N) > 0) N++; + dpcm_ac_mapper(N); + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 5) { max_k = 6; id_length = 3; } + else { max_k = 0; id_length = 0; } + ac_gaggle_encode(max_k, id_length, N); +} + +// bit_plane_encoding (parallela, split per blocco [bstart,bend)) +static void bit_plane_encoding_worker(unsigned int bit_plane_number, unsigned int bstart, unsigned int bend) { + unsigned int temp_x, temp_y, block_num; + unsigned char symbol; + int temp_value; + unsigned int final_position; + int bit_set_plane = (1 << (bit_plane_number - 1)); + + for (block_num = bstart; block_num < bend; ++block_num) { + symbol = 0; + final_position = block_num; + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + + for (int j = 0; j < 3; ++j) { + switch (j) { + case 0: temp_y = 1; break; + case 1: temp_y = BLOCKSIZEIMAGE; break; + case 2: temp_y = BLOCKSIZEIMAGE + 1; break; + default: temp_y = 0; break; + } + temp_x = final_position; + if (!type_of_compression) { + if (((j==0)&&(BITHL3>=bit_plane_number)) || ((j==1)&&(BITLH3>=bit_plane_number)) || ((j==2)&&(BITHH3>=bit_plane_number))) + continue; + } + if ((block_data[block_num].type_p & (1 << (2-j))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_P; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + if (ABSOLUTE(block_string[temp_x][temp_y]) >= (1 << (bit_plane_number-1)) && + ABSOLUTE(block_string[temp_x][temp_y]) < (1 << bit_plane_number)) { + block_data[block_num].type_p += (1 << (2-j)); + block_data[block_num].symbol_block[symbol].symbol_val += 1; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[temp_x][temp_y]); + } + } else { + temp_value = (ABSOLUTE(block_string[temp_x][temp_y]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((j==0)&&(BITHL3=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = 1; break; + } + if (break_flag) break; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = 1; break; + } + if (break_flag) break; + } + } + + if (block_data[block_num].tran_b == 0) { + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 0; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) ++symbol; + + if (block_data[block_num].tran_b == 1) { + for (int k = 0; k < 3; k++) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)&&(BITHL1>=bit_plane_number)) + || ((k==1)&&(BITLH2>=bit_plane_number)&&(BITLH1>=bit_plane_number)) + || ((k==2)&&(BITHH2>=bit_plane_number)&&(BITHH1>=bit_plane_number))) + continue; + } + int break_flag = 0; + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + int jump_flag = 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + jump_flag = 1; + } + if (!jump_flag) { + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = 1; break; + } + } + if (!break_flag) { + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = 1; break; + } + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + } + + for (unsigned int k = 0; k < 3; ++k) { + if ((block_data[block_num].tran_d & (1 << (2-k))) != 0) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + int p = 1; + for (unsigned int i = 0; i < 4; ++i) + if ((block_data[block_num].type_ci[k] & (1 << (3-i))) == 0) { p = 0; break; } + if (p == 0) { + unsigned int counter = 0; + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_CI; + for (unsigned int i = temp_x; i < temp_x+2; ++i) { + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) { + if ((block_data[block_num].type_ci[k] & (1 << (3-counter))) == 0) { + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].type_ci[k] += (1 << (3-counter)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (((block_data[block_num].tran_d & (1 << (2-k))) != 0) && ((block_data[block_num].tran_gi & (1 << (2-k))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_GI; + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + int break_flag = 0; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].tran_gi += (1 << (2-k)); + break_flag = 1; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if (((block_data[block_num].tran_gi & (1 << (2-i3))) != 0) && ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_HI; + int break_flag = 0; + for (unsigned int k = temp_x; k < temp_x+2 && !break_flag; k++) + for (unsigned int p = temp_y; p < temp_y+2; p++) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].tran_hi[i3] += (1 << (3-jx)); + break_flag = 1; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) != 0) { + unsigned int counter = 0; + for (unsigned int k = 0; k < 4; ++k) + if ((block_data[block_num].type_hi[i3*SIZE_TYPE+jx] & (1 << (3-k))) == 0) counter++; + if (counter == 0) { + for (unsigned int k = temp_x; k < temp_x+2; k++) { + for (unsigned int p = temp_y; p < temp_y+2; p++) { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1 0) { + block_data[block_num].type_hi[i3*SIZE_TYPE+jx] += (1 << (3-t)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1symbol_len) { + case 0: return; + case 1: s->symbol_mapped_pattern = s->symbol_val; break; + case 2: s->symbol_mapped_pattern = bit2_pattern[s->symbol_val]; break; + case 3: + s->symbol_mapped_pattern = (s->type == ENUM_TYPE_TRAN_D) ? bit3_pattern_TranD[s->symbol_val] : bit3_pattern[s->symbol_val]; + break; + case 4: + if (s->type == ENUM_TYPE_CI) s->symbol_mapped_pattern = bit4_pattern_TypeCi[s->symbol_val]; + else if (s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ) s->symbol_mapped_pattern = bit4_pattern_TypeHij_TranHi[s->symbol_val]; + break; + default: break; + } +} + +static void coding_options(unsigned int bit_plane_number, unsigned char *code_option_gaggle) { + unsigned int bit_counter_2bits[2] = {0}; + unsigned int bit_counter_3bits[3] = {0}; + unsigned int bit_counter_4bits[4] = {0}; + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + if (s->type == ENUM_NONE) continue; + if (s->symbol_len == 1) { pattern_mapping(s); continue; } + pattern_mapping(s); + if (s->symbol_len == 2) { + switch (s->symbol_mapped_pattern) { + case 0: bit_counter_2bits[0] += 1; break; + case 1: bit_counter_2bits[0] += 2; break; + default: bit_counter_2bits[0] += 3; break; + } + bit_counter_2bits[1] += 2; + } else if (s->symbol_len == 3) { + if (s->symbol_mapped_pattern <= 2) bit_counter_3bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 5) bit_counter_3bits[0] += 5; + else bit_counter_3bits[0] += 6; + if (s->symbol_mapped_pattern <= 1) bit_counter_3bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_3bits[1] += 3; + else bit_counter_3bits[1] += 4; + bit_counter_3bits[2] += 3; + } else if (s->symbol_len == 4) { + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[0] += 7; + else bit_counter_4bits[0] += 8; + if (s->symbol_mapped_pattern <= 1) bit_counter_4bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[1] += 3; + else if (s->symbol_mapped_pattern <= 5) bit_counter_4bits[1] += 4; + else if (s->symbol_mapped_pattern <= 11) bit_counter_4bits[1] += 6; + else bit_counter_4bits[1] += 7; + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[2] += 3; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[2] += 4; + else bit_counter_4bits[2] += 5; + bit_counter_4bits[3] += 4; + } + } + } + code_option_gaggle[0] = (bit_counter_2bits[0] < bit_counter_2bits[1]) ? 0 : 1; + if (bit_counter_3bits[2] <= bit_counter_3bits[0] && bit_counter_3bits[2] <= bit_counter_3bits[1]) code_option_gaggle[1] = 3; + else if (bit_counter_3bits[0] <= bit_counter_3bits[1]) code_option_gaggle[1] = 0; + else code_option_gaggle[1] = 1; + if (bit_counter_4bits[3] <= bit_counter_4bits[1] && bit_counter_4bits[3] <= bit_counter_4bits[0] && bit_counter_4bits[3] <= bit_counter_4bits[2]) code_option_gaggle[2] = 3; + else if (bit_counter_4bits[0] <= bit_counter_4bits[1] && bit_counter_4bits[0] <= bit_counter_4bits[2]) code_option_gaggle[2] = 0; + else if (bit_counter_4bits[1] <= bit_counter_4bits[2]) code_option_gaggle[2] = 1; + else code_option_gaggle[2] = 2; +} + +static void rice_coding(long input_value, unsigned int bit_length, unsigned char *code_option) { + switch (bit_length) { + case 0: return; + case 1: write_to_the_output_segment(input_value, 1); break; + case 2: + if (code_option[0] == 1) write_to_the_output_segment(input_value, 2); + else { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1, 1); } + else write_to_the_output_segment(0, 3); + } + break; + case 3: + if (code_option[1] == 0) { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 5) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-3,2); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,3); } + } else if (code_option[1] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else { + write_to_the_output_segment(0,2); + switch (input_value) { + case 4: write_to_the_output_segment(2,2); break; + case 5: write_to_the_output_segment(3,2); break; + case 6: write_to_the_output_segment(0,2); break; + case 7: write_to_the_output_segment(1,2); break; + } + } + } else write_to_the_output_segment(input_value, 3); + break; + case 4: + if (code_option[2] == 0) { + if (input_value <= 3) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 7) { write_to_the_output_segment(0,5); write_to_the_output_segment(input_value-4,2); } + else { write_to_the_output_segment(0,4); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else if (input_value <= 5) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-2,2); } + else if (input_value <= 11) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-6,3); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 2) { + if (input_value <= 3) write_to_the_output_segment(input_value+4, 3); + else if (input_value <= 7) write_to_the_output_segment(input_value, 4); + else if (input_value <= 11) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-4,3); } + else write_to_the_output_segment(input_value-12, 5); + } else write_to_the_output_segment(input_value, 4); + break; + } +} + +static void gaggle_pass(unsigned int bit_plane_number, unsigned char *code_option_gaggle, int *hit_flag, int pass) { + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + int match = (pass == 1 && s->type == ENUM_TYPE_P) + || (pass == 2 && (s->type == ENUM_TYPE_TRAN_B || s->type == ENUM_TYPE_TRAN_D || s->type == ENUM_TYPE_CI)) + || (pass == 3 && (s->type == ENUM_TRAN_GI || s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ)); + if (!match) continue; + if (s->symbol_len > 1 && !hit_flag[s->symbol_len - 2]) { + hit_flag[s->symbol_len - 2] = 1; + if (s->symbol_len == 2) write_to_the_output_segment(code_option_gaggle[0], 1); + else if (s->symbol_len == 3) write_to_the_output_segment(code_option_gaggle[1], 2); + else if (s->symbol_len == 4) write_to_the_output_segment(code_option_gaggle[2], 2); + } + rice_coding(s->symbol_mapped_pattern, s->symbol_len, code_option_gaggle); + if (s->type == ENUM_TYPE_CI || s->type == ENUM_TYPE_HIJ) { + unsigned int counter = 0, i; + for (i = 0; i < s->symbol_len; ++i) if ((s->symbol_val & (1 << i)) > 0) counter++; + write_to_the_output_segment(s->sign, counter); + } + s->symbol_val = 0; s->symbol_len = 0; s->symbol_mapped_pattern = 0; s->sign = 0; s->type = 0; + } + } +} + +static void ref_bit_end_encode(void) { + unsigned int block_num, i; + for (block_num = 0; block_num < SEGMENT_SIZE; ++block_num) { + if (block_data[block_num].parent_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].parent_ref_symbol, block_data[block_num].parent_sym_len); + block_data[block_num].parent_ref_symbol = 0; block_data[block_num].parent_sym_len = 0; + } + if (block_data[block_num].children_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].children_ref_symbol, block_data[block_num].children_sym_len); + block_data[block_num].children_ref_symbol = 0; block_data[block_num].children_sym_len = 0; + } + for (i = 0; i < 3; ++i) { + if (block_data[block_num].grand_children_sym_len[i] > 0) + write_to_the_output_segment(block_data[block_num].grand_children_ref_symbol[i], block_data[block_num].grand_children_sym_len[i]); + block_data[block_num].grand_children_sym_len[i] = 0; + block_data[block_num].grand_children_ref_symbol[i] = 0; + } + } +} + +static void stages_encoding(unsigned int bit_plane_number) { + unsigned char code_option_gaggle[3] = {0,0,0}; + int hit_flag[3] = {0,0,0}; + coding_options(bit_plane_number, code_option_gaggle); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 1); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 2); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 3); + ref_bit_end_encode(); +} + +// ============================================================ +// Cluster test harness +// ============================================================ +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_2_2_par_float", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + g_num_cores = get_core_num(); + + if (rt_core_id() < g_num_cores) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int core_id = (unsigned int)get_core_id(); + unsigned int n = g_num_cores; + unsigned int i, k; + + if (core_id == 0) { + generate_input(); + for (i = 0; i < OUTPUT_BUFFER_BYTES; ++i) output_stream.OutputBitStream[i] = 0u; + output_stream.num_bits = 0u; + output_stream.num_total_bytes = 0u; + output_stream.overflow = 0u; + + header_data.start_img_flag = 1; + header_data.end_img_flag = 1; + header_data.segment_count = 0; + header_data.part_2_flag = 0; + header_data.part_3_flag = 0; + header_data.part_4_flag = 0; + header_data.pad_rows = 0; + + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + } + synch_barrier(); + + start(); + + // ---- DWT float (parallela) ---- + { + unsigned int s, e, lvl; + int level_size = W_PADDED; + + split_range(H_PADDED, core_id, n, &s, &e); + dwt_convert_in_worker(s, e); + synch_barrier(); + + for (lvl = 0; lvl < LEVELS_DWT; ++lvl) { + split_range((unsigned int)level_size, core_id, n, &s, &e); + dwt_row_pass_worker(level_size, s, e); + synch_barrier(); + split_range((unsigned int)level_size, core_id, n, &s, &e); + dwt_col_pass_worker(level_size, s, e, core_id); + synch_barrier(); + level_size /= 2; + } + + split_range(H_PADDED, core_id, n, &s, &e); + dwt_round_out_worker(s, e); + synch_barrier(); + } + + if (core_id == 0) { + g_dwt_cycles = cpu_perf_get(CSR_PCER_CYCLES); + coeff_regroup(); + } + synch_barrier(); + + // ---- build_block_string (parallela) ---- + { + unsigned int bs, be; + split_range(TOTAL_BLOCKS, core_id, n, &bs, &be); + build_block_string_worker(bs, be); + } + synch_barrier(); + + // ---- DC encoding + AC depth encoding (core 0) ---- + if (core_id == 0) { + g_qf = dc_encoding(); + if (header_data.bit_depth_ac == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].max_ac_bit_size, 1); + } else if (header_data.bit_depth_ac > 1) { + ac_depth_encoder(); + } + } + synch_barrier(); + + // ---- AC bit-plane encoding ---- + { + unsigned int bit_depth_ac = header_data.bit_depth_ac; + unsigned int qf = g_qf; + unsigned int bit_plane, bs, be; + split_range(SEGMENT_SIZE, core_id, n, &bs, &be); + + for (bit_plane = bit_depth_ac; bit_plane > 0; bit_plane--) { + if (core_id == 0 && + (bit_plane <= qf) && + (type_of_compression || (qf > BITLL3 && BITLL3 < bit_plane))) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment((block_data[i].dc_reminder >> (bit_plane - 1)) & 0x01, 1); + } + + bit_plane_encoding_worker(bit_plane, bs, be); + synch_barrier(); + + if (core_id == 0) stages_encoding(bit_plane); + synch_barrier(); + } + } + + stop(); + + if (core_id != 0) return; + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + int coeff_checksum = 0; + for (i = 0; i < TOTAL_BLOCKS; ++i) + for (k = 0; k < BLOCKSIZEIMAGE * BLOCKSIZEIMAGE; ++k) + coeff_checksum += block_string[i][k]; + + unsigned int checksum = 0u; + for (i = 0; i < output_stream.num_total_bytes + 1u && i < OUTPUT_BUFFER_BYTES; ++i) + checksum += output_stream.OutputBitStream[i]; + + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + unsigned int out0 = (unsigned int)output_stream.OutputBitStream[0]; + + printf("OBPMark 2.2 - CCSDS 122.0 Image Compression (FLOAT)\n"); + printf("PULP cluster parallel (%u cores, RV32) - Astral @ 50 MHz\n", n); + printf(" Elapsed cycles : %u\n", cycles); + printf(" DWT cycles : %u\n", g_dwt_cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Coeff checksum : %d\n", coeff_checksum); + printf(" Block[0] DC : %d\n", block_string[0][0]); + printf(" bit_depth_dc : %u\n", header_data.bit_depth_dc); + printf(" bit_depth_ac : %u\n", header_data.bit_depth_ac); + printf(" quant. factor : %u\n", g_qf); + printf(" Output bytes : %u\n", output_stream.num_total_bytes); + printf(" Overflow : %u\n", output_stream.overflow); + printf(" Checksum : %u\n", checksum); + printf(" Output[0] : %u\n", out0); + + if (n <= MAX_CORES && output_stream.overflow == 0u && + coeff_checksum == EXPECTED_COEFF_CHECKSUM && + block_string[0][0] == EXPECTED_BLOCK0_DC && + out0 == EXPECTED_OUTPUT_0 && checksum == EXPECTED_CHECKSUM && + output_stream.num_total_bytes == EXPECTED_TOTAL_BYTES && + header_data.bit_depth_dc == EXPECTED_BIT_DEPTH_DC && + header_data.bit_depth_ac == EXPECTED_BIT_DEPTH_AC) { + result->errors = 0; + printf("RESULT: PASS\n"); + } else { + result->errors = 1; + printf("RESULT: FAIL (atteso coeff=%d dc0=%d dc=%u ac=%u bytes=%u chk=%u out0=%u, core<=%u)\n", + EXPECTED_COEFF_CHECKSUM, EXPECTED_BLOCK0_DC, EXPECTED_BIT_DEPTH_DC, + EXPECTED_BIT_DEPTH_AC, EXPECTED_TOTAL_BYTES, EXPECTED_CHECKSUM, + EXPECTED_OUTPUT_0, MAX_CORES); + } +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq/Makefile new file mode 100644 index 000000000..49b07c6c6 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_2_2_pulpd_seq.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq/obpmark_2_2_pulpd_seq.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq/obpmark_2_2_pulpd_seq.c new file mode 100644 index 000000000..3f5488015 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq/obpmark_2_2_pulpd_seq.c @@ -0,0 +1,1195 @@ +/* + * Copyright 2024 ETH Zurich and University of Bologna. + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + * + * OBPMark Benchmark #2.2 - CCSDS 122.0 Image Compression + * Sequential (single-core) port for the PULP integer cluster of Astral + * (core 0 only, RV32) + * + * Same algorithm and compile-time configuration as obpmark_2_2_host_seq.c + * (w_size=h_size=32, bit_size=16, segment_size=16, percorso integer). + * Essendo aritmetica interamente intera (nessuna floating point, a + * differenza di 1.2), il risultato e' atteso bit-identico al porting host + * sequenziale gia' validato: bit_depth_dc=17, bit_depth_ac=13, + * Output bytes=301, Checksum=35042, Output[0]=192. + * + * Cluster test harness (testcase_t / run_suite / check_obpmark / + * bench_cluster_forward / cpu_perf_*), HEAPSRAM buffer placement e + * meccanismo di conteggio cicli seguono le stesse convenzioni di + * obpmark_2_1_pulpd_seq.c. Solo core 0 entra in run_suite(): porting + * single-core, non SPMD a 8 core - nessun split_range()/synch_barrier() + * nel corpo del calcolo. + */ + +#include "pulp.h" +#include +#include + +#pragma GCC optimize ("O2") + +#define HEAPSRAM __attribute__((section(".heapsram"))) + +// ============================================================ +// Configurazione (identica a obpmark_2_2_host_seq.c) +// ============================================================ +#define IMAGE_WIDTH 32u +#define IMAGE_HEIGHT 32u +#define BIT_SIZE 16u +#define LEVELS_DWT 3u +#define BLOCKSIZEIMAGE 8u +#define SEGMENT_SIZE 16u +#define GAGGLE_SIZE 16u +#define UNCODED_VALUE 0xFFu +#define BITLL3 3u + +#define MAX_SYMBOLS_IN_BLOCK 22u +#define SIZE_TYPE 4u + +#define ENUM_NONE 0 +#define ENUM_TYPE_P 1 +#define ENUM_TYPE_TRAN_B 2 +#define ENUM_TYPE_TRAN_D 3 +#define ENUM_TYPE_CI 4 +#define ENUM_TRAN_GI 5 +#define ENUM_TRAN_HI 6 +#define ENUM_TYPE_HIJ 7 + +#define BITHH1 0 +#define BITLH1 1 +#define BITHL1 1 +#define BITHH2 1 +#define BITLH2 2 +#define BITHL2 2 +#define BITHH3 2 +#define BITHL3 3 +#define BITLH3 3 + +#define ABSOLUTE(x) ((x) < 0 ? -(x) : (x)) +#define SIGN(x) ((x) < 0 ? 1 : 0) + +static const int bit2_pattern[4] = {0, 2, 1, 3}; +static const int bit3_pattern[8] = {1, 4, 0, 5, 2, 6, 3, 7}; +static const int bit3_pattern_TranD[8] = {0, 3, 0, 4, 1, 5, 2, 6}; +static const int bit4_pattern_TypeCi[16] = {10, 1, 3, 6, 2, 5, 9, 12, 0, 8, 7, 13, 4, 14, 11, 15}; +static const int bit4_pattern_TypeHij_TranHi[16] = {0, 1, 3, 6, 2, 5, 9, 11, 0, 8, 7, 12, 4, 13, 10, 14}; + +static const int type_of_compression = 0; // 0 = integer path + +#define W_PADDED IMAGE_WIDTH +#define H_PADDED IMAGE_HEIGHT +#define TOTAL_BLOCKS ((W_PADDED / BLOCKSIZEIMAGE) * (H_PADDED / BLOCKSIZEIMAGE)) // 16 +#define NUM_SEGMENTS 1u + +#define OUTPUT_BUFFER_BYTES 512u + +#define EXPECTED_OUTPUT_0 192u +#define EXPECTED_CHECKSUM 35042u +#define EXPECTED_TOTAL_BYTES 301u +#define EXPECTED_BIT_DEPTH_DC 17u +#define EXPECTED_BIT_DEPTH_AC 13u + +// ============================================================ +// Buffer statici HEAPSRAM (niente heap; stessa collocazione usata +// per obpmark_2_1_pulpd_seq.c) +// ============================================================ +static HEAPSRAM int image_data[H_PADDED][W_PADDED]; +static HEAPSRAM int block_string[TOTAL_BLOCKS][BLOCKSIZEIMAGE * BLOCKSIZEIMAGE]; +static HEAPSRAM int scratch_2d[H_PADDED][W_PADDED]; // condiviso: aux_row (Stage 1 DWT) e regroup_tmp (coeff_regroup), fasi non sovrapposte +static HEAPSRAM int aux_col_in[H_PADDED]; +static HEAPSRAM int aux_col_out[H_PADDED]; + +static inline int floor_div(int a, int b) { + int q = a / b; + int r = a % b; + if (r != 0 && ((r < 0) != (b < 0))) q--; + return q; +} + +static void ccsds_wavelet_transform_1D_int(const int *A, int *B, int size) { + int full = 2 * size; + int i; + for (i = 0; i < size; ++i) { + int s, val; + if (i == 0) { + s = 9 * (A[0] + A[2]) - (A[2] + A[4]); + val = A[1] - floor_div(s + 8, 16); + } else if (i == size - 2) { + s = 9 * (A[full-4] + A[full-2]) - (A[full-6] + A[full-2]); + val = A[full-3] - floor_div(s + 8, 16); + } else if (i == size - 1) { + s = 9 * A[full-2] - A[full-4]; + val = A[full-1] - floor_div(s + 4, 8); + } else { + s = 9 * (A[2*i] + A[2*i+2]) - (A[2*i-2] + A[2*i+4]); + val = A[2*i+1] - floor_div(s + 8, 16); + } + B[size + i] = val; + } + for (i = 0; i < size; ++i) { + int val; + if (i == 0) + val = A[0] - floor_div(1 - B[size], 2); + else + val = A[2*i] - floor_div(2 - (B[i+size-1] + B[i+size]), 4); + B[i] = val; + } +} + +static void ccsds_wavelet_transform_2D_int(int A[H_PADDED][W_PADDED], int w, int h) { + int i, x; + for (i = 0; i < h; ++i) + ccsds_wavelet_transform_1D_int(A[i], scratch_2d[i], w / 2); + for (i = 0; i < w; ++i) { + for (x = 0; x < h; ++x) aux_col_in[x] = scratch_2d[x][i]; + ccsds_wavelet_transform_1D_int(aux_col_in, aux_col_out, h / 2); + for (x = 0; x < h; ++x) A[x][i] = aux_col_out[x]; + } +} + +static void dwt2D_compression_computation_integer(void) { + int level_size = W_PADDED; + int lvl, i, j; + for (lvl = 0; lvl < LEVELS_DWT; ++lvl) { + ccsds_wavelet_transform_2D_int(image_data, level_size, level_size); + level_size /= 2; + } +} + +static void coefficient_scaling(void) { + const int hh_1 = 1, hl_lh = 2, hh_2 = 2, hl_lh_2 = 4, hh_3 = 4, hl_lh_3 = 8, ll_4 = 8; + int i, j; + for (i = H_PADDED>>1; i < H_PADDED; i++) + for (j = W_PADDED>>1; j < W_PADDED; j++) image_data[i][j] *= hh_1; + for (i = 0; i < H_PADDED>>1; i++) + for (j = W_PADDED>>1; j < W_PADDED; j++) image_data[i][j] *= hl_lh; + for (i = H_PADDED>>1; i < H_PADDED; i++) + for (j = 0; j < W_PADDED>>1; j++) image_data[i][j] *= hl_lh; + for (i = H_PADDED>>2; i < H_PADDED>>1; i++) + for (j = W_PADDED>>2; j < W_PADDED>>1; j++) image_data[i][j] *= hh_2; + for (i = 0; i < H_PADDED>>2; i++) + for (j = W_PADDED>>2; j < W_PADDED>>1; j++) image_data[i][j] *= hl_lh_2; + for (i = H_PADDED>>2; i < H_PADDED>>1; i++) + for (j = 0; j < W_PADDED>>2; j++) image_data[i][j] *= hl_lh_2; + for (i = H_PADDED>>3; i < H_PADDED>>2; i++) + for (j = W_PADDED>>3; j < W_PADDED>>2; j++) image_data[i][j] *= hh_3; + for (i = 0; i < H_PADDED>>3; i++) + for (j = W_PADDED>>3; j < W_PADDED>>2; j++) image_data[i][j] *= hl_lh_3; + for (i = H_PADDED>>3; i < H_PADDED>>2; i++) + for (j = 0; j < W_PADDED>>3; j++) image_data[i][j] *= hl_lh_3; + for (i = 0; i < H_PADDED>>3; i++) + for (j = 0; j < W_PADDED>>3; j++) image_data[i][j] *= ll_4; +} + +static void coeff_regroup(void) { + int i, j, p, k, x, y, xb; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) scratch_2d[i][j] = 0; + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 4; p < 8; ++p) + for (k = 4; k < 8; ++k) + scratch_2d[x+p][y+k] = image_data[i+p-4][j+k-4]; + } + for (i = 0; i < H_PADDED>>1; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = i << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 0; p < 4; ++p) + for (k = 4; k < 8; ++k) + scratch_2d[x+p][y+k] = image_data[i+p][j+k-4]; + } + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = 0; j < W_PADDED>>1; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = j << 1; + for (p = 4; p < 8; ++p) + for (k = 0; k < 4; ++k) + scratch_2d[x+p][y+k] = image_data[i+p-4][j+k]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = (j - (W_PADDED>>2)) << 2; + scratch_2d[x+2][y+2] = image_data[i][j]; + scratch_2d[x+2][y+3] = image_data[i][j+1]; + scratch_2d[x+3][y+2] = image_data[i+1][j]; + scratch_2d[x+3][y+3] = image_data[i+1][j+1]; + } + for (i = 0; i < H_PADDED>>2; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = i << 2; y = (j - (W_PADDED>>2)) << 2; + scratch_2d[x][y+2] = image_data[i][j]; + scratch_2d[x][y+3] = image_data[i][j+1]; + scratch_2d[x+1][y+2] = image_data[i+1][j]; + scratch_2d[x+1][y+3] = image_data[i+1][j+1]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = 0; j < W_PADDED>>2; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = j << 2; + scratch_2d[x+2][y] = image_data[i][j]; + scratch_2d[x+2][y+1] = image_data[i][j+1]; + scratch_2d[x+3][y] = image_data[i+1][j]; + scratch_2d[x+3][y+1] = image_data[i+1][j+1]; + } + xb = H_PADDED>>3; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + scratch_2d[((i-xb)<<3)+1][((j-(W_PADDED>>3))<<3)+1] = image_data[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + scratch_2d[i<<3][((j-(W_PADDED>>3))<<3)+1] = image_data[i][j]; + xb = H_PADDED>>3; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + scratch_2d[((i-xb)<<3)+1][j<<3] = image_data[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + scratch_2d[i<<3][j<<3] = image_data[i][j]; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + image_data[i][j] = scratch_2d[i][j]; +} + +static void build_block_string(void) { + int block_h = H_PADDED / BLOCKSIZEIMAGE; + int block_w = W_PADDED / BLOCKSIZEIMAGE; + int i, j, x, y, counter = 0; + for (i = 0; i < block_h; ++i) + for (j = 0; j < block_w; ++j) { + for (x = 0; x < BLOCKSIZEIMAGE; ++x) + for (y = 0; y < BLOCKSIZEIMAGE; ++y) + block_string[counter][x*BLOCKSIZEIMAGE + y] = + image_data[i*BLOCKSIZEIMAGE + x][j*BLOCKSIZEIMAGE + y]; + ++counter; + } +} + +static void generate_input(void) { + unsigned int i, j; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + image_data[i][j] = (int)((i * 173u + j * 91u + 13u) & 0xFFFFu); +} + +// ============================================================ +// Output bitstream statico +// ============================================================ +typedef struct { + uint8_t OutputBitStream[OUTPUT_BUFFER_BYTES]; + unsigned int num_bits; + unsigned int num_total_bytes; +} OutputBitStream_t; + +static HEAPSRAM OutputBitStream_t output_stream; + +static void __attribute__((noinline)) write_to_the_output_segment(long word, unsigned int length) { + int i; + for (i = (int)length - 1; i >= 0; --i) { + output_stream.OutputBitStream[output_stream.num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1) << (7 - output_stream.num_bits)); + output_stream.num_bits++; + if (output_stream.num_bits >= 8u) { + output_stream.num_bits = 0u; + output_stream.num_total_bytes++; + } + } +} + +static unsigned int ceil_log2(unsigned long x) { + unsigned int r = 0; + unsigned long v = 1; + while (v < x) { v <<= 1; r++; } + return r; +} + +static unsigned int bit_size_with_pow2_adjust(long v) { + unsigned long av = (unsigned long)(v < 0 ? -v : v); + if (av == 0) return 0; + unsigned int b = ceil_log2(av); + if ((av & (av - 1)) == 0) b += 1; + return b; +} + +static unsigned long conv_to_twos_complement(int value, unsigned int num_bits) { + unsigned long temp, complement; + short i; + if (num_bits == 1) return 0; + if (value >= 0) return (unsigned long) value; + complement = ~(unsigned long)(-value); + temp = 0; + for (i = 0; i < (short)num_bits; i++) { temp <<= 1; temp++; } + complement &= temp; + complement++; + return complement; +} + +typedef struct { + unsigned int symbol_val; + unsigned int symbol_len; + unsigned int symbol_mapped_pattern; + unsigned int sign; + unsigned int type; +} str_symbol_details_t; + +typedef struct { + long shifted_dc; + unsigned int dc_reminder; + unsigned long mapped_dc; + long max_ac_bit_size; + unsigned long mapped_ac; + unsigned int type_p; + unsigned int tran_b; + unsigned int tran_d; + unsigned int tran_gi; + unsigned int parent_ref_symbol; + unsigned int parent_sym_len; + unsigned int children_ref_symbol; + unsigned int children_sym_len; + unsigned int type_ci[3]; + unsigned int tran_hi[3]; + unsigned int grand_children_ref_symbol[3]; + unsigned int grand_children_sym_len[3]; + unsigned int type_hi[12]; + str_symbol_details_t symbol_block[MAX_SYMBOLS_IN_BLOCK]; +} block_data_t; + +typedef struct { + int start_img_flag; + int end_img_flag; + unsigned int segment_count; + unsigned int bit_depth_dc; + unsigned int bit_depth_ac; + int part_2_flag; + int part_3_flag; + int part_4_flag; + unsigned int pad_rows; +} header_data_t; + +static HEAPSRAM block_data_t block_data[SEGMENT_SIZE]; +static HEAPSRAM header_data_t header_data; + +static void header_output(void) { + write_to_the_output_segment(header_data.start_img_flag, 1); + write_to_the_output_segment(header_data.end_img_flag, 1); + write_to_the_output_segment(header_data.segment_count, 8); + write_to_the_output_segment(header_data.bit_depth_dc, 5); + write_to_the_output_segment(header_data.bit_depth_ac, 5); + write_to_the_output_segment(0, 1); + write_to_the_output_segment(header_data.part_2_flag, 1); + write_to_the_output_segment(header_data.part_3_flag, 1); + write_to_the_output_segment(header_data.part_4_flag, 1); + if (header_data.end_img_flag) { + write_to_the_output_segment(header_data.pad_rows, 3); + write_to_the_output_segment(0, 5); + } +} + +static void coding_quantized_coefficients(unsigned int N) { + long x_min = -(1L << (N - 1)); + long x_max = (1L << (N - 1)) - 1; + unsigned int number_bits = 0, i; + long theta, gamma; + for (i = 0; i < N - 1; i++) number_bits = (number_bits << 1) + 1; + if ((block_data[0].shifted_dc & (1L << (N - 1))) > 0) + block_data[0].shifted_dc = -(long)(((block_data[0].shifted_dc ^ number_bits) & number_bits) + 1); + block_data[0].mapped_dc = (unsigned long) block_data[0].shifted_dc; + for (i = 1; i < SEGMENT_SIZE; i++) { + if ((block_data[i].shifted_dc & (1L << (N - 1))) > 0) + block_data[i].shifted_dc = -(long)(((block_data[i].shifted_dc ^ number_bits) & number_bits) + 1); + theta = (block_data[i-1].shifted_dc - x_min) < (x_max - block_data[i-1].shifted_dc) + ? (block_data[i-1].shifted_dc - x_min) : (x_max - block_data[i-1].shifted_dc); + gamma = block_data[i].shifted_dc - block_data[i-1].shifted_dc; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_dc = (unsigned long)(2 * gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_dc = (unsigned long)(2 * (-gamma) - 1); + else + block_data[i].mapped_dc = (unsigned long)(theta + (gamma < 0 ? -gamma : gamma)); + } +} + +static void dc_encoder(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + unsigned int total_bits, min_bits = 0xFFFFu; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position > 1 ? start_position : 1); i < end_position; i++) + total_bits += (unsigned int)((block_data[i].mapped_dc >> k) + 1) + k; + if (total_bits < min_bits && total_bits < N * GAGGLE_SIZE) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_dc, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_dc >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position > 1 ? start_position : 1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_dc, encode_selection); + } +} + +static void dc_entropy_coding(unsigned int N, unsigned int quantization_factor) { + unsigned int id_length = 0, max_k = 0, i; + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 8) { max_k = 6; id_length = 3; } + else { max_k = 8; id_length = 4; } + dc_encoder(max_k, id_length, N); + if (header_data.bit_depth_ac < quantization_factor) { + unsigned int num_additional_bit_planes = quantization_factor - header_data.bit_depth_ac; + for (i = 0; i < num_additional_bit_planes; i++) { + unsigned int kk; + for (kk = 0; kk < SEGMENT_SIZE; kk++) + write_to_the_output_segment( + (block_data[kk].dc_reminder >> (quantization_factor - i - 1)), 1); + } + } +} + +static unsigned int dc_encoding(void) { + long max_ac_value = 0; + unsigned int quantization_factor; + unsigned int i, j, k_mask; + header_data.bit_depth_dc = 0; + for (i = 0; i < SEGMENT_SIZE; ++i) { + long max_ac_value_block = 0; + int dc_value = block_string[i][0]; + unsigned int dc_bit_size; + if (dc_value < 0) + dc_bit_size = ceil_log2((unsigned long)(-dc_value) + 1UL); + else + dc_bit_size = ceil_log2((unsigned long)(1 + dc_value)) + 1; + if (dc_bit_size > header_data.bit_depth_dc) header_data.bit_depth_dc = dc_bit_size; + for (j = 1; j < BLOCKSIZEIMAGE * BLOCKSIZEIMAGE; ++j) { + long v = block_string[i][j]; + long av = v < 0 ? -v : v; + if (av > (max_ac_value < 0 ? -max_ac_value : max_ac_value)) max_ac_value = v; + if (av > (max_ac_value_block < 0 ? -max_ac_value_block : max_ac_value_block)) max_ac_value_block = v; + } + block_data[i].max_ac_bit_size = (long) bit_size_with_pow2_adjust(max_ac_value_block); + } + header_data.bit_depth_ac = bit_size_with_pow2_adjust(max_ac_value); + header_output(); + if (header_data.bit_depth_dc <= 3) + quantization_factor = 0; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) <= 1) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 3; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) > 10) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 10; + else + quantization_factor = 1 + (header_data.bit_depth_ac >> 1); + k_mask = (1u << quantization_factor) - 1u; + for (i = 0; i < SEGMENT_SIZE; ++i) { + unsigned long new_value_twos = conv_to_twos_complement(block_string[i][0], header_data.bit_depth_dc); + block_data[i].shifted_dc = (long)(new_value_twos >> quantization_factor); + block_data[i].dc_reminder = (unsigned int)(new_value_twos & k_mask); + } + { + unsigned int N = header_data.bit_depth_dc > quantization_factor + ? header_data.bit_depth_dc - quantization_factor : 1; + if (N == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].shifted_dc, 1); + } else { + coding_quantized_coefficients(N); + dc_entropy_coding(N, quantization_factor); + } + } + return quantization_factor; +} + +static void dpcm_ac_mapper(unsigned int N) { + long x_min = 0; + long x_max = (1L << N) - 1; + long theta, gamma; + unsigned int i; + block_data[0].mapped_ac = (unsigned long) block_data[0].max_ac_bit_size; + for (i = 1; i < SEGMENT_SIZE; i++) { + theta = (block_data[i-1].max_ac_bit_size - x_min) < (x_max - block_data[i-1].max_ac_bit_size) + ? (block_data[i-1].max_ac_bit_size - x_min) : (x_max - block_data[i-1].max_ac_bit_size); + gamma = block_data[i].max_ac_bit_size - block_data[i-1].max_ac_bit_size; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_ac = (unsigned long)(2*gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_ac = (unsigned long)(2*(-gamma) - 1); + else + block_data[i].mapped_ac = (unsigned long)(theta + (gamma<0?-gamma:gamma)); + } +} + +static void ac_gaggle_encode(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + long total_bits, min_bits = 0xFFFF; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position>1?start_position:1); i < end_position; i++) + total_bits += (long)((block_data[i].mapped_ac >> k) + 1) + k; + if (total_bits < min_bits && total_bits < (long)(N * GAGGLE_SIZE)) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_ac, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_ac >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position>1?start_position:1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_ac, encode_selection); + } +} + +static void ac_depth_encoder(void) { + unsigned int N = 0, id_length, max_k; + while ((header_data.bit_depth_ac >> N) > 0) N++; + dpcm_ac_mapper(N); + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 5) { max_k = 6; id_length = 3; } + else { max_k = 0; id_length = 0; } + ac_gaggle_encode(max_k, id_length, N); +} + +static void bit_plane_encoding(unsigned int bit_plane_number) { + unsigned int temp_x, temp_y, block_num; + unsigned char symbol; + int temp_value; + unsigned int final_position; + int bit_set_plane = (1 << (bit_plane_number - 1)); + + for (block_num = 0; block_num < SEGMENT_SIZE; ++block_num) { + symbol = 0; + final_position = block_num; + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + + for (int j = 0; j < 3; ++j) { + switch (j) { + case 0: temp_y = 1; break; + case 1: temp_y = BLOCKSIZEIMAGE; break; + case 2: temp_y = BLOCKSIZEIMAGE + 1; break; + default: temp_y = 0; break; + } + temp_x = final_position; + if (!type_of_compression) { + if (((j==0)&&(BITHL3>=bit_plane_number)) || ((j==1)&&(BITLH3>=bit_plane_number)) || ((j==2)&&(BITHH3>=bit_plane_number))) + continue; + } + if ((block_data[block_num].type_p & (1 << (2-j))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_P; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + if (ABSOLUTE(block_string[temp_x][temp_y]) >= (1 << (bit_plane_number-1)) && + ABSOLUTE(block_string[temp_x][temp_y]) < (1 << bit_plane_number)) { + block_data[block_num].type_p += (1 << (2-j)); + block_data[block_num].symbol_block[symbol].symbol_val += 1; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[temp_x][temp_y]); + } + } else { + temp_value = (ABSOLUTE(block_string[temp_x][temp_y]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((j==0)&&(BITHL3=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = 1; break; + } + if (break_flag) break; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = 1; break; + } + if (break_flag) break; + } + } + + if (block_data[block_num].tran_b == 0) { + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 0; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) ++symbol; + + if (block_data[block_num].tran_b == 1) { + for (int k = 0; k < 3; k++) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)&&(BITHL1>=bit_plane_number)) + || ((k==1)&&(BITLH2>=bit_plane_number)&&(BITLH1>=bit_plane_number)) + || ((k==2)&&(BITHH2>=bit_plane_number)&&(BITHH1>=bit_plane_number))) + continue; + } + int break_flag = 0; + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + int jump_flag = 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + jump_flag = 1; + } + if (!jump_flag) { + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = 1; break; + } + } + if (!break_flag) { + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = 1; break; + } + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + } + + for (unsigned int k = 0; k < 3; ++k) { + if ((block_data[block_num].tran_d & (1 << (2-k))) != 0) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + int p = 1; + for (unsigned int i = 0; i < 4; ++i) + if ((block_data[block_num].type_ci[k] & (1 << (3-i))) == 0) { p = 0; break; } + if (p == 0) { + unsigned int counter = 0; + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_CI; + for (unsigned int i = temp_x; i < temp_x+2; ++i) { + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) { + if ((block_data[block_num].type_ci[k] & (1 << (3-counter))) == 0) { + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].type_ci[k] += (1 << (3-counter)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (((block_data[block_num].tran_d & (1 << (2-k))) != 0) && ((block_data[block_num].tran_gi & (1 << (2-k))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_GI; + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + int break_flag = 0; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].tran_gi += (1 << (2-k)); + break_flag = 1; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if (((block_data[block_num].tran_gi & (1 << (2-i3))) != 0) && ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_HI; + int break_flag = 0; + for (unsigned int k = temp_x; k < temp_x+2 && !break_flag; k++) + for (unsigned int p = temp_y; p < temp_y+2; p++) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].tran_hi[i3] += (1 << (3-jx)); + break_flag = 1; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) != 0) { + unsigned int counter = 0; + for (unsigned int k = 0; k < 4; ++k) + if ((block_data[block_num].type_hi[i3*SIZE_TYPE+jx] & (1 << (3-k))) == 0) counter++; + if (counter == 0) { + for (unsigned int k = temp_x; k < temp_x+2; k++) { + for (unsigned int p = temp_y; p < temp_y+2; p++) { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1 0) { + block_data[block_num].type_hi[i3*SIZE_TYPE+jx] += (1 << (3-t)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1symbol_len) { + case 0: return; + case 1: s->symbol_mapped_pattern = s->symbol_val; break; + case 2: s->symbol_mapped_pattern = bit2_pattern[s->symbol_val]; break; + case 3: + s->symbol_mapped_pattern = (s->type == ENUM_TYPE_TRAN_D) ? bit3_pattern_TranD[s->symbol_val] : bit3_pattern[s->symbol_val]; + break; + case 4: + if (s->type == ENUM_TYPE_CI) s->symbol_mapped_pattern = bit4_pattern_TypeCi[s->symbol_val]; + else if (s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ) s->symbol_mapped_pattern = bit4_pattern_TypeHij_TranHi[s->symbol_val]; + break; + default: break; + } +} + +static void coding_options(unsigned int bit_plane_number, unsigned char *code_option_gaggle) { + unsigned int bit_counter_2bits[2] = {0}; + unsigned int bit_counter_3bits[3] = {0}; + unsigned int bit_counter_4bits[4] = {0}; + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + if (s->type == ENUM_NONE) continue; + if (s->symbol_len == 1) { pattern_mapping(s); continue; } + pattern_mapping(s); + if (s->symbol_len == 2) { + switch (s->symbol_mapped_pattern) { + case 0: bit_counter_2bits[0] += 1; break; + case 1: bit_counter_2bits[0] += 2; break; + default: bit_counter_2bits[0] += 3; break; + } + bit_counter_2bits[1] += 2; + } else if (s->symbol_len == 3) { + if (s->symbol_mapped_pattern <= 2) bit_counter_3bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 5) bit_counter_3bits[0] += 5; + else bit_counter_3bits[0] += 6; + if (s->symbol_mapped_pattern <= 1) bit_counter_3bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_3bits[1] += 3; + else bit_counter_3bits[1] += 4; + bit_counter_3bits[2] += 3; + } else if (s->symbol_len == 4) { + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[0] += 7; + else bit_counter_4bits[0] += 8; + if (s->symbol_mapped_pattern <= 1) bit_counter_4bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[1] += 3; + else if (s->symbol_mapped_pattern <= 5) bit_counter_4bits[1] += 4; + else if (s->symbol_mapped_pattern <= 11) bit_counter_4bits[1] += 6; + else bit_counter_4bits[1] += 7; + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[2] += 3; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[2] += 4; + else bit_counter_4bits[2] += 5; + bit_counter_4bits[3] += 4; + } + } + } + code_option_gaggle[0] = (bit_counter_2bits[0] < bit_counter_2bits[1]) ? 0 : 1; + if (bit_counter_3bits[2] <= bit_counter_3bits[0] && bit_counter_3bits[2] <= bit_counter_3bits[1]) code_option_gaggle[1] = 3; + else if (bit_counter_3bits[0] <= bit_counter_3bits[1]) code_option_gaggle[1] = 0; + else code_option_gaggle[1] = 1; + if (bit_counter_4bits[3] <= bit_counter_4bits[1] && bit_counter_4bits[3] <= bit_counter_4bits[0] && bit_counter_4bits[3] <= bit_counter_4bits[2]) code_option_gaggle[2] = 3; + else if (bit_counter_4bits[0] <= bit_counter_4bits[1] && bit_counter_4bits[0] <= bit_counter_4bits[2]) code_option_gaggle[2] = 0; + else if (bit_counter_4bits[1] <= bit_counter_4bits[2]) code_option_gaggle[2] = 1; + else code_option_gaggle[2] = 2; +} + +static void rice_coding(long input_value, unsigned int bit_length, unsigned char *code_option) { + switch (bit_length) { + case 0: return; + case 1: write_to_the_output_segment(input_value, 1); break; + case 2: + if (code_option[0] == 1) write_to_the_output_segment(input_value, 2); + else { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1, 1); } + else write_to_the_output_segment(0, 3); + } + break; + case 3: + if (code_option[1] == 0) { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 5) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-3,2); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,3); } + } else if (code_option[1] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else { + write_to_the_output_segment(0,2); + switch (input_value) { + case 4: write_to_the_output_segment(2,2); break; + case 5: write_to_the_output_segment(3,2); break; + case 6: write_to_the_output_segment(0,2); break; + case 7: write_to_the_output_segment(1,2); break; + } + } + } else write_to_the_output_segment(input_value, 3); + break; + case 4: + if (code_option[2] == 0) { + if (input_value <= 3) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 7) { write_to_the_output_segment(0,5); write_to_the_output_segment(input_value-4,2); } + else { write_to_the_output_segment(0,4); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else if (input_value <= 5) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-2,2); } + else if (input_value <= 11) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-6,3); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 2) { + if (input_value <= 3) write_to_the_output_segment(input_value+4, 3); + else if (input_value <= 7) write_to_the_output_segment(input_value, 4); + else if (input_value <= 11) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-4,3); } + else write_to_the_output_segment(input_value-12, 5); + } else write_to_the_output_segment(input_value, 4); + break; + } +} + +static void gaggle_pass(unsigned int bit_plane_number, unsigned char *code_option_gaggle, int *hit_flag, int pass) { + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + int match = (pass == 1 && s->type == ENUM_TYPE_P) + || (pass == 2 && (s->type == ENUM_TYPE_TRAN_B || s->type == ENUM_TYPE_TRAN_D || s->type == ENUM_TYPE_CI)) + || (pass == 3 && (s->type == ENUM_TRAN_GI || s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ)); + if (!match) continue; + if (s->symbol_len > 1 && !hit_flag[s->symbol_len - 2]) { + hit_flag[s->symbol_len - 2] = 1; + if (s->symbol_len == 2) write_to_the_output_segment(code_option_gaggle[0], 1); + else if (s->symbol_len == 3) write_to_the_output_segment(code_option_gaggle[1], 2); + else if (s->symbol_len == 4) write_to_the_output_segment(code_option_gaggle[2], 2); + } + rice_coding(s->symbol_mapped_pattern, s->symbol_len, code_option_gaggle); + if (s->type == ENUM_TYPE_CI || s->type == ENUM_TYPE_HIJ) { + unsigned int counter = 0, i; + for (i = 0; i < s->symbol_len; ++i) if ((s->symbol_val & (1 << i)) > 0) counter++; + write_to_the_output_segment(s->sign, counter); + } + s->symbol_val = 0; s->symbol_len = 0; s->symbol_mapped_pattern = 0; s->sign = 0; s->type = 0; + } + } +} + +static void ref_bit_end_encode(void) { + unsigned int block_num, i; + for (block_num = 0; block_num < SEGMENT_SIZE; ++block_num) { + if (block_data[block_num].parent_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].parent_ref_symbol, block_data[block_num].parent_sym_len); + block_data[block_num].parent_ref_symbol = 0; block_data[block_num].parent_sym_len = 0; + } + if (block_data[block_num].children_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].children_ref_symbol, block_data[block_num].children_sym_len); + block_data[block_num].children_ref_symbol = 0; block_data[block_num].children_sym_len = 0; + } + for (i = 0; i < 3; ++i) { + if (block_data[block_num].grand_children_sym_len[i] > 0) + write_to_the_output_segment(block_data[block_num].grand_children_ref_symbol[i], block_data[block_num].grand_children_sym_len[i]); + block_data[block_num].grand_children_sym_len[i] = 0; + block_data[block_num].grand_children_ref_symbol[i] = 0; + } + } +} + +static void stages_encoding(unsigned int bit_plane_number) { + unsigned char code_option_gaggle[3] = {0,0,0}; + int hit_flag[3] = {0,0,0}; + coding_options(bit_plane_number, code_option_gaggle); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 1); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 2); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 3); + ref_bit_end_encode(); +} + +static void ac_encoding(unsigned int quantization_factor) { + unsigned int bit_plane, i; + if (header_data.bit_depth_ac == 0) return; + if (header_data.bit_depth_ac == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].max_ac_bit_size, 1); + } else { + ac_depth_encoder(); + } + for (bit_plane = header_data.bit_depth_ac; bit_plane > 0; bit_plane--) { + if ((bit_plane <= quantization_factor) && + (type_of_compression || (quantization_factor > BITLL3 && BITLL3 < bit_plane))) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment((block_data[i].dc_reminder >> (bit_plane - 1)) & 0x01, 1); + } + bit_plane_encoding(bit_plane); + stages_encoding(bit_plane); + } +} + +// ============================================================ +// Cluster test harness (stesso schema di obpmark_2_1_pulpd_seq.c) +// ============================================================ +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_2_2_seq", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + if (rt_core_id() == 0) + run_suite(testcases); + + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int i, qf, checksum; + + generate_input(); + for (i = 0; i < OUTPUT_BUFFER_BYTES; ++i) output_stream.OutputBitStream[i] = 0u; + output_stream.num_bits = 0u; + output_stream.num_total_bytes = 0u; + + header_data.start_img_flag = 1; + header_data.end_img_flag = 1; + header_data.segment_count = 0; + header_data.part_2_flag = 0; + header_data.part_3_flag = 0; + header_data.part_4_flag = 0; + header_data.pad_rows = 0; + + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + + start(); + + dwt2D_compression_computation_integer(); + coefficient_scaling(); + coeff_regroup(); + build_block_string(); + qf = dc_encoding(); + ac_encoding(qf); + + stop(); + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + checksum = 0u; + for (i = 0; i < output_stream.num_total_bytes + 1u; ++i) + checksum += output_stream.OutputBitStream[i]; + + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + unsigned int out0 = (unsigned int)output_stream.OutputBitStream[0]; + + printf("OBPMark 2.2 - CCSDS 122.0 Image Compression\n"); + printf("PULP cluster sequential (1 core, RV32) - Astral @ 50 MHz\n"); + printf("w_size=%u h_size=%u bit_size=%u segment_size=%u\n", + IMAGE_WIDTH, IMAGE_HEIGHT, BIT_SIZE, SEGMENT_SIZE); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" bit_depth_dc : %u\n", header_data.bit_depth_dc); + printf(" bit_depth_ac : %u\n", header_data.bit_depth_ac); + printf(" Output bytes : %u\n", output_stream.num_total_bytes); + printf(" Checksum : %u\n", checksum); + printf(" Output[0] : %u\n", out0); + + if (out0 == EXPECTED_OUTPUT_0 && checksum == EXPECTED_CHECKSUM && + output_stream.num_total_bytes == EXPECTED_TOTAL_BYTES && + header_data.bit_depth_dc == EXPECTED_BIT_DEPTH_DC && + header_data.bit_depth_ac == EXPECTED_BIT_DEPTH_AC) { + result->errors = 0; + printf("RESULT: PASS\n"); + } else { + result->errors = 1; + printf("RESULT: FAIL (expected Output[0]=%u Checksum=%u bytes=%u dc=%u ac=%u)\n", + EXPECTED_OUTPUT_0, EXPECTED_CHECKSUM, EXPECTED_TOTAL_BYTES, + EXPECTED_BIT_DEPTH_DC, EXPECTED_BIT_DEPTH_AC); + } +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq_float/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq_float/Makefile new file mode 100644 index 000000000..8d40fa418 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq_float/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_2_2_pulpd_seq_float.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq_float/obpmark_2_2_pulpd_seq_float.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq_float/obpmark_2_2_pulpd_seq_float.c new file mode 100644 index 000000000..986fbb673 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_2_2_seq_float/obpmark_2_2_pulpd_seq_float.c @@ -0,0 +1,1222 @@ +/* + * Copyright 2024 ETH Zurich and University of Bologna. + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + * + * OBPMark Benchmark #2.2 - CCSDS 122.0 Image Compression - percorso FLOAT + * Sequential (single-core) port for the PULP integer cluster of Astral (RV32) + * + * Stesso algoritmo di obpmark_2_2_host_seq_float.c: DWT 9/7 irreversibile + * in singola precisione (filtri verbatim da benchmark.h), nessun + * coefficient_scaling, type_of_compression = 1. Valori attesi dal + * riferimento host float (seq e SMP, bit-identici): Coeff checksum=398521, + * Block[0] DC=4232, bit_depth_dc=17, bit_depth_ac=12, Output bytes=336, + * Checksum=42236, Output[0]=192. + * + * Vincoli del cluster (dal sequenziale integer): + * - text entro i primi 64 KiB della L2 cluster: pragma O2 limitato al + * file (PULP_CFLAGS resta -O3 per pulp-runtime) e noinline su + * write_to_the_output_segment; + * - footprint dati: i buffer 32x32 sono unioni int/float riusate tra le + * fasi (img: immagine -> campioni float -> coefficienti arrotondati, + * conversioni elemento per elemento in place; scr: uscita row-pass + * float durante la DWT, scratch intero di coeff_regroup), quindi il + * footprint resta quello dell'integer; buffer di uscita a 1 KiB con + * controllo di overflow. + * Fedelta' numerica: fp-contract=off (nessuna fmadd.s) e round_half_away() + * esatto in singola precisione, come nell'host; no-strict-aliasing per + * l'accesso alle unioni int/float. + */ + +#include "pulp.h" +#include +#include + +#pragma GCC optimize ("O2", "fp-contract=off", "no-strict-aliasing") + +#define HEAPSRAM __attribute__((section(".heapsram"))) + +#define IMAGE_WIDTH 32u +#define IMAGE_HEIGHT 32u +#define BIT_SIZE 16u +#define LEVELS_DWT 3u +#define BLOCKSIZEIMAGE 8u +#define SEGMENT_SIZE 16u +#define GAGGLE_SIZE 16u +#define UNCODED_VALUE 0xFFu +#define BITLL3 3u + +#define MAX_SYMBOLS_IN_BLOCK 22u +#define SIZE_TYPE 4u + +#define ENUM_NONE 0 +#define ENUM_TYPE_P 1 +#define ENUM_TYPE_TRAN_B 2 +#define ENUM_TYPE_TRAN_D 3 +#define ENUM_TYPE_CI 4 +#define ENUM_TRAN_GI 5 +#define ENUM_TRAN_HI 6 +#define ENUM_TYPE_HIJ 7 + +#define BITHH1 0 +#define BITLH1 1 +#define BITHL1 1 +#define BITHH2 1 +#define BITLH2 2 +#define BITHL2 2 +#define BITHH3 2 +#define BITHL3 3 +#define BITLH3 3 + +#define ABSOLUTE(x) ((x) < 0 ? -(x) : (x)) +#define SIGN(x) ((x) < 0 ? 1 : 0) + +#define HIGHPASSFILTERSIZE 7 +#define LOWPASSFILTERSIZE 9 + +// verbatim da src/2.2-image_compression/include/benchmark.h +static const float lowpass_filter_cpu[LOWPASSFILTERSIZE] = {0.037828455507,-0.023849465020,-0.110624404418,0.377402855613, 0.852698679009,0.377402855613, -0.110624404418,-0.023849465020, 0.037828455507}; +static const float highpass_filter_cpu[HIGHPASSFILTERSIZE] = {-0.064538882629, 0.040689417609, 0.418092273222,-0.788485616406,0.418092273222,0.040689417609,-0.064538882629}; + +static const int bit2_pattern[4] = {0, 2, 1, 3}; +static const int bit3_pattern[8] = {1, 4, 0, 5, 2, 6, 3, 7}; +static const int bit3_pattern_TranD[8] = {0, 3, 0, 4, 1, 5, 2, 6}; +static const int bit4_pattern_TypeCi[16] = {10, 1, 3, 6, 2, 5, 9, 12, 0, 8, 7, 13, 4, 14, 11, 15}; +static const int bit4_pattern_TypeHij_TranHi[16] = {0, 1, 3, 6, 2, 5, 9, 11, 0, 8, 7, 12, 4, 13, 10, 14}; + +static const int type_of_compression = 1; // 1 = float path + +#define W_PADDED IMAGE_WIDTH +#define H_PADDED IMAGE_HEIGHT +#define TOTAL_BLOCKS ((W_PADDED / BLOCKSIZEIMAGE) * (H_PADDED / BLOCKSIZEIMAGE)) // 16 + +#define OUTPUT_BUFFER_BYTES 1024u + +#define EXPECTED_COEFF_CHECKSUM 398521 +#define EXPECTED_BLOCK0_DC 4232 +#define EXPECTED_OUTPUT_0 192u +#define EXPECTED_CHECKSUM 42236u +#define EXPECTED_TOTAL_BYTES 336u +#define EXPECTED_BIT_DEPTH_DC 17u +#define EXPECTED_BIT_DEPTH_AC 12u + +// ============================================================ +// Buffer HEAPSRAM: unioni int/float riusate tra le fasi +// ============================================================ +typedef union { + int i[H_PADDED][W_PADDED]; + float f[H_PADDED][W_PADDED]; +} buf2d_t; + +static HEAPSRAM buf2d_t img; // immagine -> campioni float -> coefficienti +static HEAPSRAM buf2d_t scr; // uscita row-pass (float) / scratch coeff_regroup (int) +static HEAPSRAM int block_string[TOTAL_BLOCKS][BLOCKSIZEIMAGE * BLOCKSIZEIMAGE]; +static HEAPSRAM float col_in_f[H_PADDED]; +static HEAPSRAM float col_out_f[H_PADDED]; + +// ============================================================ +// DWT float (identica all'host) +// ============================================================ +static void ccsds_wavelet_transform_1D_float(const float *A, float *B, int size) { + int full_size = size * 2; + int hi_start = -(LOWPASSFILTERSIZE / 2); + int hi_end = LOWPASSFILTERSIZE / 2; + int gi_start = -(HIGHPASSFILTERSIZE / 2); + int gi_end = HIGHPASSFILTERSIZE / 2; + int i; + + for (i = 0; i < size; ++i) { + float sum_value_low = 0.0f; + for (int hi = hi_start; hi < hi_end + 1; ++hi) { + int x_position = (2 * i) + hi; + if (x_position < 0) + x_position = -x_position; + else if (x_position > full_size - 1) + x_position = full_size - 1 - (x_position - (full_size - 1)); + sum_value_low += lowpass_filter_cpu[hi + hi_end] * A[x_position]; + } + B[i] = sum_value_low; + + float sum_value_high = 0.0f; + for (int gi = gi_start; gi < gi_end + 1; ++gi) { + int x_position = (2 * i) + gi + 1; + if (x_position < 0) + x_position = -x_position; + else if (x_position > full_size - 1) + x_position = full_size - 1 - (x_position - (full_size - 1)); + sum_value_high += highpass_filter_cpu[gi + gi_end] * A[x_position]; + } + B[i + size] = sum_value_high; + } +} + +static void ccsds_wavelet_transform_2D_float(int w, int h) { + int i, x; + for (i = 0; i < h; ++i) + ccsds_wavelet_transform_1D_float(img.f[i], scr.f[i], w / 2); + for (i = 0; i < w; ++i) { + for (x = 0; x < h; ++x) col_in_f[x] = scr.f[x][i]; + ccsds_wavelet_transform_1D_float(col_in_f, col_out_f, h / 2); + for (x = 0; x < h; ++x) img.f[x][i] = col_out_f[x]; + } +} + +static inline int round_half_away(float x) { + int t = (int)x; + float f = x - (float)t; + if (x >= 0.0f) return t + (f >= 0.5f ? 1 : 0); + else return t - (f <= -0.5f ? 1 : 0); +} + +static void dwt2D_compression_computation_float(void) { + int level_size = W_PADDED; + int lvl, i, j; + + for (i = 0; i < H_PADDED; ++i) // int -> float, in place + for (j = 0; j < W_PADDED; ++j) { + int v = img.i[i][j]; + img.f[i][j] = (float)v; + } + + for (lvl = 0; lvl < (int)LEVELS_DWT; ++lvl) { + ccsds_wavelet_transform_2D_float(level_size, level_size); + level_size /= 2; + } + + for (i = 0; i < H_PADDED; ++i) // float -> int, in place + for (j = 0; j < W_PADDED; ++j) { + float v = img.f[i][j]; + img.i[i][j] = round_half_away(v); + } +} + +// ============================================================ +// Riorganizzazione dei coefficienti (su img.i, scratch scr.i) +// ============================================================ +static void coeff_regroup(void) { + int i, j, p, k, x, y, xb; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) scr.i[i][j] = 0; + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 4; p < 8; ++p) + for (k = 4; k < 8; ++k) + scr.i[x+p][y+k] = img.i[i+p-4][j+k-4]; + } + for (i = 0; i < H_PADDED>>1; i += 4) + for (j = W_PADDED>>1; j < W_PADDED; j += 4) { + x = i << 1; y = (j - (W_PADDED>>1)) << 1; + for (p = 0; p < 4; ++p) + for (k = 4; k < 8; ++k) + scr.i[x+p][y+k] = img.i[i+p][j+k-4]; + } + for (i = H_PADDED>>1; i < H_PADDED; i += 4) + for (j = 0; j < W_PADDED>>1; j += 4) { + x = (i - (H_PADDED>>1)) << 1; y = j << 1; + for (p = 4; p < 8; ++p) + for (k = 0; k < 4; ++k) + scr.i[x+p][y+k] = img.i[i+p-4][j+k]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = (j - (W_PADDED>>2)) << 2; + scr.i[x+2][y+2] = img.i[i][j]; + scr.i[x+2][y+3] = img.i[i][j+1]; + scr.i[x+3][y+2] = img.i[i+1][j]; + scr.i[x+3][y+3] = img.i[i+1][j+1]; + } + for (i = 0; i < H_PADDED>>2; i += 2) + for (j = W_PADDED>>2; j < W_PADDED>>1; j += 2) { + x = i << 2; y = (j - (W_PADDED>>2)) << 2; + scr.i[x][y+2] = img.i[i][j]; + scr.i[x][y+3] = img.i[i][j+1]; + scr.i[x+1][y+2] = img.i[i+1][j]; + scr.i[x+1][y+3] = img.i[i+1][j+1]; + } + for (i = H_PADDED>>2; i < H_PADDED>>1; i += 2) + for (j = 0; j < W_PADDED>>2; j += 2) { + x = (i - (H_PADDED>>2)) << 2; y = j << 2; + scr.i[x+2][y] = img.i[i][j]; + scr.i[x+2][y+1] = img.i[i][j+1]; + scr.i[x+3][y] = img.i[i+1][j]; + scr.i[x+3][y+1] = img.i[i+1][j+1]; + } + xb = H_PADDED>>3; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + scr.i[((i-xb)<<3)+1][((j-(W_PADDED>>3))<<3)+1] = img.i[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = W_PADDED>>3; j < W_PADDED>>2; ++j) + scr.i[i<<3][((j-(W_PADDED>>3))<<3)+1] = img.i[i][j]; + for (i = H_PADDED>>3; i < H_PADDED>>2; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + scr.i[((i-xb)<<3)+1][j<<3] = img.i[i][j]; + for (i = 0; i < H_PADDED>>3; ++i) + for (j = 0; j < W_PADDED>>3; ++j) + scr.i[i<<3][j<<3] = img.i[i][j]; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + img.i[i][j] = scr.i[i][j]; +} + +static void build_block_string(void) { + int block_h = H_PADDED / BLOCKSIZEIMAGE; + int block_w = W_PADDED / BLOCKSIZEIMAGE; + int i, j, x, y, counter = 0; + for (i = 0; i < block_h; ++i) + for (j = 0; j < block_w; ++j) { + for (x = 0; x < (int)BLOCKSIZEIMAGE; ++x) + for (y = 0; y < (int)BLOCKSIZEIMAGE; ++y) + block_string[counter][x*BLOCKSIZEIMAGE + y] = + img.i[i*BLOCKSIZEIMAGE + x][j*BLOCKSIZEIMAGE + y]; + ++counter; + } +} + +static void generate_input(void) { + unsigned int i, j; + for (i = 0; i < H_PADDED; i++) + for (j = 0; j < W_PADDED; j++) + img.i[i][j] = (int)((i * 173u + j * 91u + 13u) & 0xFFFFu); +} + +// ============================================================ +// Output bitstream con controllo di overflow +// ============================================================ +typedef struct { + uint8_t OutputBitStream[OUTPUT_BUFFER_BYTES]; + unsigned int num_bits; + unsigned int num_total_bytes; + unsigned int overflow; +} OutputBitStream_t; + +static HEAPSRAM OutputBitStream_t output_stream; + +static void __attribute__((noinline)) write_to_the_output_segment(long word, unsigned int length) { + int i; + if (output_stream.num_total_bytes + (length >> 3) + 2u > OUTPUT_BUFFER_BYTES) { + output_stream.overflow = 1u; + return; + } + for (i = (int)length - 1; i >= 0; --i) { + output_stream.OutputBitStream[output_stream.num_total_bytes] |= + (uint8_t)(((word >> i) & 0x1) << (7 - output_stream.num_bits)); + output_stream.num_bits++; + if (output_stream.num_bits >= 8u) { + output_stream.num_bits = 0u; + output_stream.num_total_bytes++; + } + } +} + +static unsigned int ceil_log2(unsigned long x) { + unsigned int r = 0; + unsigned long v = 1; + while (v < x) { v <<= 1; r++; } + return r; +} + +static unsigned int bit_size_with_pow2_adjust(long v) { + unsigned long av = (unsigned long)(v < 0 ? -v : v); + if (av == 0) return 0; + unsigned int b = ceil_log2(av); + if ((av & (av - 1)) == 0) b += 1; + return b; +} + +static unsigned long conv_to_twos_complement(int value, unsigned int num_bits) { + unsigned long temp, complement; + short i; + if (num_bits == 1) return 0; + if (value >= 0) return (unsigned long) value; + complement = ~(unsigned long)(-value); + temp = 0; + for (i = 0; i < (short)num_bits; i++) { temp <<= 1; temp++; } + complement &= temp; + complement++; + return complement; +} + +typedef struct { + unsigned int symbol_val; + unsigned int symbol_len; + unsigned int symbol_mapped_pattern; + unsigned int sign; + unsigned int type; +} str_symbol_details_t; + +typedef struct { + long shifted_dc; + unsigned int dc_reminder; + unsigned long mapped_dc; + long max_ac_bit_size; + unsigned long mapped_ac; + unsigned int type_p; + unsigned int tran_b; + unsigned int tran_d; + unsigned int tran_gi; + unsigned int parent_ref_symbol; + unsigned int parent_sym_len; + unsigned int children_ref_symbol; + unsigned int children_sym_len; + unsigned int type_ci[3]; + unsigned int tran_hi[3]; + unsigned int grand_children_ref_symbol[3]; + unsigned int grand_children_sym_len[3]; + unsigned int type_hi[12]; + str_symbol_details_t symbol_block[MAX_SYMBOLS_IN_BLOCK]; +} block_data_t; + +typedef struct { + int start_img_flag; + int end_img_flag; + unsigned int segment_count; + unsigned int bit_depth_dc; + unsigned int bit_depth_ac; + int part_2_flag; + int part_3_flag; + int part_4_flag; + unsigned int pad_rows; +} header_data_t; + +static HEAPSRAM block_data_t block_data[SEGMENT_SIZE]; +static HEAPSRAM header_data_t header_data; + +static void header_output(void) { + write_to_the_output_segment(header_data.start_img_flag, 1); + write_to_the_output_segment(header_data.end_img_flag, 1); + write_to_the_output_segment(header_data.segment_count, 8); + write_to_the_output_segment(header_data.bit_depth_dc, 5); + write_to_the_output_segment(header_data.bit_depth_ac, 5); + write_to_the_output_segment(0, 1); + write_to_the_output_segment(header_data.part_2_flag, 1); + write_to_the_output_segment(header_data.part_3_flag, 1); + write_to_the_output_segment(header_data.part_4_flag, 1); + if (header_data.end_img_flag) { + write_to_the_output_segment(header_data.pad_rows, 3); + write_to_the_output_segment(0, 5); + } +} + +static void coding_quantized_coefficients(unsigned int N) { + long x_min = -(1L << (N - 1)); + long x_max = (1L << (N - 1)) - 1; + unsigned int number_bits = 0, i; + long theta, gamma; + for (i = 0; i < N - 1; i++) number_bits = (number_bits << 1) + 1; + if ((block_data[0].shifted_dc & (1L << (N - 1))) > 0) + block_data[0].shifted_dc = -(long)(((block_data[0].shifted_dc ^ number_bits) & number_bits) + 1); + block_data[0].mapped_dc = (unsigned long) block_data[0].shifted_dc; + for (i = 1; i < SEGMENT_SIZE; i++) { + if ((block_data[i].shifted_dc & (1L << (N - 1))) > 0) + block_data[i].shifted_dc = -(long)(((block_data[i].shifted_dc ^ number_bits) & number_bits) + 1); + theta = (block_data[i-1].shifted_dc - x_min) < (x_max - block_data[i-1].shifted_dc) + ? (block_data[i-1].shifted_dc - x_min) : (x_max - block_data[i-1].shifted_dc); + gamma = block_data[i].shifted_dc - block_data[i-1].shifted_dc; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_dc = (unsigned long)(2 * gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_dc = (unsigned long)(2 * (-gamma) - 1); + else + block_data[i].mapped_dc = (unsigned long)(theta + (gamma < 0 ? -gamma : gamma)); + } +} + +static void dc_encoder(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + unsigned int total_bits, min_bits = 0xFFFFu; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position > 1 ? start_position : 1); i < end_position; i++) + total_bits += (unsigned int)((block_data[i].mapped_dc >> k) + 1) + k; + if (total_bits < min_bits && total_bits < N * GAGGLE_SIZE) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_dc, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_dc >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position > 1 ? start_position : 1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_dc, encode_selection); + } +} + +static void dc_entropy_coding(unsigned int N, unsigned int quantization_factor) { + unsigned int id_length = 0, max_k = 0, i; + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 8) { max_k = 6; id_length = 3; } + else { max_k = 8; id_length = 4; } + dc_encoder(max_k, id_length, N); + if (header_data.bit_depth_ac < quantization_factor) { + unsigned int num_additional_bit_planes = quantization_factor - header_data.bit_depth_ac; + for (i = 0; i < num_additional_bit_planes; i++) { + unsigned int kk; + for (kk = 0; kk < SEGMENT_SIZE; kk++) + write_to_the_output_segment( + (block_data[kk].dc_reminder >> (quantization_factor - i - 1)), 1); + } + } +} + +static unsigned int dc_encoding(void) { + long max_ac_value = 0; + unsigned int quantization_factor; + unsigned int i, j, k_mask; + header_data.bit_depth_dc = 0; + for (i = 0; i < SEGMENT_SIZE; ++i) { + long max_ac_value_block = 0; + int dc_value = block_string[i][0]; + unsigned int dc_bit_size; + if (dc_value < 0) + dc_bit_size = ceil_log2((unsigned long)(-dc_value) + 1UL); + else + dc_bit_size = ceil_log2((unsigned long)(1 + dc_value)) + 1; + if (dc_bit_size > header_data.bit_depth_dc) header_data.bit_depth_dc = dc_bit_size; + for (j = 1; j < BLOCKSIZEIMAGE * BLOCKSIZEIMAGE; ++j) { + long v = block_string[i][j]; + long av = v < 0 ? -v : v; + if (av > (max_ac_value < 0 ? -max_ac_value : max_ac_value)) max_ac_value = v; + if (av > (max_ac_value_block < 0 ? -max_ac_value_block : max_ac_value_block)) max_ac_value_block = v; + } + block_data[i].max_ac_bit_size = (long) bit_size_with_pow2_adjust(max_ac_value_block); + } + header_data.bit_depth_ac = bit_size_with_pow2_adjust(max_ac_value); + header_output(); + if (header_data.bit_depth_dc <= 3) + quantization_factor = 0; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) <= 1) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 3; + else if (((long)header_data.bit_depth_dc - (long)(1 + (header_data.bit_depth_ac >> 1)) > 10) && header_data.bit_depth_dc > 3) + quantization_factor = header_data.bit_depth_dc - 10; + else + quantization_factor = 1 + (header_data.bit_depth_ac >> 1); + k_mask = (1u << quantization_factor) - 1u; + for (i = 0; i < SEGMENT_SIZE; ++i) { + unsigned long new_value_twos = conv_to_twos_complement(block_string[i][0], header_data.bit_depth_dc); + block_data[i].shifted_dc = (long)(new_value_twos >> quantization_factor); + block_data[i].dc_reminder = (unsigned int)(new_value_twos & k_mask); + } + { + unsigned int N = header_data.bit_depth_dc > quantization_factor + ? header_data.bit_depth_dc - quantization_factor : 1; + if (N == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].shifted_dc, 1); + } else { + coding_quantized_coefficients(N); + dc_entropy_coding(N, quantization_factor); + } + } + return quantization_factor; +} + +static void dpcm_ac_mapper(unsigned int N) { + long x_min = 0; + long x_max = (1L << N) - 1; + long theta, gamma; + unsigned int i; + block_data[0].mapped_ac = (unsigned long) block_data[0].max_ac_bit_size; + for (i = 1; i < SEGMENT_SIZE; i++) { + theta = (block_data[i-1].max_ac_bit_size - x_min) < (x_max - block_data[i-1].max_ac_bit_size) + ? (block_data[i-1].max_ac_bit_size - x_min) : (x_max - block_data[i-1].max_ac_bit_size); + gamma = block_data[i].max_ac_bit_size - block_data[i-1].max_ac_bit_size; + if (0 <= gamma && gamma <= theta) + block_data[i].mapped_ac = (unsigned long)(2*gamma); + else if (-theta <= gamma && gamma < 0) + block_data[i].mapped_ac = (unsigned long)(2*(-gamma) - 1); + else + block_data[i].mapped_ac = (unsigned long)(theta + (gamma<0?-gamma:gamma)); + } +} + +static void ac_gaggle_encode(unsigned int max_k, unsigned int id_length, unsigned int N) { + unsigned int start_position = 0, end_position = GAGGLE_SIZE; + unsigned int encode_selection = UNCODED_VALUE; + long total_bits, min_bits = 0xFFFF; + unsigned int k, i; + for (k = 0; k <= max_k; ++k) { + total_bits = (start_position == 0) ? N : 0; + for (i = (start_position>1?start_position:1); i < end_position; i++) + total_bits += (long)((block_data[i].mapped_ac >> k) + 1) + k; + if (total_bits < min_bits && total_bits < (long)(N * GAGGLE_SIZE)) { + min_bits = total_bits; + encode_selection = k; + } + } + write_to_the_output_segment((long)encode_selection, id_length); + for (i = start_position; i < start_position + GAGGLE_SIZE; i++) { + if (encode_selection == UNCODED_VALUE || i == 0) + write_to_the_output_segment((long)block_data[i].mapped_ac, N); + else + write_to_the_output_segment(1, (unsigned int)((block_data[i].mapped_ac >> encode_selection) + 1)); + } + if (encode_selection != UNCODED_VALUE) { + for (i = (start_position>1?start_position:1); i < start_position + GAGGLE_SIZE; i++) + write_to_the_output_segment((long)block_data[i].mapped_ac, encode_selection); + } +} + +static void ac_depth_encoder(void) { + unsigned int N = 0, id_length, max_k; + while ((header_data.bit_depth_ac >> N) > 0) N++; + dpcm_ac_mapper(N); + if (N == 2) { max_k = 0; id_length = 1; } + else if (N <= 4) { max_k = 2; id_length = 2; } + else if (N <= 5) { max_k = 6; id_length = 3; } + else { max_k = 0; id_length = 0; } + ac_gaggle_encode(max_k, id_length, N); +} + +static void bit_plane_encoding(unsigned int bit_plane_number) { + unsigned int temp_x, temp_y, block_num; + unsigned char symbol; + int temp_value; + unsigned int final_position; + int bit_set_plane = (1 << (bit_plane_number - 1)); + + for (block_num = 0; block_num < SEGMENT_SIZE; ++block_num) { + symbol = 0; + final_position = block_num; + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + + for (int j = 0; j < 3; ++j) { + switch (j) { + case 0: temp_y = 1; break; + case 1: temp_y = BLOCKSIZEIMAGE; break; + case 2: temp_y = BLOCKSIZEIMAGE + 1; break; + default: temp_y = 0; break; + } + temp_x = final_position; + if (!type_of_compression) { + if (((j==0)&&(BITHL3>=bit_plane_number)) || ((j==1)&&(BITLH3>=bit_plane_number)) || ((j==2)&&(BITHH3>=bit_plane_number))) + continue; + } + if ((block_data[block_num].type_p & (1 << (2-j))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_P; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + if (ABSOLUTE(block_string[temp_x][temp_y]) >= (1 << (bit_plane_number-1)) && + ABSOLUTE(block_string[temp_x][temp_y]) < (1 << bit_plane_number)) { + block_data[block_num].type_p += (1 << (2-j)); + block_data[block_num].symbol_block[symbol].symbol_val += 1; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[temp_x][temp_y]); + } + } else { + temp_value = (ABSOLUTE(block_string[temp_x][temp_y]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((j==0)&&(BITHL3=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = 1; break; + } + if (break_flag) break; + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_b = 1; + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 1; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + symbol++; break_flag = 1; break; + } + if (break_flag) break; + } + } + + if (block_data[block_num].tran_b == 0) { + block_data[block_num].symbol_block[symbol].symbol_len = 1; + block_data[block_num].symbol_block[symbol].symbol_val = 0; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_B; + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) ++symbol; + + if (block_data[block_num].tran_b == 1) { + for (int k = 0; k < 3; k++) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)&&(BITHL1>=bit_plane_number)) + || ((k==1)&&(BITLH2>=bit_plane_number)&&(BITLH1>=bit_plane_number)) + || ((k==2)&&(BITHH2>=bit_plane_number)&&(BITHH1>=bit_plane_number))) + continue; + } + int break_flag = 0; + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + int jump_flag = 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + jump_flag = 1; + } + if (!jump_flag) { + for (unsigned int i = temp_x; i < temp_x+2 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = 1; break; + } + } + if (!break_flag) { + if (!type_of_compression) { + if (((k==0)&&(BITHL1>=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].tran_d += (1 << (2-k)); + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + break_flag = 1; break; + } + if ((block_data[block_num].tran_d & (1 << (2-k))) == 0) { + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_TRAN_D; + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + } + + for (unsigned int k = 0; k < 3; ++k) { + if ((block_data[block_num].tran_d & (1 << (2-k))) != 0) { + if (!type_of_compression) { + if (((k==0)&&(BITHL2>=bit_plane_number)) || ((k==1)&&(BITLH2>=bit_plane_number)) || ((k==2)&&(BITHH2>=bit_plane_number))) + continue; + } + temp_x = (k>=1?1:0)*2; + temp_y = (k!=1?1:0)*2; + int p = 1; + for (unsigned int i = 0; i < 4; ++i) + if ((block_data[block_num].type_ci[k] & (1 << (3-i))) == 0) { p = 0; break; } + if (p == 0) { + unsigned int counter = 0; + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + block_data[block_num].symbol_block[symbol].type = ENUM_TYPE_CI; + for (unsigned int i = temp_x; i < temp_x+2; ++i) { + for (unsigned int jj = temp_y; jj < temp_y+2; ++jj) { + if ((block_data[block_num].type_ci[k] & (1 << (3-counter))) == 0) { + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].type_ci[k] += (1 << (3-counter)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2 0 ? 1 : 0; + if (!type_of_compression) { + if (((k==0)&&(BITHL2=bit_plane_number)) || ((k==1)&&(BITLH1>=bit_plane_number)) || ((k==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (((block_data[block_num].tran_d & (1 << (2-k))) != 0) && ((block_data[block_num].tran_gi & (1 << (2-k))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_GI; + temp_x = (k>=1?1:0)*4; + temp_y = (k!=1?1:0)*4; + int break_flag = 0; + for (unsigned int i = temp_x; i < temp_x+4 && !break_flag; ++i) + for (unsigned int jj = temp_y; jj < temp_y+4; ++jj) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(i*BLOCKSIZEIMAGE)+jj])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].tran_gi += (1 << (2-k)); + break_flag = 1; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + if (block_data[block_num].symbol_block[symbol].symbol_len != 0) symbol++; + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if (((block_data[block_num].tran_gi & (1 << (2-i3))) != 0) && ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) == 0)) { + block_data[block_num].symbol_block[symbol].type = ENUM_TRAN_HI; + int break_flag = 0; + for (unsigned int k = temp_x; k < temp_x+2 && !break_flag; k++) + for (unsigned int p = temp_y; p < temp_y+2; p++) + if ((bit_set_plane & ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p])) > 0) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].tran_hi[i3] += (1 << (3-jx)); + break_flag = 1; break; + } + if (!break_flag) { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } + } + } + + for (unsigned int i3 = 0; i3 < 3; i3++) { + if (!type_of_compression) { + if (((i3==0)&&(BITHL1>=bit_plane_number)) || ((i3==1)&&(BITLH1>=bit_plane_number)) || ((i3==2)&&(BITHH1>=bit_plane_number))) + continue; + } + for (unsigned int jx = 0; jx < 4; jx++) { + temp_x = (i3>=1?1:0)*4 + (jx>=2?1:0)*2; + temp_y = (i3!=1?1:0)*4 + (jx%2)*2; + if ((block_data[block_num].tran_hi[i3] & (1 << (3-jx))) != 0) { + unsigned int counter = 0; + for (unsigned int k = 0; k < 4; ++k) + if ((block_data[block_num].type_hi[i3*SIZE_TYPE+jx] & (1 << (3-k))) == 0) counter++; + if (counter == 0) { + for (unsigned int k = temp_x; k < temp_x+2; k++) { + for (unsigned int p = temp_y; p < temp_y+2; p++) { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1 0) { + block_data[block_num].type_hi[i3*SIZE_TYPE+jx] += (1 << (3-t)); + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + block_data[block_num].symbol_block[symbol].symbol_val++; + block_data[block_num].symbol_block[symbol].sign <<= 1; + block_data[block_num].symbol_block[symbol].sign += SIGN(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]); + } else { + block_data[block_num].symbol_block[symbol].symbol_len++; + block_data[block_num].symbol_block[symbol].symbol_val <<= 1; + } + } else { + temp_value = (ABSOLUTE(block_string[final_position][(k*BLOCKSIZEIMAGE)+p]) & bit_set_plane) > 0 ? 1 : 0; + if (!type_of_compression) { + if (((i3==0)&&(BITHL1symbol_len) { + case 0: return; + case 1: s->symbol_mapped_pattern = s->symbol_val; break; + case 2: s->symbol_mapped_pattern = bit2_pattern[s->symbol_val]; break; + case 3: + s->symbol_mapped_pattern = (s->type == ENUM_TYPE_TRAN_D) ? bit3_pattern_TranD[s->symbol_val] : bit3_pattern[s->symbol_val]; + break; + case 4: + if (s->type == ENUM_TYPE_CI) s->symbol_mapped_pattern = bit4_pattern_TypeCi[s->symbol_val]; + else if (s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ) s->symbol_mapped_pattern = bit4_pattern_TypeHij_TranHi[s->symbol_val]; + break; + default: break; + } +} + +static void coding_options(unsigned int bit_plane_number, unsigned char *code_option_gaggle) { + unsigned int bit_counter_2bits[2] = {0}; + unsigned int bit_counter_3bits[3] = {0}; + unsigned int bit_counter_4bits[4] = {0}; + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + if (s->type == ENUM_NONE) continue; + if (s->symbol_len == 1) { pattern_mapping(s); continue; } + pattern_mapping(s); + if (s->symbol_len == 2) { + switch (s->symbol_mapped_pattern) { + case 0: bit_counter_2bits[0] += 1; break; + case 1: bit_counter_2bits[0] += 2; break; + default: bit_counter_2bits[0] += 3; break; + } + bit_counter_2bits[1] += 2; + } else if (s->symbol_len == 3) { + if (s->symbol_mapped_pattern <= 2) bit_counter_3bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 5) bit_counter_3bits[0] += 5; + else bit_counter_3bits[0] += 6; + if (s->symbol_mapped_pattern <= 1) bit_counter_3bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_3bits[1] += 3; + else bit_counter_3bits[1] += 4; + bit_counter_3bits[2] += 3; + } else if (s->symbol_len == 4) { + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[0] += s->symbol_mapped_pattern + 1; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[0] += 7; + else bit_counter_4bits[0] += 8; + if (s->symbol_mapped_pattern <= 1) bit_counter_4bits[1] += 2; + else if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[1] += 3; + else if (s->symbol_mapped_pattern <= 5) bit_counter_4bits[1] += 4; + else if (s->symbol_mapped_pattern <= 11) bit_counter_4bits[1] += 6; + else bit_counter_4bits[1] += 7; + if (s->symbol_mapped_pattern <= 3) bit_counter_4bits[2] += 3; + else if (s->symbol_mapped_pattern <= 7) bit_counter_4bits[2] += 4; + else bit_counter_4bits[2] += 5; + bit_counter_4bits[3] += 4; + } + } + } + code_option_gaggle[0] = (bit_counter_2bits[0] < bit_counter_2bits[1]) ? 0 : 1; + if (bit_counter_3bits[2] <= bit_counter_3bits[0] && bit_counter_3bits[2] <= bit_counter_3bits[1]) code_option_gaggle[1] = 3; + else if (bit_counter_3bits[0] <= bit_counter_3bits[1]) code_option_gaggle[1] = 0; + else code_option_gaggle[1] = 1; + if (bit_counter_4bits[3] <= bit_counter_4bits[1] && bit_counter_4bits[3] <= bit_counter_4bits[0] && bit_counter_4bits[3] <= bit_counter_4bits[2]) code_option_gaggle[2] = 3; + else if (bit_counter_4bits[0] <= bit_counter_4bits[1] && bit_counter_4bits[0] <= bit_counter_4bits[2]) code_option_gaggle[2] = 0; + else if (bit_counter_4bits[1] <= bit_counter_4bits[2]) code_option_gaggle[2] = 1; + else code_option_gaggle[2] = 2; +} + +static void rice_coding(long input_value, unsigned int bit_length, unsigned char *code_option) { + switch (bit_length) { + case 0: return; + case 1: write_to_the_output_segment(input_value, 1); break; + case 2: + if (code_option[0] == 1) write_to_the_output_segment(input_value, 2); + else { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1, 1); } + else write_to_the_output_segment(0, 3); + } + break; + case 3: + if (code_option[1] == 0) { + if (input_value <= 2) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 5) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-3,2); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,3); } + } else if (code_option[1] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else { + write_to_the_output_segment(0,2); + switch (input_value) { + case 4: write_to_the_output_segment(2,2); break; + case 5: write_to_the_output_segment(3,2); break; + case 6: write_to_the_output_segment(0,2); break; + case 7: write_to_the_output_segment(1,2); break; + } + } + } else write_to_the_output_segment(input_value, 3); + break; + case 4: + if (code_option[2] == 0) { + if (input_value <= 3) { write_to_the_output_segment(0, input_value); write_to_the_output_segment(1,1); } + else if (input_value <= 7) { write_to_the_output_segment(0,5); write_to_the_output_segment(input_value-4,2); } + else { write_to_the_output_segment(0,4); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 1) { + if (input_value <= 1) write_to_the_output_segment(input_value+2, 2); + else if (input_value <= 3) write_to_the_output_segment(input_value, 3); + else if (input_value <= 5) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-2,2); } + else if (input_value <= 11) { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value-6,3); } + else { write_to_the_output_segment(0,3); write_to_the_output_segment(input_value,4); } + } else if (code_option[2] == 2) { + if (input_value <= 3) write_to_the_output_segment(input_value+4, 3); + else if (input_value <= 7) write_to_the_output_segment(input_value, 4); + else if (input_value <= 11) { write_to_the_output_segment(0,2); write_to_the_output_segment(input_value-4,3); } + else write_to_the_output_segment(input_value-12, 5); + } else write_to_the_output_segment(input_value, 4); + break; + } +} + +static void gaggle_pass(unsigned int bit_plane_number, unsigned char *code_option_gaggle, int *hit_flag, int pass) { + unsigned int block_num, symbol_id; + for (block_num = 0; block_num < SEGMENT_SIZE; block_num++) { + if (block_data[block_num].max_ac_bit_size < (long)bit_plane_number) continue; + for (symbol_id = 0; symbol_id < MAX_SYMBOLS_IN_BLOCK; symbol_id++) { + str_symbol_details_t *s = &block_data[block_num].symbol_block[symbol_id]; + int match = (pass == 1 && s->type == ENUM_TYPE_P) + || (pass == 2 && (s->type == ENUM_TYPE_TRAN_B || s->type == ENUM_TYPE_TRAN_D || s->type == ENUM_TYPE_CI)) + || (pass == 3 && (s->type == ENUM_TRAN_GI || s->type == ENUM_TRAN_HI || s->type == ENUM_TYPE_HIJ)); + if (!match) continue; + if (s->symbol_len > 1 && !hit_flag[s->symbol_len - 2]) { + hit_flag[s->symbol_len - 2] = 1; + if (s->symbol_len == 2) write_to_the_output_segment(code_option_gaggle[0], 1); + else if (s->symbol_len == 3) write_to_the_output_segment(code_option_gaggle[1], 2); + else if (s->symbol_len == 4) write_to_the_output_segment(code_option_gaggle[2], 2); + } + rice_coding(s->symbol_mapped_pattern, s->symbol_len, code_option_gaggle); + if (s->type == ENUM_TYPE_CI || s->type == ENUM_TYPE_HIJ) { + unsigned int counter = 0, i; + for (i = 0; i < s->symbol_len; ++i) if ((s->symbol_val & (1 << i)) > 0) counter++; + write_to_the_output_segment(s->sign, counter); + } + s->symbol_val = 0; s->symbol_len = 0; s->symbol_mapped_pattern = 0; s->sign = 0; s->type = 0; + } + } +} + +static void ref_bit_end_encode(void) { + unsigned int block_num, i; + for (block_num = 0; block_num < SEGMENT_SIZE; ++block_num) { + if (block_data[block_num].parent_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].parent_ref_symbol, block_data[block_num].parent_sym_len); + block_data[block_num].parent_ref_symbol = 0; block_data[block_num].parent_sym_len = 0; + } + if (block_data[block_num].children_sym_len > 0) { + write_to_the_output_segment(block_data[block_num].children_ref_symbol, block_data[block_num].children_sym_len); + block_data[block_num].children_ref_symbol = 0; block_data[block_num].children_sym_len = 0; + } + for (i = 0; i < 3; ++i) { + if (block_data[block_num].grand_children_sym_len[i] > 0) + write_to_the_output_segment(block_data[block_num].grand_children_ref_symbol[i], block_data[block_num].grand_children_sym_len[i]); + block_data[block_num].grand_children_sym_len[i] = 0; + block_data[block_num].grand_children_ref_symbol[i] = 0; + } + } +} + +static void stages_encoding(unsigned int bit_plane_number) { + unsigned char code_option_gaggle[3] = {0,0,0}; + int hit_flag[3] = {0,0,0}; + coding_options(bit_plane_number, code_option_gaggle); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 1); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 2); + gaggle_pass(bit_plane_number, code_option_gaggle, hit_flag, 3); + ref_bit_end_encode(); +} + +static void ac_encoding(unsigned int quantization_factor) { + unsigned int bit_plane, i; + if (header_data.bit_depth_ac == 0) return; + if (header_data.bit_depth_ac == 1) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment(block_data[i].max_ac_bit_size, 1); + } else { + ac_depth_encoder(); + } + for (bit_plane = header_data.bit_depth_ac; bit_plane > 0; bit_plane--) { + if ((bit_plane <= quantization_factor) && + (type_of_compression || (quantization_factor > BITLL3 && BITLL3 < bit_plane))) { + for (i = 0; i < SEGMENT_SIZE; ++i) + write_to_the_output_segment((block_data[i].dc_reminder >> (bit_plane - 1)) & 0x01, 1); + } + bit_plane_encoding(bit_plane); + stages_encoding(bit_plane); + } +} + +// ============================================================ +// Cluster test harness +// ============================================================ +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_2_2_seq_float", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + if (rt_core_id() == 0) + run_suite(testcases); + + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int i, k, qf, checksum; + int coeff_checksum; + + generate_input(); + for (i = 0; i < OUTPUT_BUFFER_BYTES; ++i) output_stream.OutputBitStream[i] = 0u; + output_stream.num_bits = 0u; + output_stream.num_total_bytes = 0u; + output_stream.overflow = 0u; + + header_data.start_img_flag = 1; + header_data.end_img_flag = 1; + header_data.segment_count = 0; + header_data.part_2_flag = 0; + header_data.part_3_flag = 0; + header_data.part_4_flag = 0; + header_data.pad_rows = 0; + + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + + start(); + + dwt2D_compression_computation_float(); + coeff_regroup(); + build_block_string(); + qf = dc_encoding(); + ac_encoding(qf); + + stop(); + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + coeff_checksum = 0; + for (i = 0; i < TOTAL_BLOCKS; ++i) + for (k = 0; k < BLOCKSIZEIMAGE * BLOCKSIZEIMAGE; ++k) + coeff_checksum += block_string[i][k]; + + checksum = 0u; + for (i = 0; i < output_stream.num_total_bytes + 1u && i < OUTPUT_BUFFER_BYTES; ++i) + checksum += output_stream.OutputBitStream[i]; + + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + unsigned int out0 = (unsigned int)output_stream.OutputBitStream[0]; + + printf("OBPMark 2.2 - CCSDS 122.0 Image Compression (FLOAT)\n"); + printf("PULP cluster sequential (1 core, RV32) - Astral @ 50 MHz\n"); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Coeff checksum : %d\n", coeff_checksum); + printf(" Block[0] DC : %d\n", block_string[0][0]); + printf(" bit_depth_dc : %u\n", header_data.bit_depth_dc); + printf(" bit_depth_ac : %u\n", header_data.bit_depth_ac); + printf(" quant. factor : %u\n", qf); + printf(" Output bytes : %u\n", output_stream.num_total_bytes); + printf(" Overflow : %u\n", output_stream.overflow); + printf(" Checksum : %u\n", checksum); + printf(" Output[0] : %u\n", out0); + + if (output_stream.overflow == 0u && + coeff_checksum == EXPECTED_COEFF_CHECKSUM && + block_string[0][0] == EXPECTED_BLOCK0_DC && + out0 == EXPECTED_OUTPUT_0 && checksum == EXPECTED_CHECKSUM && + output_stream.num_total_bytes == EXPECTED_TOTAL_BYTES && + header_data.bit_depth_dc == EXPECTED_BIT_DEPTH_DC && + header_data.bit_depth_ac == EXPECTED_BIT_DEPTH_AC) { + result->errors = 0; + printf("RESULT: PASS\n"); + } else { + result->errors = 1; + printf("RESULT: FAIL (atteso coeff=%d dc0=%d dc=%u ac=%u bytes=%u chk=%u out0=%u)\n", + EXPECTED_COEFF_CHECKSUM, EXPECTED_BLOCK0_DC, EXPECTED_BIT_DEPTH_DC, + EXPECTED_BIT_DEPTH_AC, EXPECTED_TOTAL_BYTES, EXPECTED_CHECKSUM, EXPECTED_OUTPUT_0); + } +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_par/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_par/Makefile new file mode 100644 index 000000000..a0244dd7c --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_par/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_3_1_pulpd_par.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_par/obpmark_3_1_pulpd_par.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_par/obpmark_3_1_pulpd_par.c new file mode 100644 index 000000000..bf56b89cb --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_par/obpmark_3_1_pulpd_par.c @@ -0,0 +1,388 @@ +/* + * Copyright 2024 ETH Zurich and University of Bologna. + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + * + * OBPMark Benchmark #3.1 - AES Encryption (AES-CTR mode) + * Parallel (SPMD, 8 core) port for the PULP integer cluster of Astral (RV32) + * + * Stesso algoritmo, stessa configurazione e stesso posizionamento dei dati + * di obpmark_3_1_pulpd_seq.c; stessa strategia di parallelizzazione di + * obpmark_3_1_host_smp.c: + * Fase 1 (SEQUENZIALE, core 0): AES_KeyExpansion (ricorrenza su w[i-1] e + * w[i-Nk], costo fisso), seguita da synch_barrier(). + * Fase 2 (PARALLELA, range contiguo di blocchi per core): cifratura CTR. + * Il contatore del blocco b e' calcolato direttamente come iv0 + b + * (addizione a 128 bit big-endian con riporto completo), eliminando + * l'unica dipendenza tra blocchi del riferimento sequenziale (iv + * condiviso incrementato dopo ogni blocco), come nel port OpenMP di + * riferimento. Ogni core usa un contatore privato sullo stack e scrive + * porzioni disgiunte di cyphertext. + * Il parallelismo e' a granularita' di blocco: con DATA_LENGTH=64 byte + * (4 blocchi) lavorano solo i core 0-3, i core 4-7 passano direttamente + * alla barriera finale. Un parallelismo intra-blocco (per colonna) non e' + * conveniente: ShiftRows mescola le colonne a ogni round, imponendo una + * barriera per round su un lavoro di poche decine di istruzioni. + * + * Il valore finale dell'iv (iv0 + N_BLOCKS nel riferimento) non fa parte + * dell'output del benchmark e non viene aggiornato. + * + * Stesso algoritmo e stessa configurazione di obpmark_3_1_host_seq.c + * (AES-128, Nb=4 Nk=4 Nr=10, data_length=64 byte = 4 blocchi, vettore + * NIST SP 800-38A F.5.1 "CTR-AES128.Encrypt"). Aritmetica interamente + * intera su byte e word a 32 bit: il risultato e' atteso bit-identico al + * porting host gia' validato su RTL: + * Ciphertext = 874d6191b620e3261bef6864990db6ce + * 9806f66b7970fdff8617187bb9fffdff + * 5ae4df3edbd5d35e5b4f09020db03eab + * 1e031dda2fbe03d1792170a0f3009cee + * Output[0]=135, Checksum=8196 + * + * Differenze rispetto al porting host (comuni al port sequenziale): + * - Tabelle e dati di lavoro (sbox, rcon, key, iv, plaintext, + * cyphertext, expanded_key) risiedono in HEAPSRAM (L1/TCDM). Le copie + * costanti in L2 sono trasferite in L1 prima dell'avvio del contatore, + * analogamente a copy_memory_to_device() del riferimento, che nel port + * CPU e' fuori dalla regione cronometrata. La sbox e' acceduta 16 volte + * per round: lasciarla in L2 misurerebbe la latenza d'interconnessione + * piu' che il calcolo. + * - Il riporto del contatore CTR usa unsigned int invece di uint64_t + * (RV32: evita aritmetica a 64 bit emulata); il riporto massimo di un + * byte + 1 e' 1, quindi la semantica e' invariata. + * - Verifica tramite confronto dell'intero ciphertext con il vettore NIST + * e verdetto RESULT: PASS/FAIL, come negli altri test di cluster. + * + * Cluster test harness (testcase_t / run_suite / check_obpmark / + * bench_cluster_forward / cpu_perf_*), HEAPSRAM e conteggio cicli seguono + * le convenzioni di obpmark_2_1_pulpd_seq.c e obpmark_2_2_pulpd_seq.c. + * Solo core 0 entra in run_suite(): porting single-core, nessun + * split_range()/synch_barrier() nel corpo del calcolo. + */ + +#include "pulp.h" +#include +#include + +#define HEAPSRAM __attribute__((section(".heapsram"))) + +// ============================================================ +// Configurazione (identica a obpmark_3_1_host_seq.c) +// ============================================================ +#define AES_NB 4u +#define AES_NK 4u /* AES-128 */ +#define AES_NR 10u +#define DATA_LENGTH 64u +#define N_BLOCKS (DATA_LENGTH / 16u) +#define EXPKEY_WORDS (AES_NB * (AES_NR + 1u)) + +#define EXPECTED_OUTPUT_0 135u +#define EXPECTED_CHECKSUM 8196u + +// ============================================================ +// Costanti in L2 (benchmark.h: SBOX_INIT / RCON_INIT; vettore NIST) +// ============================================================ +static const uint8_t sbox_l2[256] = { + 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, + 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, + 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, + 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, + 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, + 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, + 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, + 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, + 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, + 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, + 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, + 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, + 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, + 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, + 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, + 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16 +}; + +static const uint8_t rcon_l2[11] = { + 0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36 +}; + +static const uint8_t key_l2[16] = { + 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c +}; + +static const uint8_t iv_l2[16] = { + 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff +}; + +static const uint8_t plaintext_l2[DATA_LENGTH] = { + 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a, + 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51, + 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef, + 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10 +}; + +static const uint8_t expected_ct[DATA_LENGTH] = { + 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce, + 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff, + 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab, + 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee +}; + +// ============================================================ +// Dati di lavoro in L1 (HEAPSRAM) +// ============================================================ +HEAPSRAM static uint8_t sbox[256]; +HEAPSRAM static uint8_t rcon[11]; +HEAPSRAM static uint8_t key[16]; +HEAPSRAM static uint8_t iv[16]; +HEAPSRAM static uint8_t plaintext[DATA_LENGTH]; +HEAPSRAM static uint8_t cyphertext[DATA_LENGTH]; +HEAPSRAM static uint32_t expanded_key[EXPKEY_WORDS]; + +// Equivalente di copy_memory_to_device() del riferimento: fuori dalla +// regione cronometrata. +static void copy_to_l1(void) { + unsigned int i; + for (i = 0; i < 256u; i++) sbox[i] = sbox_l2[i]; + for (i = 0; i < 11u; i++) rcon[i] = rcon_l2[i]; + for (i = 0; i < 16u; i++) { key[i] = key_l2[i]; iv[i] = iv_l2[i]; } + for (i = 0; i < DATA_LENGTH; i++) { plaintext[i] = plaintext_l2[i]; cyphertext[i] = 0u; } +} + +// ============================================================ +// AES core (identico a obpmark_3_1_host_seq.c) +// ============================================================ +static inline uint32_t AES_RotWord(uint32_t n) { + return (n >> 8) | (n << 24); +} + +static uint32_t AES_SubWord(uint32_t word) { + return ((uint32_t)sbox[(word >> 24) & 0xFFu] << 24) | + ((uint32_t)sbox[(word >> 16) & 0xFFu] << 16) | + ((uint32_t)sbox[(word >> 8) & 0xFFu] << 8) | + ((uint32_t)sbox[word & 0xFFu]); +} + +static void AES_KeyExpansion(void) { + uint32_t temp; + unsigned int i = 0u; + while (i < AES_NK) { + expanded_key[i] = (uint32_t)key[4u*i] | + ((uint32_t)key[4u*i + 1u] << 8) | + ((uint32_t)key[4u*i + 2u] << 16) | + ((uint32_t)key[4u*i + 3u] << 24); + i++; + } + for (; i < EXPKEY_WORDS; i++) { + temp = expanded_key[i - 1u]; + if (i % AES_NK == 0u) { + temp = AES_SubWord(AES_RotWord(temp)) ^ (uint32_t)rcon[i / AES_NK]; + } + expanded_key[i] = expanded_key[i - AES_NK] ^ temp; + } +} + +static inline uint8_t roundkey_byte(unsigned int round, unsigned int col, unsigned int row) { + unsigned int idx = round * AES_NB * 4u + col * AES_NB + row; + return (uint8_t)(expanded_key[idx >> 2] >> ((idx & 3u) * 8u)); +} + +static void AES_AddRoundKey(const uint8_t in_state[4][4], uint8_t state[4][4], unsigned int round) { + unsigned int i, j; + for (i = 0; i < AES_NB; i++) + for (j = 0; j < 4u; j++) + state[i][j] = in_state[i][j] ^ roundkey_byte(round, i, j); +} + +static void AES_SubBytes(uint8_t state[4][4]) { + unsigned int i, j; + for (i = 0; i < 4u; i++) + for (j = 0; j < 4u; j++) + state[i][j] = sbox[state[i][j]]; +} + +static void AES_ShiftRows(uint8_t state[4][4]) { + uint8_t temp; + temp = state[0][1]; + state[0][1] = state[1][1]; + state[1][1] = state[2][1]; + state[2][1] = state[3][1]; + state[3][1] = temp; + + temp = state[0][2]; + state[0][2] = state[2][2]; + state[2][2] = temp; + temp = state[1][2]; + state[1][2] = state[3][2]; + state[3][2] = temp; + + temp = state[3][3]; + state[3][3] = state[2][3]; + state[2][3] = state[1][3]; + state[1][3] = state[0][3]; + state[0][3] = temp; +} + +static inline uint8_t xtime(uint8_t x) { + return (uint8_t)((x << 1) ^ (((x >> 7) & 1u) * 0x1bu)); +} + +static void AES_MixColumns(uint8_t state[4][4]) { + uint8_t Tmp, Tm, t; + unsigned int i; + for (i = 0; i < 4u; i++) { + t = state[i][0]; /* valore originale, serve all'ultima colonna */ + Tmp = state[i][0] ^ state[i][1] ^ state[i][2] ^ state[i][3]; + Tm = xtime((uint8_t)(state[i][0] ^ state[i][1])); state[i][0] ^= Tm ^ Tmp; + Tm = xtime((uint8_t)(state[i][1] ^ state[i][2])); state[i][1] ^= Tm ^ Tmp; + Tm = xtime((uint8_t)(state[i][2] ^ state[i][3])); state[i][2] ^= Tm ^ Tmp; + Tm = xtime((uint8_t)(state[i][3] ^ t)); state[i][3] ^= Tm ^ Tmp; + } +} + +static void AES_encrypt_state(const uint8_t in_state[4][4], uint8_t state[4][4], unsigned int num_rounds) { + unsigned int roundi; + AES_AddRoundKey(in_state, state, 0u); + for (roundi = 1u; roundi < num_rounds; roundi++) { + AES_SubBytes(state); + AES_ShiftRows(state); + AES_MixColumns(state); + AES_AddRoundKey(state, state, roundi); + } + AES_SubBytes(state); + AES_ShiftRows(state); + AES_AddRoundKey(state, state, num_rounds); +} + +// Contatore del blocco b = iv0 + b (128 bit big-endian, iv[15] = LSB), +// riporto completo: equivale a b applicazioni di counter_add(iv, 1). +// iv resta invariato (sola lettura durante la fase parallela). +static void counter_for_block(uint8_t ctr[16], unsigned int b) { + unsigned int carry = b; + int id; + for (id = 15; id >= 0; id--) { + unsigned int s = (unsigned int)iv[id] + carry; + ctr[id] = (uint8_t)s; + carry = s >> 8; + } +} + +static void AES_encrypt_block(unsigned int b) { + uint8_t ctr[16]; + uint8_t counter_state[4][4]; + uint8_t out_state[4][4]; + unsigned int col, row; + + counter_for_block(ctr, b); + + for (col = 0; col < 4u; col++) + for (row = 0; row < 4u; row++) + counter_state[col][row] = ctr[col*4u + row]; + + AES_encrypt_state(counter_state, out_state, AES_NR); + + for (col = 0; col < 4u; col++) + for (row = 0; row < 4u; row++) + cyphertext[b*16u + col*4u + row] = + plaintext[b*16u + col*4u + row] ^ out_state[col][row]; +} + +// ============================================================ +// Partizionamento (resto distribuito sui primi core) +// ============================================================ +static unsigned int num_cores; + +static inline void split_range(unsigned int total, unsigned int id, + unsigned int n, unsigned int *start, + unsigned int *end) { + unsigned int chunk = total / n; + unsigned int rem = total % n; + *start = id * chunk + (id < rem ? id : rem); + *end = *start + chunk + (id < rem ? 1u : 0u); +} + +// ============================================================ +// Test harness +// ============================================================ +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_3_1_par", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + num_cores = get_core_num(); + + if (rt_core_id() < num_cores) + run_suite(testcases); + + synch_barrier(); + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int i, checksum, mismatches; + + if (get_core_id() == 0) { + copy_to_l1(); + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + } + synch_barrier(); // dati in L1 visibili a tutti i core + + start(); + + // --- Fase 1 (SEQUENZIALE, core 0): key expansion --- + if (get_core_id() == 0) AES_KeyExpansion(); + synch_barrier(); // expanded_key visibile a tutti i core + + // --- Fase 2 (PARALLELA): cifratura CTR per range di blocchi --- + { + unsigned int bs, be, b; + split_range(N_BLOCKS, (unsigned int)get_core_id(), num_cores, &bs, &be); + for (b = bs; b < be; b++) AES_encrypt_block(b); + } + synch_barrier(); // cyphertext completo + + stop(); + + if (get_core_id() != 0) return; + + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + checksum = 0u; + mismatches = 0u; + for (i = 0; i < DATA_LENGTH; i++) { + checksum += cyphertext[i]; + if (cyphertext[i] != expected_ct[i]) mismatches++; + } + + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + unsigned int out0 = (unsigned int)cyphertext[0]; + + printf("OBPMark 3.1 - AES Encryption (CTR mode)\n"); + printf("PULP cluster parallel (%u core, RV32) - Astral @ 50 MHz\n", num_cores); + printf(" key_size=128 bit Nb=%u Nk=%u Nr=%u data_length=%u byte (%u blocchi)\n", + AES_NB, AES_NK, AES_NR, DATA_LENGTH, N_BLOCKS); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Ciphertext : 0x"); + for (i = 0; i < DATA_LENGTH; i++) printf("%02x", cyphertext[i]); + printf("\n"); + printf(" Output[0] : %u\n", out0); + printf(" Checksum : %u\n", checksum); + printf(" Byte mismatches : %u\n", mismatches); + + if (mismatches == 0u && out0 == EXPECTED_OUTPUT_0 && checksum == EXPECTED_CHECKSUM) { + result->errors = 0; + printf("RESULT: PASS\n"); + } else { + result->errors = 1; + printf("RESULT: FAIL (expected Output[0]=%u Checksum=%u, 0 mismatches)\n", + EXPECTED_OUTPUT_0, EXPECTED_CHECKSUM); + } +} diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_seq/Makefile b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_seq/Makefile new file mode 100644 index 000000000..597958700 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_seq/Makefile @@ -0,0 +1,4 @@ +PULP_APP = test +PULP_APP_SRCS = obpmark_3_1_pulpd_seq.c +PULP_CFLAGS = -O3 -I$(PULPRT_HOME)/drivers/include +include $(PULP_SDK_HOME)/install/rules/pulp.mk diff --git a/sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_seq/obpmark_3_1_pulpd_seq.c b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_seq/obpmark_3_1_pulpd_seq.c new file mode 100644 index 000000000..7a0f33c46 --- /dev/null +++ b/sw/tests/bare-metal/pulpd-benchmarks/obpmark_3_1_seq/obpmark_3_1_pulpd_seq.c @@ -0,0 +1,339 @@ +/* + * Copyright 2024 ETH Zurich and University of Bologna. + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + * + * OBPMark Benchmark #3.1 - AES Encryption (AES-CTR mode) + * Sequential (single-core) port for the PULP integer cluster of Astral + * (core 0 only, RV32) + * + * Stesso algoritmo e stessa configurazione di obpmark_3_1_host_seq.c + * (AES-128, Nb=4 Nk=4 Nr=10, data_length=64 byte = 4 blocchi, vettore + * NIST SP 800-38A F.5.1 "CTR-AES128.Encrypt"). Aritmetica interamente + * intera su byte e word a 32 bit: il risultato e' atteso bit-identico al + * porting host gia' validato su RTL: + * Ciphertext = 874d6191b620e3261bef6864990db6ce + * 9806f66b7970fdff8617187bb9fffdff + * 5ae4df3edbd5d35e5b4f09020db03eab + * 1e031dda2fbe03d1792170a0f3009cee + * Output[0]=135, Checksum=8196 + * + * Differenze rispetto al porting host: + * - Tabelle e dati di lavoro (sbox, rcon, key, iv, plaintext, + * cyphertext, expanded_key) risiedono in HEAPSRAM (L1/TCDM). Le copie + * costanti in L2 sono trasferite in L1 prima dell'avvio del contatore, + * analogamente a copy_memory_to_device() del riferimento, che nel port + * CPU e' fuori dalla regione cronometrata. La sbox e' acceduta 16 volte + * per round: lasciarla in L2 misurerebbe la latenza d'interconnessione + * piu' che il calcolo. + * - Il riporto del contatore CTR usa unsigned int invece di uint64_t + * (RV32: evita aritmetica a 64 bit emulata); il riporto massimo di un + * byte + 1 e' 1, quindi la semantica e' invariata. + * - Verifica tramite confronto dell'intero ciphertext con il vettore NIST + * e verdetto RESULT: PASS/FAIL, come negli altri test di cluster. + * + * Cluster test harness (testcase_t / run_suite / check_obpmark / + * bench_cluster_forward / cpu_perf_*), HEAPSRAM e conteggio cicli seguono + * le convenzioni di obpmark_2_1_pulpd_seq.c e obpmark_2_2_pulpd_seq.c. + * Solo core 0 entra in run_suite(): porting single-core, nessun + * split_range()/synch_barrier() nel corpo del calcolo. + */ + +#include "pulp.h" +#include +#include + +#define HEAPSRAM __attribute__((section(".heapsram"))) + +// ============================================================ +// Configurazione (identica a obpmark_3_1_host_seq.c) +// ============================================================ +#define AES_NB 4u +#define AES_NK 4u /* AES-128 */ +#define AES_NR 10u +#define DATA_LENGTH 64u +#define N_BLOCKS (DATA_LENGTH / 16u) +#define EXPKEY_WORDS (AES_NB * (AES_NR + 1u)) + +#define EXPECTED_OUTPUT_0 135u +#define EXPECTED_CHECKSUM 8196u + +// ============================================================ +// Costanti in L2 (benchmark.h: SBOX_INIT / RCON_INIT; vettore NIST) +// ============================================================ +static const uint8_t sbox_l2[256] = { + 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, + 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, + 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, + 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, + 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, + 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, + 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, + 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, + 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, + 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, + 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, + 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, + 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, + 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, + 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, + 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16 +}; + +static const uint8_t rcon_l2[11] = { + 0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36 +}; + +static const uint8_t key_l2[16] = { + 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c +}; + +static const uint8_t iv_l2[16] = { + 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff +}; + +static const uint8_t plaintext_l2[DATA_LENGTH] = { + 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a, + 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51, + 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef, + 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10 +}; + +static const uint8_t expected_ct[DATA_LENGTH] = { + 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce, + 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff, + 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab, + 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee +}; + +// ============================================================ +// Dati di lavoro in L1 (HEAPSRAM) +// ============================================================ +HEAPSRAM static uint8_t sbox[256]; +HEAPSRAM static uint8_t rcon[11]; +HEAPSRAM static uint8_t key[16]; +HEAPSRAM static uint8_t iv[16]; +HEAPSRAM static uint8_t plaintext[DATA_LENGTH]; +HEAPSRAM static uint8_t cyphertext[DATA_LENGTH]; +HEAPSRAM static uint32_t expanded_key[EXPKEY_WORDS]; + +// Equivalente di copy_memory_to_device() del riferimento: fuori dalla +// regione cronometrata. +static void copy_to_l1(void) { + unsigned int i; + for (i = 0; i < 256u; i++) sbox[i] = sbox_l2[i]; + for (i = 0; i < 11u; i++) rcon[i] = rcon_l2[i]; + for (i = 0; i < 16u; i++) { key[i] = key_l2[i]; iv[i] = iv_l2[i]; } + for (i = 0; i < DATA_LENGTH; i++) { plaintext[i] = plaintext_l2[i]; cyphertext[i] = 0u; } +} + +// ============================================================ +// AES core (identico a obpmark_3_1_host_seq.c) +// ============================================================ +static inline uint32_t AES_RotWord(uint32_t n) { + return (n >> 8) | (n << 24); +} + +static uint32_t AES_SubWord(uint32_t word) { + return ((uint32_t)sbox[(word >> 24) & 0xFFu] << 24) | + ((uint32_t)sbox[(word >> 16) & 0xFFu] << 16) | + ((uint32_t)sbox[(word >> 8) & 0xFFu] << 8) | + ((uint32_t)sbox[word & 0xFFu]); +} + +static void AES_KeyExpansion(void) { + uint32_t temp; + unsigned int i = 0u; + while (i < AES_NK) { + expanded_key[i] = (uint32_t)key[4u*i] | + ((uint32_t)key[4u*i + 1u] << 8) | + ((uint32_t)key[4u*i + 2u] << 16) | + ((uint32_t)key[4u*i + 3u] << 24); + i++; + } + for (; i < EXPKEY_WORDS; i++) { + temp = expanded_key[i - 1u]; + if (i % AES_NK == 0u) { + temp = AES_SubWord(AES_RotWord(temp)) ^ (uint32_t)rcon[i / AES_NK]; + } + expanded_key[i] = expanded_key[i - AES_NK] ^ temp; + } +} + +static inline uint8_t roundkey_byte(unsigned int round, unsigned int col, unsigned int row) { + unsigned int idx = round * AES_NB * 4u + col * AES_NB + row; + return (uint8_t)(expanded_key[idx >> 2] >> ((idx & 3u) * 8u)); +} + +static void AES_AddRoundKey(const uint8_t in_state[4][4], uint8_t state[4][4], unsigned int round) { + unsigned int i, j; + for (i = 0; i < AES_NB; i++) + for (j = 0; j < 4u; j++) + state[i][j] = in_state[i][j] ^ roundkey_byte(round, i, j); +} + +static void AES_SubBytes(uint8_t state[4][4]) { + unsigned int i, j; + for (i = 0; i < 4u; i++) + for (j = 0; j < 4u; j++) + state[i][j] = sbox[state[i][j]]; +} + +static void AES_ShiftRows(uint8_t state[4][4]) { + uint8_t temp; + temp = state[0][1]; + state[0][1] = state[1][1]; + state[1][1] = state[2][1]; + state[2][1] = state[3][1]; + state[3][1] = temp; + + temp = state[0][2]; + state[0][2] = state[2][2]; + state[2][2] = temp; + temp = state[1][2]; + state[1][2] = state[3][2]; + state[3][2] = temp; + + temp = state[3][3]; + state[3][3] = state[2][3]; + state[2][3] = state[1][3]; + state[1][3] = state[0][3]; + state[0][3] = temp; +} + +static inline uint8_t xtime(uint8_t x) { + return (uint8_t)((x << 1) ^ (((x >> 7) & 1u) * 0x1bu)); +} + +static void AES_MixColumns(uint8_t state[4][4]) { + uint8_t Tmp, Tm, t; + unsigned int i; + for (i = 0; i < 4u; i++) { + t = state[i][0]; /* valore originale, serve all'ultima colonna */ + Tmp = state[i][0] ^ state[i][1] ^ state[i][2] ^ state[i][3]; + Tm = xtime((uint8_t)(state[i][0] ^ state[i][1])); state[i][0] ^= Tm ^ Tmp; + Tm = xtime((uint8_t)(state[i][1] ^ state[i][2])); state[i][1] ^= Tm ^ Tmp; + Tm = xtime((uint8_t)(state[i][2] ^ state[i][3])); state[i][2] ^= Tm ^ Tmp; + Tm = xtime((uint8_t)(state[i][3] ^ t)); state[i][3] ^= Tm ^ Tmp; + } +} + +static void AES_encrypt_state(const uint8_t in_state[4][4], uint8_t state[4][4], unsigned int num_rounds) { + unsigned int roundi; + AES_AddRoundKey(in_state, state, 0u); + for (roundi = 1u; roundi < num_rounds; roundi++) { + AES_SubBytes(state); + AES_ShiftRows(state); + AES_MixColumns(state); + AES_AddRoundKey(state, state, roundi); + } + AES_SubBytes(state); + AES_ShiftRows(state); + AES_AddRoundKey(state, state, num_rounds); +} + +// +1 sul contatore a 128 bit big-endian (iv[15] = LSB), riporto completo: +// equivalente iterativo di counter_add(iv, 1, 15) del riferimento. +static void counter_increment(uint8_t *ivptr) { + int id = 15; + unsigned int block = 1u; + for (;;) { + unsigned int carry = (unsigned int)ivptr[id] + block; + ivptr[id] = (uint8_t)carry; + if (carry <= 255u || id == 0) break; + block = carry >> 8; + id--; + } +} + +static void AES_encrypt_block(unsigned int b) { + uint8_t counter_state[4][4]; + uint8_t out_state[4][4]; + unsigned int col, row; + + for (col = 0; col < 4u; col++) + for (row = 0; row < 4u; row++) + counter_state[col][row] = iv[col*4u + row]; + + AES_encrypt_state(counter_state, out_state, AES_NR); + + for (col = 0; col < 4u; col++) + for (row = 0; row < 4u; row++) + cyphertext[b*16u + col*4u + row] = + plaintext[b*16u + col*4u + row] ^ out_state[col][row]; + + counter_increment(iv); +} + +// ============================================================ +// Test harness +// ============================================================ +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()); + +testcase_t testcases[] = { + { .name = "obpmark_3_1_seq", .test = check_obpmark }, + {0, 0} +}; + +int main(void) { + if (rt_cluster_id() != 0) + return bench_cluster_forward(0); + + if (rt_core_id() == 0) + run_suite(testcases); + + return 0; +} + +void check_obpmark(testresult_t *result, void (*start)(), void (*stop)()) { + unsigned int b, i, checksum, mismatches; + + copy_to_l1(); + + cpu_perf_conf_events(1 << CSR_PCER_CYCLES); + cpu_perf_setall(0); + cpu_perf_start(0); + + start(); + + AES_KeyExpansion(); + for (b = 0; b < N_BLOCKS; b++) { + AES_encrypt_block(b); + } + + stop(); + cpu_perf_stop(); + unsigned int cycles = cpu_perf_get(CSR_PCER_CYCLES); + + checksum = 0u; + mismatches = 0u; + for (i = 0; i < DATA_LENGTH; i++) { + checksum += cyphertext[i]; + if (cyphertext[i] != expected_ct[i]) mismatches++; + } + + unsigned int time_us = (unsigned int)(((unsigned long long)cycles * 20ULL) / 1000ULL); + unsigned int out0 = (unsigned int)cyphertext[0]; + + printf("OBPMark 3.1 - AES Encryption (CTR mode)\n"); + printf("PULP cluster sequential (1 core, RV32) - Astral @ 50 MHz\n"); + printf(" key_size=128 bit Nb=%u Nk=%u Nr=%u data_length=%u byte (%u blocchi)\n", + AES_NB, AES_NK, AES_NR, DATA_LENGTH, N_BLOCKS); + printf(" Elapsed cycles : %u\n", cycles); + printf(" Elapsed time : %u us\n", time_us); + printf(" Ciphertext : 0x"); + for (i = 0; i < DATA_LENGTH; i++) printf("%02x", cyphertext[i]); + printf("\n"); + printf(" Output[0] : %u\n", out0); + printf(" Checksum : %u\n", checksum); + printf(" Byte mismatches : %u\n", mismatches); + + if (mismatches == 0u && out0 == EXPECTED_OUTPUT_0 && checksum == EXPECTED_CHECKSUM) { + result->errors = 0; + printf("RESULT: PASS\n"); + } else { + result->errors = 1; + printf("RESULT: FAIL (expected Output[0]=%u Checksum=%u, 0 mismatches)\n", + EXPECTED_OUTPUT_0, EXPECTED_CHECKSUM); + } +}