[BACK]Return to gmp_fake.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_toolkit

Annotation of OpenXM/src/ox_toolkit/gmp_fake.c, Revision 1.3

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.3     ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/gmp_fake.c,v 1.2 2003/06/05 21:12:07 ohara Exp $ */
1.1       ohara       3:
                      4: #include <stdio.h>
                      5: #include <stdlib.h>
                      6: #include <string.h>
                      7: #include <ctype.h>
1.3     ! ohara       8: #include <limits.h>
1.1       ohara       9: #include "gmp_fake.h"
                     10:
                     11: #define DEFAULT_LIMB_SIZE 1
                     12: #define MALLOC(x) malloc((x))
1.2       ohara      13: #define MALLOC_ATOMIC(x) malloc((x))
                     14: #define REALLOC(p,x) realloc((p),(x))
                     15: #define ALLOCA(x) alloca((x))
1.1       ohara      16:
                     17: #define HAVE_UNSIGNED_LONG_LONG
                     18:
                     19: #if defined(HAVE_UNSIGNED_LONG_LONG)
                     20: typedef unsigned long long ulong64;
                     21: #elif defined(SIZEOF_LONG) && SIZEOF_LONG == 8
                     22: typedef unsigned long ulong64;
                     23: #endif
                     24:
                     25: #define RADIUS     ((ulong64)1 << ((CHAR_BIT)*sizeof(mp_limb_t)))
                     26: #define LL_HIGH(x) ((x)>>((CHAR_BIT)*sizeof(mp_limb_t)))
                     27: #define LL_LOW(x)  ((x) & ((RADIUS)-1))
                     28:
                     29: inline static void __mpz_clear(mpz_ptr z)
                     30: {
                     31:     int size = abs(z->_mp_size);
                     32:     z->_mp_size = size;
                     33:     memset(z->_mp_d, 0, sizeof(mp_limb_t)*size);
                     34: }
                     35:
                     36: void *_mpz_realloc(mpz_ptr z, size_t n)
                     37: {
1.2       ohara      38:     mp_limb_t *p = REALLOC(z->_mp_d, n*sizeof(mp_limb_t));
                     39:     if (p != NULL) {
                     40:         z->_mp_d = p;
                     41:     }
                     42:     return p;
1.1       ohara      43: }
                     44:
                     45: void mpz_init(mpz_ptr z)
                     46: {
                     47:     z->_mp_size = DEFAULT_LIMB_SIZE;
1.2       ohara      48:     z->_mp_d    = MALLOC_ATOMIC(DEFAULT_LIMB_SIZE);
1.1       ohara      49:     __mpz_clear(z);
                     50: }
                     51:
                     52: void mpz_init_set(mpz_ptr z, mpz_ptr src)
                     53: {
                     54:     mpz_init(z);
                     55:     mpz_set(z, src);
                     56: }
                     57:
                     58: void mpz_set_si(mpz_ptr z, int i)
                     59: {
                     60:     __mpz_clear(z);
                     61:     z->_mp_size = (i < 0)? -z->_mp_size: z->_mp_size;
                     62:     z->_mp_d[0] = abs(i);
                     63: }
                     64:
                     65: void mpz_set(mpz_ptr dest, mpz_ptr src)
                     66: {
                     67:     int dsize=abs(dest->_mp_size);
                     68:     int ssize=abs(src->_mp_size);
                     69:     if (dsize < ssize) {
                     70:         _mpz_realloc(dest, ssize);
                     71:         dest->_mp_size = src->_mp_size;
                     72:     }else {
                     73:         __mpz_clear(dest);
                     74:         dest->_mp_size = (src->_mp_size < 0)? -dsize: dsize;
                     75:     }
                     76:     memcpy(dest->_mp_d, src->_mp_d, ssize*sizeof(mp_limb_t));
                     77: }
                     78:
                     79: inline static void __mpz_neg(mpz_ptr z)
                     80: {
                     81:     z->_mp_size *= -1;
                     82: }
                     83:
                     84: void mpz_neg(mpz_ptr dest, mpz_ptr src)
                     85: {
                     86:     mpz_set(dest, src);
                     87:     __mpz_neg(dest);
                     88: }
                     89:
                     90: int  mpz_init_set_str(mpz_ptr z, char *nptr, int base)
                     91: {
                     92:     mpz_init(z);
                     93:     mpz_set_str(z, nptr, base);
                     94:     return 0;
                     95: }
                     96:
                     97: /* base admits 0, 8, 10, or 16 */
                     98: char *mpz_get_str(char *s, int base, mpz_ptr src)
                     99: {
                    100:     mpz_t z;
                    101:     int i, j, len;
                    102:     mp_limb_t res;
                    103:     char *t;
                    104:     ulong64 uu;
                    105:
                    106:     if (base != 10 && base != 16 && base != 8) {
                    107:         base = 10;
                    108:     }
                    109:     /* log2(8)=3 < log2(10) < log2(16)=4 */
                    110:     len = ((CHAR_BIT)*sizeof(mp_limb_t)*abs(src->_mp_size))/((base==16)? 4: 3)+4;
                    111:     mpz_init(z);
                    112:     mpz_set(z, src);
1.2       ohara     113:     t = ALLOCA(len+1);
1.1       ohara     114:     t[len] = '\0';
                    115:     for(i=len-1; i>=0; i--) {
                    116:         res = 0;
                    117:         /* res := z % base, z := z / base  */
                    118:         for(j=abs(z->_mp_size)-1; j>=0; j--) {
                    119:             uu  = RADIUS*res + z->_mp_d[j];
                    120:             res = uu % base;
                    121:             z->_mp_d[j] = uu / base;
                    122:         }
                    123:         t[i] = res + ((res < 10)? '0' : ('a'-10));
                    124:     }
                    125:     /* skip 0's */
                    126:     while (t[++i] == '0') {
                    127:     }
                    128:     if (t[i] == '\0' || base == 8) {
                    129:         t[--i] = '0';
                    130:     }else if (base == 16){
                    131:         t[--i] = 'x';
                    132:         t[--i] = '0';
                    133:     }
                    134:     if (src->_mp_size < 0) {
                    135:         t[--i] = '-';
                    136:     }
                    137:     len = strlen(t+i)+1;
                    138:     if (s == NULL) {
1.2       ohara     139:         s = MALLOC_ATOMIC(len);
1.1       ohara     140:     }
                    141:     memcpy(s, t+i, len);
                    142:     return s;
                    143: }
                    144:
                    145: int  mpz_get_si(mpz_ptr z)
                    146: {
                    147:     return (z->_mp_size < 0)? - z->_mp_d[0]: z->_mp_d[0];
                    148: }
                    149:
                    150: /* base admits 0, 8, 10, or 16 */
                    151: int mpz_set_str(mpz_ptr z, char *s, int base)
                    152: {
                    153:     unsigned char c;
                    154:     int neg = 0;
                    155:     int len, i, size;
                    156:     mp_limb_t res;
                    157:     ulong64 uu;
                    158:     char *t = s;
                    159:
                    160:     do {
                    161:         c = *t++;
                    162:     } while (isspace(c));
                    163:     if (c == '-') {
                    164:         neg = 1;
                    165:         c = *t++;
                    166:     }else if (c == '+') {
                    167:         c = *t++;
                    168:     }
                    169:     if ((base == 0 || base == 16) &&
                    170:         c == '0' && (*t == 'x' || *t == 'X')) {
                    171:         c = t[1];
                    172:         t += 2;
                    173:         base = 16;
                    174:     }
                    175:     if (base == 0) {
                    176:         base = (c == '0')? 8: 10;
                    177:     }
                    178:     __mpz_clear(z);
                    179:     /* log2(8)=3 < log2(10) < log2(16)=4 */
                    180:     len = (((base == 8)? 3: 4)*(strlen(t)+1))/((CHAR_BIT)*sizeof(mp_limb_t))+1;
                    181:     _mpz_realloc(z, len);
                    182:
                    183:     for( ;; c = *t++) {
                    184:         if (isdigit(c)) {
                    185:             c -= '0';
                    186:         }else if (isalpha(c)) {
                    187:             c -= isupper(c)? ('A'-10): ('a'-10);
                    188:         }else {
                    189:             break;
                    190:         }
                    191:         if (c >= base) {
                    192:             break;
                    193:         }
                    194:         size = abs(z->_mp_size);
                    195:         res  = c;
                    196:         /* z := z*base + c */
                    197:         for(i=0; i<size; i++) {
                    198:             uu  = ((ulong64)z->_mp_d[i])*base+res;
                    199:             res = LL_HIGH(uu);
                    200:             z->_mp_d[i] = LL_LOW(uu);
                    201:         }
                    202:     }
                    203:     if (neg) {
                    204:         __mpz_neg(z);
                    205:     }
                    206:     return 0;
                    207: }
                    208:
                    209: #ifdef DEBUG
                    210: void dump(mpz_ptr z)
                    211: {
                    212:     int i;
                    213:     int size = abs(z->_mp_size);
                    214:     printf("_mp_size = %d\n_mp_d=%x", z->_mp_size, z->_mp_d[0]);
                    215:     for(i=1; i<size; i++) {
                    216:         printf(":%x", z->_mp_d[i]);
                    217:     }
                    218:     printf("\n");
                    219: }
                    220:
                    221: int main(int argc, char *argv[])
                    222: {
                    223:     mpz_t z;
                    224:     mpz_init(z);
                    225:     mpz_set_str(z, argv[1], 0);
                    226:     printf("%s\n", mpz_get_str(NULL, 10, z));
                    227:     return 0;
                    228: }
                    229: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>