Page 228 - ARM 64 Bit Assembly Language
P. 228
216 Chapter 7
97 }
98 }
99 if (negative) {
100 tmp = bigint_negate(d);
101 bigint_free(d);
102 d = tmp;
103 }
104 bigint_free(ten);
105 bigint_free(power);
106 return d;
107 }
108
109 /* convert integer to bigint */
110 bigint bigint_from_int(int val) {
111 bigint d, tmp;
112 int i;
113 int nchunks = sizeof(int) / sizeof(chunk);
114 if (nchunks < 1)
115 nchunks++;
116 d = bigint_alloc(nchunks);
117 for (i = 0; i < d->size; i++)
118 d->blks[i] = (val >> (i*BITSPERCHUNK)) & CHUNKMASK;
119 tmp = bigint_trim(d);
120 bigint_free(d);
121 return tmp;
122 }
123
124 /* duplicate a bigint */
125 bigint bigint_copy(bigint source) {
126 bigint r;
127 r = bigint_alloc(source->size);
128 memcpy(r->blks, source->blks, r->size * sizeof(chunk));
129 return r;
130 }
131
132 /* convert a bigint into and integer, if possible */
133 int bigint_to_int(bigint b) {
134 int i, negative=0, result=0;
135 bigint tmp1, tmp2;
136 tmp1 = bigint_trim(b); /* make a trimmed copy */
137 if (tmp1->size * sizeof(chunk) > sizeof(int)) {
138 fprintf(stderr, "Cannot convert bigint to int\n%ld bytes\n",
139 (long)tmp1->size * sizeof(chunk));
140 exit(1);
141 }
142 /* check sign and negate if necessary */
143 if (tmp1->blks[tmp1->size-1] & ((bigchunk)1<<(BITSPERCHUNK-1))) {
144 negative = 1;
145 tmp2 = bigint_negate(tmp1);