While the prototype of lrintf()
is long int lrintf (float);
, OP clarified in comments that they are looking for a conversion from float
to 32-bit int
.
The AVX intrinsic _mm256_cvtps_epi32 is the perfect fit for this: It provides a conversion from float
to 32-bit int
using the current rounding mode, which defaults to round-to-nearest-even for all of the software environments I am familiar with.
The output from the little test program below should look as follows:
source vector: 1.000000 1.100000 1.500000 1.900000 -1.000000 -1.100000 -1.500000 -1.900000
round to nearest: 1 1 2 2 -1 -1 -2 -2
round down: 1 1 1 1 -1 -2 -2 -2
round up: 1 2 2 2 -1 -1 -1 -1
round toward zero: 1 1 1 1 -1 -1 -1 -1
I note, however, that choosing any optimization level above -O0
gives incorrect results with my older Intel compiler, presumably because the compiler moves the __MM_SET_ROUNDING_MODE()
instances around, not respecting the implicit dependencies between those and surrounding computation. Not sure what to do about that. I already compiled with the strictest floating-point settings and also tried adding #include <fenv.h>
followed by #pragma STDC FENV_ACCESS ON
, to no avail.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "immintrin.h"
int main (void)
{
__m256 float_vec;
__m256i int_vec_rn;
__m256i int_vec_rd;
__m256i int_vec_ru;
__m256i int_vec_rz;
float arg[8] = {1.0f, 1.1f, 1.5f, 1.9f, -1.0f, -1.1f, -1.5f, -1.9f};
int32_t res[8];
unsigned int old_rm = _MM_GET_ROUNDING_MODE();
printf ("source vector: % f % f % f % f % f % f % f % f\n",
arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6], arg[7]);
memcpy (&float_vec, arg, sizeof float_vec);
_MM_SET_ROUNDING_MODE (_MM_ROUND_NEAREST);
int_vec_rn = _mm256_cvtps_epi32 (float_vec);
memcpy (res, &int_vec_rn, sizeof res);
printf ("round to nearest: % d % d % d % d % d % d % d % d\n",
res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7]);
_MM_SET_ROUNDING_MODE (_MM_ROUND_DOWN);
int_vec_rd = _mm256_cvtps_epi32 (float_vec);
memcpy (res, &int_vec_rd, sizeof res);
printf ("round down: % d % d % d % d % d % d % d % d\n",
res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7]);
_MM_SET_ROUNDING_MODE (_MM_ROUND_UP);
int_vec_ru = _mm256_cvtps_epi32 (float_vec);
memcpy (res, &int_vec_ru, sizeof res);
printf ("round up: % d % d % d % d % d % d % d %d\n",
res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7]);
_MM_SET_ROUNDING_MODE (_MM_ROUND_TOWARD_ZERO);
int_vec_rz = _mm256_cvtps_epi32 (float_vec);
memcpy (res, &int_vec_rz, sizeof res);
printf ("round toward zero: % d % d % d % d % d % d % d % d\n",
res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7]);
_MM_SET_ROUNDING_MODE (old_rm);
return EXIT_SUCCESS;
}