1 // { dg-do compile }
2 // { dg-options "-O2 -std=gnu++11 -fdump-tree-optimized" } */
3 
4 #include <array>
5 
6 // Return a copy of the underlying memory of an arbitrary value.
7 template <
8     typename T,
9     typename = typename std::enable_if<std::is_trivially_copyable<T>::value>::type
10 >
11 auto getMem(
12     T const & value
13 ) -> std::array<char, sizeof(T)> {
14     auto ret = std::array<char, sizeof(T)>{};
15     __builtin_memcpy(ret.data(), &value, sizeof(T));
16     return ret;
17 }
18 
19 template <
20     typename T,
21     typename = typename std::enable_if<std::is_trivially_copyable<T>::value>::type
22 >
23 auto fromMem(
24     std::array<char, sizeof(T)> const & buf
25 ) -> T {
26     auto ret = T{};
27     __builtin_memcpy(&ret, buf.data(), sizeof(T));
28     return ret;
29 }
30 
foo1(std::uint64_t arg)31 double foo1(std::uint64_t arg) {
32     return fromMem<double>(getMem(arg));
33 }
34 
foo2(std::uint64_t arg)35 double foo2(std::uint64_t arg) {
36     return *reinterpret_cast<double*>(&arg);
37 }
38 
foo3(std::uint64_t arg)39 double foo3(std::uint64_t arg) {
40     double ret;
41     __builtin_memcpy(&ret, &arg, sizeof(arg));
42     return ret;
43 }
44 
45 // { dg-final { scan-tree-dump-not "BIT_FIELD_REF" "optimized" } }
46