[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.2

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

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