• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE Support
  • Sitemap
  • Contact Us
 

qca

big_ops3.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 1999-2007 The Botan Project. All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, for any use, with or without
00005 modification, is permitted provided that the following conditions are met:
00006 
00007 1. Redistributions of source code must retain the above copyright notice, this
00008 list of conditions, and the following disclaimer.
00009 
00010 2. Redistributions in binary form must reproduce the above copyright notice,
00011 this list of conditions, and the following disclaimer in the documentation
00012 and/or other materials provided with the distribution.
00013 
00014 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED
00015 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00016 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
00017 
00018 IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT,
00019 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00020 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00021 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00022 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00023 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00024 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025 */
00026 // LICENSEHEADER_END
00027 namespace QCA { // WRAPNS_LINE
00028 /*************************************************
00029 * BigInt Binary Operators Source File            *
00030 * (C) 1999-2007 The Botan Project                *
00031 *************************************************/
00032 
00033 } // WRAPNS_LINE
00034 #include <botan/bigint.h>
00035 namespace QCA { // WRAPNS_LINE
00036 } // WRAPNS_LINE
00037 #include <botan/numthry.h>
00038 namespace QCA { // WRAPNS_LINE
00039 } // WRAPNS_LINE
00040 #include <botan/mp_core.h>
00041 namespace QCA { // WRAPNS_LINE
00042 } // WRAPNS_LINE
00043 #include <botan/bit_ops.h>
00044 namespace QCA { // WRAPNS_LINE
00045 } // WRAPNS_LINE
00046 #include <algorithm>
00047 namespace QCA { // WRAPNS_LINE
00048 
00049 namespace Botan {
00050 
00051 /*************************************************
00052 * Addition Operator                              *
00053 *************************************************/
00054 BigInt operator+(const BigInt& x, const BigInt& y)
00055    {
00056    const u32bit x_sw = x.sig_words(), y_sw = y.sig_words();
00057 
00058 #ifdef BOTAN_TYPES_QT
00059    BigInt z(x.sign(), qMax(x_sw, y_sw) + 1);
00060 #else
00061    BigInt z(x.sign(), std::max(x_sw, y_sw) + 1);
00062 #endif
00063 
00064    if((x.sign() == y.sign()))
00065       bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
00066    else
00067       {
00068       s32bit relative_size = bigint_cmp(x.data(), x_sw, y.data(), y_sw);
00069 
00070       if(relative_size < 0)
00071          {
00072          bigint_sub3(z.get_reg(), y.data(), y_sw, x.data(), x_sw);
00073          z.set_sign(y.sign());
00074          }
00075       else if(relative_size == 0)
00076          z.set_sign(BigInt::Positive);
00077       else if(relative_size > 0)
00078          bigint_sub3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
00079       }
00080 
00081    return z;
00082    }
00083 
00084 /*************************************************
00085 * Subtraction Operator                           *
00086 *************************************************/
00087 BigInt operator-(const BigInt& x, const BigInt& y)
00088    {
00089    const u32bit x_sw = x.sig_words(), y_sw = y.sig_words();
00090 
00091    s32bit relative_size = bigint_cmp(x.data(), x_sw, y.data(), y_sw);
00092 
00093 #ifdef BOTAN_TYPES_QT
00094    BigInt z(BigInt::Positive, qMax(x_sw, y_sw) + 1);
00095 #else
00096    BigInt z(BigInt::Positive, std::max(x_sw, y_sw) + 1);
00097 #endif
00098 
00099    if(relative_size < 0)
00100       {
00101       if(x.sign() == y.sign())
00102          bigint_sub3(z.get_reg(), y.data(), y_sw, x.data(), x_sw);
00103       else
00104          bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
00105       z.set_sign(y.reverse_sign());
00106       }
00107    else if(relative_size == 0)
00108       {
00109       if(x.sign() != y.sign())
00110          bigint_shl2(z.get_reg(), x.data(), x_sw, 0, 1);
00111       }
00112    else if(relative_size > 0)
00113       {
00114       if(x.sign() == y.sign())
00115          bigint_sub3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
00116       else
00117          bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
00118       z.set_sign(x.sign());
00119       }
00120    return z;
00121    }
00122 
00123 /*************************************************
00124 * Multiplication Operator                        *
00125 *************************************************/
00126 BigInt operator*(const BigInt& x, const BigInt& y)
00127    {
00128    const u32bit x_sw = x.sig_words(), y_sw = y.sig_words();
00129 
00130    BigInt z(BigInt::Positive, x.size() + y.size());
00131 
00132    if(x_sw == 1 && y_sw)
00133       bigint_linmul3(z.get_reg(), y.data(), y_sw, x.word_at(0));
00134    else if(y_sw == 1 && x_sw)
00135       bigint_linmul3(z.get_reg(), x.data(), x_sw, y.word_at(0));
00136    else if(x_sw && y_sw)
00137       {
00138       SecureVector<word> workspace(z.size());
00139       bigint_mul(z.get_reg(), z.size(), workspace,
00140                  x.data(), x.size(), x_sw,
00141                  y.data(), y.size(), y_sw);
00142       }
00143 
00144    if(x_sw && y_sw && x.sign() != y.sign())
00145       z.flip_sign();
00146    return z;
00147    }
00148 
00149 /*************************************************
00150 * Division Operator                              *
00151 *************************************************/
00152 BigInt operator/(const BigInt& x, const BigInt& y)
00153    {
00154    BigInt q, r;
00155    divide(x, y, q, r);
00156    return q;
00157    }
00158 
00159 /*************************************************
00160 * Modulo Operator                                *
00161 *************************************************/
00162 BigInt operator%(const BigInt& n, const BigInt& mod)
00163    {
00164    if(mod.is_zero())
00165       throw BigInt::DivideByZero();
00166    if(mod.is_negative())
00167       throw Invalid_Argument("BigInt::operator%: modulus must be > 0");
00168    if(n.is_positive() && mod.is_positive() && n < mod)
00169       return n;
00170 
00171    BigInt q, r;
00172    divide(n, mod, q, r);
00173    return r;
00174    }
00175 
00176 /*************************************************
00177 * Modulo Operator                                *
00178 *************************************************/
00179 word operator%(const BigInt& n, word mod)
00180    {
00181    if(mod == 0)
00182       throw BigInt::DivideByZero();
00183    if(power_of_2(mod))
00184       return (n.word_at(0) & (mod - 1));
00185 
00186    word remainder = 0;
00187 
00188    for(u32bit j = n.sig_words(); j > 0; --j)
00189       remainder = bigint_modop(remainder, n.word_at(j-1), mod);
00190 
00191    if(remainder && n.sign() == BigInt::Negative)
00192       return mod - remainder;
00193    return remainder;
00194    }
00195 
00196 /*************************************************
00197 * Left Shift Operator                            *
00198 *************************************************/
00199 BigInt operator<<(const BigInt& x, u32bit shift)
00200    {
00201    if(shift == 0)
00202       return x;
00203 
00204    const u32bit shift_words = shift / MP_WORD_BITS,
00205                 shift_bits  = shift % MP_WORD_BITS;
00206 
00207    const u32bit x_sw = x.sig_words();
00208 
00209    BigInt y(x.sign(), x_sw + shift_words + (shift_bits ? 1 : 0));
00210    bigint_shl2(y.get_reg(), x.data(), x_sw, shift_words, shift_bits);
00211    return y;
00212    }
00213 
00214 /*************************************************
00215 * Right Shift Operator                           *
00216 *************************************************/
00217 BigInt operator>>(const BigInt& x, u32bit shift)
00218    {
00219    if(shift == 0)
00220       return x;
00221    if(x.bits() <= shift)
00222       return 0;
00223 
00224    const u32bit shift_words = shift / MP_WORD_BITS,
00225                 shift_bits  = shift % MP_WORD_BITS,
00226                 x_sw = x.sig_words();
00227 
00228    BigInt y(x.sign(), x_sw - shift_words);
00229    bigint_shr2(y.get_reg(), x.data(), x_sw, shift_words, shift_bits);
00230    return y;
00231    }
00232 
00233 }
00234 } // WRAPNS_LINE

qca

Skip menu "qca"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE Support

Skip menu "KDE Support"
  • akonadi
  • Decibel
  • grantlee
  • kdewin
  • phonon
  •     Backend
  • polkit-qt
  • qca
  • qimageblitz
  • soprano
  • strigi
  •     searchclient
  •     streamanalyzer
  •     streams
Generated for KDE Support by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal