LCOV - code coverage report
Current view: top level - mpi - mpi-cmp.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 45 46 97.8 %
Date: 2016-09-12 12:23:59 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* mpi-cmp.c  -  MPI functions
       2             :  * Copyright (C) 1998, 1999, 2001, 2002, 2005 Free Software Foundation, Inc.
       3             :  *
       4             :  * This file is part of Libgcrypt.
       5             :  *
       6             :  * Libgcrypt is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU Lesser General Public License as
       8             :  * published by the Free Software Foundation; either version 2.1 of
       9             :  * the License, or (at your option) any later version.
      10             :  *
      11             :  * Libgcrypt is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with this program; if not, write to the Free Software
      18             :  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
      19             :  */
      20             : 
      21             : #include <config.h>
      22             : #include <stdio.h>
      23             : #include <stdlib.h>
      24             : #include "mpi-internal.h"
      25             : 
      26             : int
      27     3106245 : _gcry_mpi_cmp_ui (gcry_mpi_t u, unsigned long v)
      28             : {
      29     3106245 :   mpi_limb_t limb = v;
      30             : 
      31     3106245 :   _gcry_mpi_normalize (u);
      32             : 
      33             :   /* Handle the case that U contains no limb.  */
      34     3106245 :   if (u->nlimbs == 0)
      35       18804 :     return -(limb != 0);
      36             : 
      37             :   /* Handle the case that U is negative.  */
      38     3087441 :   if (u->sign)
      39     1298243 :     return -1;
      40             : 
      41     1789198 :   if (u->nlimbs == 1)
      42             :     {
      43             :       /* Handle the case that U contains exactly one limb.  */
      44             : 
      45      352697 :       if (u->d[0] > limb)
      46      264253 :         return 1;
      47       88444 :       if (u->d[0] < limb)
      48           1 :         return -1;
      49       88443 :       return 0;
      50             :     }
      51             :   else
      52             :     /* Handle the case that U contains more than one limb.  */
      53     1436501 :     return 1;
      54             : }
      55             : 
      56             : 
      57             : int
      58      100942 : _gcry_mpi_cmp (gcry_mpi_t u, gcry_mpi_t v)
      59             : {
      60             :   mpi_size_t usize;
      61             :   mpi_size_t vsize;
      62             :   int cmp;
      63             : 
      64      100942 :   if (mpi_is_opaque (u) || mpi_is_opaque (v))
      65             :     {
      66          14 :       if (mpi_is_opaque (u) && !mpi_is_opaque (v))
      67           1 :         return -1;
      68          13 :       if (!mpi_is_opaque (u) && mpi_is_opaque (v))
      69           1 :         return 1;
      70          12 :       if (!u->sign && !v->sign)
      71           1 :         return 0; /* Empty buffers are identical.  */
      72          11 :       if (u->sign < v->sign)
      73           1 :         return -1;
      74          10 :       if (u->sign > v->sign)
      75           1 :         return 1;
      76           9 :       return memcmp (u->d, v->d, (u->sign+7)/8);
      77             :     }
      78             :   else
      79             :     {
      80      100928 :       _gcry_mpi_normalize (u);
      81      100928 :       _gcry_mpi_normalize (v);
      82             : 
      83      100928 :       usize = u->nlimbs;
      84      100928 :       vsize = v->nlimbs;
      85             : 
      86             :       /* Compare sign bits.  */
      87             : 
      88      100928 :       if (!u->sign && v->sign)
      89           1 :         return 1;
      90      100927 :       if (u->sign && !v->sign)
      91           2 :         return -1;
      92             : 
      93             :       /* U and V are either both positive or both negative.  */
      94             : 
      95      100925 :       if (usize != vsize && !u->sign && !v->sign)
      96        5814 :         return usize - vsize;
      97       95111 :       if (usize != vsize && u->sign && v->sign)
      98           0 :         return vsize + usize;
      99       95111 :       if (!usize )
     100           7 :         return 0;
     101       95104 :       if (!(cmp = _gcry_mpih_cmp (u->d, v->d, usize)))
     102        7465 :         return 0;
     103       87639 :       if ((cmp < 0?1:0) == (u->sign?1:0))
     104       36925 :         return 1;
     105             :     }
     106       50714 :   return -1;
     107             : }

Generated by: LCOV version 1.11