monotone

monotone Mtn Source Tree

Root/botan/mp_shift.cpp

1/*************************************************
2* MP Shift Algorithms Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/mp_core.h>
7#include <botan/mem_ops.h>
8
9namespace Botan {
10
11extern "C" {
12
13/*************************************************
14* Single Operand Left Shift *
15*************************************************/
16void bigint_shl1(word x[], u32bit x_size, u32bit word_shift, u32bit bit_shift)
17 {
18 if(word_shift)
19 {
20 for(u32bit j = 1; j != x_size + 1; ++j)
21 x[(x_size - j) + word_shift] = x[x_size - j];
22 clear_mem(x, word_shift);
23 }
24
25 if(bit_shift)
26 {
27 word carry = 0;
28 for(u32bit j = word_shift; j != x_size + word_shift + 1; ++j)
29 {
30 word temp = x[j];
31 x[j] = (temp << bit_shift) | carry;
32 carry = (temp >> (MP_WORD_BITS - bit_shift));
33 }
34 }
35 }
36
37/*************************************************
38* Single Operand Right Shift *
39*************************************************/
40void bigint_shr1(word x[], u32bit x_size, u32bit word_shift, u32bit bit_shift)
41 {
42 if(x_size < word_shift)
43 {
44 clear_mem(x, x_size);
45 return;
46 }
47
48 if(word_shift)
49 {
50 for(u32bit j = 0; j != x_size - word_shift; ++j)
51 x[j] = x[j + word_shift];
52 for(u32bit j = x_size - word_shift; j != x_size; ++j)
53 x[j] = 0;
54 }
55
56 if(bit_shift)
57 {
58 word carry = 0;
59 for(u32bit j = x_size - word_shift; j > 0; --j)
60 {
61 word temp = x[j-1];
62 x[j-1] = (temp >> bit_shift) | carry;
63 carry = (temp << (MP_WORD_BITS - bit_shift));
64 }
65 }
66 }
67
68/*************************************************
69* Two Operand Left Shift *
70*************************************************/
71void bigint_shl2(word y[], const word x[], u32bit x_size,
72 u32bit word_shift, u32bit bit_shift)
73 {
74 for(u32bit j = 0; j != x_size; ++j)
75 y[j + word_shift] = x[j];
76 if(bit_shift)
77 {
78 word carry = 0;
79 for(u32bit j = word_shift; j != x_size + word_shift + 1; ++j)
80 {
81 word temp = y[j];
82 y[j] = (temp << bit_shift) | carry;
83 carry = (temp >> (MP_WORD_BITS - bit_shift));
84 }
85 }
86 }
87
88/*************************************************
89* Two Operand Right Shift *
90*************************************************/
91void bigint_shr2(word y[], const word x[], u32bit x_size,
92 u32bit word_shift, u32bit bit_shift)
93 {
94 if(x_size < word_shift) return;
95
96 for(u32bit j = 0; j != x_size - word_shift; ++j)
97 y[j] = x[j + word_shift];
98 if(bit_shift)
99 {
100 word carry = 0;
101 for(u32bit j = x_size - word_shift; j > 0; --j)
102 {
103 word temp = y[j-1];
104 y[j-1] = (temp >> bit_shift) | carry;
105 carry = (temp << (MP_WORD_BITS - bit_shift));
106 }
107 }
108 }
109
110}
111
112}

Archive Download this file

Branches

Tags

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