Back to home page

OSCL-LXR

 
 

    


0001 /* mpi-cmp.c  -  MPI functions
0002  * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
0003  *
0004  * This file is part of GnuPG.
0005  *
0006  * GnuPG is free software; you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License as published by
0008  * the Free Software Foundation; either version 2 of the License, or
0009  * (at your option) any later version.
0010  *
0011  * GnuPG is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program; if not, write to the Free Software
0018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
0019  */
0020 
0021 #include "mpi-internal.h"
0022 
0023 int mpi_cmp_ui(MPI u, unsigned long v)
0024 {
0025     mpi_limb_t limb = v;
0026 
0027     mpi_normalize(u);
0028     if (!u->nlimbs && !limb)
0029         return 0;
0030     if (u->sign)
0031         return -1;
0032     if (u->nlimbs > 1)
0033         return 1;
0034 
0035     if (u->d[0] == limb)
0036         return 0;
0037     else if (u->d[0] > limb)
0038         return 1;
0039     else
0040         return -1;
0041 }
0042 EXPORT_SYMBOL_GPL(mpi_cmp_ui);
0043 
0044 static int do_mpi_cmp(MPI u, MPI v, int absmode)
0045 {
0046     mpi_size_t usize;
0047     mpi_size_t vsize;
0048     int usign;
0049     int vsign;
0050     int cmp;
0051 
0052     mpi_normalize(u);
0053     mpi_normalize(v);
0054 
0055     usize = u->nlimbs;
0056     vsize = v->nlimbs;
0057     usign = absmode ? 0 : u->sign;
0058     vsign = absmode ? 0 : v->sign;
0059 
0060     /* Compare sign bits.  */
0061 
0062     if (!usign && vsign)
0063         return 1;
0064     if (usign && !vsign)
0065         return -1;
0066 
0067     /* U and V are either both positive or both negative.  */
0068 
0069     if (usize != vsize && !usign && !vsign)
0070         return usize - vsize;
0071     if (usize != vsize && usign && vsign)
0072         return vsize + usize;
0073     if (!usize)
0074         return 0;
0075     cmp = mpihelp_cmp(u->d, v->d, usize);
0076     if (!cmp)
0077         return 0;
0078     if ((cmp < 0?1:0) == (usign?1:0))
0079         return 1;
0080 
0081     return -1;
0082 }
0083 
0084 int mpi_cmp(MPI u, MPI v)
0085 {
0086     return do_mpi_cmp(u, v, 0);
0087 }
0088 EXPORT_SYMBOL_GPL(mpi_cmp);
0089 
0090 int mpi_cmpabs(MPI u, MPI v)
0091 {
0092     return do_mpi_cmp(u, v, 1);
0093 }
0094 EXPORT_SYMBOL_GPL(mpi_cmpabs);