From 15c3039ba54f45c13d6513a6c484271d7ec7d669 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 2 Oct 2013 08:25:03 +0000 Subject: [PATCH] misc: fix some coding errors found by Coverity. --- src/base/string.cpp | 12 +++----- src/gpu/texture.cpp | 68 +++++++++++++++++++++++++++++++---------- src/sys/timer.cpp | 22 +++++++++++++ test/benchmark/half.cpp | 21 +++++++------ 4 files changed, 90 insertions(+), 33 deletions(-) diff --git a/src/base/string.cpp b/src/base/string.cpp index 949d3861..0dbc4e59 100644 --- a/src/base/string.cpp +++ b/src/base/string.cpp @@ -46,20 +46,18 @@ String String::Printf(char const *format, va_list ap) String ret; - /* Visual Studio 2010 does not support it va_copy. */ -#if defined _MSC_VER -# undef va_copy -# define va_copy(dst, src) (dst = src) -#endif va_list ap2; +#if !defined _MSC_VER + /* Visual Studio 2010 does not support va_copy. */ va_copy(ap2, ap); -#if defined _MSC_VER -# undef va_copy +#else + ap2 = ap; #endif /* vsnprintf() tells us how many character we need, and we need to * add one for the terminating null byte. */ size_t needed = vsnprintf(nullptr, 0, format, ap2) + 1; + va_end(ap2); ((Super &)ret).Reserve(needed); ret.m_count = needed; diff --git a/src/gpu/texture.cpp b/src/gpu/texture.cpp index cc3d7e6b..5882faf0 100644 --- a/src/gpu/texture.cpp +++ b/src/gpu/texture.cpp @@ -250,16 +250,26 @@ void Texture::SetMagFiltering(TextureMagFilter filter) // In DirectX, texture filtering is a per-texture-unit state switch (filter) { - case TextureMagFilter::NEAREST_TEXEL: m_data->m_mag_filter = D3DTEXF_POINT; break; - case TextureMagFilter::LINEAR_TEXEL: m_data->m_mag_filter = D3DTEXF_LINEAR; break; + case TextureMagFilter::LINEAR_TEXEL: + m_data->m_mag_filter = D3DTEXF_LINEAR; + break; + case TextureMagFilter::NEAREST_TEXEL: + default: + m_data->m_mag_filter = D3DTEXF_POINT; + break; } #else glBindTexture(GL_TEXTURE_2D, m_data->m_texture); GLenum gl_filter; switch (filter) { - case TextureMagFilter::NEAREST_TEXEL: gl_filter = GL_NEAREST; break; - case TextureMagFilter::LINEAR_TEXEL: gl_filter = GL_LINEAR; break; + case TextureMagFilter::LINEAR_TEXEL: + gl_filter = GL_LINEAR; + break; + case TextureMagFilter::NEAREST_TEXEL: + default: + gl_filter = GL_NEAREST; + break; } glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter); #endif @@ -273,12 +283,25 @@ void Texture::SetMinFiltering(TextureMinFilter filter) m_data->m_min_filter = x; m_data->m_mip_filter = y; switch (filter) { - case TextureMinFilter::NEAREST_TEXEL_NO_MIPMAP: F(D3DTEXF_POINT, D3DTEXF_NONE); break; - case TextureMinFilter::LINEAR_TEXEL_NO_MIPMAP: F(D3DTEXF_POINT, D3DTEXF_NONE); break; - case TextureMinFilter::NEAREST_TEXEL_NEAREST_MIPMAP: F(D3DTEXF_POINT, D3DTEXF_POINT); break; - case TextureMinFilter::LINEAR_TEXEL_NEAREST_MIPMAP: F(D3DTEXF_LINEAR, D3DTEXF_POINT); break; - case TextureMinFilter::NEAREST_TEXEL_LINEAR_MIPMAP: F(D3DTEXF_POINT, D3DTEXF_LINEAR); break; - case TextureMinFilter::LINEAR_TEXEL_LINEAR_MIPMAP: F(D3DTEXF_LINEAR, D3DTEXF_LINEAR); break; + case TextureMinFilter::LINEAR_TEXEL_NO_MIPMAP: + F(D3DTEXF_POINT, D3DTEXF_NONE); + break; + case TextureMinFilter::NEAREST_TEXEL_NEAREST_MIPMAP: + F(D3DTEXF_POINT, D3DTEXF_POINT); + break; + case TextureMinFilter::LINEAR_TEXEL_NEAREST_MIPMAP: + F(D3DTEXF_LINEAR, D3DTEXF_POINT); + break; + case TextureMinFilter::NEAREST_TEXEL_LINEAR_MIPMAP: + F(D3DTEXF_POINT, D3DTEXF_LINEAR); + break; + case TextureMinFilter::LINEAR_TEXEL_LINEAR_MIPMAP: + F(D3DTEXF_LINEAR, D3DTEXF_LINEAR); + break; + case TextureMinFilter::NEAREST_TEXEL_NO_MIPMAP: + default: + F(D3DTEXF_POINT, D3DTEXF_NONE); + break; } #undef F @@ -287,12 +310,25 @@ void Texture::SetMinFiltering(TextureMinFilter filter) GLenum gl_filter; switch (filter) { - case TextureMinFilter::NEAREST_TEXEL_NO_MIPMAP: gl_filter = GL_NEAREST; break; - case TextureMinFilter::LINEAR_TEXEL_NO_MIPMAP: gl_filter = GL_LINEAR; break; - case TextureMinFilter::NEAREST_TEXEL_NEAREST_MIPMAP: gl_filter = GL_NEAREST_MIPMAP_NEAREST; break; - case TextureMinFilter::NEAREST_TEXEL_LINEAR_MIPMAP: gl_filter = GL_NEAREST_MIPMAP_LINEAR; break; - case TextureMinFilter::LINEAR_TEXEL_NEAREST_MIPMAP: gl_filter = GL_LINEAR_MIPMAP_NEAREST; break; - case TextureMinFilter::LINEAR_TEXEL_LINEAR_MIPMAP: gl_filter = GL_LINEAR_MIPMAP_LINEAR; break; + case TextureMinFilter::LINEAR_TEXEL_NO_MIPMAP: + gl_filter = GL_LINEAR; + break; + case TextureMinFilter::NEAREST_TEXEL_NEAREST_MIPMAP: + gl_filter = GL_NEAREST_MIPMAP_NEAREST; + break; + case TextureMinFilter::NEAREST_TEXEL_LINEAR_MIPMAP: + gl_filter = GL_NEAREST_MIPMAP_LINEAR; + break; + case TextureMinFilter::LINEAR_TEXEL_NEAREST_MIPMAP: + gl_filter = GL_LINEAR_MIPMAP_NEAREST; + break; + case TextureMinFilter::LINEAR_TEXEL_LINEAR_MIPMAP: + gl_filter = GL_LINEAR_MIPMAP_LINEAR; + break; + case TextureMinFilter::NEAREST_TEXEL_NO_MIPMAP: + default: + gl_filter = GL_NEAREST; + break; } glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter); #endif diff --git a/src/sys/timer.cpp b/src/sys/timer.cpp index 164aaa97..a4ff08d3 100644 --- a/src/sys/timer.cpp +++ b/src/sys/timer.cpp @@ -53,11 +53,18 @@ class TimerData private: TimerData() { + Init(); (void)GetSeconds(true); } #if __linux__ || __native_client__ || __APPLE__ \ || (HAVE_GETTIMEOFDAY && HAVE_USLEEP) + inline void Init() + { + m_tv.tv_usec = 0; + m_tv.tv_sec = 0; + } + float GetSeconds(bool reset) { struct timeval tv, tv0 = m_tv; @@ -76,6 +83,11 @@ private: struct timeval m_tv; #elif _WIN32 + inline void Init() + { + m_cycles.QuadPart = 0; + } + float GetSeconds(bool reset) { static float secs_per_cycle = GetSecondsPerCycle(); @@ -102,6 +114,11 @@ private: LARGE_INTEGER m_cycles; #elif __CELLOS_LV2__ + inline void Init() + { + m_cycles = 0; + } + float GetSeconds(bool reset) { static float secs_per_cycle = GetSecondsPerCycle(); @@ -126,6 +143,11 @@ private: uint64_t m_cycles; #else + inline void Init() + { + m_ticks = 0; + } + float GetSeconds(bool reset) { static bool initialised = Init(); diff --git a/test/benchmark/half.cpp b/test/benchmark/half.cpp index 400a93f6..d52cb6e2 100644 --- a/test/benchmark/half.cpp +++ b/test/benchmark/half.cpp @@ -40,26 +40,27 @@ void bench_half(int mode) ph[i] = half::makebits(rand()); break; case 2: + default: for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++) ph[i] = rand(-2.0f, 2.0f); break; } - /* Copy float */ - timer.Get(); - for (size_t i = 0; i < HALF_TABLE_SIZE; i++) - pf[i] = pf[i + 1]; - result[0] += timer.Get(); - /* Convert half to float (array) */ timer.Get(); half::convert(pf, ph, HALF_TABLE_SIZE); - result[1] += timer.Get(); + result[0] += timer.Get(); /* Convert half to float (fast) */ timer.Get(); for (size_t i = 0; i < HALF_TABLE_SIZE; i++) pf[i] = (float)ph[i]; + result[1] += timer.Get(); + + /* Copy float */ + timer.Get(); + for (size_t i = 0; i < HALF_TABLE_SIZE; i++) + pf[i] = pf[i + 1]; result[2] += timer.Get(); /* Add a half to every float */ @@ -111,9 +112,9 @@ void bench_half(int mode) result[i] *= 1e9f / (HALF_TABLE_SIZE * HALF_RUNS); Log::Info(" ns/elem\n"); - Log::Info("float = float %7.3f\n", result[0]); - Log::Info("float = half (array) %7.3f\n", result[1]); - Log::Info("float = half (fast) %7.3f\n", result[2]); + Log::Info("float = half (array) %7.3f\n", result[0]); + Log::Info("float = half (fast) %7.3f\n", result[1]); + Log::Info("float = float %7.3f\n", result[2]); Log::Info("float += half %7.3f\n", result[3]); Log::Info("half = half %7.3f\n", result[4]); Log::Info("half = -half %7.3f\n", result[5]);