np-conv.h

Tue, 29 Aug 2000 00:03:00 +0200

author
tuomov
date
Tue, 29 Aug 2000 00:03:00 +0200
changeset 23
5f8005150227
parent 9
55e7f2ff6021
child 36
63cd573ffbcf
permissions
-rw-r--r--

trunk: changeset 26
Once again, 'make install' portability fixes

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

mercurial