Wed, 20 Dec 2000 20:34:02 +0100
trunk: changeset 32
Use 'ranlib' instead of 'ar s'.
0 | 1 | /* |
2 | * libtu/np-conv.h | |
3 | * | |
9 | 4 | * Copyright (c) Tuomo Valkonen 1999-2000. |
0 | 5 | * See the included file LICENSE for details. |
6 | */ | |
7 | ||
8 | #include <math.h> | |
9 | ||
3 | 10 | #ifdef NP_SIMPLE_IMPL |
11 | ||
12 | #define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN) \ | |
13 | static int num_to_##T(T *ret, const NPNum *num, bool allow_uns_big) \ | |
14 | { \ | |
15 | if(num->type!=NPNUM_INT) \ | |
16 | return E_TOKZ_NOTINT; \ | |
17 | \ | |
18 | if(!num->negative){ \ | |
19 | *ret=num->ival; \ | |
20 | if(allow_uns_big?num->ival>UMAX:num->ival>MAX) \ | |
21 | return E_TOKZ_RANGE; \ | |
22 | }else{ \ | |
23 | *ret=-num->ival; \ | |
24 | if(num->ival>-(ulong)MIN) \ | |
25 | return E_TOKZ_RANGE; \ | |
26 | } \ | |
27 | return 0; \ | |
28 | } | |
29 | ||
30 | #define FN_NUM_TO_UNSIGNED(T, UMAX, MIN) \ | |
31 | static int num_to_##T(T *ret, const NPNum *num, bool allow_neg) \ | |
32 | { \ | |
33 | if(num->type!=NPNUM_INT) \ | |
34 | return E_TOKZ_NOTINT; \ | |
35 | \ | |
36 | if(!num->negative){ \ | |
37 | *ret=num->ival; \ | |
38 | if(num->ival>UMAX) \ | |
39 | return E_TOKZ_RANGE; \ | |
40 | }else{ \ | |
41 | *ret=-num->ival; \ | |
42 | if(!allow_neg || num->ival>(ulong)-MIN) \ | |
43 | return E_TOKZ_RANGE; \ | |
44 | } \ | |
45 | return 0; \ | |
46 | } | |
47 | ||
48 | #define FN_NUM_TO_FLOAT(T, POW) \ | |
49 | static int num_to_##T(T *ret, const NPNum *num) \ | |
50 | { \ | |
51 | *ret=(num->negative?-num->fval:num->fval); \ | |
52 | return 0; \ | |
53 | } | |
54 | ||
55 | #else /* NP_SIMPLE_IMPL */ | |
0 | 56 | |
57 | #define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN) \ | |
58 | static int num_to_##T(T *ret, const NPNum *num, bool allow_uns_big) \ | |
59 | { \ | |
60 | if(num->exponent) \ | |
61 | return E_TOKZ_NOTINT; \ | |
62 | if(num->nmantissa>0) \ | |
63 | return E_TOKZ_RANGE; \ | |
64 | \ | |
65 | if(!num->negative){ \ | |
66 | *ret=num->mantissa[0]; \ | |
67 | if(allow_uns_big?num->mantissa[0]>UMAX:num->mantissa[0]>MAX) \ | |
68 | return E_TOKZ_RANGE; \ | |
69 | }else{ \ | |
70 | *ret=-num->mantissa[0]; \ | |
71 | if(num->mantissa[0]>-(ulong)MIN) \ | |
72 | return E_TOKZ_RANGE; \ | |
73 | } \ | |
74 | return 0; \ | |
75 | } | |
76 | ||
77 | #define FN_NUM_TO_UNSIGNED(T, UMAX, MIN) \ | |
78 | static int num_to_##T(T *ret, const NPNum *num, bool allow_neg) \ | |
79 | { \ | |
80 | if(num->exponent) \ | |
81 | return E_TOKZ_NOTINT; \ | |
82 | if(num->nmantissa>0) \ | |
83 | return E_TOKZ_RANGE; \ | |
84 | \ | |
85 | if(!num->negative){ \ | |
86 | *ret=num->mantissa[0]; \ | |
87 | if(num->mantissa[0]>UMAX) \ | |
88 | return E_TOKZ_RANGE; \ | |
89 | }else{ \ | |
90 | *ret=-num->mantissa[0]; \ | |
91 | if(!allow_neg || num->mantissa[0]>(ulong)-MIN) \ | |
92 | return E_TOKZ_RANGE; \ | |
93 | } \ | |
94 | return 0; \ | |
95 | } | |
96 | ||
97 | ||
98 | #define FN_NUM_TO_FLOAT(T, POW) \ | |
99 | static int num_to_##T(T *ret, const NPNum *num) \ | |
100 | { \ | |
101 | T d=0; \ | |
102 | int i; \ | |
103 | \ | |
104 | for(i=num->nmantissa;i>=0;i--) \ | |
105 | d=d*(T)(ULONG_MAX+1.0)+num->mantissa[i]; \ | |
106 | \ | |
3 | 107 | d*=POW(num->base, num->exponent); \ |
0 | 108 | *ret=d; \ |
109 | \ | |
110 | return 0; \ | |
111 | } | |
112 | ||
3 | 113 | #endif /* NP_SIMPLE_IMPL */ |
114 | ||
0 | 115 | FN_NUM_TO_SIGNED(long, ULONG_MAX, LONG_MAX, LONG_MIN) |
116 | FN_NUM_TO_SIGNED(char, UCHAR_MAX, CHAR_MAX, CHAR_MIN) | |
117 | FN_NUM_TO_FLOAT(double, pow) | |
118 | ||
119 | #undef NEG |