~ubuntu-branches/ubuntu/trusty/cloog/trusty

« back to all changes in this revision

Viewing changes to osl/source/int.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-10-17 15:54:24 UTC
  • mfrom: (3.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20131017155424-3q1gw7yhddylfkpj
Tags: 0.18.1-1
* New upstream version.
* Add a comment to build-depend on libpod-latex-perl | perl (<< 5.17.0),
  when the documentation is built. Closes: #711681.
* Use dh_autotools-dev to update config.{sub,guess}. Closes: #719957.

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
#include <osl/int.h>
71
71
 
72
72
 
73
 
 
74
73
/*+***************************************************************************
75
74
 *                                Basic Functions                            *
76
75
 *****************************************************************************/
77
76
 
78
77
 
79
78
/**
 
79
 * osl_int_is_precision_supported function:
 
80
 * this function returns 1 if the precision provided as parameter is supported
 
81
 * by the library and 0 otherwise. Possible values for the precision
 
82
 * parameter are OSL_PRECISION_SP for 32 bits (single) precision,
 
83
 * OSL_PRECISION_DP for 64 bits (double) precision and OSL_PRECISION_MP for
 
84
 * multiple precision.
 
85
 * \param[in] precision The precision to check for.
 
86
 * \return 1 if the precision is supported, 0 otherwise.
 
87
 */
 
88
int osl_int_is_precision_supported(int precision) {
 
89
  switch (precision) {
 
90
    case OSL_PRECISION_SP:
 
91
      return 1;
 
92
    case OSL_PRECISION_DP:
 
93
      return 1;
 
94
#ifdef OSL_GMP_IS_HERE
 
95
    case OSL_PRECISION_MP:
 
96
      return 1;
 
97
#endif
 
98
  }
 
99
 
 
100
  return 0;
 
101
}
 
102
 
 
103
 
 
104
/**
80
105
 * osl_int_dump_precision function:
81
106
 * this function prints in a human readable fashion the precision
82
107
 * corresponding to the "precision" parameter.
84
109
 * \param[in] precision The precision to print.
85
110
 */
86
111
void osl_int_dump_precision(FILE * file, int precision) {
87
 
 
88
112
  switch (precision) {
89
113
    case OSL_PRECISION_SP:
90
114
      fprintf(file, "32 bits");
103
127
}
104
128
 
105
129
 
106
 
int osl_int_sizeof(int precision) {
107
 
  switch (precision) {
108
 
    case OSL_PRECISION_SP:
109
 
      return sizeof(long int);
110
 
 
111
 
    case OSL_PRECISION_DP:
112
 
      return sizeof(long long int);
113
 
 
114
 
#ifdef OSL_GMP_IS_HERE
115
 
    case OSL_PRECISION_MP:
116
 
      return sizeof(mpz_t);
117
 
#endif
118
 
 
119
 
    default:
120
 
      OSL_error("unknown precision");
121
 
  }
122
 
}
123
 
 
124
 
 
125
 
void * osl_int_address(int precision, void * base, int offset) {
126
 
  switch (precision) {
127
 
    case OSL_PRECISION_SP:
128
 
      return (long int *)base + offset;
129
 
 
130
 
    case OSL_PRECISION_DP:
131
 
      return (long long int *)base + offset;
132
 
 
133
 
#ifdef OSL_GMP_IS_HERE
134
 
    case OSL_PRECISION_MP:
135
 
      return (mpz_t *)base + offset;
136
 
#endif
137
 
 
138
 
    default:
139
 
      OSL_error("unknown precision");
140
 
  }
141
 
}
142
 
 
143
 
 
144
 
void osl_int_init(int precision, void * value_base, int value_offset) {
145
 
  void * value = osl_int_address(precision, value_base, value_offset);
146
 
 
147
 
  switch (precision) {
148
 
    case OSL_PRECISION_SP:
149
 
      *(long int *)value = 0;
150
 
      break;
151
 
 
152
 
    case OSL_PRECISION_DP:
153
 
      *(long long int *)value = 0;
154
 
      break;
155
 
 
156
 
#ifdef OSL_GMP_IS_HERE
157
 
    case OSL_PRECISION_MP:
158
 
      mpz_init(*(mpz_t *)value);
159
 
      break;
160
 
#endif
161
 
 
162
 
    default:
163
 
      OSL_error("unknown precision");
164
 
  }
165
 
}
166
 
 
167
 
 
168
 
void * osl_int_malloc(int precision) {
169
 
  void * value;
170
 
 
171
 
  switch (precision) {
172
 
    case OSL_PRECISION_SP:
173
 
      value = malloc(sizeof(long int));
174
 
      break;
175
 
 
176
 
    case OSL_PRECISION_DP:
177
 
      value = malloc(sizeof(long long int));
178
 
      *(long long int *)value = 0;
179
 
      break;
180
 
 
181
 
#ifdef OSL_GMP_IS_HERE
182
 
    case OSL_PRECISION_MP:
183
 
      value = malloc(sizeof(mpz_t));
184
 
      break;
185
 
#endif
186
 
 
187
 
    default:
188
 
      OSL_error("unknown precision");
189
 
  }
190
 
 
191
 
  osl_int_init(precision, value, 0);
192
 
  return value;
193
 
}
194
 
 
195
 
 
196
 
/**
197
 
 * val1_base[val1_offset] = val2_base[val2_offset];
198
 
 */
199
 
void osl_int_assign(int precision,
200
 
                    void * val1_base, int val1_offset,
201
 
                    void * val2_base, int val2_offset) {
202
 
  void * val1 = osl_int_address(precision, val1_base, val1_offset);
203
 
  void * val2 = osl_int_address(precision, val2_base, val2_offset);
204
 
 
205
 
  switch (precision) {
206
 
    case OSL_PRECISION_SP:
207
 
      *(long int *)val1 = *(long int *)val2;
208
 
      break;
209
 
 
210
 
    case OSL_PRECISION_DP:
211
 
      *(long long int *)val1 = *(long long int *)val2;
212
 
      break;
213
 
 
214
 
#ifdef OSL_GMP_IS_HERE
215
 
    case OSL_PRECISION_MP:
216
 
      mpz_set(*(mpz_t *)val1, *(mpz_t *)val2);
217
 
      break;
218
 
#endif
219
 
 
220
 
    default:
221
 
      OSL_error("unknown precision");
222
 
  }
223
 
}
224
 
 
225
 
 
226
 
/**
227
 
 * value_base[value_offset] = i;
228
 
 */
229
 
void osl_int_set_si(int precision, void * value_base, int value_offset,
230
 
                    int i) {
231
 
  void * value = osl_int_address(precision, value_base, value_offset);
232
 
 
233
 
  switch (precision) {
234
 
    case OSL_PRECISION_SP:
235
 
      *(long int *)value = (long int)i;
236
 
      break;
237
 
 
238
 
    case OSL_PRECISION_DP:
239
 
      *(long long int *)value = (long long int)i;
240
 
      break;
241
 
 
242
 
#ifdef OSL_GMP_IS_HERE
243
 
    case OSL_PRECISION_MP:
244
 
      mpz_set_si(*(mpz_t *)value, i);
245
 
      break;
246
 
#endif
247
 
 
248
 
    default:
249
 
      OSL_error("unknown precision");
250
 
  }
251
 
}
252
 
 
253
 
 
254
 
/**
255
 
 * return value_base[value_offset];
256
 
 */
257
 
int osl_int_get_si(int precision, void * value_base, int value_offset) {
258
 
  void * value = osl_int_address(precision, value_base, value_offset);
259
 
 
260
 
  switch (precision) {
261
 
    case OSL_PRECISION_SP:
262
 
      return *(int *)value;
263
 
 
264
 
    case OSL_PRECISION_DP:
265
 
      return *(int *)value;
266
 
 
267
 
#ifdef OSL_GMP_IS_HERE
268
 
    case OSL_PRECISION_MP:
269
 
      return mpz_get_si(*(mpz_t *)value);
270
 
#endif
271
 
 
272
 
    default:
273
 
      OSL_error("unknown precision");
274
 
  }
275
 
}
276
 
 
277
 
 
278
 
/**
279
 
 * value_base[value_offset] = i; // including initialization for GMP
280
 
 */
281
 
void osl_int_init_set_si(int precision,
282
 
                         void * value_base, int value_offset, int i) {
283
 
  void * value = osl_int_address(precision, value_base, value_offset);
284
 
 
285
 
  switch (precision) {
286
 
    case OSL_PRECISION_SP:
287
 
      *(long int *)value = (long int)i;
288
 
      break;
289
 
 
290
 
    case OSL_PRECISION_DP:
291
 
      *(long long int *)value = (long long int)i;
292
 
      break;
293
 
 
294
 
#ifdef OSL_GMP_IS_HERE
295
 
    case OSL_PRECISION_MP:
296
 
      mpz_init_set_si(*(mpz_t *)value, i);
297
 
      break;
298
 
#endif
299
 
 
300
 
    default:
301
 
      OSL_error("unknown precision");
302
 
  }
303
 
}
304
 
 
305
 
 
306
 
/**
307
 
 * value_base[value_offset] = 0; // Including cleaning for GMP
308
 
 */
309
 
void osl_int_clear(int precision, void * value_base, int value_offset) {
310
 
  void * value = osl_int_address(precision, value_base, value_offset);
311
 
  
312
 
  switch (precision) {
313
 
    case OSL_PRECISION_SP:
314
 
      *(long int *)value = 0;
315
 
      break;
316
 
 
317
 
    case OSL_PRECISION_DP:
318
 
      *(long long int *)value = 0;
319
 
      break;
320
 
 
321
 
#ifdef OSL_GMP_IS_HERE
322
 
    case OSL_PRECISION_MP:
323
 
      mpz_clear(*(mpz_t *)value);
324
 
      break;
325
 
#endif
326
 
 
327
 
    default:
328
 
      OSL_error("unknown precision");
329
 
  }
330
 
}
331
 
 
332
 
 
333
 
void osl_int_free(int precision, void * value_base, int value_offset) {
334
 
  void * value = osl_int_address(precision, value_base, value_offset);
335
 
 
336
 
  osl_int_clear(precision, value_base, value_offset);
337
 
  free(value);
 
130
void osl_int_init(int precision, osl_int_p variable) {
 
131
  switch (precision) {
 
132
    case OSL_PRECISION_SP:
 
133
      variable->sp = 0;
 
134
      break;
 
135
 
 
136
    case OSL_PRECISION_DP:
 
137
      variable->dp = 0;
 
138
      break;
 
139
 
 
140
#ifdef OSL_GMP_IS_HERE
 
141
    case OSL_PRECISION_MP:
 
142
      OSL_malloc(variable->mp, void*, sizeof(mpz_t)); 
 
143
      mpz_init(*((mpz_t*)variable->mp));
 
144
      break;
 
145
#endif
 
146
 
 
147
    default:
 
148
      OSL_error("unknown precision");
 
149
  }
 
150
}
 
151
 
 
152
 
 
153
osl_int_p osl_int_malloc(int precision) {
 
154
  osl_int_p variable;
 
155
 
 
156
  OSL_malloc(variable, osl_int_p, sizeof(osl_int_t));
 
157
  osl_int_init(precision, variable);
 
158
  return variable;
 
159
}
 
160
 
 
161
 
 
162
/**
 
163
 * variable = value;
 
164
 */
 
165
void osl_int_assign(int precision, osl_int_p variable, osl_int_t value) {
 
166
  switch (precision) {
 
167
    case OSL_PRECISION_SP:
 
168
      variable->sp = value.sp;
 
169
      break;
 
170
 
 
171
    case OSL_PRECISION_DP:
 
172
      variable->dp = value.dp;
 
173
      break;
 
174
 
 
175
#ifdef OSL_GMP_IS_HERE
 
176
    case OSL_PRECISION_MP:
 
177
      mpz_set(*((mpz_t*)variable->mp), *((mpz_t*)value.mp));
 
178
      break;
 
179
#endif
 
180
 
 
181
    default:
 
182
      OSL_error("unknown precision");
 
183
  }
 
184
}
 
185
 
 
186
 
 
187
/**
 
188
 * variable = i;
 
189
 */
 
190
void osl_int_set_si(int precision, osl_int_p variable, int i) {
 
191
  switch (precision) {
 
192
    case OSL_PRECISION_SP:
 
193
      variable->sp = (long int)i;
 
194
      break;
 
195
 
 
196
    case OSL_PRECISION_DP:
 
197
      variable->dp = (long long int)i;
 
198
      break;
 
199
 
 
200
#ifdef OSL_GMP_IS_HERE
 
201
    case OSL_PRECISION_MP:
 
202
      mpz_set_si(*((mpz_t*)variable->mp), i);
 
203
      break;
 
204
#endif
 
205
 
 
206
    default:
 
207
      OSL_error("unknown precision");
 
208
  }
 
209
}
 
210
 
 
211
 
 
212
/**
 
213
 * return value;
 
214
 */
 
215
int osl_int_get_si(int precision, osl_int_t value) {
 
216
  switch (precision) {
 
217
    case OSL_PRECISION_SP:
 
218
      return value.sp;
 
219
 
 
220
    case OSL_PRECISION_DP:
 
221
      return (int)value.dp;
 
222
 
 
223
#ifdef OSL_GMP_IS_HERE
 
224
    case OSL_PRECISION_MP:
 
225
      return mpz_get_si(*((mpz_t*)value.mp));
 
226
#endif
 
227
 
 
228
    default:
 
229
      OSL_error("unknown precision");
 
230
  }
 
231
}
 
232
 
 
233
 
 
234
/**
 
235
 * variable = i; // including initialization for GMP
 
236
 */
 
237
void osl_int_init_set_si(int precision, osl_int_p variable, int i) {
 
238
  switch (precision) {
 
239
    case OSL_PRECISION_SP:
 
240
      variable->sp = (long int)i;
 
241
      break;
 
242
 
 
243
    case OSL_PRECISION_DP:
 
244
      variable->dp = (long long int)i;
 
245
      break;
 
246
 
 
247
#ifdef OSL_GMP_IS_HERE
 
248
    case OSL_PRECISION_MP:
 
249
      OSL_malloc(variable->mp, void*, sizeof(mpz_t)); 
 
250
      mpz_init_set_si(*((mpz_t*)variable->mp), i);
 
251
      break;
 
252
#endif
 
253
 
 
254
    default:
 
255
      OSL_error("unknown precision");
 
256
  }
 
257
}
 
258
 
 
259
 
 
260
/**
 
261
 * var1 <=> var2;
 
262
 */
 
263
void osl_int_swap(int precision, osl_int_p var1, osl_int_p var2) {
 
264
  switch (precision) {
 
265
    case OSL_PRECISION_SP: {
 
266
      long int temp = var1->sp;
 
267
      var1->sp = var2->sp;
 
268
      var2->sp = temp;
 
269
      break;
 
270
    }
 
271
 
 
272
    case OSL_PRECISION_DP: {
 
273
      long long int temp = var1->dp;
 
274
      var1->dp = var2->dp;
 
275
      var2->dp = temp;
 
276
      break;
 
277
    }
 
278
 
 
279
#ifdef OSL_GMP_IS_HERE
 
280
    case OSL_PRECISION_MP: {
 
281
      mpz_t temp;
 
282
      mpz_init(temp);
 
283
      mpz_set(temp, *((mpz_t*)var1->mp));
 
284
      mpz_set(*((mpz_t*)var1->mp), *((mpz_t*)var2->mp));
 
285
      mpz_set(*((mpz_t*)var2->mp), temp);
 
286
      mpz_clear(temp);
 
287
      break;
 
288
    }
 
289
#endif
 
290
 
 
291
    default:
 
292
      OSL_error("unknown precision");
 
293
  }
 
294
}
 
295
 
 
296
 
 
297
/**
 
298
 * variable = 0; // Including cleaning for GMP
 
299
 */
 
300
void osl_int_clear(int precision, osl_int_p variable) {
 
301
  switch (precision) {
 
302
    case OSL_PRECISION_SP:
 
303
      variable->sp = 0;
 
304
      break;
 
305
 
 
306
    case OSL_PRECISION_DP:
 
307
      variable->dp = 0;
 
308
      break;
 
309
 
 
310
#ifdef OSL_GMP_IS_HERE
 
311
    case OSL_PRECISION_MP:
 
312
      mpz_clear(*((mpz_t*)variable->mp));
 
313
      free(variable->mp);
 
314
      break;
 
315
#endif
 
316
 
 
317
    default:
 
318
      OSL_error("unknown precision");
 
319
  }
 
320
}
 
321
 
 
322
 
 
323
void osl_int_free(int precision, osl_int_p variable) {
 
324
  osl_int_clear(precision, variable);
 
325
  free(variable);
338
326
}
339
327
 
340
328
 
341
329
/**
342
330
 * osl_int_print function:
343
331
 * this function displays an integer value into a file (file, possibly stdout).
344
 
 * \param file         The file where the integer has to be printed.
345
 
 * \param precision    The precision of the integer.
346
 
 * \param value_base   Address of the base integer value.
347
 
 * \param value_offset Offset in number of values from the base integer value.
 
332
 * \param file      The file where the integer has to be printed.
 
333
 * \param precision The precision of the integer.
 
334
 * \param value     The integer element to print.
348
335
 */
349
 
void osl_int_print(FILE * file, int precision,
350
 
                   void * value_base, int value_offset) {
 
336
void osl_int_print(FILE * file, int precision, osl_int_t value) {
351
337
  char string[OSL_MAX_STRING];
352
338
  
353
 
  osl_int_sprint(string, precision, value_base, value_offset);
 
339
  osl_int_sprint(string, precision, value);
354
340
  fprintf(file, "%s", string);
355
341
}
356
342
 
359
345
 * osl_int_sprint function:
360
346
 * this function prints an integer value into a string, it uses the
361
347
 * OpenScop Library formats OSL_FMT_* to format the printing.
362
 
 * \param string       The string where the integer has to be printed.
363
 
 * \param precision    The precision of the integer.
364
 
 * \param value_base   Address of the base integer value.
365
 
 * \param value_offset Offset in number of values from the base integer value.
 
348
 * \param string    The string where the integer has to be printed.
 
349
 * \param precision The precision of the integer.
 
350
 * \param value     The integer element to print.
366
351
 */
367
 
void osl_int_sprint(char * string, int precision,
368
 
                    void * value_base, int value_offset) {
369
 
  void * value = osl_int_address(precision, value_base, value_offset);
370
 
            
 
352
void osl_int_sprint(char * string, int precision, osl_int_t value) {
371
353
  switch (precision) {
372
354
    case OSL_PRECISION_SP:
373
 
      sprintf(string, OSL_FMT_SP, *(long int *)value);
 
355
      sprintf(string, OSL_FMT_SP, value.sp);
374
356
      break;
375
357
 
376
358
    case OSL_PRECISION_DP:
377
 
      sprintf(string, OSL_FMT_DP, *(long long int *)value);
 
359
      sprintf(string, OSL_FMT_DP, value.dp);
378
360
      break;
379
361
 
380
362
#ifdef OSL_GMP_IS_HERE
381
363
    case OSL_PRECISION_MP: {
382
364
      char * str;
383
 
      str = mpz_get_str(0, 10, *(mpz_t *)value); //TODO: 10 -> #define
 
365
      str = mpz_get_str(0, 10, *((mpz_t*)value.mp)); //TODO: 10 -> #define
384
366
      sprintf(string, OSL_FMT_MP, str);
385
367
      free(str);
386
368
      break;
399
381
 * using OSL_TMT_TXT_* formats.
400
382
 * \see osl_int_sprintf
401
383
 */
402
 
void osl_int_sprint_txt(char * string, int precision,
403
 
                        void * value_base, int value_offset) {
404
 
  void * value = osl_int_address(precision, value_base, value_offset);
405
 
            
 
384
void osl_int_sprint_txt(char * string, int precision, osl_int_t value) {
406
385
  switch (precision) {
407
386
    case OSL_PRECISION_SP:
408
 
      sprintf(string, OSL_FMT_TXT_SP, *(long int *)value);
 
387
      sprintf(string, OSL_FMT_TXT_SP, value.sp);
409
388
      break;
410
389
 
411
390
    case OSL_PRECISION_DP:
412
 
      sprintf(string, OSL_FMT_TXT_DP, *(long long int *)value);
 
391
      sprintf(string, OSL_FMT_TXT_DP, value.dp);
413
392
      break;
414
393
 
415
394
#ifdef OSL_GMP_IS_HERE
416
395
    case OSL_PRECISION_MP: {
417
396
      char * str;
418
 
      str = mpz_get_str(0, 10, *(mpz_t *)value); //TODO: 10 -> #define
 
397
      str = mpz_get_str(0, 10, *((mpz_t*)value.mp)); //TODO: 10 -> #define
419
398
      sprintf(string, OSL_FMT_TXT_MP, str);
420
399
      free(str);
421
400
      break;
428
407
}
429
408
 
430
409
 
431
 
void osl_int_sread(char ** string, int precision,
432
 
                   void * value_base, int value_offset) {
433
 
  void * value = osl_int_address(precision, value_base, value_offset);
 
410
void osl_int_sread(char ** string, int precision, osl_int_p variable) {
434
411
  int nb_read = 0;
435
412
 
436
413
  switch (precision) {
437
414
    case OSL_PRECISION_SP:
438
 
      nb_read = sscanf(*string, OSL_FMT_TXT_SP, (long int *)value);
 
415
      nb_read = sscanf(*string, OSL_FMT_TXT_SP, &(variable->sp));
439
416
      if (nb_read == 0)
440
417
        OSL_error("failed to read an integer");
441
418
      break;
442
419
 
443
420
    case OSL_PRECISION_DP:
444
 
      nb_read = sscanf(*string, OSL_FMT_TXT_DP, (long long int *)value);
 
421
      nb_read = sscanf(*string, OSL_FMT_TXT_DP, &(variable->dp));
445
422
      if (nb_read == 0)
446
423
        OSL_error("failed to read an integer");
447
424
      break;
452
429
      nb_read = sscanf(*string, OSL_FMT_TXT_DP, &tmp);
453
430
      if (nb_read == 0)
454
431
        OSL_error("failed to read an integer");
455
 
      mpz_set_si(*(mpz_t *)value, tmp);
 
432
      mpz_set_si(*((mpz_t*)variable->mp), tmp);
456
433
      break;
457
434
    }
458
435
#endif
472
449
 
473
450
 
474
451
/**
475
 
 * result_base[result_offset] = value_base[value_offset] + 1;
 
452
 * variable <- value + 1;
476
453
 */
477
 
void osl_int_increment(int precision,
478
 
                       void * result_base, int result_offset,
479
 
                       void * value_base,  int value_offset) {
480
 
  void * result = osl_int_address(precision, result_base, result_offset);
481
 
  void * value  = osl_int_address(precision, value_base, value_offset);
482
 
 
 
454
void osl_int_increment(int precision, osl_int_p variable, osl_int_t value) {
483
455
  switch (precision) {
484
456
    case OSL_PRECISION_SP:
485
 
      *(long int *)result = *(long int *)value + (long int)1;
 
457
      variable->sp = value.sp + 1;
486
458
      break;
487
459
 
488
460
    case OSL_PRECISION_DP:
489
 
      *(long long int *)result = *(long long int *)value + (long long int)1;
 
461
      variable->dp = value.dp + (long long int)1;
490
462
      break;
491
463
 
492
464
#ifdef OSL_GMP_IS_HERE
493
465
    case OSL_PRECISION_MP:
494
 
      mpz_add_ui(*(mpz_t *)result, *(mpz_t *)value, 1);
 
466
      mpz_add_ui(*((mpz_t*)variable->mp), *((mpz_t*)value.mp), 1);
495
467
      break;
496
468
#endif
497
469
 
502
474
 
503
475
 
504
476
/**
505
 
 * result_base[result_offset] = value_base[value_offset] - 1;
 
477
 * variable <- value - 1;
506
478
 */
507
 
void osl_int_decrement(int precision,
508
 
                       void * result_base, int result_offset,
509
 
                       void * value_base,  int value_offset) {
510
 
  void * result = osl_int_address(precision, result_base, result_offset);
511
 
  void * value  = osl_int_address(precision, value_base, value_offset);
512
 
 
 
479
void osl_int_decrement(int precision, osl_int_p variable, osl_int_t value) {
513
480
  switch (precision) {
514
481
    case OSL_PRECISION_SP:
515
 
      *(long int *)result = *(long int *)value - (long int)1;
 
482
      variable->sp = value.sp - 1;
516
483
      break;
517
484
 
518
485
    case OSL_PRECISION_DP:
519
 
      *(long long int *)result = *(long long int *)value - (long long int)1;
 
486
      variable->dp = value.dp - (long long int)1;
520
487
      break;
521
488
 
522
489
#ifdef OSL_GMP_IS_HERE
523
490
    case OSL_PRECISION_MP: {
524
491
      mpz_t one;
525
492
      mpz_init_set_si(one, 1);
526
 
      mpz_sub(*(mpz_t *)result, *(mpz_t *)value, one);
 
493
      mpz_sub(*((mpz_t*)variable->mp), *((mpz_t*)value.mp), one);
527
494
      mpz_clear(one);
528
495
      break;
529
496
    }
536
503
 
537
504
 
538
505
/**
539
 
 * result_base[result_offset] = val1_base[val1_offset]+val2_base[val2_offset];
 
506
 * variable <- val1 + val2;
540
507
 */
541
508
void osl_int_add(int precision,
542
 
                 void * result_base, int result_offset,
543
 
                 void * val1_base,   int val1_offset,
544
 
                 void * val2_base,   int val2_offset) {
545
 
  void * result = osl_int_address(precision, result_base, result_offset);
546
 
  void * val1   = osl_int_address(precision, val1_base, val1_offset);
547
 
  void * val2   = osl_int_address(precision, val2_base, val2_offset);
548
 
 
 
509
                 osl_int_p variable, osl_int_t val1, osl_int_t val2) {
549
510
  switch (precision) {
550
511
    case OSL_PRECISION_SP:
551
 
      *(long int *)result = *(long int *)val1 + *(long int *)val2;
 
512
      variable->sp = val1.sp + val2.sp;
552
513
      break;
553
514
 
554
515
    case OSL_PRECISION_DP:
555
 
      *(long long int *)result = *(long long int *)val1 +
556
 
                                 *(long long int *)val2;
 
516
      variable->dp = val1.dp + val2.dp;
557
517
      break;
558
518
 
559
519
#ifdef OSL_GMP_IS_HERE
560
520
    case OSL_PRECISION_MP:
561
 
      mpz_add(*(mpz_t *)result, *(mpz_t *)val1, *(mpz_t *)val2);
 
521
      mpz_add(*((mpz_t*)variable->mp), *((mpz_t*)val1.mp), *((mpz_t*)val2.mp));
562
522
      break;
563
523
#endif
564
524
 
569
529
 
570
530
 
571
531
/**
572
 
 * result_base[result_offset] = value_base[value_offset] + i;
 
532
 * variable <- value + i;
573
533
 */
574
534
void osl_int_add_si(int precision,
575
 
                    void * result_base, int result_offset,
576
 
                    void * value_base,  int value_offset, int i) {
577
 
  void * result = osl_int_address(precision, result_base, result_offset);
578
 
  void * value  = osl_int_address(precision, value_base, value_offset);
579
 
 
 
535
                    osl_int_p variable, osl_int_t value, int i) {
580
536
  switch (precision) {
581
537
    case OSL_PRECISION_SP:
582
 
      *(long int *)result = *(long int *)value + (long int)i;
 
538
      variable->sp = value.sp + (long int)i;
583
539
      break;
584
540
 
585
541
    case OSL_PRECISION_DP:
586
 
      *(long long int *)result = *(long long int *)value + (long long int)i;
 
542
      variable->dp = value.dp + (long long int)i;
587
543
      break;
588
544
 
589
545
#ifdef OSL_GMP_IS_HERE
590
546
    case OSL_PRECISION_MP: {
591
547
      mpz_t si;
592
548
      mpz_init_set_si(si, i);
593
 
      mpz_add(*(mpz_t *)result, *(mpz_t *)value, si);
 
549
      mpz_add(*((mpz_t*)variable->mp), *((mpz_t*)value.mp), si);
594
550
      mpz_clear(si);
595
551
      break;
596
552
    }
603
559
 
604
560
 
605
561
/**
606
 
 * result_base[result_offset] = val1_base[val1_offset]*val2_base[val2_offset];
 
562
 * variable <- val1 * val2;
607
563
 */
608
564
void osl_int_mul(int precision,
609
 
                 void * result_base, int result_offset,
610
 
                 void * val1_base,   int val1_offset,
611
 
                 void * val2_base,   int val2_offset) {
612
 
  void * result = osl_int_address(precision, result_base, result_offset);
613
 
  void * val1   = osl_int_address(precision, val1_base, val1_offset);
614
 
  void * val2   = osl_int_address(precision, val2_base, val2_offset);
615
 
 
 
565
                 osl_int_p variable, osl_int_t val1, osl_int_t val2) {
616
566
  switch (precision) {
617
567
    case OSL_PRECISION_SP:
618
 
      *(long int *)result = *(long int *)val1 * *(long int *)val2;
 
568
      variable->sp = val1.sp * val2.sp;
619
569
      break;
620
570
 
621
571
    case OSL_PRECISION_DP:
622
 
      *(long long int *)result = *(long long int *)val1 *
623
 
                                 *(long long int *)val2;
 
572
      variable->dp = val1.dp * val2.dp;
624
573
      break;
625
574
 
626
575
#ifdef OSL_GMP_IS_HERE
627
576
    case OSL_PRECISION_MP:
628
 
      mpz_mul(*(mpz_t *)result, *(mpz_t *)val1, *(mpz_t *)val2);
 
577
      mpz_mul(*((mpz_t*)variable->mp), *((mpz_t*)val1.mp), *((mpz_t*)val2.mp));
629
578
      break;
630
579
#endif
631
580
 
636
585
 
637
586
 
638
587
/**
639
 
 * result_base[result_offset] = value_base[value_offset] * i;
 
588
 * variable <- value * i;
640
589
 */
641
590
void osl_int_mul_si(int precision,
642
 
                    void * result_base, int result_offset,
643
 
                    void * value_base,  int value_offset, int i) {
644
 
  void * result = osl_int_address(precision, result_base, result_offset);
645
 
  void * value  = osl_int_address(precision, value_base, value_offset);
646
 
 
 
591
                    osl_int_p variable, osl_int_t value, int i) {
647
592
  switch (precision) {
648
593
    case OSL_PRECISION_SP:
649
 
      *(long int *)result = *(long int *)value * (long int)i;
 
594
      variable->sp = value.sp * (long int)i;
650
595
      break;
651
596
 
652
597
    case OSL_PRECISION_DP:
653
 
      *(long long int *)result = *(long long int *)value * (long long int)i;
 
598
      variable->dp = value.dp * (long long int)i;
654
599
      break;
655
600
 
656
601
#ifdef OSL_GMP_IS_HERE
657
602
    case OSL_PRECISION_MP:
658
 
      mpz_mul_si(*(mpz_t *)result, *(mpz_t *)value, i);
 
603
      mpz_mul_si(*((mpz_t*)variable->mp), *((mpz_t*)value.mp), i);
659
604
      break;
660
605
#endif
661
606
 
666
611
 
667
612
 
668
613
/**
669
 
 * result_base[result_offset] = val1_base[val1_offset]-val2_base[val2_offset];
 
614
 * variable <- val1 - val2;
670
615
 */
671
616
void osl_int_sub(int precision,
672
 
                 void * result_base, int result_offset,
673
 
                 void * val1_base,   int val1_offset,
674
 
                 void * val2_base,   int val2_offset) {
675
 
  void * result = osl_int_address(precision, result_base, result_offset);
676
 
  void * val1   = osl_int_address(precision, val1_base, val1_offset);
677
 
  void * val2   = osl_int_address(precision, val2_base, val2_offset);
678
 
 
679
 
  switch (precision) {
680
 
    case OSL_PRECISION_SP:
681
 
      *(long int *)result = *(long int *)val1 - *(long int *)val2;
682
 
      break;
683
 
 
684
 
    case OSL_PRECISION_DP:
685
 
      *(long long int *)result = *(long long int *)val1 -
686
 
                                 *(long long int *)val2;
687
 
      break;
688
 
 
689
 
#ifdef OSL_GMP_IS_HERE
690
 
    case OSL_PRECISION_MP:
691
 
      mpz_sub(*(mpz_t *)result, *(mpz_t *)val1, *(mpz_t *)val2);
692
 
      break;
693
 
#endif
694
 
 
695
 
    default:
696
 
      OSL_error("unknown precision");
697
 
  }
698
 
}
699
 
 
700
 
 
701
 
/**
702
 
 * result_base[result_offset] = -value_base[value_offset];
703
 
 */
704
 
void osl_int_oppose(int precision,
705
 
                    void * result_base, int result_offset,
706
 
                    void * value_base,  int value_offset) {
707
 
  void * result = osl_int_address(precision, result_base, result_offset);
708
 
  void * value  = osl_int_address(precision, value_base, value_offset);
709
 
 
710
 
  switch (precision) {
711
 
    case OSL_PRECISION_SP:
712
 
      *(long int *)result = -*(long int *)value;
713
 
      break;
714
 
 
715
 
    case OSL_PRECISION_DP:
716
 
      *(long long int *)result = -*(long long int *)value;
717
 
      break;
718
 
 
719
 
#ifdef OSL_GMP_IS_HERE
720
 
    case OSL_PRECISION_MP:
721
 
      mpz_neg(*(mpz_t *)result, *(mpz_t *)value);
 
617
                 osl_int_p variable, osl_int_t val1, osl_int_t val2) {
 
618
  switch (precision) {
 
619
    case OSL_PRECISION_SP:
 
620
      variable->sp = val1.sp - val2.sp;
 
621
      break;
 
622
 
 
623
    case OSL_PRECISION_DP:
 
624
      variable->dp = val1.dp - val2.dp;
 
625
      break;
 
626
 
 
627
#ifdef OSL_GMP_IS_HERE
 
628
    case OSL_PRECISION_MP:
 
629
      mpz_sub(*((mpz_t*)variable->mp), *((mpz_t*)val1.mp), *((mpz_t*)val2.mp));
 
630
      break;
 
631
#endif
 
632
 
 
633
    default:
 
634
      OSL_error("unknown precision");
 
635
  }
 
636
}
 
637
 
 
638
 
 
639
/**
 
640
 * variable <- -value;
 
641
 */
 
642
void osl_int_oppose(int precision, osl_int_p variable, osl_int_t value) {
 
643
  switch (precision) {
 
644
    case OSL_PRECISION_SP:
 
645
      variable->sp = -value.sp;
 
646
      break;
 
647
 
 
648
    case OSL_PRECISION_DP:
 
649
      variable->dp = -value.dp;
 
650
      break;
 
651
 
 
652
#ifdef OSL_GMP_IS_HERE
 
653
    case OSL_PRECISION_MP:
 
654
      mpz_neg(*((mpz_t*)variable->mp), *((mpz_t*)value.mp));
 
655
      break;
 
656
#endif
 
657
 
 
658
    default:
 
659
      OSL_error("unknown precision");
 
660
  }
 
661
}
 
662
 
 
663
 
 
664
/**
 
665
 * variable <- | value |;
 
666
 */
 
667
void osl_int_abs(int precision, osl_int_p variable, osl_int_t value) {
 
668
  switch (precision) {
 
669
    case OSL_PRECISION_SP:
 
670
      variable->sp = (value.sp > 0) ? value.sp : -value.sp;
 
671
      break;
 
672
 
 
673
    case OSL_PRECISION_DP:
 
674
      variable->dp = (value.dp > 0) ? value.dp : -value.dp;
 
675
      break;
 
676
 
 
677
#ifdef OSL_GMP_IS_HERE
 
678
    case OSL_PRECISION_MP:
 
679
      mpz_abs(*((mpz_t*)variable->mp), *((mpz_t*)value.mp));
722
680
      break;
723
681
#endif
724
682
 
734
692
 
735
693
 
736
694
/**
737
 
 * (val1_base[val1_offset] == val2_base[val2_offset])
738
 
 */
739
 
int osl_int_eq(int precision,
740
 
               void * val1_base, int val1_offset,
741
 
               void * val2_base, int val2_offset) {
742
 
  void * val1 = osl_int_address(precision, val1_base, val1_offset);
743
 
  void * val2 = osl_int_address(precision, val2_base, val2_offset);
744
 
 
745
 
  switch (precision) {
746
 
    case OSL_PRECISION_SP:
747
 
      return (*(long int *)val1 == *(long int *)val2);
748
 
 
749
 
    case OSL_PRECISION_DP:
750
 
      return (*(long long int *)val1 == *(long long int *)val2);
751
 
 
752
 
#ifdef OSL_GMP_IS_HERE
753
 
    case OSL_PRECISION_MP:
754
 
      return (mpz_cmp(*(mpz_t *)val1, *(mpz_t *)val2) == 0);
755
 
#endif
756
 
 
757
 
    default:
758
 
      OSL_error("unknown precision");
759
 
  }
760
 
}
761
 
 
762
 
 
763
 
/**
764
 
 * (val1_base[val1_offset] != val2_base[val2_offset])
765
 
 */
766
 
int osl_int_ne(int precision,
767
 
               void * val1_base, int val1_offset,
768
 
               void * val2_base, int val2_offset) {
769
 
  return !osl_int_eq(precision,
770
 
                          val1_base, val1_offset,
771
 
                          val2_base, val2_offset);
772
 
}
773
 
 
774
 
 
775
 
/**
776
 
 * (value_base[value_offset] > 0)
777
 
 */
778
 
int osl_int_pos(int precision, void * value_base, int value_offset) {
779
 
  void * value = osl_int_address(precision, value_base, value_offset);
780
 
 
781
 
  switch (precision) {
782
 
    case OSL_PRECISION_SP:
783
 
      return (*(long int *)value > 0);
784
 
 
785
 
    case OSL_PRECISION_DP:
786
 
      return (*(long long int *)value > 0);
787
 
 
788
 
#ifdef OSL_GMP_IS_HERE
789
 
    case OSL_PRECISION_MP:
790
 
      return (mpz_sgn(*(mpz_t *)value) > 0);
791
 
#endif
792
 
 
793
 
    default:
794
 
      OSL_error("unknown precision");
795
 
  }
796
 
}
797
 
 
798
 
 
799
 
/**
800
 
 * (value_base[value_offset] < 0)
801
 
 */
802
 
int osl_int_neg(int precision, void * value_base, int value_offset) {
803
 
  void * value = osl_int_address(precision, value_base, value_offset);
804
 
 
805
 
  switch (precision) {
806
 
    case OSL_PRECISION_SP:
807
 
      return (*(long int *)value < 0);
808
 
 
809
 
    case OSL_PRECISION_DP:
810
 
      return (*(long long int *)value < 0);
811
 
 
812
 
#ifdef OSL_GMP_IS_HERE
813
 
    case OSL_PRECISION_MP:
814
 
      return (mpz_sgn(*(mpz_t *)value) < 0);
815
 
#endif
816
 
 
817
 
    default:
818
 
      OSL_error("unknown precision");
819
 
  }
820
 
}
821
 
 
822
 
 
823
 
/**
824
 
 * (value_base[value_offset] == 0)
825
 
 */
826
 
int osl_int_zero(int precision, void * value_base, int value_offset) {
827
 
  void * value = osl_int_address(precision, value_base, value_offset);
828
 
 
829
 
  switch (precision) {
830
 
    case OSL_PRECISION_SP:
831
 
      return (*(long int *)value == 0);
832
 
 
833
 
    case OSL_PRECISION_DP:
834
 
      return (*(long long int *)value == 0);
835
 
 
836
 
#ifdef OSL_GMP_IS_HERE
837
 
    case OSL_PRECISION_MP:
838
 
      return (mpz_sgn(*(mpz_t *)value) == 0);
839
 
#endif
840
 
 
841
 
    default:
842
 
      OSL_error("unknown precision");
843
 
  }
844
 
}
845
 
 
846
 
 
847
 
/**
848
 
 * (value_base[value_offset] == 1)
849
 
 */
850
 
int osl_int_one(int precision, void * value_base, int value_offset) {
851
 
  void * value = osl_int_address(precision, value_base, value_offset);
852
 
 
853
 
  switch (precision) {
854
 
    case OSL_PRECISION_SP:
855
 
      return (*(long int *)value == (long int)1);
856
 
 
857
 
    case OSL_PRECISION_DP:
858
 
      return (*(long long int *)value == (long long int)1);
859
 
 
860
 
#ifdef OSL_GMP_IS_HERE
861
 
    case OSL_PRECISION_MP:
862
 
      return (mpz_cmp_si(*(mpz_t *)value, 1) == 0);
863
 
#endif
864
 
 
865
 
    default:
866
 
      OSL_error("unknown precision");
867
 
  }
868
 
}
869
 
 
870
 
 
871
 
/**
872
 
 * (value_base[value_offset] == -1)
873
 
 */
874
 
int osl_int_mone(int precision, void * value_base, int value_offset) {
875
 
  void * value = osl_int_address(precision, value_base, value_offset);
876
 
 
877
 
  switch (precision) {
878
 
    case OSL_PRECISION_SP:
879
 
      return (*(long int *)value == (long int)-1);
880
 
 
881
 
    case OSL_PRECISION_DP:
882
 
      return (*(long long int *)value == (long long int)-1);
883
 
 
884
 
#ifdef OSL_GMP_IS_HERE
885
 
    case OSL_PRECISION_MP:
886
 
      return (mpz_cmp_si(*(mpz_t *)value, -1) == 0);
887
 
#endif
888
 
 
889
 
    default:
890
 
      OSL_error("unknown precision");
891
 
  }
892
 
}
893
 
 
894
 
 
895
 
/**
896
 
 * ((val1_base[val1_offset] % val2_base[val2_offset]) == 0)
897
 
 */
898
 
int osl_int_divisible(int precision,
899
 
                      void * val1_base, int val1_offset,
900
 
                      void * val2_base, int val2_offset) {
901
 
  void * val1 = osl_int_address(precision, val1_base, val1_offset);
902
 
  void * val2 = osl_int_address(precision, val2_base, val2_offset);
903
 
 
904
 
  switch (precision) {
905
 
    case OSL_PRECISION_SP:
906
 
      return ((*(long int *)val1 % *(long int *)val2) == 0);
907
 
 
908
 
    case OSL_PRECISION_DP:
909
 
      return ((*(long long int *)val1 % *(long long int *)val2) == 0);
910
 
 
911
 
#ifdef OSL_GMP_IS_HERE
912
 
    case OSL_PRECISION_MP:
913
 
      return mpz_divisible_p(*(mpz_t *)val1, *(mpz_t *)val2);
 
695
 * (val1 == val2)
 
696
 */
 
697
int osl_int_eq(int precision, osl_int_t val1, osl_int_t val2) {
 
698
  switch (precision) {
 
699
    case OSL_PRECISION_SP:
 
700
      return (val1.sp == val2.sp);
 
701
 
 
702
    case OSL_PRECISION_DP:
 
703
      return (val1.dp == val2.dp);
 
704
 
 
705
#ifdef OSL_GMP_IS_HERE
 
706
    case OSL_PRECISION_MP:
 
707
      return (mpz_cmp(*((mpz_t*)val1.mp), *((mpz_t*)val2.mp)) == 0);
 
708
#endif
 
709
 
 
710
    default:
 
711
      OSL_error("unknown precision");
 
712
  }
 
713
}
 
714
 
 
715
 
 
716
/**
 
717
 * (val1 != val2)
 
718
 */
 
719
int osl_int_ne(int precision, osl_int_t val1, osl_int_t val2) {
 
720
  return !osl_int_eq(precision, val1, val2);
 
721
}
 
722
 
 
723
 
 
724
/**
 
725
 * (value > 0)
 
726
 */
 
727
int osl_int_pos(int precision, osl_int_t value) {
 
728
  switch (precision) {
 
729
    case OSL_PRECISION_SP:
 
730
      return (value.sp > 0);
 
731
 
 
732
    case OSL_PRECISION_DP:
 
733
      return (value.dp > 0);
 
734
 
 
735
#ifdef OSL_GMP_IS_HERE
 
736
    case OSL_PRECISION_MP:
 
737
      return (mpz_sgn(*((mpz_t*)value.mp)) > 0);
 
738
#endif
 
739
 
 
740
    default:
 
741
      OSL_error("unknown precision");
 
742
  }
 
743
}
 
744
 
 
745
 
 
746
/**
 
747
 * (value < 0)
 
748
 */
 
749
int osl_int_neg(int precision, osl_int_t value) {
 
750
  switch (precision) {
 
751
    case OSL_PRECISION_SP:
 
752
      return (value.sp < 0);
 
753
 
 
754
    case OSL_PRECISION_DP:
 
755
      return (value.dp < 0);
 
756
 
 
757
#ifdef OSL_GMP_IS_HERE
 
758
    case OSL_PRECISION_MP:
 
759
      return (mpz_sgn(*((mpz_t*)value.mp)) < 0);
 
760
#endif
 
761
 
 
762
    default:
 
763
      OSL_error("unknown precision");
 
764
  }
 
765
}
 
766
 
 
767
 
 
768
/**
 
769
 * (value == 0)
 
770
 */
 
771
int osl_int_zero(int precision, osl_int_t value) {
 
772
  switch (precision) {
 
773
    case OSL_PRECISION_SP:
 
774
      return (value.sp == 0);
 
775
 
 
776
    case OSL_PRECISION_DP:
 
777
      return (value.dp == 0);
 
778
 
 
779
#ifdef OSL_GMP_IS_HERE
 
780
    case OSL_PRECISION_MP:
 
781
      return (mpz_sgn(*((mpz_t*)value.mp)) == 0);
 
782
#endif
 
783
 
 
784
    default:
 
785
      OSL_error("unknown precision");
 
786
  }
 
787
}
 
788
 
 
789
 
 
790
/**
 
791
 * (value == 1)
 
792
 */
 
793
int osl_int_one(int precision, osl_int_t value) {
 
794
  switch (precision) {
 
795
    case OSL_PRECISION_SP:
 
796
      return (value.sp == (long int)1);
 
797
 
 
798
    case OSL_PRECISION_DP:
 
799
      return (value.dp == (long long int)1);
 
800
 
 
801
#ifdef OSL_GMP_IS_HERE
 
802
    case OSL_PRECISION_MP:
 
803
      return (mpz_cmp_si(*((mpz_t*)value.mp), 1) == 0);
 
804
#endif
 
805
 
 
806
    default:
 
807
      OSL_error("unknown precision");
 
808
  }
 
809
}
 
810
 
 
811
 
 
812
/**
 
813
 * (value == -1)
 
814
 */
 
815
int osl_int_mone(int precision, osl_int_t value) {
 
816
  switch (precision) {
 
817
    case OSL_PRECISION_SP:
 
818
      return (value.sp == (long int)-1);
 
819
 
 
820
    case OSL_PRECISION_DP:
 
821
      return (value.dp == (long long int)-1);
 
822
 
 
823
#ifdef OSL_GMP_IS_HERE
 
824
    case OSL_PRECISION_MP:
 
825
      return (mpz_cmp_si(*((mpz_t*)value.mp), -1) == 0);
 
826
#endif
 
827
 
 
828
    default:
 
829
      OSL_error("unknown precision");
 
830
  }
 
831
}
 
832
 
 
833
 
 
834
/**
 
835
 * ((val1 % val2) == 0)
 
836
 */
 
837
int osl_int_divisible(int precision, osl_int_t val1, osl_int_t val2) {
 
838
  switch (precision) {
 
839
    case OSL_PRECISION_SP:
 
840
      return ((val1.sp % val2.sp) == 0);
 
841
 
 
842
    case OSL_PRECISION_DP:
 
843
      return ((val1.dp % val2.dp) == 0);
 
844
 
 
845
#ifdef OSL_GMP_IS_HERE
 
846
    case OSL_PRECISION_MP:
 
847
      return mpz_divisible_p(*((mpz_t*)val1.mp), *((mpz_t*)val2.mp));
914
848
#endif
915
849
 
916
850
    default: