Added casts between half to double precision [skip ci]

This commit is contained in:
Andrew Kane
2023-12-04 12:49:28 -08:00
parent 95eff595f0
commit d09aa9f873
5 changed files with 75 additions and 0 deletions

View File

@@ -49,6 +49,12 @@ CREATE FUNCTION float4_to_half(real, integer, boolean) RETURNS half
CREATE FUNCTION half_to_float4(half, integer, boolean) RETURNS real
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION float8_to_half(float8, integer, boolean) RETURNS half
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION half_to_float8(half, integer, boolean) RETURNS float8
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION integer_to_half(integer, integer, boolean) RETURNS half
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
@@ -64,6 +70,12 @@ CREATE CAST (real AS half)
CREATE CAST (half AS real)
WITH FUNCTION half_to_float4(half, integer, boolean) AS IMPLICIT;
CREATE CAST (float8 AS half)
WITH FUNCTION float8_to_half(float8, integer, boolean) AS IMPLICIT;
CREATE CAST (half AS float8)
WITH FUNCTION half_to_float8(half, integer, boolean) AS IMPLICIT;
CREATE CAST (integer AS half)
WITH FUNCTION integer_to_half(integer, integer, boolean) AS IMPLICIT;

View File

@@ -347,6 +347,12 @@ CREATE FUNCTION float4_to_half(real, integer, boolean) RETURNS half
CREATE FUNCTION half_to_float4(half, integer, boolean) RETURNS real
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION float8_to_half(float8, integer, boolean) RETURNS half
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION half_to_float8(half, integer, boolean) RETURNS float8
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION integer_to_half(integer, integer, boolean) RETURNS half
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
@@ -364,6 +370,12 @@ CREATE CAST (real AS half)
CREATE CAST (half AS real)
WITH FUNCTION half_to_float4(half, integer, boolean) AS IMPLICIT;
CREATE CAST (float8 AS half)
WITH FUNCTION float8_to_half(float8, integer, boolean) AS IMPLICIT;
CREATE CAST (half AS float8)
WITH FUNCTION half_to_float8(half, integer, boolean) AS IMPLICIT;
CREATE CAST (integer AS half)
WITH FUNCTION integer_to_half(integer, integer, boolean) AS IMPLICIT;

View File

@@ -298,6 +298,16 @@ Float4ToHalf(float num)
return result;
}
/*
* Convert a float8 to a half
*/
static half
Float8ToHalf(double num)
{
/* TODO Convert directly for greater accuracy */
return Float4ToHalf((float) num);
}
/*
* Convert textual representation to internal representation
*/
@@ -466,6 +476,32 @@ half_to_float4(PG_FUNCTION_ARGS)
PG_RETURN_FLOAT4(f);
}
/*
* Convert float8 to half
*/
PGDLLEXPORT PG_FUNCTION_INFO_V1(float8_to_half);
Datum
float8_to_half(PG_FUNCTION_ARGS)
{
float8 d = PG_GETARG_FLOAT8(0);
half h = Float8ToHalf(d);
PG_RETURN_HALF(h);
}
/*
* Convert half to float8
*/
PGDLLEXPORT PG_FUNCTION_INFO_V1(half_to_float8);
Datum
half_to_float8(PG_FUNCTION_ARGS)
{
half h = PG_GETARG_HALF(0);
float f = HalfToFloat4(h);
PG_RETURN_FLOAT8((double) f);
}
/*
* Get the L2 distance between half arrays
*/

View File

@@ -94,6 +94,18 @@ SELECT '1.5'::real::half;
1.5
(1 row)
SELECT '1.5'::half::double precision;
float8
--------
1.5
(1 row)
SELECT '1.5'::double precision::half;
half
------
1.5
(1 row)
SELECT '1.5'::half::numeric;
numeric
---------

View File

@@ -21,6 +21,9 @@ SELECT 'Infinity'::real::half;
SELECT '1.5'::half::real;
SELECT '1.5'::real::half;
SELECT '1.5'::half::double precision;
SELECT '1.5'::double precision::half;
SELECT '1.5'::half::numeric;
SELECT '1.5'::numeric::half;