Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Replace attribute alias in CRC32 #1713

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 22 additions & 28 deletions src/crc32.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static uint32_t crc32_calc(const uint32_t crc, const uint8_t data)
return (crc << 8U) ^ crc32_table[((crc >> 24U) ^ data) & 0xffU];
}

bool generic_crc32(target_s *const target, uint32_t *const result, const uint32_t base, const size_t len)
static bool generic_crc32(target_s *const target, uint32_t *const result, const uint32_t base, const size_t len)
dragonmux marked this conversation as resolved.
Show resolved Hide resolved
{
uint32_t crc = 0xffffffffU;
#if PC_HOSTED == 1
Expand All @@ -113,9 +113,6 @@ bool generic_crc32(target_s *const target, uint32_t *const result, const uint32_
uint8_t bytes[128U];
#endif

#ifndef DEBUG_INFO_IS_NOOP
const uint32_t start_time = platform_time_ms();
#endif
uint32_t last_time = platform_time_ms();
for (size_t offset = 0; offset < len; offset += sizeof(bytes)) {
const uint32_t actual_time = platform_time_ms();
Expand All @@ -132,36 +129,20 @@ bool generic_crc32(target_s *const target, uint32_t *const result, const uint32_
for (size_t i = 0; i < read_len; i++)
crc = crc32_calc(crc, bytes[i]);
}
#ifndef DEBUG_INFO_IS_NOOP
/* "generic_crc32: 08000110+75272 -> 1353ms, 54 KiB/s" */
const uint32_t end_time = platform_time_ms();
const uint32_t time_elapsed = end_time - start_time;
DEBUG_INFO("%s: %08" PRIx32 "+%" PRIu32 " -> %" PRIu32 "ms", __func__, base, (uint32_t)len, time_elapsed);
if (len >= 512U) {
const uint32_t speed = len * 1000U / time_elapsed / 1024U;
DEBUG_INFO(", %" PRIu32 " KiB/s", speed);
}
DEBUG_INFO("\n");
#endif
*result = crc;
return true;
}

__attribute__((alias("generic_crc32"))) bool bmd_crc32(
target_s *const target, uint32_t *const result, const uint32_t base, const size_t len);
#else
#include <libopencm3/stm32/crc.h>
#include "buffer_utils.h"

bool stm32_crc32(target_s *const target, uint32_t *const result, const uint32_t base, const size_t len)
static bool stm32_crc32(target_s *const target, uint32_t *const result, const uint32_t base, const size_t len)
{
uint8_t bytes[1024U]; /* ADIv5 MEM-AP AutoInc range */

CRC_CR |= CRC_CR_RESET;

#ifndef DEBUG_INFO_IS_NOOP
const uint32_t start_time = platform_time_ms();
#endif
uint32_t last_time = platform_time_ms();
const size_t adjusted_len = len & ~3U;
for (size_t offset = 0; offset < adjusted_len; offset += sizeof(bytes)) {
Expand Down Expand Up @@ -198,21 +179,34 @@ bool stm32_crc32(target_s *const target, uint32_t *const result, const uint32_t
}
}
}
*result = crc;
return true;
}
#endif

/* Shim to dispatch host-specific implementation (and keep the `__func__` meaningful) */
bool bmd_crc32(target_s *const target, uint32_t *const result, const uint32_t base, const size_t len)
{
#ifndef DEBUG_INFO_IS_NOOP
const uint32_t start_time = platform_time_ms();
#endif
#if !defined(STM32F0) && !defined(STM32F1) && !defined(STM32F2) && !defined(STM32F3) && !defined(STM32F4) && \
!defined(STM32F7) && !defined(STM32L0) && !defined(STM32L1) && !defined(STM32G0) && !defined(STM32G4)
const bool status = generic_crc32(target, result, base, len);
#else
const bool status = stm32_crc32(target, result, base, len);
#endif
#ifndef DEBUG_INFO_IS_NOOP
/* "generic_crc32: 08000110+75272 -> 1353ms, 54 KiB/s" */
/* "stm32_crc32: 08000110+75272 -> 237ms, 310 KiB/s" */
const uint32_t end_time = platform_time_ms();
const uint32_t time_elapsed = end_time - start_time;
DEBUG_INFO("%s: %08" PRIx32 "+%" PRIu32 " -> %" PRIu32 "ms", __func__, base, (uint32_t)len, time_elapsed);
DEBUG_INFO("%s: 0x%08" PRIx32 "+%" PRIu32 " -> %" PRIu32 "ms", __func__, base, (uint32_t)len, time_elapsed);
if (len >= 512U) {
const uint32_t speed = len * 1000U / time_elapsed / 1024U;
DEBUG_INFO(", %" PRIu32 " KiB/s", speed);
}
DEBUG_INFO("\n");
#endif
*result = crc;
return true;
return status;
}

__attribute__((alias("stm32_crc32"))) bool bmd_crc32(
target_s *const target, uint32_t *const result, const uint32_t base, const size_t len);
#endif