~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/mpbsd/mtox.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mtox -- Convert OPERAND to hexadecimal and return a malloc'ed string
 
2
   with the result of the conversion.
 
3
 
 
4
Copyright 1991, 1994, 2000, 2001, 2002 Free Software Foundation, Inc.
 
5
 
 
6
This file is part of the GNU MP Library.
 
7
 
 
8
The GNU MP Library is free software; you can redistribute it and/or modify
 
9
it under the terms of the GNU Lesser General Public License as published by
 
10
the Free Software Foundation; either version 2.1 of the License, or (at your
 
11
option) any later version.
 
12
 
 
13
The GNU MP Library is distributed in the hope that it will be useful, but
 
14
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
15
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
16
License for more details.
 
17
 
 
18
You should have received a copy of the GNU Lesser General Public License
 
19
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
 
20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
21
MA 02111-1307, USA. */
 
22
 
 
23
#include "mp.h"
 
24
#include "gmp.h"
 
25
#include "gmp-impl.h"
 
26
 
 
27
char *
 
28
mtox (const MINT *x)
 
29
{
 
30
  mp_ptr xp;
 
31
  mp_size_t xsize = x->_mp_size;
 
32
  mp_size_t xsign;
 
33
  unsigned char *str, *s;
 
34
  size_t str_size, i;
 
35
  int zeros;
 
36
  TMP_DECL (marker);
 
37
 
 
38
  if (xsize == 0)
 
39
    {
 
40
      str = (unsigned char *) (*__gmp_allocate_func) (2);
 
41
      str[0] = '0';
 
42
      str[1] = 0;
 
43
      return (char *) str;
 
44
    }
 
45
  xsign = xsize;
 
46
  if (xsize < 0)
 
47
    xsize = -xsize;
 
48
 
 
49
  TMP_MARK (marker);
 
50
  str_size = ((size_t) (xsize * BITS_PER_MP_LIMB
 
51
                        * __mp_bases[16].chars_per_bit_exactly)) + 3;
 
52
  str = (unsigned char *) (*__gmp_allocate_func) (str_size);
 
53
  s = str;
 
54
 
 
55
  if (xsign < 0)
 
56
    *s++ = '-';
 
57
 
 
58
  /* Move the number to convert into temporary space, since mpn_get_str
 
59
     clobbers its argument + needs one extra high limb....  */
 
60
  xp = (mp_ptr) TMP_ALLOC ((xsize + 1) * BYTES_PER_MP_LIMB);
 
61
  MPN_COPY (xp, x->_mp_d, xsize);
 
62
 
 
63
  str_size = mpn_get_str (s, 16, xp, xsize);
 
64
 
 
65
  /* mpn_get_str might make some leading zeros.  Skip them.  */
 
66
  for (zeros = 0; s[zeros] == 0; zeros++)
 
67
    str_size--;
 
68
 
 
69
  /* Translate to printable chars and move string down.  */
 
70
  for (i = 0; i < str_size; i++)
 
71
    s[i] = "0123456789abcdef"[s[zeros + i]];
 
72
  s[str_size] = 0;
 
73
 
 
74
  TMP_FREE (marker);
 
75
  return (char *) str;
 
76
}