Include 8-byte integer type.
At the moment, probably only works for i686/gcc and Alphas...
This commit is contained in:
parent
03fb60c52e
commit
fa838876e9
@ -4,7 +4,7 @@
|
||||
# Makefile for utils/adt
|
||||
#
|
||||
# IDENTIFICATION
|
||||
# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.13 1998/04/29 12:38:01 scrappy Exp $
|
||||
# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.14 1998/07/08 13:57:05 thomas Exp $
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
|
||||
@ -17,12 +17,13 @@ ifdef MB
|
||||
CFLAGS+=-DMB=$(MB)
|
||||
endif
|
||||
|
||||
OBJS = acl.o arrayfuncs.o arrayutils.o bool.o cash.o char.o chunk.o date.o \
|
||||
datum.o dt.o filename.o float.o geo_ops.o geo_selfuncs.o int.o \
|
||||
misc.o nabstime.o name.o not_in.o numutils.o oid.o \
|
||||
oidname.o oidint2.o oidint4.o oracle_compat.o regexp.o regproc.o \
|
||||
selfuncs.o \
|
||||
tid.o varchar.o varlena.o sets.o datetime.o like.o timestamp.o version.o
|
||||
OBJS = acl.o arrayfuncs.o arrayutils.o bool.o cash.o char.o chunk.o \
|
||||
date.o datetime.o datum.o dt.o filename.o float.o \
|
||||
geo_ops.o geo_selfuncs.o int.o int8.o like.o \
|
||||
misc.o nabstime.o name.o not_in.o numutils.o \
|
||||
oid.o oidname.o oidint2.o oidint4.o oracle_compat.o \
|
||||
regexp.o regproc.o selfuncs.o sets.o tid.o timestamp.o \
|
||||
varchar.o varlena.o version.o
|
||||
|
||||
all: SUBSYS.o
|
||||
|
||||
|
521
src/backend/utils/adt/int8.c
Normal file
521
src/backend/utils/adt/int8.c
Normal file
@ -0,0 +1,521 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* int8.c--
|
||||
* Internal 64-bit integer operations
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#include <stdio.h> /* for sprintf proto, etc. */
|
||||
#include <stdlib.h> /* for strtod, etc. */
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "postgres.h"
|
||||
#include "utils/palloc.h"
|
||||
#include "utils/int8.h"
|
||||
|
||||
#define MAXINT8LEN 25
|
||||
|
||||
|
||||
/***********************************************************************
|
||||
**
|
||||
** Routines for 64-bit integers.
|
||||
**
|
||||
***********************************************************************/
|
||||
|
||||
/*----------------------------------------------------------
|
||||
* Formatting and conversion routines.
|
||||
*---------------------------------------------------------*/
|
||||
|
||||
/* int8in()
|
||||
*/
|
||||
int64 *
|
||||
int8in(char *str)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if (!PointerIsValid(str))
|
||||
elog(ERROR, "Bad (null) int8 external representation", NULL);
|
||||
|
||||
if (sscanf(str, INT64_FORMAT, result) != 1)
|
||||
elog(ERROR, "Bad int8 external representation '%s'", str);
|
||||
|
||||
#if FALSE
|
||||
elog(ERROR, "64-bit integers are not supported", NULL);
|
||||
result = NULL;
|
||||
#endif
|
||||
|
||||
return (result);
|
||||
} /* int8in() */
|
||||
|
||||
|
||||
/* int8out()
|
||||
*/
|
||||
char *
|
||||
int8out(int64 * val)
|
||||
{
|
||||
char *result;
|
||||
|
||||
int len;
|
||||
char buf[MAXINT8LEN + 1];
|
||||
|
||||
if (!PointerIsValid(val))
|
||||
return (NULL);
|
||||
|
||||
if ((len = snprintf(buf, MAXINT8LEN, INT64_FORMAT, *val)) < 0)
|
||||
elog(ERROR, "Unable to format int8", NULL);
|
||||
|
||||
result = palloc(len + 1);
|
||||
|
||||
strcpy(result, buf);
|
||||
|
||||
return (result);
|
||||
} /* int8out() */
|
||||
|
||||
|
||||
/*----------------------------------------------------------
|
||||
* Relational operators for int8s.
|
||||
*---------------------------------------------------------*/
|
||||
|
||||
/* int8relop()
|
||||
* Is val1 relop val2?
|
||||
*/
|
||||
bool
|
||||
int8eq(int64 * val1, int64 * val2)
|
||||
{
|
||||
return (*val1 == *val2);
|
||||
} /* int8eq() */
|
||||
|
||||
bool
|
||||
int8ne(int64 * val1, int64 * val2)
|
||||
{
|
||||
return (*val1 != *val2);
|
||||
} /* int8ne() */
|
||||
|
||||
bool
|
||||
int8lt(int64 * val1, int64 * val2)
|
||||
{
|
||||
return (*val1 < *val2);
|
||||
} /* int8lt() */
|
||||
|
||||
bool
|
||||
int8gt(int64 * val1, int64 * val2)
|
||||
{
|
||||
return (*val1 > *val2);
|
||||
} /* int8gt() */
|
||||
|
||||
bool
|
||||
int8le(int64 * val1, int64 * val2)
|
||||
{
|
||||
return (*val1 <= *val2);
|
||||
} /* int8le() */
|
||||
|
||||
bool
|
||||
int8ge(int64 * val1, int64 * val2)
|
||||
{
|
||||
return (*val1 >= *val2);
|
||||
} /* int8ge() */
|
||||
|
||||
|
||||
/* int84relop()
|
||||
* Is 64-bit val1 relop 32-bit val2?
|
||||
*/
|
||||
bool
|
||||
int84eq(int64 * val1, int32 val2)
|
||||
{
|
||||
return (*val1 == val2);
|
||||
} /* int84eq() */
|
||||
|
||||
bool
|
||||
int84ne(int64 * val1, int32 val2)
|
||||
{
|
||||
return (*val1 != val2);
|
||||
} /* int84ne() */
|
||||
|
||||
bool
|
||||
int84lt(int64 * val1, int32 val2)
|
||||
{
|
||||
return (*val1 < val2);
|
||||
} /* int84lt() */
|
||||
|
||||
bool
|
||||
int84gt(int64 * val1, int32 val2)
|
||||
{
|
||||
return (*val1 > val2);
|
||||
} /* int84gt() */
|
||||
|
||||
bool
|
||||
int84le(int64 * val1, int32 val2)
|
||||
{
|
||||
return (*val1 <= val2);
|
||||
} /* int84le() */
|
||||
|
||||
bool
|
||||
int84ge(int64 * val1, int32 val2)
|
||||
{
|
||||
return (*val1 >= val2);
|
||||
} /* int84ge() */
|
||||
|
||||
|
||||
/* int48relop()
|
||||
* Is 32-bit val1 relop 64-bit val2?
|
||||
*/
|
||||
bool
|
||||
int48eq(int32 val1, int64 * val2)
|
||||
{
|
||||
return (val1 == *val2);
|
||||
} /* int48eq() */
|
||||
|
||||
bool
|
||||
int48ne(int32 val1, int64 * val2)
|
||||
{
|
||||
return (val1 != *val2);
|
||||
} /* int48ne() */
|
||||
|
||||
bool
|
||||
int48lt(int32 val1, int64 * val2)
|
||||
{
|
||||
return (val1 < *val2);
|
||||
} /* int48lt() */
|
||||
|
||||
bool
|
||||
int48gt(int32 val1, int64 * val2)
|
||||
{
|
||||
return (val1 > *val2);
|
||||
} /* int48gt() */
|
||||
|
||||
bool
|
||||
int48le(int32 val1, int64 * val2)
|
||||
{
|
||||
return (val1 <= *val2);
|
||||
} /* int48le() */
|
||||
|
||||
bool
|
||||
int48ge(int32 val1, int64 * val2)
|
||||
{
|
||||
return (val1 >= *val2);
|
||||
} /* int48ge() */
|
||||
|
||||
|
||||
/*----------------------------------------------------------
|
||||
* Arithmetic operators on 64-bit integers.
|
||||
*---------------------------------------------------------*/
|
||||
|
||||
int64 *
|
||||
int8um(int64 * val)
|
||||
{
|
||||
int64 temp = 0;
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if (!PointerIsValid(val))
|
||||
return NULL;
|
||||
|
||||
#if FALSE
|
||||
*result = temp - (*val);
|
||||
#else
|
||||
result = int8mi(&temp, val);
|
||||
#endif
|
||||
|
||||
return (result);
|
||||
} /* int8um() */
|
||||
|
||||
|
||||
int64 *
|
||||
int8pl(int64 * val1, int64 * val2)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
|
||||
*result = *val1 + *val2;
|
||||
|
||||
return (result);
|
||||
} /* int8pl() */
|
||||
|
||||
int64 *
|
||||
int8mi(int64 * val1, int64 * val2)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
|
||||
*result = *val1 - *val2;
|
||||
|
||||
return (result);
|
||||
} /* int8mi() */
|
||||
|
||||
int64 *
|
||||
int8mul(int64 * val1, int64 * val2)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
|
||||
*result = *val1 * *val2;
|
||||
|
||||
return (result);
|
||||
} /* int8mul() */
|
||||
|
||||
int64 *
|
||||
int8div(int64 * val1, int64 * val2)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
|
||||
*result = *val1 / *val2;
|
||||
|
||||
return (result);
|
||||
} /* int8div() */
|
||||
|
||||
int64 *
|
||||
int8larger(int64 * val1, int64 * val2)
|
||||
{
|
||||
#if FALSE
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
#endif
|
||||
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
|
||||
#if FALSE
|
||||
*result = ((*val1 > *val2)? *val1: *val2);
|
||||
|
||||
return (result);
|
||||
#endif
|
||||
return ((*val1 > *val2)? val1: val2);
|
||||
} /* int8larger() */
|
||||
|
||||
int64 *
|
||||
int8smaller(int64 * val1, int64 * val2)
|
||||
{
|
||||
#if FALSE
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
#endif
|
||||
|
||||
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
|
||||
return NULL;
|
||||
|
||||
#if FALSE
|
||||
*result = ((*val1 < *val2)? *val1: *val2);
|
||||
|
||||
return (result);
|
||||
#endif
|
||||
return ((*val1 < *val2)? val1: val2);
|
||||
} /* int8smaller() */
|
||||
|
||||
|
||||
int64 *
|
||||
int84pl(int64 * val1, int32 val2)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if (!PointerIsValid(val1))
|
||||
return NULL;
|
||||
|
||||
*result = *val1 + (int64) val2;
|
||||
|
||||
return (result);
|
||||
} /* int84pl() */
|
||||
|
||||
int64 *
|
||||
int84mi(int64 * val1, int32 val2)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if (!PointerIsValid(val1))
|
||||
return NULL;
|
||||
|
||||
*result = *val1 - (int64) val2;
|
||||
|
||||
return (result);
|
||||
} /* int84mi() */
|
||||
|
||||
int64 *
|
||||
int84mul(int64 * val1, int32 val2)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if (!PointerIsValid(val1))
|
||||
return NULL;
|
||||
|
||||
*result = *val1 * (int64) val2;
|
||||
|
||||
return (result);
|
||||
} /* int84mul() */
|
||||
|
||||
int64 *
|
||||
int84div(int64 * val1, int32 val2)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if (!PointerIsValid(val1))
|
||||
return NULL;
|
||||
|
||||
*result = *val1 / (int64) val2;
|
||||
|
||||
return (result);
|
||||
} /* int84div() */
|
||||
|
||||
|
||||
int64 *
|
||||
int48pl(int32 val1, int64 * val2)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if (!PointerIsValid(val2))
|
||||
return NULL;
|
||||
|
||||
*result = (int64) val1 + *val2;
|
||||
|
||||
return (result);
|
||||
} /* int48pl() */
|
||||
|
||||
int64 *
|
||||
int48mi(int32 val1, int64 * val2)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if (!PointerIsValid(val2))
|
||||
return NULL;
|
||||
|
||||
*result = (int64) val1 - *val2;
|
||||
|
||||
return (result);
|
||||
} /* int48mi() */
|
||||
|
||||
int64 *
|
||||
int48mul(int32 val1, int64 * val2)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if (!PointerIsValid(val2))
|
||||
return NULL;
|
||||
|
||||
*result = (int64) val1 * *val2;
|
||||
|
||||
return (result);
|
||||
} /* int48mul() */
|
||||
|
||||
int64 *
|
||||
int48div(int32 val1, int64 * val2)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if (!PointerIsValid(val2))
|
||||
return NULL;
|
||||
|
||||
*result = (int64) val1 / *val2;
|
||||
|
||||
return (result);
|
||||
} /* int48div() */
|
||||
|
||||
|
||||
/*----------------------------------------------------------
|
||||
* Conversion operators.
|
||||
*---------------------------------------------------------*/
|
||||
|
||||
int64 *
|
||||
int48(int32 val)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
*result = val;
|
||||
|
||||
return (result);
|
||||
} /* int48() */
|
||||
|
||||
int32
|
||||
int84(int64 * val)
|
||||
{
|
||||
int32 result;
|
||||
|
||||
if (!PointerIsValid(val))
|
||||
elog(ERROR, "Invalid (null) int64, can't convert int8 to int4", NULL);
|
||||
|
||||
#if FALSE
|
||||
/* Hmm. This conditional always tests true on my i686/linux box.
|
||||
* It's a gcc compiler bug, or I'm missing something obvious, which is more likely...
|
||||
* - thomas 1998-06-09
|
||||
*/
|
||||
if ((*val < INT_MIN) || (*val > INT_MAX))
|
||||
#endif
|
||||
if ((*val < (-pow(2, 31) + 1)) || (*val > (pow(2, 31) - 1)))
|
||||
elog(ERROR, "int8 conversion to int4 is out of range", NULL);
|
||||
|
||||
result = *val;
|
||||
|
||||
return (result);
|
||||
} /* int84() */
|
||||
|
||||
#if FALSE
|
||||
int64 *
|
||||
int28 (int16 val)
|
||||
{
|
||||
int64 *result;
|
||||
|
||||
result = palloc(sizeof(int64));
|
||||
|
||||
*result = val;
|
||||
|
||||
return (result);
|
||||
} /* int28() */
|
||||
|
||||
int16
|
||||
int82(int64 * val)
|
||||
{
|
||||
int16 result;
|
||||
|
||||
if (!PointerIsValid(val))
|
||||
elog(ERROR, "Invalid (null) int8, can't convert to int2", NULL);
|
||||
|
||||
if ((*val < (-pow(2, 15) + 1)) || (*val > (pow(2, 15) - 1)))
|
||||
elog(ERROR, "int8 conversion to int2 is out of range", NULL);
|
||||
|
||||
result = *val;
|
||||
|
||||
return (result);
|
||||
} /* int82() */
|
||||
|
||||
#endif
|
||||
|
||||
float64
|
||||
i8tod(int64 * val)
|
||||
{
|
||||
float64 result = palloc(sizeof(float64data));
|
||||
|
||||
*result = *val;
|
||||
|
||||
return (result);
|
||||
} /* i8tod() */
|
||||
|
||||
/* dtoi8()
|
||||
* Convert double float to 8-byte integer.
|
||||
* Do a range check before the conversion.
|
||||
* Note that the comparison probably isn't quite right
|
||||
* since we only have ~52 bits of precision in a double float
|
||||
* and so subtracting one from a large number gives the large
|
||||
* number exactly. However, for some reason the comparison below
|
||||
* does the right thing on my i686/linux-rh4.2 box.
|
||||
* - thomas 1998-06-16
|
||||
*/
|
||||
int64 *
|
||||
dtoi8(float64 val)
|
||||
{
|
||||
int64 *result = palloc(sizeof(int64));
|
||||
|
||||
if ((*val < (-pow(2, 63) + 1)) || (*val > (pow(2, 63) - 1)))
|
||||
elog(ERROR, "Floating point conversion to int64 is out of range", NULL);
|
||||
|
||||
*result = *val;
|
||||
|
||||
return (result);
|
||||
} /* dtoi8() */
|
Loading…
x
Reference in New Issue
Block a user