mirror of
https://github.com/pgvector/pgvector.git
synced 2026-06-06 05:51:21 +08:00
Added casts between half to double precision [skip ci]
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
36
src/half.c
36
src/half.c
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
---------
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user