raw
mpi-genesis             1 /* mpi-cmp.c  -  MPI functions
mpi-genesis 2 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
mpi-genesis 3 *
mpi-genesis 4 * This file is part of GnuPG.
mpi-genesis 5 *
mpi-genesis 6 * GnuPG is free software; you can redistribute it and/or modify
mpi-genesis 7 * it under the terms of the GNU General Public License as published by
mpi-genesis 8 * the Free Software Foundation; either version 3 of the License, or
mpi-genesis 9 * (at your option) any later version.
mpi-genesis 10 *
mpi-genesis 11 * GnuPG is distributed in the hope that it will be useful,
mpi-genesis 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis 14 * GNU General Public License for more details.
mpi-genesis 15 *
mpi-genesis 16 * You should have received a copy of the GNU General Public License
mpi-genesis 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
mpi-genesis 18 */
mpi-genesis 19
mpi-genesis 20 #include <config.h>
mpi-genesis 21 #include <stdio.h>
mpi-genesis 22 #include <stdlib.h>
mpi-genesis 23 #include "mpi-internal.h"
mpi-genesis 24
mpi-genesis 25 int
mpi-genesis 26 mpi_cmp_ui( MPI u, unsigned long v )
mpi-genesis 27 {
mpi-genesis 28 mpi_limb_t limb = v;
mpi-genesis 29
mpi-genesis 30 mpi_normalize( u );
mpi-genesis 31 if( !u->nlimbs && !limb )
mpi-genesis 32 return 0;
mpi-genesis 33 if( u->sign )
mpi-genesis 34 return -1;
mpi-genesis 35 if( u->nlimbs > 1 )
mpi-genesis 36 return 1;
mpi-genesis 37
mpi-genesis 38 if( u->d[0] == limb )
mpi-genesis 39 return 0;
mpi-genesis 40 else if( u->d[0] > limb )
mpi-genesis 41 return 1;
mpi-genesis 42 else
mpi-genesis 43 return -1;
mpi-genesis 44 }
mpi-genesis 45
mpi-genesis 46 int
mpi-genesis 47 mpi_cmp( MPI u, MPI v )
mpi-genesis 48 {
mpi-genesis 49 mpi_size_t usize, vsize;
mpi-genesis 50 int cmp;
mpi-genesis 51
mpi-genesis 52 mpi_normalize( u );
mpi-genesis 53 mpi_normalize( v );
mpi-genesis 54 usize = u->nlimbs;
mpi-genesis 55 vsize = v->nlimbs;
mpi-genesis 56 if( !u->sign && v->sign )
mpi-genesis 57 return 1;
mpi-genesis 58 if( u->sign && !v->sign )
mpi-genesis 59 return -1;
mpi-genesis 60 if( usize != vsize && !u->sign && !v->sign )
mpi-genesis 61 return usize - vsize;
mpi-genesis 62 if( usize != vsize && u->sign && v->sign )
mpi-genesis 63 return vsize + usize;
mpi-genesis 64 if( !usize )
mpi-genesis 65 return 0;
mpi-genesis 66 if( !(cmp=mpihelp_cmp( u->d, v->d, usize )) )
mpi-genesis 67 return 0;
mpi-genesis 68 if( (cmp < 0?1:0) == (u->sign?1:0))
mpi-genesis 69 return 1;
mpi-genesis 70 return -1;
mpi-genesis 71 }
mpi-genesis 72
mpi-genesis 73