Skip to content

Commit

Permalink
crnlib: fix CodeQL report cpp/*-multiplication-cast-to-*
Browse files Browse the repository at this point in the history
  • Loading branch information
illwieckz committed Jul 6, 2024
1 parent a386984 commit 999be62
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 22 deletions.
8 changes: 4 additions & 4 deletions crnlib/crn_clusterizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class clusterizer {
root.m_total_weight += weight;
root.m_vectors.push_back(i);

ttsum += v.dot(v) * weight;
ttsum += (double)v.dot(v) * (double)weight;
}

root.m_variance = (float)(ttsum - (root.m_centroid.dot(root.m_centroid) / root.m_total_weight));
Expand Down Expand Up @@ -409,7 +409,7 @@ class clusterizer {
double sum = 0;

for (uint j = 0; j < N; j++)
sum += axis[j] * covar[i][j];
sum += static_cast<double>(axis[j]) * static_cast<double>(covar[i][j]);

x[i] = static_cast<float>(sum);

Expand Down Expand Up @@ -629,14 +629,14 @@ class clusterizer {
new_left_child += (v * (float)weight);
left_weight += weight;

left_ttsum += v.dot(v) * weight;
left_ttsum += (double)v.dot(v) * (double)weight;
} else {
m_right_children.push_back(parent_node.m_vectors[i]);

new_right_child += (v * (float)weight);
right_weight += weight;

right_ttsum += v.dot(v) * weight;
right_ttsum += (double)v.dot(v) * (double)weight;
}
}

Expand Down
6 changes: 3 additions & 3 deletions crnlib/crn_dxt1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ void dxt1_endpoint_optimizer::compute_pca(vec3F& axis, const vec3F_array& norm_c
double cov[6] = {0, 0, 0, 0, 0, 0};
for (uint i = 0; i < norm_colors.size(); i++) {
const vec3F& v = norm_colors[i];
float r = v[0];
float g = v[1];
float b = v[2];
double r = (double)v[0];
double g = (double)v[1];
double b = (double)v[2];
if (m_unique_colors[i].m_weight > 1) {
const double weight = m_unique_colors[i].m_weight;
cov[0] += r * r * weight;
Expand Down
2 changes: 1 addition & 1 deletion crnlib/crn_dxt_fast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ static bool refine_endpoints(uint n, const color_quad_u8* pBlock, uint& low16, u
for (uint i = 0; i < n; i++) {
const color_quad_u8& p = pBlock[i];

int e = v[pSelectors[i]] - p[axis];
uint64 e = v[pSelectors[i]] - p[axis];

axis_error += e * e;

Expand Down
4 changes: 2 additions & 2 deletions crnlib/crn_image_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,9 @@ bool resample_multithreaded(const image_u8& src, image_u8& dst, const resample_p
return false;

p.m_pSrc_pixels = src_samples.get_ptr();
p.m_src_pitch = src_width * resampler_comps * sizeof(float);
p.m_src_pitch = (size_t)src_width * (size_t)resampler_comps * sizeof(float);
p.m_pDst_pixels = dst_samples.get_ptr();
p.m_dst_pitch = dst_width * resampler_comps * sizeof(float);
p.m_dst_pitch = (size_t)dst_width * (size_t)resampler_comps * sizeof(float);

for (uint src_y = 0; src_y < src_height; src_y++) {
const color_quad_u8* pSrc = src.get_scanline(src_y);
Expand Down
4 changes: 2 additions & 2 deletions crnlib/crn_jpgd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2441,7 +2441,7 @@ jpeg_decoder::coeff_buf* jpeg_decoder::coeff_buf_open(int block_num_x, int block
cb->block_len_x = block_len_x;
cb->block_len_y = block_len_y;
cb->block_size = (block_len_x * block_len_y) * sizeof(jpgd_block_t);
cb->pData = (uint8*)alloc(cb->block_size * block_num_x * block_num_y, true);
cb->pData = (uint8*)alloc((size_t)cb->block_size * (size_t)block_num_x * (size_t)block_num_y, true);
return cb;
}

Expand Down Expand Up @@ -2870,7 +2870,7 @@ unsigned char* decompress_jpeg_image_from_stream(jpeg_decoder_stream* pStream, i

const int dst_bpl = image_width * req_comps;

uint8* pImage_data = (uint8*)jpgd_malloc(dst_bpl * image_height);
uint8* pImage_data = (uint8*)jpgd_malloc((size_t)dst_bpl * (size_t)image_height);
if (!pImage_data)
return NULL;

Expand Down
2 changes: 1 addition & 1 deletion crnlib/crn_jpge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ bool jpeg_encoder::jpg_open(int p_x_res, int p_y_res, int src_channels) {
m_image_bpl_mcu = m_image_x_mcu * m_num_components;
m_mcus_per_row = m_image_x_mcu / m_mcu_x;

if ((m_mcu_lines[0] = static_cast<uint8*>(jpge_malloc(m_image_bpl_mcu * m_mcu_y))) == NULL)
if ((m_mcu_lines[0] = static_cast<uint8*>(jpge_malloc((size_t)m_image_bpl_mcu * (size_t)m_mcu_y))) == NULL)
return false;
for (int i = 1; i < m_mcu_y; i++)
m_mcu_lines[i] = m_mcu_lines[i - 1] + m_image_bpl_mcu;
Expand Down
2 changes: 1 addition & 1 deletion crnlib/crn_mipmapped_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2763,7 +2763,7 @@ bool mipmapped_texture::read_crn_from_memory(const void* pData, uint data_size,

if (!pDXT_image->init(
dxt_fmt, level_width, level_height,
num_blocks_x * num_blocks_y * (tex_info.m_bytes_per_block / sizeof(dxt_image::element)),
(size_t)num_blocks_x * (size_t)num_blocks_y * (tex_info.m_bytes_per_block / sizeof(dxt_image::element)),
reinterpret_cast<dxt_image::element*>(pFaces[f]), true)) {
crnlib_delete(pDXT_image);

Expand Down
2 changes: 1 addition & 1 deletion crnlib/crn_threaded_clusterizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class threaded_clusterizer {
double sum = 0;

for (uint j = 0; j < N; j++)
sum += axis[j] * covar[i][j];
sum += static_cast<double>(axis[j]) * static_cast<double>(covar[i][j]);

x[i] = static_cast<float>(sum);

Expand Down
8 changes: 4 additions & 4 deletions crnlib/crn_tree_clusterizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class tree_clusterizer {
m_weightedVectors[i] = v * (float)weight;
root.m_centroid += m_weightedVectors[i];
root.m_total_weight += weight;
m_weightedDotProducts[i] = v.dot(v) * weight;
m_weightedDotProducts[i] = (double)v.dot(v) * (double)weight;
ttsum += m_weightedDotProducts[i];
}
root.m_variance = (float)(ttsum - (root.m_centroid.dot(root.m_centroid) / root.m_total_weight));
Expand Down Expand Up @@ -289,7 +289,7 @@ class tree_clusterizer {
double sum = 0;

for (uint j = 0; j < N; j++)
sum += axis[j] * covar[i][j];
sum += (double)axis[j] * (double)covar[i][j];

x[i] = (float)sum;

Expand Down Expand Up @@ -464,7 +464,7 @@ void split_vectors(VectorType (&vectors)[64], uint (&weights)[64], uint size, Ve
weightedVectors[i] = v * (float)weight;
centroid += weightedVectors[i];
total_weight += weight;
weightedDotProducts[i] = v.dot(v) * weight;
weightedDotProducts[i] = (double)v.dot(v) * (double)weight;
ttsum += weightedDotProducts[i];
}
float variance = (float)(ttsum - (centroid.dot(centroid) / total_weight));
Expand Down Expand Up @@ -520,7 +520,7 @@ void split_vectors(VectorType (&vectors)[64], uint (&weights)[64], uint size, Ve
for (uint i = 0; i < N; i++) {
double sum = 0;
for (uint j = 0; j < N; j++)
sum += axis[j] * covar[i][j];
sum += (double)axis[j] * (double)covar[i][j];
x[i] = (float)sum;
max_sum = i ? math::maximum(max_sum, sum) : sum;
}
Expand Down
6 changes: 3 additions & 3 deletions crnlib/crn_vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,9 @@ class vec : public helpers::rel_ops<vec<N, T> > {
}

inline double normalize(const vec* pDefaultVec = NULL) {
double n = m_s[0] * m_s[0];
double n = (double)m_s[0] * (double)m_s[0];
for (uint i = 1; i < N; i++)
n += m_s[i] * m_s[i];
n += (double)m_s[i] * (double)m_s[i];

if (n != 0)
*this *= static_cast<T>((1.0f / sqrt(n)));
Expand All @@ -489,7 +489,7 @@ class vec : public helpers::rel_ops<vec<N, T> > {
inline double normalize3(const vec* pDefaultVec = NULL) {
CRNLIB_ASSUME(N >= 3);

double n = m_s[0] * m_s[0] + m_s[1] * m_s[1] + m_s[2] * m_s[2];
double n = (double)m_s[0] * (double)m_s[0] + (double)m_s[1] * (double)m_s[1] + (double)m_s[2] * (double)m_s[2];

if (n != 0)
*this *= static_cast<T>((1.0f / sqrt(n)));
Expand Down

0 comments on commit 999be62

Please sign in to comment.