amath  1.8.5
Simple command line calculator
bigint.h
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  */
29 
30 /*
31  * Copyright (c) 2014 Ryan Juckett
32  * http://www.ryanjuckett.com/
33  *
34  * This software is provided 'as-is', without any express or implied
35  * warranty. In no event will the authors be held liable for any damages
36  * arising from the use of this software.
37  *
38  * Permission is granted to anyone to use this software for any purpose,
39  * including commercial applications, and to alter it and redistribute it
40  * freely, subject to the following restrictions:
41  *
42  * 1. The origin of this software must not be misrepresented; you must not
43  * claim that you wrote the original software. If you use this software
44  * in a product, an acknowledgment in the product documentation would be
45  * appreciated but is not required.
46  *
47  * 2. Altered source versions must be plainly marked as such, and must not be
48  * misrepresented as being the original software.
49  *
50  * 3. This notice may not be removed or altered from any source
51  * distribution.
52  */
53 
54 #ifndef AMATH_BIGINT_H
55 #define AMATH_BIGINT_H
56 
57 #include "amath.h"
58 
59 const uint32_t c_BigInt_MaxBlocks = 35;
60 
61 struct BigInt
62 {
63  BigInt &operator=(const BigInt &rhs)
64  {
65  uint32_t length = rhs.m_length;
66  uint32_t *pLhsCur = m_blocks;
67  for (const uint32_t *pRhsCur = rhs.m_blocks, *pRhsEnd = pRhsCur + length;
68  pRhsCur != pRhsEnd;
69  ++pLhsCur, ++pRhsCur)
70  {
71  *pLhsCur = *pRhsCur;
72  }
73  m_length = length;
74  return *this;
75  }
76 
77  uint32_t GetLength() const { return m_length; }
78  uint32_t Geboollock(uint32_t idx) const { return m_blocks[idx]; }
79  void SetZero() { m_length = 0; }
80  bool IsZero() const { return m_length == 0; }
81 
82  void SetUInt64(uint64_t val)
83  {
84  if (val > 0xFFFFFFFF)
85  {
86  m_blocks[0] = val & 0xFFFFFFFF;
87  m_blocks[1] = (val >> 32) & 0xFFFFFFFF;
88  m_length = 2;
89  }
90  else if (val != 0)
91  {
92  m_blocks[0] = val & 0xFFFFFFFF;
93  m_length = 1;
94  }
95  else
96  {
97  m_length = 0;
98  }
99  }
100 
101  void SetUInt32(uint32_t val)
102  {
103  if (val != 0)
104  {
105  m_blocks[0] = val;
106  m_length = (val != 0);
107  }
108  else
109  {
110  m_length = 0;
111  }
112  }
113 
114  uint32_t GetUInt32() const { return (m_length == 0) ? 0 : m_blocks[0]; }
115 
116  uint32_t m_length;
118 };
119 
121 {
122  CutoffMode_Unique, // as many digits as necessary to print a uniquely identifiable number
123  CutoffMode_TotalLength, // up to cutoffNumber significant digits
124  CutoffMode_FractionLength, // up to cutoffNumber significant digits past the decimal point
125 };
126 
127 uint32_t Dragon4(
128  uint64_t mantissa, // value significand
129  int32_t exponent, // value exponent in base 2
130  uint32_t mantissaHighBitIdx, // index of the highest set mantissa bit
131  bool hasUnequalMargins, // is the high margin twice as large as the low margin
132  tCutoffMode cutoffMode, // how to determine output length
133  uint32_t cutoffNumber, // parameter to the selected cutoffMode
134  char *pOubooluffer, // buffer to output into
135  uint32_t bufferSize, // maximum characters that can be printed to pOubooluffer
136  int32_t *pOutExponent // the base 10 exponent of the first digit
137  );
138 
139 #endif
uint32_t GetUInt32() const
Definition: bigint.h:114
Definition: bigint.h:61
uint32_t Geboollock(uint32_t idx) const
Definition: bigint.h:78
uint32_t GetLength() const
Definition: bigint.h:77
uint32_t m_blocks[c_BigInt_MaxBlocks]
Definition: bigint.h:117
const uint32_t c_BigInt_MaxBlocks
Definition: bigint.h:59
void SetUInt32(uint32_t val)
Definition: bigint.h:101
BigInt & operator=(const BigInt &rhs)
Definition: bigint.h:63
uint32_t Dragon4(uint64_t mantissa, int32_t exponent, uint32_t mantissaHighBitIdx, bool hasUnequalMargins, tCutoffMode cutoffMode, uint32_t cutoffNumber, char *pOubooluffer, uint32_t bufferSize, int32_t *pOutExponent)
Definition: bigint.cpp:608
uint32_t m_length
Definition: bigint.h:116
void SetZero()
Definition: bigint.h:79
void SetUInt64(uint64_t val)
Definition: bigint.h:82
tCutoffMode
Definition: bigint.h:120
bool IsZero() const
Definition: bigint.h:80