pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: higuoxing (@higuoxing) <noreply+higuoxing@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] PR #639: Combine overflow checks and computation steps.
Date: Thu, 08 Aug 2024 03:08:33 +0000
Message-ID: <gh-pgvector-pgvector-639@github.com> (raw)

Some of the overflow checks can be combined with the computation step and can save some loops.

inline patch 1: src/halfvec.c, src/vector.c (+13/-30 lines) download inline-1.patchdiff --git a/src/halfvec.c b/src/halfvec.c index 2cb1127ff..be006e5cf 100644 --- a/src/halfvec.c +++ b/src/halfvec.c @@ -753,11 +753,10 @@ halfvec_l2_normalize(PG_FUNCTION_ARGS) if (norm > 0) { for (int i = 0; i < a->dim; i++) + { rx[i] = Float4ToHalfUnchecked(HalfToFloat4(ax[i]) / norm); - /* Check for overflow */ - for (int i = 0; i < a->dim; i++) - { + /* Check for overflow */ if (HalfIsInf(rx[i])) float_overflow_error(); } @@ -793,11 +792,7 @@ halfvec_add(PG_FUNCTION_ARGS) #else rx[i] = Float4ToHalfUnchecked(HalfToFloat4(ax[i]) + HalfToFloat4(bx[i])); #endif - } - - /* Check for overflow */ - for (int i = 0, imax = a->dim; i < imax; i++) - { + /* Check for overflow */ if (HalfIsInf(rx[i])) float_overflow_error(); } @@ -832,11 +827,7 @@ halfvec_sub(PG_FUNCTION_ARGS) #else rx[i] = Float4ToHalfUnchecked(HalfToFloat4(ax[i]) - HalfToFloat4(bx[i])); #endif - } - - /* Check for overflow */ - for (int i = 0, imax = a->dim; i < imax; i++) - { + /* Check for overflow */ if (HalfIsInf(rx[i])) float_overflow_error(); } @@ -871,11 +862,7 @@ halfvec_mul(PG_FUNCTION_ARGS) #else rx[i] = Float4ToHalfUnchecked(HalfToFloat4(ax[i]) * HalfToFloat4(bx[i])); #endif - } - - /* Check for overflow and underflow */ - for (int i = 0, imax = a->dim; i < imax; i++) - { + /* Check for overflow and underflow */ if (HalfIsInf(rx[i])) float_overflow_error(); diff --git a/src/vector.c b/src/vector.c index a0e2c3fb1..7619a2268 100644 --- a/src/vector.c +++ b/src/vector.c @@ -813,11 +813,10 @@ l2_normalize(PG_FUNCTION_ARGS) if (norm > 0) { for (int i = 0; i < a->dim; i++) + { rx[i] = ax[i] / norm; - /* Check for overflow */ - for (int i = 0; i < a->dim; i++) - { + /* Check for overflow */ if (isinf(rx[i])) float_overflow_error(); } @@ -847,11 +846,10 @@ vector_add(PG_FUNCTION_ARGS) /* Auto-vectorized */ for (int i = 0, imax = a->dim; i < imax; i++) + { rx[i] = ax[i] + bx[i]; - /* Check for overflow */ - for (int i = 0, imax = a->dim; i < imax; i++) - { + /* Check for overflow */ if (isinf(rx[i])) float_overflow_error(); } @@ -880,11 +878,10 @@ vector_sub(PG_FUNCTION_ARGS) /* Auto-vectorized */ for (int i = 0, imax = a->dim; i < imax; i++) + { rx[i] = ax[i] - bx[i]; - /* Check for overflow */ - for (int i = 0, imax = a->dim; i < imax; i++) - { + /* Check for overflow */ if (isinf(rx[i])) float_overflow_error(); } @@ -913,11 +910,10 @@ vector_mul(PG_FUNCTION_ARGS) /* Auto-vectorized */ for (int i = 0, imax = a->dim; i < imax; i++) + { rx[i] = ax[i] * bx[i]; - /* Check for overflow and underflow */ - for (int i = 0, imax = a->dim; i < imax; i++) - { + /* Check for overflow and underflow */ if (isinf(rx[i])) float_overflow_error();

view thread (3+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-639@github.com>
Permalink:  ../gh-pgvector-pgvector-639@github.com/

 ·  · 


This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox