From e6ca831f3d6924759a60b39db416a99f278de468 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 10 Apr 2024 13:59:51 -0700 Subject: [PATCH] Store very small values as zero for halfvec --- src/halfvec.c | 33 ++------------------------------- test/expected/cast.out | 8 ++++++-- test/expected/halfvec_input.out | 16 ++++++++++------ 3 files changed, 18 insertions(+), 39 deletions(-) diff --git a/src/halfvec.c b/src/halfvec.c index 010c03f..4faea8f 100644 --- a/src/halfvec.c +++ b/src/halfvec.c @@ -53,19 +53,6 @@ HalfIsInf(half num) #endif } -/* - * Check if half is zero - */ -static inline bool -HalfIsZero(half num) -{ -#ifdef FLT16_SUPPORT - return num == 0; -#else - return (num & 0x7FFF) == 0x0000; -#endif -} - /* * Get a half from a message buffer */ @@ -304,8 +291,6 @@ Float4ToHalf(float num) (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("\"%s\" is out of range for type halfvec", buf))); } - if (unlikely(HalfIsZero(result)) && num != 0.0) - float_underflow_error(); return result; } @@ -401,16 +386,6 @@ halfvec_isspace(char ch) return false; } -#if PG_VERSION_NUM < 120003 -static pg_noinline void -float_underflow_error(void) -{ - ereport(ERROR, - (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), - errmsg("value out of range: underflow"))); -} -#endif - /* * Convert textual representation to internal representation */ @@ -470,7 +445,7 @@ halfvec_in(PG_FUNCTION_ARGS) x[dim] = Float4ToHalfUnchecked(val); - if ((errno == ERANGE && (isinf(val) || val == 0)) || (HalfIsInf(x[dim]) && !isinf(val)) || (HalfIsZero(x[dim]) && val != 0)) + if ((errno == ERANGE && isinf(val)) || (HalfIsInf(x[dim]) && !isinf(val))) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("\"%s\" is out of range for type halfvec", pt))); @@ -792,11 +767,7 @@ vector_to_halfvec(PG_FUNCTION_ARGS) result = InitHalfVector(vec->dim); for (int i = 0; i < vec->dim; i++) - { - result->x[i] = Float4ToHalfUnchecked(vec->x[i]); - /* TODO Better error for overflow */ - CheckElement(result->x[i]); - } + result->x[i] = Float4ToHalf(vec->x[i]); PG_RETURN_POINTER(result); } diff --git a/test/expected/cast.out b/test/expected/cast.out index 2286c58..0b60d3a 100644 --- a/test/expected/cast.out +++ b/test/expected/cast.out @@ -75,7 +75,7 @@ SELECT '[1,2,3]'::vector::halfvec(3); SELECT '[1,2,3]'::vector::halfvec(2); ERROR: expected 2 dimensions, not 3 SELECT '[65520]'::vector::halfvec; -ERROR: infinite value not allowed in halfvec +ERROR: "65520" is out of range for type halfvec SELECT '[1e-8]'::vector::halfvec; halfvec --------- @@ -113,7 +113,11 @@ ERROR: expected 2 dimensions, not 3 SELECT '{65520,-65520}'::real[]::halfvec; ERROR: "65520" is out of range for type halfvec SELECT '{1e-8,-1e-8}'::real[]::halfvec; -ERROR: value out of range: underflow + halfvec +--------- + [0,-0] +(1 row) + SELECT '[0,1.5,0,3.5,0]'::vector::sparsevec; sparsevec ----------------- diff --git a/test/expected/halfvec_input.out b/test/expected/halfvec_input.out index 81ce5ee..74442c9 100644 --- a/test/expected/halfvec_input.out +++ b/test/expected/halfvec_input.out @@ -55,17 +55,21 @@ ERROR: "65520" is out of range for type halfvec LINE 1: SELECT '[65520,-65520]'::halfvec; ^ SELECT '[1e-8,-1e-8]'::halfvec; -ERROR: "1e-8" is out of range for type halfvec -LINE 1: SELECT '[1e-8,-1e-8]'::halfvec; - ^ + halfvec +--------- + [0,-0] +(1 row) + SELECT '[4e38,1]'::halfvec; ERROR: "4e38" is out of range for type halfvec LINE 1: SELECT '[4e38,1]'::halfvec; ^ SELECT '[1e-46,1]'::halfvec; -ERROR: "1e-46" is out of range for type halfvec -LINE 1: SELECT '[1e-46,1]'::halfvec; - ^ + halfvec +--------- + [0,1] +(1 row) + SELECT '[1,2,3'::halfvec; ERROR: malformed halfvec literal: "[1,2,3" LINE 1: SELECT '[1,2,3'::halfvec;