File: mapmutil.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 (475 lines) | stat: -rw-r--r-- 10,054 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475

/*
 *  M_APM  -  mapmutil.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 various utility functions needed by the
 *	library in addition to some basic user callable functions.
 */

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

static  UCHAR	*M_mul_div = NULL;
static  UCHAR   *M_mul_rem = NULL;

static  UCHAR   M_mul_div_10[100];
static	UCHAR   M_mul_rem_10[100];

static	int	M_util_firsttime = TRUE;
static	int     M_firsttime3 = TRUE;

static	M_APM	M_work_0_5;

static  const char    *M_init_error_msg = "\'m_apm_init\', Out of memory";

/****************************************************************************/
M_APM	m_apm_init()
{
	M_APM	atmp;

	if (M_firsttime3)
	{
		M_firsttime3 = FALSE;
		M_init_util_data();
		M_init_trig_globals();
	}

	if ((atmp = (M_APM)MAPM_MALLOC(sizeof(M_APM_struct))) == NULL)
	{
		/* fatal, this does not return */

		M_apm_log_error_msg(M_APM_FATAL, M_init_error_msg);
	}

	atmp->m_apm_id           = M_APM_IDENT;
	atmp->m_apm_malloclength = 80;
	atmp->m_apm_datalength   = 1;
	atmp->m_apm_refcount     = 1;           /* not for us, for MAPM C++ class */
	atmp->m_apm_exponent     = 0;
	atmp->m_apm_sign         = 0;

	if ((atmp->m_apm_data = (UCHAR *)MAPM_MALLOC(84)) == NULL)
	{
		/* fatal, this does not return */

		M_apm_log_error_msg(M_APM_FATAL, M_init_error_msg);
	}

	atmp->m_apm_data[0] = 0;
	return(atmp);
}
/****************************************************************************/
void	m_apm_free(M_APM atmp)
{
	if (atmp->m_apm_id == M_APM_IDENT)
	{
		atmp->m_apm_id = 0x0FFFFFF0L;
		MAPM_FREE(atmp->m_apm_data);
		MAPM_FREE(atmp);
	}
	else
	{
		M_apm_log_error_msg(M_APM_RETURN, "\'m_apm_free\', Invalid M_APM variable");
	}
}
/****************************************************************************/
void	M_free_all_util()
{
	if (M_util_firsttime == FALSE)
	{
		m_apm_free(M_work_0_5);
		M_util_firsttime = TRUE;
	}

	if (M_firsttime3 == FALSE)
	{
		MAPM_FREE(M_mul_div);
		MAPM_FREE(M_mul_rem);

		M_mul_div    = NULL;
		M_mul_rem    = NULL;
		M_firsttime3 = TRUE;
	}
}
/****************************************************************************/
/*
 *      just a dummy wrapper to keep some compilers from complaining
 */
int 	M_get_sizeof_int()
{
	return(sizeof(int));
}
/****************************************************************************/
void	M_init_util_data()
{
	int	k;
	UCHAR   ndiv, nrem;

	if (M_mul_div != NULL)
		return;

	M_mul_div = (UCHAR *)MAPM_MALLOC(10000 * sizeof(UCHAR));
	M_mul_rem = (UCHAR *)MAPM_MALLOC(10000 * sizeof(UCHAR));

	if (M_mul_div == NULL || M_mul_rem == NULL)
	{
		/* fatal, this does not return */

		M_apm_log_error_msg(M_APM_FATAL, "\'M_init_util_data\', Out of memory");
	}

	ndiv = 0;
	nrem = 0;

	for (k = 0; k < 100; k++)
	{
		M_mul_div_10[k] = ndiv;
		M_mul_rem_10[k] = nrem;

		if (++nrem == 10)
		{
			nrem = 0;
			ndiv++;
		}
	}

	ndiv = 0;
	nrem = 0;

	for (k = 0; k < 10000; k++)
	{
		M_mul_div[k] = ndiv;
		M_mul_rem[k] = nrem;

		if (++nrem == 100)
		{
			nrem = 0;
			ndiv++;
		}
	}
}
/****************************************************************************/
void	M_get_div_rem_addr(UCHAR **ndivp, UCHAR **nremp)
{
	*ndivp = M_mul_div;
	*nremp = M_mul_rem;
}
/****************************************************************************/
void	M_get_div_rem(int tbl_lookup, UCHAR *ndiv, UCHAR *nrem)
{
	*ndiv = M_mul_div[tbl_lookup];
	*nrem = M_mul_rem[tbl_lookup];
}
/****************************************************************************/
void	M_get_div_rem_10(int tbl_lookup, UCHAR *ndiv, UCHAR *nrem)
{
	*ndiv = M_mul_div_10[tbl_lookup];
	*nrem = M_mul_rem_10[tbl_lookup];
}
/****************************************************************************/
void	m_apm_round(M_APM btmp, int places, M_APM atmp)
{
	int	ii;

	if (M_util_firsttime)
	{
		M_util_firsttime = FALSE;

		M_work_0_5 = m_apm_init();
		m_apm_set_string(M_work_0_5, "5");
	}

	ii = places + 1;

	if (atmp->m_apm_datalength <= ii)
	{
		m_apm_copy(btmp, atmp);
		return;
	}

	M_work_0_5->m_apm_exponent = atmp->m_apm_exponent - ii;

	if (atmp->m_apm_sign > 0)
		m_apm_add(btmp, atmp, M_work_0_5);
	else
		m_apm_subtract(btmp, atmp, M_work_0_5);

	btmp->m_apm_datalength = ii;
	M_apm_normalize(btmp);
}
/****************************************************************************/
void	M_apm_normalize(M_APM atmp)
{
	int	i, index, datalength, exponent;
	UCHAR   *ucp, numdiv, numrem, numrem2;

	if (atmp->m_apm_sign == 0)
		return;

	datalength = atmp->m_apm_datalength;
	exponent   = atmp->m_apm_exponent;

	/* make sure trailing bytes/chars are 0                */
	/* the following function will adjust the 'datalength' */
	/* we want the original value and will fix it later    */

	M_apm_pad(atmp, (datalength + 3));

	while (TRUE)			/* remove lead-in '0' if any */
	{
		M_get_div_rem_10((int)atmp->m_apm_data[0], &numdiv, &numrem);

		if (numdiv >= 1)      /* number is normalized, done here */
			break;

		index = (datalength + 1) >> 1;

		if (numrem == 0)      /* both nibbles are 0, we can move full bytes */
		{
			i = 0;
			ucp = atmp->m_apm_data;

			while (TRUE)	 /* find out how many '00' bytes we can move */
			{
				if (*ucp != 0)
					break;

				ucp++;
				i++;
			}

			memmove(atmp->m_apm_data, ucp, (index + 1 - i));
			datalength -= 2 * i;
			exponent -= 2 * i;
		}
		else
		{
			for (i = 0; i < index; i++)
			{
				M_get_div_rem_10((int)atmp->m_apm_data[i + 1], &numdiv, &numrem2);
				atmp->m_apm_data[i] = 10 * numrem + numdiv;
				numrem = numrem2;
			}

			datalength--;
			exponent--;
		}
	}

	while (TRUE)			/* remove trailing '0' if any */
	{
		index = ((datalength + 1) >> 1) - 1;

		if ((datalength & 1) == 0)   /* back-up full bytes at a time if the */
		{
			/* current length is an even number    */
			ucp = atmp->m_apm_data + index;
			if (*ucp == 0)
			{
				while (TRUE)
				{
					datalength -= 2;
					index--;
					ucp--;

					if (*ucp != 0)
						break;
				}
			}
		}

		M_get_div_rem_10((int)atmp->m_apm_data[index], &numdiv, &numrem);

		if (numrem != 0)		/* last digit non-zero, all done */
			break;

		if ((datalength & 1) != 0)   /* if odd, then first char must be non-zero */
		{
			if (numdiv != 0)
				break;
		}

		if (datalength == 1)
		{
			atmp->m_apm_sign = 0;
			exponent = 0;
			break;
		}

		datalength--;
	}

	atmp->m_apm_datalength = datalength;
	atmp->m_apm_exponent   = exponent;
}
/****************************************************************************/
void	M_apm_scale(M_APM ctmp, int count)
{
	int	ii, numb, ct;
	UCHAR	*chp, numdiv, numdiv2, numrem;
	void	*vp;

	ct = count;

	ii = (ctmp->m_apm_datalength + ct + 1) >> 1;
	if (ii > ctmp->m_apm_malloclength)
	{
		if ((vp = MAPM_REALLOC(ctmp->m_apm_data, (ii + 32))) == NULL)
		{
			/* fatal, this does not return */

			M_apm_log_error_msg(M_APM_FATAL, "\'M_apm_scale\', Out of memory");
		}

		ctmp->m_apm_malloclength = ii + 28;
		ctmp->m_apm_data = (UCHAR *)vp;
	}

	if ((ct & 1) != 0)          /* move odd number first */
	{
		ct--;
		chp = ctmp->m_apm_data;
		ii  = ((ctmp->m_apm_datalength + 1) >> 1) - 1;

		if ((ctmp->m_apm_datalength & 1) == 0)
		{
			/*
			 *   original datalength is even:
			 *
			 *   uv  wx  yz   becomes  -->   0u  vw  xy  z0
			 */

			numdiv = 0;

			while (TRUE)
			{
				M_get_div_rem_10((int)chp[ii], &numdiv2, &numrem);

				chp[ii + 1] = 10 * numrem + numdiv;
				numdiv = numdiv2;

				if (ii == 0)
					break;

				ii--;
			}

			chp[0] = numdiv2;
		}
		else
		{
			/*
			 *   original datalength is odd:
			 *
			 *   uv  wx  y0   becomes  -->   0u  vw  xy
			 */

			M_get_div_rem_10((int)chp[ii], &numdiv2, &numrem);

			if (ii == 0)
			{
				chp[0] = numdiv2;
			}
			else
			{
				while (TRUE)
				{
					M_get_div_rem_10((int)chp[ii - 1], &numdiv, &numrem);

					chp[ii] = 10 * numrem + numdiv2;
					numdiv2 = numdiv;

					if (--ii == 0)
						break;
				}

				chp[0] = numdiv;
			}
		}

		ctmp->m_apm_exponent++;
		ctmp->m_apm_datalength++;
	}

	/* ct is even here */

	if (ct > 0)
	{
		numb = (ctmp->m_apm_datalength + 1) >> 1;
		ii   = ct >> 1;

		memmove((ctmp->m_apm_data + ii), ctmp->m_apm_data, numb);
		memset(ctmp->m_apm_data, 0, ii);

		ctmp->m_apm_datalength += ct;
		ctmp->m_apm_exponent += ct;
	}
}
/****************************************************************************/
void	M_apm_pad(M_APM ctmp, int new_length)
{
	int	num1, numb, ct;
	UCHAR	numdiv, numrem;
	void	*vp;

	ct = new_length;
	if (ctmp->m_apm_datalength >= ct)
		return;

	numb = (ct + 1) >> 1;
	if (numb > ctmp->m_apm_malloclength)
	{
		if ((vp = MAPM_REALLOC(ctmp->m_apm_data, (numb + 32))) == NULL)
		{
			/* fatal, this does not return */

			M_apm_log_error_msg(M_APM_FATAL, "\'M_apm_pad\', Out of memory");
		}

		ctmp->m_apm_malloclength = numb + 28;
		ctmp->m_apm_data = (UCHAR *)vp;
	}

	num1 = (ctmp->m_apm_datalength + 1) >> 1;

	if ((ctmp->m_apm_datalength & 1) != 0)
	{
		M_get_div_rem_10((int)ctmp->m_apm_data[num1 - 1], &numdiv, &numrem);
		ctmp->m_apm_data[num1 - 1] = 10 * numdiv;
	}

	memset((ctmp->m_apm_data + num1), 0, (numb - num1));
	ctmp->m_apm_datalength = ct;
}
/****************************************************************************/

/*
      debug_dsp(cc)
      M_APM cc;
      {
static char buffer[8192];

m_apm_to_string(buffer, -1, cc);
printf("(dsp func) = [%s]\n",buffer);

      }
*/