Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 150 lines (125 sloc) 5.962 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/*
Copyright (c) 2010-2011, 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 util.h

@brief
*/

#ifndef ISPC_UTIL_H
#define ISPC_UTIL_H

#include "ispc.h"
#ifdef ISPC_IS_WINDOWS
#include <stdarg.h>
#endif

struct SourcePos;

/** Rounds up the given value to the next power of two, if it isn't a power
of two already. */
inline uint32_t RoundUpPow2(uint32_t v) {
    v--;
    v |= v >> 1;
    v |= v >> 2;
    v |= v >> 4;
    v |= v >> 8;
    v |= v >> 16;
    return v+1;
}

#ifdef __GNUG__
#define PRINTF_FUNC __attribute__ \
((__format__ (__printf__, 2, 3)))
#else
#define PRINTF_FUNC
#endif // __GNUG__

// for cross-platform compatibility
#ifdef ISPC_IS_WINDOWS
int vasprintf(char **sptr, const char *fmt, va_list argv);
int asprintf(char **sptr, const char *fmt, ...);
#endif

/** Prints a debugging message. These messages are only printed if
g->debugPrint is \c true. In addition to a program source code
position to associate with the message, a printf()-style format string
is passed along with any values needed for items in the format
string.
*/
void Debug(SourcePos p, const char *format, ...) PRINTF_FUNC;

/** Prints a warning about an issue found during compilation. Compilation
can still continue after warnings; they are purely informative for the
user. In addition to a program source code position to associate with
the message, a printf()-style format string is passed along with any
values needed for items in the format string.
*/
void Warning(SourcePos p, const char *format, ...) PRINTF_FUNC;

/** Prints an error message. It is assumd that compilation can not be
successfully completed after an error has been issued, though the
system tries to continue compiling as much as possible, in order to be
able to issue any subsequent error messages. In addition to a program
source code position to associate with the message, a printf()-style
format string is passed along with any values needed for items in the
format string.
*/
void Error(SourcePos p, const char *format, ...) PRINTF_FUNC;

/** Prints a message about a potential performance issue in the user's
code. These messages are purely informative and don't affect the
completion of compilation. In addition to a program source code
position to associate with the message, a printf()-style format string
is passed along with any values needed for items in the format
string.
*/
void PerformanceWarning(SourcePos p, const char *format, ...) PRINTF_FUNC;

/** Reports a fatal error that causes the program to terminate. This
should only be used for cases where there is an internal error in the
compiler.
*/
#define FATAL(message) FatalError(__FILE__, __LINE__, message)

/** This function generally shouldn't be called directly, but should be
used via the FATAL macro, which includes the file and line number where
the error was issued.
*/
void FatalError(const char *file, int line, const char *message);

/** Returns the number of single-character edits needed to transform
between the two strings.

@param str1 First string
@param str2 Second string
@param maxDist Maximum number of single-character edits allowed
@return Number of single-character edits to transform from str1
to str2, or maxDist+1 if it's not psosible to do so
in fewer than maxDist steps
*/
int StringEditDistance(const std::string &str1, const std::string &str2,
                       int maxDist);

/** Given a string and a set of candidate strings, returns the set of
candidates that are "close" to the given string, where distance is
measured by the number of single-character changes needed to transform
between the two. An empty vector may be returned if none of the
options is close to \c str.
*/
std::vector<std::string> MatchStrings(const std::string &str,
                                      const std::vector<std::string> &options);

/** Given the current working directory and a filename relative to that
directory, this function returns the final directory that the resulting
file is in and the base name of the file itself. */
void GetDirectoryAndFileName(const std::string &currentDir,
                             const std::string &relativeName,
                             std::string *directory, std::string *filename);

#endif // ISPC_UTIL_H
Something went wrong with that request. Please try again.