Back to index

glibc  2.9
cmp.c
Go to the documentation of this file.
00001 /* mpn_cmp -- Compare two low-level natural-number integers.
00002 
00003 Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
00004 
00005 This file is part of the GNU MP Library.
00006 
00007 The GNU MP Library is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU Lesser General Public License as published by
00009 the Free Software Foundation; either version 2.1 of the License, or (at your
00010 option) any later version.
00011 
00012 The GNU MP Library is distributed in the hope that it will be useful, but
00013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00014 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00015 License for more details.
00016 
00017 You should have received a copy of the GNU Lesser General Public License
00018 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
00019 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00020 MA 02111-1307, USA. */
00021 
00022 #include <gmp.h>
00023 #include "gmp-impl.h"
00024 
00025 /* Compare OP1_PTR/OP1_SIZE with OP2_PTR/OP2_SIZE.
00026    There are no restrictions on the relative sizes of
00027    the two arguments.
00028    Return 1 if OP1 > OP2, 0 if they are equal, and -1 if OP1 < OP2.  */
00029 
00030 int
00031 #if __STDC__
00032 mpn_cmp (mp_srcptr op1_ptr, mp_srcptr op2_ptr, mp_size_t size)
00033 #else
00034 mpn_cmp (op1_ptr, op2_ptr, size)
00035      mp_srcptr op1_ptr;
00036      mp_srcptr op2_ptr;
00037      mp_size_t size;
00038 #endif
00039 {
00040   mp_size_t i;
00041   mp_limb_t op1_word, op2_word;
00042 
00043   for (i = size - 1; i >= 0; i--)
00044     {
00045       op1_word = op1_ptr[i];
00046       op2_word = op2_ptr[i];
00047       if (op1_word != op2_word)
00048        goto diff;
00049     }
00050   return 0;
00051  diff:
00052   /* This can *not* be simplified to
00053        op2_word - op2_word
00054      since that expression might give signed overflow.  */
00055   return (op1_word > op2_word) ? 1 : -1;
00056 }