blob: e09a431c79b35880645ed88ad4efb21384e774e2 [file] [log] [blame]
Gavin Howard3eb626f2018-02-14 13:54:35 -07001/*
Gavin Howardb5904bf2018-02-20 13:28:18 -07002 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -07003 *
Gavin Howardb5904bf2018-02-20 13:28:18 -07004 * Copyright 2018 Gavin D. Howard
Gavin Howard3eb626f2018-02-14 13:54:35 -07005 *
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 Howardb5904bf2018-02-20 13:28:18 -070017 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -070018 *
19 * Definitions for the num type.
20 *
21 */
22
23#ifndef BC_NUM_H
24#define BC_NUM_H
25
Gavin Howard3eb626f2018-02-14 13:54:35 -070026#include <stdbool.h>
Gavin Howard411f7322018-09-26 17:21:19 -060027#include <stddef.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070028#include <stdio.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070029
Gavin Howard29493062018-03-20 19:57:37 -060030#include <status.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070031
Gavin Howard021150b2018-03-10 15:40:42 -070032typedef signed char BcDigit;
33
Gavin Howardb11bc8a2018-03-01 17:23:00 -070034typedef struct BcNum {
35
Gavin Howard021150b2018-03-10 15:40:42 -070036 BcDigit *num;
Gavin Howardb11bc8a2018-03-01 17:23:00 -070037 size_t rdx;
38 size_t len;
39 size_t cap;
40 bool neg;
41
42} BcNum;
43
Gavin Howarddb319982018-09-21 10:27:21 -060044#define BC_NUM_MIN_BASE ((unsigned long) 2)
Gavin Howard1dc4f092018-09-21 10:46:52 -060045#define BC_NUM_MAX_IBASE ((unsigned long) 16)
Gavin Howard3eb626f2018-02-14 13:54:35 -070046#define BC_NUM_DEF_SIZE (16)
Gavin Howard5ce40842018-03-28 14:55:41 -060047#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard903a1e22018-02-15 16:32:01 -070048
Gavin Howardb11bc8a2018-03-01 17:23:00 -070049#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
Gavin Howard00759132018-08-27 15:37:54 -060050#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
Gavin Howard52e01262018-08-29 12:03:15 -060051#define BC_NUM_AREQ(a, b) \
Gavin Howard63738202018-09-26 15:34:20 -060052 (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
Gavin Howard52e01262018-08-29 12:03:15 -060053#define BC_NUM_MREQ(a, b, scale) \
Gavin Howard63738202018-09-26 15:34:20 -060054 (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
Gavin Howard00759132018-08-27 15:37:54 -060055
Gavin Howard6e0f3c52018-08-27 17:28:22 -060056typedef BcStatus (*BcNumBinaryOp)(BcNum*, BcNum*, BcNum*, size_t);
57typedef BcStatus (*BcNumDigitOp)(size_t, size_t, bool, size_t*, size_t);
Gavin Howard2682a1f2018-03-03 09:09:13 -070058
Gavin Howard8d1f1db2018-02-23 11:29:41 -070059BcStatus bc_num_init(BcNum *n, size_t request);
Gavin Howard8d1f1db2018-02-23 11:29:41 -070060BcStatus bc_num_expand(BcNum *n, size_t request);
Gavin Howard902a16c2018-05-16 01:22:53 -060061BcStatus bc_num_copy(BcNum *d, BcNum *s);
Gavin Howarded392aa2018-02-27 13:09:26 -070062void bc_num_free(void *num);
Gavin Howardb5c77212018-02-14 17:12:34 -070063
Gavin Howard8d1f1db2018-02-23 11:29:41 -070064BcStatus bc_num_ulong(BcNum *n, unsigned long *result);
Gavin Howard8d1f1db2018-02-23 11:29:41 -070065BcStatus bc_num_ulong2num(BcNum *n, unsigned long val);
Gavin Howard8e2cc692018-02-15 17:39:14 -070066
Gavin Howard0be26ed2018-08-31 20:21:56 -060067ssize_t bc_num_cmp(BcNum *a, BcNum *b);
68
Gavin Howard4538c902018-08-29 14:31:40 -060069BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *res, size_t scale);
70BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *res, size_t scale);
71BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *res, size_t scale);
72BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *res, size_t scale);
73BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *res, size_t scale);
74BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *res, size_t scale);
Gavin Howard3eb626f2018-02-14 13:54:35 -070075
Gavin Howardd75aaec2018-03-10 20:33:39 -070076// ** Exclude start. **
Gavin Howard4538c902018-08-29 14:31:40 -060077BcStatus bc_num_sqrt(BcNum *a, BcNum *res, size_t scale);
Gavin Howard3eb626f2018-02-14 13:54:35 -070078
Gavin Howard43a027f2018-02-26 13:27:10 -070079void bc_num_zero(BcNum *n);
80void bc_num_one(BcNum *n);
Gavin Howard5d74e962018-02-26 13:44:13 -070081void bc_num_ten(BcNum *n);
Gavin Howard43a027f2018-02-26 13:27:10 -070082
Gavin Howardd75aaec2018-03-10 20:33:39 -070083BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t);
Gavin Howard0011a3a2018-03-29 23:11:32 -060084BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline,
85 size_t *nchars, size_t line_len);
Gavin Howardd75aaec2018-03-10 20:33:39 -070086// ** Exclude end. **
87
Gavin Howardf456d372018-03-10 20:11:41 -070088extern const char bc_num_hex_digits[];
89
Gavin Howard3eb626f2018-02-14 13:54:35 -070090#endif // BC_NUM_H