Skip to content

Commit

Permalink
gtests: test_sum: avoid potential overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
dzarukin committed Jan 13, 2025
1 parent ba5a271 commit 9d9b21a
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions tests/gtests/test_sum.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2016-2024 Intel Corporation
* Copyright 2016-2025 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -249,10 +249,13 @@ class sum_test_t : public ::testing::TestWithParam<sum_test_params> {
int mant_digits
= dnnl::impl::nstl::numeric_limits<src_data_t>::digits;
int want_mant_digits = 3;
int digits_shift = mant_digits - want_mant_digits;
uint_type max_val = (uint_type)-1;
// Move left to keep mask value in uint_type range, move left
// to flush all digits but `want_mant_digits`.
uint_type mask = (max_val >> digits_shift) << digits_shift;
auto src_ptr = map_memory<src_data_t>(src_memory);
for (size_t i = 0; i < sz; i++) {
uint_type mask = (uint_type)-1
<< (mant_digits - want_mant_digits);
*((uint_type *)&src_ptr[i]) &= mask;
}
}
Expand Down

0 comments on commit 9d9b21a

Please sign in to comment.