Files
Aaru.Checksums.Native/adler32_neon.c

208 lines
8.6 KiB
C
Raw Normal View History

2021-10-13 03:25:16 +01:00
/*
* This file is part of the Aaru Data Preservation Suite.
2022-12-01 23:06:20 +00:00
* Copyright (c) 2019-2023 Natalia Portillo.
2021-10-13 03:25:16 +01:00
* Copyright 2017 The Chromium Authors. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2021-09-29 01:27:02 +01:00
#if defined(__aarch64__) || defined(_M_ARM64) || ((defined(__arm__) || defined(_M_ARM)))
2021-09-29 01:27:02 +01:00
#include <arm_neon.h>
#include "library.h"
#include "adler32.h"
#include "simd.h"
2023-09-23 18:10:44 +01:00
/**
* @brief Calculate Adler-32 checksum for a given data using NEON instructions.
*
* This function calculates the Adler-32 checksum for a block of data using NEON vector instructions.
*
* @param sum1 Pointer to the variable where the first 16-bit checksum value is stored.
* @param sum2 Pointer to the variable where the second 16-bit checksum value is stored.
* @param data Pointer to the data buffer.
* @param len Length of the data buffer in bytes.
*/
2023-09-23 18:55:52 +01:00
TARGET_WITH_NEON void adler32_neon(uint16_t *sum1, uint16_t *sum2, const uint8_t *data, uint32_t len)
2021-09-29 01:27:02 +01:00
{
/*
* Split Adler-32 into component sums.
*/
uint32_t s1 = *sum1;
uint32_t s2 = *sum2;
/*
* Serially compute s1 & s2, until the data is 16-byte aligned.
*/
2023-09-23 18:55:52 +01:00
if((uintptr_t)data & 15)
2021-09-29 01:27:02 +01:00
{
2023-09-23 18:55:52 +01:00
while((uintptr_t)data & 15)
2021-09-29 01:27:02 +01:00
{
2021-10-13 03:07:04 +01:00
s2 += (s1 += *data++);
2021-09-29 01:27:02 +01:00
--len;
}
2023-09-23 18:55:52 +01:00
if(s1 >= ADLER_MODULE) s1 -= ADLER_MODULE;
2021-09-29 01:27:02 +01:00
s2 %= ADLER_MODULE;
}
/*
* Process the data in blocks.
*/
const unsigned BLOCK_SIZE = 1 << 5;
2023-09-23 18:55:52 +01:00
uint32_t blocks = len / BLOCK_SIZE;
2021-09-29 01:27:02 +01:00
len -= blocks * BLOCK_SIZE;
2023-09-23 18:55:52 +01:00
while(blocks)
2021-09-29 01:27:02 +01:00
{
unsigned n = NMAX / BLOCK_SIZE; /* The NMAX constraint. */
2023-09-23 18:55:52 +01:00
if(n > blocks) n = (unsigned)blocks;
2021-09-29 01:27:02 +01:00
blocks -= n;
/*
* Process n blocks of data. At most NMAX data bytes can be
* processed before s2 must be reduced modulo ADLER_MODULE.
*/
2021-10-13 21:06:33 +01:00
#ifdef _MSC_VER
uint32x4_t v_s2 = {.n128_u32 = {0, 0, 0, s1 * n}};
uint32x4_t v_s1 = {.n128_u32 = {0, 0, 0, 0}};
#else
2023-09-23 18:55:52 +01:00
uint32x4_t v_s2 = (uint32x4_t){0, 0, 0, s1 * n};
uint32x4_t v_s1 = (uint32x4_t){0, 0, 0, 0};
#endif
2021-09-29 01:27:02 +01:00
uint16x8_t v_column_sum_1 = vdupq_n_u16(0);
uint16x8_t v_column_sum_2 = vdupq_n_u16(0);
uint16x8_t v_column_sum_3 = vdupq_n_u16(0);
uint16x8_t v_column_sum_4 = vdupq_n_u16(0);
do
{
2021-09-29 01:27:02 +01:00
/*
* Load 32 input bytes.
*/
2023-09-23 18:55:52 +01:00
const uint8x16_t bytes1 = vld1q_u8((uint8_t *)(data));
const uint8x16_t bytes2 = vld1q_u8((uint8_t *)(data + 16));
2021-09-29 01:27:02 +01:00
/*
* Add previous block byte sum to v_s2.
*/
2023-09-23 18:55:52 +01:00
v_s2 = vaddq_u32(v_s2, v_s1);
2021-09-29 01:27:02 +01:00
/*
* Horizontally add the bytes for s1.
*/
2023-09-23 18:55:52 +01:00
v_s1 = vpadalq_u16(v_s1, vpadalq_u8(vpaddlq_u8(bytes1), bytes2));
2021-09-29 01:27:02 +01:00
/*
* Vertically add the bytes for s2.
*/
v_column_sum_1 = vaddw_u8(v_column_sum_1, vget_low_u8(bytes1));
v_column_sum_2 = vaddw_u8(v_column_sum_2, vget_high_u8(bytes1));
v_column_sum_3 = vaddw_u8(v_column_sum_3, vget_low_u8(bytes2));
v_column_sum_4 = vaddw_u8(v_column_sum_4, vget_high_u8(bytes2));
2021-10-13 03:07:04 +01:00
data += BLOCK_SIZE;
2023-09-23 18:55:52 +01:00
}
while(--n);
v_s2 = vshlq_n_u32(v_s2, 5);
2021-09-29 01:27:02 +01:00
/*
* Multiply-add bytes by [ 32, 31, 30, ... ] for s2.
*/
2021-10-13 21:06:33 +01:00
#ifdef _MSC_VER
#ifdef _M_ARM64
2023-09-23 18:55:52 +01:00
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_column_sum_1), neon_ld1m_16((uint16_t[]){32, 31, 30, 29}));
v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_1), neon_ld1m_16((uint16_t[]){28, 27, 26, 25}));
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_column_sum_2), neon_ld1m_16((uint16_t[]){24, 23, 22, 21}));
v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_2), neon_ld1m_16((uint16_t[]){20, 19, 18, 17}));
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_column_sum_3), neon_ld1m_16((uint16_t[]){16, 15, 14, 13}));
v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_3), neon_ld1m_16((uint16_t[]){12, 11, 10, 9}));
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_column_sum_4), neon_ld1m_16((uint16_t[]){8, 7, 6, 5}));
v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_4), neon_ld1m_16((uint16_t[]){4, 3, 2, 1}));
#else
2023-09-23 18:55:52 +01:00
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_column_sum_1), vld1_u16(((uint16_t[]){32, 31, 30, 29})));
v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_1), vld1_u16(((uint16_t[]){28, 27, 26, 25})));
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_column_sum_2), vld1_u16(((uint16_t[]){24, 23, 22, 21})));
v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_2), vld1_u16(((uint16_t[]){20, 19, 18, 17})));
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_column_sum_3), vld1_u16(((uint16_t[]){16, 15, 14, 13})));
v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_3), vld1_u16(((uint16_t[]){12, 11, 10, 9})));
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_column_sum_4), vld1_u16(((uint16_t[]){8, 7, 6, 5})));
v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_4), vld1_u16(((uint16_t[]){4, 3, 2, 1})));
#endif
#else
2023-09-23 18:55:52 +01:00
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_column_sum_1), (uint16x4_t){32, 31, 30, 29});
v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_1), (uint16x4_t){28, 27, 26, 25});
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_column_sum_2), (uint16x4_t){24, 23, 22, 21});
v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_2), (uint16x4_t){20, 19, 18, 17});
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_column_sum_3), (uint16x4_t){16, 15, 14, 13});
v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_3), (uint16x4_t){12, 11, 10, 9});
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_column_sum_4), (uint16x4_t){8, 7, 6, 5});
v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_4), (uint16x4_t){4, 3, 2, 1});
#endif
2021-09-29 01:27:02 +01:00
/*
* Sum epi32 ints v_s1(s2) and accumulate in s1(s2).
*/
2023-09-23 18:55:52 +01:00
uint32x2_t t_s1 = vpadd_u32(vget_low_u32(v_s1), vget_high_u32(v_s1));
uint32x2_t t_s2 = vpadd_u32(vget_low_u32(v_s2), vget_high_u32(v_s2));
uint32x2_t s1s2 = vpadd_u32(t_s1, t_s2);
2021-09-29 01:27:02 +01:00
s1 += vget_lane_u32(s1s2, 0);
s2 += vget_lane_u32(s1s2, 1);
/*
* Reduce.
*/
s1 %= ADLER_MODULE;
s2 %= ADLER_MODULE;
}
/*
* Handle leftover data.
*/
2023-09-23 18:55:52 +01:00
if(len)
2021-09-29 01:27:02 +01:00
{
2023-09-23 18:55:52 +01:00
if(len >= 16)
2021-09-29 01:27:02 +01:00
{
2021-10-13 03:07:04 +01:00
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
s2 += (s1 += *data++);
2021-09-29 01:27:02 +01:00
len -= 16;
}
2023-09-23 18:55:52 +01:00
while(len--)
{ s2 += (s1 += *data++); }
2023-09-23 18:55:52 +01:00
if(s1 >= ADLER_MODULE) s1 -= ADLER_MODULE;
2021-09-29 01:27:02 +01:00
s2 %= ADLER_MODULE;
}
/*
* Return the recombined sums.
*/
*sum1 = s1 & 0xFFFF;
*sum2 = s2 & 0xFFFF;
}
2021-10-13 21:06:33 +01:00
#endif