blob: 3187777d33d42f88fbd86b34c42684b7223f0345 [file] [log] [blame]
Nicolas Capens598f8d82016-09-26 15:09:10 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "Nucleus.hpp"
16
17#include "Reactor.hpp"
18#include "Routine.hpp"
19
Nicolas Capens2ae9d742016-11-24 14:43:05 -050020#include "Optimizer.hpp"
21
Nicolas Capens598f8d82016-09-26 15:09:10 -040022#include "src/IceTypes.h"
23#include "src/IceCfg.h"
24#include "src/IceELFStreamer.h"
25#include "src/IceGlobalContext.h"
26#include "src/IceCfgNode.h"
27#include "src/IceELFObjectWriter.h"
Nicolas Capens8dfd9a72016-10-13 17:44:51 -040028#include "src/IceGlobalInits.h"
Nicolas Capens598f8d82016-09-26 15:09:10 -040029
30#include "llvm/Support/FileSystem.h"
31#include "llvm/Support/raw_os_ostream.h"
32
Nicolas Capensbd65da92017-01-05 16:31:06 -050033#if defined(_WIN32)
Alexis Hetu113e33a2017-01-19 10:49:19 -050034#ifndef WIN32_LEAN_AND_MEAN
Nicolas Capens598f8d82016-09-26 15:09:10 -040035#define WIN32_LEAN_AND_MEAN
Alexis Hetu113e33a2017-01-19 10:49:19 -050036#endif // !WIN32_LEAN_AND_MEAN
37#ifndef NOMINMAX
Nicolas Capens598f8d82016-09-26 15:09:10 -040038#define NOMINMAX
Alexis Hetu113e33a2017-01-19 10:49:19 -050039#endif // !NOMINMAX
Nicolas Capens598f8d82016-09-26 15:09:10 -040040#include <Windows.h>
Nicolas Capensbd65da92017-01-05 16:31:06 -050041#else
42#include <sys/mman.h>
Nicolas Capens8b275742017-01-20 17:11:41 -050043#if !defined(MAP_ANONYMOUS)
44#define MAP_ANONYMOUS MAP_ANON
45#endif
Nicolas Capensbd65da92017-01-05 16:31:06 -050046#endif
Nicolas Capens598f8d82016-09-26 15:09:10 -040047
48#include <mutex>
49#include <limits>
50#include <iostream>
51#include <cassert>
52
53namespace
54{
55 Ice::GlobalContext *context = nullptr;
56 Ice::Cfg *function = nullptr;
57 Ice::CfgNode *basicBlock = nullptr;
58 Ice::CfgLocalAllocatorScope *allocator = nullptr;
59 sw::Routine *routine = nullptr;
60
61 std::mutex codegenMutex;
62
63 Ice::ELFFileStreamer *elfFile = nullptr;
64 Ice::Fdstream *out = nullptr;
65}
66
Nicolas Capensccd5ecb2017-01-14 12:52:55 -050067namespace
68{
69 class CPUID
70 {
71 public:
72 const static bool SSE4_1;
73
74 private:
75 static void cpuid(int registers[4], int info)
76 {
77 #if defined(_WIN32)
78 __cpuid(registers, info);
79 #else
80 __asm volatile("cpuid": "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]): "a" (info));
81 #endif
82 }
83
84 static bool detectSSE4_1()
85 {
86 int registers[4];
87 cpuid(registers, 1);
88 return (registers[2] & 0x00080000) != 0;
89 }
90 };
91
92 const bool CPUID::SSE4_1 = CPUID::detectSSE4_1();
93}
94
Nicolas Capens598f8d82016-09-26 15:09:10 -040095namespace sw
96{
Nicolas Capens23d99a42016-09-30 14:57:16 -040097 enum EmulatedType
98 {
99 EmulatedShift = 16,
100 EmulatedV2 = 2 << EmulatedShift,
101 EmulatedV4 = 4 << EmulatedShift,
102 EmulatedV8 = 8 << EmulatedShift,
103 EmulatedBits = EmulatedV2 | EmulatedV4 | EmulatedV8,
104
105 Type_v2i32 = Ice::IceType_v4i32 | EmulatedV2,
106 Type_v4i16 = Ice::IceType_v8i16 | EmulatedV4,
107 Type_v2i16 = Ice::IceType_v8i16 | EmulatedV2,
108 Type_v8i8 = Ice::IceType_v16i8 | EmulatedV8,
109 Type_v4i8 = Ice::IceType_v16i8 | EmulatedV4,
Nicolas Capens4cfd4572016-10-20 01:00:19 -0400110 Type_v2f32 = Ice::IceType_v4f32 | EmulatedV2,
Nicolas Capens23d99a42016-09-30 14:57:16 -0400111 };
112
Nicolas Capens15060bb2016-12-05 22:17:19 -0500113 class Value : public Ice::Operand {};
Nicolas Capensb98fe5c2016-11-09 12:24:06 -0500114 class SwitchCases : public Ice::InstSwitch {};
Nicolas Capens598f8d82016-09-26 15:09:10 -0400115 class BasicBlock : public Ice::CfgNode {};
116
117 Ice::Type T(Type *t)
118 {
Alexis Hetu113e33a2017-01-19 10:49:19 -0500119 static_assert(static_cast<unsigned int>(Ice::IceType_NUM) < static_cast<unsigned int>(EmulatedBits), "Ice::Type overlaps with our emulated types!");
Nicolas Capens23d99a42016-09-30 14:57:16 -0400120 return (Ice::Type)(reinterpret_cast<std::intptr_t>(t) & ~EmulatedBits);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400121 }
122
123 Type *T(Ice::Type t)
124 {
125 return reinterpret_cast<Type*>(t);
126 }
127
Nicolas Capens23d99a42016-09-30 14:57:16 -0400128 Type *T(EmulatedType t)
129 {
130 return reinterpret_cast<Type*>(t);
131 }
132
Nicolas Capens15060bb2016-12-05 22:17:19 -0500133 Value *V(Ice::Operand *v)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400134 {
135 return reinterpret_cast<Value*>(v);
136 }
137
Nicolas Capens611642a2016-09-28 16:45:04 -0400138 BasicBlock *B(Ice::CfgNode *b)
139 {
140 return reinterpret_cast<BasicBlock*>(b);
141 }
142
Nicolas Capens598f8d82016-09-26 15:09:10 -0400143 Optimization optimization[10] = {InstructionCombining, Disabled};
144
Nicolas Capens66478362016-10-13 15:36:36 -0400145 using ElfHeader = std::conditional<sizeof(void*) == 8, Elf64_Ehdr, Elf32_Ehdr>::type;
146 using SectionHeader = std::conditional<sizeof(void*) == 8, Elf64_Shdr, Elf32_Shdr>::type;
147
148 inline const SectionHeader *sectionHeader(const ElfHeader *elfHeader)
149 {
150 return reinterpret_cast<const SectionHeader*>((intptr_t)elfHeader + elfHeader->e_shoff);
151 }
Nicolas Capens87852e12016-11-24 14:45:06 -0500152
Nicolas Capens66478362016-10-13 15:36:36 -0400153 inline const SectionHeader *elfSection(const ElfHeader *elfHeader, int index)
154 {
155 return &sectionHeader(elfHeader)[index];
156 }
157
158 static void *relocateSymbol(const ElfHeader *elfHeader, const Elf32_Rel &relocation, const SectionHeader &relocationTable)
159 {
160 const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info);
Nicolas Capens87852e12016-11-24 14:45:06 -0500161
Nicolas Capens66478362016-10-13 15:36:36 -0400162 intptr_t address = (intptr_t)elfHeader + target->sh_offset;
163 int32_t *patchSite = (int*)(address + relocation.r_offset);
164 uint32_t index = relocation.getSymbol();
165 int table = relocationTable.sh_link;
166 void *symbolValue = nullptr;
Nicolas Capens87852e12016-11-24 14:45:06 -0500167
Nicolas Capens66478362016-10-13 15:36:36 -0400168 if(index != SHN_UNDEF)
169 {
170 if(table == SHN_UNDEF) return nullptr;
171 const SectionHeader *symbolTable = elfSection(elfHeader, table);
Nicolas Capens87852e12016-11-24 14:45:06 -0500172
Nicolas Capens66478362016-10-13 15:36:36 -0400173 uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize;
174 if(index >= symtab_entries)
175 {
176 assert(index < symtab_entries && "Symbol Index out of range");
177 return nullptr;
178 }
Nicolas Capens87852e12016-11-24 14:45:06 -0500179
Nicolas Capens66478362016-10-13 15:36:36 -0400180 intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset;
181 Elf32_Sym &symbol = ((Elf32_Sym*)symbolAddress)[index];
182 uint16_t section = symbol.st_shndx;
183
184 if(section != SHN_UNDEF && section < SHN_LORESERVE)
185 {
186 const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx);
187 symbolValue = reinterpret_cast<void*>((intptr_t)elfHeader + symbol.st_value + target->sh_offset);
188 }
189 else
190 {
191 return nullptr;
192 }
193 }
194
195 switch(relocation.getType())
196 {
197 case R_386_NONE:
198 // No relocation
199 break;
200 case R_386_32:
201 *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite);
202 break;
203 // case R_386_PC32:
204 // *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite);
205 // break;
206 default:
207 assert(false && "Unsupported relocation type");
208 return nullptr;
209 }
210
211 return symbolValue;
212 }
213
214 static void *relocateSymbol(const ElfHeader *elfHeader, const Elf64_Rela &relocation, const SectionHeader &relocationTable)
215 {
216 const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info);
Nicolas Capens87852e12016-11-24 14:45:06 -0500217
Nicolas Capens66478362016-10-13 15:36:36 -0400218 intptr_t address = (intptr_t)elfHeader + target->sh_offset;
219 int32_t *patchSite = (int*)(address + relocation.r_offset);
220 uint32_t index = relocation.getSymbol();
221 int table = relocationTable.sh_link;
222 void *symbolValue = nullptr;
223
224 if(index != SHN_UNDEF)
225 {
226 if(table == SHN_UNDEF) return nullptr;
227 const SectionHeader *symbolTable = elfSection(elfHeader, table);
Nicolas Capens87852e12016-11-24 14:45:06 -0500228
Nicolas Capens66478362016-10-13 15:36:36 -0400229 uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize;
230 if(index >= symtab_entries)
231 {
232 assert(index < symtab_entries && "Symbol Index out of range");
233 return nullptr;
234 }
Nicolas Capens87852e12016-11-24 14:45:06 -0500235
Nicolas Capens66478362016-10-13 15:36:36 -0400236 intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset;
237 Elf64_Sym &symbol = ((Elf64_Sym*)symbolAddress)[index];
238 uint16_t section = symbol.st_shndx;
239
240 if(section != SHN_UNDEF && section < SHN_LORESERVE)
241 {
242 const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx);
243 symbolValue = reinterpret_cast<void*>((intptr_t)elfHeader + symbol.st_value + target->sh_offset);
244 }
245 else
246 {
247 return nullptr;
248 }
249 }
250
251 switch(relocation.getType())
252 {
253 case R_X86_64_NONE:
254 // No relocation
255 break;
Nicolas Capensb98fe5c2016-11-09 12:24:06 -0500256 case R_X86_64_64:
257 *(int64_t*)patchSite = (int64_t)((intptr_t)symbolValue + *(int64_t*)patchSite) + relocation.r_addend;
258 break;
Nicolas Capens66478362016-10-13 15:36:36 -0400259 case R_X86_64_PC32:
260 *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite) + relocation.r_addend;
261 break;
Nicolas Capensb98fe5c2016-11-09 12:24:06 -0500262 case R_X86_64_32S:
263 *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite) + relocation.r_addend;
264 break;
Nicolas Capens66478362016-10-13 15:36:36 -0400265 default:
266 assert(false && "Unsupported relocation type");
267 return nullptr;
268 }
269
270 return symbolValue;
271 }
272
Nicolas Capens598f8d82016-09-26 15:09:10 -0400273 void *loadImage(uint8_t *const elfImage)
274 {
Nicolas Capens598f8d82016-09-26 15:09:10 -0400275 ElfHeader *elfHeader = (ElfHeader*)elfImage;
276
277 if(!elfHeader->checkMagic())
278 {
279 return nullptr;
280 }
281
Nicolas Capens66478362016-10-13 15:36:36 -0400282 // Expect ELF bitness to match platform
Nicolas Capens65047112016-11-07 13:01:07 -0500283 assert(sizeof(void*) == 8 ? elfHeader->getFileClass() == ELFCLASS64 : elfHeader->getFileClass() == ELFCLASS32);
Nicolas Capens66478362016-10-13 15:36:36 -0400284 assert(sizeof(void*) == 8 ? elfHeader->e_machine == EM_X86_64 : elfHeader->e_machine == EM_386);
285
Nicolas Capens598f8d82016-09-26 15:09:10 -0400286 SectionHeader *sectionHeader = (SectionHeader*)(elfImage + elfHeader->e_shoff);
287 void *entry = nullptr;
288
289 for(int i = 0; i < elfHeader->e_shnum; i++)
290 {
Nicolas Capens66478362016-10-13 15:36:36 -0400291 if(sectionHeader[i].sh_type == SHT_PROGBITS)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400292 {
Nicolas Capens66478362016-10-13 15:36:36 -0400293 if(sectionHeader[i].sh_flags & SHF_EXECINSTR)
294 {
295 entry = elfImage + sectionHeader[i].sh_offset;
296 }
297 }
298 else if(sectionHeader[i].sh_type == SHT_REL)
299 {
300 assert(sizeof(void*) == 4 && "UNIMPLEMENTED"); // Only expected/implemented for 32-bit code
301
Alexis Hetu113e33a2017-01-19 10:49:19 -0500302 for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++)
Nicolas Capens66478362016-10-13 15:36:36 -0400303 {
304 const Elf32_Rel &relocation = ((const Elf32_Rel*)(elfImage + sectionHeader[i].sh_offset))[index];
Alexis Hetu113e33a2017-01-19 10:49:19 -0500305 relocateSymbol(elfHeader, relocation, sectionHeader[i]);
Nicolas Capens66478362016-10-13 15:36:36 -0400306 }
307 }
308 else if(sectionHeader[i].sh_type == SHT_RELA)
309 {
310 assert(sizeof(void*) == 8 && "UNIMPLEMENTED"); // Only expected/implemented for 64-bit code
311
Alexis Hetu113e33a2017-01-19 10:49:19 -0500312 for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++)
Nicolas Capens66478362016-10-13 15:36:36 -0400313 {
314 const Elf64_Rela &relocation = ((const Elf64_Rela*)(elfImage + sectionHeader[i].sh_offset))[index];
Alexis Hetu113e33a2017-01-19 10:49:19 -0500315 relocateSymbol(elfHeader, relocation, sectionHeader[i]);
Nicolas Capens66478362016-10-13 15:36:36 -0400316 }
Nicolas Capens598f8d82016-09-26 15:09:10 -0400317 }
318 }
319
320 return entry;
321 }
322
323 template<typename T>
324 struct ExecutableAllocator
325 {
326 ExecutableAllocator() {};
327 template<class U> ExecutableAllocator(const ExecutableAllocator<U> &other) {};
328
329 using value_type = T;
330 using size_type = std::size_t;
331
332 T *allocate(size_type n)
333 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500334 #if defined(_WIN32)
335 return (T*)VirtualAlloc(NULL, sizeof(T) * n, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
336 #else
337 return (T*)mmap(nullptr, sizeof(T) * n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
338 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400339 }
340
341 void deallocate(T *p, size_type n)
342 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500343 #if defined(_WIN32)
344 VirtualFree(p, 0, MEM_RELEASE);
345 #else
346 munmap(p, sizeof(T) * n);
347 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400348 }
349 };
350
351 class ELFMemoryStreamer : public Ice::ELFStreamer, public Routine
352 {
353 ELFMemoryStreamer(const ELFMemoryStreamer &) = delete;
354 ELFMemoryStreamer &operator=(const ELFMemoryStreamer &) = delete;
355
356 public:
Nicolas Capens58274b52016-10-19 23:45:19 -0400357 ELFMemoryStreamer() : Routine(), entry(nullptr)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400358 {
359 position = 0;
360 buffer.reserve(0x1000);
361 }
362
363 virtual ~ELFMemoryStreamer()
364 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500365 #if defined(_WIN32)
366 if(buffer.size() != 0)
367 {
368 DWORD exeProtection;
369 VirtualProtect(&buffer[0], buffer.size(), oldProtection, &exeProtection);
370 }
371 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400372 }
373
374 void write8(uint8_t Value) override
375 {
376 if(position == (uint64_t)buffer.size())
377 {
378 buffer.push_back(Value);
379 position++;
380 }
381 else if(position < (uint64_t)buffer.size())
382 {
383 buffer[position] = Value;
384 position++;
385 }
386 else assert(false && "UNIMPLEMENTED");
387 }
388
389 void writeBytes(llvm::StringRef Bytes) override
390 {
391 std::size_t oldSize = buffer.size();
392 buffer.resize(oldSize + Bytes.size());
393 memcpy(&buffer[oldSize], Bytes.begin(), Bytes.size());
394 position += Bytes.size();
395 }
396
397 uint64_t tell() const override { return position; }
398
399 void seek(uint64_t Off) override { position = Off; }
400
401 const void *getEntry() override
402 {
Nicolas Capens58274b52016-10-19 23:45:19 -0400403 if(!entry)
404 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500405 #if defined(_WIN32)
406 VirtualProtect(&buffer[0], buffer.size(), PAGE_EXECUTE_READWRITE, &oldProtection);
407 #else
408 mprotect(&buffer[0], buffer.size(), PROT_READ | PROT_WRITE | PROT_EXEC);
409 #endif
410
411 position = std::numeric_limits<std::size_t>::max(); // Can't stream more data after this
Nicolas Capens598f8d82016-09-26 15:09:10 -0400412
Nicolas Capens58274b52016-10-19 23:45:19 -0400413 entry = loadImage(&buffer[0]);
414 }
415
416 return entry;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400417 }
418
419 private:
Nicolas Capens58274b52016-10-19 23:45:19 -0400420 void *entry;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400421 std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer;
422 std::size_t position;
Nicolas Capensbd65da92017-01-05 16:31:06 -0500423
424 #if defined(_WIN32)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400425 DWORD oldProtection;
Nicolas Capensbd65da92017-01-05 16:31:06 -0500426 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400427 };
428
429 Nucleus::Nucleus()
430 {
431 ::codegenMutex.lock(); // Reactor is currently not thread safe
432
Nicolas Capens66478362016-10-13 15:36:36 -0400433 Ice::ClFlags &Flags = Ice::ClFlags::Flags;
434 Ice::ClFlags::getParsedClFlags(Flags);
435
436 Flags.setTargetArch(sizeof(void*) == 8 ? Ice::Target_X8664 : Ice::Target_X8632);
437 Flags.setOutFileType(Ice::FT_Elf);
438 Flags.setOptLevel(Ice::Opt_2);
439 Flags.setApplicationBinaryInterface(Ice::ABI_Platform);
Nicolas Capensccd5ecb2017-01-14 12:52:55 -0500440 Flags.setTargetInstructionSet(CPUID::SSE4_1 ? Ice::X86InstructionSet_SSE4_1 : Ice::X86InstructionSet_SSE2);
Nicolas Capens65047112016-11-07 13:01:07 -0500441 Flags.setVerbose(false ? Ice::IceV_All : Ice::IceV_None);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400442
Nicolas Capens65047112016-11-07 13:01:07 -0500443 static llvm::raw_os_ostream cout(std::cout);
444 static llvm::raw_os_ostream cerr(std::cerr);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400445
446 if(false) // Write out to a file
447 {
448 std::error_code errorCode;
449 ::out = new Ice::Fdstream("out.o", errorCode, llvm::sys::fs::F_None);
450 ::elfFile = new Ice::ELFFileStreamer(*out);
Nicolas Capens65047112016-11-07 13:01:07 -0500451 ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfFile);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400452 }
453 else
454 {
455 ELFMemoryStreamer *elfMemory = new ELFMemoryStreamer();
Nicolas Capens65047112016-11-07 13:01:07 -0500456 ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfMemory);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400457 ::routine = elfMemory;
458 }
459 }
460
461 Nucleus::~Nucleus()
462 {
463 delete ::allocator;
464 delete ::function;
465 delete ::context;
466
467 delete ::elfFile;
468 delete ::out;
469
470 ::codegenMutex.unlock();
471 }
472
473 Routine *Nucleus::acquireRoutine(const wchar_t *name, bool runOptimizations)
474 {
475 if(basicBlock->getInsts().empty() || basicBlock->getInsts().back().getKind() != Ice::Inst::Ret)
476 {
477 createRetVoid();
478 }
479
480 std::wstring wideName(name);
481 std::string asciiName(wideName.begin(), wideName.end());
482 ::function->setFunctionName(Ice::GlobalString::createWithString(::context, asciiName));
483
Nicolas Capens2ae9d742016-11-24 14:43:05 -0500484 optimize();
485
Nicolas Capens598f8d82016-09-26 15:09:10 -0400486 ::function->translate();
Nicolas Capensde19f392016-10-19 10:29:49 -0400487 assert(!::function->hasError());
488
Nicolas Capens66478362016-10-13 15:36:36 -0400489 auto *globals = ::function->getGlobalInits().release();
490
491 if(globals && !globals->empty())
492 {
493 ::context->getGlobals()->merge(globals);
494 }
Nicolas Capens598f8d82016-09-26 15:09:10 -0400495
496 ::context->emitFileHeader();
497 ::function->emitIAS();
498 auto assembler = ::function->releaseAssembler();
Nicolas Capens66478362016-10-13 15:36:36 -0400499 auto objectWriter = ::context->getObjectWriter();
500 assembler->alignFunction();
501 objectWriter->writeFunctionCode(::function->getFunctionName(), false, assembler.get());
502 ::context->lowerGlobals("last");
Nicolas Capens73dd7a22016-10-20 13:20:34 -0400503 ::context->lowerConstants();
Nicolas Capensb98fe5c2016-11-09 12:24:06 -0500504 ::context->lowerJumpTables();
Nicolas Capens66478362016-10-13 15:36:36 -0400505 objectWriter->setUndefinedSyms(::context->getConstantExternSyms());
506 objectWriter->writeNonUserSections();
Nicolas Capens598f8d82016-09-26 15:09:10 -0400507
508 return ::routine;
509 }
510
511 void Nucleus::optimize()
512 {
Nicolas Capens2ae9d742016-11-24 14:43:05 -0500513 sw::optimize(::function);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400514 }
515
516 Value *Nucleus::allocateStackVariable(Type *t, int arraySize)
517 {
518 Ice::Type type = T(t);
Nicolas Capensa8f98632016-10-20 11:25:55 -0400519 int typeSize = Ice::typeWidthInBytes(type);
520 int totalSize = typeSize * (arraySize ? arraySize : 1);
Nicolas Capense12780d2016-09-27 14:18:07 -0400521
Nicolas Capensa8f98632016-10-20 11:25:55 -0400522 auto bytes = Ice::ConstantInteger32::create(::context, type, totalSize);
Nicolas Capense12780d2016-09-27 14:18:07 -0400523 auto address = ::function->makeVariable(T(getPointerType(t)));
Nicolas Capensa8f98632016-10-20 11:25:55 -0400524 auto alloca = Ice::InstAlloca::create(::function, address, bytes, typeSize);
Nicolas Capense12780d2016-09-27 14:18:07 -0400525 ::function->getEntryNode()->getInsts().push_front(alloca);
526
527 return V(address);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400528 }
529
530 BasicBlock *Nucleus::createBasicBlock()
531 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400532 return B(::function->makeNode());
Nicolas Capens598f8d82016-09-26 15:09:10 -0400533 }
534
535 BasicBlock *Nucleus::getInsertBlock()
536 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400537 return B(::basicBlock);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400538 }
539
540 void Nucleus::setInsertBlock(BasicBlock *basicBlock)
541 {
Nicolas Capens9ed1a182016-10-24 09:52:23 -0400542 // assert(::basicBlock->getInsts().back().getTerminatorEdges().size() >= 0 && "Previous basic block must have a terminator");
Nicolas Capens611642a2016-09-28 16:45:04 -0400543 ::basicBlock = basicBlock;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400544 }
545
Nicolas Capens598f8d82016-09-26 15:09:10 -0400546 void Nucleus::createFunction(Type *ReturnType, std::vector<Type*> &Params)
547 {
548 uint32_t sequenceNumber = 0;
549 ::function = Ice::Cfg::create(::context, sequenceNumber).release();
550 ::allocator = new Ice::CfgLocalAllocatorScope(::function);
551
552 for(Type *type : Params)
553 {
554 Ice::Variable *arg = ::function->makeVariable(T(type));
555 ::function->addArg(arg);
556 }
557
558 Ice::CfgNode *node = ::function->makeNode();
559 ::function->setEntryNode(node);
560 ::basicBlock = node;
561 }
562
563 Value *Nucleus::getArgument(unsigned int index)
564 {
565 return V(::function->getArgs()[index]);
566 }
567
568 void Nucleus::createRetVoid()
569 {
Nicolas Capensfdcca2d2016-10-20 11:31:36 -0400570 Ice::InstRet *ret = Ice::InstRet::create(::function);
571 ::basicBlock->appendInst(ret);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400572 }
573
574 void Nucleus::createRet(Value *v)
575 {
Nicolas Capensfdcca2d2016-10-20 11:31:36 -0400576 Ice::InstRet *ret = Ice::InstRet::create(::function, v);
577 ::basicBlock->appendInst(ret);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400578 }
579
580 void Nucleus::createBr(BasicBlock *dest)
581 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400582 auto br = Ice::InstBr::create(::function, dest);
583 ::basicBlock->appendInst(br);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400584 }
585
586 void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse)
587 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400588 auto br = Ice::InstBr::create(::function, cond, ifTrue, ifFalse);
589 ::basicBlock->appendInst(br);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400590 }
591
Nicolas Capensf8360ba2017-01-25 11:35:00 -0800592 static bool isCommutative(Ice::InstArithmetic::OpKind op)
593 {
594 switch(op)
595 {
596 case Ice::InstArithmetic::Add:
597 case Ice::InstArithmetic::Fadd:
598 case Ice::InstArithmetic::Mul:
599 case Ice::InstArithmetic::Fmul:
600 case Ice::InstArithmetic::And:
601 case Ice::InstArithmetic::Or:
602 case Ice::InstArithmetic::Xor:
603 return true;
604 default:
605 return false;
606 }
607 }
608
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400609 static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs)
610 {
Nicolas Capens327f1df2016-10-21 14:26:34 -0400611 assert(lhs->getType() == rhs->getType() || (llvm::isa<Ice::Constant>(rhs) && (op == Ice::InstArithmetic::Shl || Ice::InstArithmetic::Lshr || Ice::InstArithmetic::Ashr)));
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400612
Nicolas Capensf8360ba2017-01-25 11:35:00 -0800613 bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op);
614
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400615 Ice::Variable *result = ::function->makeVariable(lhs->getType());
Nicolas Capensf8360ba2017-01-25 11:35:00 -0800616 Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs);
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400617 ::basicBlock->appendInst(arithmetic);
618
619 return V(result);
620 }
621
Nicolas Capens598f8d82016-09-26 15:09:10 -0400622 Value *Nucleus::createAdd(Value *lhs, Value *rhs)
623 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400624 return createArithmetic(Ice::InstArithmetic::Add, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400625 }
626
627 Value *Nucleus::createSub(Value *lhs, Value *rhs)
628 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400629 return createArithmetic(Ice::InstArithmetic::Sub, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400630 }
631
632 Value *Nucleus::createMul(Value *lhs, Value *rhs)
633 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400634 return createArithmetic(Ice::InstArithmetic::Mul, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400635 }
636
637 Value *Nucleus::createUDiv(Value *lhs, Value *rhs)
638 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400639 return createArithmetic(Ice::InstArithmetic::Udiv, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400640 }
641
642 Value *Nucleus::createSDiv(Value *lhs, Value *rhs)
643 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400644 return createArithmetic(Ice::InstArithmetic::Sdiv, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400645 }
646
647 Value *Nucleus::createFAdd(Value *lhs, Value *rhs)
648 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400649 return createArithmetic(Ice::InstArithmetic::Fadd, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400650 }
651
652 Value *Nucleus::createFSub(Value *lhs, Value *rhs)
653 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400654 return createArithmetic(Ice::InstArithmetic::Fsub, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400655 }
656
657 Value *Nucleus::createFMul(Value *lhs, Value *rhs)
658 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400659 return createArithmetic(Ice::InstArithmetic::Fmul, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400660 }
661
662 Value *Nucleus::createFDiv(Value *lhs, Value *rhs)
663 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400664 return createArithmetic(Ice::InstArithmetic::Fdiv, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400665 }
666
667 Value *Nucleus::createURem(Value *lhs, Value *rhs)
668 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400669 return createArithmetic(Ice::InstArithmetic::Urem, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400670 }
671
672 Value *Nucleus::createSRem(Value *lhs, Value *rhs)
673 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400674 return createArithmetic(Ice::InstArithmetic::Srem, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400675 }
676
677 Value *Nucleus::createFRem(Value *lhs, Value *rhs)
678 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400679 return createArithmetic(Ice::InstArithmetic::Frem, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400680 }
681
682 Value *Nucleus::createShl(Value *lhs, Value *rhs)
683 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400684 return createArithmetic(Ice::InstArithmetic::Shl, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400685 }
686
687 Value *Nucleus::createLShr(Value *lhs, Value *rhs)
688 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400689 return createArithmetic(Ice::InstArithmetic::Lshr, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400690 }
691
692 Value *Nucleus::createAShr(Value *lhs, Value *rhs)
693 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400694 return createArithmetic(Ice::InstArithmetic::Ashr, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400695 }
696
697 Value *Nucleus::createAnd(Value *lhs, Value *rhs)
698 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400699 return createArithmetic(Ice::InstArithmetic::And, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400700 }
701
702 Value *Nucleus::createOr(Value *lhs, Value *rhs)
703 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400704 return createArithmetic(Ice::InstArithmetic::Or, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400705 }
706
707 Value *Nucleus::createXor(Value *lhs, Value *rhs)
708 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400709 return createArithmetic(Ice::InstArithmetic::Xor, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400710 }
711
712 Value *Nucleus::createNeg(Value *v)
713 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500714 return createSub(createNullValue(T(v->getType())), v);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400715 }
716
717 Value *Nucleus::createFNeg(Value *v)
718 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500719 double c[4] = {-0.0, -0.0, -0.0, -0.0};
720 Value *negativeZero = Ice::isVectorType(v->getType()) ?
721 createConstantVector(c, T(v->getType())) :
Nicolas Capens15060bb2016-12-05 22:17:19 -0500722 V(::context->getConstantFloat(-0.0f));
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500723
724 return createFSub(negativeZero, v);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400725 }
726
727 Value *Nucleus::createNot(Value *v)
728 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500729 if(Ice::isScalarIntegerType(v->getType()))
730 {
Nicolas Capens15060bb2016-12-05 22:17:19 -0500731 return createXor(v, V(::context->getConstantInt(v->getType(), -1)));
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500732 }
733 else // Vector
734 {
735 int64_t c[4] = {-1, -1, -1, -1};
736 return createXor(v, createConstantVector(c, T(v->getType())));
737 }
Nicolas Capens598f8d82016-09-26 15:09:10 -0400738 }
739
Nicolas Capense12780d2016-09-27 14:18:07 -0400740 Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400741 {
Nicolas Capens23d99a42016-09-30 14:57:16 -0400742 int valueType = (int)reinterpret_cast<intptr_t>(type);
743 Ice::Variable *result = ::function->makeVariable(T(type));
744
745 if(valueType & EmulatedBits)
746 {
747 switch(valueType)
748 {
749 case Type_v4i8:
750 case Type_v2i16:
751 {
752 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::LoadSubVector, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
753 auto target = ::context->getConstantUndef(Ice::IceType_i32);
754 auto load = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
Nicolas Capens23d99a42016-09-30 14:57:16 -0400755 load->addArg(ptr);
Nicolas Capens87852e12016-11-24 14:45:06 -0500756 load->addArg(::context->getConstantInt32(4));
Nicolas Capens23d99a42016-09-30 14:57:16 -0400757 ::basicBlock->appendInst(load);
758 }
759 break;
760 case Type_v2i32:
761 case Type_v8i8:
762 case Type_v4i16:
Nicolas Capens4cfd4572016-10-20 01:00:19 -0400763 case Type_v2f32:
Nicolas Capens23d99a42016-09-30 14:57:16 -0400764 {
765 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::LoadSubVector, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
766 auto target = ::context->getConstantUndef(Ice::IceType_i32);
767 auto load = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
Nicolas Capens23d99a42016-09-30 14:57:16 -0400768 load->addArg(ptr);
Nicolas Capens87852e12016-11-24 14:45:06 -0500769 load->addArg(::context->getConstantInt32(8));
Nicolas Capens23d99a42016-09-30 14:57:16 -0400770 ::basicBlock->appendInst(load);
771 }
772 break;
773 default: assert(false && "UNIMPLEMENTED");
774 }
775 }
776 else
777 {
778 auto load = Ice::InstLoad::create(::function, result, ptr, align);
779 ::basicBlock->appendInst(load);
780 }
781
782 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400783 }
784
Nicolas Capens6d738712016-09-30 04:15:22 -0400785 Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400786 {
Nicolas Capens23d99a42016-09-30 14:57:16 -0400787 int valueType = (int)reinterpret_cast<intptr_t>(type);
788
789 if(valueType & EmulatedBits)
790 {
791 switch(valueType)
792 {
793 case Type_v4i8:
794 case Type_v2i16:
795 {
796 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::StoreSubVector, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T};
797 auto target = ::context->getConstantUndef(Ice::IceType_i32);
798 auto store = Ice::InstIntrinsicCall::create(::function, 3, nullptr, target, intrinsic);
Nicolas Capens23d99a42016-09-30 14:57:16 -0400799 store->addArg(value);
800 store->addArg(ptr);
Nicolas Capens87852e12016-11-24 14:45:06 -0500801 store->addArg(::context->getConstantInt32(4));
Nicolas Capens23d99a42016-09-30 14:57:16 -0400802 ::basicBlock->appendInst(store);
803 }
804 break;
805 case Type_v2i32:
806 case Type_v8i8:
807 case Type_v4i16:
Nicolas Capens4cfd4572016-10-20 01:00:19 -0400808 case Type_v2f32:
Nicolas Capens23d99a42016-09-30 14:57:16 -0400809 {
810 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::StoreSubVector, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T};
811 auto target = ::context->getConstantUndef(Ice::IceType_i32);
812 auto store = Ice::InstIntrinsicCall::create(::function, 3, nullptr, target, intrinsic);
Nicolas Capens23d99a42016-09-30 14:57:16 -0400813 store->addArg(value);
814 store->addArg(ptr);
Nicolas Capens87852e12016-11-24 14:45:06 -0500815 store->addArg(::context->getConstantInt32(8));
Nicolas Capens23d99a42016-09-30 14:57:16 -0400816 ::basicBlock->appendInst(store);
817 }
818 break;
819 default: assert(false && "UNIMPLEMENTED");
820 }
821 }
822 else
823 {
824 assert(T(value->getType()) == type);
825
826 auto store = Ice::InstStore::create(::function, value, ptr, align);
827 ::basicBlock->appendInst(store);
828 }
829
Nicolas Capens598f8d82016-09-26 15:09:10 -0400830 return value;
831 }
832
Nicolas Capens6d738712016-09-30 04:15:22 -0400833 Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400834 {
Nicolas Capens8820f642016-09-30 04:42:43 -0400835 assert(index->getType() == Ice::IceType_i32);
836
Nicolas Capens15060bb2016-12-05 22:17:19 -0500837 if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index))
838 {
839 int32_t offset = constant->getValue() * (int)Ice::typeWidthInBytes(T(type));
840
841 if(offset == 0)
842 {
843 return ptr;
844 }
845
846 return createAdd(ptr, createConstantInt(offset));
847 }
848
Nicolas Capens8820f642016-09-30 04:42:43 -0400849 if(!Ice::isByteSizedType(T(type)))
850 {
Nicolas Capens13ac2322016-10-13 14:52:12 -0400851 index = createMul(index, createConstantInt((int)Ice::typeWidthInBytes(T(type))));
Nicolas Capens8820f642016-09-30 04:42:43 -0400852 }
853
854 if(sizeof(void*) == 8)
855 {
856 index = createSExt(index, T(Ice::IceType_i64));
857 }
858
859 return createAdd(ptr, index);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400860 }
861
862 Value *Nucleus::createAtomicAdd(Value *ptr, Value *value)
863 {
864 assert(false && "UNIMPLEMENTED"); return nullptr;
865 }
866
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400867 static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType)
868 {
Nicolas Capens23d99a42016-09-30 14:57:16 -0400869 if(v->getType() == T(destType))
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400870 {
871 return v;
872 }
873
874 Ice::Variable *result = ::function->makeVariable(T(destType));
875 Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v);
876 ::basicBlock->appendInst(cast);
877
878 return V(result);
879 }
880
Nicolas Capens598f8d82016-09-26 15:09:10 -0400881 Value *Nucleus::createTrunc(Value *v, Type *destType)
882 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400883 return createCast(Ice::InstCast::Trunc, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400884 }
885
886 Value *Nucleus::createZExt(Value *v, Type *destType)
887 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400888 return createCast(Ice::InstCast::Zext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400889 }
890
891 Value *Nucleus::createSExt(Value *v, Type *destType)
892 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400893 return createCast(Ice::InstCast::Sext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400894 }
895
896 Value *Nucleus::createFPToSI(Value *v, Type *destType)
897 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400898 return createCast(Ice::InstCast::Fptosi, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400899 }
900
Nicolas Capens598f8d82016-09-26 15:09:10 -0400901 Value *Nucleus::createSIToFP(Value *v, Type *destType)
902 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400903 return createCast(Ice::InstCast::Sitofp, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400904 }
905
906 Value *Nucleus::createFPTrunc(Value *v, Type *destType)
907 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400908 return createCast(Ice::InstCast::Fptrunc, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400909 }
910
911 Value *Nucleus::createFPExt(Value *v, Type *destType)
912 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400913 return createCast(Ice::InstCast::Fpext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400914 }
915
916 Value *Nucleus::createBitCast(Value *v, Type *destType)
917 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400918 return createCast(Ice::InstCast::Bitcast, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400919 }
920
Nicolas Capens43dc6292016-10-20 00:01:38 -0400921 static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400922 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400923 assert(lhs->getType() == rhs->getType());
924
Nicolas Capens43dc6292016-10-20 00:01:38 -0400925 auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType());
926 auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs);
Nicolas Capens611642a2016-09-28 16:45:04 -0400927 ::basicBlock->appendInst(cmp);
928
929 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400930 }
931
Nicolas Capens43dc6292016-10-20 00:01:38 -0400932 Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs)
933 {
934 return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs);
935 }
936
937 Value *Nucleus::createICmpNE(Value *lhs, Value *rhs)
938 {
939 return createIntCompare(Ice::InstIcmp::Ne, lhs, rhs);
940 }
941
942 Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs)
943 {
944 return createIntCompare(Ice::InstIcmp::Ugt, lhs, rhs);
945 }
946
947 Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs)
948 {
949 return createIntCompare(Ice::InstIcmp::Uge, lhs, rhs);
950 }
951
952 Value *Nucleus::createICmpULT(Value *lhs, Value *rhs)
953 {
954 return createIntCompare(Ice::InstIcmp::Ult, lhs, rhs);
955 }
956
957 Value *Nucleus::createICmpULE(Value *lhs, Value *rhs)
958 {
959 return createIntCompare(Ice::InstIcmp::Ule, lhs, rhs);
960 }
961
962 Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs)
963 {
964 return createIntCompare(Ice::InstIcmp::Sgt, lhs, rhs);
965 }
966
967 Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs)
968 {
969 return createIntCompare(Ice::InstIcmp::Sge, lhs, rhs);
970 }
971
972 Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs)
973 {
974 return createIntCompare(Ice::InstIcmp::Slt, lhs, rhs);
975 }
976
Nicolas Capens598f8d82016-09-26 15:09:10 -0400977 Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs)
978 {
Nicolas Capens43dc6292016-10-20 00:01:38 -0400979 return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs);
980 }
981
982 static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs)
983 {
984 assert(lhs->getType() == rhs->getType());
985 assert(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32);
986
987 auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32);
988 auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs);
989 ::basicBlock->appendInst(cmp);
990
991 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400992 }
993
994 Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs)
995 {
Nicolas Capens43dc6292016-10-20 00:01:38 -0400996 return createFloatCompare(Ice::InstFcmp::Oeq, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400997 }
998
999 Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs)
1000 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001001 return createFloatCompare(Ice::InstFcmp::Ogt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001002 }
1003
1004 Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs)
1005 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001006 return createFloatCompare(Ice::InstFcmp::Oge, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001007 }
1008
1009 Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs)
1010 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001011 return createFloatCompare(Ice::InstFcmp::Olt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001012 }
1013
1014 Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs)
1015 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001016 return createFloatCompare(Ice::InstFcmp::Ole, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001017 }
1018
1019 Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs)
1020 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001021 return createFloatCompare(Ice::InstFcmp::One, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001022 }
1023
1024 Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs)
1025 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001026 return createFloatCompare(Ice::InstFcmp::Ord, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001027 }
1028
1029 Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs)
1030 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001031 return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001032 }
1033
1034 Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs)
1035 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001036 return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001037 }
1038
1039 Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs)
1040 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001041 return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001042 }
1043
1044 Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs)
1045 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001046 return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001047 }
1048
1049 Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs)
1050 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001051 return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001052 }
1053
1054 Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs)
1055 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001056 return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001057 }
1058
1059 Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs)
1060 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001061 return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001062 }
1063
Nicolas Capense95d5342016-09-30 11:37:28 -04001064 Value *Nucleus::createExtractElement(Value *vector, Type *type, int index)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001065 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001066 auto result = ::function->makeVariable(T(type));
1067 auto extract = Ice::InstExtractElement::create(::function, result, vector, ::context->getConstantInt32(index));
1068 ::basicBlock->appendInst(extract);
1069
1070 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001071 }
1072
1073 Value *Nucleus::createInsertElement(Value *vector, Value *element, int index)
1074 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001075 auto result = ::function->makeVariable(vector->getType());
1076 auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index));
1077 ::basicBlock->appendInst(insert);
1078
1079 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001080 }
1081
Nicolas Capense89cd582016-09-30 14:23:47 -04001082 Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001083 {
Nicolas Capens619c0ab2016-09-30 14:46:24 -04001084 assert(V1->getType() == V2->getType());
1085
1086 int size = Ice::typeNumElements(V1->getType());
1087 auto result = ::function->makeVariable(V1->getType());
1088 auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2);
1089
1090 for(int i = 0; i < size; i++)
1091 {
1092 shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i])));
1093 }
1094
1095 ::basicBlock->appendInst(shuffle);
1096
1097 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001098 }
1099
1100 Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse)
1101 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04001102 assert(ifTrue->getType() == ifFalse->getType());
1103
1104 auto result = ::function->makeVariable(ifTrue->getType());
1105 auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse);
1106 ::basicBlock->appendInst(select);
1107
1108 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001109 }
1110
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001111 SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001112 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001113 auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch);
1114 ::basicBlock->appendInst(switchInst);
1115
1116 return reinterpret_cast<SwitchCases*>(switchInst);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001117 }
1118
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001119 void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001120 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001121 switchCases->addBranch(label, label, branch);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001122 }
1123
1124 void Nucleus::createUnreachable()
1125 {
Nicolas Capensfdcca2d2016-10-20 11:31:36 -04001126 Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function);
1127 ::basicBlock->appendInst(unreachable);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001128 }
1129
Nicolas Capense95d5342016-09-30 11:37:28 -04001130 static Value *createSwizzle4(Value *val, unsigned char select)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001131 {
Nicolas Capens619c0ab2016-09-30 14:46:24 -04001132 int swizzle[4] =
1133 {
1134 (select >> 0) & 0x03,
1135 (select >> 2) & 0x03,
1136 (select >> 4) & 0x03,
1137 (select >> 6) & 0x03,
1138 };
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001139
Nicolas Capens619c0ab2016-09-30 14:46:24 -04001140 return Nucleus::createShuffleVector(val, val, swizzle);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001141 }
1142
Nicolas Capense95d5342016-09-30 11:37:28 -04001143 static Value *createMask4(Value *lhs, Value *rhs, unsigned char select)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001144 {
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001145 int64_t mask[4] = {0, 0, 0, 0};
1146
1147 mask[(select >> 0) & 0x03] = -1;
1148 mask[(select >> 2) & 0x03] = -1;
1149 mask[(select >> 4) & 0x03] = -1;
1150 mask[(select >> 6) & 0x03] = -1;
1151
1152 Value *condition = Nucleus::createConstantVector(mask, T(Ice::IceType_v4i1));
1153 Value *result = Nucleus::createSelect(condition, rhs, lhs);
1154
1155 return result;
Nicolas Capens598f8d82016-09-26 15:09:10 -04001156 }
1157
Nicolas Capens598f8d82016-09-26 15:09:10 -04001158 Type *Nucleus::getPointerType(Type *ElementType)
1159 {
Nicolas Capense12780d2016-09-27 14:18:07 -04001160 if(sizeof(void*) == 8)
1161 {
1162 return T(Ice::IceType_i64);
1163 }
1164 else
1165 {
1166 return T(Ice::IceType_i32);
1167 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04001168 }
1169
Nicolas Capens13ac2322016-10-13 14:52:12 -04001170 Value *Nucleus::createNullValue(Type *Ty)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001171 {
Nicolas Capens73dd7a22016-10-20 13:20:34 -04001172 if(Ice::isVectorType(T(Ty)))
1173 {
1174 int64_t c[4] = {0, 0, 0, 0};
1175 return createConstantVector(c, Ty);
1176 }
1177 else
1178 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001179 return V(::context->getConstantZero(T(Ty)));
Nicolas Capens73dd7a22016-10-20 13:20:34 -04001180 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04001181 }
1182
Nicolas Capens13ac2322016-10-13 14:52:12 -04001183 Value *Nucleus::createConstantLong(int64_t i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001184 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001185 return V(::context->getConstantInt64(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001186 }
1187
Nicolas Capens13ac2322016-10-13 14:52:12 -04001188 Value *Nucleus::createConstantInt(int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001189 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001190 return V(::context->getConstantInt32(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001191 }
1192
Nicolas Capens13ac2322016-10-13 14:52:12 -04001193 Value *Nucleus::createConstantInt(unsigned int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001194 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001195 return V(::context->getConstantInt32(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001196 }
1197
Nicolas Capens13ac2322016-10-13 14:52:12 -04001198 Value *Nucleus::createConstantBool(bool b)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001199 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001200 return V(::context->getConstantInt1(b));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001201 }
1202
Nicolas Capens13ac2322016-10-13 14:52:12 -04001203 Value *Nucleus::createConstantByte(signed char i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001204 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001205 return V(::context->getConstantInt8(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001206 }
1207
Nicolas Capens13ac2322016-10-13 14:52:12 -04001208 Value *Nucleus::createConstantByte(unsigned char i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001209 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001210 return V(::context->getConstantInt8(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001211 }
1212
Nicolas Capens13ac2322016-10-13 14:52:12 -04001213 Value *Nucleus::createConstantShort(short i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001214 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001215 return V(::context->getConstantInt16(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001216 }
1217
Nicolas Capens13ac2322016-10-13 14:52:12 -04001218 Value *Nucleus::createConstantShort(unsigned short i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001219 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001220 return V(::context->getConstantInt16(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001221 }
1222
Nicolas Capens13ac2322016-10-13 14:52:12 -04001223 Value *Nucleus::createConstantFloat(float x)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001224 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001225 return V(::context->getConstantFloat(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001226 }
1227
Nicolas Capens13ac2322016-10-13 14:52:12 -04001228 Value *Nucleus::createNullPointer(Type *Ty)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001229 {
Nicolas Capensa29d6532016-12-05 21:38:09 -05001230 return createNullValue(T(sizeof(void*) == 8 ? Ice::IceType_i64 : Ice::IceType_i32));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001231 }
1232
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001233 Value *Nucleus::createConstantVector(const int64_t *constants, Type *type)
Nicolas Capens13ac2322016-10-13 14:52:12 -04001234 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001235 const int vectorSize = 16;
1236 assert(Ice::typeWidthInBytes(T(type)) == vectorSize);
1237 const int alignment = vectorSize;
1238 auto globalPool = ::function->getGlobalPool();
1239
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001240 const int64_t *i = constants;
1241 const double *f = reinterpret_cast<const double*>(constants);
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001242 Ice::VariableDeclaration::DataInitializer *dataInitializer = nullptr;
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001243
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001244 switch((int)reinterpret_cast<intptr_t>(type))
1245 {
1246 case Ice::IceType_v4i32:
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001247 case Ice::IceType_v4i1:
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001248 {
1249 const int initializer[4] = {(int)i[0], (int)i[1], (int)i[2], (int)i[3]};
1250 static_assert(sizeof(initializer) == vectorSize, "!");
1251 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1252 }
1253 break;
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001254 case Ice::IceType_v4f32:
1255 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001256 const float initializer[4] = {(float)f[0], (float)f[1], (float)f[2], (float)f[3]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001257 static_assert(sizeof(initializer) == vectorSize, "!");
1258 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1259 }
1260 break;
1261 case Ice::IceType_v8i16:
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001262 case Ice::IceType_v8i1:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001263 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001264 const short initializer[8] = {(short)i[0], (short)i[1], (short)i[2], (short)i[3], (short)i[4], (short)i[5], (short)i[6], (short)i[7]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001265 static_assert(sizeof(initializer) == vectorSize, "!");
1266 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1267 }
1268 break;
1269 case Ice::IceType_v16i8:
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001270 case Ice::IceType_v16i1:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001271 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001272 const char initializer[16] = {(char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[4], (char)i[5], (char)i[6], (char)i[7], (char)i[8], (char)i[9], (char)i[10], (char)i[11], (char)i[12], (char)i[13], (char)i[14], (char)i[15]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001273 static_assert(sizeof(initializer) == vectorSize, "!");
1274 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1275 }
1276 break;
1277 case Type_v2i32:
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001278 {
1279 const int initializer[4] = {(int)i[0], (int)i[1], (int)i[0], (int)i[1]};
1280 static_assert(sizeof(initializer) == vectorSize, "!");
1281 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1282 }
1283 break;
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001284 case Type_v2f32:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001285 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001286 const float initializer[4] = {(float)f[0], (float)f[1], (float)f[0], (float)f[1]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001287 static_assert(sizeof(initializer) == vectorSize, "!");
1288 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1289 }
1290 break;
1291 case Type_v4i16:
1292 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001293 const short initializer[8] = {(short)i[0], (short)i[1], (short)i[2], (short)i[3], (short)i[0], (short)i[1], (short)i[2], (short)i[3]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001294 static_assert(sizeof(initializer) == vectorSize, "!");
1295 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1296 }
1297 break;
1298 case Type_v8i8:
1299 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001300 const char initializer[16] = {(char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[4], (char)i[5], (char)i[6], (char)i[7], (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[4], (char)i[5], (char)i[6], (char)i[7]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001301 static_assert(sizeof(initializer) == vectorSize, "!");
1302 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1303 }
1304 break;
1305 case Type_v4i8:
1306 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001307 const char initializer[16] = {(char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[0], (char)i[1], (char)i[2], (char)i[3]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001308 static_assert(sizeof(initializer) == vectorSize, "!");
1309 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1310 }
1311 break;
1312 default:
1313 assert(false && "Unknown constant vector type" && type);
1314 }
1315
1316 auto name = Ice::GlobalString::createWithoutString(::context);
1317 auto *variableDeclaration = Ice::VariableDeclaration::create(globalPool);
1318 variableDeclaration->setName(name);
1319 variableDeclaration->setAlignment(alignment);
1320 variableDeclaration->setIsConstant(true);
1321 variableDeclaration->addInitializer(dataInitializer);
Nicolas Capens87852e12016-11-24 14:45:06 -05001322
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001323 ::function->addGlobal(variableDeclaration);
1324
1325 constexpr int32_t offset = 0;
1326 Ice::Operand *ptr = ::context->getConstantSym(offset, name);
1327
1328 Ice::Variable *result = ::function->makeVariable(T(type));
1329 auto load = Ice::InstLoad::create(::function, result, ptr, alignment);
1330 ::basicBlock->appendInst(load);
1331
1332 return V(result);
Nicolas Capens13ac2322016-10-13 14:52:12 -04001333 }
1334
1335 Value *Nucleus::createConstantVector(const double *constants, Type *type)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001336 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001337 return createConstantVector((const int64_t*)constants, type);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001338 }
1339
1340 Type *Void::getType()
1341 {
1342 return T(Ice::IceType_void);
1343 }
1344
Nicolas Capens598f8d82016-09-26 15:09:10 -04001345 Bool::Bool(Argument<Bool> argument)
1346 {
1347 storeValue(argument.value);
1348 }
1349
Nicolas Capens598f8d82016-09-26 15:09:10 -04001350 Bool::Bool(bool x)
1351 {
1352 storeValue(Nucleus::createConstantBool(x));
1353 }
1354
1355 Bool::Bool(RValue<Bool> rhs)
1356 {
1357 storeValue(rhs.value);
1358 }
1359
1360 Bool::Bool(const Bool &rhs)
1361 {
1362 Value *value = rhs.loadValue();
1363 storeValue(value);
1364 }
1365
1366 Bool::Bool(const Reference<Bool> &rhs)
1367 {
1368 Value *value = rhs.loadValue();
1369 storeValue(value);
1370 }
1371
Nicolas Capens96d4e092016-11-18 14:22:38 -05001372 RValue<Bool> Bool::operator=(RValue<Bool> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001373 {
1374 storeValue(rhs.value);
1375
1376 return rhs;
1377 }
1378
Nicolas Capens96d4e092016-11-18 14:22:38 -05001379 RValue<Bool> Bool::operator=(const Bool &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001380 {
1381 Value *value = rhs.loadValue();
1382 storeValue(value);
1383
1384 return RValue<Bool>(value);
1385 }
1386
Nicolas Capens96d4e092016-11-18 14:22:38 -05001387 RValue<Bool> Bool::operator=(const Reference<Bool> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001388 {
1389 Value *value = rhs.loadValue();
1390 storeValue(value);
1391
1392 return RValue<Bool>(value);
1393 }
1394
1395 RValue<Bool> operator!(RValue<Bool> val)
1396 {
1397 return RValue<Bool>(Nucleus::createNot(val.value));
1398 }
1399
1400 RValue<Bool> operator&&(RValue<Bool> lhs, RValue<Bool> rhs)
1401 {
1402 return RValue<Bool>(Nucleus::createAnd(lhs.value, rhs.value));
1403 }
1404
1405 RValue<Bool> operator||(RValue<Bool> lhs, RValue<Bool> rhs)
1406 {
1407 return RValue<Bool>(Nucleus::createOr(lhs.value, rhs.value));
1408 }
1409
1410 Type *Bool::getType()
1411 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001412 return T(Ice::IceType_i1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001413 }
1414
1415 Byte::Byte(Argument<Byte> argument)
1416 {
1417 storeValue(argument.value);
1418 }
1419
1420 Byte::Byte(RValue<Int> cast)
1421 {
1422 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1423
1424 storeValue(integer);
1425 }
1426
1427 Byte::Byte(RValue<UInt> cast)
1428 {
1429 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1430
1431 storeValue(integer);
1432 }
1433
1434 Byte::Byte(RValue<UShort> cast)
1435 {
1436 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1437
1438 storeValue(integer);
1439 }
1440
Nicolas Capens598f8d82016-09-26 15:09:10 -04001441 Byte::Byte(int x)
1442 {
1443 storeValue(Nucleus::createConstantByte((unsigned char)x));
1444 }
1445
1446 Byte::Byte(unsigned char x)
1447 {
1448 storeValue(Nucleus::createConstantByte(x));
1449 }
1450
1451 Byte::Byte(RValue<Byte> rhs)
1452 {
1453 storeValue(rhs.value);
1454 }
1455
1456 Byte::Byte(const Byte &rhs)
1457 {
1458 Value *value = rhs.loadValue();
1459 storeValue(value);
1460 }
1461
1462 Byte::Byte(const Reference<Byte> &rhs)
1463 {
1464 Value *value = rhs.loadValue();
1465 storeValue(value);
1466 }
1467
Nicolas Capens96d4e092016-11-18 14:22:38 -05001468 RValue<Byte> Byte::operator=(RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001469 {
1470 storeValue(rhs.value);
1471
1472 return rhs;
1473 }
1474
Nicolas Capens96d4e092016-11-18 14:22:38 -05001475 RValue<Byte> Byte::operator=(const Byte &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001476 {
1477 Value *value = rhs.loadValue();
1478 storeValue(value);
1479
1480 return RValue<Byte>(value);
1481 }
1482
Nicolas Capens96d4e092016-11-18 14:22:38 -05001483 RValue<Byte> Byte::operator=(const Reference<Byte> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001484 {
1485 Value *value = rhs.loadValue();
1486 storeValue(value);
1487
1488 return RValue<Byte>(value);
1489 }
1490
1491 RValue<Byte> operator+(RValue<Byte> lhs, RValue<Byte> rhs)
1492 {
1493 return RValue<Byte>(Nucleus::createAdd(lhs.value, rhs.value));
1494 }
1495
1496 RValue<Byte> operator-(RValue<Byte> lhs, RValue<Byte> rhs)
1497 {
1498 return RValue<Byte>(Nucleus::createSub(lhs.value, rhs.value));
1499 }
1500
1501 RValue<Byte> operator*(RValue<Byte> lhs, RValue<Byte> rhs)
1502 {
1503 return RValue<Byte>(Nucleus::createMul(lhs.value, rhs.value));
1504 }
1505
1506 RValue<Byte> operator/(RValue<Byte> lhs, RValue<Byte> rhs)
1507 {
1508 return RValue<Byte>(Nucleus::createUDiv(lhs.value, rhs.value));
1509 }
1510
1511 RValue<Byte> operator%(RValue<Byte> lhs, RValue<Byte> rhs)
1512 {
1513 return RValue<Byte>(Nucleus::createURem(lhs.value, rhs.value));
1514 }
1515
1516 RValue<Byte> operator&(RValue<Byte> lhs, RValue<Byte> rhs)
1517 {
1518 return RValue<Byte>(Nucleus::createAnd(lhs.value, rhs.value));
1519 }
1520
1521 RValue<Byte> operator|(RValue<Byte> lhs, RValue<Byte> rhs)
1522 {
1523 return RValue<Byte>(Nucleus::createOr(lhs.value, rhs.value));
1524 }
1525
1526 RValue<Byte> operator^(RValue<Byte> lhs, RValue<Byte> rhs)
1527 {
1528 return RValue<Byte>(Nucleus::createXor(lhs.value, rhs.value));
1529 }
1530
1531 RValue<Byte> operator<<(RValue<Byte> lhs, RValue<Byte> rhs)
1532 {
1533 return RValue<Byte>(Nucleus::createShl(lhs.value, rhs.value));
1534 }
1535
1536 RValue<Byte> operator>>(RValue<Byte> lhs, RValue<Byte> rhs)
1537 {
1538 return RValue<Byte>(Nucleus::createLShr(lhs.value, rhs.value));
1539 }
1540
Nicolas Capens96d4e092016-11-18 14:22:38 -05001541 RValue<Byte> operator+=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001542 {
1543 return lhs = lhs + rhs;
1544 }
1545
Nicolas Capens96d4e092016-11-18 14:22:38 -05001546 RValue<Byte> operator-=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001547 {
1548 return lhs = lhs - rhs;
1549 }
1550
Nicolas Capens96d4e092016-11-18 14:22:38 -05001551 RValue<Byte> operator*=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001552 {
1553 return lhs = lhs * rhs;
1554 }
1555
Nicolas Capens96d4e092016-11-18 14:22:38 -05001556 RValue<Byte> operator/=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001557 {
1558 return lhs = lhs / rhs;
1559 }
1560
Nicolas Capens96d4e092016-11-18 14:22:38 -05001561 RValue<Byte> operator%=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001562 {
1563 return lhs = lhs % rhs;
1564 }
1565
Nicolas Capens96d4e092016-11-18 14:22:38 -05001566 RValue<Byte> operator&=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001567 {
1568 return lhs = lhs & rhs;
1569 }
1570
Nicolas Capens96d4e092016-11-18 14:22:38 -05001571 RValue<Byte> operator|=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001572 {
1573 return lhs = lhs | rhs;
1574 }
1575
Nicolas Capens96d4e092016-11-18 14:22:38 -05001576 RValue<Byte> operator^=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001577 {
1578 return lhs = lhs ^ rhs;
1579 }
1580
Nicolas Capens96d4e092016-11-18 14:22:38 -05001581 RValue<Byte> operator<<=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001582 {
1583 return lhs = lhs << rhs;
1584 }
1585
Nicolas Capens96d4e092016-11-18 14:22:38 -05001586 RValue<Byte> operator>>=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001587 {
1588 return lhs = lhs >> rhs;
1589 }
1590
1591 RValue<Byte> operator+(RValue<Byte> val)
1592 {
1593 return val;
1594 }
1595
1596 RValue<Byte> operator-(RValue<Byte> val)
1597 {
1598 return RValue<Byte>(Nucleus::createNeg(val.value));
1599 }
1600
1601 RValue<Byte> operator~(RValue<Byte> val)
1602 {
1603 return RValue<Byte>(Nucleus::createNot(val.value));
1604 }
1605
Nicolas Capens96d4e092016-11-18 14:22:38 -05001606 RValue<Byte> operator++(Byte &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001607 {
1608 RValue<Byte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001609 val += Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001610 return res;
1611 }
1612
Nicolas Capens96d4e092016-11-18 14:22:38 -05001613 const Byte &operator++(Byte &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001614 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001615 val += Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001616 return val;
1617 }
1618
Nicolas Capens96d4e092016-11-18 14:22:38 -05001619 RValue<Byte> operator--(Byte &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001620 {
1621 RValue<Byte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001622 val -= Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001623 return res;
1624 }
1625
Nicolas Capens96d4e092016-11-18 14:22:38 -05001626 const Byte &operator--(Byte &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001627 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001628 val -= Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001629 return val;
1630 }
1631
1632 RValue<Bool> operator<(RValue<Byte> lhs, RValue<Byte> rhs)
1633 {
1634 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
1635 }
1636
1637 RValue<Bool> operator<=(RValue<Byte> lhs, RValue<Byte> rhs)
1638 {
1639 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
1640 }
1641
1642 RValue<Bool> operator>(RValue<Byte> lhs, RValue<Byte> rhs)
1643 {
1644 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
1645 }
1646
1647 RValue<Bool> operator>=(RValue<Byte> lhs, RValue<Byte> rhs)
1648 {
1649 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
1650 }
1651
1652 RValue<Bool> operator!=(RValue<Byte> lhs, RValue<Byte> rhs)
1653 {
1654 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
1655 }
1656
1657 RValue<Bool> operator==(RValue<Byte> lhs, RValue<Byte> rhs)
1658 {
1659 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
1660 }
1661
1662 Type *Byte::getType()
1663 {
Nicolas Capens6d738712016-09-30 04:15:22 -04001664 return T(Ice::IceType_i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001665 }
1666
1667 SByte::SByte(Argument<SByte> argument)
1668 {
1669 storeValue(argument.value);
1670 }
1671
1672 SByte::SByte(RValue<Int> cast)
1673 {
1674 Value *integer = Nucleus::createTrunc(cast.value, SByte::getType());
1675
1676 storeValue(integer);
1677 }
1678
1679 SByte::SByte(RValue<Short> cast)
1680 {
1681 Value *integer = Nucleus::createTrunc(cast.value, SByte::getType());
1682
1683 storeValue(integer);
1684 }
1685
Nicolas Capens598f8d82016-09-26 15:09:10 -04001686 SByte::SByte(signed char x)
1687 {
1688 storeValue(Nucleus::createConstantByte(x));
1689 }
1690
1691 SByte::SByte(RValue<SByte> rhs)
1692 {
1693 storeValue(rhs.value);
1694 }
1695
1696 SByte::SByte(const SByte &rhs)
1697 {
1698 Value *value = rhs.loadValue();
1699 storeValue(value);
1700 }
1701
1702 SByte::SByte(const Reference<SByte> &rhs)
1703 {
1704 Value *value = rhs.loadValue();
1705 storeValue(value);
1706 }
1707
Nicolas Capens96d4e092016-11-18 14:22:38 -05001708 RValue<SByte> SByte::operator=(RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001709 {
1710 storeValue(rhs.value);
1711
1712 return rhs;
1713 }
1714
Nicolas Capens96d4e092016-11-18 14:22:38 -05001715 RValue<SByte> SByte::operator=(const SByte &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001716 {
1717 Value *value = rhs.loadValue();
1718 storeValue(value);
1719
1720 return RValue<SByte>(value);
1721 }
1722
Nicolas Capens96d4e092016-11-18 14:22:38 -05001723 RValue<SByte> SByte::operator=(const Reference<SByte> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001724 {
1725 Value *value = rhs.loadValue();
1726 storeValue(value);
1727
1728 return RValue<SByte>(value);
1729 }
1730
1731 RValue<SByte> operator+(RValue<SByte> lhs, RValue<SByte> rhs)
1732 {
1733 return RValue<SByte>(Nucleus::createAdd(lhs.value, rhs.value));
1734 }
1735
1736 RValue<SByte> operator-(RValue<SByte> lhs, RValue<SByte> rhs)
1737 {
1738 return RValue<SByte>(Nucleus::createSub(lhs.value, rhs.value));
1739 }
1740
1741 RValue<SByte> operator*(RValue<SByte> lhs, RValue<SByte> rhs)
1742 {
1743 return RValue<SByte>(Nucleus::createMul(lhs.value, rhs.value));
1744 }
1745
1746 RValue<SByte> operator/(RValue<SByte> lhs, RValue<SByte> rhs)
1747 {
1748 return RValue<SByte>(Nucleus::createSDiv(lhs.value, rhs.value));
1749 }
1750
1751 RValue<SByte> operator%(RValue<SByte> lhs, RValue<SByte> rhs)
1752 {
1753 return RValue<SByte>(Nucleus::createSRem(lhs.value, rhs.value));
1754 }
1755
1756 RValue<SByte> operator&(RValue<SByte> lhs, RValue<SByte> rhs)
1757 {
1758 return RValue<SByte>(Nucleus::createAnd(lhs.value, rhs.value));
1759 }
1760
1761 RValue<SByte> operator|(RValue<SByte> lhs, RValue<SByte> rhs)
1762 {
1763 return RValue<SByte>(Nucleus::createOr(lhs.value, rhs.value));
1764 }
1765
1766 RValue<SByte> operator^(RValue<SByte> lhs, RValue<SByte> rhs)
1767 {
1768 return RValue<SByte>(Nucleus::createXor(lhs.value, rhs.value));
1769 }
1770
1771 RValue<SByte> operator<<(RValue<SByte> lhs, RValue<SByte> rhs)
1772 {
1773 return RValue<SByte>(Nucleus::createShl(lhs.value, rhs.value));
1774 }
1775
1776 RValue<SByte> operator>>(RValue<SByte> lhs, RValue<SByte> rhs)
1777 {
1778 return RValue<SByte>(Nucleus::createAShr(lhs.value, rhs.value));
1779 }
1780
Nicolas Capens96d4e092016-11-18 14:22:38 -05001781 RValue<SByte> operator+=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001782 {
1783 return lhs = lhs + rhs;
1784 }
1785
Nicolas Capens96d4e092016-11-18 14:22:38 -05001786 RValue<SByte> operator-=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001787 {
1788 return lhs = lhs - rhs;
1789 }
1790
Nicolas Capens96d4e092016-11-18 14:22:38 -05001791 RValue<SByte> operator*=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001792 {
1793 return lhs = lhs * rhs;
1794 }
1795
Nicolas Capens96d4e092016-11-18 14:22:38 -05001796 RValue<SByte> operator/=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001797 {
1798 return lhs = lhs / rhs;
1799 }
1800
Nicolas Capens96d4e092016-11-18 14:22:38 -05001801 RValue<SByte> operator%=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001802 {
1803 return lhs = lhs % rhs;
1804 }
1805
Nicolas Capens96d4e092016-11-18 14:22:38 -05001806 RValue<SByte> operator&=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001807 {
1808 return lhs = lhs & rhs;
1809 }
1810
Nicolas Capens96d4e092016-11-18 14:22:38 -05001811 RValue<SByte> operator|=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001812 {
1813 return lhs = lhs | rhs;
1814 }
1815
Nicolas Capens96d4e092016-11-18 14:22:38 -05001816 RValue<SByte> operator^=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001817 {
1818 return lhs = lhs ^ rhs;
1819 }
1820
Nicolas Capens96d4e092016-11-18 14:22:38 -05001821 RValue<SByte> operator<<=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001822 {
1823 return lhs = lhs << rhs;
1824 }
1825
Nicolas Capens96d4e092016-11-18 14:22:38 -05001826 RValue<SByte> operator>>=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001827 {
1828 return lhs = lhs >> rhs;
1829 }
1830
1831 RValue<SByte> operator+(RValue<SByte> val)
1832 {
1833 return val;
1834 }
1835
1836 RValue<SByte> operator-(RValue<SByte> val)
1837 {
1838 return RValue<SByte>(Nucleus::createNeg(val.value));
1839 }
1840
1841 RValue<SByte> operator~(RValue<SByte> val)
1842 {
1843 return RValue<SByte>(Nucleus::createNot(val.value));
1844 }
1845
Nicolas Capens96d4e092016-11-18 14:22:38 -05001846 RValue<SByte> operator++(SByte &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001847 {
1848 RValue<SByte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001849 val += SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001850 return res;
1851 }
1852
Nicolas Capens96d4e092016-11-18 14:22:38 -05001853 const SByte &operator++(SByte &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001854 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001855 val += SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001856 return val;
1857 }
1858
Nicolas Capens96d4e092016-11-18 14:22:38 -05001859 RValue<SByte> operator--(SByte &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001860 {
1861 RValue<SByte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001862 val -= SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001863 return res;
1864 }
1865
Nicolas Capens96d4e092016-11-18 14:22:38 -05001866 const SByte &operator--(SByte &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001867 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001868 val -= SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001869 return val;
1870 }
1871
1872 RValue<Bool> operator<(RValue<SByte> lhs, RValue<SByte> rhs)
1873 {
1874 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
1875 }
1876
1877 RValue<Bool> operator<=(RValue<SByte> lhs, RValue<SByte> rhs)
1878 {
1879 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
1880 }
1881
1882 RValue<Bool> operator>(RValue<SByte> lhs, RValue<SByte> rhs)
1883 {
1884 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
1885 }
1886
1887 RValue<Bool> operator>=(RValue<SByte> lhs, RValue<SByte> rhs)
1888 {
1889 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
1890 }
1891
1892 RValue<Bool> operator!=(RValue<SByte> lhs, RValue<SByte> rhs)
1893 {
1894 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
1895 }
1896
1897 RValue<Bool> operator==(RValue<SByte> lhs, RValue<SByte> rhs)
1898 {
1899 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
1900 }
1901
1902 Type *SByte::getType()
1903 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001904 return T(Ice::IceType_i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001905 }
1906
1907 Short::Short(Argument<Short> argument)
1908 {
1909 storeValue(argument.value);
1910 }
1911
1912 Short::Short(RValue<Int> cast)
1913 {
1914 Value *integer = Nucleus::createTrunc(cast.value, Short::getType());
1915
1916 storeValue(integer);
1917 }
1918
Nicolas Capens598f8d82016-09-26 15:09:10 -04001919 Short::Short(short x)
1920 {
1921 storeValue(Nucleus::createConstantShort(x));
1922 }
1923
1924 Short::Short(RValue<Short> rhs)
1925 {
1926 storeValue(rhs.value);
1927 }
1928
1929 Short::Short(const Short &rhs)
1930 {
1931 Value *value = rhs.loadValue();
1932 storeValue(value);
1933 }
1934
1935 Short::Short(const Reference<Short> &rhs)
1936 {
1937 Value *value = rhs.loadValue();
1938 storeValue(value);
1939 }
1940
Nicolas Capens96d4e092016-11-18 14:22:38 -05001941 RValue<Short> Short::operator=(RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001942 {
1943 storeValue(rhs.value);
1944
1945 return rhs;
1946 }
1947
Nicolas Capens96d4e092016-11-18 14:22:38 -05001948 RValue<Short> Short::operator=(const Short &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001949 {
1950 Value *value = rhs.loadValue();
1951 storeValue(value);
1952
1953 return RValue<Short>(value);
1954 }
1955
Nicolas Capens96d4e092016-11-18 14:22:38 -05001956 RValue<Short> Short::operator=(const Reference<Short> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001957 {
1958 Value *value = rhs.loadValue();
1959 storeValue(value);
1960
1961 return RValue<Short>(value);
1962 }
1963
1964 RValue<Short> operator+(RValue<Short> lhs, RValue<Short> rhs)
1965 {
1966 return RValue<Short>(Nucleus::createAdd(lhs.value, rhs.value));
1967 }
1968
1969 RValue<Short> operator-(RValue<Short> lhs, RValue<Short> rhs)
1970 {
1971 return RValue<Short>(Nucleus::createSub(lhs.value, rhs.value));
1972 }
1973
1974 RValue<Short> operator*(RValue<Short> lhs, RValue<Short> rhs)
1975 {
1976 return RValue<Short>(Nucleus::createMul(lhs.value, rhs.value));
1977 }
1978
1979 RValue<Short> operator/(RValue<Short> lhs, RValue<Short> rhs)
1980 {
1981 return RValue<Short>(Nucleus::createSDiv(lhs.value, rhs.value));
1982 }
1983
1984 RValue<Short> operator%(RValue<Short> lhs, RValue<Short> rhs)
1985 {
1986 return RValue<Short>(Nucleus::createSRem(lhs.value, rhs.value));
1987 }
1988
1989 RValue<Short> operator&(RValue<Short> lhs, RValue<Short> rhs)
1990 {
1991 return RValue<Short>(Nucleus::createAnd(lhs.value, rhs.value));
1992 }
1993
1994 RValue<Short> operator|(RValue<Short> lhs, RValue<Short> rhs)
1995 {
1996 return RValue<Short>(Nucleus::createOr(lhs.value, rhs.value));
1997 }
1998
1999 RValue<Short> operator^(RValue<Short> lhs, RValue<Short> rhs)
2000 {
2001 return RValue<Short>(Nucleus::createXor(lhs.value, rhs.value));
2002 }
2003
2004 RValue<Short> operator<<(RValue<Short> lhs, RValue<Short> rhs)
2005 {
2006 return RValue<Short>(Nucleus::createShl(lhs.value, rhs.value));
2007 }
2008
2009 RValue<Short> operator>>(RValue<Short> lhs, RValue<Short> rhs)
2010 {
2011 return RValue<Short>(Nucleus::createAShr(lhs.value, rhs.value));
2012 }
2013
Nicolas Capens96d4e092016-11-18 14:22:38 -05002014 RValue<Short> operator+=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002015 {
2016 return lhs = lhs + rhs;
2017 }
2018
Nicolas Capens96d4e092016-11-18 14:22:38 -05002019 RValue<Short> operator-=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002020 {
2021 return lhs = lhs - rhs;
2022 }
2023
Nicolas Capens96d4e092016-11-18 14:22:38 -05002024 RValue<Short> operator*=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002025 {
2026 return lhs = lhs * rhs;
2027 }
2028
Nicolas Capens96d4e092016-11-18 14:22:38 -05002029 RValue<Short> operator/=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002030 {
2031 return lhs = lhs / rhs;
2032 }
2033
Nicolas Capens96d4e092016-11-18 14:22:38 -05002034 RValue<Short> operator%=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002035 {
2036 return lhs = lhs % rhs;
2037 }
2038
Nicolas Capens96d4e092016-11-18 14:22:38 -05002039 RValue<Short> operator&=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002040 {
2041 return lhs = lhs & rhs;
2042 }
2043
Nicolas Capens96d4e092016-11-18 14:22:38 -05002044 RValue<Short> operator|=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002045 {
2046 return lhs = lhs | rhs;
2047 }
2048
Nicolas Capens96d4e092016-11-18 14:22:38 -05002049 RValue<Short> operator^=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002050 {
2051 return lhs = lhs ^ rhs;
2052 }
2053
Nicolas Capens96d4e092016-11-18 14:22:38 -05002054 RValue<Short> operator<<=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002055 {
2056 return lhs = lhs << rhs;
2057 }
2058
Nicolas Capens96d4e092016-11-18 14:22:38 -05002059 RValue<Short> operator>>=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002060 {
2061 return lhs = lhs >> rhs;
2062 }
2063
2064 RValue<Short> operator+(RValue<Short> val)
2065 {
2066 return val;
2067 }
2068
2069 RValue<Short> operator-(RValue<Short> val)
2070 {
2071 return RValue<Short>(Nucleus::createNeg(val.value));
2072 }
2073
2074 RValue<Short> operator~(RValue<Short> val)
2075 {
2076 return RValue<Short>(Nucleus::createNot(val.value));
2077 }
2078
Nicolas Capens96d4e092016-11-18 14:22:38 -05002079 RValue<Short> operator++(Short &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002080 {
2081 RValue<Short> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002082 val += Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002083 return res;
2084 }
2085
Nicolas Capens96d4e092016-11-18 14:22:38 -05002086 const Short &operator++(Short &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002087 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002088 val += Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002089 return val;
2090 }
2091
Nicolas Capens96d4e092016-11-18 14:22:38 -05002092 RValue<Short> operator--(Short &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002093 {
2094 RValue<Short> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002095 val -= Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002096 return res;
2097 }
2098
Nicolas Capens96d4e092016-11-18 14:22:38 -05002099 const Short &operator--(Short &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002100 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002101 val -= Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002102 return val;
2103 }
2104
2105 RValue<Bool> operator<(RValue<Short> lhs, RValue<Short> rhs)
2106 {
2107 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
2108 }
2109
2110 RValue<Bool> operator<=(RValue<Short> lhs, RValue<Short> rhs)
2111 {
2112 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
2113 }
2114
2115 RValue<Bool> operator>(RValue<Short> lhs, RValue<Short> rhs)
2116 {
2117 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
2118 }
2119
2120 RValue<Bool> operator>=(RValue<Short> lhs, RValue<Short> rhs)
2121 {
2122 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
2123 }
2124
2125 RValue<Bool> operator!=(RValue<Short> lhs, RValue<Short> rhs)
2126 {
2127 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
2128 }
2129
2130 RValue<Bool> operator==(RValue<Short> lhs, RValue<Short> rhs)
2131 {
2132 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
2133 }
2134
2135 Type *Short::getType()
2136 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002137 return T(Ice::IceType_i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002138 }
2139
2140 UShort::UShort(Argument<UShort> argument)
2141 {
2142 storeValue(argument.value);
2143 }
2144
2145 UShort::UShort(RValue<UInt> cast)
2146 {
2147 Value *integer = Nucleus::createTrunc(cast.value, UShort::getType());
2148
2149 storeValue(integer);
2150 }
2151
2152 UShort::UShort(RValue<Int> cast)
2153 {
2154 Value *integer = Nucleus::createTrunc(cast.value, UShort::getType());
2155
2156 storeValue(integer);
2157 }
2158
Nicolas Capens598f8d82016-09-26 15:09:10 -04002159 UShort::UShort(unsigned short x)
2160 {
2161 storeValue(Nucleus::createConstantShort(x));
2162 }
2163
2164 UShort::UShort(RValue<UShort> rhs)
2165 {
2166 storeValue(rhs.value);
2167 }
2168
2169 UShort::UShort(const UShort &rhs)
2170 {
2171 Value *value = rhs.loadValue();
2172 storeValue(value);
2173 }
2174
2175 UShort::UShort(const Reference<UShort> &rhs)
2176 {
2177 Value *value = rhs.loadValue();
2178 storeValue(value);
2179 }
2180
Nicolas Capens96d4e092016-11-18 14:22:38 -05002181 RValue<UShort> UShort::operator=(RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002182 {
2183 storeValue(rhs.value);
2184
2185 return rhs;
2186 }
2187
Nicolas Capens96d4e092016-11-18 14:22:38 -05002188 RValue<UShort> UShort::operator=(const UShort &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002189 {
2190 Value *value = rhs.loadValue();
2191 storeValue(value);
2192
2193 return RValue<UShort>(value);
2194 }
2195
Nicolas Capens96d4e092016-11-18 14:22:38 -05002196 RValue<UShort> UShort::operator=(const Reference<UShort> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002197 {
2198 Value *value = rhs.loadValue();
2199 storeValue(value);
2200
2201 return RValue<UShort>(value);
2202 }
2203
2204 RValue<UShort> operator+(RValue<UShort> lhs, RValue<UShort> rhs)
2205 {
2206 return RValue<UShort>(Nucleus::createAdd(lhs.value, rhs.value));
2207 }
2208
2209 RValue<UShort> operator-(RValue<UShort> lhs, RValue<UShort> rhs)
2210 {
2211 return RValue<UShort>(Nucleus::createSub(lhs.value, rhs.value));
2212 }
2213
2214 RValue<UShort> operator*(RValue<UShort> lhs, RValue<UShort> rhs)
2215 {
2216 return RValue<UShort>(Nucleus::createMul(lhs.value, rhs.value));
2217 }
2218
2219 RValue<UShort> operator/(RValue<UShort> lhs, RValue<UShort> rhs)
2220 {
2221 return RValue<UShort>(Nucleus::createUDiv(lhs.value, rhs.value));
2222 }
2223
2224 RValue<UShort> operator%(RValue<UShort> lhs, RValue<UShort> rhs)
2225 {
2226 return RValue<UShort>(Nucleus::createURem(lhs.value, rhs.value));
2227 }
2228
2229 RValue<UShort> operator&(RValue<UShort> lhs, RValue<UShort> rhs)
2230 {
2231 return RValue<UShort>(Nucleus::createAnd(lhs.value, rhs.value));
2232 }
2233
2234 RValue<UShort> operator|(RValue<UShort> lhs, RValue<UShort> rhs)
2235 {
2236 return RValue<UShort>(Nucleus::createOr(lhs.value, rhs.value));
2237 }
2238
2239 RValue<UShort> operator^(RValue<UShort> lhs, RValue<UShort> rhs)
2240 {
2241 return RValue<UShort>(Nucleus::createXor(lhs.value, rhs.value));
2242 }
2243
2244 RValue<UShort> operator<<(RValue<UShort> lhs, RValue<UShort> rhs)
2245 {
2246 return RValue<UShort>(Nucleus::createShl(lhs.value, rhs.value));
2247 }
2248
2249 RValue<UShort> operator>>(RValue<UShort> lhs, RValue<UShort> rhs)
2250 {
2251 return RValue<UShort>(Nucleus::createLShr(lhs.value, rhs.value));
2252 }
2253
Nicolas Capens96d4e092016-11-18 14:22:38 -05002254 RValue<UShort> operator+=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002255 {
2256 return lhs = lhs + rhs;
2257 }
2258
Nicolas Capens96d4e092016-11-18 14:22:38 -05002259 RValue<UShort> operator-=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002260 {
2261 return lhs = lhs - rhs;
2262 }
2263
Nicolas Capens96d4e092016-11-18 14:22:38 -05002264 RValue<UShort> operator*=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002265 {
2266 return lhs = lhs * rhs;
2267 }
2268
Nicolas Capens96d4e092016-11-18 14:22:38 -05002269 RValue<UShort> operator/=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002270 {
2271 return lhs = lhs / rhs;
2272 }
2273
Nicolas Capens96d4e092016-11-18 14:22:38 -05002274 RValue<UShort> operator%=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002275 {
2276 return lhs = lhs % rhs;
2277 }
2278
Nicolas Capens96d4e092016-11-18 14:22:38 -05002279 RValue<UShort> operator&=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002280 {
2281 return lhs = lhs & rhs;
2282 }
2283
Nicolas Capens96d4e092016-11-18 14:22:38 -05002284 RValue<UShort> operator|=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002285 {
2286 return lhs = lhs | rhs;
2287 }
2288
Nicolas Capens96d4e092016-11-18 14:22:38 -05002289 RValue<UShort> operator^=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002290 {
2291 return lhs = lhs ^ rhs;
2292 }
2293
Nicolas Capens96d4e092016-11-18 14:22:38 -05002294 RValue<UShort> operator<<=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002295 {
2296 return lhs = lhs << rhs;
2297 }
2298
Nicolas Capens96d4e092016-11-18 14:22:38 -05002299 RValue<UShort> operator>>=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002300 {
2301 return lhs = lhs >> rhs;
2302 }
2303
2304 RValue<UShort> operator+(RValue<UShort> val)
2305 {
2306 return val;
2307 }
2308
2309 RValue<UShort> operator-(RValue<UShort> val)
2310 {
2311 return RValue<UShort>(Nucleus::createNeg(val.value));
2312 }
2313
2314 RValue<UShort> operator~(RValue<UShort> val)
2315 {
2316 return RValue<UShort>(Nucleus::createNot(val.value));
2317 }
2318
Nicolas Capens96d4e092016-11-18 14:22:38 -05002319 RValue<UShort> operator++(UShort &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002320 {
2321 RValue<UShort> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002322 val += UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002323 return res;
2324 }
2325
Nicolas Capens96d4e092016-11-18 14:22:38 -05002326 const UShort &operator++(UShort &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002327 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002328 val += UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002329 return val;
2330 }
2331
Nicolas Capens96d4e092016-11-18 14:22:38 -05002332 RValue<UShort> operator--(UShort &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002333 {
2334 RValue<UShort> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002335 val -= UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002336 return res;
2337 }
2338
Nicolas Capens96d4e092016-11-18 14:22:38 -05002339 const UShort &operator--(UShort &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002340 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002341 val -= UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002342 return val;
2343 }
2344
2345 RValue<Bool> operator<(RValue<UShort> lhs, RValue<UShort> rhs)
2346 {
2347 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
2348 }
2349
2350 RValue<Bool> operator<=(RValue<UShort> lhs, RValue<UShort> rhs)
2351 {
2352 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
2353 }
2354
2355 RValue<Bool> operator>(RValue<UShort> lhs, RValue<UShort> rhs)
2356 {
2357 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
2358 }
2359
2360 RValue<Bool> operator>=(RValue<UShort> lhs, RValue<UShort> rhs)
2361 {
2362 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
2363 }
2364
2365 RValue<Bool> operator!=(RValue<UShort> lhs, RValue<UShort> rhs)
2366 {
2367 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
2368 }
2369
2370 RValue<Bool> operator==(RValue<UShort> lhs, RValue<UShort> rhs)
2371 {
2372 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
2373 }
2374
2375 Type *UShort::getType()
2376 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002377 return T(Ice::IceType_i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002378 }
2379
Nicolas Capens16b5f152016-10-13 13:39:01 -04002380 Byte4::Byte4(RValue<Byte8> cast)
2381 {
Nicolas Capens16b5f152016-10-13 13:39:01 -04002382 storeValue(Nucleus::createBitCast(cast.value, getType()));
2383 }
2384
2385 Byte4::Byte4(const Reference<Byte4> &rhs)
2386 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002387 Value *value = rhs.loadValue();
2388 storeValue(value);
Nicolas Capens16b5f152016-10-13 13:39:01 -04002389 }
2390
Nicolas Capens598f8d82016-09-26 15:09:10 -04002391 Type *Byte4::getType()
2392 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002393 return T(Type_v4i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002394 }
2395
2396 Type *SByte4::getType()
2397 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002398 return T(Type_v4i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002399 }
2400
Nicolas Capens598f8d82016-09-26 15:09:10 -04002401 Byte8::Byte8(uint8_t x0, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4, uint8_t x5, uint8_t x6, uint8_t x7)
2402 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04002403 int64_t constantVector[8] = {x0, x1, x2, x3, x4, x5, x6, x7};
2404 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002405 }
2406
2407 Byte8::Byte8(RValue<Byte8> rhs)
2408 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002409 storeValue(rhs.value);
2410 }
2411
2412 Byte8::Byte8(const Byte8 &rhs)
2413 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002414 Value *value = rhs.loadValue();
2415 storeValue(value);
2416 }
2417
2418 Byte8::Byte8(const Reference<Byte8> &rhs)
2419 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002420 Value *value = rhs.loadValue();
2421 storeValue(value);
2422 }
2423
Nicolas Capens96d4e092016-11-18 14:22:38 -05002424 RValue<Byte8> Byte8::operator=(RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002425 {
2426 storeValue(rhs.value);
2427
2428 return rhs;
2429 }
2430
Nicolas Capens96d4e092016-11-18 14:22:38 -05002431 RValue<Byte8> Byte8::operator=(const Byte8 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002432 {
2433 Value *value = rhs.loadValue();
2434 storeValue(value);
2435
2436 return RValue<Byte8>(value);
2437 }
2438
Nicolas Capens96d4e092016-11-18 14:22:38 -05002439 RValue<Byte8> Byte8::operator=(const Reference<Byte8> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002440 {
2441 Value *value = rhs.loadValue();
2442 storeValue(value);
2443
2444 return RValue<Byte8>(value);
2445 }
2446
2447 RValue<Byte8> operator+(RValue<Byte8> lhs, RValue<Byte8> rhs)
2448 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002449 return RValue<Byte8>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002450 }
2451
2452 RValue<Byte8> operator-(RValue<Byte8> lhs, RValue<Byte8> rhs)
2453 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002454 return RValue<Byte8>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002455 }
2456
2457// RValue<Byte8> operator*(RValue<Byte8> lhs, RValue<Byte8> rhs)
2458// {
2459// return RValue<Byte8>(Nucleus::createMul(lhs.value, rhs.value));
2460// }
2461
2462// RValue<Byte8> operator/(RValue<Byte8> lhs, RValue<Byte8> rhs)
2463// {
2464// return RValue<Byte8>(Nucleus::createUDiv(lhs.value, rhs.value));
2465// }
2466
2467// RValue<Byte8> operator%(RValue<Byte8> lhs, RValue<Byte8> rhs)
2468// {
2469// return RValue<Byte8>(Nucleus::createURem(lhs.value, rhs.value));
2470// }
2471
2472 RValue<Byte8> operator&(RValue<Byte8> lhs, RValue<Byte8> rhs)
2473 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002474 return RValue<Byte8>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002475 }
2476
2477 RValue<Byte8> operator|(RValue<Byte8> lhs, RValue<Byte8> rhs)
2478 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002479 return RValue<Byte8>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002480 }
2481
2482 RValue<Byte8> operator^(RValue<Byte8> lhs, RValue<Byte8> rhs)
2483 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002484 return RValue<Byte8>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002485 }
2486
2487// RValue<Byte8> operator<<(RValue<Byte8> lhs, unsigned char rhs)
2488// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002489// return RValue<Byte8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002490// }
2491
2492// RValue<Byte8> operator>>(RValue<Byte8> lhs, unsigned char rhs)
2493// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002494// return RValue<Byte8>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002495// }
2496
Nicolas Capens96d4e092016-11-18 14:22:38 -05002497 RValue<Byte8> operator+=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002498 {
2499 return lhs = lhs + rhs;
2500 }
2501
Nicolas Capens96d4e092016-11-18 14:22:38 -05002502 RValue<Byte8> operator-=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002503 {
2504 return lhs = lhs - rhs;
2505 }
2506
Nicolas Capens96d4e092016-11-18 14:22:38 -05002507// RValue<Byte8> operator*=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002508// {
2509// return lhs = lhs * rhs;
2510// }
2511
Nicolas Capens96d4e092016-11-18 14:22:38 -05002512// RValue<Byte8> operator/=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002513// {
2514// return lhs = lhs / rhs;
2515// }
2516
Nicolas Capens96d4e092016-11-18 14:22:38 -05002517// RValue<Byte8> operator%=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002518// {
2519// return lhs = lhs % rhs;
2520// }
2521
Nicolas Capens96d4e092016-11-18 14:22:38 -05002522 RValue<Byte8> operator&=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002523 {
2524 return lhs = lhs & rhs;
2525 }
2526
Nicolas Capens96d4e092016-11-18 14:22:38 -05002527 RValue<Byte8> operator|=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002528 {
2529 return lhs = lhs | rhs;
2530 }
2531
Nicolas Capens96d4e092016-11-18 14:22:38 -05002532 RValue<Byte8> operator^=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002533 {
2534 return lhs = lhs ^ rhs;
2535 }
2536
Nicolas Capens96d4e092016-11-18 14:22:38 -05002537// RValue<Byte8> operator<<=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002538// {
2539// return lhs = lhs << rhs;
2540// }
2541
Nicolas Capens96d4e092016-11-18 14:22:38 -05002542// RValue<Byte8> operator>>=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002543// {
2544// return lhs = lhs >> rhs;
2545// }
2546
2547// RValue<Byte8> operator+(RValue<Byte8> val)
2548// {
2549// return val;
2550// }
2551
2552// RValue<Byte8> operator-(RValue<Byte8> val)
2553// {
2554// return RValue<Byte8>(Nucleus::createNeg(val.value));
2555// }
2556
2557 RValue<Byte8> operator~(RValue<Byte8> val)
2558 {
2559 return RValue<Byte8>(Nucleus::createNot(val.value));
2560 }
2561
2562 RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y)
2563 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002564 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2565 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2566 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2567 auto paddusb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2568 paddusb->addArg(x.value);
2569 paddusb->addArg(y.value);
2570 ::basicBlock->appendInst(paddusb);
2571
2572 return RValue<Byte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002573 }
2574
2575 RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y)
2576 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002577 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2578 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2579 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2580 auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2581 psubusw->addArg(x.value);
2582 psubusw->addArg(y.value);
2583 ::basicBlock->appendInst(psubusw);
2584
2585 return RValue<Byte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002586 }
2587
2588 RValue<Short4> Unpack(RValue<Byte4> x)
2589 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04002590 int shuffle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}; // Real type is v16i8
2591 return RValue<Short4>(Nucleus::createShuffleVector(x.value, x.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002592 }
2593
2594 RValue<Short4> UnpackLow(RValue<Byte8> x, RValue<Byte8> y)
2595 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04002596 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
2597 return RValue<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002598 }
2599
2600 RValue<Short4> UnpackHigh(RValue<Byte8> x, RValue<Byte8> y)
2601 {
Nicolas Capens20e22c42016-10-25 17:32:37 -04002602 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
2603 auto lowHigh = RValue<Byte16>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
2604 return As<Short4>(Swizzle(As<Int4>(lowHigh), 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002605 }
2606
2607 RValue<Int> SignMask(RValue<Byte8> x)
2608 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002609 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
2610 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2611 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2612 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
2613 movmsk->addArg(x.value);
2614 ::basicBlock->appendInst(movmsk);
2615
2616 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002617 }
2618
2619// RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y)
2620// {
Nicolas Capens2f970b62016-11-08 14:28:59 -05002621// return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002622// }
2623
2624 RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y)
2625 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002626 return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002627 }
2628
2629 Type *Byte8::getType()
2630 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002631 return T(Type_v8i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002632 }
2633
Nicolas Capens598f8d82016-09-26 15:09:10 -04002634 SByte8::SByte8(uint8_t x0, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4, uint8_t x5, uint8_t x6, uint8_t x7)
2635 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04002636 int64_t constantVector[8] = { x0, x1, x2, x3, x4, x5, x6, x7 };
2637 Value *vector = V(Nucleus::createConstantVector(constantVector, getType()));
2638
2639 storeValue(Nucleus::createBitCast(vector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002640 }
2641
Nicolas Capens598f8d82016-09-26 15:09:10 -04002642 SByte8::SByte8(RValue<SByte8> rhs)
2643 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002644 storeValue(rhs.value);
2645 }
2646
2647 SByte8::SByte8(const SByte8 &rhs)
2648 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002649 Value *value = rhs.loadValue();
2650 storeValue(value);
2651 }
2652
2653 SByte8::SByte8(const Reference<SByte8> &rhs)
2654 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002655 Value *value = rhs.loadValue();
2656 storeValue(value);
2657 }
2658
Nicolas Capens96d4e092016-11-18 14:22:38 -05002659 RValue<SByte8> SByte8::operator=(RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002660 {
2661 storeValue(rhs.value);
2662
2663 return rhs;
2664 }
2665
Nicolas Capens96d4e092016-11-18 14:22:38 -05002666 RValue<SByte8> SByte8::operator=(const SByte8 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002667 {
2668 Value *value = rhs.loadValue();
2669 storeValue(value);
2670
2671 return RValue<SByte8>(value);
2672 }
2673
Nicolas Capens96d4e092016-11-18 14:22:38 -05002674 RValue<SByte8> SByte8::operator=(const Reference<SByte8> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002675 {
2676 Value *value = rhs.loadValue();
2677 storeValue(value);
2678
2679 return RValue<SByte8>(value);
2680 }
2681
2682 RValue<SByte8> operator+(RValue<SByte8> lhs, RValue<SByte8> rhs)
2683 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002684 return RValue<SByte8>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002685 }
2686
2687 RValue<SByte8> operator-(RValue<SByte8> lhs, RValue<SByte8> rhs)
2688 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002689 return RValue<SByte8>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002690 }
2691
2692// RValue<SByte8> operator*(RValue<SByte8> lhs, RValue<SByte8> rhs)
2693// {
2694// return RValue<SByte8>(Nucleus::createMul(lhs.value, rhs.value));
2695// }
2696
2697// RValue<SByte8> operator/(RValue<SByte8> lhs, RValue<SByte8> rhs)
2698// {
2699// return RValue<SByte8>(Nucleus::createSDiv(lhs.value, rhs.value));
2700// }
2701
2702// RValue<SByte8> operator%(RValue<SByte8> lhs, RValue<SByte8> rhs)
2703// {
2704// return RValue<SByte8>(Nucleus::createSRem(lhs.value, rhs.value));
2705// }
2706
2707 RValue<SByte8> operator&(RValue<SByte8> lhs, RValue<SByte8> rhs)
2708 {
2709 return RValue<SByte8>(Nucleus::createAnd(lhs.value, rhs.value));
2710 }
2711
2712 RValue<SByte8> operator|(RValue<SByte8> lhs, RValue<SByte8> rhs)
2713 {
2714 return RValue<SByte8>(Nucleus::createOr(lhs.value, rhs.value));
2715 }
2716
2717 RValue<SByte8> operator^(RValue<SByte8> lhs, RValue<SByte8> rhs)
2718 {
2719 return RValue<SByte8>(Nucleus::createXor(lhs.value, rhs.value));
2720 }
2721
2722// RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs)
2723// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002724// return RValue<SByte8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002725// }
2726
2727// RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs)
2728// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002729// return RValue<SByte8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002730// }
2731
Nicolas Capens96d4e092016-11-18 14:22:38 -05002732 RValue<SByte8> operator+=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002733 {
2734 return lhs = lhs + rhs;
2735 }
2736
Nicolas Capens96d4e092016-11-18 14:22:38 -05002737 RValue<SByte8> operator-=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002738 {
2739 return lhs = lhs - rhs;
2740 }
2741
Nicolas Capens96d4e092016-11-18 14:22:38 -05002742// RValue<SByte8> operator*=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002743// {
2744// return lhs = lhs * rhs;
2745// }
2746
Nicolas Capens96d4e092016-11-18 14:22:38 -05002747// RValue<SByte8> operator/=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002748// {
2749// return lhs = lhs / rhs;
2750// }
2751
Nicolas Capens96d4e092016-11-18 14:22:38 -05002752// RValue<SByte8> operator%=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002753// {
2754// return lhs = lhs % rhs;
2755// }
2756
Nicolas Capens96d4e092016-11-18 14:22:38 -05002757 RValue<SByte8> operator&=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002758 {
2759 return lhs = lhs & rhs;
2760 }
2761
Nicolas Capens96d4e092016-11-18 14:22:38 -05002762 RValue<SByte8> operator|=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002763 {
2764 return lhs = lhs | rhs;
2765 }
2766
Nicolas Capens96d4e092016-11-18 14:22:38 -05002767 RValue<SByte8> operator^=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002768 {
2769 return lhs = lhs ^ rhs;
2770 }
2771
Nicolas Capens96d4e092016-11-18 14:22:38 -05002772// RValue<SByte8> operator<<=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002773// {
2774// return lhs = lhs << rhs;
2775// }
2776
Nicolas Capens96d4e092016-11-18 14:22:38 -05002777// RValue<SByte8> operator>>=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002778// {
2779// return lhs = lhs >> rhs;
2780// }
2781
2782// RValue<SByte8> operator+(RValue<SByte8> val)
2783// {
2784// return val;
2785// }
2786
2787// RValue<SByte8> operator-(RValue<SByte8> val)
2788// {
2789// return RValue<SByte8>(Nucleus::createNeg(val.value));
2790// }
2791
2792 RValue<SByte8> operator~(RValue<SByte8> val)
2793 {
2794 return RValue<SByte8>(Nucleus::createNot(val.value));
2795 }
2796
2797 RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y)
2798 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002799 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2800 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2801 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2802 auto paddsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2803 paddsb->addArg(x.value);
2804 paddsb->addArg(y.value);
2805 ::basicBlock->appendInst(paddsb);
2806
2807 return RValue<SByte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002808 }
2809
2810 RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y)
2811 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002812 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2813 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2814 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2815 auto psubsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2816 psubsb->addArg(x.value);
2817 psubsb->addArg(y.value);
2818 ::basicBlock->appendInst(psubsb);
2819
2820 return RValue<SByte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002821 }
2822
2823 RValue<Short4> UnpackLow(RValue<SByte8> x, RValue<SByte8> y)
2824 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04002825 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
2826 return RValue<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002827 }
2828
2829 RValue<Short4> UnpackHigh(RValue<SByte8> x, RValue<SByte8> y)
2830 {
Nicolas Capens20e22c42016-10-25 17:32:37 -04002831 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
2832 auto lowHigh = RValue<Byte16>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
2833 return As<Short4>(Swizzle(As<Int4>(lowHigh), 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002834 }
2835
2836 RValue<Int> SignMask(RValue<SByte8> x)
2837 {
Nicolas Capensf2cb9df2016-10-21 17:26:13 -04002838 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
2839 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2840 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2841 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
2842 movmsk->addArg(x.value);
2843 ::basicBlock->appendInst(movmsk);
2844
2845 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002846 }
2847
2848 RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y)
2849 {
Nicolas Capens2f970b62016-11-08 14:28:59 -05002850 return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002851 }
2852
2853 RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y)
2854 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002855 return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002856 }
2857
2858 Type *SByte8::getType()
2859 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002860 return T(Type_v8i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002861 }
2862
2863 Byte16::Byte16(RValue<Byte16> rhs)
2864 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002865 storeValue(rhs.value);
2866 }
2867
2868 Byte16::Byte16(const Byte16 &rhs)
2869 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002870 Value *value = rhs.loadValue();
2871 storeValue(value);
2872 }
2873
2874 Byte16::Byte16(const Reference<Byte16> &rhs)
2875 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002876 Value *value = rhs.loadValue();
2877 storeValue(value);
2878 }
2879
Nicolas Capens96d4e092016-11-18 14:22:38 -05002880 RValue<Byte16> Byte16::operator=(RValue<Byte16> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002881 {
2882 storeValue(rhs.value);
2883
2884 return rhs;
2885 }
2886
Nicolas Capens96d4e092016-11-18 14:22:38 -05002887 RValue<Byte16> Byte16::operator=(const Byte16 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002888 {
2889 Value *value = rhs.loadValue();
2890 storeValue(value);
2891
2892 return RValue<Byte16>(value);
2893 }
2894
Nicolas Capens96d4e092016-11-18 14:22:38 -05002895 RValue<Byte16> Byte16::operator=(const Reference<Byte16> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002896 {
2897 Value *value = rhs.loadValue();
2898 storeValue(value);
2899
2900 return RValue<Byte16>(value);
2901 }
2902
2903 Type *Byte16::getType()
2904 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002905 return T(Ice::IceType_v16i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002906 }
2907
2908 Type *SByte16::getType()
2909 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002910 return T(Ice::IceType_v16i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002911 }
2912
Nicolas Capens16b5f152016-10-13 13:39:01 -04002913 Short2::Short2(RValue<Short4> cast)
2914 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05002915 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens16b5f152016-10-13 13:39:01 -04002916 }
2917
2918 Type *Short2::getType()
2919 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002920 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04002921 }
2922
2923 UShort2::UShort2(RValue<UShort4> cast)
2924 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05002925 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens16b5f152016-10-13 13:39:01 -04002926 }
2927
2928 Type *UShort2::getType()
2929 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002930 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04002931 }
2932
Nicolas Capens598f8d82016-09-26 15:09:10 -04002933 Short4::Short4(RValue<Int> cast)
2934 {
Nicolas Capensd4227962016-11-09 14:24:25 -05002935 Value *vector = loadValue();
Nicolas Capensbf22bbf2017-01-13 17:37:45 -05002936 Value *element = Nucleus::createTrunc(cast.value, Short::getType());
2937 Value *insert = Nucleus::createInsertElement(vector, element, 0);
Nicolas Capensd4227962016-11-09 14:24:25 -05002938 Value *swizzle = Swizzle(RValue<Short4>(insert), 0x00).value;
Nicolas Capens598f8d82016-09-26 15:09:10 -04002939
2940 storeValue(swizzle);
2941 }
2942
2943 Short4::Short4(RValue<Int4> cast)
2944 {
Nicolas Capensd4227962016-11-09 14:24:25 -05002945 int pshufb[16] = {0, 1, 4, 5, 8, 9, 12, 13, 0, 1, 4, 5, 8, 9, 12, 13};
2946 Value *byte16 = Nucleus::createBitCast(cast.value, Byte16::getType());
2947 Value *packed = Nucleus::createShuffleVector(byte16, byte16, pshufb);
2948
2949 Value *int2 = RValue<Int2>(Int2(RValue<Int4>(packed))).value;
2950 Value *short4 = Nucleus::createBitCast(int2, Short4::getType());
2951
2952 storeValue(short4);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002953 }
2954
2955// Short4::Short4(RValue<Float> cast)
2956// {
2957// }
2958
2959 Short4::Short4(RValue<Float4> cast)
2960 {
2961 assert(false && "UNIMPLEMENTED");
2962 }
2963
Nicolas Capens598f8d82016-09-26 15:09:10 -04002964 Short4::Short4(short xyzw)
2965 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04002966 int64_t constantVector[4] = {xyzw, xyzw, xyzw, xyzw};
2967 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002968 }
2969
2970 Short4::Short4(short x, short y, short z, short w)
2971 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04002972 int64_t constantVector[4] = {x, y, z, w};
2973 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002974 }
2975
2976 Short4::Short4(RValue<Short4> rhs)
2977 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002978 storeValue(rhs.value);
2979 }
2980
2981 Short4::Short4(const Short4 &rhs)
2982 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002983 Value *value = rhs.loadValue();
2984 storeValue(value);
2985 }
2986
2987 Short4::Short4(const Reference<Short4> &rhs)
2988 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002989 Value *value = rhs.loadValue();
2990 storeValue(value);
2991 }
2992
2993 Short4::Short4(RValue<UShort4> rhs)
2994 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002995 storeValue(rhs.value);
2996 }
2997
2998 Short4::Short4(const UShort4 &rhs)
2999 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003000 storeValue(rhs.loadValue());
3001 }
3002
3003 Short4::Short4(const Reference<UShort4> &rhs)
3004 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003005 storeValue(rhs.loadValue());
3006 }
3007
Nicolas Capens96d4e092016-11-18 14:22:38 -05003008 RValue<Short4> Short4::operator=(RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003009 {
3010 storeValue(rhs.value);
3011
3012 return rhs;
3013 }
3014
Nicolas Capens96d4e092016-11-18 14:22:38 -05003015 RValue<Short4> Short4::operator=(const Short4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003016 {
3017 Value *value = rhs.loadValue();
3018 storeValue(value);
3019
3020 return RValue<Short4>(value);
3021 }
3022
Nicolas Capens96d4e092016-11-18 14:22:38 -05003023 RValue<Short4> Short4::operator=(const Reference<Short4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003024 {
3025 Value *value = rhs.loadValue();
3026 storeValue(value);
3027
3028 return RValue<Short4>(value);
3029 }
3030
Nicolas Capens96d4e092016-11-18 14:22:38 -05003031 RValue<Short4> Short4::operator=(RValue<UShort4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003032 {
3033 storeValue(rhs.value);
3034
3035 return RValue<Short4>(rhs);
3036 }
3037
Nicolas Capens96d4e092016-11-18 14:22:38 -05003038 RValue<Short4> Short4::operator=(const UShort4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003039 {
3040 Value *value = rhs.loadValue();
3041 storeValue(value);
3042
3043 return RValue<Short4>(value);
3044 }
3045
Nicolas Capens96d4e092016-11-18 14:22:38 -05003046 RValue<Short4> Short4::operator=(const Reference<UShort4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003047 {
3048 Value *value = rhs.loadValue();
3049 storeValue(value);
3050
3051 return RValue<Short4>(value);
3052 }
3053
3054 RValue<Short4> operator+(RValue<Short4> lhs, RValue<Short4> rhs)
3055 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003056 return RValue<Short4>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003057 }
3058
3059 RValue<Short4> operator-(RValue<Short4> lhs, RValue<Short4> rhs)
3060 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003061 return RValue<Short4>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003062 }
3063
3064 RValue<Short4> operator*(RValue<Short4> lhs, RValue<Short4> rhs)
3065 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003066 return RValue<Short4>(Nucleus::createMul(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003067 }
3068
3069// RValue<Short4> operator/(RValue<Short4> lhs, RValue<Short4> rhs)
3070// {
3071// return RValue<Short4>(Nucleus::createSDiv(lhs.value, rhs.value));
3072// }
3073
3074// RValue<Short4> operator%(RValue<Short4> lhs, RValue<Short4> rhs)
3075// {
3076// return RValue<Short4>(Nucleus::createSRem(lhs.value, rhs.value));
3077// }
3078
3079 RValue<Short4> operator&(RValue<Short4> lhs, RValue<Short4> rhs)
3080 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003081 return RValue<Short4>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003082 }
3083
3084 RValue<Short4> operator|(RValue<Short4> lhs, RValue<Short4> rhs)
3085 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003086 return RValue<Short4>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003087 }
3088
3089 RValue<Short4> operator^(RValue<Short4> lhs, RValue<Short4> rhs)
3090 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003091 return RValue<Short4>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003092 }
3093
3094 RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs)
3095 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003096 return RValue<Short4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003097 }
3098
3099 RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs)
3100 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003101 return RValue<Short4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003102 }
3103
Nicolas Capens96d4e092016-11-18 14:22:38 -05003104 RValue<Short4> operator+=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003105 {
3106 return lhs = lhs + rhs;
3107 }
3108
Nicolas Capens96d4e092016-11-18 14:22:38 -05003109 RValue<Short4> operator-=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003110 {
3111 return lhs = lhs - rhs;
3112 }
3113
Nicolas Capens96d4e092016-11-18 14:22:38 -05003114 RValue<Short4> operator*=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003115 {
3116 return lhs = lhs * rhs;
3117 }
3118
Nicolas Capens96d4e092016-11-18 14:22:38 -05003119// RValue<Short4> operator/=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003120// {
3121// return lhs = lhs / rhs;
3122// }
3123
Nicolas Capens96d4e092016-11-18 14:22:38 -05003124// RValue<Short4> operator%=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003125// {
3126// return lhs = lhs % rhs;
3127// }
3128
Nicolas Capens96d4e092016-11-18 14:22:38 -05003129 RValue<Short4> operator&=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003130 {
3131 return lhs = lhs & rhs;
3132 }
3133
Nicolas Capens96d4e092016-11-18 14:22:38 -05003134 RValue<Short4> operator|=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003135 {
3136 return lhs = lhs | rhs;
3137 }
3138
Nicolas Capens96d4e092016-11-18 14:22:38 -05003139 RValue<Short4> operator^=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003140 {
3141 return lhs = lhs ^ rhs;
3142 }
3143
Nicolas Capens96d4e092016-11-18 14:22:38 -05003144 RValue<Short4> operator<<=(Short4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003145 {
3146 return lhs = lhs << rhs;
3147 }
3148
Nicolas Capens96d4e092016-11-18 14:22:38 -05003149 RValue<Short4> operator>>=(Short4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003150 {
3151 return lhs = lhs >> rhs;
3152 }
3153
Nicolas Capens598f8d82016-09-26 15:09:10 -04003154// RValue<Short4> operator+(RValue<Short4> val)
3155// {
3156// return val;
3157// }
3158
3159 RValue<Short4> operator-(RValue<Short4> val)
3160 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05003161 return RValue<Short4>(Nucleus::createNeg(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003162 }
3163
3164 RValue<Short4> operator~(RValue<Short4> val)
3165 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05003166 return RValue<Short4>(Nucleus::createNot(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003167 }
3168
3169 RValue<Short4> RoundShort4(RValue<Float4> cast)
3170 {
Nicolas Capensd4227962016-11-09 14:24:25 -05003171 RValue<Int4> int4 = RoundInt(cast);
3172 return As<Short4>(Pack(int4, int4));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003173 }
3174
3175 RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y)
3176 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003177 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3178 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value, y.value);
3179 ::basicBlock->appendInst(cmp);
3180
3181 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3182 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3183 ::basicBlock->appendInst(select);
3184
3185 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003186 }
3187
3188 RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y)
3189 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003190 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3191 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value);
3192 ::basicBlock->appendInst(cmp);
3193
3194 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3195 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3196 ::basicBlock->appendInst(select);
3197
3198 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003199 }
3200
3201 RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y)
3202 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003203 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3204 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3205 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3206 auto paddsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3207 paddsw->addArg(x.value);
3208 paddsw->addArg(y.value);
3209 ::basicBlock->appendInst(paddsw);
3210
3211 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003212 }
3213
3214 RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y)
3215 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003216 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3217 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3218 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3219 auto psubsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3220 psubsw->addArg(x.value);
3221 psubsw->addArg(y.value);
3222 ::basicBlock->appendInst(psubsw);
3223
3224 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003225 }
3226
3227 RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y)
3228 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003229 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3230 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3231 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3232 auto pmulhw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3233 pmulhw->addArg(x.value);
3234 pmulhw->addArg(y.value);
3235 ::basicBlock->appendInst(pmulhw);
3236
Nicolas Capens5b41ba32016-12-08 14:34:00 -05003237 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003238 }
3239
3240 RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y)
3241 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003242 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3243 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyAddPairs, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3244 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3245 auto pmaddwd = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3246 pmaddwd->addArg(x.value);
3247 pmaddwd->addArg(y.value);
3248 ::basicBlock->appendInst(pmaddwd);
3249
3250 return RValue<Int2>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003251 }
3252
3253 RValue<SByte8> Pack(RValue<Short4> x, RValue<Short4> y)
3254 {
Nicolas Capensec54a172016-10-25 17:32:37 -04003255 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
3256 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3257 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3258 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3259 pack->addArg(x.value);
3260 pack->addArg(y.value);
3261 ::basicBlock->appendInst(pack);
3262
Nicolas Capens70dfff42016-10-27 10:20:28 -04003263 return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x88));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003264 }
3265
3266 RValue<Int2> UnpackLow(RValue<Short4> x, RValue<Short4> y)
3267 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04003268 int shuffle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; // Real type is v8i16
3269 return RValue<Int2>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003270 }
3271
3272 RValue<Int2> UnpackHigh(RValue<Short4> x, RValue<Short4> y)
3273 {
Nicolas Capens20e22c42016-10-25 17:32:37 -04003274 int shuffle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; // Real type is v8i16
3275 auto lowHigh = RValue<Short8>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
3276 return As<Int2>(Swizzle(As<Int4>(lowHigh), 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003277 }
3278
3279 RValue<Short4> Swizzle(RValue<Short4> x, unsigned char select)
3280 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04003281 // Real type is v8i16
3282 int shuffle[8] =
3283 {
3284 (select >> 0) & 0x03,
3285 (select >> 2) & 0x03,
3286 (select >> 4) & 0x03,
3287 (select >> 6) & 0x03,
3288 (select >> 0) & 0x03,
3289 (select >> 2) & 0x03,
3290 (select >> 4) & 0x03,
3291 (select >> 6) & 0x03,
3292 };
3293
3294 return RValue<Short4>(Nucleus::createShuffleVector(x.value, x.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003295 }
3296
3297 RValue<Short4> Insert(RValue<Short4> val, RValue<Short> element, int i)
3298 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05003299 return RValue<Short4>(Nucleus::createInsertElement(val.value, element.value, i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003300 }
3301
3302 RValue<Short> Extract(RValue<Short4> val, int i)
3303 {
Nicolas Capens0133d0f2017-01-16 16:25:08 -05003304 return RValue<Short>(Nucleus::createExtractElement(val.value, Short::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003305 }
3306
3307 RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y)
3308 {
Nicolas Capens2f970b62016-11-08 14:28:59 -05003309 return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003310 }
3311
3312 RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y)
3313 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003314 return RValue<Short4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003315 }
3316
3317 Type *Short4::getType()
3318 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003319 return T(Type_v4i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003320 }
3321
3322 UShort4::UShort4(RValue<Int4> cast)
3323 {
3324 *this = Short4(cast);
3325 }
3326
3327 UShort4::UShort4(RValue<Float4> cast, bool saturate)
3328 {
Nicolas Capensd4227962016-11-09 14:24:25 -05003329 if(saturate)
3330 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003331 if(CPUID::SSE4_1)
Nicolas Capensd4227962016-11-09 14:24:25 -05003332 {
3333 Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation
3334 *this = As<Short4>(Pack(As<UInt4>(int4), As<UInt4>(int4)));
3335 }
3336 else
3337 {
3338 *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000))));
3339 }
3340 }
3341 else
3342 {
3343 *this = Short4(Int4(cast));
3344 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04003345 }
3346
Nicolas Capens598f8d82016-09-26 15:09:10 -04003347 UShort4::UShort4(unsigned short xyzw)
3348 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003349 int64_t constantVector[4] = {xyzw, xyzw, xyzw, xyzw};
3350 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003351 }
3352
3353 UShort4::UShort4(unsigned short x, unsigned short y, unsigned short z, unsigned short w)
3354 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003355 int64_t constantVector[4] = {x, y, z, w};
3356 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003357 }
3358
3359 UShort4::UShort4(RValue<UShort4> rhs)
3360 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003361 storeValue(rhs.value);
3362 }
3363
3364 UShort4::UShort4(const UShort4 &rhs)
3365 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003366 Value *value = rhs.loadValue();
3367 storeValue(value);
3368 }
3369
3370 UShort4::UShort4(const Reference<UShort4> &rhs)
3371 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003372 Value *value = rhs.loadValue();
3373 storeValue(value);
3374 }
3375
3376 UShort4::UShort4(RValue<Short4> rhs)
3377 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003378 storeValue(rhs.value);
3379 }
3380
3381 UShort4::UShort4(const Short4 &rhs)
3382 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003383 Value *value = rhs.loadValue();
3384 storeValue(value);
3385 }
3386
3387 UShort4::UShort4(const Reference<Short4> &rhs)
3388 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003389 Value *value = rhs.loadValue();
3390 storeValue(value);
3391 }
3392
Nicolas Capens96d4e092016-11-18 14:22:38 -05003393 RValue<UShort4> UShort4::operator=(RValue<UShort4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003394 {
3395 storeValue(rhs.value);
3396
3397 return rhs;
3398 }
3399
Nicolas Capens96d4e092016-11-18 14:22:38 -05003400 RValue<UShort4> UShort4::operator=(const UShort4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003401 {
3402 Value *value = rhs.loadValue();
3403 storeValue(value);
3404
3405 return RValue<UShort4>(value);
3406 }
3407
Nicolas Capens96d4e092016-11-18 14:22:38 -05003408 RValue<UShort4> UShort4::operator=(const Reference<UShort4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003409 {
3410 Value *value = rhs.loadValue();
3411 storeValue(value);
3412
3413 return RValue<UShort4>(value);
3414 }
3415
Nicolas Capens96d4e092016-11-18 14:22:38 -05003416 RValue<UShort4> UShort4::operator=(RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003417 {
3418 storeValue(rhs.value);
3419
3420 return RValue<UShort4>(rhs);
3421 }
3422
Nicolas Capens96d4e092016-11-18 14:22:38 -05003423 RValue<UShort4> UShort4::operator=(const Short4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003424 {
3425 Value *value = rhs.loadValue();
3426 storeValue(value);
3427
3428 return RValue<UShort4>(value);
3429 }
3430
Nicolas Capens96d4e092016-11-18 14:22:38 -05003431 RValue<UShort4> UShort4::operator=(const Reference<Short4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003432 {
3433 Value *value = rhs.loadValue();
3434 storeValue(value);
3435
3436 return RValue<UShort4>(value);
3437 }
3438
3439 RValue<UShort4> operator+(RValue<UShort4> lhs, RValue<UShort4> rhs)
3440 {
Nicolas Capens5b41ba32016-12-08 14:34:00 -05003441 return RValue<UShort4>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003442 }
3443
3444 RValue<UShort4> operator-(RValue<UShort4> lhs, RValue<UShort4> rhs)
3445 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003446 return RValue<UShort4>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003447 }
3448
3449 RValue<UShort4> operator*(RValue<UShort4> lhs, RValue<UShort4> rhs)
3450 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003451 return RValue<UShort4>(Nucleus::createMul(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003452 }
3453
Nicolas Capens16b5f152016-10-13 13:39:01 -04003454 RValue<UShort4> operator&(RValue<UShort4> lhs, RValue<UShort4> rhs)
3455 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003456 return RValue<UShort4>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003457 }
3458
3459 RValue<UShort4> operator|(RValue<UShort4> lhs, RValue<UShort4> rhs)
3460 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003461 return RValue<UShort4>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003462 }
3463
3464 RValue<UShort4> operator^(RValue<UShort4> lhs, RValue<UShort4> rhs)
3465 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003466 return RValue<UShort4>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003467 }
3468
Nicolas Capens598f8d82016-09-26 15:09:10 -04003469 RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs)
3470 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003471 return RValue<UShort4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003472 }
3473
3474 RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs)
3475 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003476 return RValue<UShort4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003477 }
3478
Nicolas Capens96d4e092016-11-18 14:22:38 -05003479 RValue<UShort4> operator<<=(UShort4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003480 {
3481 return lhs = lhs << rhs;
3482 }
3483
Nicolas Capens96d4e092016-11-18 14:22:38 -05003484 RValue<UShort4> operator>>=(UShort4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003485 {
3486 return lhs = lhs >> rhs;
3487 }
3488
Nicolas Capens598f8d82016-09-26 15:09:10 -04003489 RValue<UShort4> operator~(RValue<UShort4> val)
3490 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05003491 return RValue<UShort4>(Nucleus::createNot(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003492 }
3493
3494 RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y)
3495 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003496 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3497 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value, y.value);
3498 ::basicBlock->appendInst(cmp);
3499
3500 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3501 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3502 ::basicBlock->appendInst(select);
3503
3504 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003505 }
3506
3507 RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y)
3508 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003509 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3510 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value);
3511 ::basicBlock->appendInst(cmp);
3512
3513 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3514 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3515 ::basicBlock->appendInst(select);
3516
3517 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003518 }
3519
3520 RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y)
3521 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003522 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3523 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3524 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3525 auto paddusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3526 paddusw->addArg(x.value);
3527 paddusw->addArg(y.value);
3528 ::basicBlock->appendInst(paddusw);
3529
3530 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003531 }
3532
3533 RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y)
3534 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003535 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3536 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3537 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3538 auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3539 psubusw->addArg(x.value);
3540 psubusw->addArg(y.value);
3541 ::basicBlock->appendInst(psubusw);
3542
3543 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003544 }
3545
3546 RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y)
3547 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003548 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3549 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3550 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3551 auto pmulhuw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3552 pmulhuw->addArg(x.value);
3553 pmulhuw->addArg(y.value);
3554 ::basicBlock->appendInst(pmulhuw);
3555
3556 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003557 }
3558
3559 RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)
3560 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003561 assert(false && "UNIMPLEMENTED"); return RValue<UShort4>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003562 }
3563
3564 RValue<Byte8> Pack(RValue<UShort4> x, RValue<UShort4> y)
3565 {
Nicolas Capensec54a172016-10-25 17:32:37 -04003566 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
3567 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3568 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3569 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3570 pack->addArg(x.value);
3571 pack->addArg(y.value);
3572 ::basicBlock->appendInst(pack);
3573
Nicolas Capens70dfff42016-10-27 10:20:28 -04003574 return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x88));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003575 }
3576
3577 Type *UShort4::getType()
3578 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003579 return T(Type_v4i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003580 }
3581
Nicolas Capens3e7062b2017-01-17 14:01:33 -05003582 Short8::Short8(short c)
3583 {
3584 int64_t constantVector[8] = {c, c, c, c, c, c, c, c};
3585 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3586 }
3587
Nicolas Capens598f8d82016-09-26 15:09:10 -04003588 Short8::Short8(short c0, short c1, short c2, short c3, short c4, short c5, short c6, short c7)
3589 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003590 int64_t constantVector[8] = {c0, c1, c2, c3, c4, c5, c6, c7};
3591 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003592 }
3593
3594 Short8::Short8(RValue<Short8> rhs)
3595 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003596 storeValue(rhs.value);
3597 }
3598
3599 Short8::Short8(const Reference<Short8> &rhs)
3600 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003601 Value *value = rhs.loadValue();
3602 storeValue(value);
3603 }
3604
3605 Short8::Short8(RValue<Short4> lo, RValue<Short4> hi)
3606 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05003607 int shuffle[8] = {0, 1, 2, 3, 8, 9, 10, 11}; // Real type is v8i16
3608 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
3609
3610 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003611 }
3612
3613 RValue<Short8> operator+(RValue<Short8> lhs, RValue<Short8> rhs)
3614 {
3615 return RValue<Short8>(Nucleus::createAdd(lhs.value, rhs.value));
3616 }
3617
3618 RValue<Short8> operator&(RValue<Short8> lhs, RValue<Short8> rhs)
3619 {
3620 return RValue<Short8>(Nucleus::createAnd(lhs.value, rhs.value));
3621 }
3622
3623 RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs)
3624 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003625 return RValue<Short8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003626 }
3627
3628 RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs)
3629 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003630 return RValue<Short8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003631 }
3632
3633 RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)
3634 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003635 assert(false && "UNIMPLEMENTED"); return RValue<Int4>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003636 }
3637
3638 RValue<Int4> Abs(RValue<Int4> x)
3639 {
Nicolas Capens84272242016-11-09 13:31:06 -05003640 auto negative = x >> 31;
3641 return (x ^ negative) - negative;
Nicolas Capens598f8d82016-09-26 15:09:10 -04003642 }
3643
3644 RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)
3645 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003646 assert(false && "UNIMPLEMENTED"); return RValue<Short8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003647 }
3648
3649 Type *Short8::getType()
3650 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04003651 return T(Ice::IceType_v8i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003652 }
3653
Nicolas Capens3e7062b2017-01-17 14:01:33 -05003654 UShort8::UShort8(unsigned short c)
3655 {
3656 int64_t constantVector[8] = {c, c, c, c, c, c, c, c};
3657 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3658 }
3659
Nicolas Capens598f8d82016-09-26 15:09:10 -04003660 UShort8::UShort8(unsigned short c0, unsigned short c1, unsigned short c2, unsigned short c3, unsigned short c4, unsigned short c5, unsigned short c6, unsigned short c7)
3661 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003662 int64_t constantVector[8] = {c0, c1, c2, c3, c4, c5, c6, c7};
3663 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003664 }
3665
3666 UShort8::UShort8(RValue<UShort8> rhs)
3667 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003668 storeValue(rhs.value);
3669 }
3670
3671 UShort8::UShort8(const Reference<UShort8> &rhs)
3672 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003673 Value *value = rhs.loadValue();
3674 storeValue(value);
3675 }
3676
3677 UShort8::UShort8(RValue<UShort4> lo, RValue<UShort4> hi)
3678 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05003679 int shuffle[8] = {0, 1, 2, 3, 8, 9, 10, 11}; // Real type is v8i16
3680 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
3681
3682 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003683 }
3684
Nicolas Capens96d4e092016-11-18 14:22:38 -05003685 RValue<UShort8> UShort8::operator=(RValue<UShort8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003686 {
3687 storeValue(rhs.value);
3688
3689 return rhs;
3690 }
3691
Nicolas Capens96d4e092016-11-18 14:22:38 -05003692 RValue<UShort8> UShort8::operator=(const UShort8 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003693 {
3694 Value *value = rhs.loadValue();
3695 storeValue(value);
3696
3697 return RValue<UShort8>(value);
3698 }
3699
Nicolas Capens96d4e092016-11-18 14:22:38 -05003700 RValue<UShort8> UShort8::operator=(const Reference<UShort8> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003701 {
3702 Value *value = rhs.loadValue();
3703 storeValue(value);
3704
3705 return RValue<UShort8>(value);
3706 }
3707
3708 RValue<UShort8> operator&(RValue<UShort8> lhs, RValue<UShort8> rhs)
3709 {
3710 return RValue<UShort8>(Nucleus::createAnd(lhs.value, rhs.value));
3711 }
3712
3713 RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs)
3714 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003715 return RValue<UShort8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003716 }
3717
3718 RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs)
3719 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003720 return RValue<UShort8>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003721 }
3722
3723 RValue<UShort8> operator+(RValue<UShort8> lhs, RValue<UShort8> rhs)
3724 {
3725 return RValue<UShort8>(Nucleus::createAdd(lhs.value, rhs.value));
3726 }
3727
3728 RValue<UShort8> operator*(RValue<UShort8> lhs, RValue<UShort8> rhs)
3729 {
3730 return RValue<UShort8>(Nucleus::createMul(lhs.value, rhs.value));
3731 }
3732
Nicolas Capens96d4e092016-11-18 14:22:38 -05003733 RValue<UShort8> operator+=(UShort8 &lhs, RValue<UShort8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003734 {
3735 return lhs = lhs + rhs;
3736 }
3737
3738 RValue<UShort8> operator~(RValue<UShort8> val)
3739 {
3740 return RValue<UShort8>(Nucleus::createNot(val.value));
3741 }
3742
3743 RValue<UShort8> Swizzle(RValue<UShort8> x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7)
3744 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003745 assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003746 }
3747
3748 RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)
3749 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003750 assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003751 }
3752
3753 // FIXME: Implement as Shuffle(x, y, Select(i0, ..., i16)) and Shuffle(x, y, SELECT_PACK_REPEAT(element))
3754// RValue<UShort8> PackRepeat(RValue<Byte16> x, RValue<Byte16> y, int element)
3755// {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003756// assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003757// }
3758
3759 Type *UShort8::getType()
3760 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04003761 return T(Ice::IceType_v8i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003762 }
3763
3764 Int::Int(Argument<Int> argument)
3765 {
3766 storeValue(argument.value);
3767 }
3768
3769 Int::Int(RValue<Byte> cast)
3770 {
3771 Value *integer = Nucleus::createZExt(cast.value, Int::getType());
3772
3773 storeValue(integer);
3774 }
3775
3776 Int::Int(RValue<SByte> cast)
3777 {
3778 Value *integer = Nucleus::createSExt(cast.value, Int::getType());
3779
3780 storeValue(integer);
3781 }
3782
3783 Int::Int(RValue<Short> cast)
3784 {
3785 Value *integer = Nucleus::createSExt(cast.value, Int::getType());
3786
3787 storeValue(integer);
3788 }
3789
3790 Int::Int(RValue<UShort> cast)
3791 {
3792 Value *integer = Nucleus::createZExt(cast.value, Int::getType());
3793
3794 storeValue(integer);
3795 }
3796
3797 Int::Int(RValue<Int2> cast)
3798 {
3799 *this = Extract(cast, 0);
3800 }
3801
3802 Int::Int(RValue<Long> cast)
3803 {
3804 Value *integer = Nucleus::createTrunc(cast.value, Int::getType());
3805
3806 storeValue(integer);
3807 }
3808
3809 Int::Int(RValue<Float> cast)
3810 {
3811 Value *integer = Nucleus::createFPToSI(cast.value, Int::getType());
3812
3813 storeValue(integer);
3814 }
3815
Nicolas Capens598f8d82016-09-26 15:09:10 -04003816 Int::Int(int x)
3817 {
3818 storeValue(Nucleus::createConstantInt(x));
3819 }
3820
3821 Int::Int(RValue<Int> rhs)
3822 {
3823 storeValue(rhs.value);
3824 }
3825
3826 Int::Int(RValue<UInt> rhs)
3827 {
3828 storeValue(rhs.value);
3829 }
3830
3831 Int::Int(const Int &rhs)
3832 {
3833 Value *value = rhs.loadValue();
3834 storeValue(value);
3835 }
3836
3837 Int::Int(const Reference<Int> &rhs)
3838 {
3839 Value *value = rhs.loadValue();
3840 storeValue(value);
3841 }
3842
3843 Int::Int(const UInt &rhs)
3844 {
3845 Value *value = rhs.loadValue();
3846 storeValue(value);
3847 }
3848
3849 Int::Int(const Reference<UInt> &rhs)
3850 {
3851 Value *value = rhs.loadValue();
3852 storeValue(value);
3853 }
3854
Nicolas Capens96d4e092016-11-18 14:22:38 -05003855 RValue<Int> Int::operator=(int rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003856 {
3857 return RValue<Int>(storeValue(Nucleus::createConstantInt(rhs)));
3858 }
3859
Nicolas Capens96d4e092016-11-18 14:22:38 -05003860 RValue<Int> Int::operator=(RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003861 {
3862 storeValue(rhs.value);
3863
3864 return rhs;
3865 }
3866
Nicolas Capens96d4e092016-11-18 14:22:38 -05003867 RValue<Int> Int::operator=(RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003868 {
3869 storeValue(rhs.value);
3870
3871 return RValue<Int>(rhs);
3872 }
3873
Nicolas Capens96d4e092016-11-18 14:22:38 -05003874 RValue<Int> Int::operator=(const Int &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003875 {
3876 Value *value = rhs.loadValue();
3877 storeValue(value);
3878
3879 return RValue<Int>(value);
3880 }
3881
Nicolas Capens96d4e092016-11-18 14:22:38 -05003882 RValue<Int> Int::operator=(const Reference<Int> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003883 {
3884 Value *value = rhs.loadValue();
3885 storeValue(value);
3886
3887 return RValue<Int>(value);
3888 }
3889
Nicolas Capens96d4e092016-11-18 14:22:38 -05003890 RValue<Int> Int::operator=(const UInt &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003891 {
3892 Value *value = rhs.loadValue();
3893 storeValue(value);
3894
3895 return RValue<Int>(value);
3896 }
3897
Nicolas Capens96d4e092016-11-18 14:22:38 -05003898 RValue<Int> Int::operator=(const Reference<UInt> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003899 {
3900 Value *value = rhs.loadValue();
3901 storeValue(value);
3902
3903 return RValue<Int>(value);
3904 }
3905
3906 RValue<Int> operator+(RValue<Int> lhs, RValue<Int> rhs)
3907 {
3908 return RValue<Int>(Nucleus::createAdd(lhs.value, rhs.value));
3909 }
3910
3911 RValue<Int> operator-(RValue<Int> lhs, RValue<Int> rhs)
3912 {
3913 return RValue<Int>(Nucleus::createSub(lhs.value, rhs.value));
3914 }
3915
3916 RValue<Int> operator*(RValue<Int> lhs, RValue<Int> rhs)
3917 {
3918 return RValue<Int>(Nucleus::createMul(lhs.value, rhs.value));
3919 }
3920
3921 RValue<Int> operator/(RValue<Int> lhs, RValue<Int> rhs)
3922 {
3923 return RValue<Int>(Nucleus::createSDiv(lhs.value, rhs.value));
3924 }
3925
3926 RValue<Int> operator%(RValue<Int> lhs, RValue<Int> rhs)
3927 {
3928 return RValue<Int>(Nucleus::createSRem(lhs.value, rhs.value));
3929 }
3930
3931 RValue<Int> operator&(RValue<Int> lhs, RValue<Int> rhs)
3932 {
3933 return RValue<Int>(Nucleus::createAnd(lhs.value, rhs.value));
3934 }
3935
3936 RValue<Int> operator|(RValue<Int> lhs, RValue<Int> rhs)
3937 {
3938 return RValue<Int>(Nucleus::createOr(lhs.value, rhs.value));
3939 }
3940
3941 RValue<Int> operator^(RValue<Int> lhs, RValue<Int> rhs)
3942 {
3943 return RValue<Int>(Nucleus::createXor(lhs.value, rhs.value));
3944 }
3945
3946 RValue<Int> operator<<(RValue<Int> lhs, RValue<Int> rhs)
3947 {
3948 return RValue<Int>(Nucleus::createShl(lhs.value, rhs.value));
3949 }
3950
3951 RValue<Int> operator>>(RValue<Int> lhs, RValue<Int> rhs)
3952 {
3953 return RValue<Int>(Nucleus::createAShr(lhs.value, rhs.value));
3954 }
3955
Nicolas Capens96d4e092016-11-18 14:22:38 -05003956 RValue<Int> operator+=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003957 {
3958 return lhs = lhs + rhs;
3959 }
3960
Nicolas Capens96d4e092016-11-18 14:22:38 -05003961 RValue<Int> operator-=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003962 {
3963 return lhs = lhs - rhs;
3964 }
3965
Nicolas Capens96d4e092016-11-18 14:22:38 -05003966 RValue<Int> operator*=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003967 {
3968 return lhs = lhs * rhs;
3969 }
3970
Nicolas Capens96d4e092016-11-18 14:22:38 -05003971 RValue<Int> operator/=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003972 {
3973 return lhs = lhs / rhs;
3974 }
3975
Nicolas Capens96d4e092016-11-18 14:22:38 -05003976 RValue<Int> operator%=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003977 {
3978 return lhs = lhs % rhs;
3979 }
3980
Nicolas Capens96d4e092016-11-18 14:22:38 -05003981 RValue<Int> operator&=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003982 {
3983 return lhs = lhs & rhs;
3984 }
3985
Nicolas Capens96d4e092016-11-18 14:22:38 -05003986 RValue<Int> operator|=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003987 {
3988 return lhs = lhs | rhs;
3989 }
3990
Nicolas Capens96d4e092016-11-18 14:22:38 -05003991 RValue<Int> operator^=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003992 {
3993 return lhs = lhs ^ rhs;
3994 }
3995
Nicolas Capens96d4e092016-11-18 14:22:38 -05003996 RValue<Int> operator<<=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003997 {
3998 return lhs = lhs << rhs;
3999 }
4000
Nicolas Capens96d4e092016-11-18 14:22:38 -05004001 RValue<Int> operator>>=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004002 {
4003 return lhs = lhs >> rhs;
4004 }
4005
4006 RValue<Int> operator+(RValue<Int> val)
4007 {
4008 return val;
4009 }
4010
4011 RValue<Int> operator-(RValue<Int> val)
4012 {
4013 return RValue<Int>(Nucleus::createNeg(val.value));
4014 }
4015
4016 RValue<Int> operator~(RValue<Int> val)
4017 {
4018 return RValue<Int>(Nucleus::createNot(val.value));
4019 }
4020
Nicolas Capens96d4e092016-11-18 14:22:38 -05004021 RValue<Int> operator++(Int &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004022 {
Nicolas Capens5b41ba32016-12-08 14:34:00 -05004023 RValue<Int> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05004024 val += 1;
4025 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004026 }
4027
Nicolas Capens96d4e092016-11-18 14:22:38 -05004028 const Int &operator++(Int &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004029 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004030 val += 1;
4031 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004032 }
4033
Nicolas Capens96d4e092016-11-18 14:22:38 -05004034 RValue<Int> operator--(Int &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004035 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004036 RValue<Int> res = val;
4037 val -= 1;
4038 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004039 }
4040
Nicolas Capens96d4e092016-11-18 14:22:38 -05004041 const Int &operator--(Int &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004042 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004043 val -= 1;
4044 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004045 }
4046
4047 RValue<Bool> operator<(RValue<Int> lhs, RValue<Int> rhs)
4048 {
4049 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
4050 }
4051
4052 RValue<Bool> operator<=(RValue<Int> lhs, RValue<Int> rhs)
4053 {
4054 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
4055 }
4056
4057 RValue<Bool> operator>(RValue<Int> lhs, RValue<Int> rhs)
4058 {
4059 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
4060 }
4061
4062 RValue<Bool> operator>=(RValue<Int> lhs, RValue<Int> rhs)
4063 {
4064 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
4065 }
4066
4067 RValue<Bool> operator!=(RValue<Int> lhs, RValue<Int> rhs)
4068 {
4069 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
4070 }
4071
4072 RValue<Bool> operator==(RValue<Int> lhs, RValue<Int> rhs)
4073 {
4074 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
4075 }
4076
4077 RValue<Int> Max(RValue<Int> x, RValue<Int> y)
4078 {
4079 return IfThenElse(x > y, x, y);
4080 }
4081
4082 RValue<Int> Min(RValue<Int> x, RValue<Int> y)
4083 {
4084 return IfThenElse(x < y, x, y);
4085 }
4086
4087 RValue<Int> Clamp(RValue<Int> x, RValue<Int> min, RValue<Int> max)
4088 {
4089 return Min(Max(x, min), max);
4090 }
4091
4092 RValue<Int> RoundInt(RValue<Float> cast)
4093 {
Nicolas Capensa8086512016-11-07 17:32:17 -05004094 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
Nicolas Capens9ca48d52017-01-14 12:52:55 -05004095 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
4096 auto target = ::context->getConstantUndef(Ice::IceType_i32);
4097 auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
4098 nearbyint->addArg(cast.value);
4099 ::basicBlock->appendInst(nearbyint);
Nicolas Capensa8086512016-11-07 17:32:17 -05004100
4101 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004102 }
4103
4104 Type *Int::getType()
4105 {
4106 return T(Ice::IceType_i32);
4107 }
4108
4109 Long::Long(RValue<Int> cast)
4110 {
4111 Value *integer = Nucleus::createSExt(cast.value, Long::getType());
4112
4113 storeValue(integer);
4114 }
4115
4116 Long::Long(RValue<UInt> cast)
4117 {
4118 Value *integer = Nucleus::createZExt(cast.value, Long::getType());
4119
4120 storeValue(integer);
4121 }
4122
Nicolas Capens598f8d82016-09-26 15:09:10 -04004123 Long::Long(RValue<Long> rhs)
4124 {
4125 storeValue(rhs.value);
4126 }
4127
Nicolas Capens96d4e092016-11-18 14:22:38 -05004128 RValue<Long> Long::operator=(int64_t rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004129 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04004130 return RValue<Long>(storeValue(Nucleus::createConstantLong(rhs)));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004131 }
4132
Nicolas Capens96d4e092016-11-18 14:22:38 -05004133 RValue<Long> Long::operator=(RValue<Long> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004134 {
4135 storeValue(rhs.value);
4136
4137 return rhs;
4138 }
4139
Nicolas Capens96d4e092016-11-18 14:22:38 -05004140 RValue<Long> Long::operator=(const Long &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004141 {
4142 Value *value = rhs.loadValue();
4143 storeValue(value);
4144
4145 return RValue<Long>(value);
4146 }
4147
Nicolas Capens96d4e092016-11-18 14:22:38 -05004148 RValue<Long> Long::operator=(const Reference<Long> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004149 {
4150 Value *value = rhs.loadValue();
4151 storeValue(value);
4152
4153 return RValue<Long>(value);
4154 }
4155
4156 RValue<Long> operator+(RValue<Long> lhs, RValue<Long> rhs)
4157 {
4158 return RValue<Long>(Nucleus::createAdd(lhs.value, rhs.value));
4159 }
4160
4161 RValue<Long> operator-(RValue<Long> lhs, RValue<Long> rhs)
4162 {
4163 return RValue<Long>(Nucleus::createSub(lhs.value, rhs.value));
4164 }
4165
Nicolas Capens96d4e092016-11-18 14:22:38 -05004166 RValue<Long> operator+=(Long &lhs, RValue<Long> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004167 {
4168 return lhs = lhs + rhs;
4169 }
4170
Nicolas Capens96d4e092016-11-18 14:22:38 -05004171 RValue<Long> operator-=(Long &lhs, RValue<Long> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004172 {
4173 return lhs = lhs - rhs;
4174 }
4175
4176 RValue<Long> AddAtomic(RValue<Pointer<Long> > x, RValue<Long> y)
4177 {
4178 return RValue<Long>(Nucleus::createAtomicAdd(x.value, y.value));
4179 }
4180
4181 Type *Long::getType()
4182 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04004183 return T(Ice::IceType_i64);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004184 }
4185
Nicolas Capens598f8d82016-09-26 15:09:10 -04004186 UInt::UInt(Argument<UInt> argument)
4187 {
4188 storeValue(argument.value);
4189 }
4190
4191 UInt::UInt(RValue<UShort> cast)
4192 {
4193 Value *integer = Nucleus::createZExt(cast.value, UInt::getType());
4194
4195 storeValue(integer);
4196 }
4197
4198 UInt::UInt(RValue<Long> cast)
4199 {
4200 Value *integer = Nucleus::createTrunc(cast.value, UInt::getType());
4201
4202 storeValue(integer);
4203 }
4204
4205 UInt::UInt(RValue<Float> cast)
4206 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05004207 // Smallest positive value representable in UInt, but not in Int
4208 const unsigned int ustart = 0x80000000u;
4209 const float ustartf = float(ustart);
4210
4211 // If the value is negative, store 0, otherwise store the result of the conversion
4212 storeValue((~(As<Int>(cast) >> 31) &
4213 // Check if the value can be represented as an Int
4214 IfThenElse(cast >= ustartf,
4215 // If the value is too large, subtract ustart and re-add it after conversion.
4216 As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)),
4217 // Otherwise, just convert normally
4218 Int(cast))).value);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004219 }
4220
Nicolas Capens598f8d82016-09-26 15:09:10 -04004221 UInt::UInt(int x)
4222 {
4223 storeValue(Nucleus::createConstantInt(x));
4224 }
4225
4226 UInt::UInt(unsigned int x)
4227 {
4228 storeValue(Nucleus::createConstantInt(x));
4229 }
4230
4231 UInt::UInt(RValue<UInt> rhs)
4232 {
4233 storeValue(rhs.value);
4234 }
4235
4236 UInt::UInt(RValue<Int> rhs)
4237 {
4238 storeValue(rhs.value);
4239 }
4240
4241 UInt::UInt(const UInt &rhs)
4242 {
4243 Value *value = rhs.loadValue();
4244 storeValue(value);
4245 }
4246
4247 UInt::UInt(const Reference<UInt> &rhs)
4248 {
4249 Value *value = rhs.loadValue();
4250 storeValue(value);
4251 }
4252
4253 UInt::UInt(const Int &rhs)
4254 {
4255 Value *value = rhs.loadValue();
4256 storeValue(value);
4257 }
4258
4259 UInt::UInt(const Reference<Int> &rhs)
4260 {
4261 Value *value = rhs.loadValue();
4262 storeValue(value);
4263 }
4264
Nicolas Capens96d4e092016-11-18 14:22:38 -05004265 RValue<UInt> UInt::operator=(unsigned int rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004266 {
4267 return RValue<UInt>(storeValue(Nucleus::createConstantInt(rhs)));
4268 }
4269
Nicolas Capens96d4e092016-11-18 14:22:38 -05004270 RValue<UInt> UInt::operator=(RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004271 {
4272 storeValue(rhs.value);
4273
4274 return rhs;
4275 }
4276
Nicolas Capens96d4e092016-11-18 14:22:38 -05004277 RValue<UInt> UInt::operator=(RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004278 {
4279 storeValue(rhs.value);
4280
4281 return RValue<UInt>(rhs);
4282 }
4283
Nicolas Capens96d4e092016-11-18 14:22:38 -05004284 RValue<UInt> UInt::operator=(const UInt &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004285 {
4286 Value *value = rhs.loadValue();
4287 storeValue(value);
4288
4289 return RValue<UInt>(value);
4290 }
4291
Nicolas Capens96d4e092016-11-18 14:22:38 -05004292 RValue<UInt> UInt::operator=(const Reference<UInt> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004293 {
4294 Value *value = rhs.loadValue();
4295 storeValue(value);
4296
4297 return RValue<UInt>(value);
4298 }
4299
Nicolas Capens96d4e092016-11-18 14:22:38 -05004300 RValue<UInt> UInt::operator=(const Int &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004301 {
4302 Value *value = rhs.loadValue();
4303 storeValue(value);
4304
4305 return RValue<UInt>(value);
4306 }
4307
Nicolas Capens96d4e092016-11-18 14:22:38 -05004308 RValue<UInt> UInt::operator=(const Reference<Int> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004309 {
4310 Value *value = rhs.loadValue();
4311 storeValue(value);
4312
4313 return RValue<UInt>(value);
4314 }
4315
4316 RValue<UInt> operator+(RValue<UInt> lhs, RValue<UInt> rhs)
4317 {
4318 return RValue<UInt>(Nucleus::createAdd(lhs.value, rhs.value));
4319 }
4320
4321 RValue<UInt> operator-(RValue<UInt> lhs, RValue<UInt> rhs)
4322 {
4323 return RValue<UInt>(Nucleus::createSub(lhs.value, rhs.value));
4324 }
4325
4326 RValue<UInt> operator*(RValue<UInt> lhs, RValue<UInt> rhs)
4327 {
4328 return RValue<UInt>(Nucleus::createMul(lhs.value, rhs.value));
4329 }
4330
4331 RValue<UInt> operator/(RValue<UInt> lhs, RValue<UInt> rhs)
4332 {
4333 return RValue<UInt>(Nucleus::createUDiv(lhs.value, rhs.value));
4334 }
4335
4336 RValue<UInt> operator%(RValue<UInt> lhs, RValue<UInt> rhs)
4337 {
4338 return RValue<UInt>(Nucleus::createURem(lhs.value, rhs.value));
4339 }
4340
4341 RValue<UInt> operator&(RValue<UInt> lhs, RValue<UInt> rhs)
4342 {
4343 return RValue<UInt>(Nucleus::createAnd(lhs.value, rhs.value));
4344 }
4345
4346 RValue<UInt> operator|(RValue<UInt> lhs, RValue<UInt> rhs)
4347 {
4348 return RValue<UInt>(Nucleus::createOr(lhs.value, rhs.value));
4349 }
4350
4351 RValue<UInt> operator^(RValue<UInt> lhs, RValue<UInt> rhs)
4352 {
4353 return RValue<UInt>(Nucleus::createXor(lhs.value, rhs.value));
4354 }
4355
4356 RValue<UInt> operator<<(RValue<UInt> lhs, RValue<UInt> rhs)
4357 {
4358 return RValue<UInt>(Nucleus::createShl(lhs.value, rhs.value));
4359 }
4360
4361 RValue<UInt> operator>>(RValue<UInt> lhs, RValue<UInt> rhs)
4362 {
4363 return RValue<UInt>(Nucleus::createLShr(lhs.value, rhs.value));
4364 }
4365
Nicolas Capens96d4e092016-11-18 14:22:38 -05004366 RValue<UInt> operator+=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004367 {
4368 return lhs = lhs + rhs;
4369 }
4370
Nicolas Capens96d4e092016-11-18 14:22:38 -05004371 RValue<UInt> operator-=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004372 {
4373 return lhs = lhs - rhs;
4374 }
4375
Nicolas Capens96d4e092016-11-18 14:22:38 -05004376 RValue<UInt> operator*=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004377 {
4378 return lhs = lhs * rhs;
4379 }
4380
Nicolas Capens96d4e092016-11-18 14:22:38 -05004381 RValue<UInt> operator/=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004382 {
4383 return lhs = lhs / rhs;
4384 }
4385
Nicolas Capens96d4e092016-11-18 14:22:38 -05004386 RValue<UInt> operator%=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004387 {
4388 return lhs = lhs % rhs;
4389 }
4390
Nicolas Capens96d4e092016-11-18 14:22:38 -05004391 RValue<UInt> operator&=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004392 {
4393 return lhs = lhs & rhs;
4394 }
4395
Nicolas Capens96d4e092016-11-18 14:22:38 -05004396 RValue<UInt> operator|=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004397 {
4398 return lhs = lhs | rhs;
4399 }
4400
Nicolas Capens96d4e092016-11-18 14:22:38 -05004401 RValue<UInt> operator^=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004402 {
4403 return lhs = lhs ^ rhs;
4404 }
4405
Nicolas Capens96d4e092016-11-18 14:22:38 -05004406 RValue<UInt> operator<<=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004407 {
4408 return lhs = lhs << rhs;
4409 }
4410
Nicolas Capens96d4e092016-11-18 14:22:38 -05004411 RValue<UInt> operator>>=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004412 {
4413 return lhs = lhs >> rhs;
4414 }
4415
4416 RValue<UInt> operator+(RValue<UInt> val)
4417 {
4418 return val;
4419 }
4420
4421 RValue<UInt> operator-(RValue<UInt> val)
4422 {
4423 return RValue<UInt>(Nucleus::createNeg(val.value));
4424 }
4425
4426 RValue<UInt> operator~(RValue<UInt> val)
4427 {
4428 return RValue<UInt>(Nucleus::createNot(val.value));
4429 }
4430
Nicolas Capens96d4e092016-11-18 14:22:38 -05004431 RValue<UInt> operator++(UInt &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004432 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004433 RValue<UInt> res = val;
4434 val += 1;
4435 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004436 }
4437
Nicolas Capens96d4e092016-11-18 14:22:38 -05004438 const UInt &operator++(UInt &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004439 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004440 val += 1;
4441 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004442 }
4443
Nicolas Capens96d4e092016-11-18 14:22:38 -05004444 RValue<UInt> operator--(UInt &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004445 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004446 RValue<UInt> res = val;
4447 val -= 1;
4448 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004449 }
4450
Nicolas Capens96d4e092016-11-18 14:22:38 -05004451 const UInt &operator--(UInt &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004452 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004453 val -= 1;
4454 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004455 }
4456
4457 RValue<UInt> Max(RValue<UInt> x, RValue<UInt> y)
4458 {
4459 return IfThenElse(x > y, x, y);
4460 }
4461
4462 RValue<UInt> Min(RValue<UInt> x, RValue<UInt> y)
4463 {
4464 return IfThenElse(x < y, x, y);
4465 }
4466
4467 RValue<UInt> Clamp(RValue<UInt> x, RValue<UInt> min, RValue<UInt> max)
4468 {
4469 return Min(Max(x, min), max);
4470 }
4471
4472 RValue<Bool> operator<(RValue<UInt> lhs, RValue<UInt> rhs)
4473 {
4474 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
4475 }
4476
4477 RValue<Bool> operator<=(RValue<UInt> lhs, RValue<UInt> rhs)
4478 {
4479 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
4480 }
4481
4482 RValue<Bool> operator>(RValue<UInt> lhs, RValue<UInt> rhs)
4483 {
4484 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
4485 }
4486
4487 RValue<Bool> operator>=(RValue<UInt> lhs, RValue<UInt> rhs)
4488 {
4489 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
4490 }
4491
4492 RValue<Bool> operator!=(RValue<UInt> lhs, RValue<UInt> rhs)
4493 {
4494 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
4495 }
4496
4497 RValue<Bool> operator==(RValue<UInt> lhs, RValue<UInt> rhs)
4498 {
4499 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
4500 }
4501
4502// RValue<UInt> RoundUInt(RValue<Float> cast)
4503// {
Nicolas Capensc37252c2016-09-28 16:11:54 -04004504// assert(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004505// }
4506
4507 Type *UInt::getType()
4508 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04004509 return T(Ice::IceType_i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004510 }
4511
4512// Int2::Int2(RValue<Int> cast)
4513// {
4514// Value *extend = Nucleus::createZExt(cast.value, Long::getType());
4515// Value *vector = Nucleus::createBitCast(extend, Int2::getType());
4516//
4517// Constant *shuffle[2];
4518// shuffle[0] = Nucleus::createConstantInt(0);
4519// shuffle[1] = Nucleus::createConstantInt(0);
4520//
4521// Value *replicate = Nucleus::createShuffleVector(vector, UndefValue::get(Int2::getType()), Nucleus::createConstantVector(shuffle, 2));
4522//
4523// storeValue(replicate);
4524// }
4525
4526 Int2::Int2(RValue<Int4> cast)
4527 {
Nicolas Capens22008782016-10-20 01:11:47 -04004528 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004529 }
4530
Nicolas Capens598f8d82016-09-26 15:09:10 -04004531 Int2::Int2(int x, int y)
4532 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004533 int64_t constantVector[2] = {x, y};
4534 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004535 }
4536
4537 Int2::Int2(RValue<Int2> rhs)
4538 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004539 storeValue(rhs.value);
4540 }
4541
4542 Int2::Int2(const Int2 &rhs)
4543 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004544 Value *value = rhs.loadValue();
4545 storeValue(value);
4546 }
4547
4548 Int2::Int2(const Reference<Int2> &rhs)
4549 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004550 Value *value = rhs.loadValue();
4551 storeValue(value);
4552 }
4553
4554 Int2::Int2(RValue<Int> lo, RValue<Int> hi)
4555 {
Nicolas Capensd4227962016-11-09 14:24:25 -05004556 int shuffle[4] = {0, 4, 1, 5};
4557 Value *packed = Nucleus::createShuffleVector(Int4(lo).loadValue(), Int4(hi).loadValue(), shuffle);
4558
4559 storeValue(Nucleus::createBitCast(packed, Int2::getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004560 }
4561
Nicolas Capens96d4e092016-11-18 14:22:38 -05004562 RValue<Int2> Int2::operator=(RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004563 {
4564 storeValue(rhs.value);
4565
4566 return rhs;
4567 }
4568
Nicolas Capens96d4e092016-11-18 14:22:38 -05004569 RValue<Int2> Int2::operator=(const Int2 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004570 {
4571 Value *value = rhs.loadValue();
4572 storeValue(value);
4573
4574 return RValue<Int2>(value);
4575 }
4576
Nicolas Capens96d4e092016-11-18 14:22:38 -05004577 RValue<Int2> Int2::operator=(const Reference<Int2> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004578 {
4579 Value *value = rhs.loadValue();
4580 storeValue(value);
4581
4582 return RValue<Int2>(value);
4583 }
4584
4585 RValue<Int2> operator+(RValue<Int2> lhs, RValue<Int2> rhs)
4586 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004587 return RValue<Int2>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004588 }
4589
4590 RValue<Int2> operator-(RValue<Int2> lhs, RValue<Int2> rhs)
4591 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004592 return RValue<Int2>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004593 }
4594
4595// RValue<Int2> operator*(RValue<Int2> lhs, RValue<Int2> rhs)
4596// {
4597// return RValue<Int2>(Nucleus::createMul(lhs.value, rhs.value));
4598// }
4599
4600// RValue<Int2> operator/(RValue<Int2> lhs, RValue<Int2> rhs)
4601// {
4602// return RValue<Int2>(Nucleus::createSDiv(lhs.value, rhs.value));
4603// }
4604
4605// RValue<Int2> operator%(RValue<Int2> lhs, RValue<Int2> rhs)
4606// {
4607// return RValue<Int2>(Nucleus::createSRem(lhs.value, rhs.value));
4608// }
4609
4610 RValue<Int2> operator&(RValue<Int2> lhs, RValue<Int2> rhs)
4611 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004612 return RValue<Int2>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004613 }
4614
4615 RValue<Int2> operator|(RValue<Int2> lhs, RValue<Int2> rhs)
4616 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004617 return RValue<Int2>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004618 }
4619
4620 RValue<Int2> operator^(RValue<Int2> lhs, RValue<Int2> rhs)
4621 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004622 return RValue<Int2>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004623 }
4624
4625 RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs)
4626 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004627 return RValue<Int2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004628 }
4629
4630 RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs)
4631 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004632 return RValue<Int2>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004633 }
4634
Nicolas Capens96d4e092016-11-18 14:22:38 -05004635 RValue<Int2> operator+=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004636 {
4637 return lhs = lhs + rhs;
4638 }
4639
Nicolas Capens96d4e092016-11-18 14:22:38 -05004640 RValue<Int2> operator-=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004641 {
4642 return lhs = lhs - rhs;
4643 }
4644
Nicolas Capens96d4e092016-11-18 14:22:38 -05004645// RValue<Int2> operator*=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004646// {
4647// return lhs = lhs * rhs;
4648// }
4649
Nicolas Capens96d4e092016-11-18 14:22:38 -05004650// RValue<Int2> operator/=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004651// {
4652// return lhs = lhs / rhs;
4653// }
4654
Nicolas Capens96d4e092016-11-18 14:22:38 -05004655// RValue<Int2> operator%=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004656// {
4657// return lhs = lhs % rhs;
4658// }
4659
Nicolas Capens96d4e092016-11-18 14:22:38 -05004660 RValue<Int2> operator&=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004661 {
4662 return lhs = lhs & rhs;
4663 }
4664
Nicolas Capens96d4e092016-11-18 14:22:38 -05004665 RValue<Int2> operator|=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004666 {
4667 return lhs = lhs | rhs;
4668 }
4669
Nicolas Capens96d4e092016-11-18 14:22:38 -05004670 RValue<Int2> operator^=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004671 {
4672 return lhs = lhs ^ rhs;
4673 }
4674
Nicolas Capens96d4e092016-11-18 14:22:38 -05004675 RValue<Int2> operator<<=(Int2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004676 {
4677 return lhs = lhs << rhs;
4678 }
4679
Nicolas Capens96d4e092016-11-18 14:22:38 -05004680 RValue<Int2> operator>>=(Int2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004681 {
4682 return lhs = lhs >> rhs;
4683 }
4684
Nicolas Capens598f8d82016-09-26 15:09:10 -04004685// RValue<Int2> operator+(RValue<Int2> val)
4686// {
4687// return val;
4688// }
4689
4690// RValue<Int2> operator-(RValue<Int2> val)
4691// {
4692// return RValue<Int2>(Nucleus::createNeg(val.value));
4693// }
4694
4695 RValue<Int2> operator~(RValue<Int2> val)
4696 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05004697 return RValue<Int2>(Nucleus::createNot(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004698 }
4699
Nicolas Capens45f187a2016-12-02 15:30:56 -05004700 RValue<Short4> UnpackLow(RValue<Int2> x, RValue<Int2> y)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004701 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05004702 int shuffle[4] = {0, 4, 1, 5}; // Real type is v4i32
4703 return As<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004704 }
4705
Nicolas Capens45f187a2016-12-02 15:30:56 -05004706 RValue<Short4> UnpackHigh(RValue<Int2> x, RValue<Int2> y)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004707 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05004708 int shuffle[16] = {0, 4, 1, 5}; // Real type is v4i32
4709 auto lowHigh = RValue<Int4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
4710 return As<Short4>(Swizzle(lowHigh, 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004711 }
4712
4713 RValue<Int> Extract(RValue<Int2> val, int i)
4714 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05004715 return RValue<Int>(Nucleus::createExtractElement(val.value, Int::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004716 }
4717
4718 RValue<Int2> Insert(RValue<Int2> val, RValue<Int> element, int i)
4719 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05004720 return RValue<Int2>(Nucleus::createInsertElement(val.value, element.value, i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004721 }
4722
4723 Type *Int2::getType()
4724 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004725 return T(Type_v2i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004726 }
4727
Nicolas Capens598f8d82016-09-26 15:09:10 -04004728 UInt2::UInt2(unsigned int x, unsigned int y)
4729 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004730 int64_t constantVector[2] = {x, y};
4731 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004732 }
4733
4734 UInt2::UInt2(RValue<UInt2> rhs)
4735 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004736 storeValue(rhs.value);
4737 }
4738
4739 UInt2::UInt2(const UInt2 &rhs)
4740 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004741 Value *value = rhs.loadValue();
4742 storeValue(value);
4743 }
4744
4745 UInt2::UInt2(const Reference<UInt2> &rhs)
4746 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004747 Value *value = rhs.loadValue();
4748 storeValue(value);
4749 }
4750
Nicolas Capens96d4e092016-11-18 14:22:38 -05004751 RValue<UInt2> UInt2::operator=(RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004752 {
4753 storeValue(rhs.value);
4754
4755 return rhs;
4756 }
4757
Nicolas Capens96d4e092016-11-18 14:22:38 -05004758 RValue<UInt2> UInt2::operator=(const UInt2 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004759 {
4760 Value *value = rhs.loadValue();
4761 storeValue(value);
4762
4763 return RValue<UInt2>(value);
4764 }
4765
Nicolas Capens96d4e092016-11-18 14:22:38 -05004766 RValue<UInt2> UInt2::operator=(const Reference<UInt2> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004767 {
4768 Value *value = rhs.loadValue();
4769 storeValue(value);
4770
4771 return RValue<UInt2>(value);
4772 }
4773
4774 RValue<UInt2> operator+(RValue<UInt2> lhs, RValue<UInt2> rhs)
4775 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004776 return RValue<UInt2>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004777 }
4778
4779 RValue<UInt2> operator-(RValue<UInt2> lhs, RValue<UInt2> rhs)
4780 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004781 return RValue<UInt2>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004782 }
4783
4784// RValue<UInt2> operator*(RValue<UInt2> lhs, RValue<UInt2> rhs)
4785// {
4786// return RValue<UInt2>(Nucleus::createMul(lhs.value, rhs.value));
4787// }
4788
4789// RValue<UInt2> operator/(RValue<UInt2> lhs, RValue<UInt2> rhs)
4790// {
4791// return RValue<UInt2>(Nucleus::createUDiv(lhs.value, rhs.value));
4792// }
4793
4794// RValue<UInt2> operator%(RValue<UInt2> lhs, RValue<UInt2> rhs)
4795// {
4796// return RValue<UInt2>(Nucleus::createURem(lhs.value, rhs.value));
4797// }
4798
4799 RValue<UInt2> operator&(RValue<UInt2> lhs, RValue<UInt2> rhs)
4800 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004801 return RValue<UInt2>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004802 }
4803
4804 RValue<UInt2> operator|(RValue<UInt2> lhs, RValue<UInt2> rhs)
4805 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004806 return RValue<UInt2>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004807 }
4808
4809 RValue<UInt2> operator^(RValue<UInt2> lhs, RValue<UInt2> rhs)
4810 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004811 return RValue<UInt2>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004812 }
4813
4814 RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs)
4815 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004816 return RValue<UInt2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004817 }
4818
4819 RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs)
4820 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004821 return RValue<UInt2>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004822 }
4823
Nicolas Capens96d4e092016-11-18 14:22:38 -05004824 RValue<UInt2> operator+=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004825 {
4826 return lhs = lhs + rhs;
4827 }
4828
Nicolas Capens96d4e092016-11-18 14:22:38 -05004829 RValue<UInt2> operator-=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004830 {
4831 return lhs = lhs - rhs;
4832 }
4833
Nicolas Capens96d4e092016-11-18 14:22:38 -05004834// RValue<UInt2> operator*=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004835// {
4836// return lhs = lhs * rhs;
4837// }
4838
Nicolas Capens96d4e092016-11-18 14:22:38 -05004839// RValue<UInt2> operator/=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004840// {
4841// return lhs = lhs / rhs;
4842// }
4843
Nicolas Capens96d4e092016-11-18 14:22:38 -05004844// RValue<UInt2> operator%=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004845// {
4846// return lhs = lhs % rhs;
4847// }
4848
Nicolas Capens96d4e092016-11-18 14:22:38 -05004849 RValue<UInt2> operator&=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004850 {
4851 return lhs = lhs & rhs;
4852 }
4853
Nicolas Capens96d4e092016-11-18 14:22:38 -05004854 RValue<UInt2> operator|=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004855 {
4856 return lhs = lhs | rhs;
4857 }
4858
Nicolas Capens96d4e092016-11-18 14:22:38 -05004859 RValue<UInt2> operator^=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004860 {
4861 return lhs = lhs ^ rhs;
4862 }
4863
Nicolas Capens96d4e092016-11-18 14:22:38 -05004864 RValue<UInt2> operator<<=(UInt2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004865 {
4866 return lhs = lhs << rhs;
4867 }
4868
Nicolas Capens96d4e092016-11-18 14:22:38 -05004869 RValue<UInt2> operator>>=(UInt2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004870 {
4871 return lhs = lhs >> rhs;
4872 }
4873
Nicolas Capens598f8d82016-09-26 15:09:10 -04004874// RValue<UInt2> operator+(RValue<UInt2> val)
4875// {
4876// return val;
4877// }
4878
4879// RValue<UInt2> operator-(RValue<UInt2> val)
4880// {
4881// return RValue<UInt2>(Nucleus::createNeg(val.value));
4882// }
4883
4884 RValue<UInt2> operator~(RValue<UInt2> val)
4885 {
4886 return RValue<UInt2>(Nucleus::createNot(val.value));
4887 }
4888
4889 Type *UInt2::getType()
4890 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04004891 return T(Type_v2i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004892 }
4893
4894 Int4::Int4(RValue<Byte4> cast)
4895 {
Nicolas Capensd4227962016-11-09 14:24:25 -05004896 Value *x = Nucleus::createBitCast(cast.value, Int::getType());
4897 Value *a = Nucleus::createInsertElement(loadValue(), x, 0);
4898
4899 Value *e;
4900 int swizzle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23};
4901 Value *b = Nucleus::createBitCast(a, Byte16::getType());
4902 Value *c = Nucleus::createShuffleVector(b, V(Nucleus::createNullValue(Byte16::getType())), swizzle);
4903
4904 int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11};
4905 Value *d = Nucleus::createBitCast(c, Short8::getType());
4906 e = Nucleus::createShuffleVector(d, V(Nucleus::createNullValue(Short8::getType())), swizzle2);
4907
4908 Value *f = Nucleus::createBitCast(e, Int4::getType());
4909 storeValue(f);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004910 }
4911
4912 Int4::Int4(RValue<SByte4> cast)
4913 {
Nicolas Capensd4227962016-11-09 14:24:25 -05004914 Value *x = Nucleus::createBitCast(cast.value, Int::getType());
4915 Value *a = Nucleus::createInsertElement(loadValue(), x, 0);
4916
4917 Value *e;
4918 int swizzle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7};
4919 Value *b = Nucleus::createBitCast(a, Byte16::getType());
4920 Value *c = Nucleus::createShuffleVector(b, b, swizzle);
4921
4922 int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3};
4923 Value *d = Nucleus::createBitCast(c, Short8::getType());
4924 e = Nucleus::createShuffleVector(d, d, swizzle2);
4925
4926 Value *f = Nucleus::createBitCast(e, Int4::getType());
Nicolas Capens15060bb2016-12-05 22:17:19 -05004927 Value *g = Nucleus::createAShr(f, V(::context->getConstantInt32(24)));
Nicolas Capensd4227962016-11-09 14:24:25 -05004928 storeValue(g);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004929 }
4930
4931 Int4::Int4(RValue<Float4> cast)
4932 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004933 Value *xyzw = Nucleus::createFPToSI(cast.value, Int4::getType());
4934
4935 storeValue(xyzw);
4936 }
4937
4938 Int4::Int4(RValue<Short4> cast)
4939 {
Nicolas Capensd4227962016-11-09 14:24:25 -05004940 int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3};
4941 Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle);
4942 Value *d = Nucleus::createBitCast(c, Int4::getType());
Nicolas Capens15060bb2016-12-05 22:17:19 -05004943 Value *e = Nucleus::createAShr(d, V(::context->getConstantInt32(16)));
Nicolas Capensd4227962016-11-09 14:24:25 -05004944 storeValue(e);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004945 }
4946
4947 Int4::Int4(RValue<UShort4> cast)
4948 {
Nicolas Capensd4227962016-11-09 14:24:25 -05004949 int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11};
4950 Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle);
4951 Value *d = Nucleus::createBitCast(c, Int4::getType());
4952 storeValue(d);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004953 }
4954
Nicolas Capens598f8d82016-09-26 15:09:10 -04004955 Int4::Int4(int xyzw)
4956 {
4957 constant(xyzw, xyzw, xyzw, xyzw);
4958 }
4959
4960 Int4::Int4(int x, int yzw)
4961 {
4962 constant(x, yzw, yzw, yzw);
4963 }
4964
4965 Int4::Int4(int x, int y, int zw)
4966 {
4967 constant(x, y, zw, zw);
4968 }
4969
4970 Int4::Int4(int x, int y, int z, int w)
4971 {
4972 constant(x, y, z, w);
4973 }
4974
4975 void Int4::constant(int x, int y, int z, int w)
4976 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04004977 int64_t constantVector[4] = {x, y, z, w};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004978 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004979 }
4980
4981 Int4::Int4(RValue<Int4> rhs)
4982 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004983 storeValue(rhs.value);
4984 }
4985
4986 Int4::Int4(const Int4 &rhs)
4987 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004988 Value *value = rhs.loadValue();
4989 storeValue(value);
4990 }
4991
4992 Int4::Int4(const Reference<Int4> &rhs)
4993 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004994 Value *value = rhs.loadValue();
4995 storeValue(value);
4996 }
4997
4998 Int4::Int4(RValue<UInt4> rhs)
4999 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005000 storeValue(rhs.value);
5001 }
5002
5003 Int4::Int4(const UInt4 &rhs)
5004 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005005 Value *value = rhs.loadValue();
5006 storeValue(value);
5007 }
5008
5009 Int4::Int4(const Reference<UInt4> &rhs)
5010 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005011 Value *value = rhs.loadValue();
5012 storeValue(value);
5013 }
5014
5015 Int4::Int4(RValue<Int2> lo, RValue<Int2> hi)
5016 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05005017 int shuffle[4] = {0, 1, 4, 5}; // Real type is v4i32
5018 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
5019
5020 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005021 }
5022
5023 Int4::Int4(RValue<Int> rhs)
5024 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005025 Value *vector = loadValue();
5026 Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0);
5027
5028 int swizzle[4] = {0, 0, 0, 0};
5029 Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle);
5030
5031 storeValue(replicate);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005032 }
5033
5034 Int4::Int4(const Int &rhs)
5035 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005036 *this = RValue<Int>(rhs.loadValue());
5037 }
5038
5039 Int4::Int4(const Reference<Int> &rhs)
5040 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005041 *this = RValue<Int>(rhs.loadValue());
5042 }
5043
Nicolas Capens96d4e092016-11-18 14:22:38 -05005044 RValue<Int4> Int4::operator=(RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005045 {
5046 storeValue(rhs.value);
5047
5048 return rhs;
5049 }
5050
Nicolas Capens96d4e092016-11-18 14:22:38 -05005051 RValue<Int4> Int4::operator=(const Int4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005052 {
5053 Value *value = rhs.loadValue();
5054 storeValue(value);
5055
5056 return RValue<Int4>(value);
5057 }
5058
Nicolas Capens96d4e092016-11-18 14:22:38 -05005059 RValue<Int4> Int4::operator=(const Reference<Int4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005060 {
5061 Value *value = rhs.loadValue();
5062 storeValue(value);
5063
5064 return RValue<Int4>(value);
5065 }
5066
5067 RValue<Int4> operator+(RValue<Int4> lhs, RValue<Int4> rhs)
5068 {
5069 return RValue<Int4>(Nucleus::createAdd(lhs.value, rhs.value));
5070 }
5071
5072 RValue<Int4> operator-(RValue<Int4> lhs, RValue<Int4> rhs)
5073 {
5074 return RValue<Int4>(Nucleus::createSub(lhs.value, rhs.value));
5075 }
5076
5077 RValue<Int4> operator*(RValue<Int4> lhs, RValue<Int4> rhs)
5078 {
5079 return RValue<Int4>(Nucleus::createMul(lhs.value, rhs.value));
5080 }
5081
5082 RValue<Int4> operator/(RValue<Int4> lhs, RValue<Int4> rhs)
5083 {
5084 return RValue<Int4>(Nucleus::createSDiv(lhs.value, rhs.value));
5085 }
5086
5087 RValue<Int4> operator%(RValue<Int4> lhs, RValue<Int4> rhs)
5088 {
5089 return RValue<Int4>(Nucleus::createSRem(lhs.value, rhs.value));
5090 }
5091
5092 RValue<Int4> operator&(RValue<Int4> lhs, RValue<Int4> rhs)
5093 {
5094 return RValue<Int4>(Nucleus::createAnd(lhs.value, rhs.value));
5095 }
5096
5097 RValue<Int4> operator|(RValue<Int4> lhs, RValue<Int4> rhs)
5098 {
5099 return RValue<Int4>(Nucleus::createOr(lhs.value, rhs.value));
5100 }
5101
5102 RValue<Int4> operator^(RValue<Int4> lhs, RValue<Int4> rhs)
5103 {
5104 return RValue<Int4>(Nucleus::createXor(lhs.value, rhs.value));
5105 }
5106
5107 RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs)
5108 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005109 return RValue<Int4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005110 }
5111
5112 RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs)
5113 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005114 return RValue<Int4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005115 }
5116
5117 RValue<Int4> operator<<(RValue<Int4> lhs, RValue<Int4> rhs)
5118 {
5119 return RValue<Int4>(Nucleus::createShl(lhs.value, rhs.value));
5120 }
5121
5122 RValue<Int4> operator>>(RValue<Int4> lhs, RValue<Int4> rhs)
5123 {
5124 return RValue<Int4>(Nucleus::createAShr(lhs.value, rhs.value));
5125 }
5126
Nicolas Capens96d4e092016-11-18 14:22:38 -05005127 RValue<Int4> operator+=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005128 {
5129 return lhs = lhs + rhs;
5130 }
5131
Nicolas Capens96d4e092016-11-18 14:22:38 -05005132 RValue<Int4> operator-=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005133 {
5134 return lhs = lhs - rhs;
5135 }
5136
Nicolas Capens96d4e092016-11-18 14:22:38 -05005137 RValue<Int4> operator*=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005138 {
5139 return lhs = lhs * rhs;
5140 }
5141
Nicolas Capens96d4e092016-11-18 14:22:38 -05005142// RValue<Int4> operator/=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005143// {
5144// return lhs = lhs / rhs;
5145// }
5146
Nicolas Capens96d4e092016-11-18 14:22:38 -05005147// RValue<Int4> operator%=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005148// {
5149// return lhs = lhs % rhs;
5150// }
5151
Nicolas Capens96d4e092016-11-18 14:22:38 -05005152 RValue<Int4> operator&=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005153 {
5154 return lhs = lhs & rhs;
5155 }
5156
Nicolas Capens96d4e092016-11-18 14:22:38 -05005157 RValue<Int4> operator|=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005158 {
5159 return lhs = lhs | rhs;
5160 }
5161
Nicolas Capens96d4e092016-11-18 14:22:38 -05005162 RValue<Int4> operator^=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005163 {
5164 return lhs = lhs ^ rhs;
5165 }
5166
Nicolas Capens96d4e092016-11-18 14:22:38 -05005167 RValue<Int4> operator<<=(Int4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005168 {
5169 return lhs = lhs << rhs;
5170 }
5171
Nicolas Capens96d4e092016-11-18 14:22:38 -05005172 RValue<Int4> operator>>=(Int4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005173 {
5174 return lhs = lhs >> rhs;
5175 }
5176
5177 RValue<Int4> operator+(RValue<Int4> val)
5178 {
5179 return val;
5180 }
5181
5182 RValue<Int4> operator-(RValue<Int4> val)
5183 {
5184 return RValue<Int4>(Nucleus::createNeg(val.value));
5185 }
5186
5187 RValue<Int4> operator~(RValue<Int4> val)
5188 {
5189 return RValue<Int4>(Nucleus::createNot(val.value));
5190 }
5191
5192 RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y)
5193 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005194 return RValue<Int4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005195 }
5196
5197 RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y)
5198 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005199 return RValue<Int4>(Nucleus::createICmpSLT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005200 }
5201
5202 RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y)
5203 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005204 return RValue<Int4>(Nucleus::createICmpSLE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005205 }
5206
5207 RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y)
5208 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005209 return RValue<Int4>(Nucleus::createICmpNE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005210 }
5211
5212 RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y)
5213 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005214 return RValue<Int4>(Nucleus::createICmpSGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005215 }
5216
5217 RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y)
5218 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005219 return RValue<Int4>(Nucleus::createICmpSGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005220 }
5221
5222 RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y)
5223 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005224 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5225 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value, y.value);
5226 ::basicBlock->appendInst(cmp);
5227
5228 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5229 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5230 ::basicBlock->appendInst(select);
5231
5232 return RValue<Int4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005233 }
5234
5235 RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y)
5236 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005237 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5238 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value);
5239 ::basicBlock->appendInst(cmp);
5240
5241 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5242 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5243 ::basicBlock->appendInst(select);
5244
5245 return RValue<Int4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005246 }
5247
5248 RValue<Int4> RoundInt(RValue<Float4> cast)
5249 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005250 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
Nicolas Capens9ca48d52017-01-14 12:52:55 -05005251 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5252 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5253 auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5254 nearbyint->addArg(cast.value);
5255 ::basicBlock->appendInst(nearbyint);
Nicolas Capensa8086512016-11-07 17:32:17 -05005256
5257 return RValue<Int4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005258 }
5259
5260 RValue<Short8> Pack(RValue<Int4> x, RValue<Int4> y)
5261 {
Nicolas Capensec54a172016-10-25 17:32:37 -04005262 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
5263 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5264 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5265 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
5266 pack->addArg(x.value);
5267 pack->addArg(y.value);
5268 ::basicBlock->appendInst(pack);
5269
5270 return RValue<Short8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005271 }
5272
5273 RValue<Int> Extract(RValue<Int4> x, int i)
5274 {
Nicolas Capense95d5342016-09-30 11:37:28 -04005275 return RValue<Int>(Nucleus::createExtractElement(x.value, Int::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005276 }
5277
5278 RValue<Int4> Insert(RValue<Int4> x, RValue<Int> element, int i)
5279 {
5280 return RValue<Int4>(Nucleus::createInsertElement(x.value, element.value, i));
5281 }
5282
5283 RValue<Int> SignMask(RValue<Int4> x)
5284 {
Nicolas Capensf2cb9df2016-10-21 17:26:13 -04005285 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
5286 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5287 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5288 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5289 movmsk->addArg(x.value);
5290 ::basicBlock->appendInst(movmsk);
5291
5292 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005293 }
5294
5295 RValue<Int4> Swizzle(RValue<Int4> x, unsigned char select)
5296 {
Nicolas Capense95d5342016-09-30 11:37:28 -04005297 return RValue<Int4>(createSwizzle4(x.value, select));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005298 }
5299
5300 Type *Int4::getType()
5301 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04005302 return T(Ice::IceType_v4i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005303 }
5304
5305 UInt4::UInt4(RValue<Float4> cast)
5306 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05005307 // Smallest positive value representable in UInt, but not in Int
5308 const unsigned int ustart = 0x80000000u;
5309 const float ustartf = float(ustart);
5310
5311 // Check if the value can be represented as an Int
5312 Int4 uiValue = CmpNLT(cast, Float4(ustartf));
5313 // If the value is too large, subtract ustart and re-add it after conversion.
5314 uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) |
5315 // Otherwise, just convert normally
5316 (~uiValue & Int4(cast));
5317 // If the value is negative, store 0, otherwise store the result of the conversion
5318 storeValue((~(As<Int4>(cast) >> 31) & uiValue).value);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005319 }
5320
Nicolas Capens598f8d82016-09-26 15:09:10 -04005321 UInt4::UInt4(int xyzw)
5322 {
5323 constant(xyzw, xyzw, xyzw, xyzw);
5324 }
5325
5326 UInt4::UInt4(int x, int yzw)
5327 {
5328 constant(x, yzw, yzw, yzw);
5329 }
5330
5331 UInt4::UInt4(int x, int y, int zw)
5332 {
5333 constant(x, y, zw, zw);
5334 }
5335
5336 UInt4::UInt4(int x, int y, int z, int w)
5337 {
5338 constant(x, y, z, w);
5339 }
5340
5341 void UInt4::constant(int x, int y, int z, int w)
5342 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04005343 int64_t constantVector[4] = {x, y, z, w};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04005344 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005345 }
5346
5347 UInt4::UInt4(RValue<UInt4> rhs)
5348 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005349 storeValue(rhs.value);
5350 }
5351
5352 UInt4::UInt4(const UInt4 &rhs)
5353 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005354 Value *value = rhs.loadValue();
5355 storeValue(value);
5356 }
5357
5358 UInt4::UInt4(const Reference<UInt4> &rhs)
5359 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005360 Value *value = rhs.loadValue();
5361 storeValue(value);
5362 }
5363
5364 UInt4::UInt4(RValue<Int4> rhs)
5365 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005366 storeValue(rhs.value);
5367 }
5368
5369 UInt4::UInt4(const Int4 &rhs)
5370 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005371 Value *value = rhs.loadValue();
5372 storeValue(value);
5373 }
5374
5375 UInt4::UInt4(const Reference<Int4> &rhs)
5376 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005377 Value *value = rhs.loadValue();
5378 storeValue(value);
5379 }
5380
5381 UInt4::UInt4(RValue<UInt2> lo, RValue<UInt2> hi)
5382 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05005383 int shuffle[4] = {0, 1, 4, 5}; // Real type is v4i32
5384 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
5385
5386 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005387 }
5388
Nicolas Capens96d4e092016-11-18 14:22:38 -05005389 RValue<UInt4> UInt4::operator=(RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005390 {
5391 storeValue(rhs.value);
5392
5393 return rhs;
5394 }
5395
Nicolas Capens96d4e092016-11-18 14:22:38 -05005396 RValue<UInt4> UInt4::operator=(const UInt4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005397 {
5398 Value *value = rhs.loadValue();
5399 storeValue(value);
5400
5401 return RValue<UInt4>(value);
5402 }
5403
Nicolas Capens96d4e092016-11-18 14:22:38 -05005404 RValue<UInt4> UInt4::operator=(const Reference<UInt4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005405 {
5406 Value *value = rhs.loadValue();
5407 storeValue(value);
5408
5409 return RValue<UInt4>(value);
5410 }
5411
5412 RValue<UInt4> operator+(RValue<UInt4> lhs, RValue<UInt4> rhs)
5413 {
5414 return RValue<UInt4>(Nucleus::createAdd(lhs.value, rhs.value));
5415 }
5416
5417 RValue<UInt4> operator-(RValue<UInt4> lhs, RValue<UInt4> rhs)
5418 {
5419 return RValue<UInt4>(Nucleus::createSub(lhs.value, rhs.value));
5420 }
5421
5422 RValue<UInt4> operator*(RValue<UInt4> lhs, RValue<UInt4> rhs)
5423 {
5424 return RValue<UInt4>(Nucleus::createMul(lhs.value, rhs.value));
5425 }
5426
5427 RValue<UInt4> operator/(RValue<UInt4> lhs, RValue<UInt4> rhs)
5428 {
5429 return RValue<UInt4>(Nucleus::createUDiv(lhs.value, rhs.value));
5430 }
5431
5432 RValue<UInt4> operator%(RValue<UInt4> lhs, RValue<UInt4> rhs)
5433 {
5434 return RValue<UInt4>(Nucleus::createURem(lhs.value, rhs.value));
5435 }
5436
5437 RValue<UInt4> operator&(RValue<UInt4> lhs, RValue<UInt4> rhs)
5438 {
5439 return RValue<UInt4>(Nucleus::createAnd(lhs.value, rhs.value));
5440 }
5441
5442 RValue<UInt4> operator|(RValue<UInt4> lhs, RValue<UInt4> rhs)
5443 {
5444 return RValue<UInt4>(Nucleus::createOr(lhs.value, rhs.value));
5445 }
5446
5447 RValue<UInt4> operator^(RValue<UInt4> lhs, RValue<UInt4> rhs)
5448 {
5449 return RValue<UInt4>(Nucleus::createXor(lhs.value, rhs.value));
5450 }
5451
5452 RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs)
5453 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005454 return RValue<UInt4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005455 }
5456
5457 RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs)
5458 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005459 return RValue<UInt4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005460 }
5461
5462 RValue<UInt4> operator<<(RValue<UInt4> lhs, RValue<UInt4> rhs)
5463 {
5464 return RValue<UInt4>(Nucleus::createShl(lhs.value, rhs.value));
5465 }
5466
5467 RValue<UInt4> operator>>(RValue<UInt4> lhs, RValue<UInt4> rhs)
5468 {
5469 return RValue<UInt4>(Nucleus::createLShr(lhs.value, rhs.value));
5470 }
5471
Nicolas Capens96d4e092016-11-18 14:22:38 -05005472 RValue<UInt4> operator+=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005473 {
5474 return lhs = lhs + rhs;
5475 }
5476
Nicolas Capens96d4e092016-11-18 14:22:38 -05005477 RValue<UInt4> operator-=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005478 {
5479 return lhs = lhs - rhs;
5480 }
5481
Nicolas Capens96d4e092016-11-18 14:22:38 -05005482 RValue<UInt4> operator*=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005483 {
5484 return lhs = lhs * rhs;
5485 }
5486
Nicolas Capens96d4e092016-11-18 14:22:38 -05005487// RValue<UInt4> operator/=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005488// {
5489// return lhs = lhs / rhs;
5490// }
5491
Nicolas Capens96d4e092016-11-18 14:22:38 -05005492// RValue<UInt4> operator%=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005493// {
5494// return lhs = lhs % rhs;
5495// }
5496
Nicolas Capens96d4e092016-11-18 14:22:38 -05005497 RValue<UInt4> operator&=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005498 {
5499 return lhs = lhs & rhs;
5500 }
5501
Nicolas Capens96d4e092016-11-18 14:22:38 -05005502 RValue<UInt4> operator|=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005503 {
5504 return lhs = lhs | rhs;
5505 }
5506
Nicolas Capens96d4e092016-11-18 14:22:38 -05005507 RValue<UInt4> operator^=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005508 {
5509 return lhs = lhs ^ rhs;
5510 }
5511
Nicolas Capens96d4e092016-11-18 14:22:38 -05005512 RValue<UInt4> operator<<=(UInt4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005513 {
5514 return lhs = lhs << rhs;
5515 }
5516
Nicolas Capens96d4e092016-11-18 14:22:38 -05005517 RValue<UInt4> operator>>=(UInt4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005518 {
5519 return lhs = lhs >> rhs;
5520 }
5521
5522 RValue<UInt4> operator+(RValue<UInt4> val)
5523 {
5524 return val;
5525 }
5526
5527 RValue<UInt4> operator-(RValue<UInt4> val)
5528 {
5529 return RValue<UInt4>(Nucleus::createNeg(val.value));
5530 }
5531
5532 RValue<UInt4> operator~(RValue<UInt4> val)
5533 {
5534 return RValue<UInt4>(Nucleus::createNot(val.value));
5535 }
5536
5537 RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y)
5538 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005539 return RValue<UInt4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005540 }
5541
5542 RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y)
5543 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005544 return RValue<UInt4>(Nucleus::createICmpULT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005545 }
5546
5547 RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y)
5548 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005549 return RValue<UInt4>(Nucleus::createICmpULE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005550 }
5551
5552 RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y)
5553 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005554 return RValue<UInt4>(Nucleus::createICmpNE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005555 }
5556
5557 RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y)
5558 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005559 return RValue<UInt4>(Nucleus::createICmpUGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005560 }
5561
5562 RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y)
5563 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005564 return RValue<UInt4>(Nucleus::createICmpUGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005565 }
5566
5567 RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y)
5568 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005569 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5570 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value, y.value);
5571 ::basicBlock->appendInst(cmp);
5572
5573 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5574 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5575 ::basicBlock->appendInst(select);
5576
5577 return RValue<UInt4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005578 }
5579
5580 RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y)
5581 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005582 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5583 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value);
5584 ::basicBlock->appendInst(cmp);
5585
5586 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5587 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5588 ::basicBlock->appendInst(select);
5589
5590 return RValue<UInt4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005591 }
5592
5593 RValue<UShort8> Pack(RValue<UInt4> x, RValue<UInt4> y)
5594 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05005595 if(CPUID::SSE4_1)
5596 {
5597 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
5598 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5599 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5600 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
5601 pack->addArg(x.value);
5602 pack->addArg(y.value);
5603 ::basicBlock->appendInst(pack);
Nicolas Capensec54a172016-10-25 17:32:37 -04005604
Nicolas Capens9ca48d52017-01-14 12:52:55 -05005605 return RValue<UShort8>(V(result));
5606 }
5607 else
5608 {
5609 RValue<Int4> sx = As<Int4>(x);
5610 RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000);
5611
5612 RValue<Int4> sy = As<Int4>(y);
5613 RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000);
5614
5615 return As<UShort8>(Pack(bx, by) + Short8(0x8000u));
5616 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04005617 }
5618
5619 Type *UInt4::getType()
5620 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04005621 return T(Ice::IceType_v4i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005622 }
5623
5624 Float::Float(RValue<Int> cast)
5625 {
5626 Value *integer = Nucleus::createSIToFP(cast.value, Float::getType());
5627
5628 storeValue(integer);
5629 }
5630
Nicolas Capens598f8d82016-09-26 15:09:10 -04005631 Float::Float(float x)
5632 {
5633 storeValue(Nucleus::createConstantFloat(x));
5634 }
5635
5636 Float::Float(RValue<Float> rhs)
5637 {
5638 storeValue(rhs.value);
5639 }
5640
5641 Float::Float(const Float &rhs)
5642 {
5643 Value *value = rhs.loadValue();
5644 storeValue(value);
5645 }
5646
5647 Float::Float(const Reference<Float> &rhs)
5648 {
5649 Value *value = rhs.loadValue();
5650 storeValue(value);
5651 }
5652
Nicolas Capens96d4e092016-11-18 14:22:38 -05005653 RValue<Float> Float::operator=(RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005654 {
5655 storeValue(rhs.value);
5656
5657 return rhs;
5658 }
5659
Nicolas Capens96d4e092016-11-18 14:22:38 -05005660 RValue<Float> Float::operator=(const Float &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005661 {
5662 Value *value = rhs.loadValue();
5663 storeValue(value);
5664
5665 return RValue<Float>(value);
5666 }
5667
Nicolas Capens96d4e092016-11-18 14:22:38 -05005668 RValue<Float> Float::operator=(const Reference<Float> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005669 {
5670 Value *value = rhs.loadValue();
5671 storeValue(value);
5672
5673 return RValue<Float>(value);
5674 }
5675
5676 RValue<Float> operator+(RValue<Float> lhs, RValue<Float> rhs)
5677 {
5678 return RValue<Float>(Nucleus::createFAdd(lhs.value, rhs.value));
5679 }
5680
5681 RValue<Float> operator-(RValue<Float> lhs, RValue<Float> rhs)
5682 {
5683 return RValue<Float>(Nucleus::createFSub(lhs.value, rhs.value));
5684 }
5685
5686 RValue<Float> operator*(RValue<Float> lhs, RValue<Float> rhs)
5687 {
5688 return RValue<Float>(Nucleus::createFMul(lhs.value, rhs.value));
5689 }
5690
5691 RValue<Float> operator/(RValue<Float> lhs, RValue<Float> rhs)
5692 {
5693 return RValue<Float>(Nucleus::createFDiv(lhs.value, rhs.value));
5694 }
5695
Nicolas Capens96d4e092016-11-18 14:22:38 -05005696 RValue<Float> operator+=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005697 {
5698 return lhs = lhs + rhs;
5699 }
5700
Nicolas Capens96d4e092016-11-18 14:22:38 -05005701 RValue<Float> operator-=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005702 {
5703 return lhs = lhs - rhs;
5704 }
5705
Nicolas Capens96d4e092016-11-18 14:22:38 -05005706 RValue<Float> operator*=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005707 {
5708 return lhs = lhs * rhs;
5709 }
5710
Nicolas Capens96d4e092016-11-18 14:22:38 -05005711 RValue<Float> operator/=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005712 {
5713 return lhs = lhs / rhs;
5714 }
5715
5716 RValue<Float> operator+(RValue<Float> val)
5717 {
5718 return val;
5719 }
5720
5721 RValue<Float> operator-(RValue<Float> val)
5722 {
5723 return RValue<Float>(Nucleus::createFNeg(val.value));
5724 }
5725
5726 RValue<Bool> operator<(RValue<Float> lhs, RValue<Float> rhs)
5727 {
5728 return RValue<Bool>(Nucleus::createFCmpOLT(lhs.value, rhs.value));
5729 }
5730
5731 RValue<Bool> operator<=(RValue<Float> lhs, RValue<Float> rhs)
5732 {
5733 return RValue<Bool>(Nucleus::createFCmpOLE(lhs.value, rhs.value));
5734 }
5735
5736 RValue<Bool> operator>(RValue<Float> lhs, RValue<Float> rhs)
5737 {
5738 return RValue<Bool>(Nucleus::createFCmpOGT(lhs.value, rhs.value));
5739 }
5740
5741 RValue<Bool> operator>=(RValue<Float> lhs, RValue<Float> rhs)
5742 {
5743 return RValue<Bool>(Nucleus::createFCmpOGE(lhs.value, rhs.value));
5744 }
5745
5746 RValue<Bool> operator!=(RValue<Float> lhs, RValue<Float> rhs)
5747 {
5748 return RValue<Bool>(Nucleus::createFCmpONE(lhs.value, rhs.value));
5749 }
5750
5751 RValue<Bool> operator==(RValue<Float> lhs, RValue<Float> rhs)
5752 {
5753 return RValue<Bool>(Nucleus::createFCmpOEQ(lhs.value, rhs.value));
5754 }
5755
5756 RValue<Float> Abs(RValue<Float> x)
5757 {
5758 return IfThenElse(x > 0.0f, x, -x);
5759 }
5760
5761 RValue<Float> Max(RValue<Float> x, RValue<Float> y)
5762 {
5763 return IfThenElse(x > y, x, y);
5764 }
5765
5766 RValue<Float> Min(RValue<Float> x, RValue<Float> y)
5767 {
5768 return IfThenElse(x < y, x, y);
5769 }
5770
5771 RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2)
5772 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04005773 return 1.0f / x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005774 }
5775
5776 RValue<Float> RcpSqrt_pp(RValue<Float> x)
5777 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04005778 return Rcp_pp(Sqrt(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005779 }
5780
5781 RValue<Float> Sqrt(RValue<Float> x)
5782 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04005783 Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32);
5784 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5785 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5786 auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5787 sqrt->addArg(x.value);
5788 ::basicBlock->appendInst(sqrt);
5789
5790 return RValue<Float>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005791 }
5792
5793 RValue<Float> Round(RValue<Float> x)
5794 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005795 return Float4(Round(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005796 }
5797
5798 RValue<Float> Trunc(RValue<Float> x)
5799 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005800 return Float4(Trunc(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005801 }
5802
5803 RValue<Float> Frac(RValue<Float> x)
5804 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005805 return Float4(Frac(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005806 }
5807
5808 RValue<Float> Floor(RValue<Float> x)
5809 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005810 return Float4(Floor(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005811 }
5812
5813 RValue<Float> Ceil(RValue<Float> x)
5814 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005815 return Float4(Ceil(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005816 }
5817
5818 Type *Float::getType()
5819 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04005820 return T(Ice::IceType_f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005821 }
5822
5823 Float2::Float2(RValue<Float4> cast)
5824 {
Nicolas Capens22008782016-10-20 01:11:47 -04005825 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005826 }
5827
5828 Type *Float2::getType()
5829 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04005830 return T(Type_v2f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005831 }
5832
Nicolas Capensa25311a2017-01-16 17:19:00 -05005833 Float4::Float4(RValue<Byte4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005834 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005835 Value *a = Int4(cast).loadValue();
5836 Value *xyzw = Nucleus::createSIToFP(a, Float4::getType());
5837
5838 storeValue(xyzw);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005839 }
5840
Nicolas Capensa25311a2017-01-16 17:19:00 -05005841 Float4::Float4(RValue<SByte4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005842 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005843 Value *a = Int4(cast).loadValue();
5844 Value *xyzw = Nucleus::createSIToFP(a, Float4::getType());
5845
5846 storeValue(xyzw);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005847 }
5848
Nicolas Capensa25311a2017-01-16 17:19:00 -05005849 Float4::Float4(RValue<Short4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005850 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005851 Int4 c(cast);
5852 storeValue(Nucleus::createSIToFP(RValue<Int4>(c).value, Float4::getType()));
5853 }
5854
Nicolas Capensa25311a2017-01-16 17:19:00 -05005855 Float4::Float4(RValue<UShort4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005856 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005857 Int4 c(cast);
5858 storeValue(Nucleus::createSIToFP(RValue<Int4>(c).value, Float4::getType()));
5859 }
5860
Nicolas Capensa25311a2017-01-16 17:19:00 -05005861 Float4::Float4(RValue<Int4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005862 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005863 Value *xyzw = Nucleus::createSIToFP(cast.value, Float4::getType());
5864
5865 storeValue(xyzw);
5866 }
5867
Nicolas Capensa25311a2017-01-16 17:19:00 -05005868 Float4::Float4(RValue<UInt4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005869 {
Nicolas Capens96445fe2016-12-15 14:45:13 -05005870 RValue<Float4> result = Float4(Int4(cast & UInt4(0x7FFFFFFF))) +
5871 As<Float4>((As<Int4>(cast) >> 31) & As<Int4>(Float4(0x80000000u)));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005872
Nicolas Capens96445fe2016-12-15 14:45:13 -05005873 storeValue(result.value);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005874 }
5875
Nicolas Capensa25311a2017-01-16 17:19:00 -05005876 Float4::Float4() : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005877 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005878 }
5879
Nicolas Capensa25311a2017-01-16 17:19:00 -05005880 Float4::Float4(float xyzw) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005881 {
5882 constant(xyzw, xyzw, xyzw, xyzw);
5883 }
5884
Nicolas Capensa25311a2017-01-16 17:19:00 -05005885 Float4::Float4(float x, float yzw) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005886 {
5887 constant(x, yzw, yzw, yzw);
5888 }
5889
Nicolas Capensa25311a2017-01-16 17:19:00 -05005890 Float4::Float4(float x, float y, float zw) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005891 {
5892 constant(x, y, zw, zw);
5893 }
5894
Nicolas Capensa25311a2017-01-16 17:19:00 -05005895 Float4::Float4(float x, float y, float z, float w) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005896 {
5897 constant(x, y, z, w);
5898 }
5899
5900 void Float4::constant(float x, float y, float z, float w)
5901 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04005902 double constantVector[4] = {x, y, z, w};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04005903 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005904 }
5905
Nicolas Capensa25311a2017-01-16 17:19:00 -05005906 Float4::Float4(RValue<Float4> rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005907 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005908 storeValue(rhs.value);
5909 }
5910
Nicolas Capensa25311a2017-01-16 17:19:00 -05005911 Float4::Float4(const Float4 &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005912 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005913 Value *value = rhs.loadValue();
5914 storeValue(value);
5915 }
5916
Nicolas Capensa25311a2017-01-16 17:19:00 -05005917 Float4::Float4(const Reference<Float4> &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005918 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005919 Value *value = rhs.loadValue();
5920 storeValue(value);
5921 }
5922
Nicolas Capensa25311a2017-01-16 17:19:00 -05005923 Float4::Float4(RValue<Float> rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005924 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005925 Value *vector = loadValue();
5926 Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0);
5927
5928 int swizzle[4] = {0, 0, 0, 0};
5929 Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle);
5930
5931 storeValue(replicate);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005932 }
5933
Nicolas Capensa25311a2017-01-16 17:19:00 -05005934 Float4::Float4(const Float &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005935 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005936 *this = RValue<Float>(rhs.loadValue());
5937 }
5938
Nicolas Capensa25311a2017-01-16 17:19:00 -05005939 Float4::Float4(const Reference<Float> &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005940 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005941 *this = RValue<Float>(rhs.loadValue());
5942 }
5943
Nicolas Capens96d4e092016-11-18 14:22:38 -05005944 RValue<Float4> Float4::operator=(float x)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005945 {
5946 return *this = Float4(x, x, x, x);
5947 }
5948
Nicolas Capens96d4e092016-11-18 14:22:38 -05005949 RValue<Float4> Float4::operator=(RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005950 {
5951 storeValue(rhs.value);
5952
5953 return rhs;
5954 }
5955
Nicolas Capens96d4e092016-11-18 14:22:38 -05005956 RValue<Float4> Float4::operator=(const Float4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005957 {
5958 Value *value = rhs.loadValue();
5959 storeValue(value);
5960
5961 return RValue<Float4>(value);
5962 }
5963
Nicolas Capens96d4e092016-11-18 14:22:38 -05005964 RValue<Float4> Float4::operator=(const Reference<Float4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005965 {
5966 Value *value = rhs.loadValue();
5967 storeValue(value);
5968
5969 return RValue<Float4>(value);
5970 }
5971
Nicolas Capens96d4e092016-11-18 14:22:38 -05005972 RValue<Float4> Float4::operator=(RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005973 {
5974 return *this = Float4(rhs);
5975 }
5976
Nicolas Capens96d4e092016-11-18 14:22:38 -05005977 RValue<Float4> Float4::operator=(const Float &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005978 {
5979 return *this = Float4(rhs);
5980 }
5981
Nicolas Capens96d4e092016-11-18 14:22:38 -05005982 RValue<Float4> Float4::operator=(const Reference<Float> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005983 {
5984 return *this = Float4(rhs);
5985 }
5986
5987 RValue<Float4> operator+(RValue<Float4> lhs, RValue<Float4> rhs)
5988 {
5989 return RValue<Float4>(Nucleus::createFAdd(lhs.value, rhs.value));
5990 }
5991
5992 RValue<Float4> operator-(RValue<Float4> lhs, RValue<Float4> rhs)
5993 {
5994 return RValue<Float4>(Nucleus::createFSub(lhs.value, rhs.value));
5995 }
5996
5997 RValue<Float4> operator*(RValue<Float4> lhs, RValue<Float4> rhs)
5998 {
5999 return RValue<Float4>(Nucleus::createFMul(lhs.value, rhs.value));
6000 }
6001
6002 RValue<Float4> operator/(RValue<Float4> lhs, RValue<Float4> rhs)
6003 {
6004 return RValue<Float4>(Nucleus::createFDiv(lhs.value, rhs.value));
6005 }
6006
6007 RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs)
6008 {
6009 return RValue<Float4>(Nucleus::createFRem(lhs.value, rhs.value));
6010 }
6011
Nicolas Capens96d4e092016-11-18 14:22:38 -05006012 RValue<Float4> operator+=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006013 {
6014 return lhs = lhs + rhs;
6015 }
6016
Nicolas Capens96d4e092016-11-18 14:22:38 -05006017 RValue<Float4> operator-=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006018 {
6019 return lhs = lhs - rhs;
6020 }
6021
Nicolas Capens96d4e092016-11-18 14:22:38 -05006022 RValue<Float4> operator*=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006023 {
6024 return lhs = lhs * rhs;
6025 }
6026
Nicolas Capens96d4e092016-11-18 14:22:38 -05006027 RValue<Float4> operator/=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006028 {
6029 return lhs = lhs / rhs;
6030 }
6031
Nicolas Capens96d4e092016-11-18 14:22:38 -05006032 RValue<Float4> operator%=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006033 {
6034 return lhs = lhs % rhs;
6035 }
6036
6037 RValue<Float4> operator+(RValue<Float4> val)
6038 {
6039 return val;
6040 }
6041
6042 RValue<Float4> operator-(RValue<Float4> val)
6043 {
6044 return RValue<Float4>(Nucleus::createFNeg(val.value));
6045 }
6046
6047 RValue<Float4> Abs(RValue<Float4> x)
6048 {
Nicolas Capens84272242016-11-09 13:31:06 -05006049 Value *vector = Nucleus::createBitCast(x.value, Int4::getType());
6050 int64_t constantVector[4] = {0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF};
6051 Value *result = Nucleus::createAnd(vector, V(Nucleus::createConstantVector(constantVector, Int4::getType())));
6052
6053 return As<Float4>(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006054 }
6055
6056 RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y)
6057 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006058 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
6059 auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Ule, condition, x.value, y.value);
6060 ::basicBlock->appendInst(cmp);
6061
6062 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6063 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
6064 ::basicBlock->appendInst(select);
6065
6066 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006067 }
6068
6069 RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y)
6070 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006071 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
6072 auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Ugt, condition, x.value, y.value);
6073 ::basicBlock->appendInst(cmp);
6074
6075 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6076 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
6077 ::basicBlock->appendInst(select);
6078
6079 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006080 }
6081
6082 RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2)
6083 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04006084 return Float4(1.0f) / x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04006085 }
6086
6087 RValue<Float4> RcpSqrt_pp(RValue<Float4> x)
6088 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04006089 return Rcp_pp(Sqrt(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006090 }
6091
6092 RValue<Float4> Sqrt(RValue<Float4> x)
6093 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04006094 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6095 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6096 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6097 auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
6098 sqrt->addArg(x.value);
6099 ::basicBlock->appendInst(sqrt);
6100
6101 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006102 }
6103
Nicolas Capensc94ab742016-11-08 15:15:31 -05006104 RValue<Float4> Insert(RValue<Float4> x, RValue<Float> element, int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006105 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05006106 return RValue<Float4>(Nucleus::createInsertElement(x.value, element.value, i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006107 }
6108
6109 RValue<Float> Extract(RValue<Float4> x, int i)
6110 {
Nicolas Capense95d5342016-09-30 11:37:28 -04006111 return RValue<Float>(Nucleus::createExtractElement(x.value, Float::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006112 }
6113
6114 RValue<Float4> Swizzle(RValue<Float4> x, unsigned char select)
6115 {
Nicolas Capense95d5342016-09-30 11:37:28 -04006116 return RValue<Float4>(createSwizzle4(x.value, select));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006117 }
6118
6119 RValue<Float4> ShuffleLowHigh(RValue<Float4> x, RValue<Float4> y, unsigned char imm)
6120 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04006121 int shuffle[4] =
6122 {
6123 ((imm >> 0) & 0x03) + 0,
6124 ((imm >> 2) & 0x03) + 0,
6125 ((imm >> 4) & 0x03) + 4,
6126 ((imm >> 6) & 0x03) + 4,
6127 };
6128
6129 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006130 }
6131
6132 RValue<Float4> UnpackLow(RValue<Float4> x, RValue<Float4> y)
6133 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04006134 int shuffle[4] = {0, 4, 1, 5};
6135 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006136 }
6137
6138 RValue<Float4> UnpackHigh(RValue<Float4> x, RValue<Float4> y)
6139 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04006140 int shuffle[4] = {2, 6, 3, 7};
6141 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006142 }
6143
6144 RValue<Float4> Mask(Float4 &lhs, RValue<Float4> rhs, unsigned char select)
6145 {
6146 Value *vector = lhs.loadValue();
Nicolas Capensa4c30b02016-11-08 15:43:17 -05006147 Value *result = createMask4(vector, rhs.value, select);
6148 lhs.storeValue(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006149
Nicolas Capensa4c30b02016-11-08 15:43:17 -05006150 return RValue<Float4>(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006151 }
6152
6153 RValue<Int> SignMask(RValue<Float4> x)
6154 {
Nicolas Capensf2cb9df2016-10-21 17:26:13 -04006155 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
6156 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6157 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6158 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
6159 movmsk->addArg(x.value);
6160 ::basicBlock->appendInst(movmsk);
6161
6162 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006163 }
6164
6165 RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y)
6166 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006167 return RValue<Int4>(Nucleus::createFCmpOEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006168 }
6169
6170 RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y)
6171 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006172 return RValue<Int4>(Nucleus::createFCmpOLT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006173 }
6174
6175 RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y)
6176 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006177 return RValue<Int4>(Nucleus::createFCmpOLE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006178 }
6179
6180 RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y)
6181 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006182 return RValue<Int4>(Nucleus::createFCmpONE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006183 }
6184
6185 RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y)
6186 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006187 return RValue<Int4>(Nucleus::createFCmpOGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006188 }
6189
6190 RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y)
6191 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006192 return RValue<Int4>(Nucleus::createFCmpOGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006193 }
6194
6195 RValue<Float4> Round(RValue<Float4> x)
6196 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006197 if(CPUID::SSE4_1)
6198 {
6199 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6200 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6201 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6202 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6203 round->addArg(x.value);
6204 round->addArg(::context->getConstantInt32(0));
6205 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006206
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006207 return RValue<Float4>(V(result));
6208 }
6209 else
6210 {
6211 return Float4(RoundInt(x));
6212 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006213 }
6214
6215 RValue<Float4> Trunc(RValue<Float4> x)
6216 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006217 if(CPUID::SSE4_1)
6218 {
6219 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6220 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6221 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6222 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6223 round->addArg(x.value);
6224 round->addArg(::context->getConstantInt32(3));
6225 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006226
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006227 return RValue<Float4>(V(result));
6228 }
6229 else
6230 {
6231 return Float4(Int4(x));
6232 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006233 }
6234
6235 RValue<Float4> Frac(RValue<Float4> x)
6236 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006237 if(CPUID::SSE4_1)
6238 {
6239 return x - Floor(x);
6240 }
6241 else
6242 {
6243 Float4 frc = x - Float4(Int4(x)); // Signed fractional part
6244
6245 return frc + As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1, 1, 1, 1)));
6246 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006247 }
6248
6249 RValue<Float4> Floor(RValue<Float4> x)
6250 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006251 if(CPUID::SSE4_1)
6252 {
6253 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6254 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6255 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6256 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6257 round->addArg(x.value);
6258 round->addArg(::context->getConstantInt32(1));
6259 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006260
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006261 return RValue<Float4>(V(result));
6262 }
6263 else
6264 {
6265 return x - Frac(x);
6266 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006267 }
6268
6269 RValue<Float4> Ceil(RValue<Float4> x)
6270 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006271 if(CPUID::SSE4_1)
6272 {
6273 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6274 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6275 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6276 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6277 round->addArg(x.value);
6278 round->addArg(::context->getConstantInt32(2));
6279 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006280
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006281 return RValue<Float4>(V(result));
6282 }
6283 else
6284 {
6285 return -Floor(-x);
6286 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006287 }
6288
6289 Type *Float4::getType()
6290 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04006291 return T(Ice::IceType_v4f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006292 }
6293
6294 RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, int offset)
6295 {
Nicolas Capens8820f642016-09-30 04:42:43 -04006296 return lhs + RValue<Int>(Nucleus::createConstantInt(offset));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006297 }
6298
6299 RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<Int> offset)
6300 {
Nicolas Capens6d738712016-09-30 04:15:22 -04006301 return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006302 }
6303
6304 RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<UInt> offset)
6305 {
Nicolas Capens6d738712016-09-30 04:15:22 -04006306 return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006307 }
6308
Nicolas Capens96d4e092016-11-18 14:22:38 -05006309 RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, int offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006310 {
6311 return lhs = lhs + offset;
6312 }
6313
Nicolas Capens96d4e092016-11-18 14:22:38 -05006314 RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, RValue<Int> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006315 {
6316 return lhs = lhs + offset;
6317 }
6318
Nicolas Capens96d4e092016-11-18 14:22:38 -05006319 RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, RValue<UInt> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006320 {
6321 return lhs = lhs + offset;
6322 }
6323
6324 RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, int offset)
6325 {
6326 return lhs + -offset;
6327 }
6328
6329 RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, RValue<Int> offset)
6330 {
6331 return lhs + -offset;
6332 }
6333
6334 RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, RValue<UInt> offset)
6335 {
6336 return lhs + -offset;
6337 }
6338
Nicolas Capens96d4e092016-11-18 14:22:38 -05006339 RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, int offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006340 {
6341 return lhs = lhs - offset;
6342 }
6343
Nicolas Capens96d4e092016-11-18 14:22:38 -05006344 RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, RValue<Int> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006345 {
6346 return lhs = lhs - offset;
6347 }
6348
Nicolas Capens96d4e092016-11-18 14:22:38 -05006349 RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, RValue<UInt> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006350 {
6351 return lhs = lhs - offset;
6352 }
6353
6354 void Return()
6355 {
6356 Nucleus::createRetVoid();
6357 Nucleus::setInsertBlock(Nucleus::createBasicBlock());
6358 Nucleus::createUnreachable();
6359 }
6360
Nicolas Capenseb253d02016-11-18 14:40:40 -05006361 void Return(RValue<Int> ret)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006362 {
Nicolas Capenseb253d02016-11-18 14:40:40 -05006363 Nucleus::createRet(ret.value);
Nicolas Capensfdcca2d2016-10-20 11:31:36 -04006364 Nucleus::setInsertBlock(Nucleus::createBasicBlock());
6365 Nucleus::createUnreachable();
Nicolas Capens598f8d82016-09-26 15:09:10 -04006366 }
6367
Nicolas Capens598f8d82016-09-26 15:09:10 -04006368 bool branch(RValue<Bool> cmp, BasicBlock *bodyBB, BasicBlock *endBB)
6369 {
6370 Nucleus::createCondBr(cmp.value, bodyBB, endBB);
6371 Nucleus::setInsertBlock(bodyBB);
6372
6373 return true;
6374 }
6375
Nicolas Capens598f8d82016-09-26 15:09:10 -04006376 RValue<Long> Ticks()
6377 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04006378 assert(false && "UNIMPLEMENTED"); return RValue<Long>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006379 }
6380}