blob: 1129fa1aae40e3b2a07e8f94c2fa24fe81e00a67 [file] [log] [blame]
Howard Hinnant50f7eee2012-01-13 01:22:31 +00001//===------------------------- dynamic_cast_stress.cpp --------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnant50f7eee2012-01-13 01:22:31 +00006//
7//===----------------------------------------------------------------------===//
8
Louis Dionne31cbe0f2020-06-01 10:38:23 -04009// UNSUPPORTED: c++03
Eric Fiselierc79a8f72015-08-20 01:22:17 +000010
Howard Hinnant50f7eee2012-01-13 01:22:31 +000011#include <cassert>
12#include <tuple>
Nico Weber086048d2019-08-12 19:11:23 +000013#include "support/timer.h"
Howard Hinnant50f7eee2012-01-13 01:22:31 +000014
15template <std::size_t Indx, std::size_t Depth>
16struct C
17 : public virtual C<Indx, Depth-1>,
18 public virtual C<Indx+1, Depth-1>
19{
20 virtual ~C() {}
21};
22
23template <std::size_t Indx>
24struct C<Indx, 0>
25{
26 virtual ~C() {}
27};
28
29template <std::size_t Indx, std::size_t Depth>
30struct B
31 : public virtual C<Indx, Depth-1>,
32 public virtual C<Indx+1, Depth-1>
33{
34};
35
36template <class Indx, std::size_t Depth>
37struct makeB;
38
39template <std::size_t ...Indx, std::size_t Depth>
40struct makeB<std::__tuple_indices<Indx...>, Depth>
41 : public B<Indx, Depth>...
42{
43};
44
45template <std::size_t Width, std::size_t Depth>
46struct A
47 : public makeB<typename std::__make_tuple_indices<Width>::type, Depth>
48{
49};
50
51void test()
52{
Howard Hinnant372e2f42012-01-31 23:52:20 +000053 const std::size_t Width = 10;
54 const std::size_t Depth = 5;
Howard Hinnant50f7eee2012-01-13 01:22:31 +000055 A<Width, Depth> a;
56 typedef B<Width/2, Depth> Destination;
57// typedef A<Width, Depth> Destination;
Eric Fiselierc5de7b12014-11-24 22:38:57 +000058 Destination *b = nullptr;
59 {
60 timer t;
61 b = dynamic_cast<Destination*>((C<Width/2, 0>*)&a);
62 }
Howard Hinnant50f7eee2012-01-13 01:22:31 +000063 assert(b != 0);
64}
65
Louis Dionne504bc072020-10-08 13:36:33 -040066int main(int, char**)
Howard Hinnant50f7eee2012-01-13 01:22:31 +000067{
68 test();
Louis Dionne504bc072020-10-08 13:36:33 -040069
70 return 0;
Howard Hinnant50f7eee2012-01-13 01:22:31 +000071}
72
73/*
74Timing results I'm seeing (median of 3 microseconds):
75
76 libc++abi gcc's dynamic_cast
Howard Hinnantb24c9442012-01-16 18:21:05 +000077B<Width/2, Depth> -O3 48.334 93.190 libc++abi 93% faster
78B<Width/2, Depth> -Os 58.535 94.103 libc++abi 61% faster
79A<Width, Depth> -O3 11.515 33.134 libc++abi 188% faster
80A<Width, Depth> -Os 12.631 31.553 libc++abi 150% faster
Howard Hinnant50f7eee2012-01-13 01:22:31 +000081
82*/