]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/int8.c
Run pgindent on 9.2 source tree in preparation for first 9.3
[postgresql] / src / backend / utils / adt / int8.c
1 /*-------------------------------------------------------------------------
2  *
3  * int8.c
4  *        Internal 64-bit integer operations
5  *
6  * Portions Copyright (c) 1996-2012, 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
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 = -INT64CONST(0x7fffffffffffffff) - 1;
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 bigint",
115                                                           str)));
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
505 int8up(PG_FUNCTION_ARGS)
506 {
507         int64           arg = PG_GETARG_INT64(0);
508
509         PG_RETURN_INT64(arg);
510 }
511
512 Datum
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
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
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                         (result / arg2 != arg1 || (arg2 == -1 && arg1 < 0 && result < 0)))
578                         ereport(ERROR,
579                                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
580                                          errmsg("bigint out of range")));
581         }
582         PG_RETURN_INT64(result);
583 }
584
585 Datum
586 int8div(PG_FUNCTION_ARGS)
587 {
588         int64           arg1 = PG_GETARG_INT64(0);
589         int64           arg2 = PG_GETARG_INT64(1);
590         int64           result;
591
592         if (arg2 == 0)
593         {
594                 ereport(ERROR,
595                                 (errcode(ERRCODE_DIVISION_BY_ZERO),
596                                  errmsg("division by zero")));
597                 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
598                 PG_RETURN_NULL();
599         }
600
601         result = arg1 / arg2;
602
603         /*
604          * Overflow check.      The only possible overflow case is for arg1 =
605          * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
606          * can't be represented on a two's-complement machine.  Most machines
607          * produce INT64_MIN but it seems some produce zero.
608          */
609         if (arg2 == -1 && arg1 < 0 && result <= 0)
610                 ereport(ERROR,
611                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
612                                  errmsg("bigint out of range")));
613         PG_RETURN_INT64(result);
614 }
615
616 /* int8abs()
617  * Absolute value
618  */
619 Datum
620 int8abs(PG_FUNCTION_ARGS)
621 {
622         int64           arg1 = PG_GETARG_INT64(0);
623         int64           result;
624
625         result = (arg1 < 0) ? -arg1 : arg1;
626         /* overflow check (needed for INT64_MIN) */
627         if (result < 0)
628                 ereport(ERROR,
629                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
630                                  errmsg("bigint out of range")));
631         PG_RETURN_INT64(result);
632 }
633
634 /* int8mod()
635  * Modulo operation.
636  */
637 Datum
638 int8mod(PG_FUNCTION_ARGS)
639 {
640         int64           arg1 = PG_GETARG_INT64(0);
641         int64           arg2 = PG_GETARG_INT64(1);
642
643         if (arg2 == 0)
644         {
645                 ereport(ERROR,
646                                 (errcode(ERRCODE_DIVISION_BY_ZERO),
647                                  errmsg("division by zero")));
648                 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
649                 PG_RETURN_NULL();
650         }
651
652         /* No overflow is possible */
653
654         PG_RETURN_INT64(arg1 % arg2);
655 }
656
657
658 Datum
659 int8inc(PG_FUNCTION_ARGS)
660 {
661         /*
662          * When int8 is pass-by-reference, we provide this special case to avoid
663          * palloc overhead for COUNT(): when called as an aggregate, we know that
664          * the argument is modifiable local storage, so just update it in-place.
665          * (If int8 is pass-by-value, then of course this is useless as well as
666          * incorrect, so just ifdef it out.)
667          */
668 #ifndef USE_FLOAT8_BYVAL                /* controls int8 too */
669         if (AggCheckCallContext(fcinfo, NULL))
670         {
671                 int64      *arg = (int64 *) PG_GETARG_POINTER(0);
672                 int64           result;
673
674                 result = *arg + 1;
675                 /* Overflow check */
676                 if (result < 0 && *arg > 0)
677                         ereport(ERROR,
678                                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
679                                          errmsg("bigint out of range")));
680
681                 *arg = result;
682                 PG_RETURN_POINTER(arg);
683         }
684         else
685 #endif
686         {
687                 /* Not called as an aggregate, so just do it the dumb way */
688                 int64           arg = PG_GETARG_INT64(0);
689                 int64           result;
690
691                 result = arg + 1;
692                 /* Overflow check */
693                 if (result < 0 && arg > 0)
694                         ereport(ERROR,
695                                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
696                                          errmsg("bigint out of range")));
697
698                 PG_RETURN_INT64(result);
699         }
700 }
701
702 /*
703  * These functions are exactly like int8inc but are used for aggregates that
704  * count only non-null values.  Since the functions are declared strict,
705  * the null checks happen before we ever get here, and all we need do is
706  * increment the state value.  We could actually make these pg_proc entries
707  * point right at int8inc, but then the opr_sanity regression test would
708  * complain about mismatched entries for a built-in function.
709  */
710
711 Datum
712 int8inc_any(PG_FUNCTION_ARGS)
713 {
714         return int8inc(fcinfo);
715 }
716
717 Datum
718 int8inc_float8_float8(PG_FUNCTION_ARGS)
719 {
720         return int8inc(fcinfo);
721 }
722
723
724 Datum
725 int8larger(PG_FUNCTION_ARGS)
726 {
727         int64           arg1 = PG_GETARG_INT64(0);
728         int64           arg2 = PG_GETARG_INT64(1);
729         int64           result;
730
731         result = ((arg1 > arg2) ? arg1 : arg2);
732
733         PG_RETURN_INT64(result);
734 }
735
736 Datum
737 int8smaller(PG_FUNCTION_ARGS)
738 {
739         int64           arg1 = PG_GETARG_INT64(0);
740         int64           arg2 = PG_GETARG_INT64(1);
741         int64           result;
742
743         result = ((arg1 < arg2) ? arg1 : arg2);
744
745         PG_RETURN_INT64(result);
746 }
747
748 Datum
749 int84pl(PG_FUNCTION_ARGS)
750 {
751         int64           arg1 = PG_GETARG_INT64(0);
752         int32           arg2 = PG_GETARG_INT32(1);
753         int64           result;
754
755         result = arg1 + arg2;
756
757         /*
758          * Overflow check.      If the inputs are of different signs then their sum
759          * cannot overflow.  If the inputs are of the same sign, their sum had
760          * better be that sign too.
761          */
762         if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
763                 ereport(ERROR,
764                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
765                                  errmsg("bigint out of range")));
766         PG_RETURN_INT64(result);
767 }
768
769 Datum
770 int84mi(PG_FUNCTION_ARGS)
771 {
772         int64           arg1 = PG_GETARG_INT64(0);
773         int32           arg2 = PG_GETARG_INT32(1);
774         int64           result;
775
776         result = arg1 - arg2;
777
778         /*
779          * Overflow check.      If the inputs are of the same sign then their
780          * difference cannot overflow.  If they are of different signs then the
781          * result should be of the same sign as the first input.
782          */
783         if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
784                 ereport(ERROR,
785                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
786                                  errmsg("bigint out of range")));
787         PG_RETURN_INT64(result);
788 }
789
790 Datum
791 int84mul(PG_FUNCTION_ARGS)
792 {
793         int64           arg1 = PG_GETARG_INT64(0);
794         int32           arg2 = PG_GETARG_INT32(1);
795         int64           result;
796
797         result = arg1 * arg2;
798
799         /*
800          * Overflow check.      We basically check to see if result / arg1 gives arg2
801          * again.  There is one case where this fails: arg1 = 0 (which cannot
802          * overflow).
803          *
804          * Since the division is likely much more expensive than the actual
805          * multiplication, we'd like to skip it where possible.  The best bang for
806          * the buck seems to be to check whether both inputs are in the int32
807          * range; if so, no overflow is possible.
808          */
809         if (arg1 != (int64) ((int32) arg1) &&
810                 result / arg1 != arg2)
811                 ereport(ERROR,
812                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
813                                  errmsg("bigint out of range")));
814         PG_RETURN_INT64(result);
815 }
816
817 Datum
818 int84div(PG_FUNCTION_ARGS)
819 {
820         int64           arg1 = PG_GETARG_INT64(0);
821         int32           arg2 = PG_GETARG_INT32(1);
822         int64           result;
823
824         if (arg2 == 0)
825         {
826                 ereport(ERROR,
827                                 (errcode(ERRCODE_DIVISION_BY_ZERO),
828                                  errmsg("division by zero")));
829                 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
830                 PG_RETURN_NULL();
831         }
832
833         result = arg1 / arg2;
834
835         /*
836          * Overflow check.      The only possible overflow case is for arg1 =
837          * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
838          * can't be represented on a two's-complement machine.  Most machines
839          * produce INT64_MIN but it seems some produce zero.
840          */
841         if (arg2 == -1 && arg1 < 0 && result <= 0)
842                 ereport(ERROR,
843                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
844                                  errmsg("bigint out of range")));
845         PG_RETURN_INT64(result);
846 }
847
848 Datum
849 int48pl(PG_FUNCTION_ARGS)
850 {
851         int32           arg1 = PG_GETARG_INT32(0);
852         int64           arg2 = PG_GETARG_INT64(1);
853         int64           result;
854
855         result = arg1 + arg2;
856
857         /*
858          * Overflow check.      If the inputs are of different signs then their sum
859          * cannot overflow.  If the inputs are of the same sign, their sum had
860          * better be that sign too.
861          */
862         if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
863                 ereport(ERROR,
864                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
865                                  errmsg("bigint out of range")));
866         PG_RETURN_INT64(result);
867 }
868
869 Datum
870 int48mi(PG_FUNCTION_ARGS)
871 {
872         int32           arg1 = PG_GETARG_INT32(0);
873         int64           arg2 = PG_GETARG_INT64(1);
874         int64           result;
875
876         result = arg1 - arg2;
877
878         /*
879          * Overflow check.      If the inputs are of the same sign then their
880          * difference cannot overflow.  If they are of different signs then the
881          * result should be of the same sign as the first input.
882          */
883         if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
884                 ereport(ERROR,
885                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
886                                  errmsg("bigint out of range")));
887         PG_RETURN_INT64(result);
888 }
889
890 Datum
891 int48mul(PG_FUNCTION_ARGS)
892 {
893         int32           arg1 = PG_GETARG_INT32(0);
894         int64           arg2 = PG_GETARG_INT64(1);
895         int64           result;
896
897         result = arg1 * arg2;
898
899         /*
900          * Overflow check.      We basically check to see if result / arg2 gives arg1
901          * again.  There is one case where this fails: arg2 = 0 (which cannot
902          * overflow).
903          *
904          * Since the division is likely much more expensive than the actual
905          * multiplication, we'd like to skip it where possible.  The best bang for
906          * the buck seems to be to check whether both inputs are in the int32
907          * range; if so, no overflow is possible.
908          */
909         if (arg2 != (int64) ((int32) arg2) &&
910                 result / arg2 != arg1)
911                 ereport(ERROR,
912                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
913                                  errmsg("bigint out of range")));
914         PG_RETURN_INT64(result);
915 }
916
917 Datum
918 int48div(PG_FUNCTION_ARGS)
919 {
920         int32           arg1 = PG_GETARG_INT32(0);
921         int64           arg2 = PG_GETARG_INT64(1);
922
923         if (arg2 == 0)
924         {
925                 ereport(ERROR,
926                                 (errcode(ERRCODE_DIVISION_BY_ZERO),
927                                  errmsg("division by zero")));
928                 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
929                 PG_RETURN_NULL();
930         }
931
932         /* No overflow is possible */
933         PG_RETURN_INT64((int64) arg1 / arg2);
934 }
935
936 Datum
937 int82pl(PG_FUNCTION_ARGS)
938 {
939         int64           arg1 = PG_GETARG_INT64(0);
940         int16           arg2 = PG_GETARG_INT16(1);
941         int64           result;
942
943         result = arg1 + arg2;
944
945         /*
946          * Overflow check.      If the inputs are of different signs then their sum
947          * cannot overflow.  If the inputs are of the same sign, their sum had
948          * better be that sign too.
949          */
950         if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
951                 ereport(ERROR,
952                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
953                                  errmsg("bigint out of range")));
954         PG_RETURN_INT64(result);
955 }
956
957 Datum
958 int82mi(PG_FUNCTION_ARGS)
959 {
960         int64           arg1 = PG_GETARG_INT64(0);
961         int16           arg2 = PG_GETARG_INT16(1);
962         int64           result;
963
964         result = arg1 - arg2;
965
966         /*
967          * Overflow check.      If the inputs are of the same sign then their
968          * difference cannot overflow.  If they are of different signs then the
969          * result should be of the same sign as the first input.
970          */
971         if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
972                 ereport(ERROR,
973                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
974                                  errmsg("bigint out of range")));
975         PG_RETURN_INT64(result);
976 }
977
978 Datum
979 int82mul(PG_FUNCTION_ARGS)
980 {
981         int64           arg1 = PG_GETARG_INT64(0);
982         int16           arg2 = PG_GETARG_INT16(1);
983         int64           result;
984
985         result = arg1 * arg2;
986
987         /*
988          * Overflow check.      We basically check to see if result / arg1 gives arg2
989          * again.  There is one case where this fails: arg1 = 0 (which cannot
990          * overflow).
991          *
992          * Since the division is likely much more expensive than the actual
993          * multiplication, we'd like to skip it where possible.  The best bang for
994          * the buck seems to be to check whether both inputs are in the int32
995          * range; if so, no overflow is possible.
996          */
997         if (arg1 != (int64) ((int32) arg1) &&
998                 result / arg1 != arg2)
999                 ereport(ERROR,
1000                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1001                                  errmsg("bigint out of range")));
1002         PG_RETURN_INT64(result);
1003 }
1004
1005 Datum
1006 int82div(PG_FUNCTION_ARGS)
1007 {
1008         int64           arg1 = PG_GETARG_INT64(0);
1009         int16           arg2 = PG_GETARG_INT16(1);
1010         int64           result;
1011
1012         if (arg2 == 0)
1013         {
1014                 ereport(ERROR,
1015                                 (errcode(ERRCODE_DIVISION_BY_ZERO),
1016                                  errmsg("division by zero")));
1017                 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1018                 PG_RETURN_NULL();
1019         }
1020
1021         result = arg1 / arg2;
1022
1023         /*
1024          * Overflow check.      The only possible overflow case is for arg1 =
1025          * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
1026          * can't be represented on a two's-complement machine.  Most machines
1027          * produce INT64_MIN but it seems some produce zero.
1028          */
1029         if (arg2 == -1 && arg1 < 0 && result <= 0)
1030                 ereport(ERROR,
1031                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1032                                  errmsg("bigint out of range")));
1033         PG_RETURN_INT64(result);
1034 }
1035
1036 Datum
1037 int28pl(PG_FUNCTION_ARGS)
1038 {
1039         int16           arg1 = PG_GETARG_INT16(0);
1040         int64           arg2 = PG_GETARG_INT64(1);
1041         int64           result;
1042
1043         result = arg1 + arg2;
1044
1045         /*
1046          * Overflow check.      If the inputs are of different signs then their sum
1047          * cannot overflow.  If the inputs are of the same sign, their sum had
1048          * better be that sign too.
1049          */
1050         if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1051                 ereport(ERROR,
1052                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1053                                  errmsg("bigint out of range")));
1054         PG_RETURN_INT64(result);
1055 }
1056
1057 Datum
1058 int28mi(PG_FUNCTION_ARGS)
1059 {
1060         int16           arg1 = PG_GETARG_INT16(0);
1061         int64           arg2 = PG_GETARG_INT64(1);
1062         int64           result;
1063
1064         result = arg1 - arg2;
1065
1066         /*
1067          * Overflow check.      If the inputs are of the same sign then their
1068          * difference cannot overflow.  If they are of different signs then the
1069          * result should be of the same sign as the first input.
1070          */
1071         if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1072                 ereport(ERROR,
1073                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1074                                  errmsg("bigint out of range")));
1075         PG_RETURN_INT64(result);
1076 }
1077
1078 Datum
1079 int28mul(PG_FUNCTION_ARGS)
1080 {
1081         int16           arg1 = PG_GETARG_INT16(0);
1082         int64           arg2 = PG_GETARG_INT64(1);
1083         int64           result;
1084
1085         result = arg1 * arg2;
1086
1087         /*
1088          * Overflow check.      We basically check to see if result / arg2 gives arg1
1089          * again.  There is one case where this fails: arg2 = 0 (which cannot
1090          * overflow).
1091          *
1092          * Since the division is likely much more expensive than the actual
1093          * multiplication, we'd like to skip it where possible.  The best bang for
1094          * the buck seems to be to check whether both inputs are in the int32
1095          * range; if so, no overflow is possible.
1096          */
1097         if (arg2 != (int64) ((int32) arg2) &&
1098                 result / arg2 != arg1)
1099                 ereport(ERROR,
1100                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1101                                  errmsg("bigint out of range")));
1102         PG_RETURN_INT64(result);
1103 }
1104
1105 Datum
1106 int28div(PG_FUNCTION_ARGS)
1107 {
1108         int16           arg1 = PG_GETARG_INT16(0);
1109         int64           arg2 = PG_GETARG_INT64(1);
1110
1111         if (arg2 == 0)
1112         {
1113                 ereport(ERROR,
1114                                 (errcode(ERRCODE_DIVISION_BY_ZERO),
1115                                  errmsg("division by zero")));
1116                 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1117                 PG_RETURN_NULL();
1118         }
1119
1120         /* No overflow is possible */
1121         PG_RETURN_INT64((int64) arg1 / arg2);
1122 }
1123
1124 /* Binary arithmetics
1125  *
1126  *              int8and         - returns arg1 & arg2
1127  *              int8or          - returns arg1 | arg2
1128  *              int8xor         - returns arg1 # arg2
1129  *              int8not         - returns ~arg1
1130  *              int8shl         - returns arg1 << arg2
1131  *              int8shr         - returns arg1 >> arg2
1132  */
1133
1134 Datum
1135 int8and(PG_FUNCTION_ARGS)
1136 {
1137         int64           arg1 = PG_GETARG_INT64(0);
1138         int64           arg2 = PG_GETARG_INT64(1);
1139
1140         PG_RETURN_INT64(arg1 & arg2);
1141 }
1142
1143 Datum
1144 int8or(PG_FUNCTION_ARGS)
1145 {
1146         int64           arg1 = PG_GETARG_INT64(0);
1147         int64           arg2 = PG_GETARG_INT64(1);
1148
1149         PG_RETURN_INT64(arg1 | arg2);
1150 }
1151
1152 Datum
1153 int8xor(PG_FUNCTION_ARGS)
1154 {
1155         int64           arg1 = PG_GETARG_INT64(0);
1156         int64           arg2 = PG_GETARG_INT64(1);
1157
1158         PG_RETURN_INT64(arg1 ^ arg2);
1159 }
1160
1161 Datum
1162 int8not(PG_FUNCTION_ARGS)
1163 {
1164         int64           arg1 = PG_GETARG_INT64(0);
1165
1166         PG_RETURN_INT64(~arg1);
1167 }
1168
1169 Datum
1170 int8shl(PG_FUNCTION_ARGS)
1171 {
1172         int64           arg1 = PG_GETARG_INT64(0);
1173         int32           arg2 = PG_GETARG_INT32(1);
1174
1175         PG_RETURN_INT64(arg1 << arg2);
1176 }
1177
1178 Datum
1179 int8shr(PG_FUNCTION_ARGS)
1180 {
1181         int64           arg1 = PG_GETARG_INT64(0);
1182         int32           arg2 = PG_GETARG_INT32(1);
1183
1184         PG_RETURN_INT64(arg1 >> arg2);
1185 }
1186
1187 /*----------------------------------------------------------
1188  *      Conversion operators.
1189  *---------------------------------------------------------*/
1190
1191 Datum
1192 int48(PG_FUNCTION_ARGS)
1193 {
1194         int32           arg = PG_GETARG_INT32(0);
1195
1196         PG_RETURN_INT64((int64) arg);
1197 }
1198
1199 Datum
1200 int84(PG_FUNCTION_ARGS)
1201 {
1202         int64           arg = PG_GETARG_INT64(0);
1203         int32           result;
1204
1205         result = (int32) arg;
1206
1207         /* Test for overflow by reverse-conversion. */
1208         if ((int64) result != arg)
1209                 ereport(ERROR,
1210                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1211                                  errmsg("integer out of range")));
1212
1213         PG_RETURN_INT32(result);
1214 }
1215
1216 Datum
1217 int28(PG_FUNCTION_ARGS)
1218 {
1219         int16           arg = PG_GETARG_INT16(0);
1220
1221         PG_RETURN_INT64((int64) arg);
1222 }
1223
1224 Datum
1225 int82(PG_FUNCTION_ARGS)
1226 {
1227         int64           arg = PG_GETARG_INT64(0);
1228         int16           result;
1229
1230         result = (int16) arg;
1231
1232         /* Test for overflow by reverse-conversion. */
1233         if ((int64) result != arg)
1234                 ereport(ERROR,
1235                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1236                                  errmsg("smallint out of range")));
1237
1238         PG_RETURN_INT16(result);
1239 }
1240
1241 Datum
1242 i8tod(PG_FUNCTION_ARGS)
1243 {
1244         int64           arg = PG_GETARG_INT64(0);
1245         float8          result;
1246
1247         result = arg;
1248
1249         PG_RETURN_FLOAT8(result);
1250 }
1251
1252 /* dtoi8()
1253  * Convert float8 to 8-byte integer.
1254  */
1255 Datum
1256 dtoi8(PG_FUNCTION_ARGS)
1257 {
1258         float8          arg = PG_GETARG_FLOAT8(0);
1259         int64           result;
1260
1261         /* Round arg to nearest integer (but it's still in float form) */
1262         arg = rint(arg);
1263
1264         /*
1265          * Does it fit in an int64?  Avoid assuming that we have handy constants
1266          * defined for the range boundaries, instead test for overflow by
1267          * reverse-conversion.
1268          */
1269         result = (int64) arg;
1270
1271         if ((float8) result != arg)
1272                 ereport(ERROR,
1273                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1274                                  errmsg("bigint out of range")));
1275
1276         PG_RETURN_INT64(result);
1277 }
1278
1279 Datum
1280 i8tof(PG_FUNCTION_ARGS)
1281 {
1282         int64           arg = PG_GETARG_INT64(0);
1283         float4          result;
1284
1285         result = arg;
1286
1287         PG_RETURN_FLOAT4(result);
1288 }
1289
1290 /* ftoi8()
1291  * Convert float4 to 8-byte integer.
1292  */
1293 Datum
1294 ftoi8(PG_FUNCTION_ARGS)
1295 {
1296         float4          arg = PG_GETARG_FLOAT4(0);
1297         int64           result;
1298         float8          darg;
1299
1300         /* Round arg to nearest integer (but it's still in float form) */
1301         darg = rint(arg);
1302
1303         /*
1304          * Does it fit in an int64?  Avoid assuming that we have handy constants
1305          * defined for the range boundaries, instead test for overflow by
1306          * reverse-conversion.
1307          */
1308         result = (int64) darg;
1309
1310         if ((float8) result != darg)
1311                 ereport(ERROR,
1312                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1313                                  errmsg("bigint out of range")));
1314
1315         PG_RETURN_INT64(result);
1316 }
1317
1318 Datum
1319 i8tooid(PG_FUNCTION_ARGS)
1320 {
1321         int64           arg = PG_GETARG_INT64(0);
1322         Oid                     result;
1323
1324         result = (Oid) arg;
1325
1326         /* Test for overflow by reverse-conversion. */
1327         if ((int64) result != arg)
1328                 ereport(ERROR,
1329                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1330                                  errmsg("OID out of range")));
1331
1332         PG_RETURN_OID(result);
1333 }
1334
1335 Datum
1336 oidtoi8(PG_FUNCTION_ARGS)
1337 {
1338         Oid                     arg = PG_GETARG_OID(0);
1339
1340         PG_RETURN_INT64((int64) arg);
1341 }
1342
1343 /*
1344  * non-persistent numeric series generator
1345  */
1346 Datum
1347 generate_series_int8(PG_FUNCTION_ARGS)
1348 {
1349         return generate_series_step_int8(fcinfo);
1350 }
1351
1352 Datum
1353 generate_series_step_int8(PG_FUNCTION_ARGS)
1354 {
1355         FuncCallContext *funcctx;
1356         generate_series_fctx *fctx;
1357         int64           result;
1358         MemoryContext oldcontext;
1359
1360         /* stuff done only on the first call of the function */
1361         if (SRF_IS_FIRSTCALL())
1362         {
1363                 int64           start = PG_GETARG_INT64(0);
1364                 int64           finish = PG_GETARG_INT64(1);
1365                 int64           step = 1;
1366
1367                 /* see if we were given an explicit step size */
1368                 if (PG_NARGS() == 3)
1369                         step = PG_GETARG_INT64(2);
1370                 if (step == 0)
1371                         ereport(ERROR,
1372                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1373                                          errmsg("step size cannot equal zero")));
1374
1375                 /* create a function context for cross-call persistence */
1376                 funcctx = SRF_FIRSTCALL_INIT();
1377
1378                 /*
1379                  * switch to memory context appropriate for multiple function calls
1380                  */
1381                 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1382
1383                 /* allocate memory for user context */
1384                 fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1385
1386                 /*
1387                  * Use fctx to keep state from call to call. Seed current with the
1388                  * original start value
1389                  */
1390                 fctx->current = start;
1391                 fctx->finish = finish;
1392                 fctx->step = step;
1393
1394                 funcctx->user_fctx = fctx;
1395                 MemoryContextSwitchTo(oldcontext);
1396         }
1397
1398         /* stuff done on every call of the function */
1399         funcctx = SRF_PERCALL_SETUP();
1400
1401         /*
1402          * get the saved state and use current as the result for this iteration
1403          */
1404         fctx = funcctx->user_fctx;
1405         result = fctx->current;
1406
1407         if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1408                 (fctx->step < 0 && fctx->current >= fctx->finish))
1409         {
1410                 /* increment current in preparation for next iteration */
1411                 fctx->current += fctx->step;
1412
1413                 /* if next-value computation overflows, this is the final result */
1414                 if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
1415                         fctx->step = 0;
1416
1417                 /* do when there is more left to send */
1418                 SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
1419         }
1420         else
1421                 /* do when there is no more left */
1422                 SRF_RETURN_DONE(funcctx);
1423 }