From 4b630d4f278d234976575f572d5ff8b86c7b1bbe Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Mon, 4 Dec 2023 12:27:13 -0800 Subject: [PATCH] Added cast from half to real [skip ci] --- sql/vector--0.5.1--0.6.0.sql | 6 ++++++ sql/vector.sql | 6 ++++++ src/half.c | 13 +++++++++++++ test/expected/half.out | 12 ++++++++++++ test/sql/half.sql | 3 +++ 5 files changed, 40 insertions(+) diff --git a/sql/vector--0.5.1--0.6.0.sql b/sql/vector--0.5.1--0.6.0.sql index 61348fa..5687c0a 100644 --- a/sql/vector--0.5.1--0.6.0.sql +++ b/sql/vector--0.5.1--0.6.0.sql @@ -46,6 +46,9 @@ CREATE FUNCTION half_negative_inner_product(half[], half[]) RETURNS float8 CREATE FUNCTION float4_to_half(real, integer, boolean) RETURNS half AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; +CREATE FUNCTION half_to_float4(half, integer, boolean) RETURNS real + 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; @@ -55,6 +58,9 @@ CREATE FUNCTION numeric_to_half(numeric, integer, boolean) RETURNS half CREATE CAST (real AS half) WITH FUNCTION float4_to_half(real, integer, boolean) AS IMPLICIT; +CREATE CAST (half AS real) + WITH FUNCTION half_to_float4(half, integer, boolean) AS IMPLICIT; + CREATE CAST (integer AS half) WITH FUNCTION integer_to_half(integer, integer, boolean) AS IMPLICIT; diff --git a/sql/vector.sql b/sql/vector.sql index c86d6d9..bdecf37 100644 --- a/sql/vector.sql +++ b/sql/vector.sql @@ -344,6 +344,9 @@ CREATE FUNCTION half_negative_inner_product(half[], half[]) RETURNS float8 CREATE FUNCTION float4_to_half(real, integer, boolean) RETURNS half AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; +CREATE FUNCTION half_to_float4(half, integer, boolean) RETURNS real + 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; @@ -355,6 +358,9 @@ CREATE FUNCTION numeric_to_half(numeric, integer, boolean) RETURNS half CREATE CAST (real AS half) WITH FUNCTION float4_to_half(real, integer, boolean) AS IMPLICIT; +CREATE CAST (half AS real) + WITH FUNCTION half_to_float4(half, integer, boolean) AS IMPLICIT; + CREATE CAST (integer AS half) WITH FUNCTION integer_to_half(integer, integer, boolean) AS IMPLICIT; diff --git a/src/half.c b/src/half.c index 96a3100..3991ab0 100644 --- a/src/half.c +++ b/src/half.c @@ -439,6 +439,19 @@ float4_to_half(PG_FUNCTION_ARGS) PG_RETURN_HALF(h); } +/* + * Convert half to float4 + */ +PGDLLEXPORT PG_FUNCTION_INFO_V1(half_to_float4); +Datum +half_to_float4(PG_FUNCTION_ARGS) +{ + half h = PG_GETARG_HALF(0); + float f = HalfToFloat4(h); + + PG_RETURN_FLOAT4(f); +} + /* * Get the L2 distance between half arrays */ diff --git a/test/expected/half.out b/test/expected/half.out index 5e54327..ae1e391 100644 --- a/test/expected/half.out +++ b/test/expected/half.out @@ -76,6 +76,18 @@ SELECT 'Infinity'::real::half; Infinity (1 row) +SELECT '1.5'::half::real; + float4 +-------- + 1.5 +(1 row) + +SELECT '{1.5}'::half[]::real[]; + float4 +-------- + {1.5} +(1 row) + SELECT l2_distance('{0,0}'::half[], '{3,4}'::half[]); l2_distance ------------- diff --git a/test/sql/half.sql b/test/sql/half.sql index 3db34ba..166cd23 100644 --- a/test/sql/half.sql +++ b/test/sql/half.sql @@ -17,6 +17,9 @@ SELECT '65505'::integer::half; SELECT 'NaN'::real::half; SELECT 'Infinity'::real::half; +SELECT '1.5'::half::real; +SELECT '{1.5}'::half[]::real[]; + SELECT l2_distance('{0,0}'::half[], '{3,4}'::half[]); SELECT l2_distance('{0,0}'::half[], '{0,1}'::half[]); SELECT l2_distance('{1,2}'::half[], '{3}'::half[]);