\hypertarget{Fletcher_8h}{
\section{Fletcher.h File Reference}
\label{Fletcher_8h}\index{Fletcher.h@{Fletcher.h}}
}
Implements the Fletcher checksum algorithm via a series of macros. 



This graph shows which files directly or indirectly include this file:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=105pt]{Fletcher_8h__dep__incl}
\end{center}
\end{figure}
\subsection*{Defines}
\begin{CompactItemize}
\item 
\#define \hyperlink{Fletcher_8h_a0}{Fletcher\-Declare}(\_\-prefix)
\begin{CompactList}\small\item\em Declares the variables needed to implement the Fletcher Checksum. \item\end{CompactList}\item 
\#define \hyperlink{Fletcher_8h_a1}{Fletcher\-Initialize}(\_\-prefix)
\begin{CompactList}\small\item\em Initializes the specified set of Fletcher variables. These must have been declared by Fletcher\-Declare. \item\end{CompactList}\item 
\#define \hyperlink{Fletcher_8h_a2}{Fletcher\-Construct}(\_\-prefix)
\begin{CompactList}\small\item\em Declares and initializes the variables needed to implement the Fletcher Checksum. \item\end{CompactList}\item 
\#define {\bf \_\-Fletcher\-Reduce}(\_\-prefix)
\item 
\#define \hyperlink{Fletcher_8h_a4}{Fletcher\-Update}(\_\-prefix, \_\-val)
\begin{CompactList}\small\item\em Updates the specified set of Fletcher variables with {\em val\/}. \item\end{CompactList}\item 
\#define \hyperlink{Fletcher_8h_a5}{Fletcher\-Finalize}(\_\-prefix)
\begin{CompactList}\small\item\em Finalizes the Fletcher checksum, producing one 32-bit result. \item\end{CompactList}\end{CompactItemize}


\subsection{Detailed Description}
Implements the Fletcher checksum algorithm via a series of macros. 

\begin{Desc}
\item[Author:]JJRussell - \href{mailto:russell@slac.stanford.edu}{\tt russell@slac.stanford.edu}\end{Desc}


\footnotesize\begin{verbatim}   CVS $Id: Fletcher.h,v 1.1 2006/11/01 21:57:27 russell Exp $
\end{verbatim}
\normalsize


The Fletcher checksum is well documented elsewhere. The reasons it was picked here are\begin{enumerate}
\item It is reasonably fast\item It works on 16-bit data\item It can never produce 0 as a checksum value, so 0 can be used to indicate a not computed checksum\end{enumerate}


This is implemented as a series of macros so that it may be embedded in other code.

\begin{Desc}
\item[Usage]There are 4 macros\begin{enumerate}
\item Fletcher\-Declare\item Fletcher\-Initialize\item Fletcher\-Update\item Fletcher\-Finialize\end{enumerate}
\end{Desc}
plus one convenience macro that, in effect combines Fletcher\-Declare and Fletch\-Initialize\begin{enumerate}
\item Fletcher\-Construct\end{enumerate}


The first parameter of each macro is an prefix to uniquely identify the set of 3 variables needed compute the Fletcher checksum. The update macro takes, as a second parameter, the value used in updating the checksum.

A trivial example would be



\footnotesize\begin{verbatim}   unsigned int fetcher_compute (const unsigned short int *array, int cnt)
   {
       FletcherDeclare    (fcs);
       FletcherInitialize (fcs);
       while (--cnt >= 0)
       {
          FletcherUpdate (fcs, *array++)
       }

       return FletchFinalize (fcs);
   }
\end{verbatim}
\normalsize


Note that this would not be a very good implementation for something this trivial. The reason is that there is a check buried in the Fletcher\-Update macro. But the point of these macros is to able to compute a checksum when the data is not a neatly organized array.

\subsection{Define Documentation}
\hypertarget{Fletcher_8h_a3}{
\index{Fletcher.h@{Fletcher.h}!_FletcherReduce@{\_\-FletcherReduce}}
\index{_FletcherReduce@{\_\-FletcherReduce}!Fletcher.h@{Fletcher.h}}
\subsubsection[\_\-FletcherReduce]{\setlength{\rightskip}{0pt plus 5cm}\#define \_\-Fletcher\-Reduce(\_\-prefix)}}
\label{Fletcher_8h_a3}


{\bf Value:}

\footnotesize\begin{verbatim}_prefix ## sum1 = (_prefix ## sum1 &0xffff) + (_prefix ## sum1 >> 16),  \
  _prefix ## sum2 = (_prefix ## sum2 &0xffff) + (_prefix ## sum2 >> 16)   \
\end{verbatim}\normalsize 
\hypertarget{Fletcher_8h_a2}{
\index{Fletcher.h@{Fletcher.h}!FletcherConstruct@{FletcherConstruct}}
\index{FletcherConstruct@{FletcherConstruct}!Fletcher.h@{Fletcher.h}}
\subsubsection[FletcherConstruct]{\setlength{\rightskip}{0pt plus 5cm}\#define Fletcher\-Construct(\_\-prefix)}}
\label{Fletcher_8h_a2}


{\bf Value:}

\footnotesize\begin{verbatim}unsigned int _prefix ## sum1 = 0xffff;         \
    unsigned int _prefix ## sum2 = 0xffff;         \
    int          _prefix ## cnt  = 0               \
\end{verbatim}\normalsize 
Declares and initializes the variables needed to implement the Fletcher Checksum. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em \_\-prefix}]The prefix used to uniquely identify the variables\end{description}
\end{Desc}
This macro is more for convenience, essentially combining Fletcher\-Declare and Fletch\-Initialize.\hypertarget{Fletcher_8h_a0}{
\index{Fletcher.h@{Fletcher.h}!FletcherDeclare@{FletcherDeclare}}
\index{FletcherDeclare@{FletcherDeclare}!Fletcher.h@{Fletcher.h}}
\subsubsection[FletcherDeclare]{\setlength{\rightskip}{0pt plus 5cm}\#define Fletcher\-Declare(\_\-prefix)}}
\label{Fletcher_8h_a0}


{\bf Value:}

\footnotesize\begin{verbatim}unsigned int _prefix ## sum1;                  \
    unsigned int _prefix ## sum2;                  \
    int          _prefix ## cnt                    \
\end{verbatim}\normalsize 
Declares the variables needed to implement the Fletcher Checksum. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em \_\-prefix}]The prefix used to uniquely identify the variables\end{description}
\end{Desc}
\hypertarget{Fletcher_8h_a5}{
\index{Fletcher.h@{Fletcher.h}!FletcherFinalize@{FletcherFinalize}}
\index{FletcherFinalize@{FletcherFinalize}!Fletcher.h@{Fletcher.h}}
\subsubsection[FletcherFinalize]{\setlength{\rightskip}{0pt plus 5cm}\#define Fletcher\-Finalize(\_\-prefix)}}
\label{Fletcher_8h_a5}


{\bf Value:}

\footnotesize\begin{verbatim}( ((_prefix ## cnt) ? _FletcherReduce(_prefix) : 0),                     \
   _FletcherReduce (_prefix),                                             \
   ((_prefix ## sum2 << 16) | _prefix ## sum1))
\end{verbatim}\normalsize 
Finalizes the Fletcher checksum, producing one 32-bit result. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em \_\-prefix}]A prefix used to make the local names used by this macro unique\end{description}
\end{Desc}
\hypertarget{Fletcher_8h_a1}{
\index{Fletcher.h@{Fletcher.h}!FletcherInitialize@{FletcherInitialize}}
\index{FletcherInitialize@{FletcherInitialize}!Fletcher.h@{Fletcher.h}}
\subsubsection[FletcherInitialize]{\setlength{\rightskip}{0pt plus 5cm}\#define Fletcher\-Initialize(\_\-prefix)}}
\label{Fletcher_8h_a1}


{\bf Value:}

\footnotesize\begin{verbatim}_prefix ## sum1 = 0xffff;                       \
   _prefix ## sum2 = 0xffff;                       \
   _prefix ## cnt  = 0
\end{verbatim}\normalsize 
Initializes the specified set of Fletcher variables. These must have been declared by Fletcher\-Declare. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em \_\-prefix}]The prefix used to uniquely identify the variables\end{description}
\end{Desc}
\hypertarget{Fletcher_8h_a4}{
\index{Fletcher.h@{Fletcher.h}!FletcherUpdate@{FletcherUpdate}}
\index{FletcherUpdate@{FletcherUpdate}!Fletcher.h@{Fletcher.h}}
\subsubsection[FletcherUpdate]{\setlength{\rightskip}{0pt plus 5cm}\#define Fletcher\-Update(\_\-prefix, \_\-val)}}
\label{Fletcher_8h_a4}


{\bf Value:}

\footnotesize\begin{verbatim}_prefix ## sum1 += (_val);                                               \
 _prefix ## sum2 += _prefix ## sum1;                                      \
 _prefix ## cnt  += 1;                                                    \
  if (_prefix ## cnt == 360)                                              \
  {                                                                       \
      _FletcherReduce(_prefix);                                           \
      _prefix ## cnt  = 0;                                                \
  }
\end{verbatim}\normalsize 
Updates the specified set of Fletcher variables with {\em val\/}. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em \_\-prefix}]The prefix used to uniquely identify the variables \item[{\em \_\-val}]The 16 bit value used to update the checksum\end{description}
\end{Desc}
