---------------------------------------------------------------------------
--
-- int8.sql-
--    This file defines operators for 64-bit integers.
--
---------------------------------------------------------------------------

LOAD '_OBJWD_/int8.so';

CREATE FUNCTION int8in(opaque)
    RETURNS int8
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE FUNCTION int8out(opaque)
    RETURNS opaque
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE TYPE int8 (
   internallength = 8,
   input = int8in,
   output = int8out
);


-----------------------------
-- Create operators
-----------------------------

CREATE FUNCTION int8um(int8)
    RETURNS int8
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR - (
   rightarg = int8,
   procedure = int8um
);

CREATE FUNCTION int8pl(int8,int8)
    RETURNS int8
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR + (
   leftarg = int8,
   rightarg = int8,
   procedure = int8pl,
   commutator = +
);

CREATE FUNCTION int8mi(int8,int8)
    RETURNS int8
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR - (
   leftarg = int8,
   rightarg = int8,
   procedure = int8mi
);

CREATE FUNCTION int8mul(int8,int8)
    RETURNS int8
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR * (
   leftarg = int8,
   rightarg = int8,
   procedure = int8mul,
   commutator = *
);

CREATE FUNCTION int8div(int8,int8)
    RETURNS int8
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR / (
   leftarg = int8,
   rightarg = int8,
   procedure = int8div
);

--
-- 64-bit comparison operators
--

CREATE FUNCTION int8eq(int8,int8)
    RETURNS bool
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR = (
   leftarg = int8,
   rightarg = int8,
   procedure = int8eq,
   commutator = =
);

CREATE FUNCTION int8ne(int8,int8)
    RETURNS bool
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR <> (
   leftarg = int8,
   rightarg = int8,
   procedure = int8ne,
   commutator = <>
);

CREATE FUNCTION int8lt(int8,int8)
    RETURNS bool
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR < (
   leftarg = int8,
   rightarg = int8,
   procedure = int8lt
);

CREATE FUNCTION int8gt(int8,int8)
    RETURNS bool
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR > (
   leftarg = int8,
   rightarg = int8,
   procedure = int8gt
);

CREATE FUNCTION int8le(int8,int8)
    RETURNS bool
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR <= (
   leftarg = int8,
   rightarg = int8,
   procedure = int8le
);

CREATE FUNCTION int8ge(int8,int8)
    RETURNS bool
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR >= (
   leftarg = int8,
   rightarg = int8,
   procedure = int8ge
);

--
-- 64-bit/32-bit comparison operators
--

CREATE FUNCTION int84eq(int8,int4)
    RETURNS bool
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR = (
   leftarg = int8,
   rightarg = int4,
   procedure = int84eq,
   commutator = =
);

CREATE FUNCTION int84ne(int8,int4)
    RETURNS bool
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR <> (
   leftarg = int8,
   rightarg = int4,
   procedure = int84ne,
   commutator = <>
);

CREATE FUNCTION int84lt(int8,int4)
    RETURNS bool
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR < (
   leftarg = int8,
   rightarg = int4,
   procedure = int84lt
);

CREATE FUNCTION int84gt(int8,int4)
    RETURNS bool
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR > (
   leftarg = int8,
   rightarg = int4,
   procedure = int84gt
);

CREATE FUNCTION int84le(int8,int4)
    RETURNS bool
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR <= (
   leftarg = int8,
   rightarg = int4,
   procedure = int84le
);

CREATE FUNCTION int84ge(int8,int4)
    RETURNS bool
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE OPERATOR >= (
   leftarg = int8,
   rightarg = int4,
   procedure = int84ge
);

--
-- Conversion functions
--

CREATE FUNCTION int48(int4)
    RETURNS int8
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE FUNCTION int84(int8)
    RETURNS int4
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

--CREATE FUNCTION int28(int2)
--    RETURNS int8
--    AS '_OBJWD_/int8.so'
--    LANGUAGE 'c';
--
--CREATE FUNCTION int82(int8)
--    RETURNS int2
--    AS '_OBJWD_/int8.so'
--    LANGUAGE 'c';

CREATE FUNCTION i8tod(int8)
    RETURNS float8
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

CREATE FUNCTION dtoi8(float8)
    RETURNS int8
    AS '_OBJWD_/int8.so'
    LANGUAGE 'c';

--
-- Generic conversion routines
--

CREATE FUNCTION int8(int4)
    RETURNS int8
    AS 'select int48($1)'
    LANGUAGE 'sql';

CREATE FUNCTION int8(float8)
    RETURNS int8
    AS 'select dtoi8($1)'
    LANGUAGE 'sql';

CREATE FUNCTION float8(int8)
    RETURNS float8
    AS 'select i8tod($1)'
    LANGUAGE 'sql';

CREATE FUNCTION int4(int8)
    RETURNS int4
    AS 'select int84($1)'
    LANGUAGE 'sql';

