monotone

monotone Mtn Source Tree

Root/cryptopp/words.h

1#ifndef CRYPTOPP_WORDS_H
2#define CRYPTOPP_WORDS_H
3
4#include "misc.h"
5
6NAMESPACE_BEGIN(CryptoPP)
7
8inline unsigned int CountWords(const word *X, unsigned int N)
9{
10while (N && X[N-1]==0)
11N--;
12return N;
13}
14
15inline void SetWords(word *r, word a, unsigned int n)
16{
17for (unsigned int i=0; i<n; i++)
18r[i] = a;
19}
20
21inline void CopyWords(word *r, const word *a, unsigned int n)
22{
23for (unsigned int i=0; i<n; i++)
24r[i] = a[i];
25}
26
27inline void XorWords(word *r, const word *a, const word *b, unsigned int n)
28{
29for (unsigned int i=0; i<n; i++)
30r[i] = a[i] ^ b[i];
31}
32
33inline void XorWords(word *r, const word *a, unsigned int n)
34{
35for (unsigned int i=0; i<n; i++)
36r[i] ^= a[i];
37}
38
39inline void AndWords(word *r, const word *a, const word *b, unsigned int n)
40{
41for (unsigned int i=0; i<n; i++)
42r[i] = a[i] & b[i];
43}
44
45inline void AndWords(word *r, const word *a, unsigned int n)
46{
47for (unsigned int i=0; i<n; i++)
48r[i] &= a[i];
49}
50
51inline word ShiftWordsLeftByBits(word *r, unsigned int n, unsigned int shiftBits)
52{
53assert (shiftBits<WORD_BITS);
54word u, carry=0;
55if (shiftBits)
56for (unsigned int i=0; i<n; i++)
57{
58u = r[i];
59r[i] = (u << shiftBits) | carry;
60carry = u >> (WORD_BITS-shiftBits);
61}
62return carry;
63}
64
65inline word ShiftWordsRightByBits(word *r, unsigned int n, unsigned int shiftBits)
66{
67assert (shiftBits<WORD_BITS);
68word u, carry=0;
69if (shiftBits)
70for (int i=n-1; i>=0; i--)
71{
72u = r[i];
73r[i] = (u >> shiftBits) | carry;
74carry = u << (WORD_BITS-shiftBits);
75}
76return carry;
77}
78
79inline void ShiftWordsLeftByWords(word *r, unsigned int n, unsigned int shiftWords)
80{
81shiftWords = STDMIN(shiftWords, n);
82if (shiftWords)
83{
84for (unsigned int i=n-1; i>=shiftWords; i--)
85r[i] = r[i-shiftWords];
86SetWords(r, 0, shiftWords);
87}
88}
89
90inline void ShiftWordsRightByWords(word *r, unsigned int n, unsigned int shiftWords)
91{
92shiftWords = STDMIN(shiftWords, n);
93if (shiftWords)
94{
95for (unsigned int i=0; i+shiftWords<n; i++)
96r[i] = r[i+shiftWords];
97SetWords(r+n-shiftWords, 0, shiftWords);
98}
99}
100
101NAMESPACE_END
102
103#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status