Skip to content

Commit 50d4992

Browse files
committed
RV64: Make cast from unsigned->signed int explicit in rej_uniform
This is required by `-Wconversion`, and is indeed worth a comment since a cast from `unsigned` to `int` can in theory overflow. Also, unify unsigned types used in `rej_uniform` to `unsigned`, and add explicit casts where the RVV intrinsics return `unsigned long`. Signed-off-by: Hanno Becker <[email protected]>
1 parent e4e90ef commit 50d4992

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

mlkem/src/native/riscv64/meta.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ static MLK_INLINE int mlk_rej_uniform_native(int16_t *r, unsigned len,
6161
const uint8_t *buf,
6262
unsigned buflen)
6363
{
64-
return mlk_rv64v_rej_uniform(r, len, buf, buflen);
64+
/* The cast from unsigned to signed integer is safe
65+
* because the return value is <= len, which we asssume
66+
* to be bound by 4096 and hence <= INT_MAX. */
67+
return (int)mlk_rv64v_rej_uniform(r, len, buf, buflen);
6568
}
6669

6770
static MLK_INLINE int mlk_poly_reduce_native(int16_t data[MLKEM_N])

mlkem/src/native/riscv64/src/rv64v_poly.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ void mlk_rv64v_poly_reduce(int16_t *r)
740740
unsigned int mlk_rv64v_rej_uniform(int16_t *r, unsigned int len,
741741
const uint8_t *buf, unsigned int buflen)
742742
{
743-
size_t n, ctr, pos;
743+
unsigned n, ctr, pos;
744744
vuint16m1_t x, y;
745745
vbool16_t lt;
746746

@@ -749,8 +749,8 @@ unsigned int mlk_rv64v_rej_uniform(int16_t *r, unsigned int len,
749749

750750
while (ctr < len && pos < buflen)
751751
{
752-
const size_t vl = __riscv_vsetvl_e16m1((buflen - pos) * 8 / 12);
753-
const size_t vl23 = (vl * 24) / 32;
752+
const unsigned vl = (unsigned)__riscv_vsetvl_e16m1((buflen - pos) * 8 / 12);
753+
const unsigned vl23 = (vl * 24) / 32;
754754

755755
const vuint16m1_t vid = __riscv_vid_v_u16m1(vl);
756756
const vuint16m1_t srl12v = __riscv_vmul_vx_u16m1(vid, 12, vl);
@@ -772,7 +772,7 @@ unsigned int mlk_rv64v_rej_uniform(int16_t *r, unsigned int len,
772772

773773
lt = __riscv_vmsltu_vx_u16m1_b16(x, MLKEM_Q, vl);
774774
y = __riscv_vcompress_vm_u16m1(x, lt, vl);
775-
n = __riscv_vcpop_m_b16(lt, vl);
775+
n = (unsigned)__riscv_vcpop_m_b16(lt, vl);
776776

777777
if (ctr + n > len)
778778
{

0 commit comments

Comments
 (0)