1*c05d8e5dSAndroid Build Coastguard Worker //===------------------------- dynamic_cast_stress.cpp --------------------------===//
2*c05d8e5dSAndroid Build Coastguard Worker //
3*c05d8e5dSAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*c05d8e5dSAndroid Build Coastguard Worker //
5*c05d8e5dSAndroid Build Coastguard Worker // This file is dual licensed under the MIT and the University of Illinois Open
6*c05d8e5dSAndroid Build Coastguard Worker // Source Licenses. See LICENSE.TXT for details.
7*c05d8e5dSAndroid Build Coastguard Worker //
8*c05d8e5dSAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*c05d8e5dSAndroid Build Coastguard Worker
10*c05d8e5dSAndroid Build Coastguard Worker // UNSUPPORTED: c++98, c++03
11*c05d8e5dSAndroid Build Coastguard Worker
12*c05d8e5dSAndroid Build Coastguard Worker #include <cassert>
13*c05d8e5dSAndroid Build Coastguard Worker #include <tuple>
14*c05d8e5dSAndroid Build Coastguard Worker #include "support/timer.hpp"
15*c05d8e5dSAndroid Build Coastguard Worker
16*c05d8e5dSAndroid Build Coastguard Worker template <std::size_t Indx, std::size_t Depth>
17*c05d8e5dSAndroid Build Coastguard Worker struct C
18*c05d8e5dSAndroid Build Coastguard Worker : public virtual C<Indx, Depth-1>,
19*c05d8e5dSAndroid Build Coastguard Worker public virtual C<Indx+1, Depth-1>
20*c05d8e5dSAndroid Build Coastguard Worker {
~CC21*c05d8e5dSAndroid Build Coastguard Worker virtual ~C() {}
22*c05d8e5dSAndroid Build Coastguard Worker };
23*c05d8e5dSAndroid Build Coastguard Worker
24*c05d8e5dSAndroid Build Coastguard Worker template <std::size_t Indx>
25*c05d8e5dSAndroid Build Coastguard Worker struct C<Indx, 0>
26*c05d8e5dSAndroid Build Coastguard Worker {
~CC27*c05d8e5dSAndroid Build Coastguard Worker virtual ~C() {}
28*c05d8e5dSAndroid Build Coastguard Worker };
29*c05d8e5dSAndroid Build Coastguard Worker
30*c05d8e5dSAndroid Build Coastguard Worker template <std::size_t Indx, std::size_t Depth>
31*c05d8e5dSAndroid Build Coastguard Worker struct B
32*c05d8e5dSAndroid Build Coastguard Worker : public virtual C<Indx, Depth-1>,
33*c05d8e5dSAndroid Build Coastguard Worker public virtual C<Indx+1, Depth-1>
34*c05d8e5dSAndroid Build Coastguard Worker {
35*c05d8e5dSAndroid Build Coastguard Worker };
36*c05d8e5dSAndroid Build Coastguard Worker
37*c05d8e5dSAndroid Build Coastguard Worker template <class Indx, std::size_t Depth>
38*c05d8e5dSAndroid Build Coastguard Worker struct makeB;
39*c05d8e5dSAndroid Build Coastguard Worker
40*c05d8e5dSAndroid Build Coastguard Worker template <std::size_t ...Indx, std::size_t Depth>
41*c05d8e5dSAndroid Build Coastguard Worker struct makeB<std::__tuple_indices<Indx...>, Depth>
42*c05d8e5dSAndroid Build Coastguard Worker : public B<Indx, Depth>...
43*c05d8e5dSAndroid Build Coastguard Worker {
44*c05d8e5dSAndroid Build Coastguard Worker };
45*c05d8e5dSAndroid Build Coastguard Worker
46*c05d8e5dSAndroid Build Coastguard Worker template <std::size_t Width, std::size_t Depth>
47*c05d8e5dSAndroid Build Coastguard Worker struct A
48*c05d8e5dSAndroid Build Coastguard Worker : public makeB<typename std::__make_tuple_indices<Width>::type, Depth>
49*c05d8e5dSAndroid Build Coastguard Worker {
50*c05d8e5dSAndroid Build Coastguard Worker };
51*c05d8e5dSAndroid Build Coastguard Worker
test()52*c05d8e5dSAndroid Build Coastguard Worker void test()
53*c05d8e5dSAndroid Build Coastguard Worker {
54*c05d8e5dSAndroid Build Coastguard Worker const std::size_t Width = 10;
55*c05d8e5dSAndroid Build Coastguard Worker const std::size_t Depth = 5;
56*c05d8e5dSAndroid Build Coastguard Worker A<Width, Depth> a;
57*c05d8e5dSAndroid Build Coastguard Worker typedef B<Width/2, Depth> Destination;
58*c05d8e5dSAndroid Build Coastguard Worker // typedef A<Width, Depth> Destination;
59*c05d8e5dSAndroid Build Coastguard Worker Destination *b = nullptr;
60*c05d8e5dSAndroid Build Coastguard Worker {
61*c05d8e5dSAndroid Build Coastguard Worker timer t;
62*c05d8e5dSAndroid Build Coastguard Worker b = dynamic_cast<Destination*>((C<Width/2, 0>*)&a);
63*c05d8e5dSAndroid Build Coastguard Worker }
64*c05d8e5dSAndroid Build Coastguard Worker assert(b != 0);
65*c05d8e5dSAndroid Build Coastguard Worker }
66*c05d8e5dSAndroid Build Coastguard Worker
main()67*c05d8e5dSAndroid Build Coastguard Worker int main()
68*c05d8e5dSAndroid Build Coastguard Worker {
69*c05d8e5dSAndroid Build Coastguard Worker test();
70*c05d8e5dSAndroid Build Coastguard Worker }
71*c05d8e5dSAndroid Build Coastguard Worker
72*c05d8e5dSAndroid Build Coastguard Worker /*
73*c05d8e5dSAndroid Build Coastguard Worker Timing results I'm seeing (median of 3 microseconds):
74*c05d8e5dSAndroid Build Coastguard Worker
75*c05d8e5dSAndroid Build Coastguard Worker libc++abi gcc's dynamic_cast
76*c05d8e5dSAndroid Build Coastguard Worker B<Width/2, Depth> -O3 48.334 93.190 libc++abi 93% faster
77*c05d8e5dSAndroid Build Coastguard Worker B<Width/2, Depth> -Os 58.535 94.103 libc++abi 61% faster
78*c05d8e5dSAndroid Build Coastguard Worker A<Width, Depth> -O3 11.515 33.134 libc++abi 188% faster
79*c05d8e5dSAndroid Build Coastguard Worker A<Width, Depth> -Os 12.631 31.553 libc++abi 150% faster
80*c05d8e5dSAndroid Build Coastguard Worker
81*c05d8e5dSAndroid Build Coastguard Worker */
82