np/np-conv.h

Sat, 14 May 2005 22:23:20 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sat, 14 May 2005 22:23:20 +0200
changeset 100
8ae0f0aa5a16
parent 62
aae5facf9fc5
permissions
-rw-r--r--

Added some coercions to remove gcc complaints in snprintf_2.2.

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

mercurial