/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #pragma once #include #include namespace faiss { namespace gpu { template struct Limits { }; // Unfortunately we can't use constexpr because there is no // constexpr constructor for half // FIXME: faiss CPU uses +/-FLT_MAX instead of +/-infinity constexpr float kFloatMax = std::numeric_limits::max(); constexpr float kFloatMin = std::numeric_limits::lowest(); template <> struct Limits { static __device__ __host__ inline float getMin() { return kFloatMin; } static __device__ __host__ inline float getMax() { return kFloatMax; } }; inline __device__ __host__ half kGetHalf(unsigned short v) { #if CUDA_VERSION >= 9000 __half_raw h; h.x = v; return __half(h); #else half h; h.x = v; return h; #endif } template <> struct Limits { static __device__ __host__ inline half getMin() { return kGetHalf(0xfbffU); } static __device__ __host__ inline half getMax() { return kGetHalf(0x7bffU); } }; constexpr int kIntMax = std::numeric_limits::max(); constexpr int kIntMin = std::numeric_limits::lowest(); template <> struct Limits { static __device__ __host__ inline int getMin() { return kIntMin; } static __device__ __host__ inline int getMax() { return kIntMax; } }; template struct Limits> { static __device__ __host__ inline Pair getMin() { return Pair(Limits::getMin(), Limits::getMin()); } static __device__ __host__ inline Pair getMax() { return Pair(Limits::getMax(), Limits::getMax()); } }; } } // namespace