/* mpi-cmp.c - MPI functions1* Copyright (C) 1998, 1999 Free Software Foundation, Inc.2*3* This file is part of GnuPG.4*5* GnuPG is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License as published by7* the Free Software Foundation; either version 2 of the License, or8* (at your option) any later version.9*10* GnuPG is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13* GNU General Public License for more details.14*15* You should have received a copy of the GNU General Public License16* along with this program; if not, write to the Free Software17* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA18*/1920#include <linux/export.h>2122#include "mpi-internal.h"2324int mpi_cmp_ui(MPI u, unsigned long v)25{26mpi_limb_t limb = v;2728mpi_normalize(u);29if (u->nlimbs == 0) {30if (v == 0)31return 0;32else33return -1;34}35if (u->sign)36return -1;37if (u->nlimbs > 1)38return 1;3940if (u->d[0] == limb)41return 0;42else if (u->d[0] > limb)43return 1;44else45return -1;46}47EXPORT_SYMBOL_GPL(mpi_cmp_ui);4849int mpi_cmp(MPI u, MPI v)50{51mpi_size_t usize, vsize;52int cmp;5354mpi_normalize(u);55mpi_normalize(v);56usize = u->nlimbs;57vsize = v->nlimbs;58if (!u->sign && v->sign)59return 1;60if (u->sign && !v->sign)61return -1;62if (usize != vsize && !u->sign && !v->sign)63return usize - vsize;64if (usize != vsize && u->sign && v->sign)65return vsize - usize;66if (!usize)67return 0;68cmp = mpihelp_cmp(u->d, v->d, usize);69if (u->sign)70return -cmp;71return cmp;72}73EXPORT_SYMBOL_GPL(mpi_cmp);747576