np-conv.h

Mon, 17 Jan 2005 22:02:09 +0100

author
tuomov
date
Mon, 17 Jan 2005 22:02:09 +0100
changeset 85
9f94b2e96e3b
parent 53
f8f9366b359c
permissions
-rw-r--r--

trunk: changeset 1934
Fixed everything that requires locale stuff to check CF_NO_LOCALE.

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

mercurial