Skip to content

Commit

Permalink
Correct various illegal type puns
Browse files Browse the repository at this point in the history
Using reinterpret_cast for type punning (not allowed by the C++ standard)
is not supported by GCC which notoriously miscompiles such code at -O2.

Bug: 819294
Change-Id: I19a3e20a966ff590b9f3beb40984b6fc4fcc10db
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4328253
Auto-Submit: Bruno Pitrus <brunopitrus@hotmail.com>
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Commit-Queue: Kinuko Yasuda <kinuko@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1119713}
  • Loading branch information
brjsp authored and Chromium LUCI CQ committed Mar 21, 2023
1 parent e0f76ca commit ad4d52b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
5 changes: 4 additions & 1 deletion third_party/blink/renderer/modules/xr/xr_cube_map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "third_party/blink/renderer/modules/xr/xr_cube_map.h"

#include <cstring>

#include "base/cxx17_backports.h"
#include "device/vr/public/mojom/vr_service.mojom-blink.h"
#include "third_party/blink/renderer/modules/webgl/webgl_rendering_context_base.h"
Expand All @@ -19,7 +21,8 @@ bool IsPowerOfTwo(uint32_t value) {
// This is an inversion of FloatToHalfFloat in ui/gfx/half_float.cc
float HalfFloatToFloat(const uint16_t input) {
uint32_t tmp = (input & 0x7fff) << 13 | (input & 0x8000) << 16;
float tmp2 = *reinterpret_cast<float*>(&tmp);
float tmp2;
std::memcpy(&tmp2, &tmp, 4);
return tmp2 / 1.9259299444e-34f;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <algorithm>
#include <cmath>
#include <cstring>

#include "base/check_op.h"
#include "third_party/blink/renderer/platform/audio/audio_array.h"
Expand Down Expand Up @@ -208,13 +209,14 @@ void Vclip(const float* source_p,
// source_max = max(abs(source[k])) for all k
void Vmaxmgv(const float* source_p, float* max_p, uint32_t frames_to_process) {
constexpr uint32_t kMask = 0x7FFFFFFFu;

float kMask_float;
std::memcpy(&kMask_float, &kMask, 4);
const float* const source_end_p = source_p + frames_to_process;

DCHECK(IsAligned(source_p));
DCHECK_EQ(0u, frames_to_process % kPackedFloatsPerRegister);

MType m_mask = MM_PS(set1)(*reinterpret_cast<const float*>(&kMask));
MType m_mask = MM_PS(set1)(kMask_float);
MType m_max = MM_PS(setzero)();

while (source_p < source_end_p) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "third_party/blink/renderer/platform/graphics/gpu/webgl_image_conversion.h"

#include <cstring>
#include <limits>
#include <memory>

Expand Down Expand Up @@ -416,7 +417,8 @@ const unsigned char g_shift_table[512] = {
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 13};

uint16_t ConvertFloatToHalfFloat(float f) {
unsigned temp = *(reinterpret_cast<unsigned*>(&f));
unsigned temp;
std::memcpy(&temp, &f, 4);
uint16_t signexp = (temp >> 23) & 0x1ff;
return g_base_table[signexp] +
((temp & 0x007fffff) >> g_shift_table[signexp]);
Expand Down Expand Up @@ -834,7 +836,9 @@ float ConvertHalfFloatToFloat(uint16_t half) {
uint32_t temp =
g_mantissa_table[g_offset_table[half >> 10] + (half & 0x3ff)] +
g_exponent_table[half >> 10];
return *(reinterpret_cast<float*>(&temp));
float ret;
std::memcpy(&ret, &temp, 4);
return ret;
}

/* BEGIN CODE SHARED WITH MOZILLA FIREFOX */
Expand Down

0 comments on commit ad4d52b

Please sign in to comment.