Tue, 15 Feb 2005 18:57:52 +0100
Tailorization of trunk
Import of the upstream sources from the repository
http://tao.uab.es/ion/svn/libtu/trunk
as of revision 2
| 0 | 1 | /* |
| 2 | * libtu/np-conv.h | |
| 3 | * | |
| 4 | * Copyright (c) Tuomo Valkonen 1999-2000. | |
| 5 | * | |
| 6 | * This file is distributed under the terms of the "Artistic License". | |
| 7 | * See the included file LICENSE for details. | |
| 8 | */ | |
| 9 | ||
| 10 | #include <math.h> | |
| 11 | ||
| 12 | ||
| 13 | #define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN) \ | |
| 14 | static int num_to_##T(T *ret, const NPNum *num, bool allow_uns_big) \ | |
| 15 | { \ | |
| 16 | if(num->exponent) \ | |
| 17 | return E_TOKZ_NOTINT; \ | |
| 18 | if(num->nmantissa>0) \ | |
| 19 | return E_TOKZ_RANGE; \ | |
| 20 | \ | |
| 21 | if(!num->negative){ \ | |
| 22 | *ret=num->mantissa[0]; \ | |
| 23 | if(allow_uns_big?num->mantissa[0]>UMAX:num->mantissa[0]>MAX) \ | |
| 24 | return E_TOKZ_RANGE; \ | |
| 25 | }else{ \ | |
| 26 | *ret=-num->mantissa[0]; \ | |
| 27 | if(num->mantissa[0]>-(ulong)MIN) \ | |
| 28 | return E_TOKZ_RANGE; \ | |
| 29 | } \ | |
| 30 | return 0; \ | |
| 31 | } | |
| 32 | ||
| 33 | #define FN_NUM_TO_UNSIGNED(T, UMAX, MIN) \ | |
| 34 | static int num_to_##T(T *ret, const NPNum *num, bool allow_neg) \ | |
| 35 | { \ | |
| 36 | if(num->exponent) \ | |
| 37 | return E_TOKZ_NOTINT; \ | |
| 38 | if(num->nmantissa>0) \ | |
| 39 | return E_TOKZ_RANGE; \ | |
| 40 | \ | |
| 41 | if(!num->negative){ \ | |
| 42 | *ret=num->mantissa[0]; \ | |
| 43 | if(num->mantissa[0]>UMAX) \ | |
| 44 | return E_TOKZ_RANGE; \ | |
| 45 | }else{ \ | |
| 46 | *ret=-num->mantissa[0]; \ | |
| 47 | if(!allow_neg || num->mantissa[0]>(ulong)-MIN) \ | |
| 48 | return E_TOKZ_RANGE; \ | |
| 49 | } \ | |
| 50 | return 0; \ | |
| 51 | } | |
| 52 | ||
| 53 | ||
| 54 | #define FN_NUM_TO_FLOAT(T, POW) \ | |
| 55 | static int num_to_##T(T *ret, const NPNum *num) \ | |
| 56 | { \ | |
| 57 | T d=0; \ | |
| 58 | int i; \ | |
| 59 | \ | |
| 60 | for(i=num->nmantissa;i>=0;i--) \ | |
| 61 | d=d*(T)(ULONG_MAX+1.0)+num->mantissa[i]; \ | |
| 62 | \ | |
| 63 | d*=POW(10, num->exponent); \ | |
| 64 | *ret=d; \ | |
| 65 | \ | |
| 66 | return 0; \ | |
| 67 | } | |
| 68 | ||
| 69 | FN_NUM_TO_SIGNED(long, ULONG_MAX, LONG_MAX, LONG_MIN) | |
| 70 | FN_NUM_TO_SIGNED(char, UCHAR_MAX, CHAR_MAX, CHAR_MIN) | |
| 71 | FN_NUM_TO_FLOAT(double, pow) | |
| 72 | ||
| 73 | #undef NEG |