7 * See the included file LICENSE for details. |
7 * See the included file LICENSE for details. |
8 */ |
8 */ |
9 |
9 |
10 #include <math.h> |
10 #include <math.h> |
11 |
11 |
|
12 #ifdef NP_SIMPLE_IMPL |
|
13 |
|
14 #define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN) \ |
|
15 static int num_to_##T(T *ret, const NPNum *num, bool allow_uns_big) \ |
|
16 { \ |
|
17 if(num->type!=NPNUM_INT) \ |
|
18 return E_TOKZ_NOTINT; \ |
|
19 \ |
|
20 if(!num->negative){ \ |
|
21 *ret=num->ival; \ |
|
22 if(allow_uns_big?num->ival>UMAX:num->ival>MAX) \ |
|
23 return E_TOKZ_RANGE; \ |
|
24 }else{ \ |
|
25 *ret=-num->ival; \ |
|
26 if(num->ival>-(ulong)MIN) \ |
|
27 return E_TOKZ_RANGE; \ |
|
28 } \ |
|
29 return 0; \ |
|
30 } |
|
31 |
|
32 #define FN_NUM_TO_UNSIGNED(T, UMAX, MIN) \ |
|
33 static int num_to_##T(T *ret, const NPNum *num, bool allow_neg) \ |
|
34 { \ |
|
35 if(num->type!=NPNUM_INT) \ |
|
36 return E_TOKZ_NOTINT; \ |
|
37 \ |
|
38 if(!num->negative){ \ |
|
39 *ret=num->ival; \ |
|
40 if(num->ival>UMAX) \ |
|
41 return E_TOKZ_RANGE; \ |
|
42 }else{ \ |
|
43 *ret=-num->ival; \ |
|
44 if(!allow_neg || num->ival>(ulong)-MIN) \ |
|
45 return E_TOKZ_RANGE; \ |
|
46 } \ |
|
47 return 0; \ |
|
48 } |
|
49 |
|
50 #define FN_NUM_TO_FLOAT(T, POW) \ |
|
51 static int num_to_##T(T *ret, const NPNum *num) \ |
|
52 { \ |
|
53 *ret=(num->negative?-num->fval:num->fval); \ |
|
54 return 0; \ |
|
55 } |
|
56 |
|
57 #else /* NP_SIMPLE_IMPL */ |
12 |
58 |
13 #define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN) \ |
59 #define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN) \ |
14 static int num_to_##T(T *ret, const NPNum *num, bool allow_uns_big) \ |
60 static int num_to_##T(T *ret, const NPNum *num, bool allow_uns_big) \ |
15 { \ |
61 { \ |
16 if(num->exponent) \ |
62 if(num->exponent) \ |
58 int i; \ |
104 int i; \ |
59 \ |
105 \ |
60 for(i=num->nmantissa;i>=0;i--) \ |
106 for(i=num->nmantissa;i>=0;i--) \ |
61 d=d*(T)(ULONG_MAX+1.0)+num->mantissa[i]; \ |
107 d=d*(T)(ULONG_MAX+1.0)+num->mantissa[i]; \ |
62 \ |
108 \ |
63 d*=POW(10, num->exponent); \ |
109 d*=POW(num->base, num->exponent); \ |
64 *ret=d; \ |
110 *ret=d; \ |
65 \ |
111 \ |
66 return 0; \ |
112 return 0; \ |
67 } |
113 } |
|
114 |
|
115 #endif /* NP_SIMPLE_IMPL */ |
68 |
116 |
69 FN_NUM_TO_SIGNED(long, ULONG_MAX, LONG_MAX, LONG_MIN) |
117 FN_NUM_TO_SIGNED(long, ULONG_MAX, LONG_MAX, LONG_MIN) |
70 FN_NUM_TO_SIGNED(char, UCHAR_MAX, CHAR_MAX, CHAR_MIN) |
118 FN_NUM_TO_SIGNED(char, UCHAR_MAX, CHAR_MAX, CHAR_MIN) |
71 FN_NUM_TO_FLOAT(double, pow) |
119 FN_NUM_TO_FLOAT(double, pow) |
72 |
120 |