Skip to content

Commit

Permalink
8297539: Use PrimitiveConversions::cast for local uses of the int<->f…
Browse files Browse the repository at this point in the history
…loat union conversion trick

Reviewed-by: coleenp, kbarrett, dholmes
  • Loading branch information
afshin-zafari authored and David Holmes committed Apr 1, 2023
1 parent 8eb4e7e commit a19b28a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 48 deletions.
11 changes: 4 additions & 7 deletions src/hotspot/cpu/aarch64/assembler_aarch64.cpp
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<float>(ival);
}

address Assembler::locate_next_instruction(address inst) {
Expand Down
10 changes: 4 additions & 6 deletions src/hotspot/cpu/aarch64/immediate_aarch64.cpp
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -26,8 +27,9 @@
#include <stdint.h>

#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
Expand Down Expand Up @@ -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<uint32_t>(immediate);
uint32_t s, r, f, res;
// sign bit is 31
s = (val >> 31) & 0x1;
Expand Down
32 changes: 13 additions & 19 deletions src/hotspot/cpu/arm/assembler_arm.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -279,37 +279,31 @@ class VFP {
class float_num : public fpnum {
public:
float_num(float v) {
_num.val = v;
_bits = PrimitiveConversions::cast<unsigned int>(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<uint64_t>(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
Expand Down
9 changes: 3 additions & 6 deletions src/hotspot/cpu/arm/macroAssembler_arm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<jint>(c);

flds(fd, Address(PC), cond);
b(skip_constant);
emit_int32(accessor.i);
emit_int32(float_bits);
bind(skip_constant);
}

Expand Down
19 changes: 9 additions & 10 deletions src/hotspot/share/runtime/sharedRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<juint>(x);
juint ybits = PrimitiveConversions::cast<juint>(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));
Expand All @@ -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<julong>(x);
julong ybits = PrimitiveConversions::cast<julong>(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));
Expand Down

1 comment on commit a19b28a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.