1ec727ea7Spatrick /*===---- openmp_wrapper/math.h -------- OpenMP math.h intercept ------ c++ -=== 2e5dd7070Spatrick * 3e5dd7070Spatrick * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e5dd7070Spatrick * See https://llvm.org/LICENSE.txt for license information. 5e5dd7070Spatrick * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e5dd7070Spatrick * 7e5dd7070Spatrick *===-----------------------------------------------------------------------=== 8e5dd7070Spatrick */ 9e5dd7070Spatrick 10ec727ea7Spatrick // If we are in C++ mode and include <math.h> (not <cmath>) first, we still need 11ec727ea7Spatrick // to make sure <cmath> is read first. The problem otherwise is that we haven't 12ec727ea7Spatrick // seen the declarations of the math.h functions when the system math.h includes 13ec727ea7Spatrick // our cmath overlay. However, our cmath overlay, or better the underlying 14ec727ea7Spatrick // overlay, e.g. CUDA, uses the math.h functions. Since we haven't declared them 15ec727ea7Spatrick // yet we get errors. CUDA avoids this by eagerly declaring all math functions 16ec727ea7Spatrick // (in the __device__ space) but we cannot do this. Instead we break the 17ec727ea7Spatrick // dependence by forcing cmath to go first. While our cmath will in turn include 18ec727ea7Spatrick // this file, the cmath guards will prevent recursion. 19ec727ea7Spatrick #ifdef __cplusplus 20ec727ea7Spatrick #include <cmath> 21e5dd7070Spatrick #endif 22e5dd7070Spatrick 23ec727ea7Spatrick #ifndef __CLANG_OPENMP_MATH_H__ 24ec727ea7Spatrick #define __CLANG_OPENMP_MATH_H__ 25ec727ea7Spatrick 26ec727ea7Spatrick #ifndef _OPENMP 27ec727ea7Spatrick #error "This file is for OpenMP compilation only." 28ec727ea7Spatrick #endif 29ec727ea7Spatrick 30ec727ea7Spatrick #include_next <math.h> 31ec727ea7Spatrick 32ec727ea7Spatrick // We need limits.h for __clang_cuda_math.h below and because it should not hurt 33ec727ea7Spatrick // we include it eagerly here. 34ec727ea7Spatrick #include <limits.h> 35ec727ea7Spatrick 36ec727ea7Spatrick // We need stdlib.h because (for now) __clang_cuda_math.h below declares `abs` 37ec727ea7Spatrick // which should live in stdlib.h. 38ec727ea7Spatrick #include <stdlib.h> 39ec727ea7Spatrick 40ec727ea7Spatrick #pragma omp begin declare variant match( \ 41ec727ea7Spatrick device = {arch(nvptx, nvptx64)}, implementation = {extension(match_any)}) 42ec727ea7Spatrick 43ec727ea7Spatrick #define __CUDA__ 44ec727ea7Spatrick #define __OPENMP_NVPTX__ 45ec727ea7Spatrick #include <__clang_cuda_math.h> 46ec727ea7Spatrick #undef __OPENMP_NVPTX__ 47ec727ea7Spatrick #undef __CUDA__ 48ec727ea7Spatrick 49ec727ea7Spatrick #pragma omp end declare variant 50ec727ea7Spatrick 51*a9ac8606Spatrick #ifdef __AMDGCN__ 52*a9ac8606Spatrick #pragma omp begin declare variant match(device = {arch(amdgcn)}) 53*a9ac8606Spatrick 54*a9ac8606Spatrick #define __OPENMP_AMDGCN__ 55*a9ac8606Spatrick #include <__clang_hip_math.h> 56*a9ac8606Spatrick #undef __OPENMP_AMDGCN__ 57*a9ac8606Spatrick 58*a9ac8606Spatrick #pragma omp end declare variant 59*a9ac8606Spatrick #endif 60*a9ac8606Spatrick 61ec727ea7Spatrick #endif 62