Commit 2a72d697 authored by David C. Lonie's avatar David C. Lonie

Add compiler detection, VTK_ASSUME.

VTK_ASSUME can be used to provide optimization hints to
the compiler by specifying that certain non-obvious
conditions are true.

This is especially useful for fixing the number of components
in an array; if an array is known to have 3 components, adding

VTK_ASSUME(array->GetNumberOfComponents() == 3);

can allow the compiler to perform advanced loop unrolling and
vectorization in certain cases.
parent 893fb6ed
......@@ -286,6 +286,7 @@ set(${vtk-module}_HDRS
vtkArrayIteratorTemplate.txx
vtkArrayPrint.h
vtkArrayPrint.txx
vtkAssume.h
vtkAtomicTypeConcepts.h
vtkAtomicTypes.h
vtkAutoInit.h
......
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAssume.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#ifndef vtkAssume_h
#define vtkAssume_h
#include "vtkConfigure.h"
#include <cassert>
// Description:
// VTK_ASSUME instructs the compiler that a certain non-obvious condition will
// *always* be true. Beware that if cond is false at runtime, the results are
// unpredictable (and likely catastrophic). A runtime assertion is added so
// that debugging builds may easily catch violations of the condition.
#define VTK_ASSUME(cond) \
do { \
const bool c = cond; \
assert("Bad assumption in VTK_ASSUME: " #cond && c); \
VTK_ASSUME_IMPL(c); \
(void)c; /* Prevents unused var warnings */ \
} while (false) /* do-while prevents extra semicolon warnings */
// VTK_ASSUME_IMPL is compiler-specific:
#if defined(VTK_COMPILER_MSVC) || defined(VTK_COMPILER_ICC)
# define VTK_ASSUME_IMPL(cond) __assume(cond)
#elif defined(VTK_COMPILER_GCC) && VTK_COMPILER_GCC_VERSION >= 40500
// Added in 4.5.0:
# define VTK_ASSUME_IMPL(cond) if (!(cond)) __builtin_unreachable()
#elif defined(VTK_COMPILER_CLANG)
# define VTK_ASSUME_IMPL(cond) if (!(cond)) __builtin_unreachable()
#else
# define VTK_ASSUME_IMPL(cond) do {} while (false) /* no-op */
#endif
#endif // vtkAssume_h
// VTK-HeaderTest-Exclude: vtkAssume.h
......@@ -17,6 +17,37 @@
/* This header is configured by VTK's build process. */
/*--------------------------------------------------------------------------*/
/* Compiler backend */
/* Be careful modifying this -- order is important. */
#if defined(_MSC_VER)
/* MSVC 2015+ can use a clang frontend, so we want to label it only as MSVC
* and not MSVC and clang. */
#define VTK_COMPILER_MSVC
#elif defined(__INTEL_COMPILER)
/* Intel 14+ on OSX uses a clang frontend, so again we want to label them as
* intel only, and not intel and clang. */
#define VTK_COMPILER_ICC
#elif defined(__PGI)
/* PGI reports as GNUC as it generates the same ABI, so we need to check for
* it before gcc. */
#define VTK_COMPILER_PGI
#elif defined(__clang__)
/* Check for clang before GCC, as clang says it is GNUC since it has ABI
* compliance. */
#define VTK_COMPILER_CLANG
#elif defined(__GNUC__)
/* Several compilers pretend to be GCC but have minor differences. Try to
* compensate for that, by checking for those compilers first. */
#define VTK_COMPILER_GCC
#define VTK_COMPILER_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
__GNUC_PATCHLEVEL__)
#endif
/*--------------------------------------------------------------------------*/
/* Platform Features */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment