12 #ifdef NP_SIMPLE_IMPL |
12 #ifdef NP_SIMPLE_IMPL |
13 |
13 |
14 #define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN) \ |
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) \ |
15 static int num_to_##T(T *ret, const NPNum *num, bool allow_uns_big) \ |
16 { \ |
16 { \ |
17 if(num->type!=NPNUM_INT) \ |
17 if(num->type!=NPNUM_INT) \ |
18 return E_TOKZ_NOTINT; \ |
18 return E_TOKZ_NOTINT; \ |
19 \ |
19 \ |
20 if(!num->negative){ \ |
20 if(!num->negative){ \ |
21 *ret=num->ival; \ |
21 *ret=num->ival; \ |
22 if(allow_uns_big?num->ival>UMAX:num->ival>MAX) \ |
22 if(allow_uns_big?num->ival>UMAX:num->ival>MAX) \ |
23 return E_TOKZ_RANGE; \ |
23 return E_TOKZ_RANGE; \ |
24 }else{ \ |
24 }else{ \ |
25 *ret=-num->ival; \ |
25 *ret=-num->ival; \ |
26 if(num->ival>-(ulong)MIN) \ |
26 if(num->ival>-(ulong)MIN) \ |
27 return E_TOKZ_RANGE; \ |
27 return E_TOKZ_RANGE; \ |
28 } \ |
28 } \ |
29 return 0; \ |
29 return 0; \ |
30 } |
30 } |
31 |
31 |
32 #define FN_NUM_TO_UNSIGNED(T, UMAX, MIN) \ |
32 #define FN_NUM_TO_UNSIGNED(T, UMAX, MIN) \ |
33 static int num_to_##T(T *ret, const NPNum *num, bool allow_neg) \ |
33 static int num_to_##T(T *ret, const NPNum *num, bool allow_neg) \ |
34 { \ |
34 { \ |
35 if(num->type!=NPNUM_INT) \ |
35 if(num->type!=NPNUM_INT) \ |
36 return E_TOKZ_NOTINT; \ |
36 return E_TOKZ_NOTINT; \ |
37 \ |
37 \ |
38 if(!num->negative){ \ |
38 if(!num->negative){ \ |
39 *ret=num->ival; \ |
39 *ret=num->ival; \ |
40 if(num->ival>UMAX) \ |
40 if(num->ival>UMAX) \ |
41 return E_TOKZ_RANGE; \ |
41 return E_TOKZ_RANGE; \ |
42 }else{ \ |
42 }else{ \ |
43 *ret=-num->ival; \ |
43 *ret=-num->ival; \ |
44 if(!allow_neg || num->ival>(ulong)-MIN) \ |
44 if(!allow_neg || num->ival>(ulong)-MIN) \ |
45 return E_TOKZ_RANGE; \ |
45 return E_TOKZ_RANGE; \ |
46 } \ |
46 } \ |
47 return 0; \ |
47 return 0; \ |
48 } |
48 } |
49 |
49 |
50 #define FN_NUM_TO_FLOAT(T, POW) \ |
50 #define FN_NUM_TO_FLOAT(T, POW) \ |
51 static int num_to_##T(T *ret, const NPNum *num) \ |
51 static int num_to_##T(T *ret, const NPNum *num) \ |
52 { \ |
52 { \ |
53 *ret=(num->negative?-num->fval:num->fval); \ |
53 *ret=(num->negative?-num->fval:num->fval); \ |
54 return 0; \ |
54 return 0; \ |
55 } |
55 } |
56 |
56 |
57 #else /* NP_SIMPLE_IMPL */ |
57 #else /* NP_SIMPLE_IMPL */ |
58 |
58 |
59 #define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN) \ |
59 #define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN) \ |
60 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) \ |
61 { \ |
61 { \ |
62 if(num->exponent) \ |
62 if(num->exponent) \ |
63 return E_TOKZ_NOTINT; \ |
63 return E_TOKZ_NOTINT; \ |
64 if(num->nmantissa>0) \ |
64 if(num->nmantissa>0) \ |
65 return E_TOKZ_RANGE; \ |
65 return E_TOKZ_RANGE; \ |
66 \ |
66 \ |
67 if(!num->negative){ \ |
67 if(!num->negative){ \ |
68 *ret=num->mantissa[0]; \ |
68 *ret=num->mantissa[0]; \ |
69 if(allow_uns_big?num->mantissa[0]>UMAX:num->mantissa[0]>MAX) \ |
69 if(allow_uns_big?num->mantissa[0]>UMAX:num->mantissa[0]>MAX) \ |
70 return E_TOKZ_RANGE; \ |
70 return E_TOKZ_RANGE; \ |
71 }else{ \ |
71 }else{ \ |
72 *ret=-num->mantissa[0]; \ |
72 *ret=-num->mantissa[0]; \ |
73 if(num->mantissa[0]>-(ulong)MIN) \ |
73 if(num->mantissa[0]>-(ulong)MIN) \ |
74 return E_TOKZ_RANGE; \ |
74 return E_TOKZ_RANGE; \ |
75 } \ |
75 } \ |
76 return 0; \ |
76 return 0; \ |
77 } |
77 } |
78 |
78 |
79 #define FN_NUM_TO_UNSIGNED(T, UMAX, MIN) \ |
79 #define FN_NUM_TO_UNSIGNED(T, UMAX, MIN) \ |
80 static int num_to_##T(T *ret, const NPNum *num, bool allow_neg) \ |
80 static int num_to_##T(T *ret, const NPNum *num, bool allow_neg) \ |
81 { \ |
81 { \ |
82 if(num->exponent) \ |
82 if(num->exponent) \ |
83 return E_TOKZ_NOTINT; \ |
83 return E_TOKZ_NOTINT; \ |
84 if(num->nmantissa>0) \ |
84 if(num->nmantissa>0) \ |
85 return E_TOKZ_RANGE; \ |
85 return E_TOKZ_RANGE; \ |
86 \ |
86 \ |
87 if(!num->negative){ \ |
87 if(!num->negative){ \ |
88 *ret=num->mantissa[0]; \ |
88 *ret=num->mantissa[0]; \ |
89 if(num->mantissa[0]>UMAX) \ |
89 if(num->mantissa[0]>UMAX) \ |
90 return E_TOKZ_RANGE; \ |
90 return E_TOKZ_RANGE; \ |
91 }else{ \ |
91 }else{ \ |
92 *ret=-num->mantissa[0]; \ |
92 *ret=-num->mantissa[0]; \ |
93 if(!allow_neg || num->mantissa[0]>(ulong)-MIN) \ |
93 if(!allow_neg || num->mantissa[0]>(ulong)-MIN) \ |
94 return E_TOKZ_RANGE; \ |
94 return E_TOKZ_RANGE; \ |
95 } \ |
95 } \ |
96 return 0; \ |
96 return 0; \ |
97 } |
97 } |
98 |
98 |
99 |
99 |
100 #define FN_NUM_TO_FLOAT(T, POW) \ |
100 #define FN_NUM_TO_FLOAT(T, POW) \ |
101 static int num_to_##T(T *ret, const NPNum *num) \ |
101 static int num_to_##T(T *ret, const NPNum *num) \ |
102 { \ |
102 { \ |
103 T d=0; \ |
103 T d=0; \ |
104 int i; \ |
104 int i; \ |
105 \ |
|
106 for(i=num->nmantissa;i>=0;i--) \ |
|
107 d=d*(T)(ULONG_MAX+1.0)+num->mantissa[i]; \ |
|
108 \ |
105 \ |
109 d*=POW(num->base, num->exponent); \ |
106 for(i=num->nmantissa;i>=0;i--) \ |
110 *ret=d; \ |
107 d=d*(T)(ULONG_MAX+1.0)+num->mantissa[i]; \ |
111 \ |
108 \ |
112 return 0; \ |
109 d*=POW(num->base, num->exponent); \ |
|
110 *ret=d; \ |
|
111 \ |
|
112 return 0; \ |
113 } |
113 } |
114 |
114 |
115 #endif /* NP_SIMPLE_IMPL */ |
115 #endif /* NP_SIMPLE_IMPL */ |
116 |
116 |
117 FN_NUM_TO_SIGNED(long, ULONG_MAX, LONG_MAX, LONG_MIN) |
117 FN_NUM_TO_SIGNED(long, ULONG_MAX, LONG_MAX, LONG_MIN) |