mirror of
https://github.com/aaru-dps/Aaru.Checksums.Native.git
synced 2025-12-16 11:14:29 +00:00
Expand Fletcher-16 NMAX
The limit when calculating the value of NMAX is 2^16-1 only when sum1 and sum2 are represented as 16-bit. We're representing them as 32-bit. This will be helpful for SIMD implementations of Fletcher-16, because the old limit of 22 bytes would have meant that we could only process 16 bytes at a time before having to compute modulo.
This commit is contained in:
34
fletcher16.c
34
fletcher16.c
@@ -84,7 +84,7 @@ AARU_EXPORT int AARU_CALL fletcher16_update(fletcher16_ctx *ctx, const uint8_t *
|
||||
}
|
||||
|
||||
/* in case short lengths are provided, keep it somewhat fast */
|
||||
if(len < 11)
|
||||
if(len < 6)
|
||||
{
|
||||
while(len--)
|
||||
{
|
||||
@@ -102,7 +102,7 @@ AARU_EXPORT int AARU_CALL fletcher16_update(fletcher16_ctx *ctx, const uint8_t *
|
||||
while(len >= NMAX)
|
||||
{
|
||||
len -= NMAX;
|
||||
n = NMAX / 11; /* NMAX is divisible by 11 */
|
||||
n = NMAX / 6; /* NMAX is divisible by 6 */
|
||||
do
|
||||
{
|
||||
sum1 += data[0];
|
||||
@@ -117,19 +117,9 @@ AARU_EXPORT int AARU_CALL fletcher16_update(fletcher16_ctx *ctx, const uint8_t *
|
||||
sum2 += sum1;
|
||||
sum1 += data[0 + 4 + 1];
|
||||
sum2 += sum1;
|
||||
sum1 += data[0 + 4 + 2];
|
||||
sum2 += sum1;
|
||||
sum1 += data[0 + 4 + 2 + 1];
|
||||
sum2 += sum1;
|
||||
sum1 += data[8];
|
||||
sum2 += sum1;
|
||||
sum1 += data[8 + 1];
|
||||
sum2 += sum1;
|
||||
sum1 += data[8 + 2];
|
||||
sum2 += sum1;
|
||||
|
||||
/* 11 sums unrolled */
|
||||
data += 11;
|
||||
/* 6 sums unrolled */
|
||||
data += 6;
|
||||
}
|
||||
while(--n);
|
||||
sum1 %= FLETCHER16_MODULE;
|
||||
@@ -139,9 +129,9 @@ AARU_EXPORT int AARU_CALL fletcher16_update(fletcher16_ctx *ctx, const uint8_t *
|
||||
/* do remaining bytes (less than NMAX, still just one modulo) */
|
||||
if(len)
|
||||
{ /* avoid modulos if none remaining */
|
||||
while(len >= 11)
|
||||
while(len >= 6)
|
||||
{
|
||||
len -= 11;
|
||||
len -= 6;
|
||||
sum1 += data[0];
|
||||
sum2 += sum1;
|
||||
sum1 += data[0 + 1];
|
||||
@@ -154,18 +144,8 @@ AARU_EXPORT int AARU_CALL fletcher16_update(fletcher16_ctx *ctx, const uint8_t *
|
||||
sum2 += sum1;
|
||||
sum1 += data[0 + 4 + 1];
|
||||
sum2 += sum1;
|
||||
sum1 += data[0 + 4 + 2];
|
||||
sum2 += sum1;
|
||||
sum1 += data[0 + 4 + 2 + 1];
|
||||
sum2 += sum1;
|
||||
sum1 += data[8];
|
||||
sum2 += sum1;
|
||||
sum1 += data[8 + 1];
|
||||
sum2 += sum1;
|
||||
sum1 += data[8 + 2];
|
||||
sum2 += sum1;
|
||||
|
||||
data += 11;
|
||||
data += 6;
|
||||
}
|
||||
while(len--)
|
||||
{
|
||||
|
||||
@@ -20,8 +20,8 @@
|
||||
#define AARU_CHECKSUMS_NATIVE_FLETCHER16_H
|
||||
|
||||
#define FLETCHER16_MODULE 0xFF
|
||||
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(ADLER_MODULE-1) <= 2^16-1 */
|
||||
#define NMAX 22
|
||||
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(FLETCHER16_MODULE-1) <= 2^32-1 */
|
||||
#define NMAX 5802
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
#define AARU_CHECKSUMS_NATIVE_FLETCHER32_H
|
||||
|
||||
#define FLETCHER32_MODULE 0xFFFF
|
||||
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(ADLER_MODULE-1) <= 2^32-1 */
|
||||
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(FLETCHER32_MODULE-1) <= 2^32-1 */
|
||||
#define NMAX 5552
|
||||
|
||||
typedef struct
|
||||
|
||||
Reference in New Issue
Block a user