File: mapm_add.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (329 lines) | stat: -rw-r--r-- 6,167 bytes parent folder | download | duplicates (5)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329

/*
 *  M_APM  -  mapm_add.c
 *
 *  Copyright (C) 1999 - 2007   Michael C. Ring
 *
 *  Permission to use, copy, and distribute this software and its
 *  documentation for any purpose with or without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and
 *  that both that copyright notice and this permission notice appear
 *  in supporting documentation.
 *
 *  Permission to modify the software is granted. Permission to distribute
 *  the modified code is granted. Modifications are to be distributed by
 *  using the file 'license.txt' as a template to modify the file header.
 *  'license.txt' is available in the official MAPM distribution.
 *
 *  This software is provided "as is" without express or implied warranty.
 */

/*
 *
 *      This file contains the functions that implement the sin (5x)
 *	and cos (4x) multiple angle relations
 *
 */

#include "pgAdmin3.h"
#include "pgscript/utilities/mapm-lib/m_apm_lc.h"

static	M_APM	M_work1 = NULL;
static	M_APM	M_work2 = NULL;
static	int	M_add_firsttime = TRUE;

/****************************************************************************/
void	M_free_all_add()
{
	if (M_add_firsttime == FALSE)
	{
		m_apm_free(M_work1);
		m_apm_free(M_work2);
		M_add_firsttime = TRUE;
	}
}
/****************************************************************************/
void	m_apm_add(M_APM r, M_APM a, M_APM b)
{
	int	j, carry, sign, aexp, bexp, adigits, bdigits;

	if (M_add_firsttime)
	{
		M_add_firsttime = FALSE;
		M_work1 = m_apm_init();
		M_work2 = m_apm_init();
	}

	if (a->m_apm_sign == 0)
	{
		m_apm_copy(r, b);
		return;
	}

	if (b->m_apm_sign == 0)
	{
		m_apm_copy(r, a);
		return;
	}

	if (a->m_apm_sign == 1 && b->m_apm_sign == -1)
	{
		b->m_apm_sign = 1;
		m_apm_subtract(r, a, b);
		b->m_apm_sign = -1;
		return;
	}

	if (a->m_apm_sign == -1 && b->m_apm_sign == 1)
	{
		a->m_apm_sign = 1;
		m_apm_subtract(r, b, a);
		a->m_apm_sign = -1;
		return;
	}

	sign = a->m_apm_sign;         /* signs are the same, result will be same */

	aexp = a->m_apm_exponent;
	bexp = b->m_apm_exponent;

	m_apm_copy(M_work1, a);
	m_apm_copy(M_work2, b);

	/*
	 *  scale by at least 1 factor of 10 in case the MSB carrys
	 */

	if (aexp == bexp)
	{
		M_apm_scale(M_work1, 2);   /* shift 2 digits == 1 byte for efficiency */
		M_apm_scale(M_work2, 2);
	}
	else
	{
		if (aexp > bexp)
		{
			M_apm_scale(M_work1, 2);
			M_apm_scale(M_work2, (aexp + 2 - bexp));
		}
		else            /*  aexp < bexp  */
		{
			M_apm_scale(M_work2, 2);
			M_apm_scale(M_work1, (bexp + 2 - aexp));
		}
	}

	adigits = M_work1->m_apm_datalength;
	bdigits = M_work2->m_apm_datalength;

	if (adigits >= bdigits)
	{
		m_apm_copy(r, M_work1);
		j = (bdigits + 1) >> 1;
		carry = 0;

		while (TRUE)
		{
			j--;
			r->m_apm_data[j] += carry + M_work2->m_apm_data[j];

			if (r->m_apm_data[j] >= 100)
			{
				r->m_apm_data[j] -= 100;
				carry = 1;
			}
			else
				carry = 0;

			if (j == 0)
				break;
		}
	}
	else
	{
		m_apm_copy(r, M_work2);
		j = (adigits + 1) >> 1;
		carry = 0;

		while (TRUE)
		{
			j--;
			r->m_apm_data[j] += carry + M_work1->m_apm_data[j];

			if (r->m_apm_data[j] >= 100)
			{
				r->m_apm_data[j] -= 100;
				carry = 1;
			}
			else
				carry = 0;

			if (j == 0)
				break;
		}
	}

	r->m_apm_sign = sign;

	M_apm_normalize(r);
}
/****************************************************************************/
void	m_apm_subtract(M_APM r, M_APM a, M_APM b)
{
	int	itmp, j, flag, icompare, sign, aexp, bexp,
	    borrow, adigits, bdigits;

	if (M_add_firsttime)
	{
		M_add_firsttime = FALSE;
		M_work1 = m_apm_init();
		M_work2 = m_apm_init();
	}

	if (b->m_apm_sign == 0)
	{
		m_apm_copy(r, a);
		return;
	}

	if (a->m_apm_sign == 0)
	{
		m_apm_copy(r, b);
		r->m_apm_sign = -(r->m_apm_sign);
		return;
	}

	if (a->m_apm_sign == 1 && b->m_apm_sign == -1)
	{
		b->m_apm_sign = 1;
		m_apm_add(r, a, b);
		b->m_apm_sign = -1;
		return;
	}

	if (a->m_apm_sign == -1 && b->m_apm_sign == 1)
	{
		b->m_apm_sign = -1;
		m_apm_add(r, a, b);
		b->m_apm_sign = 1;
		return;
	}

	/* now, the signs are the same  */
	/* make a positive working copy */

	m_apm_absolute_value(M_work1, a);
	m_apm_absolute_value(M_work2, b);

	/* are they the same??  if so, the result is zero */

	if ((icompare = m_apm_compare(M_work1, M_work2)) == 0)
	{
		M_set_to_zero(r);
		return;
	}

	if (icompare == 1)             /*  |a| > |b|  (do A-B)  */
	{
		flag = TRUE;
		sign = a->m_apm_sign;
	}
	else                           /*  |b| > |a|  (do B-A)  */
	{
		flag = FALSE;
		sign = -(a->m_apm_sign);
	}

	aexp = M_work1->m_apm_exponent;
	bexp = M_work2->m_apm_exponent;

	if (aexp > bexp)
		M_apm_scale(M_work2, (aexp - bexp));

	if (aexp < bexp)
		M_apm_scale(M_work1, (bexp - aexp));

	adigits = M_work1->m_apm_datalength;
	bdigits = M_work2->m_apm_datalength;

	if (adigits > bdigits)
		M_apm_pad(M_work2, adigits);

	if (adigits < bdigits)
		M_apm_pad(M_work1, bdigits);

	if (flag)		/* perform A-B,  M_work1 - M_work2 */
	{
		m_apm_copy(r, M_work1);
		j = (r->m_apm_datalength + 1) >> 1;
		borrow = 0;

		while (TRUE)
		{
			j--;
			itmp = (int)r->m_apm_data[j] - ((int)M_work2->m_apm_data[j] + borrow);

			if (itmp >= 0)
			{
				r->m_apm_data[j] = (UCHAR)itmp;
				borrow = 0;
			}
			else
			{
				r->m_apm_data[j] = (UCHAR)(100 + itmp);
				borrow = 1;
			}

			if (j == 0)
				break;
		}
	}
	else   		/* perform B-A,  M_work2 - M_work1 */
	{
		m_apm_copy(r, M_work2);
		j = (r->m_apm_datalength + 1) >> 1;
		borrow = 0;

		while (TRUE)
		{
			j--;
			itmp = (int)r->m_apm_data[j] - ((int)M_work1->m_apm_data[j] + borrow);

			if (itmp >= 0)
			{
				r->m_apm_data[j] = (UCHAR)itmp;
				borrow = 0;
			}
			else
			{
				r->m_apm_data[j] = (UCHAR)(100 + itmp);
				borrow = 1;
			}

			if (j == 0)
				break;
		}
	}

	r->m_apm_sign = sign;

	M_apm_normalize(r);
}
/****************************************************************************/

#ifdef __cplusplus

MAPM operator+(const MAPM &a, const MAPM &b)
{
	MAPM ret;
	m_apm_add(ret.val(), a.cval(), b.cval());
	return ret;
}
MAPM operator-(const MAPM &a, const MAPM &b)
{
	MAPM ret;
	m_apm_subtract(ret.val(), a.cval(), b.cval());
	return ret;
}

#endif