From a19b28ab3ed2d2da4eb04ce9b187dda8a75ba16a Mon Sep 17 00:00:00 2001 From: Afshin Zafari Date: Sat, 1 Apr 2023 01:02:05 +0000 Subject: [PATCH] 8297539: Use PrimitiveConversions::cast for local uses of the int<->float union conversion trick Reviewed-by: coleenp, kbarrett, dholmes --- src/hotspot/cpu/aarch64/assembler_aarch64.cpp | 11 +++---- src/hotspot/cpu/aarch64/immediate_aarch64.cpp | 10 +++--- src/hotspot/cpu/arm/assembler_arm.hpp | 32 ++++++++----------- src/hotspot/cpu/arm/macroAssembler_arm.cpp | 9 ++---- src/hotspot/share/runtime/sharedRuntime.cpp | 19 ++++++----- 5 files changed, 33 insertions(+), 48 deletions(-) diff --git a/src/hotspot/cpu/aarch64/assembler_aarch64.cpp b/src/hotspot/cpu/aarch64/assembler_aarch64.cpp index a3ec050e42e..df44024d997 100644 --- a/src/hotspot/cpu/aarch64/assembler_aarch64.cpp +++ b/src/hotspot/cpu/aarch64/assembler_aarch64.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2014, 2020 Red Hat Inc. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * @@ -29,6 +29,7 @@ #include "compiler/disassembler.hpp" #include "immediate_aarch64.hpp" #include "memory/resourceArea.hpp" +#include "metaprogramming/primitiveConversions.hpp" #ifndef PRODUCT const uintptr_t Assembler::asm_bp = 0x0000ffffac221240; @@ -499,12 +500,8 @@ unsigned Assembler::pack(double value) { // Packed operands for Floating-point Move (immediate) static float unpack(unsigned value) { - union { - unsigned ival; - float val; - }; - ival = fp_immediate_for_encoding(value, 0); - return val; + unsigned ival = fp_immediate_for_encoding(value, 0); + return PrimitiveConversions::cast(ival); } address Assembler::locate_next_instruction(address inst) { diff --git a/src/hotspot/cpu/aarch64/immediate_aarch64.cpp b/src/hotspot/cpu/aarch64/immediate_aarch64.cpp index a0a76d66678..3d87fde2b5b 100644 --- a/src/hotspot/cpu/aarch64/immediate_aarch64.cpp +++ b/src/hotspot/cpu/aarch64/immediate_aarch64.cpp @@ -1,4 +1,5 @@ /* + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * @@ -26,8 +27,9 @@ #include #include "precompiled.hpp" -#include "utilities/globalDefinitions.hpp" #include "immediate_aarch64.hpp" +#include "metaprogramming/primitiveConversions.hpp" +#include "utilities/globalDefinitions.hpp" // there are at most 2^13 possible logical immediate encodings // however, some combinations of immr and imms are invalid @@ -431,11 +433,7 @@ uint32_t encoding_for_fp_immediate(float immediate) // return the imm8 result [s:r:f] // - union { - float fpval; - uint32_t val; - }; - fpval = immediate; + uint32_t val = PrimitiveConversions::cast(immediate); uint32_t s, r, f, res; // sign bit is 31 s = (val >> 31) & 0x1; diff --git a/src/hotspot/cpu/arm/assembler_arm.hpp b/src/hotspot/cpu/arm/assembler_arm.hpp index 1de4b0da774..818d6a8f44b 100644 --- a/src/hotspot/cpu/arm/assembler_arm.hpp +++ b/src/hotspot/cpu/arm/assembler_arm.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -279,37 +279,31 @@ class VFP { class float_num : public fpnum { public: float_num(float v) { - _num.val = v; + _bits = PrimitiveConversions::cast(v); } - virtual unsigned int f_hi4() const { return (_num.bits << 9) >> (19+9); } - virtual bool f_lo_is_null() const { return (_num.bits & ((1 << 19) - 1)) == 0; } - virtual int e() const { return ((_num.bits << 1) >> (23+1)) - 127; } - virtual unsigned int s() const { return _num.bits >> 31; } + unsigned int f_hi4() const override { return (_bits << 9) >> (19+9); } + bool f_lo_is_null() const override { return (_bits & ((1 << 19) - 1)) == 0; } + int e() const override { return ((_bits << 1) >> (23+1)) - 127; } + unsigned int s() const override { return _bits >> 31; } private: - union { - float val; - unsigned int bits; - } _num; + unsigned int _bits; }; class double_num : public fpnum { public: double_num(double v) { - _num.val = v; + _bits = PrimitiveConversions::cast(v); } - virtual unsigned int f_hi4() const { return (_num.bits << 12) >> (48+12); } - virtual bool f_lo_is_null() const { return (_num.bits & ((1LL << 48) - 1)) == 0; } - virtual int e() const { return ((_num.bits << 1) >> (52+1)) - 1023; } - virtual unsigned int s() const { return _num.bits >> 63; } + unsigned int f_hi4() const override { return (_bits << 12) >> (48+12); } + bool f_lo_is_null() const override { return (_bits & ((1LL << 48) - 1)) == 0; } + int e() const override { return ((_bits << 1) >> (52+1)) - 1023; } + unsigned int s() const override { return _bits >> 63; } private: - union { - double val; - unsigned long long bits; - } _num; + uint64_t _bits; }; }; #endif diff --git a/src/hotspot/cpu/arm/macroAssembler_arm.cpp b/src/hotspot/cpu/arm/macroAssembler_arm.cpp index 583a36b6f29..f4e7d492e63 100644 --- a/src/hotspot/cpu/arm/macroAssembler_arm.cpp +++ b/src/hotspot/cpu/arm/macroAssembler_arm.cpp @@ -37,6 +37,7 @@ #include "interpreter/bytecodeHistogram.hpp" #include "interpreter/interpreter.hpp" #include "memory/resourceArea.hpp" +#include "metaprogramming/primitiveConversions.hpp" #include "oops/accessDecorators.hpp" #include "oops/klass.inline.hpp" #include "prims/methodHandles.hpp" @@ -673,15 +674,11 @@ void MacroAssembler::mov_metadata(Register rd, Metadata* o, int metadata_index) void MacroAssembler::mov_float(FloatRegister fd, jfloat c, AsmCondition cond) { Label skip_constant; - union { - jfloat f; - jint i; - } accessor; - accessor.f = c; + jint float_bits = PrimitiveConversions::cast(c); flds(fd, Address(PC), cond); b(skip_constant); - emit_int32(accessor.i); + emit_int32(float_bits); bind(skip_constant); } diff --git a/src/hotspot/share/runtime/sharedRuntime.cpp b/src/hotspot/share/runtime/sharedRuntime.cpp index 180ad6443db..dce7ca21b94 100644 --- a/src/hotspot/share/runtime/sharedRuntime.cpp +++ b/src/hotspot/share/runtime/sharedRuntime.cpp @@ -46,6 +46,7 @@ #include "logging/log.hpp" #include "memory/resourceArea.hpp" #include "memory/universe.hpp" +#include "metaprogramming/primitiveConversions.hpp" #include "oops/compiledICHolder.inline.hpp" #include "oops/klass.hpp" #include "oops/method.inline.hpp" @@ -242,12 +243,11 @@ JRT_LEAF(jfloat, SharedRuntime::frem(jfloat x, jfloat y)) #ifdef _WIN64 // 64-bit Windows on amd64 returns the wrong values for // infinity operands. - union { jfloat f; juint i; } xbits, ybits; - xbits.f = x; - ybits.f = y; + juint xbits = PrimitiveConversions::cast(x); + juint ybits = PrimitiveConversions::cast(y); // x Mod Infinity == x unless x is infinity - if (((xbits.i & float_sign_mask) != float_infinity) && - ((ybits.i & float_sign_mask) == float_infinity) ) { + if (((xbits & float_sign_mask) != float_infinity) && + ((ybits & float_sign_mask) == float_infinity) ) { return x; } return ((jfloat)fmod_winx64((double)x, (double)y)); @@ -258,12 +258,11 @@ JRT_END JRT_LEAF(jdouble, SharedRuntime::drem(jdouble x, jdouble y)) #ifdef _WIN64 - union { jdouble d; julong l; } xbits, ybits; - xbits.d = x; - ybits.d = y; + julong xbits = PrimitiveConversions::cast(x); + julong ybits = PrimitiveConversions::cast(y); // x Mod Infinity == x unless x is infinity - if (((xbits.l & double_sign_mask) != double_infinity) && - ((ybits.l & double_sign_mask) == double_infinity) ) { + if (((xbits & double_sign_mask) != double_infinity) && + ((ybits & double_sign_mask) == double_infinity) ) { return x; } return ((jdouble)fmod_winx64((double)x, (double)y));