1 /*-------------------------------------------------------------------------
2  *
3  * int8.c
4  *	  Internal 64-bit integer operations
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  *	  src/backend/utils/adt/int8.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15 
16 #include <ctype.h>
17 #include <limits.h>
18 #include <math.h>
19 
20 #include "funcapi.h"
21 #include "libpq/pqformat.h"
22 #include "utils/int8.h"
23 #include "utils/builtins.h"
24 
25 
26 #define MAXINT8LEN		25
27 
28 #define SAMESIGN(a,b)	(((a) < 0) == ((b) < 0))
29 
30 typedef struct
31 {
32 	int64		current;
33 	int64		finish;
34 	int64		step;
35 } generate_series_fctx;
36 
37 
38 /***********************************************************************
39  **
40  **		Routines for 64-bit integers.
41  **
42  ***********************************************************************/
43 
44 /*----------------------------------------------------------
45  * Formatting and conversion routines.
46  *---------------------------------------------------------*/
47 
48 /*
49  * scanint8 --- try to parse a string into an int8.
50  *
51  * If errorOK is false, ereport a useful error message if the string is bad.
52  * If errorOK is true, just return "false" for bad input.
53  */
54 bool
scanint8(const char * str,bool errorOK,int64 * result)55 scanint8(const char *str, bool errorOK, int64 *result)
56 {
57 	const char *ptr = str;
58 	int64		tmp = 0;
59 	int			sign = 1;
60 
61 	/*
62 	 * Do our own scan, rather than relying on sscanf which might be broken
63 	 * for long long.
64 	 */
65 
66 	/* skip leading spaces */
67 	while (*ptr && isspace((unsigned char) *ptr))
68 		ptr++;
69 
70 	/* handle sign */
71 	if (*ptr == '-')
72 	{
73 		ptr++;
74 
75 		/*
76 		 * Do an explicit check for INT64_MIN.  Ugly though this is, it's
77 		 * cleaner than trying to get the loop below to handle it portably.
78 		 */
79 		if (strncmp(ptr, "9223372036854775808", 19) == 0)
80 		{
81 			tmp = PG_INT64_MIN;
82 			ptr += 19;
83 			goto gotdigits;
84 		}
85 		sign = -1;
86 	}
87 	else if (*ptr == '+')
88 		ptr++;
89 
90 	/* require at least one digit */
91 	if (!isdigit((unsigned char) *ptr))
92 	{
93 		if (errorOK)
94 			return false;
95 		else
96 			ereport(ERROR,
97 					(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
98 					 errmsg("invalid input syntax for integer: \"%s\"",
99 							str)));
100 	}
101 
102 	/* process digits */
103 	while (*ptr && isdigit((unsigned char) *ptr))
104 	{
105 		int64		newtmp = tmp * 10 + (*ptr++ - '0');
106 
107 		if ((newtmp / 10) != tmp)	/* overflow? */
108 		{
109 			if (errorOK)
110 				return false;
111 			else
112 				ereport(ERROR,
113 						(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
114 						 errmsg("value \"%s\" is out of range for type %s",
115 								str, "bigint")));
116 		}
117 		tmp = newtmp;
118 	}
119 
120 gotdigits:
121 
122 	/* allow trailing whitespace, but not other trailing chars */
123 	while (*ptr != '\0' && isspace((unsigned char) *ptr))
124 		ptr++;
125 
126 	if (*ptr != '\0')
127 	{
128 		if (errorOK)
129 			return false;
130 		else
131 			ereport(ERROR,
132 					(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
133 					 errmsg("invalid input syntax for integer: \"%s\"",
134 							str)));
135 	}
136 
137 	*result = (sign < 0) ? -tmp : tmp;
138 
139 	return true;
140 }
141 
142 /* int8in()
143  */
144 Datum
int8in(PG_FUNCTION_ARGS)145 int8in(PG_FUNCTION_ARGS)
146 {
147 	char	   *str = PG_GETARG_CSTRING(0);
148 	int64		result;
149 
150 	(void) scanint8(str, false, &result);
151 	PG_RETURN_INT64(result);
152 }
153 
154 
155 /* int8out()
156  */
157 Datum
int8out(PG_FUNCTION_ARGS)158 int8out(PG_FUNCTION_ARGS)
159 {
160 	int64		val = PG_GETARG_INT64(0);
161 	char		buf[MAXINT8LEN + 1];
162 	char	   *result;
163 
164 	pg_lltoa(val, buf);
165 	result = pstrdup(buf);
166 	PG_RETURN_CSTRING(result);
167 }
168 
169 /*
170  *		int8recv			- converts external binary format to int8
171  */
172 Datum
int8recv(PG_FUNCTION_ARGS)173 int8recv(PG_FUNCTION_ARGS)
174 {
175 	StringInfo	buf = (StringInfo) PG_GETARG_POINTER(0);
176 
177 	PG_RETURN_INT64(pq_getmsgint64(buf));
178 }
179 
180 /*
181  *		int8send			- converts int8 to binary format
182  */
183 Datum
int8send(PG_FUNCTION_ARGS)184 int8send(PG_FUNCTION_ARGS)
185 {
186 	int64		arg1 = PG_GETARG_INT64(0);
187 	StringInfoData buf;
188 
189 	pq_begintypsend(&buf);
190 	pq_sendint64(&buf, arg1);
191 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
192 }
193 
194 
195 /*----------------------------------------------------------
196  *	Relational operators for int8s, including cross-data-type comparisons.
197  *---------------------------------------------------------*/
198 
199 /* int8relop()
200  * Is val1 relop val2?
201  */
202 Datum
int8eq(PG_FUNCTION_ARGS)203 int8eq(PG_FUNCTION_ARGS)
204 {
205 	int64		val1 = PG_GETARG_INT64(0);
206 	int64		val2 = PG_GETARG_INT64(1);
207 
208 	PG_RETURN_BOOL(val1 == val2);
209 }
210 
211 Datum
int8ne(PG_FUNCTION_ARGS)212 int8ne(PG_FUNCTION_ARGS)
213 {
214 	int64		val1 = PG_GETARG_INT64(0);
215 	int64		val2 = PG_GETARG_INT64(1);
216 
217 	PG_RETURN_BOOL(val1 != val2);
218 }
219 
220 Datum
int8lt(PG_FUNCTION_ARGS)221 int8lt(PG_FUNCTION_ARGS)
222 {
223 	int64		val1 = PG_GETARG_INT64(0);
224 	int64		val2 = PG_GETARG_INT64(1);
225 
226 	PG_RETURN_BOOL(val1 < val2);
227 }
228 
229 Datum
int8gt(PG_FUNCTION_ARGS)230 int8gt(PG_FUNCTION_ARGS)
231 {
232 	int64		val1 = PG_GETARG_INT64(0);
233 	int64		val2 = PG_GETARG_INT64(1);
234 
235 	PG_RETURN_BOOL(val1 > val2);
236 }
237 
238 Datum
int8le(PG_FUNCTION_ARGS)239 int8le(PG_FUNCTION_ARGS)
240 {
241 	int64		val1 = PG_GETARG_INT64(0);
242 	int64		val2 = PG_GETARG_INT64(1);
243 
244 	PG_RETURN_BOOL(val1 <= val2);
245 }
246 
247 Datum
int8ge(PG_FUNCTION_ARGS)248 int8ge(PG_FUNCTION_ARGS)
249 {
250 	int64		val1 = PG_GETARG_INT64(0);
251 	int64		val2 = PG_GETARG_INT64(1);
252 
253 	PG_RETURN_BOOL(val1 >= val2);
254 }
255 
256 /* int84relop()
257  * Is 64-bit val1 relop 32-bit val2?
258  */
259 Datum
int84eq(PG_FUNCTION_ARGS)260 int84eq(PG_FUNCTION_ARGS)
261 {
262 	int64		val1 = PG_GETARG_INT64(0);
263 	int32		val2 = PG_GETARG_INT32(1);
264 
265 	PG_RETURN_BOOL(val1 == val2);
266 }
267 
268 Datum
int84ne(PG_FUNCTION_ARGS)269 int84ne(PG_FUNCTION_ARGS)
270 {
271 	int64		val1 = PG_GETARG_INT64(0);
272 	int32		val2 = PG_GETARG_INT32(1);
273 
274 	PG_RETURN_BOOL(val1 != val2);
275 }
276 
277 Datum
int84lt(PG_FUNCTION_ARGS)278 int84lt(PG_FUNCTION_ARGS)
279 {
280 	int64		val1 = PG_GETARG_INT64(0);
281 	int32		val2 = PG_GETARG_INT32(1);
282 
283 	PG_RETURN_BOOL(val1 < val2);
284 }
285 
286 Datum
int84gt(PG_FUNCTION_ARGS)287 int84gt(PG_FUNCTION_ARGS)
288 {
289 	int64		val1 = PG_GETARG_INT64(0);
290 	int32		val2 = PG_GETARG_INT32(1);
291 
292 	PG_RETURN_BOOL(val1 > val2);
293 }
294 
295 Datum
int84le(PG_FUNCTION_ARGS)296 int84le(PG_FUNCTION_ARGS)
297 {
298 	int64		val1 = PG_GETARG_INT64(0);
299 	int32		val2 = PG_GETARG_INT32(1);
300 
301 	PG_RETURN_BOOL(val1 <= val2);
302 }
303 
304 Datum
int84ge(PG_FUNCTION_ARGS)305 int84ge(PG_FUNCTION_ARGS)
306 {
307 	int64		val1 = PG_GETARG_INT64(0);
308 	int32		val2 = PG_GETARG_INT32(1);
309 
310 	PG_RETURN_BOOL(val1 >= val2);
311 }
312 
313 /* int48relop()
314  * Is 32-bit val1 relop 64-bit val2?
315  */
316 Datum
int48eq(PG_FUNCTION_ARGS)317 int48eq(PG_FUNCTION_ARGS)
318 {
319 	int32		val1 = PG_GETARG_INT32(0);
320 	int64		val2 = PG_GETARG_INT64(1);
321 
322 	PG_RETURN_BOOL(val1 == val2);
323 }
324 
325 Datum
int48ne(PG_FUNCTION_ARGS)326 int48ne(PG_FUNCTION_ARGS)
327 {
328 	int32		val1 = PG_GETARG_INT32(0);
329 	int64		val2 = PG_GETARG_INT64(1);
330 
331 	PG_RETURN_BOOL(val1 != val2);
332 }
333 
334 Datum
int48lt(PG_FUNCTION_ARGS)335 int48lt(PG_FUNCTION_ARGS)
336 {
337 	int32		val1 = PG_GETARG_INT32(0);
338 	int64		val2 = PG_GETARG_INT64(1);
339 
340 	PG_RETURN_BOOL(val1 < val2);
341 }
342 
343 Datum
int48gt(PG_FUNCTION_ARGS)344 int48gt(PG_FUNCTION_ARGS)
345 {
346 	int32		val1 = PG_GETARG_INT32(0);
347 	int64		val2 = PG_GETARG_INT64(1);
348 
349 	PG_RETURN_BOOL(val1 > val2);
350 }
351 
352 Datum
int48le(PG_FUNCTION_ARGS)353 int48le(PG_FUNCTION_ARGS)
354 {
355 	int32		val1 = PG_GETARG_INT32(0);
356 	int64		val2 = PG_GETARG_INT64(1);
357 
358 	PG_RETURN_BOOL(val1 <= val2);
359 }
360 
361 Datum
int48ge(PG_FUNCTION_ARGS)362 int48ge(PG_FUNCTION_ARGS)
363 {
364 	int32		val1 = PG_GETARG_INT32(0);
365 	int64		val2 = PG_GETARG_INT64(1);
366 
367 	PG_RETURN_BOOL(val1 >= val2);
368 }
369 
370 /* int82relop()
371  * Is 64-bit val1 relop 16-bit val2?
372  */
373 Datum
int82eq(PG_FUNCTION_ARGS)374 int82eq(PG_FUNCTION_ARGS)
375 {
376 	int64		val1 = PG_GETARG_INT64(0);
377 	int16		val2 = PG_GETARG_INT16(1);
378 
379 	PG_RETURN_BOOL(val1 == val2);
380 }
381 
382 Datum
int82ne(PG_FUNCTION_ARGS)383 int82ne(PG_FUNCTION_ARGS)
384 {
385 	int64		val1 = PG_GETARG_INT64(0);
386 	int16		val2 = PG_GETARG_INT16(1);
387 
388 	PG_RETURN_BOOL(val1 != val2);
389 }
390 
391 Datum
int82lt(PG_FUNCTION_ARGS)392 int82lt(PG_FUNCTION_ARGS)
393 {
394 	int64		val1 = PG_GETARG_INT64(0);
395 	int16		val2 = PG_GETARG_INT16(1);
396 
397 	PG_RETURN_BOOL(val1 < val2);
398 }
399 
400 Datum
int82gt(PG_FUNCTION_ARGS)401 int82gt(PG_FUNCTION_ARGS)
402 {
403 	int64		val1 = PG_GETARG_INT64(0);
404 	int16		val2 = PG_GETARG_INT16(1);
405 
406 	PG_RETURN_BOOL(val1 > val2);
407 }
408 
409 Datum
int82le(PG_FUNCTION_ARGS)410 int82le(PG_FUNCTION_ARGS)
411 {
412 	int64		val1 = PG_GETARG_INT64(0);
413 	int16		val2 = PG_GETARG_INT16(1);
414 
415 	PG_RETURN_BOOL(val1 <= val2);
416 }
417 
418 Datum
int82ge(PG_FUNCTION_ARGS)419 int82ge(PG_FUNCTION_ARGS)
420 {
421 	int64		val1 = PG_GETARG_INT64(0);
422 	int16		val2 = PG_GETARG_INT16(1);
423 
424 	PG_RETURN_BOOL(val1 >= val2);
425 }
426 
427 /* int28relop()
428  * Is 16-bit val1 relop 64-bit val2?
429  */
430 Datum
int28eq(PG_FUNCTION_ARGS)431 int28eq(PG_FUNCTION_ARGS)
432 {
433 	int16		val1 = PG_GETARG_INT16(0);
434 	int64		val2 = PG_GETARG_INT64(1);
435 
436 	PG_RETURN_BOOL(val1 == val2);
437 }
438 
439 Datum
int28ne(PG_FUNCTION_ARGS)440 int28ne(PG_FUNCTION_ARGS)
441 {
442 	int16		val1 = PG_GETARG_INT16(0);
443 	int64		val2 = PG_GETARG_INT64(1);
444 
445 	PG_RETURN_BOOL(val1 != val2);
446 }
447 
448 Datum
int28lt(PG_FUNCTION_ARGS)449 int28lt(PG_FUNCTION_ARGS)
450 {
451 	int16		val1 = PG_GETARG_INT16(0);
452 	int64		val2 = PG_GETARG_INT64(1);
453 
454 	PG_RETURN_BOOL(val1 < val2);
455 }
456 
457 Datum
int28gt(PG_FUNCTION_ARGS)458 int28gt(PG_FUNCTION_ARGS)
459 {
460 	int16		val1 = PG_GETARG_INT16(0);
461 	int64		val2 = PG_GETARG_INT64(1);
462 
463 	PG_RETURN_BOOL(val1 > val2);
464 }
465 
466 Datum
int28le(PG_FUNCTION_ARGS)467 int28le(PG_FUNCTION_ARGS)
468 {
469 	int16		val1 = PG_GETARG_INT16(0);
470 	int64		val2 = PG_GETARG_INT64(1);
471 
472 	PG_RETURN_BOOL(val1 <= val2);
473 }
474 
475 Datum
int28ge(PG_FUNCTION_ARGS)476 int28ge(PG_FUNCTION_ARGS)
477 {
478 	int16		val1 = PG_GETARG_INT16(0);
479 	int64		val2 = PG_GETARG_INT64(1);
480 
481 	PG_RETURN_BOOL(val1 >= val2);
482 }
483 
484 
485 /*----------------------------------------------------------
486  *	Arithmetic operators on 64-bit integers.
487  *---------------------------------------------------------*/
488 
489 Datum
int8um(PG_FUNCTION_ARGS)490 int8um(PG_FUNCTION_ARGS)
491 {
492 	int64		arg = PG_GETARG_INT64(0);
493 	int64		result;
494 
495 	result = -arg;
496 	/* overflow check (needed for INT64_MIN) */
497 	if (arg != 0 && SAMESIGN(result, arg))
498 		ereport(ERROR,
499 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
500 				 errmsg("bigint out of range")));
501 	PG_RETURN_INT64(result);
502 }
503 
504 Datum
int8up(PG_FUNCTION_ARGS)505 int8up(PG_FUNCTION_ARGS)
506 {
507 	int64		arg = PG_GETARG_INT64(0);
508 
509 	PG_RETURN_INT64(arg);
510 }
511 
512 Datum
int8pl(PG_FUNCTION_ARGS)513 int8pl(PG_FUNCTION_ARGS)
514 {
515 	int64		arg1 = PG_GETARG_INT64(0);
516 	int64		arg2 = PG_GETARG_INT64(1);
517 	int64		result;
518 
519 	result = arg1 + arg2;
520 
521 	/*
522 	 * Overflow check.  If the inputs are of different signs then their sum
523 	 * cannot overflow.  If the inputs are of the same sign, their sum had
524 	 * better be that sign too.
525 	 */
526 	if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
527 		ereport(ERROR,
528 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
529 				 errmsg("bigint out of range")));
530 	PG_RETURN_INT64(result);
531 }
532 
533 Datum
int8mi(PG_FUNCTION_ARGS)534 int8mi(PG_FUNCTION_ARGS)
535 {
536 	int64		arg1 = PG_GETARG_INT64(0);
537 	int64		arg2 = PG_GETARG_INT64(1);
538 	int64		result;
539 
540 	result = arg1 - arg2;
541 
542 	/*
543 	 * Overflow check.  If the inputs are of the same sign then their
544 	 * difference cannot overflow.  If they are of different signs then the
545 	 * result should be of the same sign as the first input.
546 	 */
547 	if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
548 		ereport(ERROR,
549 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
550 				 errmsg("bigint out of range")));
551 	PG_RETURN_INT64(result);
552 }
553 
554 Datum
int8mul(PG_FUNCTION_ARGS)555 int8mul(PG_FUNCTION_ARGS)
556 {
557 	int64		arg1 = PG_GETARG_INT64(0);
558 	int64		arg2 = PG_GETARG_INT64(1);
559 	int64		result;
560 
561 	result = arg1 * arg2;
562 
563 	/*
564 	 * Overflow check.  We basically check to see if result / arg2 gives arg1
565 	 * again.  There are two cases where this fails: arg2 = 0 (which cannot
566 	 * overflow) and arg1 = INT64_MIN, arg2 = -1 (where the division itself
567 	 * will overflow and thus incorrectly match).
568 	 *
569 	 * Since the division is likely much more expensive than the actual
570 	 * multiplication, we'd like to skip it where possible.  The best bang for
571 	 * the buck seems to be to check whether both inputs are in the int32
572 	 * range; if so, no overflow is possible.
573 	 */
574 	if (arg1 != (int64) ((int32) arg1) || arg2 != (int64) ((int32) arg2))
575 	{
576 		if (arg2 != 0 &&
577 			((arg2 == -1 && arg1 < 0 && result < 0) ||
578 			 result / arg2 != arg1))
579 			ereport(ERROR,
580 					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
581 					 errmsg("bigint out of range")));
582 	}
583 	PG_RETURN_INT64(result);
584 }
585 
586 Datum
int8div(PG_FUNCTION_ARGS)587 int8div(PG_FUNCTION_ARGS)
588 {
589 	int64		arg1 = PG_GETARG_INT64(0);
590 	int64		arg2 = PG_GETARG_INT64(1);
591 	int64		result;
592 
593 	if (arg2 == 0)
594 	{
595 		ereport(ERROR,
596 				(errcode(ERRCODE_DIVISION_BY_ZERO),
597 				 errmsg("division by zero")));
598 		/* ensure compiler realizes we mustn't reach the division (gcc bug) */
599 		PG_RETURN_NULL();
600 	}
601 
602 	/*
603 	 * INT64_MIN / -1 is problematic, since the result can't be represented on
604 	 * a two's-complement machine.  Some machines produce INT64_MIN, some
605 	 * produce zero, some throw an exception.  We can dodge the problem by
606 	 * recognizing that division by -1 is the same as negation.
607 	 */
608 	if (arg2 == -1)
609 	{
610 		result = -arg1;
611 		/* overflow check (needed for INT64_MIN) */
612 		if (arg1 != 0 && SAMESIGN(result, arg1))
613 			ereport(ERROR,
614 					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
615 					 errmsg("bigint out of range")));
616 		PG_RETURN_INT64(result);
617 	}
618 
619 	/* No overflow is possible */
620 
621 	result = arg1 / arg2;
622 
623 	PG_RETURN_INT64(result);
624 }
625 
626 /* int8abs()
627  * Absolute value
628  */
629 Datum
int8abs(PG_FUNCTION_ARGS)630 int8abs(PG_FUNCTION_ARGS)
631 {
632 	int64		arg1 = PG_GETARG_INT64(0);
633 	int64		result;
634 
635 	result = (arg1 < 0) ? -arg1 : arg1;
636 	/* overflow check (needed for INT64_MIN) */
637 	if (result < 0)
638 		ereport(ERROR,
639 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
640 				 errmsg("bigint out of range")));
641 	PG_RETURN_INT64(result);
642 }
643 
644 /* int8mod()
645  * Modulo operation.
646  */
647 Datum
int8mod(PG_FUNCTION_ARGS)648 int8mod(PG_FUNCTION_ARGS)
649 {
650 	int64		arg1 = PG_GETARG_INT64(0);
651 	int64		arg2 = PG_GETARG_INT64(1);
652 
653 	if (arg2 == 0)
654 	{
655 		ereport(ERROR,
656 				(errcode(ERRCODE_DIVISION_BY_ZERO),
657 				 errmsg("division by zero")));
658 		/* ensure compiler realizes we mustn't reach the division (gcc bug) */
659 		PG_RETURN_NULL();
660 	}
661 
662 	/*
663 	 * Some machines throw a floating-point exception for INT64_MIN % -1,
664 	 * which is a bit silly since the correct answer is perfectly
665 	 * well-defined, namely zero.
666 	 */
667 	if (arg2 == -1)
668 		PG_RETURN_INT64(0);
669 
670 	/* No overflow is possible */
671 
672 	PG_RETURN_INT64(arg1 % arg2);
673 }
674 
675 
676 Datum
int8inc(PG_FUNCTION_ARGS)677 int8inc(PG_FUNCTION_ARGS)
678 {
679 	/*
680 	 * When int8 is pass-by-reference, we provide this special case to avoid
681 	 * palloc overhead for COUNT(): when called as an aggregate, we know that
682 	 * the argument is modifiable local storage, so just update it in-place.
683 	 * (If int8 is pass-by-value, then of course this is useless as well as
684 	 * incorrect, so just ifdef it out.)
685 	 */
686 #ifndef USE_FLOAT8_BYVAL		/* controls int8 too */
687 	if (AggCheckCallContext(fcinfo, NULL))
688 	{
689 		int64	   *arg = (int64 *) PG_GETARG_POINTER(0);
690 		int64		result;
691 
692 		result = *arg + 1;
693 		/* Overflow check */
694 		if (result < 0 && *arg > 0)
695 			ereport(ERROR,
696 					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
697 					 errmsg("bigint out of range")));
698 
699 		*arg = result;
700 		PG_RETURN_POINTER(arg);
701 	}
702 	else
703 #endif
704 	{
705 		/* Not called as an aggregate, so just do it the dumb way */
706 		int64		arg = PG_GETARG_INT64(0);
707 		int64		result;
708 
709 		result = arg + 1;
710 		/* Overflow check */
711 		if (result < 0 && arg > 0)
712 			ereport(ERROR,
713 					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
714 					 errmsg("bigint out of range")));
715 
716 		PG_RETURN_INT64(result);
717 	}
718 }
719 
720 Datum
int8dec(PG_FUNCTION_ARGS)721 int8dec(PG_FUNCTION_ARGS)
722 {
723 	/*
724 	 * When int8 is pass-by-reference, we provide this special case to avoid
725 	 * palloc overhead for COUNT(): when called as an aggregate, we know that
726 	 * the argument is modifiable local storage, so just update it in-place.
727 	 * (If int8 is pass-by-value, then of course this is useless as well as
728 	 * incorrect, so just ifdef it out.)
729 	 */
730 #ifndef USE_FLOAT8_BYVAL		/* controls int8 too */
731 	if (AggCheckCallContext(fcinfo, NULL))
732 	{
733 		int64	   *arg = (int64 *) PG_GETARG_POINTER(0);
734 		int64		result;
735 
736 		result = *arg - 1;
737 		/* Overflow check */
738 		if (result > 0 && *arg < 0)
739 			ereport(ERROR,
740 					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
741 					 errmsg("bigint out of range")));
742 
743 		*arg = result;
744 		PG_RETURN_POINTER(arg);
745 	}
746 	else
747 #endif
748 	{
749 		/* Not called as an aggregate, so just do it the dumb way */
750 		int64		arg = PG_GETARG_INT64(0);
751 		int64		result;
752 
753 		result = arg - 1;
754 		/* Overflow check */
755 		if (result > 0 && arg < 0)
756 			ereport(ERROR,
757 					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
758 					 errmsg("bigint out of range")));
759 
760 		PG_RETURN_INT64(result);
761 	}
762 }
763 
764 
765 /*
766  * These functions are exactly like int8inc/int8dec but are used for
767  * aggregates that count only non-null values.  Since the functions are
768  * declared strict, the null checks happen before we ever get here, and all we
769  * need do is increment the state value.  We could actually make these pg_proc
770  * entries point right at int8inc/int8dec, but then the opr_sanity regression
771  * test would complain about mismatched entries for a built-in function.
772  */
773 
774 Datum
int8inc_any(PG_FUNCTION_ARGS)775 int8inc_any(PG_FUNCTION_ARGS)
776 {
777 	return int8inc(fcinfo);
778 }
779 
780 Datum
int8inc_float8_float8(PG_FUNCTION_ARGS)781 int8inc_float8_float8(PG_FUNCTION_ARGS)
782 {
783 	return int8inc(fcinfo);
784 }
785 
786 Datum
int8dec_any(PG_FUNCTION_ARGS)787 int8dec_any(PG_FUNCTION_ARGS)
788 {
789 	return int8dec(fcinfo);
790 }
791 
792 
793 Datum
int8larger(PG_FUNCTION_ARGS)794 int8larger(PG_FUNCTION_ARGS)
795 {
796 	int64		arg1 = PG_GETARG_INT64(0);
797 	int64		arg2 = PG_GETARG_INT64(1);
798 	int64		result;
799 
800 	result = ((arg1 > arg2) ? arg1 : arg2);
801 
802 	PG_RETURN_INT64(result);
803 }
804 
805 Datum
int8smaller(PG_FUNCTION_ARGS)806 int8smaller(PG_FUNCTION_ARGS)
807 {
808 	int64		arg1 = PG_GETARG_INT64(0);
809 	int64		arg2 = PG_GETARG_INT64(1);
810 	int64		result;
811 
812 	result = ((arg1 < arg2) ? arg1 : arg2);
813 
814 	PG_RETURN_INT64(result);
815 }
816 
817 Datum
int84pl(PG_FUNCTION_ARGS)818 int84pl(PG_FUNCTION_ARGS)
819 {
820 	int64		arg1 = PG_GETARG_INT64(0);
821 	int32		arg2 = PG_GETARG_INT32(1);
822 	int64		result;
823 
824 	result = arg1 + arg2;
825 
826 	/*
827 	 * Overflow check.  If the inputs are of different signs then their sum
828 	 * cannot overflow.  If the inputs are of the same sign, their sum had
829 	 * better be that sign too.
830 	 */
831 	if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
832 		ereport(ERROR,
833 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
834 				 errmsg("bigint out of range")));
835 	PG_RETURN_INT64(result);
836 }
837 
838 Datum
int84mi(PG_FUNCTION_ARGS)839 int84mi(PG_FUNCTION_ARGS)
840 {
841 	int64		arg1 = PG_GETARG_INT64(0);
842 	int32		arg2 = PG_GETARG_INT32(1);
843 	int64		result;
844 
845 	result = arg1 - arg2;
846 
847 	/*
848 	 * Overflow check.  If the inputs are of the same sign then their
849 	 * difference cannot overflow.  If they are of different signs then the
850 	 * result should be of the same sign as the first input.
851 	 */
852 	if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
853 		ereport(ERROR,
854 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
855 				 errmsg("bigint out of range")));
856 	PG_RETURN_INT64(result);
857 }
858 
859 Datum
int84mul(PG_FUNCTION_ARGS)860 int84mul(PG_FUNCTION_ARGS)
861 {
862 	int64		arg1 = PG_GETARG_INT64(0);
863 	int32		arg2 = PG_GETARG_INT32(1);
864 	int64		result;
865 
866 	result = arg1 * arg2;
867 
868 	/*
869 	 * Overflow check.  We basically check to see if result / arg1 gives arg2
870 	 * again.  There is one case where this fails: arg1 = 0 (which cannot
871 	 * overflow).
872 	 *
873 	 * Since the division is likely much more expensive than the actual
874 	 * multiplication, we'd like to skip it where possible.  The best bang for
875 	 * the buck seems to be to check whether both inputs are in the int32
876 	 * range; if so, no overflow is possible.
877 	 */
878 	if (arg1 != (int64) ((int32) arg1) &&
879 		result / arg1 != arg2)
880 		ereport(ERROR,
881 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
882 				 errmsg("bigint out of range")));
883 	PG_RETURN_INT64(result);
884 }
885 
886 Datum
int84div(PG_FUNCTION_ARGS)887 int84div(PG_FUNCTION_ARGS)
888 {
889 	int64		arg1 = PG_GETARG_INT64(0);
890 	int32		arg2 = PG_GETARG_INT32(1);
891 	int64		result;
892 
893 	if (arg2 == 0)
894 	{
895 		ereport(ERROR,
896 				(errcode(ERRCODE_DIVISION_BY_ZERO),
897 				 errmsg("division by zero")));
898 		/* ensure compiler realizes we mustn't reach the division (gcc bug) */
899 		PG_RETURN_NULL();
900 	}
901 
902 	/*
903 	 * INT64_MIN / -1 is problematic, since the result can't be represented on
904 	 * a two's-complement machine.  Some machines produce INT64_MIN, some
905 	 * produce zero, some throw an exception.  We can dodge the problem by
906 	 * recognizing that division by -1 is the same as negation.
907 	 */
908 	if (arg2 == -1)
909 	{
910 		result = -arg1;
911 		/* overflow check (needed for INT64_MIN) */
912 		if (arg1 != 0 && SAMESIGN(result, arg1))
913 			ereport(ERROR,
914 					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
915 					 errmsg("bigint out of range")));
916 		PG_RETURN_INT64(result);
917 	}
918 
919 	/* No overflow is possible */
920 
921 	result = arg1 / arg2;
922 
923 	PG_RETURN_INT64(result);
924 }
925 
926 Datum
int48pl(PG_FUNCTION_ARGS)927 int48pl(PG_FUNCTION_ARGS)
928 {
929 	int32		arg1 = PG_GETARG_INT32(0);
930 	int64		arg2 = PG_GETARG_INT64(1);
931 	int64		result;
932 
933 	result = arg1 + arg2;
934 
935 	/*
936 	 * Overflow check.  If the inputs are of different signs then their sum
937 	 * cannot overflow.  If the inputs are of the same sign, their sum had
938 	 * better be that sign too.
939 	 */
940 	if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
941 		ereport(ERROR,
942 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
943 				 errmsg("bigint out of range")));
944 	PG_RETURN_INT64(result);
945 }
946 
947 Datum
int48mi(PG_FUNCTION_ARGS)948 int48mi(PG_FUNCTION_ARGS)
949 {
950 	int32		arg1 = PG_GETARG_INT32(0);
951 	int64		arg2 = PG_GETARG_INT64(1);
952 	int64		result;
953 
954 	result = arg1 - arg2;
955 
956 	/*
957 	 * Overflow check.  If the inputs are of the same sign then their
958 	 * difference cannot overflow.  If they are of different signs then the
959 	 * result should be of the same sign as the first input.
960 	 */
961 	if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
962 		ereport(ERROR,
963 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
964 				 errmsg("bigint out of range")));
965 	PG_RETURN_INT64(result);
966 }
967 
968 Datum
int48mul(PG_FUNCTION_ARGS)969 int48mul(PG_FUNCTION_ARGS)
970 {
971 	int32		arg1 = PG_GETARG_INT32(0);
972 	int64		arg2 = PG_GETARG_INT64(1);
973 	int64		result;
974 
975 	result = arg1 * arg2;
976 
977 	/*
978 	 * Overflow check.  We basically check to see if result / arg2 gives arg1
979 	 * again.  There is one case where this fails: arg2 = 0 (which cannot
980 	 * overflow).
981 	 *
982 	 * Since the division is likely much more expensive than the actual
983 	 * multiplication, we'd like to skip it where possible.  The best bang for
984 	 * the buck seems to be to check whether both inputs are in the int32
985 	 * range; if so, no overflow is possible.
986 	 */
987 	if (arg2 != (int64) ((int32) arg2) &&
988 		result / arg2 != arg1)
989 		ereport(ERROR,
990 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
991 				 errmsg("bigint out of range")));
992 	PG_RETURN_INT64(result);
993 }
994 
995 Datum
int48div(PG_FUNCTION_ARGS)996 int48div(PG_FUNCTION_ARGS)
997 {
998 	int32		arg1 = PG_GETARG_INT32(0);
999 	int64		arg2 = PG_GETARG_INT64(1);
1000 
1001 	if (arg2 == 0)
1002 	{
1003 		ereport(ERROR,
1004 				(errcode(ERRCODE_DIVISION_BY_ZERO),
1005 				 errmsg("division by zero")));
1006 		/* ensure compiler realizes we mustn't reach the division (gcc bug) */
1007 		PG_RETURN_NULL();
1008 	}
1009 
1010 	/* No overflow is possible */
1011 	PG_RETURN_INT64((int64) arg1 / arg2);
1012 }
1013 
1014 Datum
int82pl(PG_FUNCTION_ARGS)1015 int82pl(PG_FUNCTION_ARGS)
1016 {
1017 	int64		arg1 = PG_GETARG_INT64(0);
1018 	int16		arg2 = PG_GETARG_INT16(1);
1019 	int64		result;
1020 
1021 	result = arg1 + arg2;
1022 
1023 	/*
1024 	 * Overflow check.  If the inputs are of different signs then their sum
1025 	 * cannot overflow.  If the inputs are of the same sign, their sum had
1026 	 * better be that sign too.
1027 	 */
1028 	if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1029 		ereport(ERROR,
1030 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1031 				 errmsg("bigint out of range")));
1032 	PG_RETURN_INT64(result);
1033 }
1034 
1035 Datum
int82mi(PG_FUNCTION_ARGS)1036 int82mi(PG_FUNCTION_ARGS)
1037 {
1038 	int64		arg1 = PG_GETARG_INT64(0);
1039 	int16		arg2 = PG_GETARG_INT16(1);
1040 	int64		result;
1041 
1042 	result = arg1 - arg2;
1043 
1044 	/*
1045 	 * Overflow check.  If the inputs are of the same sign then their
1046 	 * difference cannot overflow.  If they are of different signs then the
1047 	 * result should be of the same sign as the first input.
1048 	 */
1049 	if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1050 		ereport(ERROR,
1051 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1052 				 errmsg("bigint out of range")));
1053 	PG_RETURN_INT64(result);
1054 }
1055 
1056 Datum
int82mul(PG_FUNCTION_ARGS)1057 int82mul(PG_FUNCTION_ARGS)
1058 {
1059 	int64		arg1 = PG_GETARG_INT64(0);
1060 	int16		arg2 = PG_GETARG_INT16(1);
1061 	int64		result;
1062 
1063 	result = arg1 * arg2;
1064 
1065 	/*
1066 	 * Overflow check.  We basically check to see if result / arg1 gives arg2
1067 	 * again.  There is one case where this fails: arg1 = 0 (which cannot
1068 	 * overflow).
1069 	 *
1070 	 * Since the division is likely much more expensive than the actual
1071 	 * multiplication, we'd like to skip it where possible.  The best bang for
1072 	 * the buck seems to be to check whether both inputs are in the int32
1073 	 * range; if so, no overflow is possible.
1074 	 */
1075 	if (arg1 != (int64) ((int32) arg1) &&
1076 		result / arg1 != arg2)
1077 		ereport(ERROR,
1078 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1079 				 errmsg("bigint out of range")));
1080 	PG_RETURN_INT64(result);
1081 }
1082 
1083 Datum
int82div(PG_FUNCTION_ARGS)1084 int82div(PG_FUNCTION_ARGS)
1085 {
1086 	int64		arg1 = PG_GETARG_INT64(0);
1087 	int16		arg2 = PG_GETARG_INT16(1);
1088 	int64		result;
1089 
1090 	if (arg2 == 0)
1091 	{
1092 		ereport(ERROR,
1093 				(errcode(ERRCODE_DIVISION_BY_ZERO),
1094 				 errmsg("division by zero")));
1095 		/* ensure compiler realizes we mustn't reach the division (gcc bug) */
1096 		PG_RETURN_NULL();
1097 	}
1098 
1099 	/*
1100 	 * INT64_MIN / -1 is problematic, since the result can't be represented on
1101 	 * a two's-complement machine.  Some machines produce INT64_MIN, some
1102 	 * produce zero, some throw an exception.  We can dodge the problem by
1103 	 * recognizing that division by -1 is the same as negation.
1104 	 */
1105 	if (arg2 == -1)
1106 	{
1107 		result = -arg1;
1108 		/* overflow check (needed for INT64_MIN) */
1109 		if (arg1 != 0 && SAMESIGN(result, arg1))
1110 			ereport(ERROR,
1111 					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1112 					 errmsg("bigint out of range")));
1113 		PG_RETURN_INT64(result);
1114 	}
1115 
1116 	/* No overflow is possible */
1117 
1118 	result = arg1 / arg2;
1119 
1120 	PG_RETURN_INT64(result);
1121 }
1122 
1123 Datum
int28pl(PG_FUNCTION_ARGS)1124 int28pl(PG_FUNCTION_ARGS)
1125 {
1126 	int16		arg1 = PG_GETARG_INT16(0);
1127 	int64		arg2 = PG_GETARG_INT64(1);
1128 	int64		result;
1129 
1130 	result = arg1 + arg2;
1131 
1132 	/*
1133 	 * Overflow check.  If the inputs are of different signs then their sum
1134 	 * cannot overflow.  If the inputs are of the same sign, their sum had
1135 	 * better be that sign too.
1136 	 */
1137 	if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1138 		ereport(ERROR,
1139 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1140 				 errmsg("bigint out of range")));
1141 	PG_RETURN_INT64(result);
1142 }
1143 
1144 Datum
int28mi(PG_FUNCTION_ARGS)1145 int28mi(PG_FUNCTION_ARGS)
1146 {
1147 	int16		arg1 = PG_GETARG_INT16(0);
1148 	int64		arg2 = PG_GETARG_INT64(1);
1149 	int64		result;
1150 
1151 	result = arg1 - arg2;
1152 
1153 	/*
1154 	 * Overflow check.  If the inputs are of the same sign then their
1155 	 * difference cannot overflow.  If they are of different signs then the
1156 	 * result should be of the same sign as the first input.
1157 	 */
1158 	if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1159 		ereport(ERROR,
1160 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1161 				 errmsg("bigint out of range")));
1162 	PG_RETURN_INT64(result);
1163 }
1164 
1165 Datum
int28mul(PG_FUNCTION_ARGS)1166 int28mul(PG_FUNCTION_ARGS)
1167 {
1168 	int16		arg1 = PG_GETARG_INT16(0);
1169 	int64		arg2 = PG_GETARG_INT64(1);
1170 	int64		result;
1171 
1172 	result = arg1 * arg2;
1173 
1174 	/*
1175 	 * Overflow check.  We basically check to see if result / arg2 gives arg1
1176 	 * again.  There is one case where this fails: arg2 = 0 (which cannot
1177 	 * overflow).
1178 	 *
1179 	 * Since the division is likely much more expensive than the actual
1180 	 * multiplication, we'd like to skip it where possible.  The best bang for
1181 	 * the buck seems to be to check whether both inputs are in the int32
1182 	 * range; if so, no overflow is possible.
1183 	 */
1184 	if (arg2 != (int64) ((int32) arg2) &&
1185 		result / arg2 != arg1)
1186 		ereport(ERROR,
1187 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1188 				 errmsg("bigint out of range")));
1189 	PG_RETURN_INT64(result);
1190 }
1191 
1192 Datum
int28div(PG_FUNCTION_ARGS)1193 int28div(PG_FUNCTION_ARGS)
1194 {
1195 	int16		arg1 = PG_GETARG_INT16(0);
1196 	int64		arg2 = PG_GETARG_INT64(1);
1197 
1198 	if (arg2 == 0)
1199 	{
1200 		ereport(ERROR,
1201 				(errcode(ERRCODE_DIVISION_BY_ZERO),
1202 				 errmsg("division by zero")));
1203 		/* ensure compiler realizes we mustn't reach the division (gcc bug) */
1204 		PG_RETURN_NULL();
1205 	}
1206 
1207 	/* No overflow is possible */
1208 	PG_RETURN_INT64((int64) arg1 / arg2);
1209 }
1210 
1211 /* Binary arithmetics
1212  *
1213  *		int8and		- returns arg1 & arg2
1214  *		int8or		- returns arg1 | arg2
1215  *		int8xor		- returns arg1 # arg2
1216  *		int8not		- returns ~arg1
1217  *		int8shl		- returns arg1 << arg2
1218  *		int8shr		- returns arg1 >> arg2
1219  */
1220 
1221 Datum
int8and(PG_FUNCTION_ARGS)1222 int8and(PG_FUNCTION_ARGS)
1223 {
1224 	int64		arg1 = PG_GETARG_INT64(0);
1225 	int64		arg2 = PG_GETARG_INT64(1);
1226 
1227 	PG_RETURN_INT64(arg1 & arg2);
1228 }
1229 
1230 Datum
int8or(PG_FUNCTION_ARGS)1231 int8or(PG_FUNCTION_ARGS)
1232 {
1233 	int64		arg1 = PG_GETARG_INT64(0);
1234 	int64		arg2 = PG_GETARG_INT64(1);
1235 
1236 	PG_RETURN_INT64(arg1 | arg2);
1237 }
1238 
1239 Datum
int8xor(PG_FUNCTION_ARGS)1240 int8xor(PG_FUNCTION_ARGS)
1241 {
1242 	int64		arg1 = PG_GETARG_INT64(0);
1243 	int64		arg2 = PG_GETARG_INT64(1);
1244 
1245 	PG_RETURN_INT64(arg1 ^ arg2);
1246 }
1247 
1248 Datum
int8not(PG_FUNCTION_ARGS)1249 int8not(PG_FUNCTION_ARGS)
1250 {
1251 	int64		arg1 = PG_GETARG_INT64(0);
1252 
1253 	PG_RETURN_INT64(~arg1);
1254 }
1255 
1256 Datum
int8shl(PG_FUNCTION_ARGS)1257 int8shl(PG_FUNCTION_ARGS)
1258 {
1259 	int64		arg1 = PG_GETARG_INT64(0);
1260 	int32		arg2 = PG_GETARG_INT32(1);
1261 
1262 	PG_RETURN_INT64(arg1 << arg2);
1263 }
1264 
1265 Datum
int8shr(PG_FUNCTION_ARGS)1266 int8shr(PG_FUNCTION_ARGS)
1267 {
1268 	int64		arg1 = PG_GETARG_INT64(0);
1269 	int32		arg2 = PG_GETARG_INT32(1);
1270 
1271 	PG_RETURN_INT64(arg1 >> arg2);
1272 }
1273 
1274 /*----------------------------------------------------------
1275  *	Conversion operators.
1276  *---------------------------------------------------------*/
1277 
1278 Datum
int48(PG_FUNCTION_ARGS)1279 int48(PG_FUNCTION_ARGS)
1280 {
1281 	int32		arg = PG_GETARG_INT32(0);
1282 
1283 	PG_RETURN_INT64((int64) arg);
1284 }
1285 
1286 Datum
int84(PG_FUNCTION_ARGS)1287 int84(PG_FUNCTION_ARGS)
1288 {
1289 	int64		arg = PG_GETARG_INT64(0);
1290 	int32		result;
1291 
1292 	result = (int32) arg;
1293 
1294 	/* Test for overflow by reverse-conversion. */
1295 	if ((int64) result != arg)
1296 		ereport(ERROR,
1297 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1298 				 errmsg("integer out of range")));
1299 
1300 	PG_RETURN_INT32(result);
1301 }
1302 
1303 Datum
int28(PG_FUNCTION_ARGS)1304 int28(PG_FUNCTION_ARGS)
1305 {
1306 	int16		arg = PG_GETARG_INT16(0);
1307 
1308 	PG_RETURN_INT64((int64) arg);
1309 }
1310 
1311 Datum
int82(PG_FUNCTION_ARGS)1312 int82(PG_FUNCTION_ARGS)
1313 {
1314 	int64		arg = PG_GETARG_INT64(0);
1315 	int16		result;
1316 
1317 	result = (int16) arg;
1318 
1319 	/* Test for overflow by reverse-conversion. */
1320 	if ((int64) result != arg)
1321 		ereport(ERROR,
1322 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1323 				 errmsg("smallint out of range")));
1324 
1325 	PG_RETURN_INT16(result);
1326 }
1327 
1328 Datum
i8tod(PG_FUNCTION_ARGS)1329 i8tod(PG_FUNCTION_ARGS)
1330 {
1331 	int64		arg = PG_GETARG_INT64(0);
1332 	float8		result;
1333 
1334 	result = arg;
1335 
1336 	PG_RETURN_FLOAT8(result);
1337 }
1338 
1339 /* dtoi8()
1340  * Convert float8 to 8-byte integer.
1341  */
1342 Datum
dtoi8(PG_FUNCTION_ARGS)1343 dtoi8(PG_FUNCTION_ARGS)
1344 {
1345 	float8		num = PG_GETARG_FLOAT8(0);
1346 
1347 	/*
1348 	 * Get rid of any fractional part in the input.  This is so we don't fail
1349 	 * on just-out-of-range values that would round into range.  Note
1350 	 * assumption that rint() will pass through a NaN or Inf unchanged.
1351 	 */
1352 	num = rint(num);
1353 
1354 	/* Range check */
1355 	if (isnan(num) || !FLOAT8_FITS_IN_INT64(num))
1356 		ereport(ERROR,
1357 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1358 				 errmsg("bigint out of range")));
1359 
1360 	PG_RETURN_INT64((int64) num);
1361 }
1362 
1363 Datum
i8tof(PG_FUNCTION_ARGS)1364 i8tof(PG_FUNCTION_ARGS)
1365 {
1366 	int64		arg = PG_GETARG_INT64(0);
1367 	float4		result;
1368 
1369 	result = arg;
1370 
1371 	PG_RETURN_FLOAT4(result);
1372 }
1373 
1374 /* ftoi8()
1375  * Convert float4 to 8-byte integer.
1376  */
1377 Datum
ftoi8(PG_FUNCTION_ARGS)1378 ftoi8(PG_FUNCTION_ARGS)
1379 {
1380 	float4		num = PG_GETARG_FLOAT4(0);
1381 
1382 	/*
1383 	 * Get rid of any fractional part in the input.  This is so we don't fail
1384 	 * on just-out-of-range values that would round into range.  Note
1385 	 * assumption that rint() will pass through a NaN or Inf unchanged.
1386 	 */
1387 	num = rint(num);
1388 
1389 	/* Range check */
1390 	if (isnan(num) || !FLOAT4_FITS_IN_INT64(num))
1391 		ereport(ERROR,
1392 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1393 				 errmsg("bigint out of range")));
1394 
1395 	PG_RETURN_INT64((int64) num);
1396 }
1397 
1398 Datum
i8tooid(PG_FUNCTION_ARGS)1399 i8tooid(PG_FUNCTION_ARGS)
1400 {
1401 	int64		arg = PG_GETARG_INT64(0);
1402 	Oid			result;
1403 
1404 	result = (Oid) arg;
1405 
1406 	/* Test for overflow by reverse-conversion. */
1407 	if ((int64) result != arg)
1408 		ereport(ERROR,
1409 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1410 				 errmsg("OID out of range")));
1411 
1412 	PG_RETURN_OID(result);
1413 }
1414 
1415 Datum
oidtoi8(PG_FUNCTION_ARGS)1416 oidtoi8(PG_FUNCTION_ARGS)
1417 {
1418 	Oid			arg = PG_GETARG_OID(0);
1419 
1420 	PG_RETURN_INT64((int64) arg);
1421 }
1422 
1423 /*
1424  * non-persistent numeric series generator
1425  */
1426 Datum
generate_series_int8(PG_FUNCTION_ARGS)1427 generate_series_int8(PG_FUNCTION_ARGS)
1428 {
1429 	return generate_series_step_int8(fcinfo);
1430 }
1431 
1432 Datum
generate_series_step_int8(PG_FUNCTION_ARGS)1433 generate_series_step_int8(PG_FUNCTION_ARGS)
1434 {
1435 	FuncCallContext *funcctx;
1436 	generate_series_fctx *fctx;
1437 	int64		result;
1438 	MemoryContext oldcontext;
1439 
1440 	/* stuff done only on the first call of the function */
1441 	if (SRF_IS_FIRSTCALL())
1442 	{
1443 		int64		start = PG_GETARG_INT64(0);
1444 		int64		finish = PG_GETARG_INT64(1);
1445 		int64		step = 1;
1446 
1447 		/* see if we were given an explicit step size */
1448 		if (PG_NARGS() == 3)
1449 			step = PG_GETARG_INT64(2);
1450 		if (step == 0)
1451 			ereport(ERROR,
1452 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1453 					 errmsg("step size cannot equal zero")));
1454 
1455 		/* create a function context for cross-call persistence */
1456 		funcctx = SRF_FIRSTCALL_INIT();
1457 
1458 		/*
1459 		 * switch to memory context appropriate for multiple function calls
1460 		 */
1461 		oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1462 
1463 		/* allocate memory for user context */
1464 		fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1465 
1466 		/*
1467 		 * Use fctx to keep state from call to call. Seed current with the
1468 		 * original start value
1469 		 */
1470 		fctx->current = start;
1471 		fctx->finish = finish;
1472 		fctx->step = step;
1473 
1474 		funcctx->user_fctx = fctx;
1475 		MemoryContextSwitchTo(oldcontext);
1476 	}
1477 
1478 	/* stuff done on every call of the function */
1479 	funcctx = SRF_PERCALL_SETUP();
1480 
1481 	/*
1482 	 * get the saved state and use current as the result for this iteration
1483 	 */
1484 	fctx = funcctx->user_fctx;
1485 	result = fctx->current;
1486 
1487 	if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1488 		(fctx->step < 0 && fctx->current >= fctx->finish))
1489 	{
1490 		/* increment current in preparation for next iteration */
1491 		fctx->current += fctx->step;
1492 
1493 		/* if next-value computation overflows, this is the final result */
1494 		if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
1495 			fctx->step = 0;
1496 
1497 		/* do when there is more left to send */
1498 		SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
1499 	}
1500 	else
1501 		/* do when there is no more left */
1502 		SRF_RETURN_DONE(funcctx);
1503 }
1504