File: builtins-c-cpu.cpp

package info (click to toggle)
ispc 1.28.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 97,620 kB
  • sloc: cpp: 77,067; python: 8,303; yacc: 3,337; lex: 1,126; ansic: 631; sh: 475; makefile: 17
file content (182 lines) | stat: -rw-r--r-- 6,968 bytes parent folder | download | duplicates (2)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
  Copyright (c) 2010-2023, Intel Corporation

  SPDX-License-Identifier: BSD-3-Clause
*/

/** @file builtins-c-cpu.cpp
    @brief Standard library function implementations written in C/C++.

    This file provides C/C++ implementations of various functions that can be
    called from ispc programs; in other words, this file is *not* linked
    into the ispc compiler executable, but rather provides functions that
    can be compiled into ispc programs.

    When the ispc compiler is built, this file is compiled with clang to
    generate LLVM bitcode.  This bitcode is later linked in to the program
    being compiled by the DefineStdlib() function.  The first way to access
    definitions from this file is by asking for them name from the
    llvm::Module's' symbol table (e.g. as the PrintStmt implementation does
    with __do_print() below.  Alternatively, if a function defined in this
    file has a signature that can be mapped back to ispc types by the
    lLLVMTypeToIspcType() function, then its declaration will be made
    available to ispc programs at compile time automatically.
  */
#ifndef WASM

#ifdef _MSC_VER
// We do want old school sprintf and don't want secure Microsoft extensions.
// And we also don't want warnings about it, so the define.
#define _CRT_SECURE_NO_WARNINGS
#else
// Some versions of glibc has "fortification" feature, which expands sprintf
// to __builtin___sprintf_chk(..., __builtin_object_size(...), ...).
// We don't want this kind of expansion, as we don't support these intrinsics.
#define _FORTIFY_SOURCE 0
#endif

#ifndef _MSC_VER
// In unistd.h we need the definition of sysconf and _SC_NPROCESSORS_ONLN used as its arguments.
// We should include unistd.h, but it doesn't really work well for cross compilation, as
// requires us to carry around unistd.h, which is not available on Windows out of the box.
#include <unistd.h>

// Just for the reference: these lines are eventually included from unistd.h
// #define _SC_NPROCESSORS_ONLN 58
// long sysconf(int);
#endif // !_MSC_VER

#endif // !WASM

#include "array.hpp"
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using SizeT = int;
using MaskT = uint64_t;
constexpr SizeT RES_STR_SIZE = 8196;
constexpr SizeT ARG_STR_SIZE = 1024;
template <typename T, SizeT Size> using StaticContainer = notstd::array<T, Size>;
template <typename T, SizeT Size> using StaticContainerRef = StaticContainer<T, Size> &;

template <SizeT Size> using StaticString = StaticContainer<char, Size>;
template <SizeT Size> using StaticStringRef = StaticContainerRef<char, Size>;
using WidthT = int;

#include "builtins/builtins-c-common.hpp"

class ArgWriter {
    const void *const *const args_;
    int curArgIdx_;
    WidthT width_;
    MaskT mask_;

  public:
    ArgWriter(const void *const *args, WidthT width, MaskT mask)
        : args_(args), curArgIdx_(0), width_(width), mask_(mask) {}

    template <typename T> auto uniform2Str() {
        auto fmt = PrintInfo::type2Specifier<T>();
        auto argPtr = getArg();
        StaticString<ARG_STR_SIZE> res;
        snprintf(&res[0], ARG_STR_SIZE, fmt, ValueAdapter<T>(*argCast<T>(argPtr)));
        return res;
    }

    template <typename T> auto varying2Str() {
        auto fmt = PrintInfo::type2Specifier<T>();
        StaticString<ARG_STR_SIZE> res;
        res[0] = '[';
        int haveBeenWritten = 1;
        auto argPtr = getArg();
        for (int lane = 0; lane < width_; ++lane) {
            if (mask_ & (1ull << lane)) {
                haveBeenWritten +=
                    snprintf(&res[haveBeenWritten], ARG_STR_SIZE, fmt, ValueAdapter<T>(argCast<T>(argPtr)[lane]));
            } else {
                haveBeenWritten = writeOffLane<T>(res, haveBeenWritten, argPtr, lane);
            }
            res[haveBeenWritten] = lane == width_ - 1 ? ']' : ',';
            ++haveBeenWritten;
        }
        res[haveBeenWritten] = '\0';
        return res;
    }

  private:
    const void *getArg() { return args_[curArgIdx_++]; }

    // casts void* to proper pointer
    // T is the type of argument (not pointer)
    template <typename T> auto argCast(const void *argPtr) { return reinterpret_cast<const T *>(argPtr); }

    // bools are passed as ints
    template <> auto argCast<bool>(const void *argPtr) { return reinterpret_cast<const int *>(argPtr); }

    template <typename T>
    int writeOffLane(StaticString<ARG_STR_SIZE> &res, int haveBeenWritten, const void *argPtr, int lane) {
        haveBeenWritten += snprintf(&res[haveBeenWritten], ARG_STR_SIZE, "((");
        auto fmt = PrintInfo::type2Specifier<T>();
        haveBeenWritten +=
            snprintf(&res[haveBeenWritten], ARG_STR_SIZE, fmt, ValueAdapter<T>(argCast<T>(argPtr)[lane]));
        haveBeenWritten += snprintf(&res[haveBeenWritten], ARG_STR_SIZE, "))");
        return haveBeenWritten;
    }

    template <>
    int writeOffLane<bool>(StaticString<ARG_STR_SIZE> &res, int haveBeenWritten, const void *argPtr, int lane) {
        auto fmt = PrintInfo::type2Specifier<bool>();
        haveBeenWritten += snprintf(&res[haveBeenWritten], ARG_STR_SIZE, fmt, OffLaneBoolStr);
        return haveBeenWritten;
    }
};

/** This function is called by PrintStmt to do the work of printing values
    from ispc programs.  Note that the function signature here must match
    the parameters that PrintStmt::EmitCode() generates.

    @param format  Print format string
    @param types   Encoded types of the values being printed.
                   (See lEncodeType()).
    @param width   Vector width of the compilation target
    @param mask    Current lane mask when the print statement is called
    @param args    Array of pointers to the values to be printed
 */
extern "C" void __do_print(const char *format, const char *types, WidthT width, MaskT mask, const void *const *args) {
    ArgWriter argWriter(args, width, mask);
    StaticString<RES_STR_SIZE> resultingStr = GetFormatedStr(format, types, argWriter);
    fputs(&resultingStr[0], stdout);
    fflush(stdout);
}

#ifdef WASM
extern "C" int __num_cores() { return 1; }
#else // WASM

extern "C" int __num_cores() {
#if defined(_MSC_VER) || defined(__MINGW32__)
    // This is quite a hack.  Including all of windows.h to get this definition
    // pulls in a bunch of stuff that leads to undefined symbols at link time.
    // So we don't #include <windows.h> but instead have the equivalent declarations
    // here.  Presumably this struct declaration won't be changing in the future
    // anyway...
    struct SYSTEM_INFO {
        int pad0[2];
        void *pad1[2];
        int *pad2;
        int dwNumberOfProcessors;
        int pad3[3];
    };

    struct SYSTEM_INFO sysInfo;
    extern void __stdcall GetSystemInfo(struct SYSTEM_INFO *);
    GetSystemInfo(&sysInfo);
    return sysInfo.dwNumberOfProcessors;
#else
    return sysconf(_SC_NPROCESSORS_ONLN);
#endif // !_MSC_VER
}
#endif // !WASM