blob: a8731da17938da4541331b5bd8308ac08b155386 [file] [log] [blame]
Manish V Badarkhe7a867922021-04-22 14:41:27 +01001/*
2 * Copyright (c) 2021, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <stdarg.h>
8#include <assert.h>
9
10#include <arm_acle.h>
11#include <common/debug.h>
12
13/* hw_crc32 - compute CRC using Arm intrinsic function
14 *
15 * This function is useful for the platforms with the CPU ARMv8.0
16 * (with CRC instructions supported), and onwards.
17 * Platforms with CPU ARMv8.0 should make sure to add a compile switch
18 * '-march=armv8-a+crc" for successful compilation of this file.
19 *
20 * @crc: previous accumulated CRC
21 * @buf: buffer base address
22 * @size: the size of the buffer
23 *
24 * Return calculated CRC value
25 */
26uint32_t hw_crc32(uint32_t crc, const unsigned char *buf, size_t size)
27{
28 assert(buf != NULL);
29
30 uint32_t calc_crc = ~crc;
31 const unsigned char *local_buf = buf;
32 size_t local_size = size;
33
34 /*
35 * calculate CRC over byte data
36 */
37 while (local_size != 0UL) {
38 calc_crc = __crc32b(calc_crc, *local_buf);
39 local_buf++;
40 local_size--;
41 }
42
43 return ~calc_crc;
44}