blob: 95276b221e4df2b035a8894eacce675b11efd6a5 [file] [log] [blame]
Howard Hinnanta39c1042012-01-13 01:22:31 +00001//===------------------------- 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>
Eric Fiselier30ad8292014-11-24 22:38:57 +000012#include "support/timer.hpp"
Howard Hinnanta39c1042012-01-13 01:22:31 +000013
14template <std::size_t Indx, std::size_t Depth>
15struct C
16 : public virtual C<Indx, Depth-1>,
17 public virtual C<Indx+1, Depth-1>
18{
19 virtual ~C() {}
20};
21
22template <std::size_t Indx>
23struct C<Indx, 0>
24{
25 virtual ~C() {}
26};
27
28template <std::size_t Indx, std::size_t Depth>
29struct B
30 : public virtual C<Indx, Depth-1>,
31 public virtual C<Indx+1, Depth-1>
32{
33};
34
35template <class Indx, std::size_t Depth>
36struct makeB;
37
38template <std::size_t ...Indx, std::size_t Depth>
39struct makeB<std::__tuple_indices<Indx...>, Depth>
40 : public B<Indx, Depth>...
41{
42};
43
44template <std::size_t Width, std::size_t Depth>
45struct A
46 : public makeB<typename std::__make_tuple_indices<Width>::type, Depth>
47{
48};
49
50void test()
51{
Howard Hinnant830713c2012-01-31 23:52:20 +000052 const std::size_t Width = 10;
53 const std::size_t Depth = 5;
Howard Hinnanta39c1042012-01-13 01:22:31 +000054 A<Width, Depth> a;
55 typedef B<Width/2, Depth> Destination;
56// typedef A<Width, Depth> Destination;
Eric Fiselier30ad8292014-11-24 22:38:57 +000057 Destination *b = nullptr;
58 {
59 timer t;
60 b = dynamic_cast<Destination*>((C<Width/2, 0>*)&a);
61 }
Howard Hinnanta39c1042012-01-13 01:22:31 +000062 assert(b != 0);
63}
64
65int main()
66{
67 test();
68}
69
70/*
71Timing results I'm seeing (median of 3 microseconds):
72
73 libc++abi gcc's dynamic_cast
Howard Hinnant65255632012-01-16 18:21:05 +000074B<Width/2, Depth> -O3 48.334 93.190 libc++abi 93% faster
75B<Width/2, Depth> -Os 58.535 94.103 libc++abi 61% faster
76A<Width, Depth> -O3 11.515 33.134 libc++abi 188% faster
77A<Width, Depth> -Os 12.631 31.553 libc++abi 150% faster
Howard Hinnanta39c1042012-01-13 01:22:31 +000078
79*/