Ion C
C library for Ion
ion_decimal.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at:
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
12  * language governing permissions and limitations under the License.
13  */
14 
36 #ifndef ION_DECIMAL_H_
37 #define ION_DECIMAL_H_
38 
39 #include "ion_types.h"
40 #include "ion_platform_config.h"
41 
42 #ifndef DECNUMDIGITS
43  #error DECNUMDIGITS must be defined to be >= DECQUAD_Pmax
44 #elif DECNUMDIGITS < DECQUAD_Pmax
45  #error DECNUMDIGITS must be defined to be >= DECQUAD_Pmax
46 #endif
47 
51 #define ION_DECIMAL_STRLEN(ion_decimal) \
52  ((size_t)(((ion_decimal)->type == ION_DECIMAL_TYPE_QUAD) \
53  ? DECQUAD_String \
54  : (((ion_decimal)->type == ION_DECIMAL_TYPE_UNKNOWN) \
55  ? -1 \
56  : ((ion_decimal)->value.num_value->digits + 14) /* +14 is specified by decNumberToString. */ \
57  ) \
58  ))
59 
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 
65 /*
66  * Internal structure definitions. The internals should NOT be depended upon (or cared about) by the end user.
67  */
68 
72 typedef enum {
73  ION_DECIMAL_TYPE_UNKNOWN = 0,
86 
88 
89 struct _ion_decimal {
90 
91  ION_DECIMAL_TYPE type;
92 
93  union {
94  decQuad quad_value;
95  decNumber *num_value;
96  } value;
97 };
98 
99 
100 /* Memory management */
101 
108 ION_API_EXPORT iERR ion_decimal_zero(ION_DECIMAL *value);
109 
118 ION_API_EXPORT iERR ion_decimal_claim(ION_DECIMAL *value);
119 
125 ION_API_EXPORT iERR ion_decimal_free(ION_DECIMAL *value);
126 
127 
128 /* Conversions */
129 
136 ION_API_EXPORT iERR ion_decimal_to_string(const ION_DECIMAL *value, char *p_string);
137 
148 ION_API_EXPORT iERR ion_decimal_from_string(ION_DECIMAL *value, const char *str, decContext *context);
149 
155 ION_API_EXPORT iERR ion_decimal_from_uint32(ION_DECIMAL *value, uint32_t num);
156 
162 ION_API_EXPORT iERR ion_decimal_from_int32(ION_DECIMAL *value, int32_t num);
163 
170 ION_API_EXPORT iERR ion_decimal_from_quad(ION_DECIMAL *value, decQuad *quad);
171 
181 ION_API_EXPORT iERR ion_decimal_from_number(ION_DECIMAL *value, decNumber *number);
182 
186 ION_API_EXPORT iERR ion_decimal_from_ion_int(ION_DECIMAL *value, decContext *context, ION_INT *p_int);
187 
193 ION_API_EXPORT iERR ion_decimal_to_ion_int(const ION_DECIMAL *value, decContext *context, ION_INT *p_int);
194 
195 ION_API_EXPORT iERR ion_decimal_to_int32(const ION_DECIMAL *value, decContext *context, int32_t *p_int);
196 ION_API_EXPORT iERR ion_decimal_to_uint32(const ION_DECIMAL *value, decContext *context, uint32_t *p_int);
197 
198 
199 /* Operator APIs (computational) */
200 
201 ION_API_EXPORT iERR ion_decimal_fma(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, const ION_DECIMAL *fhs, decContext *context);
202 ION_API_EXPORT iERR ion_decimal_add(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
203 ION_API_EXPORT iERR ion_decimal_and(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
204 ION_API_EXPORT iERR ion_decimal_divide(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
205 ION_API_EXPORT iERR ion_decimal_divide_integer(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
206 ION_API_EXPORT iERR ion_decimal_max(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
207 ION_API_EXPORT iERR ion_decimal_max_mag(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
208 ION_API_EXPORT iERR ion_decimal_min(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
209 ION_API_EXPORT iERR ion_decimal_min_mag(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
210 ION_API_EXPORT iERR ion_decimal_multiply(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
211 ION_API_EXPORT iERR ion_decimal_or(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
212 ION_API_EXPORT iERR ion_decimal_quantize(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
213 ION_API_EXPORT iERR ion_decimal_remainder(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
214 ION_API_EXPORT iERR ion_decimal_remainder_near(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
215 ION_API_EXPORT iERR ion_decimal_rotate(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
216 ION_API_EXPORT iERR ion_decimal_scaleb(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
217 ION_API_EXPORT iERR ion_decimal_shift(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
218 ION_API_EXPORT iERR ion_decimal_subtract(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
219 ION_API_EXPORT iERR ion_decimal_xor(ION_DECIMAL *value, const ION_DECIMAL *lhs, const ION_DECIMAL *rhs, decContext *context);
220 ION_API_EXPORT iERR ion_decimal_abs(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
221 ION_API_EXPORT iERR ion_decimal_invert(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
222 ION_API_EXPORT iERR ion_decimal_logb(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
223 ION_API_EXPORT iERR ion_decimal_minus(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
224 ION_API_EXPORT iERR ion_decimal_plus(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
225 ION_API_EXPORT iERR ion_decimal_reduce(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
226 ION_API_EXPORT iERR ion_decimal_to_integral_exact(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
227 ION_API_EXPORT iERR ion_decimal_to_integral_value(ION_DECIMAL *value, const ION_DECIMAL *rhs, decContext *context);
228 
229 
230 /* Utility APIs (non-computational) */
231 
232 ION_API_EXPORT uint32_t ion_decimal_digits(const ION_DECIMAL *value);
233 ION_API_EXPORT int32_t ion_decimal_get_exponent(const ION_DECIMAL *value);
234 ION_API_EXPORT uint32_t ion_decimal_radix(const ION_DECIMAL *value);
235 ION_API_EXPORT uint32_t ion_decimal_same_quantum(const ION_DECIMAL *lhs, const ION_DECIMAL *rhs);
236 ION_API_EXPORT uint32_t ion_decimal_is_integer(const ION_DECIMAL *value);
237 ION_API_EXPORT uint32_t ion_decimal_is_subnormal(const ION_DECIMAL *value, decContext *context);
238 ION_API_EXPORT uint32_t ion_decimal_is_normal(const ION_DECIMAL *value, decContext *context);
239 ION_API_EXPORT uint32_t ion_decimal_is_finite(const ION_DECIMAL *value);
240 ION_API_EXPORT uint32_t ion_decimal_is_infinite(const ION_DECIMAL *value);
241 ION_API_EXPORT uint32_t ion_decimal_is_nan(const ION_DECIMAL *value);
242 ION_API_EXPORT uint32_t ion_decimal_is_negative(const ION_DECIMAL *value);
243 ION_API_EXPORT uint32_t ion_decimal_is_zero(const ION_DECIMAL *value);
244 ION_API_EXPORT uint32_t ion_decimal_is_canonical(const ION_DECIMAL *value);
245 
246 /* Comparisons */
247 
254 ION_API_EXPORT iERR ion_decimal_compare(const ION_DECIMAL *left, const ION_DECIMAL *right, decContext *context, int32_t *result);
255 
262 ION_API_EXPORT iERR ion_decimal_equals_quad(const decQuad *left, const decQuad *right, decContext *context, BOOL *is_equal);
263 
268 ION_API_EXPORT iERR ion_decimal_equals(const ION_DECIMAL *left, const ION_DECIMAL *right, decContext *context, BOOL *is_equal);
269 
270 /* Copies */
271 
272 ION_API_EXPORT iERR ion_decimal_canonical(ION_DECIMAL *value, const ION_DECIMAL *rhs);
273 ION_API_EXPORT iERR ion_decimal_copy(ION_DECIMAL *value, const ION_DECIMAL *rhs);
274 ION_API_EXPORT iERR ion_decimal_copy_abs(ION_DECIMAL *value, const ION_DECIMAL *rhs);
275 ION_API_EXPORT iERR ion_decimal_copy_negate(ION_DECIMAL *value, const ION_DECIMAL *rhs);
276 ION_API_EXPORT iERR ion_decimal_copy_sign(ION_DECIMAL *value, const ION_DECIMAL *rhs, const ION_DECIMAL *lhs, decContext *context);
277 
278 #ifdef __cplusplus
279 }
280 #endif
281 
282 #endif /* ION_DECIMAL_H_ */
ION_API_EXPORT iERR ion_decimal_from_quad(ION_DECIMAL *value, decQuad *quad)
ION_API_EXPORT iERR ion_decimal_equals_quad(const decQuad *left, const decQuad *right, decContext *context, BOOL *is_equal)
ION_API_EXPORT iERR ion_decimal_from_number(ION_DECIMAL *value, decNumber *number)
ION_API_EXPORT iERR ion_decimal_free(ION_DECIMAL *value)
ION_API_EXPORT iERR ion_decimal_to_ion_int(const ION_DECIMAL *value, decContext *context, ION_INT *p_int)
ION_DECIMAL_TYPE
Definition: ion_decimal.h:72
@ ION_DECIMAL_TYPE_QUAD
Definition: ion_decimal.h:77
@ ION_DECIMAL_TYPE_NUMBER
Definition: ion_decimal.h:81
@ ION_DECIMAL_TYPE_NUMBER_OWNED
Definition: ion_decimal.h:85
ION_API_EXPORT iERR ion_decimal_zero(ION_DECIMAL *value)
ION_API_EXPORT iERR ion_decimal_from_ion_int(ION_DECIMAL *value, decContext *context, ION_INT *p_int)
ION_API_EXPORT iERR ion_decimal_to_string(const ION_DECIMAL *value, char *p_string)
ION_API_EXPORT iERR ion_decimal_compare(const ION_DECIMAL *left, const ION_DECIMAL *right, decContext *context, int32_t *result)
ION_API_EXPORT iERR ion_decimal_from_int32(ION_DECIMAL *value, int32_t num)
ION_API_EXPORT iERR ion_decimal_from_uint32(ION_DECIMAL *value, uint32_t num)
ION_API_EXPORT iERR ion_decimal_equals(const ION_DECIMAL *left, const ION_DECIMAL *right, decContext *context, BOOL *is_equal)
ION_API_EXPORT iERR ion_decimal_from_string(ION_DECIMAL *value, const char *str, decContext *context)
ION_API_EXPORT iERR ion_decimal_claim(ION_DECIMAL *value)
Definition: ion_decimal.h:89
Definition: ion_int.h:100