src/hotspot/share/metaprogramming/primitiveConversions.hpp
author pliden
Wed, 20 Nov 2019 10:37:46 +0100
changeset 59152 59272e9e0635
parent 53244 9807daeb47c4
permissions -rw-r--r--
8234383: Test TestBiasedLockRevocationEvents.java assumes -XX:UseBiasedLocking is enabled Reviewed-by: mgronlun, tschatzl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     1
/*
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 49364
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     4
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     8
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    13
 * accompanied this code).
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    14
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    18
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    21
 * questions.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    22
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    23
 */
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    24
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 49364
diff changeset
    25
#ifndef SHARE_METAPROGRAMMING_PRIMITIVECONVERSIONS_HPP
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 49364
diff changeset
    26
#define SHARE_METAPROGRAMMING_PRIMITIVECONVERSIONS_HPP
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    27
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    28
#include "memory/allocation.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    29
#include "metaprogramming/enableIf.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    30
#include "metaprogramming/integralConstant.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    31
#include "metaprogramming/isFloatingPoint.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    32
#include "metaprogramming/isIntegral.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    33
#include "metaprogramming/isRegisteredEnum.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    34
#include "utilities/debug.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    35
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    36
class PrimitiveConversions : public AllStatic {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    37
public:
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    38
  // Return a value of type T with the same representation as x.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    39
  //
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    40
  // T and U must be of the same size.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    41
  //
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    42
  // At least one of T or U must be an integral type.  The other must
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    43
  // be an integral, floating point, or pointer type.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    44
  template<typename T, typename U> static T cast(U x);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    45
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    46
  // Support thin wrappers over primitive types.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    47
  // If derived from TrueType, provides representational conversion
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    48
  // from T to some other type.  When true, must provide
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    49
  // - Value: typedef for T.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    50
  // - Decayed: typedef for decayed type.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    51
  // - static Decayed decay(T x): return value of type Decayed with
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    52
  //   the same representation as x.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    53
  // - static T recover(Decayed x): return a value of type T with the
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    54
  //   same representation as x.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    55
  template<typename T> struct Translate : public FalseType {};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    56
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    57
private:
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    58
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    59
  template<typename T,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    60
           typename U,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    61
           bool same_size = sizeof(T) == sizeof(U),
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    62
           typename Enable = void>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    63
  struct Cast;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    64
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    65
  template<typename T, typename U> static T cast_using_union(U x);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    66
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    67
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    68
// Return an object of type T with the same value representation as x.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    69
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    70
// T and U must be of the same size.  It is expected that one of T and
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    71
// U is an integral type, and the other is an integral type, a
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    72
// (registered) enum type, or a floating point type
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    73
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    74
// This implementation uses the "union trick", which seems to be the
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    75
// best of a bad set of options.  Though technically undefined
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    76
// behavior, it is widely and well supported, producing good code.  In
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    77
// some cases, such as gcc, that support is explicitly documented.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    78
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    79
// Using memcpy is the correct method, but some compilers produce
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    80
// wretched code for that method, even at maximal optimization levels.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    81
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    82
// Using static_cast is only possible for integral and enum types, not
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    83
// for floating point types.  And for integral and enum conversions,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    84
// static_cast has unspecified or implementation-defined behavior for
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    85
// some cases.  C++11 <type_traits> can be used to avoid most or all
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    86
// of those unspecified or implementation-defined issues, though that
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    87
// may require multi-step conversions.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    88
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    89
// Using reinterpret_cast of references has undefined behavior for
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    90
// many cases, and there is much less empirical basis for its use, as
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    91
// compared to the union trick.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    92
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    93
inline T PrimitiveConversions::cast_using_union(U x) {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    94
  STATIC_ASSERT(sizeof(T) == sizeof(U));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    95
  union { T t; U u; };
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    96
  u = x;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    97
  return t;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    98
}
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    99
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   100
//////////////////////////////////////////////////////////////////////////////
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   101
// cast<T>(x)
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   102
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   103
// Cast<T, U, same_size, Enable>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   104
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   105
// Give an informative error if the sizes differ.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   106
template<typename T, typename U>
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 47609
diff changeset
   107
struct PrimitiveConversions::Cast<T, U, false> {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   108
  STATIC_ASSERT(sizeof(T) == sizeof(U));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   109
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   110
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   111
// Conversion between integral types.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   112
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   113
struct PrimitiveConversions::Cast<
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   114
  T, U, true,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   115
  typename EnableIf<IsIntegral<T>::value && IsIntegral<U>::value>::type>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   116
{
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   117
  T operator()(U x) const { return cast_using_union<T>(x); }
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   118
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   119
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   120
// Convert an enum or floating point value to an integer value.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   121
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   122
struct PrimitiveConversions::Cast<
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   123
  T, U, true,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   124
  typename EnableIf<IsIntegral<T>::value &&
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   125
                    (IsRegisteredEnum<U>::value ||
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   126
                     IsFloatingPoint<U>::value)>::type>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   127
{
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   128
  T operator()(U x) const { return cast_using_union<T>(x); }
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   129
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   130
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   131
// Convert an integer to an enum or floating point value.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   132
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   133
struct PrimitiveConversions::Cast<
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   134
  T, U, true,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   135
  typename EnableIf<IsIntegral<U>::value &&
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   136
                    (IsRegisteredEnum<T>::value ||
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   137
                     IsFloatingPoint<T>::value)>::type>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   138
{
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   139
  T operator()(U x) const { return cast_using_union<T>(x); }
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   140
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   141
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   142
// Convert a pointer to an integral value.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   143
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   144
struct PrimitiveConversions::Cast<
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   145
  T, U*, true,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   146
  typename EnableIf<IsIntegral<T>::value>::type>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   147
{
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   148
  T operator()(U* x) const { return reinterpret_cast<T>(x); }
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   149
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   150
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   151
// Convert an integral value to a pointer.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   152
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   153
struct PrimitiveConversions::Cast<
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   154
  T*, U, true,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   155
  typename EnableIf<IsIntegral<U>::value>::type>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   156
{
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   157
  T* operator()(U x) const { return reinterpret_cast<T*>(x); }
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   158
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   159
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   160
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   161
inline T PrimitiveConversions::cast(U x) {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   162
  return Cast<T, U>()(x);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   163
}
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   164
47609
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   165
// jfloat and jdouble translation to integral types
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   166
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   167
template<>
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   168
struct PrimitiveConversions::Translate<jdouble> : public TrueType {
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   169
  typedef double Value;
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   170
  typedef int64_t Decayed;
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   171
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   172
  static Decayed decay(Value x) { return PrimitiveConversions::cast<Decayed>(x); }
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   173
  static Value recover(Decayed x) { return PrimitiveConversions::cast<Value>(x); }
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   174
};
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   175
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   176
template<>
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   177
struct PrimitiveConversions::Translate<jfloat> : public TrueType {
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   178
  typedef float Value;
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   179
  typedef int32_t Decayed;
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   180
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   181
  static Decayed decay(Value x) { return PrimitiveConversions::cast<Decayed>(x); }
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   182
  static Value recover(Decayed x) { return PrimitiveConversions::cast<Value>(x); }
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   183
};
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   184
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 49364
diff changeset
   185
#endif // SHARE_METAPROGRAMMING_PRIMITIVECONVERSIONS_HPP