\doxysubsection{rollsum.\+c}
\label{rollsum_8c_source}\index{rollsum.c@{rollsum.c}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001 \textcolor{comment}{/*= -\/*-\/ c-\/basic-\/offset: 4; indent-\/tabs-\/mode: nil; -\/*-\/}}
\DoxyCodeLine{00002 \textcolor{comment}{ *}}
\DoxyCodeLine{00003 \textcolor{comment}{ * rollsum -\/-\/ the librsync rolling checksum}}
\DoxyCodeLine{00004 \textcolor{comment}{ *}}
\DoxyCodeLine{00005 \textcolor{comment}{ * Copyright (C) 2003 by Donovan Baarda <abo@minkirri.apana.org.au>}}
\DoxyCodeLine{00006 \textcolor{comment}{ * based on work, Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net>}}
\DoxyCodeLine{00007 \textcolor{comment}{ *}}
\DoxyCodeLine{00008 \textcolor{comment}{ * This program is free software; you can redistribute it and/or modify}}
\DoxyCodeLine{00009 \textcolor{comment}{ * it under the terms of the GNU Lesser General Public License as published by}}
\DoxyCodeLine{00010 \textcolor{comment}{ * the Free Software Foundation; either version 2.1 of the License, or}}
\DoxyCodeLine{00011 \textcolor{comment}{ * (at your option) any later version.}}
\DoxyCodeLine{00012 \textcolor{comment}{ *}}
\DoxyCodeLine{00013 \textcolor{comment}{ * This program is distributed in the hope that it will be useful,}}
\DoxyCodeLine{00014 \textcolor{comment}{ * but WITHOUT ANY WARRANTY; without even the implied warranty of}}
\DoxyCodeLine{00015 \textcolor{comment}{ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the}}
\DoxyCodeLine{00016 \textcolor{comment}{ * GNU Lesser General Public License for more details.}}
\DoxyCodeLine{00017 \textcolor{comment}{ *}}
\DoxyCodeLine{00018 \textcolor{comment}{ * You should have received a copy of the GNU Lesser General Public License}}
\DoxyCodeLine{00019 \textcolor{comment}{ * along with this program; if not, write to the Free Software}}
\DoxyCodeLine{00020 \textcolor{comment}{ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.}}
\DoxyCodeLine{00021 \textcolor{comment}{ */}}
\DoxyCodeLine{00022 \textcolor{preprocessor}{\#include "{}config.h"{}}             \textcolor{comment}{/* IWYU pragma: keep */}}
\DoxyCodeLine{00023 \textcolor{preprocessor}{\#include "{}rollsum.h"{}}}
\DoxyCodeLine{00024 }
\DoxyCodeLine{00025 \textcolor{preprocessor}{\#define DO1(buf,i)  \{s1 += buf[i]; s2 += s1;\}}}
\DoxyCodeLine{00026 \textcolor{preprocessor}{\#define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);}}
\DoxyCodeLine{00027 \textcolor{preprocessor}{\#define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);}}
\DoxyCodeLine{00028 \textcolor{preprocessor}{\#define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);}}
\DoxyCodeLine{00029 \textcolor{preprocessor}{\#define DO16(buf)   DO8(buf,0); DO8(buf,8);}}
\DoxyCodeLine{00030 }
\DoxyCodeLine{00031 \textcolor{keywordtype}{void} RollsumUpdate(Rollsum *sum, \textcolor{keyword}{const} \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{char} *buf, \textcolor{keywordtype}{size\_t} len)}
\DoxyCodeLine{00032 \{}
\DoxyCodeLine{00033     \textcolor{comment}{/* ANSI C says no overflow for unsigned. zlib's adler32 goes to extra}}
\DoxyCodeLine{00034 \textcolor{comment}{       effort to avoid overflow for its mod prime, which we don't have. */}}
\DoxyCodeLine{00035     \textcolor{keywordtype}{size\_t} n = len;}
\DoxyCodeLine{00036     uint\_fast16\_t s1 = sum-\/>s1;}
\DoxyCodeLine{00037     uint\_fast16\_t s2 = sum-\/>s2;}
\DoxyCodeLine{00038 }
\DoxyCodeLine{00039     \textcolor{keywordflow}{while} (n >= 16) \{}
\DoxyCodeLine{00040         DO16(buf);}
\DoxyCodeLine{00041         buf += 16;}
\DoxyCodeLine{00042         n -\/= 16;}
\DoxyCodeLine{00043     \}}
\DoxyCodeLine{00044     \textcolor{keywordflow}{while} (n != 0) \{}
\DoxyCodeLine{00045         s1 += *buf++;}
\DoxyCodeLine{00046         s2 += s1;}
\DoxyCodeLine{00047         n-\/-\/;}
\DoxyCodeLine{00048     \}}
\DoxyCodeLine{00049     \textcolor{comment}{/* Increment s1 and s2 by the amounts added by the char offset. */}}
\DoxyCodeLine{00050     s1 += (uint\_fast16\_t)len * ROLLSUM\_CHAR\_OFFSET;}
\DoxyCodeLine{00051     s2 += (uint\_fast16\_t)((len * (len + 1)) / 2) * ROLLSUM\_CHAR\_OFFSET;}
\DoxyCodeLine{00052     sum-\/>count += len;          \textcolor{comment}{/* Increment sum count. */}}
\DoxyCodeLine{00053     sum-\/>s1 = s1;}
\DoxyCodeLine{00054     sum-\/>s2 = s2;}
\DoxyCodeLine{00055 \}}

\end{DoxyCode}
