summaryrefslogtreecommitdiffstats
path: root/sys/boot/ficl/math64.c
blob: e83000a8b4a2c7e6b279d70c913d4fd3d7b64925 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*******************************************************************
** m a t h 6 4 . c
** Forth Inspired Command Language - 64 bit math support routines
** Author: John Sadler (john_sadler@alum.mit.edu)
** Created: 25 January 1998
**
*******************************************************************/

#include "ficl.h"
#include "math64.h"


/**************************************************************************
                        m 6 4 A b s
** Returns the absolute value of an INT64
**************************************************************************/
INT64 m64Abs(INT64 x)
{
    if (m64IsNegative(x))
        x = m64Negate(x);

    return x;
}


/**************************************************************************
                        m 6 4 F l o o r e d D i v I
** 
** FROM THE FORTH ANS...
** Floored division is integer division in which the remainder carries
** the sign of the divisor or is zero, and the quotient is rounded to
** its arithmetic floor. Symmetric division is integer division in which
** the remainder carries the sign of the dividend or is zero and the
** quotient is the mathematical quotient rounded towards zero or
** truncated. Examples of each are shown in tables 3.3 and 3.4. 
** 
** Table 3.3 - Floored Division Example
** Dividend        Divisor Remainder       Quotient
** --------        ------- ---------       --------
**  10                7       3                1
** -10                7       4               -2
**  10               -7      -4               -2
** -10               -7      -3                1
** 
** 
** Table 3.4 - Symmetric Division Example
** Dividend        Divisor Remainder       Quotient
** --------        ------- ---------       --------
**  10                7       3                1
** -10                7      -3               -1
**  10               -7       3               -1
** -10               -7      -3                1
**************************************************************************/
INTQR m64FlooredDivI(INT64 num, INT32 den)
{
    INTQR qr;
    UNSQR uqr;
    int signRem = 1;
    int signQuot = 1;

    if (m64IsNegative(num))
    {
        num = m64Negate(num);
        signQuot = -signQuot;
    }

    if (den < 0)
    {
        den      = -den;
        signRem  = -signRem;
        signQuot = -signQuot;
    }

    uqr = ficlLongDiv(m64CastIU(num), (UNS32)den);
    qr = m64CastQRUI(uqr);
    if (signQuot < 0)
    {
        qr.quot = -qr.quot;
        if (qr.rem != 0)
        {
            qr.quot--;
            qr.rem = den - qr.rem;
        }
    }

    if (signRem < 0)
        qr.rem = -qr.rem;

    return qr;
}


/**************************************************************************
                        m 6 4 I s N e g a t i v e
** Returns TRUE if the specified INT64 has its sign bit set.
**************************************************************************/
int m64IsNegative(INT64 x)
{
    return (x.hi < 0);
}


/**************************************************************************
                        m 6 4 M a c
** Mixed precision multiply and accumulate primitive for number building.
** Multiplies UNS64 u by UNS32 mul and adds UNS32 add. Mul is typically
** the numeric base, and add represents a digit to be appended to the 
** growing number. 
** Returns the result of the operation
**************************************************************************/
UNS64 m64Mac(UNS64 u, UNS32 mul, UNS32 add)
{
    UNS64 resultLo = ficlLongMul(u.lo, mul);
    UNS64 resultHi = ficlLongMul(u.hi, mul);
    resultLo.hi += resultHi.lo;
    resultHi.lo = resultLo.lo + add;

    if (resultHi.lo < resultLo.lo)
        resultLo.hi++;

    resultLo.lo = resultHi.lo;

    return resultLo;
}


/**************************************************************************
                        m 6 4 M u l I
** Multiplies a pair of INT32s and returns an INT64 result.
**************************************************************************/
INT64 m64MulI(INT32 x, INT32 y)
{
    UNS64 prod;
    int sign = 1;

    if (x < 0)
    {
        sign = -sign;
        x = -x;
    }

    if (y < 0)
    {
        sign = -sign;
        y = -y;
    }

    prod = ficlLongMul(x, y);
    if (sign > 0)
        return m64CastUI(prod);
    else
        return m64Negate(m64CastUI(prod));
}


/**************************************************************************
                        m 6 4 N e g a t e
** Negates an INT64 by complementing and incrementing.
**************************************************************************/
INT64 m64Negate(INT64 x)
{
    x.hi = ~x.hi;
    x.lo = ~x.lo;
    x.lo ++;
    if (x.lo == 0)
        x.hi++;

    return x;
}


/**************************************************************************
                        m 6 4 P u s h
** Push an INT64 onto the specified stack in the order required
** by ANS Forth (most significant cell on top)
** These should probably be macros...
**************************************************************************/
void  i64Push(FICL_STACK *pStack, INT64 i64)
{
    stackPushINT32(pStack, i64.lo);
    stackPushINT32(pStack, i64.hi);
    return;
}

void  u64Push(FICL_STACK *pStack, UNS64 u64)
{
    stackPushINT32(pStack, u64.lo);
    stackPushINT32(pStack, u64.hi);
    return;
}


/**************************************************************************
                        m 6 4 P o p
** Pops an INT64 off the stack in the order required by ANS Forth
** (most significant cell on top)
** These should probably be macros...
**************************************************************************/
INT64 i64Pop(FICL_STACK *pStack)
{
    INT64 ret;
    ret.hi = stackPopINT32(pStack);
    ret.lo = stackPopINT32(pStack);
    return ret;
}

UNS64 u64Pop(FICL_STACK *pStack)
{
    UNS64 ret;
    ret.hi = stackPopINT32(pStack);
    ret.lo = stackPopINT32(pStack);
    return ret;
}


/**************************************************************************
                        m 6 4 S y m m e t r i c D i v
** Divide an INT64 by an INT32 and return an INT32 quotient and an INT32
** remainder. The absolute values of quotient and remainder are not
** affected by the signs of the numerator and denominator (the operation
** is symmetric on the number line)
**************************************************************************/
INTQR m64SymmetricDivI(INT64 num, INT32 den)
{
    INTQR qr;
    UNSQR uqr;
    int signRem = 1;
    int signQuot = 1;

    if (m64IsNegative(num))
    {
        num = m64Negate(num);
        signRem  = -signRem;
        signQuot = -signQuot;
    }

    if (den < 0)
    {
        den      = -den;
        signQuot = -signQuot;
    }

    uqr = ficlLongDiv(m64CastIU(num), (UNS32)den);
    qr = m64CastQRUI(uqr);
    if (signRem < 0)
        qr.rem = -qr.rem;

    if (signQuot < 0)
        qr.quot = -qr.quot;

    return qr;
}


/**************************************************************************
                        m 6 4 U M o d
** Divides an UNS64 by base (an UNS16) and returns an UNS16 remainder.
** Writes the quotient back to the original UNS64 as a side effect.
** This operation is typically used to convert an UNS64 to a text string
** in any base. See words.c:numberSignS, for example.
** Mechanics: performs 4 ficlLongDivs, each of which produces 16 bits
** of the quotient. C does not provide a way to divide an UNS32 by an
** UNS16 and get an UNS32 quotient (ldiv is closest, but it's signed,
** unfortunately), so I've used ficlLongDiv.
**************************************************************************/
UNS16 m64UMod(UNS64 *pUD, UNS16 base)
{
    UNS64 ud;
    UNSQR qr;
    UNS64 result;

    result.hi = result.lo = 0;

    ud.hi = 0;
    ud.lo = pUD->hi >> 16;
    qr = ficlLongDiv(ud, (UNS32)base);
    result.hi = qr.quot << 16;

    ud.lo = (qr.rem << 16) | (pUD->hi & 0x0000ffff);
    qr = ficlLongDiv(ud, (UNS32)base);
    result.hi |= qr.quot & 0x0000ffff;

    ud.lo = (qr.rem << 16) | (pUD->lo >> 16);
    qr = ficlLongDiv(ud, (UNS32)base);
    result.lo = qr.quot << 16;

    ud.lo = (qr.rem << 16) | (pUD->lo & 0x0000ffff);
    qr = ficlLongDiv(ud, (UNS32)base);
    result.lo |= qr.quot & 0x0000ffff;

    *pUD = result;

    return (UNS16)(qr.rem);
}


OpenPOWER on IntegriCloud