1 /* mpiutil.ac - Utility functions for MPI
2 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG 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 General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include "mpi-internal.h"
25 * Note: It was a bad idea to use the number of limbs to allocate
26 * because on a alpha the limbs are large but we normally need
27 * integers of n bits - So we should chnage this to bits (or bytes).
29 * But mpi_alloc is used in a lot of places :-)
31 MPI mpi_alloc(unsigned nlimbs)
35 a = (MPI) kmalloc(sizeof *a,GFP_KERNEL);
40 a->d = mpi_alloc_limb_space(nlimbs);
58 mpi_ptr_t mpi_alloc_limb_space( unsigned nlimbs )
60 size_t len = nlimbs * sizeof(mpi_limb_t);
62 return kmalloc(len,GFP_KERNEL);
65 void mpi_free_limb_space( mpi_ptr_t a )
74 void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
76 mpi_free_limb_space(a->d);
82 * Resize the array of A to NLIMBS. the additional space is cleared
83 * (set to 0) [done by m_realloc()]
85 int mpi_resize(MPI a, unsigned nlimbs)
89 if (nlimbs <= a->alloced)
90 return 0; /* no need to do it */
93 p = kmalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
96 memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
100 a->d = kmalloc( nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
103 memset(a->d, 0, nlimbs * sizeof(mpi_limb_t) );
109 void mpi_clear(MPI a)
124 mpi_free_limb_space(a->d);
128 printk("invalid flag value in mpi\n");
134 * Note: This copy function should not interpret the MPI
135 * but copy it transparently.
137 int mpi_copy(MPI *copied, const MPI a )
145 b = mpi_alloc( a->nlimbs );
149 b->nlimbs = a->nlimbs;
154 for (i = 0; i < b->nlimbs; i++ )
164 int mpi_set(MPI w, const MPI u)
167 mpi_size_t usize = u->nlimbs;
170 if (RESIZE_IF_NEEDED(w, (size_t) usize) < 0)
175 MPN_COPY(wp, up, usize);
184 int mpi_set_ui(MPI w, unsigned long u)
186 if (RESIZE_IF_NEEDED(w, 1) < 0)
196 MPI mpi_alloc_set_ui(unsigned long u)
198 MPI w = mpi_alloc(1);
208 void mpi_swap(MPI a, MPI b)
212 tmp = *a; *a = *b; *b = tmp;