Skip to content

Commit 4717d7b

Browse files
authored
Improve SimdDescrInt function descriptions in SimdLib.h
1 parent a3bc839 commit 4717d7b

1 file changed

Lines changed: 125 additions & 42 deletions

File tree

src/Simd/SimdLib.h

Lines changed: 125 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,10 +2823,25 @@ extern "C"
28232823

28242824
\fn void * SimdDescrIntInit(size_t size, size_t depth);
28252825

2826-
\short Initializes Integer Descriptor Engine.
2826+
\short Initializes Integer Descriptor Engine context.
28272827

2828-
\param [in] size - a length of original (32-bit or 16-bit) float descriptor. It must be multiple of 8. Also it must be less or equal than 32768.
2829-
\param [in] depth - a number of bits in encoded integer descriptor. Supported values: 4, 5, 6, 7, 8.
2828+
The engine context stores the parameters needed to encode float descriptors into a compact
2829+
integer representation, decode them back, and compute cosine distances directly on the
2830+
encoded form without full decoding.
2831+
2832+
Each encoded descriptor produced by this engine is a byte buffer whose layout is:
2833+
- Bytes 0.. 3: 32-bit float inverse quantization scale (1 / scale).
2834+
- Bytes 4.. 7: 32-bit float minimum value (shift) used during quantization.
2835+
- Bytes 8..11: 32-bit float precomputed sum helper for dot-product reconstruction.
2836+
- Bytes 12..15: 32-bit float precomputed L2 norm of the original float descriptor.
2837+
- Bytes 16.. N: bit-packed quantized integer values, \a depth bits per element,
2838+
packed contiguously in little-endian order.
2839+
2840+
The total byte size of the encoded buffer is returned by ::SimdDescrIntEncodedSize.
2841+
2842+
\param [in] size - a length of the original (32-bit or 16-bit float) descriptor, i.e. the number of float elements.
2843+
It must be a multiple of 8 and must not exceed 32768.
2844+
\param [in] depth - the number of bits used to represent each quantized element in the encoded descriptor. Supported values: 4, 5, 6, 7, 8.
28302845
\return a pointer to Integer Descriptor Engine context. On error it returns NULL. It must be released with using of function ::SimdRelease.
28312846
This pointer is used in functions ::SimdDescrIntEncodedSize, ::SimdDescrIntDecodedSize,
28322847
::SimdDescrIntEncode32f, ::SimdDescrIntEncode16f, ::SimdDescrIntDecode32f, ::SimdDescrIntDecode16f,
@@ -2838,132 +2853,200 @@ extern "C"
28382853

28392854
\fn size_t SimdDescrIntEncodedSize(const void* context);
28402855

2841-
\short Gets size in bytes of encoded integer descriptor.
2856+
\short Gets the size in bytes of an encoded integer descriptor produced by this engine.
2857+
2858+
The encoded descriptor consists of a 16-byte header (4 x 32-bit floats storing the inverse
2859+
quantization scale, the minimum value, a precomputed sum helper, and the precomputed L2 norm)
2860+
followed by the bit-packed quantized integer data. The total size equals
2861+
16 + ceil(size * depth / 8), where \a size and \a depth are the values passed to ::SimdDescrIntInit.
28422862

28432863
\param [in] context - a pointer to Integer Descriptor Engine context. It must be created by function ::SimdDescrIntInit and released by function ::SimdRelease.
2844-
\return size in bytes of encoded integer descriptor.
2864+
\return the size in bytes of an encoded integer descriptor.
28452865
*/
28462866
SIMD_API size_t SimdDescrIntEncodedSize(const void* context);
28472867

28482868
/*! @ingroup descrint
28492869

28502870
\fn size_t SimdDescrIntDecodedSize(const void* context);
28512871

2852-
\short Gets length of original (32-bit or 16-bit) float descriptor.
2872+
\short Gets the number of elements (floats) in the original descriptor.
2873+
2874+
This is the value of the \a size parameter that was passed to ::SimdDescrIntInit.
2875+
It equals the number of 32-bit or 16-bit float elements in the uncompressed descriptor,
2876+
and is the required length of the \a src buffer for ::SimdDescrIntEncode32f / ::SimdDescrIntEncode16f
2877+
and the \a dst buffer for ::SimdDescrIntDecode32f / ::SimdDescrIntDecode16f.
28532878

28542879
\param [in] context - a pointer to Integer Descriptor Engine context. It must be created by function ::SimdDescrIntInit and released by function ::SimdRelease.
2855-
\return length of original (32-bit or 16-bit) float descriptor.
2880+
\return the number of float elements in the original (decoded) descriptor.
28562881
*/
28572882
SIMD_API size_t SimdDescrIntDecodedSize(const void* context);
28582883

28592884
/*! @ingroup descrint
28602885

28612886
\fn void SimdDescrIntEncode32f(const void* context, const float * src, uint8_t * dst);
28622887

2863-
\short Encodes 32-bit float descriptor to integer form.
2888+
\short Encodes a 32-bit float descriptor into a compact integer representation.
2889+
2890+
The function quantizes each element of the input float array linearly into the range
2891+
[0, 2^depth - 1], where \a depth was specified at context creation. The encoding procedure:
2892+
1. Finds the minimum and maximum values of the source descriptor.
2893+
2. Computes a quantization scale: scale = (2^depth - 1) / (max - min).
2894+
3. Quantizes each element: q[i] = round((src[i] - min) * scale).
2895+
4. Packs the quantized values bit-by-bit (\a depth bits per element) into the output buffer
2896+
starting at byte offset 16.
2897+
5. Writes a 16-byte header at the beginning of \a dst containing four 32-bit floats:
2898+
inverse scale (1/scale), minimum value (min), a precomputed sum helper used for
2899+
dot-product reconstruction, and the precomputed L2 norm of the original descriptor.
2900+
2901+
The precomputed norm and sum helper in the header allow ::SimdDescrIntCosineDistance and
2902+
related functions to compute cosine distances without decoding the descriptor.
28642903

28652904
\param [in] context - a pointer to Integer Descriptor Engine context. It must be created by function ::SimdDescrIntInit and released by function ::SimdRelease.
2866-
\param [in] src - a pointer to original 32-bit float descriptor. Its length can be determined by function ::SimdDescrIntDecodedSize.
2867-
\param [out] dst - a pointer to encoded integer descriptor. Its size in bytes can be determined by function ::SimdDescrIntEncodedSize.
2905+
\param [in] src - a pointer to the input 32-bit float descriptor. The number of elements must equal the value returned by ::SimdDescrIntDecodedSize.
2906+
\param [out] dst - a pointer to the output encoded integer descriptor. The buffer size in bytes must be at least the value returned by ::SimdDescrIntEncodedSize.
28682907
*/
28692908
SIMD_API void SimdDescrIntEncode32f(const void* context, const float * src, uint8_t * dst);
28702909

28712910
/*! @ingroup descrint
28722911

28732912
\fn void SimdDescrIntEncode16f(const void* context, const uint16_t * src, uint8_t * dst);
28742913

2875-
\short Encodes 16-bit float descriptor to integer form.
2914+
\short Encodes a 16-bit float descriptor into a compact integer representation.
2915+
2916+
This function is identical in behavior to ::SimdDescrIntEncode32f except that the input
2917+
descriptor elements are 16-bit floats (half precision, stored as uint16_t). Each element is
2918+
first converted to 32-bit float internally, then quantized and packed in the same way.
2919+
The output encoded descriptor format is identical to that produced by ::SimdDescrIntEncode32f
2920+
and is fully compatible with all decode and distance functions.
28762921

28772922
\param [in] context - a pointer to Integer Descriptor Engine context. It must be created by function ::SimdDescrIntInit and released by function ::SimdRelease.
2878-
\param [in] src - a pointer to original 16-bit float descriptor. Its length can be determined by function ::SimdDescrIntDecodedSize.
2879-
\param [out] dst - a pointer to encoded integer descriptor. Its size in bytes can be determined by function ::SimdDescrIntEncodedSize.
2923+
\param [in] src - a pointer to the input 16-bit float descriptor (half precision, stored as uint16_t). The number of elements must equal the value returned by ::SimdDescrIntDecodedSize.
2924+
\param [out] dst - a pointer to the output encoded integer descriptor. The buffer size in bytes must be at least the value returned by ::SimdDescrIntEncodedSize.
28802925
*/
28812926
SIMD_API void SimdDescrIntEncode16f(const void* context, const uint16_t* src, uint8_t* dst);
28822927

28832928
/*! @ingroup descrint
28842929

28852930
\fn void SimdDescrIntDecode32f(const void* context, const uint8_t* src, float* dst);
28862931

2887-
\short Decodes integer descriptor to original 32-bit float form.
2932+
\short Decodes an integer descriptor back into a 32-bit float descriptor.
2933+
2934+
The function reconstructs the original float values from the bit-packed quantized data
2935+
using the inverse scale and minimum value stored in the 16-byte header of the encoded
2936+
descriptor. Each reconstructed element is computed as: dst[i] = q[i] * invScale + min,
2937+
where \a invScale and \a min are read from the first two 32-bit floats of \a src.
2938+
The decoded values are approximations of the original floats; precision depends on \a depth.
28882939

28892940
\param [in] context - a pointer to Integer Descriptor Engine context. It must be created by function ::SimdDescrIntInit and released by function ::SimdRelease.
2890-
\param [in] src - a pointer to encoded integer descriptor. Its size in bytes can be determined by function ::SimdDescrIntEncodedSize.
2891-
\param [out] dst - a pointer to output 32-bit float descriptor. Its length can be determined by function ::SimdDescrIntDecodedSize.
2941+
\param [in] src - a pointer to the encoded integer descriptor. The buffer size in bytes must be at least the value returned by ::SimdDescrIntEncodedSize.
2942+
\param [out] dst - a pointer to the output 32-bit float descriptor. The number of elements must equal the value returned by ::SimdDescrIntDecodedSize.
28922943
*/
28932944
SIMD_API void SimdDescrIntDecode32f(const void* context, const uint8_t* src, float* dst);
28942945

28952946
/*! @ingroup descrint
28962947

28972948
\fn void SimdDescrIntDecode16f(const void* context, const uint8_t* src, uint16_t* dst);
28982949

2899-
\short Decodes integer descriptor to original 16-bit float form.
2950+
\short Decodes an integer descriptor back into a 16-bit float descriptor.
2951+
2952+
This function is identical in behavior to ::SimdDescrIntDecode32f except that each
2953+
reconstructed element is converted from 32-bit float to 16-bit float (half precision,
2954+
stored as uint16_t) before being written to the output buffer.
29002955

29012956
\param [in] context - a pointer to Integer Descriptor Engine context. It must be created by function ::SimdDescrIntInit and released by function ::SimdRelease.
2902-
\param [in] src - a pointer to encoded integer descriptor. Its size in bytes can be determined by function ::SimdDescrIntEncodedSize.
2903-
\param [out] dst - a pointer to output 16-bit float descriptor. Its length can be determined by function ::SimdDescrIntDecodedSize.
2957+
\param [in] src - a pointer to the encoded integer descriptor. The buffer size in bytes must be at least the value returned by ::SimdDescrIntEncodedSize.
2958+
\param [out] dst - a pointer to the output 16-bit float descriptor (half precision, stored as uint16_t). The number of elements must equal the value returned by ::SimdDescrIntDecodedSize.
29042959
*/
29052960
SIMD_API void SimdDescrIntDecode16f(const void* context, const uint8_t* src, uint16_t* dst);
29062961

29072962
/*! @ingroup descrint
29082963

29092964
\fn void SimdDescrIntCosineDistance(const void* context, const uint8_t* a, const uint8_t* b, float* distance);
29102965

2911-
\short Calculates cosine distance of two integer descriptors.
2966+
\short Calculates the cosine distance between two encoded integer descriptors.
2967+
2968+
The cosine distance is defined as: distance = 1 - dot(a, b) / (||a|| * ||b||),
2969+
where \a a and \a b are treated as vectors in the original float space.
2970+
The function computes the integer dot product directly on the bit-packed data and then
2971+
reconstructs the true float dot product using the quantization scale and shift stored
2972+
in the 16-byte headers of the encoded descriptors. The L2 norms are read directly from
2973+
the precomputed values in the headers, avoiding full decoding.
2974+
The result is clamped to the range [0, 2].
29122975

2913-
\note Integer descriptor can be received with using of functions ::SimdDescrIntEncode32f or ::SimdDescrIntEncode16f. Its size in bytes is determined by function ::SimdDescrIntEncodedSize.
2976+
\note An encoded integer descriptor is produced by ::SimdDescrIntEncode32f or ::SimdDescrIntEncode16f. Its size in bytes is determined by function ::SimdDescrIntEncodedSize.
29142977

29152978
\param [in] context - a pointer to Integer Descriptor Engine context. It must be created by function ::SimdDescrIntInit and released by function ::SimdRelease.
2916-
\param [in] a - a pointer to the first integer descriptor.
2917-
\param [in] b - a pointer to the second integer descriptor.
2918-
\param [out] distance - a pointer to 32-bit float with cosine distance.
2979+
\param [in] a - a pointer to the first encoded integer descriptor.
2980+
\param [in] b - a pointer to the second encoded integer descriptor.
2981+
\param [out] distance - a pointer to a 32-bit float that receives the cosine distance in the range [0, 2].
29192982
*/
29202983
SIMD_API void SimdDescrIntCosineDistance(const void* context, const uint8_t* a, const uint8_t* b, float* distance);
29212984

29222985
/*! @ingroup descrint
29232986

29242987
\fn void SimdDescrIntCosineDistancesMxNa(const void* context, size_t M, size_t N, const uint8_t* const* A, const uint8_t* const* B, float* distances);
29252988

2926-
\short Calculates mutual cosine distance of two arrays of integer descriptor arrays.
2989+
\short Calculates all pairwise cosine distances between two sets of encoded integer descriptors (array-of-pointers form).
2990+
2991+
Computes the M x N matrix of cosine distances, where distances[i * N + j] is the cosine
2992+
distance between the i-th descriptor in \a A and the j-th descriptor in \a B.
2993+
See ::SimdDescrIntCosineDistance for the definition of cosine distance.
2994+
This variant accepts the descriptors through arrays of pointers, which allows non-contiguous
2995+
memory layouts. For contiguous storage use ::SimdDescrIntCosineDistancesMxNp instead.
2996+
The implementation automatically selects cache-friendly blocking strategies.
29272997

2928-
\note Integer descriptor can be received with using of functions ::SimdDescrIntEncode32f or ::SimdDescrIntEncode16f. Its size in bytes is determined by function ::SimdDescrIntEncodedSize.
2998+
\note An encoded integer descriptor is produced by ::SimdDescrIntEncode32f or ::SimdDescrIntEncode16f. Its size in bytes is determined by function ::SimdDescrIntEncodedSize.
29292999

29303000
\param [in] context - a pointer to Integer Descriptor Engine context. It must be created by function ::SimdDescrIntInit and released by function ::SimdRelease.
2931-
\param [in] M - a number of A arrays.
2932-
\param [in] N - a number of B arrays.
2933-
\param [in] A - a pointer to the first array with pointers to integer descriptors.
2934-
\param [in] B - a pointer to the second array with pointers to integer descriptors.
2935-
\param [out] distances - a pointer to result 32-bit float array with cosine distances. It size must be M*N.
3001+
\param [in] M - the number of descriptors in set \a A (number of rows in the output matrix).
3002+
\param [in] N - the number of descriptors in set \a B (number of columns in the output matrix).
3003+
\param [in] A - an array of M pointers, each pointing to an encoded integer descriptor.
3004+
\param [in] B - an array of N pointers, each pointing to an encoded integer descriptor.
3005+
\param [out] distances - a pointer to the output M x N matrix of 32-bit float cosine distances stored in row-major order. The buffer must hold at least M * N elements.
29363006
*/
29373007
SIMD_API void SimdDescrIntCosineDistancesMxNa(const void* context, size_t M, size_t N, const uint8_t* const* A, const uint8_t* const* B, float* distances);
29383008

29393009
/*! @ingroup descrint
29403010

29413011
\fn void SimdDescrIntCosineDistancesMxNp(const void* context, size_t M, size_t N, const uint8_t* A, const uint8_t* B, float* distances);
29423012

2943-
\short Calculates mutual cosine distance of two arrays of integer descriptors.
3013+
\short Calculates all pairwise cosine distances between two sets of encoded integer descriptors (packed/contiguous form).
29443014

2945-
\note Integer descriptor can be received with using of functions ::SimdDescrIntEncode32f or ::SimdDescrIntEncode16f. Its size in bytes is determined by function ::SimdDescrIntEncodedSize.
3015+
Computes the M x N matrix of cosine distances, where distances[i * N + j] is the cosine
3016+
distance between the i-th descriptor in \a A and the j-th descriptor in \a B.
3017+
See ::SimdDescrIntCosineDistance for the definition of cosine distance.
3018+
This variant accepts the descriptors as two flat contiguous arrays, where descriptor \a i
3019+
starts at A + i * encodedSize and descriptor \a j starts at B + j * encodedSize,
3020+
with encodedSize returned by ::SimdDescrIntEncodedSize.
3021+
For non-contiguous memory layouts use ::SimdDescrIntCosineDistancesMxNa instead.
3022+
3023+
\note An encoded integer descriptor is produced by ::SimdDescrIntEncode32f or ::SimdDescrIntEncode16f. Its size in bytes is determined by function ::SimdDescrIntEncodedSize.
29463024

29473025
\param [in] context - a pointer to Integer Descriptor Engine context. It must be created by function ::SimdDescrIntInit and released by function ::SimdRelease.
2948-
\param [in] M - a number of A arrays.
2949-
\param [in] N - a number of B arrays.
2950-
\param [in] A - a pointer to the first array with integer descriptors.
2951-
\param [in] B - a pointer to the second array with integer descriptors.
2952-
\param [out] distances - a pointer to result 32-bit float array with cosine distances. It size must be M*N.
3026+
\param [in] M - the number of descriptors in set \a A (number of rows in the output matrix).
3027+
\param [in] N - the number of descriptors in set \a B (number of columns in the output matrix).
3028+
\param [in] A - a pointer to the contiguous array of M encoded integer descriptors.
3029+
\param [in] B - a pointer to the contiguous array of N encoded integer descriptors.
3030+
\param [out] distances - a pointer to the output M x N matrix of 32-bit float cosine distances stored in row-major order. The buffer must hold at least M * N elements.
29533031
*/
29543032
SIMD_API void SimdDescrIntCosineDistancesMxNp(const void* context, size_t M, size_t N, const uint8_t* A, const uint8_t* B, float* distances);
29553033

29563034
/*! @ingroup descrint
29573035

29583036
\fn void SimdDescrIntVectorNorm(const void* context, const uint8_t* a, float* norm);
29593037

2960-
\short Calculates vector norm for integer descriptor.
3038+
\short Gets the precomputed L2 norm of an encoded integer descriptor.
3039+
3040+
The L2 norm of the original float descriptor is computed and stored in the 16-byte header
3041+
of the encoded descriptor during encoding (by ::SimdDescrIntEncode32f or ::SimdDescrIntEncode16f).
3042+
This function retrieves that precomputed value without performing any additional computation.
3043+
The norm equals the Euclidean length of the original float descriptor before quantization.
29613044

2962-
\note Integer descriptor can be received with using of functions ::SimdDescrIntEncode32f or ::SimdDescrIntEncode16f. Its size in bytes is determined by function ::SimdDescrIntEncodedSize.
3045+
\note An encoded integer descriptor is produced by ::SimdDescrIntEncode32f or ::SimdDescrIntEncode16f. Its size in bytes is determined by function ::SimdDescrIntEncodedSize.
29633046

29643047
\param [in] context - a pointer to Integer Descriptor Engine context. It must be created by function ::SimdDescrIntInit and released by function ::SimdRelease.
2965-
\param [in] a - a pointer to integer descriptor.
2966-
\param [out] norm - a pointer to result 32-bit float norm.
3048+
\param [in] a - a pointer to the encoded integer descriptor.
3049+
\param [out] norm - a pointer to a 32-bit float that receives the precomputed L2 norm of the original float descriptor.
29673050
*/
29683051
SIMD_API void SimdDescrIntVectorNorm(const void* context, const uint8_t* a, float* norm);
29693052

0 commit comments

Comments
 (0)