Software APIs
crc32_perftest.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 #include <stdbool.h>
6 #include <stdint.h>
7 
8 #include "sw/device/lib/base/crc32.h"
12 #include "sw/device/lib/testing/test_framework/check.h"
14 #include "sw/device/lib/testing/test_framework/ottf_test_config.h"
15 
16 OTTF_DEFINE_TEST_CONFIG();
17 
18 bool test_main(void) {
19  uint8_t buf[4096];
20  for (size_t i = 0; i < ARRAYSIZE(buf); ++i) {
21  buf[i] = i & UINT8_MAX;
22  }
23 
24  const size_t kNumRepetitions = 10;
25  for (size_t i = 0; i < kNumRepetitions; ++i) {
26  const uint64_t start_cycles = ibex_mcycle_read();
27  const uint32_t checksum = crc32(buf, sizeof(buf));
28  const uint64_t end_cycles = ibex_mcycle_read();
29  const uint64_t num_cycles = end_cycles - start_cycles;
30 
31  CHECK(num_cycles <= UINT32_MAX);
32  const uint32_t num_cycles_u32 = (uint32_t)num_cycles;
33  LOG_INFO("CRC32 computed in %d cycles.", num_cycles_u32);
34 
35  const uint32_t kExpectedChecksum = 0xa2912082;
36  if (checksum != kExpectedChecksum) {
37  LOG_ERROR("Checksum did not match. Expected %x, but got %x.",
38  kExpectedChecksum, checksum);
39  return false;
40  }
41  }
42  return true;
43 }