Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1 | /* |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 2 | * ***************************************************************************** |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 3 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 4 | * Copyright 2018 Gavin D. Howard |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 5 | * |
| 6 | * Permission to use, copy, modify, and/or distribute this software for any |
| 7 | * purpose with or without fee is hereby granted. |
| 8 | * |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 10 | * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 | * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 14 | * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 | * PERFORMANCE OF THIS SOFTWARE. |
| 16 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 17 | * ***************************************************************************** |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 18 | * |
| 19 | * Definitions for the num type. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #ifndef BC_NUM_H |
| 24 | #define BC_NUM_H |
| 25 | |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 26 | #include <stdbool.h> |
Gavin Howard | 411f732 | 2018-09-26 17:21:19 -0600 | [diff] [blame] | 27 | #include <stddef.h> |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 28 | #include <stdio.h> |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 29 | |
Gavin Howard | 2949306 | 2018-03-20 19:57:37 -0600 | [diff] [blame] | 30 | #include <status.h> |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 31 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 32 | typedef signed char BcDigit; |
| 33 | |
Gavin Howard | b11bc8a | 2018-03-01 17:23:00 -0700 | [diff] [blame] | 34 | typedef struct BcNum { |
| 35 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 36 | BcDigit *num; |
Gavin Howard | b11bc8a | 2018-03-01 17:23:00 -0700 | [diff] [blame] | 37 | size_t rdx; |
| 38 | size_t len; |
| 39 | size_t cap; |
| 40 | bool neg; |
| 41 | |
| 42 | } BcNum; |
| 43 | |
Gavin Howard | db31998 | 2018-09-21 10:27:21 -0600 | [diff] [blame] | 44 | #define BC_NUM_MIN_BASE ((unsigned long) 2) |
Gavin Howard | 1dc4f09 | 2018-09-21 10:46:52 -0600 | [diff] [blame] | 45 | #define BC_NUM_MAX_IBASE ((unsigned long) 16) |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 46 | #define BC_NUM_DEF_SIZE (16) |
Gavin Howard | 5ce4084 | 2018-03-28 14:55:41 -0600 | [diff] [blame] | 47 | #define BC_NUM_PRINT_WIDTH (69) |
Gavin Howard | 903a1e2 | 2018-02-15 16:32:01 -0700 | [diff] [blame] | 48 | |
Gavin Howard | b11bc8a | 2018-03-01 17:23:00 -0700 | [diff] [blame] | 49 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
Gavin Howard | 0075913 | 2018-08-27 15:37:54 -0600 | [diff] [blame] | 50 | #define BC_NUM_INT(n) ((n)->len - (n)->rdx) |
Gavin Howard | 52e0126 | 2018-08-29 12:03:15 -0600 | [diff] [blame] | 51 | #define BC_NUM_AREQ(a, b) \ |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 52 | (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1) |
Gavin Howard | 52e0126 | 2018-08-29 12:03:15 -0600 | [diff] [blame] | 53 | #define BC_NUM_MREQ(a, b, scale) \ |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 54 | (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1) |
Gavin Howard | 0075913 | 2018-08-27 15:37:54 -0600 | [diff] [blame] | 55 | |
Gavin Howard | 6e0f3c5 | 2018-08-27 17:28:22 -0600 | [diff] [blame] | 56 | typedef BcStatus (*BcNumBinaryOp)(BcNum*, BcNum*, BcNum*, size_t); |
| 57 | typedef BcStatus (*BcNumDigitOp)(size_t, size_t, bool, size_t*, size_t); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 58 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 59 | BcStatus bc_num_init(BcNum *n, size_t request); |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 60 | BcStatus bc_num_expand(BcNum *n, size_t request); |
Gavin Howard | 902a16c | 2018-05-16 01:22:53 -0600 | [diff] [blame] | 61 | BcStatus bc_num_copy(BcNum *d, BcNum *s); |
Gavin Howard | ed392aa | 2018-02-27 13:09:26 -0700 | [diff] [blame] | 62 | void bc_num_free(void *num); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 63 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 64 | BcStatus bc_num_ulong(BcNum *n, unsigned long *result); |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 65 | BcStatus bc_num_ulong2num(BcNum *n, unsigned long val); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 66 | |
Gavin Howard | 0be26ed | 2018-08-31 20:21:56 -0600 | [diff] [blame] | 67 | ssize_t bc_num_cmp(BcNum *a, BcNum *b); |
| 68 | |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 69 | BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *res, size_t scale); |
| 70 | BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *res, size_t scale); |
| 71 | BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *res, size_t scale); |
| 72 | BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *res, size_t scale); |
| 73 | BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *res, size_t scale); |
| 74 | BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *res, size_t scale); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 75 | |
Gavin Howard | d75aaec | 2018-03-10 20:33:39 -0700 | [diff] [blame] | 76 | // ** Exclude start. ** |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 77 | BcStatus bc_num_sqrt(BcNum *a, BcNum *res, size_t scale); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 78 | |
Gavin Howard | 43a027f | 2018-02-26 13:27:10 -0700 | [diff] [blame] | 79 | void bc_num_zero(BcNum *n); |
| 80 | void bc_num_one(BcNum *n); |
Gavin Howard | 5d74e96 | 2018-02-26 13:44:13 -0700 | [diff] [blame] | 81 | void bc_num_ten(BcNum *n); |
Gavin Howard | 43a027f | 2018-02-26 13:27:10 -0700 | [diff] [blame] | 82 | |
Gavin Howard | d75aaec | 2018-03-10 20:33:39 -0700 | [diff] [blame] | 83 | BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t); |
Gavin Howard | 0011a3a | 2018-03-29 23:11:32 -0600 | [diff] [blame] | 84 | BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline, |
| 85 | size_t *nchars, size_t line_len); |
Gavin Howard | d75aaec | 2018-03-10 20:33:39 -0700 | [diff] [blame] | 86 | // ** Exclude end. ** |
| 87 | |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 88 | extern const char bc_num_hex_digits[]; |
| 89 | |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 90 | #endif // BC_NUM_H |