monotone

monotone Mtn Source Tree

Root/botan/mp_shift.cpp

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

Archive Download this file

Branches

Tags

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