mirror of
https://github.com/aaru-dps/Aaru.Checksums.git
synced 2025-12-16 19:24:29 +00:00
Implement Fletcher-32 using ARM NEON instructions.
This commit is contained in:
223
Fletcher32/neon.cs
Normal file
223
Fletcher32/neon.cs
Normal file
@@ -0,0 +1,223 @@
|
||||
// /***************************************************************************
|
||||
// Aaru Data Preservation Suite
|
||||
// ----------------------------------------------------------------------------
|
||||
//
|
||||
// Filename : neon.cs
|
||||
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
||||
// The Chromium Authors
|
||||
//
|
||||
// Component : Checksums.
|
||||
//
|
||||
// --[ Description ] ----------------------------------------------------------
|
||||
//
|
||||
// Compute Fletcher32 checksum using NEON vectorization.
|
||||
//
|
||||
// --[ License ] --------------------------------------------------------------
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// Copyright © 2011-2023 Natalia Portillo
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// ****************************************************************************/
|
||||
|
||||
using System.Runtime.Intrinsics;
|
||||
using System.Runtime.Intrinsics.Arm;
|
||||
|
||||
namespace Aaru.Checksums.Fletcher32;
|
||||
|
||||
static class Neon
|
||||
{
|
||||
internal static void Step(ref ushort preSum1, ref ushort preSum2, byte[] buf, uint len)
|
||||
{
|
||||
/*
|
||||
* Split Fletcher-32 into component sums.
|
||||
*/
|
||||
uint s1 = preSum1;
|
||||
uint s2 = preSum2;
|
||||
|
||||
int bufPos = 0;
|
||||
|
||||
/*
|
||||
* Process the data in blocks.
|
||||
*/
|
||||
uint BLOCK_SIZE = 1 << 5;
|
||||
uint blocks = len / BLOCK_SIZE;
|
||||
len -= blocks * BLOCK_SIZE;
|
||||
|
||||
while(blocks != 0)
|
||||
{
|
||||
uint n = Fletcher32Context.NMAX / BLOCK_SIZE; /* The NMAX constraint. */
|
||||
|
||||
if(n > blocks)
|
||||
n = blocks;
|
||||
|
||||
blocks -= n;
|
||||
/*
|
||||
* Process n blocks of data. At most NMAX data bytes can be
|
||||
* processed before s2 must be reduced modulo FLETCHER_MODULE.
|
||||
*/
|
||||
Vector128<uint> v_s2 = Vector128.Create(s1 * n, 0, 0, 0);
|
||||
Vector128<uint> v_s1 = Vector128.Create(0u, 0, 0, 0);
|
||||
Vector128<ushort> v_column_sum_1 = AdvSimd.DuplicateToVector128((ushort)0);
|
||||
Vector128<ushort> v_column_sum_2 = AdvSimd.DuplicateToVector128((ushort)0);
|
||||
Vector128<ushort> v_column_sum_3 = AdvSimd.DuplicateToVector128((ushort)0);
|
||||
Vector128<ushort> v_column_sum_4 = AdvSimd.DuplicateToVector128((ushort)0);
|
||||
|
||||
do
|
||||
{
|
||||
/*
|
||||
* Load 32 input bytes.
|
||||
*/
|
||||
Vector128<byte> bytes1 = Vector128.Create(buf[bufPos], buf[bufPos + 1], buf[bufPos + 2],
|
||||
buf[bufPos + 3], buf[bufPos + 4], buf[bufPos + 5],
|
||||
buf[bufPos + 6], buf[bufPos + 7], buf[bufPos + 8],
|
||||
buf[bufPos + 9], buf[bufPos + 10], buf[bufPos + 11],
|
||||
buf[bufPos + 12], buf[bufPos + 13], buf[bufPos + 14],
|
||||
buf[bufPos + 15]);
|
||||
|
||||
bufPos += 16;
|
||||
|
||||
Vector128<byte> bytes2 = Vector128.Create(buf[bufPos], buf[bufPos + 1], buf[bufPos + 2],
|
||||
buf[bufPos + 3], buf[bufPos + 4], buf[bufPos + 5],
|
||||
buf[bufPos + 6], buf[bufPos + 7], buf[bufPos + 8],
|
||||
buf[bufPos + 9], buf[bufPos + 10], buf[bufPos + 11],
|
||||
buf[bufPos + 12], buf[bufPos + 13], buf[bufPos + 14],
|
||||
buf[bufPos + 15]);
|
||||
|
||||
bufPos += 16;
|
||||
/*
|
||||
* Add previous block byte sum to v_s2.
|
||||
*/
|
||||
v_s2 = AdvSimd.Add(v_s2, v_s1);
|
||||
|
||||
/*
|
||||
* Horizontally add the bytes for s1.
|
||||
*/
|
||||
v_s1 =
|
||||
AdvSimd.AddPairwiseWideningAndAdd(v_s1,
|
||||
AdvSimd.
|
||||
AddPairwiseWideningAndAdd(AdvSimd.AddPairwiseWidening(bytes1),
|
||||
bytes2));
|
||||
|
||||
/*
|
||||
* Vertically add the bytes for s2.
|
||||
*/
|
||||
v_column_sum_1 = AdvSimd.AddWideningLower(v_column_sum_1, bytes1.GetLower());
|
||||
v_column_sum_2 = AdvSimd.AddWideningLower(v_column_sum_2, bytes1.GetUpper());
|
||||
v_column_sum_3 = AdvSimd.AddWideningLower(v_column_sum_3, bytes2.GetLower());
|
||||
v_column_sum_4 = AdvSimd.AddWideningLower(v_column_sum_4, bytes2.GetUpper());
|
||||
} while(--n != 0);
|
||||
|
||||
v_s2 = AdvSimd.ShiftLeftLogical(v_s2, 5);
|
||||
|
||||
/*
|
||||
* Multiply-add bytes by [ 32, 31, 30, ... ] for s2.
|
||||
*/
|
||||
v_s2 = AdvSimd.MultiplyWideningLowerAndAdd(v_s2, v_column_sum_1.GetLower(),
|
||||
Vector64.Create((ushort)32, 31, 30, 29));
|
||||
|
||||
v_s2 = AdvSimd.MultiplyWideningLowerAndAdd(v_s2, v_column_sum_1.GetUpper(),
|
||||
Vector64.Create((ushort)28, 27, 26, 25));
|
||||
|
||||
v_s2 = AdvSimd.MultiplyWideningLowerAndAdd(v_s2, v_column_sum_2.GetLower(),
|
||||
Vector64.Create((ushort)24, 23, 22, 21));
|
||||
|
||||
v_s2 = AdvSimd.MultiplyWideningLowerAndAdd(v_s2, v_column_sum_2.GetUpper(),
|
||||
Vector64.Create((ushort)20, 19, 18, 17));
|
||||
|
||||
v_s2 = AdvSimd.MultiplyWideningLowerAndAdd(v_s2, v_column_sum_3.GetLower(),
|
||||
Vector64.Create((ushort)16, 15, 14, 13));
|
||||
|
||||
v_s2 = AdvSimd.MultiplyWideningLowerAndAdd(v_s2, v_column_sum_3.GetUpper(),
|
||||
Vector64.Create((ushort)12, 11, 10, 9));
|
||||
|
||||
v_s2 = AdvSimd.MultiplyWideningLowerAndAdd(v_s2, v_column_sum_4.GetLower(),
|
||||
Vector64.Create((ushort)8, 7, 6, 5));
|
||||
|
||||
v_s2 = AdvSimd.MultiplyWideningLowerAndAdd(v_s2, v_column_sum_4.GetUpper(),
|
||||
Vector64.Create((ushort)4, 3, 2, 1));
|
||||
|
||||
/*
|
||||
* Sum epi32 ints v_s1(s2) and accumulate in s1(s2).
|
||||
*/
|
||||
Vector64<uint> sum1 = AdvSimd.AddPairwise(v_s1.GetLower(), v_s1.GetUpper());
|
||||
Vector64<uint> sum2 = AdvSimd.AddPairwise(v_s2.GetLower(), v_s2.GetUpper());
|
||||
Vector64<uint> s1s2 = AdvSimd.AddPairwise(sum1, sum2);
|
||||
s1 += AdvSimd.Extract(s1s2, 0);
|
||||
s2 += AdvSimd.Extract(s1s2, 1);
|
||||
/*
|
||||
* Reduce.
|
||||
*/
|
||||
s1 %= Fletcher32Context.FLETCHER_MODULE;
|
||||
s2 %= Fletcher32Context.FLETCHER_MODULE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle leftover data.
|
||||
*/
|
||||
if(len != 0)
|
||||
{
|
||||
if(len >= 16)
|
||||
{
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
s2 += s1 += buf[bufPos++];
|
||||
len -= 16;
|
||||
}
|
||||
|
||||
while(len-- != 0)
|
||||
{
|
||||
s2 += s1 += buf[bufPos++];
|
||||
}
|
||||
|
||||
if(s1 >= Fletcher32Context.FLETCHER_MODULE)
|
||||
s1 -= Fletcher32Context.FLETCHER_MODULE;
|
||||
|
||||
s2 %= Fletcher32Context.FLETCHER_MODULE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the recombined sums.
|
||||
*/
|
||||
preSum1 = (ushort)(s1 & 0xFFFF);
|
||||
preSum2 = (ushort)(s2 & 0xFFFF);
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,9 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics.Arm;
|
||||
using System.Text;
|
||||
using Aaru.Checksums.Fletcher32;
|
||||
using Aaru.CommonTypes.Interfaces;
|
||||
using Aaru.Helpers;
|
||||
|
||||
@@ -45,11 +47,11 @@ namespace Aaru.Checksums;
|
||||
/// <summary>Implements the Fletcher-32 algorithm</summary>
|
||||
public sealed class Fletcher32Context : IChecksum
|
||||
{
|
||||
const ushort FLETCHER_MODULE = 0xFFFF;
|
||||
const uint NMAX = 5552;
|
||||
readonly IntPtr _nativeContext;
|
||||
readonly bool _useNative;
|
||||
ushort _sum1, _sum2;
|
||||
internal const ushort FLETCHER_MODULE = 0xFFFF;
|
||||
internal const uint NMAX = 5552;
|
||||
readonly IntPtr _nativeContext;
|
||||
readonly bool _useNative;
|
||||
ushort _sum1, _sum2;
|
||||
|
||||
/// <summary>Initializes the Fletcher-32 sums</summary>
|
||||
public Fletcher32Context()
|
||||
@@ -79,7 +81,7 @@ public sealed class Fletcher32Context : IChecksum
|
||||
/// <summary>Returns a byte array of the hash value.</summary>
|
||||
public byte[] Final()
|
||||
{
|
||||
var finalSum = (uint)((_sum2 << 16) | _sum1);
|
||||
uint finalSum = (uint)((_sum2 << 16) | _sum1);
|
||||
|
||||
if(!_useNative)
|
||||
return BigEndianBitConverter.GetBytes(finalSum);
|
||||
@@ -94,7 +96,7 @@ public sealed class Fletcher32Context : IChecksum
|
||||
/// <summary>Returns a hexadecimal representation of the hash value.</summary>
|
||||
public string End()
|
||||
{
|
||||
var finalSum = (uint)((_sum2 << 16) | _sum1);
|
||||
uint finalSum = (uint)((_sum2 << 16) | _sum1);
|
||||
|
||||
if(_useNative)
|
||||
{
|
||||
@@ -104,7 +106,7 @@ public sealed class Fletcher32Context : IChecksum
|
||||
|
||||
var fletcherOutput = new StringBuilder();
|
||||
|
||||
for(var i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
|
||||
for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
|
||||
fletcherOutput.Append(BigEndianBitConverter.GetBytes(finalSum)[i].ToString("x2"));
|
||||
|
||||
return fletcherOutput.ToString();
|
||||
@@ -132,9 +134,16 @@ public sealed class Fletcher32Context : IChecksum
|
||||
return;
|
||||
}
|
||||
|
||||
if(AdvSimd.IsSupported)
|
||||
{
|
||||
Neon.Step(ref previousSum1, ref previousSum2, data, len);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
uint sum1 = previousSum1;
|
||||
uint sum2 = previousSum2;
|
||||
var dataOff = 0;
|
||||
int dataOff = 0;
|
||||
|
||||
switch(len)
|
||||
{
|
||||
@@ -312,8 +321,8 @@ public sealed class Fletcher32Context : IChecksum
|
||||
ushort localSum1 = 0xFFFF;
|
||||
ushort localSum2 = 0xFFFF;
|
||||
|
||||
var buffer = new byte[65536];
|
||||
int read = fileStream.EnsureRead(buffer, 0, 65536);
|
||||
byte[] buffer = new byte[65536];
|
||||
int read = fileStream.EnsureRead(buffer, 0, 65536);
|
||||
|
||||
while(read > 0)
|
||||
{
|
||||
@@ -322,7 +331,7 @@ public sealed class Fletcher32Context : IChecksum
|
||||
read = fileStream.EnsureRead(buffer, 0, 65536);
|
||||
}
|
||||
|
||||
var finalSum = (uint)((localSum2 << 16) | localSum1);
|
||||
uint finalSum = (uint)((localSum2 << 16) | localSum1);
|
||||
|
||||
if(useNative)
|
||||
{
|
||||
@@ -364,7 +373,7 @@ public sealed class Fletcher32Context : IChecksum
|
||||
|
||||
Step(ref localSum1, ref localSum2, data, len, useNative, nativeContext);
|
||||
|
||||
var finalSum = (uint)((localSum2 << 16) | localSum1);
|
||||
uint finalSum = (uint)((localSum2 << 16) | localSum1);
|
||||
|
||||
if(useNative)
|
||||
{
|
||||
@@ -427,7 +436,7 @@ public sealed class Fletcher16Context : IChecksum
|
||||
/// <summary>Returns a byte array of the hash value.</summary>
|
||||
public byte[] Final()
|
||||
{
|
||||
var finalSum = (ushort)((_sum2 << 8) | _sum1);
|
||||
ushort finalSum = (ushort)((_sum2 << 8) | _sum1);
|
||||
|
||||
if(!_useNative)
|
||||
return BigEndianBitConverter.GetBytes(finalSum);
|
||||
@@ -442,7 +451,7 @@ public sealed class Fletcher16Context : IChecksum
|
||||
/// <summary>Returns a hexadecimal representation of the hash value.</summary>
|
||||
public string End()
|
||||
{
|
||||
var finalSum = (ushort)((_sum2 << 8) | _sum1);
|
||||
ushort finalSum = (ushort)((_sum2 << 8) | _sum1);
|
||||
|
||||
if(_useNative)
|
||||
{
|
||||
@@ -452,7 +461,7 @@ public sealed class Fletcher16Context : IChecksum
|
||||
|
||||
var fletcherOutput = new StringBuilder();
|
||||
|
||||
for(var i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
|
||||
for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
|
||||
fletcherOutput.Append(BigEndianBitConverter.GetBytes(finalSum)[i].ToString("x2"));
|
||||
|
||||
return fletcherOutput.ToString();
|
||||
@@ -482,7 +491,7 @@ public sealed class Fletcher16Context : IChecksum
|
||||
|
||||
uint sum1 = previousSum1;
|
||||
uint sum2 = previousSum2;
|
||||
var dataOff = 0;
|
||||
int dataOff = 0;
|
||||
|
||||
switch(len)
|
||||
{
|
||||
@@ -640,8 +649,8 @@ public sealed class Fletcher16Context : IChecksum
|
||||
byte localSum1 = 0xFF;
|
||||
byte localSum2 = 0xFF;
|
||||
|
||||
var buffer = new byte[65536];
|
||||
int read = fileStream.EnsureRead(buffer, 0, 65536);
|
||||
byte[] buffer = new byte[65536];
|
||||
int read = fileStream.EnsureRead(buffer, 0, 65536);
|
||||
|
||||
while(read > 0)
|
||||
{
|
||||
@@ -650,7 +659,7 @@ public sealed class Fletcher16Context : IChecksum
|
||||
read = fileStream.EnsureRead(buffer, 0, 65536);
|
||||
}
|
||||
|
||||
var finalSum = (ushort)((localSum2 << 8) | localSum1);
|
||||
ushort finalSum = (ushort)((localSum2 << 8) | localSum1);
|
||||
|
||||
if(useNative)
|
||||
{
|
||||
@@ -692,7 +701,7 @@ public sealed class Fletcher16Context : IChecksum
|
||||
|
||||
Step(ref localSum1, ref localSum2, data, len, useNative, nativeContext);
|
||||
|
||||
var finalSum = (ushort)((localSum2 << 8) | localSum1);
|
||||
ushort finalSum = (ushort)((localSum2 << 8) | localSum1);
|
||||
|
||||
if(useNative)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user