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