/* Copyright (c) 2019-2021, Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** @file builtins-info.h @brief This file contains information and utils shared between code, that uses builtins and the code that implements them. */ #ifndef ISPC_BUILTINS_INFO_H #define ISPC_BUILTINS_INFO_H 1 namespace PrintInfo { /* '\0' is excluded as encodings must form a c-string. L0 depends on Encoding being contiguous numbers. */ enum Encoding : char { Bool = 1, Int, UInt, Float, Long, ULong, Double, Ptr, VecBool, VecInt, VecUInt, VecFloat, VecLong, VecULong, VecDouble, VecPtr, Size = VecPtr - Bool + 1 }; // Currently the max format string length supported by L0 runtime. // FIXME: get rid of this constant and corresponeding splitting code when // L0 supports arbitrary length format string. constexpr int LZMaxFormatStrSize = 16 * 1024; // get encoding (as a char) for uniform T type, that is used in type argument of __do_print template Encoding getEncoding4Uniform(); // get encoding (as a char) for uniform T type, that is used in type argument of __do_print template Encoding getEncoding4Varying(); template <> inline constexpr Encoding getEncoding4Uniform() { return Encoding::Bool; } template <> inline constexpr Encoding getEncoding4Uniform() { return Encoding::Int; } template <> inline constexpr Encoding getEncoding4Uniform() { return Encoding::UInt; } template <> inline constexpr Encoding getEncoding4Uniform() { return Encoding::Float; } template <> inline constexpr Encoding getEncoding4Uniform() { return Encoding::Long; } template <> inline constexpr Encoding getEncoding4Uniform() { return Encoding::ULong; } template <> inline constexpr Encoding getEncoding4Uniform() { return Encoding::Double; } template <> inline constexpr Encoding getEncoding4Uniform() { return Encoding::Ptr; } template <> inline constexpr Encoding getEncoding4Varying() { return Encoding::VecBool; } template <> inline constexpr Encoding getEncoding4Varying() { return Encoding::VecInt; } template <> inline constexpr Encoding getEncoding4Varying() { return Encoding::VecUInt; } template <> inline constexpr Encoding getEncoding4Varying() { return Encoding::VecFloat; } template <> inline constexpr Encoding getEncoding4Varying() { return Encoding::VecLong; } template <> inline constexpr Encoding getEncoding4Varying() { return Encoding::VecULong; } template <> inline constexpr Encoding getEncoding4Varying() { return Encoding::VecDouble; } template <> inline constexpr Encoding getEncoding4Varying() { return Encoding::VecPtr; } /* Takes encoding for varying type, returns encoding for corresponding uniform type. For example: Encoding::VecLong -> Encoding::Long (varying long long -> uniform long long) */ inline Encoding getCorrespondingEncoding4Uniform(Encoding type) { return static_cast(type - (Encoding::VecBool - Encoding::Bool)); } inline bool isUniformEncoding(Encoding type) { return type < Encoding::VecBool; } // same as getEncoding4Uniform, but it is a functor struct Encoding4Uniform { template constexpr Encoding call() const { return getEncoding4Uniform(); } }; // same as getEncoding4Varying, but it is a functor struct Encoding4Varying { template constexpr Encoding call() const { return getEncoding4Varying(); } }; // Converts type to corresponding printf type specifier template inline const char *type2Specifier(); template <> inline const char *type2Specifier() { // %s is because we will eventually print "true" or "false" return "%s"; } template <> inline const char *type2Specifier() { return "%d"; } template <> inline const char *type2Specifier() { return "%u"; } template <> inline const char *type2Specifier() { return "%f"; } template <> inline const char *type2Specifier() { return "%lld"; } template <> inline const char *type2Specifier() { return "%llu"; } template <> inline const char *type2Specifier() { return "%f"; } template <> inline const char *type2Specifier() { return "%p"; } // same as type2Specifier, but now it is a functor struct Type2Specifier { template const char *call() const { return type2Specifier(); } }; namespace detail { template inline bool applyIfProperEncoding(Encoding type, Func f, Encoder encoder) { if (type == encoder.template call()) { f.template call(); return true; } return false; } template inline bool switchEncoding(Encoding type, Func f, Encoder encoder) { return applyIfProperEncoding(type, f, encoder) || applyIfProperEncoding(type, f, encoder) || applyIfProperEncoding(type, f, encoder) || applyIfProperEncoding(type, f, encoder) || applyIfProperEncoding(type, f, encoder) || applyIfProperEncoding(type, f, encoder) || applyIfProperEncoding(type, f, encoder) || applyIfProperEncoding(type, f, encoder); } } // namespace detail // Functions to avoid writing "switch(type) case getEncoding..." by hand. // When there's a match, f.call() with corresponding T is called // and true is retruned, otherwise false is returned. // 4Uniform means that only encodings for uniform types are checked, // similary 4Varying is for varying types. template inline bool switchEncoding4Uniform(Encoding type, Func f) { return detail::switchEncoding(type, f, Encoding4Uniform{}); } template inline bool switchEncoding4Varying(Encoding type, Func f) { return detail::switchEncoding(type, f, Encoding4Varying{}); } template inline bool switchEncoding(Encoding type, Func4Uniform fUni, Func4Varying fVar) { return switchEncoding4Uniform(type, fUni) || switchEncoding4Varying(type, fVar); } } // namespace PrintInfo #endif // ISPC_BUILTINS_INFO_H