blob: ee3fedb8f22ff45db95b0423a19cbd364778baeb [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 Capens411273e2017-01-26 15:13:36 -080043#if !defined(MAP_ANONYMOUS)
44#define MAP_ANONYMOUS MAP_ANON
Nicolas Capens8b275742017-01-20 17:11:41 -050045#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{
Nicolas Capens47dc8672017-04-25 12:54:39 -040069 #if !defined(__i386__) && defined(_M_IX86)
70 #define __i386__ 1
71 #endif
72
73 #if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64))
74 #define __x86_64__ 1
75 #endif
76
Nicolas Capensccd5ecb2017-01-14 12:52:55 -050077 class CPUID
78 {
79 public:
80 const static bool SSE4_1;
81
82 private:
83 static void cpuid(int registers[4], int info)
84 {
Nicolas Capens47dc8672017-04-25 12:54:39 -040085 #if defined(__i386__) || defined(__x86_64__)
86 #if defined(_WIN32)
87 __cpuid(registers, info);
88 #else
89 __asm volatile("cpuid": "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]): "a" (info));
90 #endif
Nicolas Capensccd5ecb2017-01-14 12:52:55 -050091 #else
Nicolas Capens47dc8672017-04-25 12:54:39 -040092 registers[0] = 0;
93 registers[1] = 0;
94 registers[2] = 0;
95 registers[3] = 0;
Nicolas Capensccd5ecb2017-01-14 12:52:55 -050096 #endif
97 }
98
99 static bool detectSSE4_1()
100 {
Nicolas Capens47dc8672017-04-25 12:54:39 -0400101 #if defined(__i386__) || defined(__x86_64__)
102 int registers[4];
103 cpuid(registers, 1);
104 return (registers[2] & 0x00080000) != 0;
105 #else
106 return false;
107 #endif
Nicolas Capensccd5ecb2017-01-14 12:52:55 -0500108 }
109 };
110
111 const bool CPUID::SSE4_1 = CPUID::detectSSE4_1();
112}
113
Nicolas Capens598f8d82016-09-26 15:09:10 -0400114namespace sw
115{
Nicolas Capens23d99a42016-09-30 14:57:16 -0400116 enum EmulatedType
117 {
118 EmulatedShift = 16,
119 EmulatedV2 = 2 << EmulatedShift,
120 EmulatedV4 = 4 << EmulatedShift,
121 EmulatedV8 = 8 << EmulatedShift,
122 EmulatedBits = EmulatedV2 | EmulatedV4 | EmulatedV8,
123
124 Type_v2i32 = Ice::IceType_v4i32 | EmulatedV2,
125 Type_v4i16 = Ice::IceType_v8i16 | EmulatedV4,
126 Type_v2i16 = Ice::IceType_v8i16 | EmulatedV2,
127 Type_v8i8 = Ice::IceType_v16i8 | EmulatedV8,
128 Type_v4i8 = Ice::IceType_v16i8 | EmulatedV4,
Nicolas Capens4cfd4572016-10-20 01:00:19 -0400129 Type_v2f32 = Ice::IceType_v4f32 | EmulatedV2,
Nicolas Capens23d99a42016-09-30 14:57:16 -0400130 };
131
Nicolas Capens15060bb2016-12-05 22:17:19 -0500132 class Value : public Ice::Operand {};
Nicolas Capensb98fe5c2016-11-09 12:24:06 -0500133 class SwitchCases : public Ice::InstSwitch {};
Nicolas Capens598f8d82016-09-26 15:09:10 -0400134 class BasicBlock : public Ice::CfgNode {};
135
136 Ice::Type T(Type *t)
137 {
Alexis Hetu113e33a2017-01-19 10:49:19 -0500138 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 -0400139 return (Ice::Type)(reinterpret_cast<std::intptr_t>(t) & ~EmulatedBits);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400140 }
141
142 Type *T(Ice::Type t)
143 {
144 return reinterpret_cast<Type*>(t);
145 }
146
Nicolas Capens23d99a42016-09-30 14:57:16 -0400147 Type *T(EmulatedType t)
148 {
149 return reinterpret_cast<Type*>(t);
150 }
151
Nicolas Capens15060bb2016-12-05 22:17:19 -0500152 Value *V(Ice::Operand *v)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400153 {
154 return reinterpret_cast<Value*>(v);
155 }
156
Nicolas Capens611642a2016-09-28 16:45:04 -0400157 BasicBlock *B(Ice::CfgNode *b)
158 {
159 return reinterpret_cast<BasicBlock*>(b);
160 }
161
Nicolas Capens584088c2017-01-26 16:05:18 -0800162 static size_t typeSize(Type *type)
163 {
164 if(reinterpret_cast<std::intptr_t>(type) & EmulatedBits)
165 {
166 switch(reinterpret_cast<std::intptr_t>(type))
167 {
168 case Type_v2i32: return 8;
169 case Type_v4i16: return 8;
170 case Type_v2i16: return 4;
171 case Type_v8i8: return 8;
172 case Type_v4i8: return 4;
173 case Type_v2f32: return 8;
174 default: assert(false);
175 }
176 }
177
178 return Ice::typeWidthInBytes(T(type));
179 }
180
Nicolas Capens598f8d82016-09-26 15:09:10 -0400181 Optimization optimization[10] = {InstructionCombining, Disabled};
182
Nicolas Capens66478362016-10-13 15:36:36 -0400183 using ElfHeader = std::conditional<sizeof(void*) == 8, Elf64_Ehdr, Elf32_Ehdr>::type;
184 using SectionHeader = std::conditional<sizeof(void*) == 8, Elf64_Shdr, Elf32_Shdr>::type;
185
186 inline const SectionHeader *sectionHeader(const ElfHeader *elfHeader)
187 {
188 return reinterpret_cast<const SectionHeader*>((intptr_t)elfHeader + elfHeader->e_shoff);
189 }
Nicolas Capens87852e12016-11-24 14:45:06 -0500190
Nicolas Capens66478362016-10-13 15:36:36 -0400191 inline const SectionHeader *elfSection(const ElfHeader *elfHeader, int index)
192 {
193 return &sectionHeader(elfHeader)[index];
194 }
195
196 static void *relocateSymbol(const ElfHeader *elfHeader, const Elf32_Rel &relocation, const SectionHeader &relocationTable)
197 {
198 const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info);
Nicolas Capens87852e12016-11-24 14:45:06 -0500199
Nicolas Capens66478362016-10-13 15:36:36 -0400200 intptr_t address = (intptr_t)elfHeader + target->sh_offset;
201 int32_t *patchSite = (int*)(address + relocation.r_offset);
202 uint32_t index = relocation.getSymbol();
203 int table = relocationTable.sh_link;
204 void *symbolValue = nullptr;
Nicolas Capens87852e12016-11-24 14:45:06 -0500205
Nicolas Capens66478362016-10-13 15:36:36 -0400206 if(index != SHN_UNDEF)
207 {
208 if(table == SHN_UNDEF) return nullptr;
209 const SectionHeader *symbolTable = elfSection(elfHeader, table);
Nicolas Capens87852e12016-11-24 14:45:06 -0500210
Nicolas Capens66478362016-10-13 15:36:36 -0400211 uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize;
212 if(index >= symtab_entries)
213 {
214 assert(index < symtab_entries && "Symbol Index out of range");
215 return nullptr;
216 }
Nicolas Capens87852e12016-11-24 14:45:06 -0500217
Nicolas Capens66478362016-10-13 15:36:36 -0400218 intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset;
219 Elf32_Sym &symbol = ((Elf32_Sym*)symbolAddress)[index];
220 uint16_t section = symbol.st_shndx;
221
222 if(section != SHN_UNDEF && section < SHN_LORESERVE)
223 {
224 const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx);
225 symbolValue = reinterpret_cast<void*>((intptr_t)elfHeader + symbol.st_value + target->sh_offset);
226 }
227 else
228 {
229 return nullptr;
230 }
231 }
232
233 switch(relocation.getType())
234 {
235 case R_386_NONE:
236 // No relocation
237 break;
238 case R_386_32:
239 *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite);
240 break;
241 // case R_386_PC32:
242 // *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite);
243 // break;
244 default:
245 assert(false && "Unsupported relocation type");
246 return nullptr;
247 }
248
249 return symbolValue;
250 }
251
252 static void *relocateSymbol(const ElfHeader *elfHeader, const Elf64_Rela &relocation, const SectionHeader &relocationTable)
253 {
254 const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info);
Nicolas Capens87852e12016-11-24 14:45:06 -0500255
Nicolas Capens66478362016-10-13 15:36:36 -0400256 intptr_t address = (intptr_t)elfHeader + target->sh_offset;
257 int32_t *patchSite = (int*)(address + relocation.r_offset);
258 uint32_t index = relocation.getSymbol();
259 int table = relocationTable.sh_link;
260 void *symbolValue = nullptr;
261
262 if(index != SHN_UNDEF)
263 {
264 if(table == SHN_UNDEF) return nullptr;
265 const SectionHeader *symbolTable = elfSection(elfHeader, table);
Nicolas Capens87852e12016-11-24 14:45:06 -0500266
Nicolas Capens66478362016-10-13 15:36:36 -0400267 uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize;
268 if(index >= symtab_entries)
269 {
270 assert(index < symtab_entries && "Symbol Index out of range");
271 return nullptr;
272 }
Nicolas Capens87852e12016-11-24 14:45:06 -0500273
Nicolas Capens66478362016-10-13 15:36:36 -0400274 intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset;
275 Elf64_Sym &symbol = ((Elf64_Sym*)symbolAddress)[index];
276 uint16_t section = symbol.st_shndx;
277
278 if(section != SHN_UNDEF && section < SHN_LORESERVE)
279 {
280 const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx);
281 symbolValue = reinterpret_cast<void*>((intptr_t)elfHeader + symbol.st_value + target->sh_offset);
282 }
283 else
284 {
285 return nullptr;
286 }
287 }
288
289 switch(relocation.getType())
290 {
291 case R_X86_64_NONE:
292 // No relocation
293 break;
Nicolas Capensb98fe5c2016-11-09 12:24:06 -0500294 case R_X86_64_64:
295 *(int64_t*)patchSite = (int64_t)((intptr_t)symbolValue + *(int64_t*)patchSite) + relocation.r_addend;
296 break;
Nicolas Capens66478362016-10-13 15:36:36 -0400297 case R_X86_64_PC32:
298 *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite) + relocation.r_addend;
299 break;
Nicolas Capensb98fe5c2016-11-09 12:24:06 -0500300 case R_X86_64_32S:
301 *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite) + relocation.r_addend;
302 break;
Nicolas Capens66478362016-10-13 15:36:36 -0400303 default:
304 assert(false && "Unsupported relocation type");
305 return nullptr;
306 }
307
308 return symbolValue;
309 }
310
Nicolas Capens1cc44382017-04-25 10:52:16 -0400311 void *loadImage(uint8_t *const elfImage, size_t &codeSize)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400312 {
Nicolas Capens598f8d82016-09-26 15:09:10 -0400313 ElfHeader *elfHeader = (ElfHeader*)elfImage;
314
315 if(!elfHeader->checkMagic())
316 {
317 return nullptr;
318 }
319
Nicolas Capens66478362016-10-13 15:36:36 -0400320 // Expect ELF bitness to match platform
Nicolas Capens65047112016-11-07 13:01:07 -0500321 assert(sizeof(void*) == 8 ? elfHeader->getFileClass() == ELFCLASS64 : elfHeader->getFileClass() == ELFCLASS32);
Nicolas Capens66478362016-10-13 15:36:36 -0400322 assert(sizeof(void*) == 8 ? elfHeader->e_machine == EM_X86_64 : elfHeader->e_machine == EM_386);
323
Nicolas Capens598f8d82016-09-26 15:09:10 -0400324 SectionHeader *sectionHeader = (SectionHeader*)(elfImage + elfHeader->e_shoff);
325 void *entry = nullptr;
326
327 for(int i = 0; i < elfHeader->e_shnum; i++)
328 {
Nicolas Capens66478362016-10-13 15:36:36 -0400329 if(sectionHeader[i].sh_type == SHT_PROGBITS)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400330 {
Nicolas Capens66478362016-10-13 15:36:36 -0400331 if(sectionHeader[i].sh_flags & SHF_EXECINSTR)
332 {
333 entry = elfImage + sectionHeader[i].sh_offset;
Nicolas Capens1cc44382017-04-25 10:52:16 -0400334 codeSize = sectionHeader[i].sh_size;
Nicolas Capens66478362016-10-13 15:36:36 -0400335 }
336 }
337 else if(sectionHeader[i].sh_type == SHT_REL)
338 {
339 assert(sizeof(void*) == 4 && "UNIMPLEMENTED"); // Only expected/implemented for 32-bit code
340
Alexis Hetu113e33a2017-01-19 10:49:19 -0500341 for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++)
Nicolas Capens66478362016-10-13 15:36:36 -0400342 {
343 const Elf32_Rel &relocation = ((const Elf32_Rel*)(elfImage + sectionHeader[i].sh_offset))[index];
Alexis Hetu113e33a2017-01-19 10:49:19 -0500344 relocateSymbol(elfHeader, relocation, sectionHeader[i]);
Nicolas Capens66478362016-10-13 15:36:36 -0400345 }
346 }
347 else if(sectionHeader[i].sh_type == SHT_RELA)
348 {
349 assert(sizeof(void*) == 8 && "UNIMPLEMENTED"); // Only expected/implemented for 64-bit code
350
Alexis Hetu113e33a2017-01-19 10:49:19 -0500351 for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++)
Nicolas Capens66478362016-10-13 15:36:36 -0400352 {
353 const Elf64_Rela &relocation = ((const Elf64_Rela*)(elfImage + sectionHeader[i].sh_offset))[index];
Alexis Hetu113e33a2017-01-19 10:49:19 -0500354 relocateSymbol(elfHeader, relocation, sectionHeader[i]);
Nicolas Capens66478362016-10-13 15:36:36 -0400355 }
Nicolas Capens598f8d82016-09-26 15:09:10 -0400356 }
357 }
358
359 return entry;
360 }
361
362 template<typename T>
363 struct ExecutableAllocator
364 {
365 ExecutableAllocator() {};
366 template<class U> ExecutableAllocator(const ExecutableAllocator<U> &other) {};
367
368 using value_type = T;
369 using size_type = std::size_t;
370
371 T *allocate(size_type n)
372 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500373 #if defined(_WIN32)
374 return (T*)VirtualAlloc(NULL, sizeof(T) * n, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
375 #else
376 return (T*)mmap(nullptr, sizeof(T) * n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
377 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400378 }
379
380 void deallocate(T *p, size_type n)
381 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500382 #if defined(_WIN32)
383 VirtualFree(p, 0, MEM_RELEASE);
384 #else
385 munmap(p, sizeof(T) * n);
386 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400387 }
388 };
389
390 class ELFMemoryStreamer : public Ice::ELFStreamer, public Routine
391 {
392 ELFMemoryStreamer(const ELFMemoryStreamer &) = delete;
393 ELFMemoryStreamer &operator=(const ELFMemoryStreamer &) = delete;
394
395 public:
Nicolas Capens58274b52016-10-19 23:45:19 -0400396 ELFMemoryStreamer() : Routine(), entry(nullptr)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400397 {
398 position = 0;
399 buffer.reserve(0x1000);
400 }
401
402 virtual ~ELFMemoryStreamer()
403 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500404 #if defined(_WIN32)
405 if(buffer.size() != 0)
406 {
407 DWORD exeProtection;
408 VirtualProtect(&buffer[0], buffer.size(), oldProtection, &exeProtection);
409 }
410 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400411 }
412
413 void write8(uint8_t Value) override
414 {
415 if(position == (uint64_t)buffer.size())
416 {
417 buffer.push_back(Value);
418 position++;
419 }
420 else if(position < (uint64_t)buffer.size())
421 {
422 buffer[position] = Value;
423 position++;
424 }
425 else assert(false && "UNIMPLEMENTED");
426 }
427
428 void writeBytes(llvm::StringRef Bytes) override
429 {
430 std::size_t oldSize = buffer.size();
431 buffer.resize(oldSize + Bytes.size());
432 memcpy(&buffer[oldSize], Bytes.begin(), Bytes.size());
433 position += Bytes.size();
434 }
435
436 uint64_t tell() const override { return position; }
437
438 void seek(uint64_t Off) override { position = Off; }
439
440 const void *getEntry() override
441 {
Nicolas Capens58274b52016-10-19 23:45:19 -0400442 if(!entry)
443 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500444 #if defined(_WIN32)
445 VirtualProtect(&buffer[0], buffer.size(), PAGE_EXECUTE_READWRITE, &oldProtection);
446 #else
447 mprotect(&buffer[0], buffer.size(), PROT_READ | PROT_WRITE | PROT_EXEC);
448 #endif
449
450 position = std::numeric_limits<std::size_t>::max(); // Can't stream more data after this
Nicolas Capens598f8d82016-09-26 15:09:10 -0400451
Nicolas Capens1cc44382017-04-25 10:52:16 -0400452 size_t codeSize = 0;
453 entry = loadImage(&buffer[0], codeSize);
454
455 #if defined(_WIN32)
456 FlushInstructionCache(GetCurrentProcess(), NULL, 0);
457 #else
458 __builtin___clear_cache((char*)entry, (char*)entry + codeSize);
459 #endif
Nicolas Capens58274b52016-10-19 23:45:19 -0400460 }
461
462 return entry;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400463 }
464
465 private:
Nicolas Capens58274b52016-10-19 23:45:19 -0400466 void *entry;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400467 std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer;
468 std::size_t position;
Nicolas Capensbd65da92017-01-05 16:31:06 -0500469
470 #if defined(_WIN32)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400471 DWORD oldProtection;
Nicolas Capensbd65da92017-01-05 16:31:06 -0500472 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400473 };
474
475 Nucleus::Nucleus()
476 {
477 ::codegenMutex.lock(); // Reactor is currently not thread safe
478
Nicolas Capens66478362016-10-13 15:36:36 -0400479 Ice::ClFlags &Flags = Ice::ClFlags::Flags;
480 Ice::ClFlags::getParsedClFlags(Flags);
481
482 Flags.setTargetArch(sizeof(void*) == 8 ? Ice::Target_X8664 : Ice::Target_X8632);
483 Flags.setOutFileType(Ice::FT_Elf);
484 Flags.setOptLevel(Ice::Opt_2);
485 Flags.setApplicationBinaryInterface(Ice::ABI_Platform);
Nicolas Capensccd5ecb2017-01-14 12:52:55 -0500486 Flags.setTargetInstructionSet(CPUID::SSE4_1 ? Ice::X86InstructionSet_SSE4_1 : Ice::X86InstructionSet_SSE2);
Nicolas Capens65047112016-11-07 13:01:07 -0500487 Flags.setVerbose(false ? Ice::IceV_All : Ice::IceV_None);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400488
Nicolas Capens65047112016-11-07 13:01:07 -0500489 static llvm::raw_os_ostream cout(std::cout);
490 static llvm::raw_os_ostream cerr(std::cerr);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400491
492 if(false) // Write out to a file
493 {
494 std::error_code errorCode;
495 ::out = new Ice::Fdstream("out.o", errorCode, llvm::sys::fs::F_None);
496 ::elfFile = new Ice::ELFFileStreamer(*out);
Nicolas Capens65047112016-11-07 13:01:07 -0500497 ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfFile);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400498 }
499 else
500 {
501 ELFMemoryStreamer *elfMemory = new ELFMemoryStreamer();
Nicolas Capens65047112016-11-07 13:01:07 -0500502 ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfMemory);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400503 ::routine = elfMemory;
504 }
505 }
506
507 Nucleus::~Nucleus()
508 {
509 delete ::allocator;
510 delete ::function;
511 delete ::context;
512
513 delete ::elfFile;
514 delete ::out;
515
516 ::codegenMutex.unlock();
517 }
518
519 Routine *Nucleus::acquireRoutine(const wchar_t *name, bool runOptimizations)
520 {
521 if(basicBlock->getInsts().empty() || basicBlock->getInsts().back().getKind() != Ice::Inst::Ret)
522 {
523 createRetVoid();
524 }
525
526 std::wstring wideName(name);
527 std::string asciiName(wideName.begin(), wideName.end());
528 ::function->setFunctionName(Ice::GlobalString::createWithString(::context, asciiName));
529
Nicolas Capens2ae9d742016-11-24 14:43:05 -0500530 optimize();
531
Nicolas Capens598f8d82016-09-26 15:09:10 -0400532 ::function->translate();
Nicolas Capensde19f392016-10-19 10:29:49 -0400533 assert(!::function->hasError());
534
Nicolas Capens66478362016-10-13 15:36:36 -0400535 auto *globals = ::function->getGlobalInits().release();
536
537 if(globals && !globals->empty())
538 {
539 ::context->getGlobals()->merge(globals);
540 }
Nicolas Capens598f8d82016-09-26 15:09:10 -0400541
542 ::context->emitFileHeader();
543 ::function->emitIAS();
544 auto assembler = ::function->releaseAssembler();
Nicolas Capens66478362016-10-13 15:36:36 -0400545 auto objectWriter = ::context->getObjectWriter();
546 assembler->alignFunction();
547 objectWriter->writeFunctionCode(::function->getFunctionName(), false, assembler.get());
548 ::context->lowerGlobals("last");
Nicolas Capens73dd7a22016-10-20 13:20:34 -0400549 ::context->lowerConstants();
Nicolas Capensb98fe5c2016-11-09 12:24:06 -0500550 ::context->lowerJumpTables();
Nicolas Capens66478362016-10-13 15:36:36 -0400551 objectWriter->setUndefinedSyms(::context->getConstantExternSyms());
552 objectWriter->writeNonUserSections();
Nicolas Capens598f8d82016-09-26 15:09:10 -0400553
554 return ::routine;
555 }
556
557 void Nucleus::optimize()
558 {
Nicolas Capens2ae9d742016-11-24 14:43:05 -0500559 sw::optimize(::function);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400560 }
561
562 Value *Nucleus::allocateStackVariable(Type *t, int arraySize)
563 {
564 Ice::Type type = T(t);
Nicolas Capensa8f98632016-10-20 11:25:55 -0400565 int typeSize = Ice::typeWidthInBytes(type);
566 int totalSize = typeSize * (arraySize ? arraySize : 1);
Nicolas Capense12780d2016-09-27 14:18:07 -0400567
Nicolas Capensa8f98632016-10-20 11:25:55 -0400568 auto bytes = Ice::ConstantInteger32::create(::context, type, totalSize);
Nicolas Capense12780d2016-09-27 14:18:07 -0400569 auto address = ::function->makeVariable(T(getPointerType(t)));
Nicolas Capensa8f98632016-10-20 11:25:55 -0400570 auto alloca = Ice::InstAlloca::create(::function, address, bytes, typeSize);
Nicolas Capense12780d2016-09-27 14:18:07 -0400571 ::function->getEntryNode()->getInsts().push_front(alloca);
572
573 return V(address);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400574 }
575
576 BasicBlock *Nucleus::createBasicBlock()
577 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400578 return B(::function->makeNode());
Nicolas Capens598f8d82016-09-26 15:09:10 -0400579 }
580
581 BasicBlock *Nucleus::getInsertBlock()
582 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400583 return B(::basicBlock);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400584 }
585
586 void Nucleus::setInsertBlock(BasicBlock *basicBlock)
587 {
Nicolas Capens9ed1a182016-10-24 09:52:23 -0400588 // assert(::basicBlock->getInsts().back().getTerminatorEdges().size() >= 0 && "Previous basic block must have a terminator");
Nicolas Capens611642a2016-09-28 16:45:04 -0400589 ::basicBlock = basicBlock;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400590 }
591
Nicolas Capens598f8d82016-09-26 15:09:10 -0400592 void Nucleus::createFunction(Type *ReturnType, std::vector<Type*> &Params)
593 {
594 uint32_t sequenceNumber = 0;
595 ::function = Ice::Cfg::create(::context, sequenceNumber).release();
596 ::allocator = new Ice::CfgLocalAllocatorScope(::function);
597
598 for(Type *type : Params)
599 {
600 Ice::Variable *arg = ::function->makeVariable(T(type));
601 ::function->addArg(arg);
602 }
603
604 Ice::CfgNode *node = ::function->makeNode();
605 ::function->setEntryNode(node);
606 ::basicBlock = node;
607 }
608
609 Value *Nucleus::getArgument(unsigned int index)
610 {
611 return V(::function->getArgs()[index]);
612 }
613
614 void Nucleus::createRetVoid()
615 {
Nicolas Capensfdcca2d2016-10-20 11:31:36 -0400616 Ice::InstRet *ret = Ice::InstRet::create(::function);
617 ::basicBlock->appendInst(ret);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400618 }
619
620 void Nucleus::createRet(Value *v)
621 {
Nicolas Capensfdcca2d2016-10-20 11:31:36 -0400622 Ice::InstRet *ret = Ice::InstRet::create(::function, v);
623 ::basicBlock->appendInst(ret);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400624 }
625
626 void Nucleus::createBr(BasicBlock *dest)
627 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400628 auto br = Ice::InstBr::create(::function, dest);
629 ::basicBlock->appendInst(br);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400630 }
631
632 void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse)
633 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400634 auto br = Ice::InstBr::create(::function, cond, ifTrue, ifFalse);
635 ::basicBlock->appendInst(br);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400636 }
637
Nicolas Capensf8360ba2017-01-25 11:35:00 -0800638 static bool isCommutative(Ice::InstArithmetic::OpKind op)
639 {
640 switch(op)
641 {
642 case Ice::InstArithmetic::Add:
643 case Ice::InstArithmetic::Fadd:
644 case Ice::InstArithmetic::Mul:
645 case Ice::InstArithmetic::Fmul:
646 case Ice::InstArithmetic::And:
647 case Ice::InstArithmetic::Or:
648 case Ice::InstArithmetic::Xor:
649 return true;
650 default:
651 return false;
652 }
653 }
654
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400655 static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs)
656 {
Nicolas Capens327f1df2016-10-21 14:26:34 -0400657 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 -0400658
Nicolas Capensf8360ba2017-01-25 11:35:00 -0800659 bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op);
660
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400661 Ice::Variable *result = ::function->makeVariable(lhs->getType());
Nicolas Capensf8360ba2017-01-25 11:35:00 -0800662 Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs);
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400663 ::basicBlock->appendInst(arithmetic);
664
665 return V(result);
666 }
667
Nicolas Capens598f8d82016-09-26 15:09:10 -0400668 Value *Nucleus::createAdd(Value *lhs, Value *rhs)
669 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400670 return createArithmetic(Ice::InstArithmetic::Add, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400671 }
672
673 Value *Nucleus::createSub(Value *lhs, Value *rhs)
674 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400675 return createArithmetic(Ice::InstArithmetic::Sub, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400676 }
677
678 Value *Nucleus::createMul(Value *lhs, Value *rhs)
679 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400680 return createArithmetic(Ice::InstArithmetic::Mul, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400681 }
682
683 Value *Nucleus::createUDiv(Value *lhs, Value *rhs)
684 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400685 return createArithmetic(Ice::InstArithmetic::Udiv, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400686 }
687
688 Value *Nucleus::createSDiv(Value *lhs, Value *rhs)
689 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400690 return createArithmetic(Ice::InstArithmetic::Sdiv, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400691 }
692
693 Value *Nucleus::createFAdd(Value *lhs, Value *rhs)
694 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400695 return createArithmetic(Ice::InstArithmetic::Fadd, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400696 }
697
698 Value *Nucleus::createFSub(Value *lhs, Value *rhs)
699 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400700 return createArithmetic(Ice::InstArithmetic::Fsub, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400701 }
702
703 Value *Nucleus::createFMul(Value *lhs, Value *rhs)
704 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400705 return createArithmetic(Ice::InstArithmetic::Fmul, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400706 }
707
708 Value *Nucleus::createFDiv(Value *lhs, Value *rhs)
709 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400710 return createArithmetic(Ice::InstArithmetic::Fdiv, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400711 }
712
713 Value *Nucleus::createURem(Value *lhs, Value *rhs)
714 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400715 return createArithmetic(Ice::InstArithmetic::Urem, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400716 }
717
718 Value *Nucleus::createSRem(Value *lhs, Value *rhs)
719 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400720 return createArithmetic(Ice::InstArithmetic::Srem, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400721 }
722
723 Value *Nucleus::createFRem(Value *lhs, Value *rhs)
724 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400725 return createArithmetic(Ice::InstArithmetic::Frem, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400726 }
727
728 Value *Nucleus::createShl(Value *lhs, Value *rhs)
729 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400730 return createArithmetic(Ice::InstArithmetic::Shl, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400731 }
732
733 Value *Nucleus::createLShr(Value *lhs, Value *rhs)
734 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400735 return createArithmetic(Ice::InstArithmetic::Lshr, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400736 }
737
738 Value *Nucleus::createAShr(Value *lhs, Value *rhs)
739 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400740 return createArithmetic(Ice::InstArithmetic::Ashr, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400741 }
742
743 Value *Nucleus::createAnd(Value *lhs, Value *rhs)
744 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400745 return createArithmetic(Ice::InstArithmetic::And, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400746 }
747
748 Value *Nucleus::createOr(Value *lhs, Value *rhs)
749 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400750 return createArithmetic(Ice::InstArithmetic::Or, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400751 }
752
753 Value *Nucleus::createXor(Value *lhs, Value *rhs)
754 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400755 return createArithmetic(Ice::InstArithmetic::Xor, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400756 }
757
758 Value *Nucleus::createNeg(Value *v)
759 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500760 return createSub(createNullValue(T(v->getType())), v);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400761 }
762
763 Value *Nucleus::createFNeg(Value *v)
764 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500765 double c[4] = {-0.0, -0.0, -0.0, -0.0};
766 Value *negativeZero = Ice::isVectorType(v->getType()) ?
767 createConstantVector(c, T(v->getType())) :
Nicolas Capens15060bb2016-12-05 22:17:19 -0500768 V(::context->getConstantFloat(-0.0f));
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500769
770 return createFSub(negativeZero, v);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400771 }
772
773 Value *Nucleus::createNot(Value *v)
774 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500775 if(Ice::isScalarIntegerType(v->getType()))
776 {
Nicolas Capens15060bb2016-12-05 22:17:19 -0500777 return createXor(v, V(::context->getConstantInt(v->getType(), -1)));
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500778 }
779 else // Vector
780 {
781 int64_t c[4] = {-1, -1, -1, -1};
782 return createXor(v, createConstantVector(c, T(v->getType())));
783 }
Nicolas Capens598f8d82016-09-26 15:09:10 -0400784 }
785
Nicolas Capense12780d2016-09-27 14:18:07 -0400786 Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400787 {
Nicolas Capens23d99a42016-09-30 14:57:16 -0400788 int valueType = (int)reinterpret_cast<intptr_t>(type);
789 Ice::Variable *result = ::function->makeVariable(T(type));
790
791 if(valueType & EmulatedBits)
792 {
Nicolas Capens584088c2017-01-26 16:05:18 -0800793 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::LoadSubVector, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
794 auto target = ::context->getConstantUndef(Ice::IceType_i32);
795 auto load = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
796 load->addArg(ptr);
797 load->addArg(::context->getConstantInt32(typeSize(type)));
798 ::basicBlock->appendInst(load);
Nicolas Capens23d99a42016-09-30 14:57:16 -0400799 }
800 else
801 {
802 auto load = Ice::InstLoad::create(::function, result, ptr, align);
803 ::basicBlock->appendInst(load);
804 }
805
806 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400807 }
808
Nicolas Capens6d738712016-09-30 04:15:22 -0400809 Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400810 {
Nicolas Capens23d99a42016-09-30 14:57:16 -0400811 int valueType = (int)reinterpret_cast<intptr_t>(type);
812
813 if(valueType & EmulatedBits)
814 {
Nicolas Capens584088c2017-01-26 16:05:18 -0800815 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::StoreSubVector, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T};
816 auto target = ::context->getConstantUndef(Ice::IceType_i32);
817 auto store = Ice::InstIntrinsicCall::create(::function, 3, nullptr, target, intrinsic);
818 store->addArg(value);
819 store->addArg(ptr);
820 store->addArg(::context->getConstantInt32(typeSize(type)));
821 ::basicBlock->appendInst(store);
Nicolas Capens23d99a42016-09-30 14:57:16 -0400822 }
823 else
824 {
825 assert(T(value->getType()) == type);
826
827 auto store = Ice::InstStore::create(::function, value, ptr, align);
828 ::basicBlock->appendInst(store);
829 }
830
Nicolas Capens598f8d82016-09-26 15:09:10 -0400831 return value;
832 }
833
Nicolas Capensd294def2017-01-26 17:44:37 -0800834 Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400835 {
Nicolas Capens8820f642016-09-30 04:42:43 -0400836 assert(index->getType() == Ice::IceType_i32);
837
Nicolas Capens15060bb2016-12-05 22:17:19 -0500838 if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index))
839 {
Nicolas Capens584088c2017-01-26 16:05:18 -0800840 int32_t offset = constant->getValue() * (int)typeSize(type);
Nicolas Capens15060bb2016-12-05 22:17:19 -0500841
842 if(offset == 0)
843 {
844 return ptr;
845 }
846
847 return createAdd(ptr, createConstantInt(offset));
848 }
849
Nicolas Capens8820f642016-09-30 04:42:43 -0400850 if(!Ice::isByteSizedType(T(type)))
851 {
Nicolas Capens584088c2017-01-26 16:05:18 -0800852 index = createMul(index, createConstantInt((int)typeSize(type)));
Nicolas Capens8820f642016-09-30 04:42:43 -0400853 }
854
855 if(sizeof(void*) == 8)
856 {
Nicolas Capensd294def2017-01-26 17:44:37 -0800857 if(unsignedIndex)
858 {
859 index = createZExt(index, T(Ice::IceType_i64));
860 }
861 else
862 {
863 index = createSExt(index, T(Ice::IceType_i64));
864 }
Nicolas Capens8820f642016-09-30 04:42:43 -0400865 }
866
867 return createAdd(ptr, index);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400868 }
869
870 Value *Nucleus::createAtomicAdd(Value *ptr, Value *value)
871 {
872 assert(false && "UNIMPLEMENTED"); return nullptr;
873 }
874
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400875 static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType)
876 {
Nicolas Capens23d99a42016-09-30 14:57:16 -0400877 if(v->getType() == T(destType))
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400878 {
879 return v;
880 }
881
882 Ice::Variable *result = ::function->makeVariable(T(destType));
883 Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v);
884 ::basicBlock->appendInst(cast);
885
886 return V(result);
887 }
888
Nicolas Capens598f8d82016-09-26 15:09:10 -0400889 Value *Nucleus::createTrunc(Value *v, Type *destType)
890 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400891 return createCast(Ice::InstCast::Trunc, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400892 }
893
894 Value *Nucleus::createZExt(Value *v, Type *destType)
895 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400896 return createCast(Ice::InstCast::Zext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400897 }
898
899 Value *Nucleus::createSExt(Value *v, Type *destType)
900 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400901 return createCast(Ice::InstCast::Sext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400902 }
903
904 Value *Nucleus::createFPToSI(Value *v, Type *destType)
905 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400906 return createCast(Ice::InstCast::Fptosi, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400907 }
908
Nicolas Capens598f8d82016-09-26 15:09:10 -0400909 Value *Nucleus::createSIToFP(Value *v, Type *destType)
910 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400911 return createCast(Ice::InstCast::Sitofp, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400912 }
913
914 Value *Nucleus::createFPTrunc(Value *v, Type *destType)
915 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400916 return createCast(Ice::InstCast::Fptrunc, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400917 }
918
919 Value *Nucleus::createFPExt(Value *v, Type *destType)
920 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400921 return createCast(Ice::InstCast::Fpext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400922 }
923
924 Value *Nucleus::createBitCast(Value *v, Type *destType)
925 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400926 return createCast(Ice::InstCast::Bitcast, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400927 }
928
Nicolas Capens43dc6292016-10-20 00:01:38 -0400929 static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400930 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400931 assert(lhs->getType() == rhs->getType());
932
Nicolas Capens43dc6292016-10-20 00:01:38 -0400933 auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType());
934 auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs);
Nicolas Capens611642a2016-09-28 16:45:04 -0400935 ::basicBlock->appendInst(cmp);
936
937 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400938 }
939
Nicolas Capens43dc6292016-10-20 00:01:38 -0400940 Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs)
941 {
942 return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs);
943 }
944
945 Value *Nucleus::createICmpNE(Value *lhs, Value *rhs)
946 {
947 return createIntCompare(Ice::InstIcmp::Ne, lhs, rhs);
948 }
949
950 Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs)
951 {
952 return createIntCompare(Ice::InstIcmp::Ugt, lhs, rhs);
953 }
954
955 Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs)
956 {
957 return createIntCompare(Ice::InstIcmp::Uge, lhs, rhs);
958 }
959
960 Value *Nucleus::createICmpULT(Value *lhs, Value *rhs)
961 {
962 return createIntCompare(Ice::InstIcmp::Ult, lhs, rhs);
963 }
964
965 Value *Nucleus::createICmpULE(Value *lhs, Value *rhs)
966 {
967 return createIntCompare(Ice::InstIcmp::Ule, lhs, rhs);
968 }
969
970 Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs)
971 {
972 return createIntCompare(Ice::InstIcmp::Sgt, lhs, rhs);
973 }
974
975 Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs)
976 {
977 return createIntCompare(Ice::InstIcmp::Sge, lhs, rhs);
978 }
979
980 Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs)
981 {
982 return createIntCompare(Ice::InstIcmp::Slt, lhs, rhs);
983 }
984
Nicolas Capens598f8d82016-09-26 15:09:10 -0400985 Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs)
986 {
Nicolas Capens43dc6292016-10-20 00:01:38 -0400987 return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs);
988 }
989
990 static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs)
991 {
992 assert(lhs->getType() == rhs->getType());
993 assert(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32);
994
995 auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32);
996 auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs);
997 ::basicBlock->appendInst(cmp);
998
999 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001000 }
1001
1002 Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs)
1003 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001004 return createFloatCompare(Ice::InstFcmp::Oeq, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001005 }
1006
1007 Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs)
1008 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001009 return createFloatCompare(Ice::InstFcmp::Ogt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001010 }
1011
1012 Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs)
1013 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001014 return createFloatCompare(Ice::InstFcmp::Oge, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001015 }
1016
1017 Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs)
1018 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001019 return createFloatCompare(Ice::InstFcmp::Olt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001020 }
1021
1022 Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs)
1023 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001024 return createFloatCompare(Ice::InstFcmp::Ole, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001025 }
1026
1027 Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs)
1028 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001029 return createFloatCompare(Ice::InstFcmp::One, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001030 }
1031
1032 Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs)
1033 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001034 return createFloatCompare(Ice::InstFcmp::Ord, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001035 }
1036
1037 Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs)
1038 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001039 return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001040 }
1041
1042 Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs)
1043 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001044 return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001045 }
1046
1047 Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs)
1048 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001049 return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001050 }
1051
1052 Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs)
1053 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001054 return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001055 }
1056
1057 Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs)
1058 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001059 return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001060 }
1061
1062 Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs)
1063 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001064 return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001065 }
1066
1067 Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs)
1068 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001069 return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001070 }
1071
Nicolas Capense95d5342016-09-30 11:37:28 -04001072 Value *Nucleus::createExtractElement(Value *vector, Type *type, int index)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001073 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001074 auto result = ::function->makeVariable(T(type));
1075 auto extract = Ice::InstExtractElement::create(::function, result, vector, ::context->getConstantInt32(index));
1076 ::basicBlock->appendInst(extract);
1077
1078 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001079 }
1080
1081 Value *Nucleus::createInsertElement(Value *vector, Value *element, int index)
1082 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001083 auto result = ::function->makeVariable(vector->getType());
1084 auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index));
1085 ::basicBlock->appendInst(insert);
1086
1087 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001088 }
1089
Nicolas Capense89cd582016-09-30 14:23:47 -04001090 Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001091 {
Nicolas Capens619c0ab2016-09-30 14:46:24 -04001092 assert(V1->getType() == V2->getType());
1093
1094 int size = Ice::typeNumElements(V1->getType());
1095 auto result = ::function->makeVariable(V1->getType());
1096 auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2);
1097
1098 for(int i = 0; i < size; i++)
1099 {
1100 shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i])));
1101 }
1102
1103 ::basicBlock->appendInst(shuffle);
1104
1105 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001106 }
1107
1108 Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse)
1109 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04001110 assert(ifTrue->getType() == ifFalse->getType());
1111
1112 auto result = ::function->makeVariable(ifTrue->getType());
1113 auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse);
1114 ::basicBlock->appendInst(select);
1115
1116 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001117 }
1118
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001119 SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001120 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001121 auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch);
1122 ::basicBlock->appendInst(switchInst);
1123
1124 return reinterpret_cast<SwitchCases*>(switchInst);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001125 }
1126
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001127 void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001128 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001129 switchCases->addBranch(label, label, branch);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001130 }
1131
1132 void Nucleus::createUnreachable()
1133 {
Nicolas Capensfdcca2d2016-10-20 11:31:36 -04001134 Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function);
1135 ::basicBlock->appendInst(unreachable);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001136 }
1137
Nicolas Capense95d5342016-09-30 11:37:28 -04001138 static Value *createSwizzle4(Value *val, unsigned char select)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001139 {
Nicolas Capens619c0ab2016-09-30 14:46:24 -04001140 int swizzle[4] =
1141 {
1142 (select >> 0) & 0x03,
1143 (select >> 2) & 0x03,
1144 (select >> 4) & 0x03,
1145 (select >> 6) & 0x03,
1146 };
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001147
Nicolas Capens619c0ab2016-09-30 14:46:24 -04001148 return Nucleus::createShuffleVector(val, val, swizzle);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001149 }
1150
Nicolas Capense95d5342016-09-30 11:37:28 -04001151 static Value *createMask4(Value *lhs, Value *rhs, unsigned char select)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001152 {
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001153 int64_t mask[4] = {0, 0, 0, 0};
1154
1155 mask[(select >> 0) & 0x03] = -1;
1156 mask[(select >> 2) & 0x03] = -1;
1157 mask[(select >> 4) & 0x03] = -1;
1158 mask[(select >> 6) & 0x03] = -1;
1159
1160 Value *condition = Nucleus::createConstantVector(mask, T(Ice::IceType_v4i1));
1161 Value *result = Nucleus::createSelect(condition, rhs, lhs);
1162
1163 return result;
Nicolas Capens598f8d82016-09-26 15:09:10 -04001164 }
1165
Nicolas Capens598f8d82016-09-26 15:09:10 -04001166 Type *Nucleus::getPointerType(Type *ElementType)
1167 {
Nicolas Capense12780d2016-09-27 14:18:07 -04001168 if(sizeof(void*) == 8)
1169 {
1170 return T(Ice::IceType_i64);
1171 }
1172 else
1173 {
1174 return T(Ice::IceType_i32);
1175 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04001176 }
1177
Nicolas Capens13ac2322016-10-13 14:52:12 -04001178 Value *Nucleus::createNullValue(Type *Ty)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001179 {
Nicolas Capens73dd7a22016-10-20 13:20:34 -04001180 if(Ice::isVectorType(T(Ty)))
1181 {
Nicolas Capens30385f02017-04-18 13:03:47 -04001182 assert(Ice::typeNumElements(T(Ty)) <= 16);
1183 int64_t c[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Nicolas Capens73dd7a22016-10-20 13:20:34 -04001184 return createConstantVector(c, Ty);
1185 }
1186 else
1187 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001188 return V(::context->getConstantZero(T(Ty)));
Nicolas Capens73dd7a22016-10-20 13:20:34 -04001189 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04001190 }
1191
Nicolas Capens13ac2322016-10-13 14:52:12 -04001192 Value *Nucleus::createConstantLong(int64_t i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001193 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001194 return V(::context->getConstantInt64(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001195 }
1196
Nicolas Capens13ac2322016-10-13 14:52:12 -04001197 Value *Nucleus::createConstantInt(int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001198 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001199 return V(::context->getConstantInt32(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001200 }
1201
Nicolas Capens13ac2322016-10-13 14:52:12 -04001202 Value *Nucleus::createConstantInt(unsigned int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001203 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001204 return V(::context->getConstantInt32(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001205 }
1206
Nicolas Capens13ac2322016-10-13 14:52:12 -04001207 Value *Nucleus::createConstantBool(bool b)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001208 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001209 return V(::context->getConstantInt1(b));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001210 }
1211
Nicolas Capens13ac2322016-10-13 14:52:12 -04001212 Value *Nucleus::createConstantByte(signed char i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001213 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001214 return V(::context->getConstantInt8(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001215 }
1216
Nicolas Capens13ac2322016-10-13 14:52:12 -04001217 Value *Nucleus::createConstantByte(unsigned char i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001218 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001219 return V(::context->getConstantInt8(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001220 }
1221
Nicolas Capens13ac2322016-10-13 14:52:12 -04001222 Value *Nucleus::createConstantShort(short i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001223 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001224 return V(::context->getConstantInt16(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001225 }
1226
Nicolas Capens13ac2322016-10-13 14:52:12 -04001227 Value *Nucleus::createConstantShort(unsigned short i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001228 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001229 return V(::context->getConstantInt16(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001230 }
1231
Nicolas Capens13ac2322016-10-13 14:52:12 -04001232 Value *Nucleus::createConstantFloat(float x)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001233 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001234 return V(::context->getConstantFloat(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001235 }
1236
Nicolas Capens13ac2322016-10-13 14:52:12 -04001237 Value *Nucleus::createNullPointer(Type *Ty)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001238 {
Nicolas Capensa29d6532016-12-05 21:38:09 -05001239 return createNullValue(T(sizeof(void*) == 8 ? Ice::IceType_i64 : Ice::IceType_i32));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001240 }
1241
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001242 Value *Nucleus::createConstantVector(const int64_t *constants, Type *type)
Nicolas Capens13ac2322016-10-13 14:52:12 -04001243 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001244 const int vectorSize = 16;
1245 assert(Ice::typeWidthInBytes(T(type)) == vectorSize);
1246 const int alignment = vectorSize;
1247 auto globalPool = ::function->getGlobalPool();
1248
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001249 const int64_t *i = constants;
1250 const double *f = reinterpret_cast<const double*>(constants);
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001251 Ice::VariableDeclaration::DataInitializer *dataInitializer = nullptr;
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001252
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001253 switch((int)reinterpret_cast<intptr_t>(type))
1254 {
1255 case Ice::IceType_v4i32:
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001256 case Ice::IceType_v4i1:
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001257 {
1258 const int initializer[4] = {(int)i[0], (int)i[1], (int)i[2], (int)i[3]};
1259 static_assert(sizeof(initializer) == vectorSize, "!");
1260 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1261 }
1262 break;
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001263 case Ice::IceType_v4f32:
1264 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001265 const float initializer[4] = {(float)f[0], (float)f[1], (float)f[2], (float)f[3]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001266 static_assert(sizeof(initializer) == vectorSize, "!");
1267 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1268 }
1269 break;
1270 case Ice::IceType_v8i16:
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001271 case Ice::IceType_v8i1:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001272 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001273 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 -04001274 static_assert(sizeof(initializer) == vectorSize, "!");
1275 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1276 }
1277 break;
1278 case Ice::IceType_v16i8:
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001279 case Ice::IceType_v16i1:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001280 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001281 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 -04001282 static_assert(sizeof(initializer) == vectorSize, "!");
1283 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1284 }
1285 break;
1286 case Type_v2i32:
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001287 {
1288 const int initializer[4] = {(int)i[0], (int)i[1], (int)i[0], (int)i[1]};
1289 static_assert(sizeof(initializer) == vectorSize, "!");
1290 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1291 }
1292 break;
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001293 case Type_v2f32:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001294 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001295 const float initializer[4] = {(float)f[0], (float)f[1], (float)f[0], (float)f[1]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001296 static_assert(sizeof(initializer) == vectorSize, "!");
1297 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1298 }
1299 break;
1300 case Type_v4i16:
1301 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001302 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 -04001303 static_assert(sizeof(initializer) == vectorSize, "!");
1304 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1305 }
1306 break;
1307 case Type_v8i8:
1308 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001309 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 -04001310 static_assert(sizeof(initializer) == vectorSize, "!");
1311 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1312 }
1313 break;
1314 case Type_v4i8:
1315 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001316 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 -04001317 static_assert(sizeof(initializer) == vectorSize, "!");
1318 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1319 }
1320 break;
1321 default:
1322 assert(false && "Unknown constant vector type" && type);
1323 }
1324
1325 auto name = Ice::GlobalString::createWithoutString(::context);
1326 auto *variableDeclaration = Ice::VariableDeclaration::create(globalPool);
1327 variableDeclaration->setName(name);
1328 variableDeclaration->setAlignment(alignment);
1329 variableDeclaration->setIsConstant(true);
1330 variableDeclaration->addInitializer(dataInitializer);
Nicolas Capens87852e12016-11-24 14:45:06 -05001331
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001332 ::function->addGlobal(variableDeclaration);
1333
1334 constexpr int32_t offset = 0;
1335 Ice::Operand *ptr = ::context->getConstantSym(offset, name);
1336
1337 Ice::Variable *result = ::function->makeVariable(T(type));
1338 auto load = Ice::InstLoad::create(::function, result, ptr, alignment);
1339 ::basicBlock->appendInst(load);
1340
1341 return V(result);
Nicolas Capens13ac2322016-10-13 14:52:12 -04001342 }
1343
1344 Value *Nucleus::createConstantVector(const double *constants, Type *type)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001345 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001346 return createConstantVector((const int64_t*)constants, type);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001347 }
1348
1349 Type *Void::getType()
1350 {
1351 return T(Ice::IceType_void);
1352 }
1353
Nicolas Capens598f8d82016-09-26 15:09:10 -04001354 Bool::Bool(Argument<Bool> argument)
1355 {
1356 storeValue(argument.value);
1357 }
1358
Nicolas Capens598f8d82016-09-26 15:09:10 -04001359 Bool::Bool(bool x)
1360 {
1361 storeValue(Nucleus::createConstantBool(x));
1362 }
1363
1364 Bool::Bool(RValue<Bool> rhs)
1365 {
1366 storeValue(rhs.value);
1367 }
1368
1369 Bool::Bool(const Bool &rhs)
1370 {
1371 Value *value = rhs.loadValue();
1372 storeValue(value);
1373 }
1374
1375 Bool::Bool(const Reference<Bool> &rhs)
1376 {
1377 Value *value = rhs.loadValue();
1378 storeValue(value);
1379 }
1380
Nicolas Capens96d4e092016-11-18 14:22:38 -05001381 RValue<Bool> Bool::operator=(RValue<Bool> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001382 {
1383 storeValue(rhs.value);
1384
1385 return rhs;
1386 }
1387
Nicolas Capens96d4e092016-11-18 14:22:38 -05001388 RValue<Bool> Bool::operator=(const Bool &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001389 {
1390 Value *value = rhs.loadValue();
1391 storeValue(value);
1392
1393 return RValue<Bool>(value);
1394 }
1395
Nicolas Capens96d4e092016-11-18 14:22:38 -05001396 RValue<Bool> Bool::operator=(const Reference<Bool> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001397 {
1398 Value *value = rhs.loadValue();
1399 storeValue(value);
1400
1401 return RValue<Bool>(value);
1402 }
1403
1404 RValue<Bool> operator!(RValue<Bool> val)
1405 {
1406 return RValue<Bool>(Nucleus::createNot(val.value));
1407 }
1408
1409 RValue<Bool> operator&&(RValue<Bool> lhs, RValue<Bool> rhs)
1410 {
1411 return RValue<Bool>(Nucleus::createAnd(lhs.value, rhs.value));
1412 }
1413
1414 RValue<Bool> operator||(RValue<Bool> lhs, RValue<Bool> rhs)
1415 {
1416 return RValue<Bool>(Nucleus::createOr(lhs.value, rhs.value));
1417 }
1418
1419 Type *Bool::getType()
1420 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001421 return T(Ice::IceType_i1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001422 }
1423
1424 Byte::Byte(Argument<Byte> argument)
1425 {
1426 storeValue(argument.value);
1427 }
1428
1429 Byte::Byte(RValue<Int> cast)
1430 {
1431 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1432
1433 storeValue(integer);
1434 }
1435
1436 Byte::Byte(RValue<UInt> cast)
1437 {
1438 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1439
1440 storeValue(integer);
1441 }
1442
1443 Byte::Byte(RValue<UShort> cast)
1444 {
1445 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1446
1447 storeValue(integer);
1448 }
1449
Nicolas Capens598f8d82016-09-26 15:09:10 -04001450 Byte::Byte(int x)
1451 {
1452 storeValue(Nucleus::createConstantByte((unsigned char)x));
1453 }
1454
1455 Byte::Byte(unsigned char x)
1456 {
1457 storeValue(Nucleus::createConstantByte(x));
1458 }
1459
1460 Byte::Byte(RValue<Byte> rhs)
1461 {
1462 storeValue(rhs.value);
1463 }
1464
1465 Byte::Byte(const Byte &rhs)
1466 {
1467 Value *value = rhs.loadValue();
1468 storeValue(value);
1469 }
1470
1471 Byte::Byte(const Reference<Byte> &rhs)
1472 {
1473 Value *value = rhs.loadValue();
1474 storeValue(value);
1475 }
1476
Nicolas Capens96d4e092016-11-18 14:22:38 -05001477 RValue<Byte> Byte::operator=(RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001478 {
1479 storeValue(rhs.value);
1480
1481 return rhs;
1482 }
1483
Nicolas Capens96d4e092016-11-18 14:22:38 -05001484 RValue<Byte> Byte::operator=(const Byte &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001485 {
1486 Value *value = rhs.loadValue();
1487 storeValue(value);
1488
1489 return RValue<Byte>(value);
1490 }
1491
Nicolas Capens96d4e092016-11-18 14:22:38 -05001492 RValue<Byte> Byte::operator=(const Reference<Byte> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001493 {
1494 Value *value = rhs.loadValue();
1495 storeValue(value);
1496
1497 return RValue<Byte>(value);
1498 }
1499
1500 RValue<Byte> operator+(RValue<Byte> lhs, RValue<Byte> rhs)
1501 {
1502 return RValue<Byte>(Nucleus::createAdd(lhs.value, rhs.value));
1503 }
1504
1505 RValue<Byte> operator-(RValue<Byte> lhs, RValue<Byte> rhs)
1506 {
1507 return RValue<Byte>(Nucleus::createSub(lhs.value, rhs.value));
1508 }
1509
1510 RValue<Byte> operator*(RValue<Byte> lhs, RValue<Byte> rhs)
1511 {
1512 return RValue<Byte>(Nucleus::createMul(lhs.value, rhs.value));
1513 }
1514
1515 RValue<Byte> operator/(RValue<Byte> lhs, RValue<Byte> rhs)
1516 {
1517 return RValue<Byte>(Nucleus::createUDiv(lhs.value, rhs.value));
1518 }
1519
1520 RValue<Byte> operator%(RValue<Byte> lhs, RValue<Byte> rhs)
1521 {
1522 return RValue<Byte>(Nucleus::createURem(lhs.value, rhs.value));
1523 }
1524
1525 RValue<Byte> operator&(RValue<Byte> lhs, RValue<Byte> rhs)
1526 {
1527 return RValue<Byte>(Nucleus::createAnd(lhs.value, rhs.value));
1528 }
1529
1530 RValue<Byte> operator|(RValue<Byte> lhs, RValue<Byte> rhs)
1531 {
1532 return RValue<Byte>(Nucleus::createOr(lhs.value, rhs.value));
1533 }
1534
1535 RValue<Byte> operator^(RValue<Byte> lhs, RValue<Byte> rhs)
1536 {
1537 return RValue<Byte>(Nucleus::createXor(lhs.value, rhs.value));
1538 }
1539
1540 RValue<Byte> operator<<(RValue<Byte> lhs, RValue<Byte> rhs)
1541 {
1542 return RValue<Byte>(Nucleus::createShl(lhs.value, rhs.value));
1543 }
1544
1545 RValue<Byte> operator>>(RValue<Byte> lhs, RValue<Byte> rhs)
1546 {
1547 return RValue<Byte>(Nucleus::createLShr(lhs.value, rhs.value));
1548 }
1549
Nicolas Capens96d4e092016-11-18 14:22:38 -05001550 RValue<Byte> operator+=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001551 {
1552 return lhs = lhs + rhs;
1553 }
1554
Nicolas Capens96d4e092016-11-18 14:22:38 -05001555 RValue<Byte> operator-=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001556 {
1557 return lhs = lhs - rhs;
1558 }
1559
Nicolas Capens96d4e092016-11-18 14:22:38 -05001560 RValue<Byte> operator*=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001561 {
1562 return lhs = lhs * rhs;
1563 }
1564
Nicolas Capens96d4e092016-11-18 14:22:38 -05001565 RValue<Byte> operator/=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001566 {
1567 return lhs = lhs / rhs;
1568 }
1569
Nicolas Capens96d4e092016-11-18 14:22:38 -05001570 RValue<Byte> operator%=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001571 {
1572 return lhs = lhs % rhs;
1573 }
1574
Nicolas Capens96d4e092016-11-18 14:22:38 -05001575 RValue<Byte> operator&=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001576 {
1577 return lhs = lhs & rhs;
1578 }
1579
Nicolas Capens96d4e092016-11-18 14:22:38 -05001580 RValue<Byte> operator|=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001581 {
1582 return lhs = lhs | rhs;
1583 }
1584
Nicolas Capens96d4e092016-11-18 14:22:38 -05001585 RValue<Byte> operator^=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001586 {
1587 return lhs = lhs ^ rhs;
1588 }
1589
Nicolas Capens96d4e092016-11-18 14:22:38 -05001590 RValue<Byte> operator<<=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001591 {
1592 return lhs = lhs << rhs;
1593 }
1594
Nicolas Capens96d4e092016-11-18 14:22:38 -05001595 RValue<Byte> operator>>=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001596 {
1597 return lhs = lhs >> rhs;
1598 }
1599
1600 RValue<Byte> operator+(RValue<Byte> val)
1601 {
1602 return val;
1603 }
1604
1605 RValue<Byte> operator-(RValue<Byte> val)
1606 {
1607 return RValue<Byte>(Nucleus::createNeg(val.value));
1608 }
1609
1610 RValue<Byte> operator~(RValue<Byte> val)
1611 {
1612 return RValue<Byte>(Nucleus::createNot(val.value));
1613 }
1614
Nicolas Capens96d4e092016-11-18 14:22:38 -05001615 RValue<Byte> operator++(Byte &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001616 {
1617 RValue<Byte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001618 val += Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001619 return res;
1620 }
1621
Nicolas Capens96d4e092016-11-18 14:22:38 -05001622 const Byte &operator++(Byte &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001623 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001624 val += Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001625 return val;
1626 }
1627
Nicolas Capens96d4e092016-11-18 14:22:38 -05001628 RValue<Byte> operator--(Byte &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001629 {
1630 RValue<Byte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001631 val -= Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001632 return res;
1633 }
1634
Nicolas Capens96d4e092016-11-18 14:22:38 -05001635 const Byte &operator--(Byte &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001636 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001637 val -= Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001638 return val;
1639 }
1640
1641 RValue<Bool> operator<(RValue<Byte> lhs, RValue<Byte> rhs)
1642 {
1643 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
1644 }
1645
1646 RValue<Bool> operator<=(RValue<Byte> lhs, RValue<Byte> rhs)
1647 {
1648 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
1649 }
1650
1651 RValue<Bool> operator>(RValue<Byte> lhs, RValue<Byte> rhs)
1652 {
1653 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
1654 }
1655
1656 RValue<Bool> operator>=(RValue<Byte> lhs, RValue<Byte> rhs)
1657 {
1658 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
1659 }
1660
1661 RValue<Bool> operator!=(RValue<Byte> lhs, RValue<Byte> rhs)
1662 {
1663 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
1664 }
1665
1666 RValue<Bool> operator==(RValue<Byte> lhs, RValue<Byte> rhs)
1667 {
1668 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
1669 }
1670
1671 Type *Byte::getType()
1672 {
Nicolas Capens6d738712016-09-30 04:15:22 -04001673 return T(Ice::IceType_i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001674 }
1675
1676 SByte::SByte(Argument<SByte> argument)
1677 {
1678 storeValue(argument.value);
1679 }
1680
1681 SByte::SByte(RValue<Int> cast)
1682 {
1683 Value *integer = Nucleus::createTrunc(cast.value, SByte::getType());
1684
1685 storeValue(integer);
1686 }
1687
1688 SByte::SByte(RValue<Short> cast)
1689 {
1690 Value *integer = Nucleus::createTrunc(cast.value, SByte::getType());
1691
1692 storeValue(integer);
1693 }
1694
Nicolas Capens598f8d82016-09-26 15:09:10 -04001695 SByte::SByte(signed char x)
1696 {
1697 storeValue(Nucleus::createConstantByte(x));
1698 }
1699
1700 SByte::SByte(RValue<SByte> rhs)
1701 {
1702 storeValue(rhs.value);
1703 }
1704
1705 SByte::SByte(const SByte &rhs)
1706 {
1707 Value *value = rhs.loadValue();
1708 storeValue(value);
1709 }
1710
1711 SByte::SByte(const Reference<SByte> &rhs)
1712 {
1713 Value *value = rhs.loadValue();
1714 storeValue(value);
1715 }
1716
Nicolas Capens96d4e092016-11-18 14:22:38 -05001717 RValue<SByte> SByte::operator=(RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001718 {
1719 storeValue(rhs.value);
1720
1721 return rhs;
1722 }
1723
Nicolas Capens96d4e092016-11-18 14:22:38 -05001724 RValue<SByte> SByte::operator=(const SByte &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001725 {
1726 Value *value = rhs.loadValue();
1727 storeValue(value);
1728
1729 return RValue<SByte>(value);
1730 }
1731
Nicolas Capens96d4e092016-11-18 14:22:38 -05001732 RValue<SByte> SByte::operator=(const Reference<SByte> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001733 {
1734 Value *value = rhs.loadValue();
1735 storeValue(value);
1736
1737 return RValue<SByte>(value);
1738 }
1739
1740 RValue<SByte> operator+(RValue<SByte> lhs, RValue<SByte> rhs)
1741 {
1742 return RValue<SByte>(Nucleus::createAdd(lhs.value, rhs.value));
1743 }
1744
1745 RValue<SByte> operator-(RValue<SByte> lhs, RValue<SByte> rhs)
1746 {
1747 return RValue<SByte>(Nucleus::createSub(lhs.value, rhs.value));
1748 }
1749
1750 RValue<SByte> operator*(RValue<SByte> lhs, RValue<SByte> rhs)
1751 {
1752 return RValue<SByte>(Nucleus::createMul(lhs.value, rhs.value));
1753 }
1754
1755 RValue<SByte> operator/(RValue<SByte> lhs, RValue<SByte> rhs)
1756 {
1757 return RValue<SByte>(Nucleus::createSDiv(lhs.value, rhs.value));
1758 }
1759
1760 RValue<SByte> operator%(RValue<SByte> lhs, RValue<SByte> rhs)
1761 {
1762 return RValue<SByte>(Nucleus::createSRem(lhs.value, rhs.value));
1763 }
1764
1765 RValue<SByte> operator&(RValue<SByte> lhs, RValue<SByte> rhs)
1766 {
1767 return RValue<SByte>(Nucleus::createAnd(lhs.value, rhs.value));
1768 }
1769
1770 RValue<SByte> operator|(RValue<SByte> lhs, RValue<SByte> rhs)
1771 {
1772 return RValue<SByte>(Nucleus::createOr(lhs.value, rhs.value));
1773 }
1774
1775 RValue<SByte> operator^(RValue<SByte> lhs, RValue<SByte> rhs)
1776 {
1777 return RValue<SByte>(Nucleus::createXor(lhs.value, rhs.value));
1778 }
1779
1780 RValue<SByte> operator<<(RValue<SByte> lhs, RValue<SByte> rhs)
1781 {
1782 return RValue<SByte>(Nucleus::createShl(lhs.value, rhs.value));
1783 }
1784
1785 RValue<SByte> operator>>(RValue<SByte> lhs, RValue<SByte> rhs)
1786 {
1787 return RValue<SByte>(Nucleus::createAShr(lhs.value, rhs.value));
1788 }
1789
Nicolas Capens96d4e092016-11-18 14:22:38 -05001790 RValue<SByte> operator+=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001791 {
1792 return lhs = lhs + rhs;
1793 }
1794
Nicolas Capens96d4e092016-11-18 14:22:38 -05001795 RValue<SByte> operator-=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001796 {
1797 return lhs = lhs - rhs;
1798 }
1799
Nicolas Capens96d4e092016-11-18 14:22:38 -05001800 RValue<SByte> operator*=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001801 {
1802 return lhs = lhs * rhs;
1803 }
1804
Nicolas Capens96d4e092016-11-18 14:22:38 -05001805 RValue<SByte> operator/=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001806 {
1807 return lhs = lhs / rhs;
1808 }
1809
Nicolas Capens96d4e092016-11-18 14:22:38 -05001810 RValue<SByte> operator%=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001811 {
1812 return lhs = lhs % rhs;
1813 }
1814
Nicolas Capens96d4e092016-11-18 14:22:38 -05001815 RValue<SByte> operator&=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001816 {
1817 return lhs = lhs & rhs;
1818 }
1819
Nicolas Capens96d4e092016-11-18 14:22:38 -05001820 RValue<SByte> operator|=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001821 {
1822 return lhs = lhs | rhs;
1823 }
1824
Nicolas Capens96d4e092016-11-18 14:22:38 -05001825 RValue<SByte> operator^=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001826 {
1827 return lhs = lhs ^ rhs;
1828 }
1829
Nicolas Capens96d4e092016-11-18 14:22:38 -05001830 RValue<SByte> operator<<=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001831 {
1832 return lhs = lhs << rhs;
1833 }
1834
Nicolas Capens96d4e092016-11-18 14:22:38 -05001835 RValue<SByte> operator>>=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001836 {
1837 return lhs = lhs >> rhs;
1838 }
1839
1840 RValue<SByte> operator+(RValue<SByte> val)
1841 {
1842 return val;
1843 }
1844
1845 RValue<SByte> operator-(RValue<SByte> val)
1846 {
1847 return RValue<SByte>(Nucleus::createNeg(val.value));
1848 }
1849
1850 RValue<SByte> operator~(RValue<SByte> val)
1851 {
1852 return RValue<SByte>(Nucleus::createNot(val.value));
1853 }
1854
Nicolas Capens96d4e092016-11-18 14:22:38 -05001855 RValue<SByte> operator++(SByte &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001856 {
1857 RValue<SByte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001858 val += SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001859 return res;
1860 }
1861
Nicolas Capens96d4e092016-11-18 14:22:38 -05001862 const SByte &operator++(SByte &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001863 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001864 val += SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001865 return val;
1866 }
1867
Nicolas Capens96d4e092016-11-18 14:22:38 -05001868 RValue<SByte> operator--(SByte &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001869 {
1870 RValue<SByte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001871 val -= SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001872 return res;
1873 }
1874
Nicolas Capens96d4e092016-11-18 14:22:38 -05001875 const SByte &operator--(SByte &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001876 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001877 val -= SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001878 return val;
1879 }
1880
1881 RValue<Bool> operator<(RValue<SByte> lhs, RValue<SByte> rhs)
1882 {
1883 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
1884 }
1885
1886 RValue<Bool> operator<=(RValue<SByte> lhs, RValue<SByte> rhs)
1887 {
1888 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
1889 }
1890
1891 RValue<Bool> operator>(RValue<SByte> lhs, RValue<SByte> rhs)
1892 {
1893 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
1894 }
1895
1896 RValue<Bool> operator>=(RValue<SByte> lhs, RValue<SByte> rhs)
1897 {
1898 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
1899 }
1900
1901 RValue<Bool> operator!=(RValue<SByte> lhs, RValue<SByte> rhs)
1902 {
1903 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
1904 }
1905
1906 RValue<Bool> operator==(RValue<SByte> lhs, RValue<SByte> rhs)
1907 {
1908 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
1909 }
1910
1911 Type *SByte::getType()
1912 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001913 return T(Ice::IceType_i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001914 }
1915
1916 Short::Short(Argument<Short> argument)
1917 {
1918 storeValue(argument.value);
1919 }
1920
1921 Short::Short(RValue<Int> cast)
1922 {
1923 Value *integer = Nucleus::createTrunc(cast.value, Short::getType());
1924
1925 storeValue(integer);
1926 }
1927
Nicolas Capens598f8d82016-09-26 15:09:10 -04001928 Short::Short(short x)
1929 {
1930 storeValue(Nucleus::createConstantShort(x));
1931 }
1932
1933 Short::Short(RValue<Short> rhs)
1934 {
1935 storeValue(rhs.value);
1936 }
1937
1938 Short::Short(const Short &rhs)
1939 {
1940 Value *value = rhs.loadValue();
1941 storeValue(value);
1942 }
1943
1944 Short::Short(const Reference<Short> &rhs)
1945 {
1946 Value *value = rhs.loadValue();
1947 storeValue(value);
1948 }
1949
Nicolas Capens96d4e092016-11-18 14:22:38 -05001950 RValue<Short> Short::operator=(RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001951 {
1952 storeValue(rhs.value);
1953
1954 return rhs;
1955 }
1956
Nicolas Capens96d4e092016-11-18 14:22:38 -05001957 RValue<Short> Short::operator=(const Short &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001958 {
1959 Value *value = rhs.loadValue();
1960 storeValue(value);
1961
1962 return RValue<Short>(value);
1963 }
1964
Nicolas Capens96d4e092016-11-18 14:22:38 -05001965 RValue<Short> Short::operator=(const Reference<Short> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001966 {
1967 Value *value = rhs.loadValue();
1968 storeValue(value);
1969
1970 return RValue<Short>(value);
1971 }
1972
1973 RValue<Short> operator+(RValue<Short> lhs, RValue<Short> rhs)
1974 {
1975 return RValue<Short>(Nucleus::createAdd(lhs.value, rhs.value));
1976 }
1977
1978 RValue<Short> operator-(RValue<Short> lhs, RValue<Short> rhs)
1979 {
1980 return RValue<Short>(Nucleus::createSub(lhs.value, rhs.value));
1981 }
1982
1983 RValue<Short> operator*(RValue<Short> lhs, RValue<Short> rhs)
1984 {
1985 return RValue<Short>(Nucleus::createMul(lhs.value, rhs.value));
1986 }
1987
1988 RValue<Short> operator/(RValue<Short> lhs, RValue<Short> rhs)
1989 {
1990 return RValue<Short>(Nucleus::createSDiv(lhs.value, rhs.value));
1991 }
1992
1993 RValue<Short> operator%(RValue<Short> lhs, RValue<Short> rhs)
1994 {
1995 return RValue<Short>(Nucleus::createSRem(lhs.value, rhs.value));
1996 }
1997
1998 RValue<Short> operator&(RValue<Short> lhs, RValue<Short> rhs)
1999 {
2000 return RValue<Short>(Nucleus::createAnd(lhs.value, rhs.value));
2001 }
2002
2003 RValue<Short> operator|(RValue<Short> lhs, RValue<Short> rhs)
2004 {
2005 return RValue<Short>(Nucleus::createOr(lhs.value, rhs.value));
2006 }
2007
2008 RValue<Short> operator^(RValue<Short> lhs, RValue<Short> rhs)
2009 {
2010 return RValue<Short>(Nucleus::createXor(lhs.value, rhs.value));
2011 }
2012
2013 RValue<Short> operator<<(RValue<Short> lhs, RValue<Short> rhs)
2014 {
2015 return RValue<Short>(Nucleus::createShl(lhs.value, rhs.value));
2016 }
2017
2018 RValue<Short> operator>>(RValue<Short> lhs, RValue<Short> rhs)
2019 {
2020 return RValue<Short>(Nucleus::createAShr(lhs.value, rhs.value));
2021 }
2022
Nicolas Capens96d4e092016-11-18 14:22:38 -05002023 RValue<Short> operator+=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002024 {
2025 return lhs = lhs + rhs;
2026 }
2027
Nicolas Capens96d4e092016-11-18 14:22:38 -05002028 RValue<Short> operator-=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002029 {
2030 return lhs = lhs - rhs;
2031 }
2032
Nicolas Capens96d4e092016-11-18 14:22:38 -05002033 RValue<Short> operator*=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002034 {
2035 return lhs = lhs * rhs;
2036 }
2037
Nicolas Capens96d4e092016-11-18 14:22:38 -05002038 RValue<Short> operator/=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002039 {
2040 return lhs = lhs / rhs;
2041 }
2042
Nicolas Capens96d4e092016-11-18 14:22:38 -05002043 RValue<Short> operator%=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002044 {
2045 return lhs = lhs % rhs;
2046 }
2047
Nicolas Capens96d4e092016-11-18 14:22:38 -05002048 RValue<Short> operator&=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002049 {
2050 return lhs = lhs & rhs;
2051 }
2052
Nicolas Capens96d4e092016-11-18 14:22:38 -05002053 RValue<Short> operator|=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002054 {
2055 return lhs = lhs | rhs;
2056 }
2057
Nicolas Capens96d4e092016-11-18 14:22:38 -05002058 RValue<Short> operator^=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002059 {
2060 return lhs = lhs ^ rhs;
2061 }
2062
Nicolas Capens96d4e092016-11-18 14:22:38 -05002063 RValue<Short> operator<<=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002064 {
2065 return lhs = lhs << rhs;
2066 }
2067
Nicolas Capens96d4e092016-11-18 14:22:38 -05002068 RValue<Short> operator>>=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002069 {
2070 return lhs = lhs >> rhs;
2071 }
2072
2073 RValue<Short> operator+(RValue<Short> val)
2074 {
2075 return val;
2076 }
2077
2078 RValue<Short> operator-(RValue<Short> val)
2079 {
2080 return RValue<Short>(Nucleus::createNeg(val.value));
2081 }
2082
2083 RValue<Short> operator~(RValue<Short> val)
2084 {
2085 return RValue<Short>(Nucleus::createNot(val.value));
2086 }
2087
Nicolas Capens96d4e092016-11-18 14:22:38 -05002088 RValue<Short> operator++(Short &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002089 {
2090 RValue<Short> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002091 val += Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002092 return res;
2093 }
2094
Nicolas Capens96d4e092016-11-18 14:22:38 -05002095 const Short &operator++(Short &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002096 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002097 val += Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002098 return val;
2099 }
2100
Nicolas Capens96d4e092016-11-18 14:22:38 -05002101 RValue<Short> operator--(Short &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002102 {
2103 RValue<Short> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002104 val -= Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002105 return res;
2106 }
2107
Nicolas Capens96d4e092016-11-18 14:22:38 -05002108 const Short &operator--(Short &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002109 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002110 val -= Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002111 return val;
2112 }
2113
2114 RValue<Bool> operator<(RValue<Short> lhs, RValue<Short> rhs)
2115 {
2116 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
2117 }
2118
2119 RValue<Bool> operator<=(RValue<Short> lhs, RValue<Short> rhs)
2120 {
2121 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
2122 }
2123
2124 RValue<Bool> operator>(RValue<Short> lhs, RValue<Short> rhs)
2125 {
2126 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
2127 }
2128
2129 RValue<Bool> operator>=(RValue<Short> lhs, RValue<Short> rhs)
2130 {
2131 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
2132 }
2133
2134 RValue<Bool> operator!=(RValue<Short> lhs, RValue<Short> rhs)
2135 {
2136 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
2137 }
2138
2139 RValue<Bool> operator==(RValue<Short> lhs, RValue<Short> rhs)
2140 {
2141 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
2142 }
2143
2144 Type *Short::getType()
2145 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002146 return T(Ice::IceType_i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002147 }
2148
2149 UShort::UShort(Argument<UShort> argument)
2150 {
2151 storeValue(argument.value);
2152 }
2153
2154 UShort::UShort(RValue<UInt> cast)
2155 {
2156 Value *integer = Nucleus::createTrunc(cast.value, UShort::getType());
2157
2158 storeValue(integer);
2159 }
2160
2161 UShort::UShort(RValue<Int> cast)
2162 {
2163 Value *integer = Nucleus::createTrunc(cast.value, UShort::getType());
2164
2165 storeValue(integer);
2166 }
2167
Nicolas Capens598f8d82016-09-26 15:09:10 -04002168 UShort::UShort(unsigned short x)
2169 {
2170 storeValue(Nucleus::createConstantShort(x));
2171 }
2172
2173 UShort::UShort(RValue<UShort> rhs)
2174 {
2175 storeValue(rhs.value);
2176 }
2177
2178 UShort::UShort(const UShort &rhs)
2179 {
2180 Value *value = rhs.loadValue();
2181 storeValue(value);
2182 }
2183
2184 UShort::UShort(const Reference<UShort> &rhs)
2185 {
2186 Value *value = rhs.loadValue();
2187 storeValue(value);
2188 }
2189
Nicolas Capens96d4e092016-11-18 14:22:38 -05002190 RValue<UShort> UShort::operator=(RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002191 {
2192 storeValue(rhs.value);
2193
2194 return rhs;
2195 }
2196
Nicolas Capens96d4e092016-11-18 14:22:38 -05002197 RValue<UShort> UShort::operator=(const UShort &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002198 {
2199 Value *value = rhs.loadValue();
2200 storeValue(value);
2201
2202 return RValue<UShort>(value);
2203 }
2204
Nicolas Capens96d4e092016-11-18 14:22:38 -05002205 RValue<UShort> UShort::operator=(const Reference<UShort> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002206 {
2207 Value *value = rhs.loadValue();
2208 storeValue(value);
2209
2210 return RValue<UShort>(value);
2211 }
2212
2213 RValue<UShort> operator+(RValue<UShort> lhs, RValue<UShort> rhs)
2214 {
2215 return RValue<UShort>(Nucleus::createAdd(lhs.value, rhs.value));
2216 }
2217
2218 RValue<UShort> operator-(RValue<UShort> lhs, RValue<UShort> rhs)
2219 {
2220 return RValue<UShort>(Nucleus::createSub(lhs.value, rhs.value));
2221 }
2222
2223 RValue<UShort> operator*(RValue<UShort> lhs, RValue<UShort> rhs)
2224 {
2225 return RValue<UShort>(Nucleus::createMul(lhs.value, rhs.value));
2226 }
2227
2228 RValue<UShort> operator/(RValue<UShort> lhs, RValue<UShort> rhs)
2229 {
2230 return RValue<UShort>(Nucleus::createUDiv(lhs.value, rhs.value));
2231 }
2232
2233 RValue<UShort> operator%(RValue<UShort> lhs, RValue<UShort> rhs)
2234 {
2235 return RValue<UShort>(Nucleus::createURem(lhs.value, rhs.value));
2236 }
2237
2238 RValue<UShort> operator&(RValue<UShort> lhs, RValue<UShort> rhs)
2239 {
2240 return RValue<UShort>(Nucleus::createAnd(lhs.value, rhs.value));
2241 }
2242
2243 RValue<UShort> operator|(RValue<UShort> lhs, RValue<UShort> rhs)
2244 {
2245 return RValue<UShort>(Nucleus::createOr(lhs.value, rhs.value));
2246 }
2247
2248 RValue<UShort> operator^(RValue<UShort> lhs, RValue<UShort> rhs)
2249 {
2250 return RValue<UShort>(Nucleus::createXor(lhs.value, rhs.value));
2251 }
2252
2253 RValue<UShort> operator<<(RValue<UShort> lhs, RValue<UShort> rhs)
2254 {
2255 return RValue<UShort>(Nucleus::createShl(lhs.value, rhs.value));
2256 }
2257
2258 RValue<UShort> operator>>(RValue<UShort> lhs, RValue<UShort> rhs)
2259 {
2260 return RValue<UShort>(Nucleus::createLShr(lhs.value, rhs.value));
2261 }
2262
Nicolas Capens96d4e092016-11-18 14:22:38 -05002263 RValue<UShort> operator+=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002264 {
2265 return lhs = lhs + rhs;
2266 }
2267
Nicolas Capens96d4e092016-11-18 14:22:38 -05002268 RValue<UShort> operator-=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002269 {
2270 return lhs = lhs - rhs;
2271 }
2272
Nicolas Capens96d4e092016-11-18 14:22:38 -05002273 RValue<UShort> operator*=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002274 {
2275 return lhs = lhs * rhs;
2276 }
2277
Nicolas Capens96d4e092016-11-18 14:22:38 -05002278 RValue<UShort> operator/=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002279 {
2280 return lhs = lhs / rhs;
2281 }
2282
Nicolas Capens96d4e092016-11-18 14:22:38 -05002283 RValue<UShort> operator%=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002284 {
2285 return lhs = lhs % rhs;
2286 }
2287
Nicolas Capens96d4e092016-11-18 14:22:38 -05002288 RValue<UShort> operator&=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002289 {
2290 return lhs = lhs & rhs;
2291 }
2292
Nicolas Capens96d4e092016-11-18 14:22:38 -05002293 RValue<UShort> operator|=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002294 {
2295 return lhs = lhs | rhs;
2296 }
2297
Nicolas Capens96d4e092016-11-18 14:22:38 -05002298 RValue<UShort> operator^=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002299 {
2300 return lhs = lhs ^ rhs;
2301 }
2302
Nicolas Capens96d4e092016-11-18 14:22:38 -05002303 RValue<UShort> operator<<=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002304 {
2305 return lhs = lhs << rhs;
2306 }
2307
Nicolas Capens96d4e092016-11-18 14:22:38 -05002308 RValue<UShort> operator>>=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002309 {
2310 return lhs = lhs >> rhs;
2311 }
2312
2313 RValue<UShort> operator+(RValue<UShort> val)
2314 {
2315 return val;
2316 }
2317
2318 RValue<UShort> operator-(RValue<UShort> val)
2319 {
2320 return RValue<UShort>(Nucleus::createNeg(val.value));
2321 }
2322
2323 RValue<UShort> operator~(RValue<UShort> val)
2324 {
2325 return RValue<UShort>(Nucleus::createNot(val.value));
2326 }
2327
Nicolas Capens96d4e092016-11-18 14:22:38 -05002328 RValue<UShort> operator++(UShort &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002329 {
2330 RValue<UShort> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002331 val += UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002332 return res;
2333 }
2334
Nicolas Capens96d4e092016-11-18 14:22:38 -05002335 const UShort &operator++(UShort &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002336 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002337 val += UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002338 return val;
2339 }
2340
Nicolas Capens96d4e092016-11-18 14:22:38 -05002341 RValue<UShort> operator--(UShort &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002342 {
2343 RValue<UShort> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002344 val -= UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002345 return res;
2346 }
2347
Nicolas Capens96d4e092016-11-18 14:22:38 -05002348 const UShort &operator--(UShort &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002349 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002350 val -= UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002351 return val;
2352 }
2353
2354 RValue<Bool> operator<(RValue<UShort> lhs, RValue<UShort> rhs)
2355 {
2356 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
2357 }
2358
2359 RValue<Bool> operator<=(RValue<UShort> lhs, RValue<UShort> rhs)
2360 {
2361 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
2362 }
2363
2364 RValue<Bool> operator>(RValue<UShort> lhs, RValue<UShort> rhs)
2365 {
2366 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
2367 }
2368
2369 RValue<Bool> operator>=(RValue<UShort> lhs, RValue<UShort> rhs)
2370 {
2371 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
2372 }
2373
2374 RValue<Bool> operator!=(RValue<UShort> lhs, RValue<UShort> rhs)
2375 {
2376 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
2377 }
2378
2379 RValue<Bool> operator==(RValue<UShort> lhs, RValue<UShort> rhs)
2380 {
2381 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
2382 }
2383
2384 Type *UShort::getType()
2385 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002386 return T(Ice::IceType_i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002387 }
2388
Nicolas Capens16b5f152016-10-13 13:39:01 -04002389 Byte4::Byte4(RValue<Byte8> cast)
2390 {
Nicolas Capens16b5f152016-10-13 13:39:01 -04002391 storeValue(Nucleus::createBitCast(cast.value, getType()));
2392 }
2393
2394 Byte4::Byte4(const Reference<Byte4> &rhs)
2395 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002396 Value *value = rhs.loadValue();
2397 storeValue(value);
Nicolas Capens16b5f152016-10-13 13:39:01 -04002398 }
2399
Nicolas Capens598f8d82016-09-26 15:09:10 -04002400 Type *Byte4::getType()
2401 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002402 return T(Type_v4i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002403 }
2404
2405 Type *SByte4::getType()
2406 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002407 return T(Type_v4i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002408 }
2409
Nicolas Capens598f8d82016-09-26 15:09:10 -04002410 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)
2411 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04002412 int64_t constantVector[8] = {x0, x1, x2, x3, x4, x5, x6, x7};
2413 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002414 }
2415
2416 Byte8::Byte8(RValue<Byte8> rhs)
2417 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002418 storeValue(rhs.value);
2419 }
2420
2421 Byte8::Byte8(const Byte8 &rhs)
2422 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002423 Value *value = rhs.loadValue();
2424 storeValue(value);
2425 }
2426
2427 Byte8::Byte8(const Reference<Byte8> &rhs)
2428 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002429 Value *value = rhs.loadValue();
2430 storeValue(value);
2431 }
2432
Nicolas Capens96d4e092016-11-18 14:22:38 -05002433 RValue<Byte8> Byte8::operator=(RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002434 {
2435 storeValue(rhs.value);
2436
2437 return rhs;
2438 }
2439
Nicolas Capens96d4e092016-11-18 14:22:38 -05002440 RValue<Byte8> Byte8::operator=(const Byte8 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002441 {
2442 Value *value = rhs.loadValue();
2443 storeValue(value);
2444
2445 return RValue<Byte8>(value);
2446 }
2447
Nicolas Capens96d4e092016-11-18 14:22:38 -05002448 RValue<Byte8> Byte8::operator=(const Reference<Byte8> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002449 {
2450 Value *value = rhs.loadValue();
2451 storeValue(value);
2452
2453 return RValue<Byte8>(value);
2454 }
2455
2456 RValue<Byte8> operator+(RValue<Byte8> lhs, RValue<Byte8> rhs)
2457 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002458 return RValue<Byte8>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002459 }
2460
2461 RValue<Byte8> operator-(RValue<Byte8> lhs, RValue<Byte8> rhs)
2462 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002463 return RValue<Byte8>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002464 }
2465
2466// RValue<Byte8> operator*(RValue<Byte8> lhs, RValue<Byte8> rhs)
2467// {
2468// return RValue<Byte8>(Nucleus::createMul(lhs.value, rhs.value));
2469// }
2470
2471// RValue<Byte8> operator/(RValue<Byte8> lhs, RValue<Byte8> rhs)
2472// {
2473// return RValue<Byte8>(Nucleus::createUDiv(lhs.value, rhs.value));
2474// }
2475
2476// RValue<Byte8> operator%(RValue<Byte8> lhs, RValue<Byte8> rhs)
2477// {
2478// return RValue<Byte8>(Nucleus::createURem(lhs.value, rhs.value));
2479// }
2480
2481 RValue<Byte8> operator&(RValue<Byte8> lhs, RValue<Byte8> rhs)
2482 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002483 return RValue<Byte8>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002484 }
2485
2486 RValue<Byte8> operator|(RValue<Byte8> lhs, RValue<Byte8> rhs)
2487 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002488 return RValue<Byte8>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002489 }
2490
2491 RValue<Byte8> operator^(RValue<Byte8> lhs, RValue<Byte8> rhs)
2492 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002493 return RValue<Byte8>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002494 }
2495
2496// RValue<Byte8> operator<<(RValue<Byte8> lhs, unsigned char rhs)
2497// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002498// return RValue<Byte8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002499// }
2500
2501// RValue<Byte8> operator>>(RValue<Byte8> lhs, unsigned char rhs)
2502// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002503// return RValue<Byte8>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002504// }
2505
Nicolas Capens96d4e092016-11-18 14:22:38 -05002506 RValue<Byte8> operator+=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002507 {
2508 return lhs = lhs + rhs;
2509 }
2510
Nicolas Capens96d4e092016-11-18 14:22:38 -05002511 RValue<Byte8> operator-=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002512 {
2513 return lhs = lhs - rhs;
2514 }
2515
Nicolas Capens96d4e092016-11-18 14:22:38 -05002516// RValue<Byte8> operator*=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002517// {
2518// return lhs = lhs * rhs;
2519// }
2520
Nicolas Capens96d4e092016-11-18 14:22:38 -05002521// RValue<Byte8> operator/=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002522// {
2523// return lhs = lhs / rhs;
2524// }
2525
Nicolas Capens96d4e092016-11-18 14:22:38 -05002526// RValue<Byte8> operator%=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002527// {
2528// return lhs = lhs % rhs;
2529// }
2530
Nicolas Capens96d4e092016-11-18 14:22:38 -05002531 RValue<Byte8> operator&=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002532 {
2533 return lhs = lhs & rhs;
2534 }
2535
Nicolas Capens96d4e092016-11-18 14:22:38 -05002536 RValue<Byte8> operator|=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002537 {
2538 return lhs = lhs | rhs;
2539 }
2540
Nicolas Capens96d4e092016-11-18 14:22:38 -05002541 RValue<Byte8> operator^=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002542 {
2543 return lhs = lhs ^ rhs;
2544 }
2545
Nicolas Capens96d4e092016-11-18 14:22:38 -05002546// RValue<Byte8> operator<<=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002547// {
2548// return lhs = lhs << rhs;
2549// }
2550
Nicolas Capens96d4e092016-11-18 14:22:38 -05002551// RValue<Byte8> operator>>=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002552// {
2553// return lhs = lhs >> rhs;
2554// }
2555
2556// RValue<Byte8> operator+(RValue<Byte8> val)
2557// {
2558// return val;
2559// }
2560
2561// RValue<Byte8> operator-(RValue<Byte8> val)
2562// {
2563// return RValue<Byte8>(Nucleus::createNeg(val.value));
2564// }
2565
2566 RValue<Byte8> operator~(RValue<Byte8> val)
2567 {
2568 return RValue<Byte8>(Nucleus::createNot(val.value));
2569 }
2570
2571 RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y)
2572 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002573 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2574 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2575 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2576 auto paddusb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2577 paddusb->addArg(x.value);
2578 paddusb->addArg(y.value);
2579 ::basicBlock->appendInst(paddusb);
2580
2581 return RValue<Byte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002582 }
2583
2584 RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y)
2585 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002586 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2587 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2588 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2589 auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2590 psubusw->addArg(x.value);
2591 psubusw->addArg(y.value);
2592 ::basicBlock->appendInst(psubusw);
2593
2594 return RValue<Byte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002595 }
2596
2597 RValue<Short4> Unpack(RValue<Byte4> x)
2598 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04002599 int shuffle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}; // Real type is v16i8
2600 return RValue<Short4>(Nucleus::createShuffleVector(x.value, x.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002601 }
2602
Nicolas Capens411273e2017-01-26 15:13:36 -08002603 RValue<Short4> Unpack(RValue<Byte4> x, RValue<Byte4> y)
2604 {
2605 return UnpackLow(As<Byte8>(x), As<Byte8>(y));
2606 }
2607
Nicolas Capens598f8d82016-09-26 15:09:10 -04002608 RValue<Short4> UnpackLow(RValue<Byte8> x, RValue<Byte8> y)
2609 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04002610 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
2611 return RValue<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002612 }
2613
2614 RValue<Short4> UnpackHigh(RValue<Byte8> x, RValue<Byte8> y)
2615 {
Nicolas Capens20e22c42016-10-25 17:32:37 -04002616 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
2617 auto lowHigh = RValue<Byte16>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
2618 return As<Short4>(Swizzle(As<Int4>(lowHigh), 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002619 }
2620
2621 RValue<Int> SignMask(RValue<Byte8> x)
2622 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002623 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
2624 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2625 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2626 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
2627 movmsk->addArg(x.value);
2628 ::basicBlock->appendInst(movmsk);
2629
2630 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002631 }
2632
2633// RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y)
2634// {
Nicolas Capens2f970b62016-11-08 14:28:59 -05002635// return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002636// }
2637
2638 RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y)
2639 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002640 return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002641 }
2642
2643 Type *Byte8::getType()
2644 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002645 return T(Type_v8i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002646 }
2647
Nicolas Capens598f8d82016-09-26 15:09:10 -04002648 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)
2649 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04002650 int64_t constantVector[8] = { x0, x1, x2, x3, x4, x5, x6, x7 };
2651 Value *vector = V(Nucleus::createConstantVector(constantVector, getType()));
2652
2653 storeValue(Nucleus::createBitCast(vector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002654 }
2655
Nicolas Capens598f8d82016-09-26 15:09:10 -04002656 SByte8::SByte8(RValue<SByte8> rhs)
2657 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002658 storeValue(rhs.value);
2659 }
2660
2661 SByte8::SByte8(const SByte8 &rhs)
2662 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002663 Value *value = rhs.loadValue();
2664 storeValue(value);
2665 }
2666
2667 SByte8::SByte8(const Reference<SByte8> &rhs)
2668 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002669 Value *value = rhs.loadValue();
2670 storeValue(value);
2671 }
2672
Nicolas Capens96d4e092016-11-18 14:22:38 -05002673 RValue<SByte8> SByte8::operator=(RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002674 {
2675 storeValue(rhs.value);
2676
2677 return rhs;
2678 }
2679
Nicolas Capens96d4e092016-11-18 14:22:38 -05002680 RValue<SByte8> SByte8::operator=(const SByte8 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002681 {
2682 Value *value = rhs.loadValue();
2683 storeValue(value);
2684
2685 return RValue<SByte8>(value);
2686 }
2687
Nicolas Capens96d4e092016-11-18 14:22:38 -05002688 RValue<SByte8> SByte8::operator=(const Reference<SByte8> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002689 {
2690 Value *value = rhs.loadValue();
2691 storeValue(value);
2692
2693 return RValue<SByte8>(value);
2694 }
2695
2696 RValue<SByte8> operator+(RValue<SByte8> lhs, RValue<SByte8> rhs)
2697 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002698 return RValue<SByte8>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002699 }
2700
2701 RValue<SByte8> operator-(RValue<SByte8> lhs, RValue<SByte8> rhs)
2702 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002703 return RValue<SByte8>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002704 }
2705
2706// RValue<SByte8> operator*(RValue<SByte8> lhs, RValue<SByte8> rhs)
2707// {
2708// return RValue<SByte8>(Nucleus::createMul(lhs.value, rhs.value));
2709// }
2710
2711// RValue<SByte8> operator/(RValue<SByte8> lhs, RValue<SByte8> rhs)
2712// {
2713// return RValue<SByte8>(Nucleus::createSDiv(lhs.value, rhs.value));
2714// }
2715
2716// RValue<SByte8> operator%(RValue<SByte8> lhs, RValue<SByte8> rhs)
2717// {
2718// return RValue<SByte8>(Nucleus::createSRem(lhs.value, rhs.value));
2719// }
2720
2721 RValue<SByte8> operator&(RValue<SByte8> lhs, RValue<SByte8> rhs)
2722 {
2723 return RValue<SByte8>(Nucleus::createAnd(lhs.value, rhs.value));
2724 }
2725
2726 RValue<SByte8> operator|(RValue<SByte8> lhs, RValue<SByte8> rhs)
2727 {
2728 return RValue<SByte8>(Nucleus::createOr(lhs.value, rhs.value));
2729 }
2730
2731 RValue<SByte8> operator^(RValue<SByte8> lhs, RValue<SByte8> rhs)
2732 {
2733 return RValue<SByte8>(Nucleus::createXor(lhs.value, rhs.value));
2734 }
2735
2736// RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs)
2737// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002738// return RValue<SByte8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002739// }
2740
2741// RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs)
2742// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002743// return RValue<SByte8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002744// }
2745
Nicolas Capens96d4e092016-11-18 14:22:38 -05002746 RValue<SByte8> operator+=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002747 {
2748 return lhs = lhs + rhs;
2749 }
2750
Nicolas Capens96d4e092016-11-18 14:22:38 -05002751 RValue<SByte8> operator-=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002752 {
2753 return lhs = lhs - rhs;
2754 }
2755
Nicolas Capens96d4e092016-11-18 14:22:38 -05002756// RValue<SByte8> operator*=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002757// {
2758// return lhs = lhs * rhs;
2759// }
2760
Nicolas Capens96d4e092016-11-18 14:22:38 -05002761// RValue<SByte8> operator/=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002762// {
2763// return lhs = lhs / rhs;
2764// }
2765
Nicolas Capens96d4e092016-11-18 14:22:38 -05002766// RValue<SByte8> operator%=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002767// {
2768// return lhs = lhs % rhs;
2769// }
2770
Nicolas Capens96d4e092016-11-18 14:22:38 -05002771 RValue<SByte8> operator&=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002772 {
2773 return lhs = lhs & rhs;
2774 }
2775
Nicolas Capens96d4e092016-11-18 14:22:38 -05002776 RValue<SByte8> operator|=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002777 {
2778 return lhs = lhs | rhs;
2779 }
2780
Nicolas Capens96d4e092016-11-18 14:22:38 -05002781 RValue<SByte8> operator^=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002782 {
2783 return lhs = lhs ^ rhs;
2784 }
2785
Nicolas Capens96d4e092016-11-18 14:22:38 -05002786// RValue<SByte8> operator<<=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002787// {
2788// return lhs = lhs << rhs;
2789// }
2790
Nicolas Capens96d4e092016-11-18 14:22:38 -05002791// RValue<SByte8> operator>>=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002792// {
2793// return lhs = lhs >> rhs;
2794// }
2795
2796// RValue<SByte8> operator+(RValue<SByte8> val)
2797// {
2798// return val;
2799// }
2800
2801// RValue<SByte8> operator-(RValue<SByte8> val)
2802// {
2803// return RValue<SByte8>(Nucleus::createNeg(val.value));
2804// }
2805
2806 RValue<SByte8> operator~(RValue<SByte8> val)
2807 {
2808 return RValue<SByte8>(Nucleus::createNot(val.value));
2809 }
2810
2811 RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y)
2812 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002813 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2814 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2815 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2816 auto paddsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2817 paddsb->addArg(x.value);
2818 paddsb->addArg(y.value);
2819 ::basicBlock->appendInst(paddsb);
2820
2821 return RValue<SByte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002822 }
2823
2824 RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y)
2825 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002826 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2827 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2828 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2829 auto psubsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2830 psubsb->addArg(x.value);
2831 psubsb->addArg(y.value);
2832 ::basicBlock->appendInst(psubsb);
2833
2834 return RValue<SByte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002835 }
2836
2837 RValue<Short4> UnpackLow(RValue<SByte8> x, RValue<SByte8> y)
2838 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04002839 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
2840 return RValue<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002841 }
2842
2843 RValue<Short4> UnpackHigh(RValue<SByte8> x, RValue<SByte8> y)
2844 {
Nicolas Capens20e22c42016-10-25 17:32:37 -04002845 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
2846 auto lowHigh = RValue<Byte16>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
2847 return As<Short4>(Swizzle(As<Int4>(lowHigh), 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002848 }
2849
2850 RValue<Int> SignMask(RValue<SByte8> x)
2851 {
Nicolas Capensf2cb9df2016-10-21 17:26:13 -04002852 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
2853 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2854 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2855 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
2856 movmsk->addArg(x.value);
2857 ::basicBlock->appendInst(movmsk);
2858
2859 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002860 }
2861
2862 RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y)
2863 {
Nicolas Capens2f970b62016-11-08 14:28:59 -05002864 return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002865 }
2866
2867 RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y)
2868 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002869 return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002870 }
2871
2872 Type *SByte8::getType()
2873 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002874 return T(Type_v8i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002875 }
2876
2877 Byte16::Byte16(RValue<Byte16> rhs)
2878 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002879 storeValue(rhs.value);
2880 }
2881
2882 Byte16::Byte16(const Byte16 &rhs)
2883 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002884 Value *value = rhs.loadValue();
2885 storeValue(value);
2886 }
2887
2888 Byte16::Byte16(const Reference<Byte16> &rhs)
2889 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002890 Value *value = rhs.loadValue();
2891 storeValue(value);
2892 }
2893
Nicolas Capens96d4e092016-11-18 14:22:38 -05002894 RValue<Byte16> Byte16::operator=(RValue<Byte16> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002895 {
2896 storeValue(rhs.value);
2897
2898 return rhs;
2899 }
2900
Nicolas Capens96d4e092016-11-18 14:22:38 -05002901 RValue<Byte16> Byte16::operator=(const Byte16 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002902 {
2903 Value *value = rhs.loadValue();
2904 storeValue(value);
2905
2906 return RValue<Byte16>(value);
2907 }
2908
Nicolas Capens96d4e092016-11-18 14:22:38 -05002909 RValue<Byte16> Byte16::operator=(const Reference<Byte16> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002910 {
2911 Value *value = rhs.loadValue();
2912 storeValue(value);
2913
2914 return RValue<Byte16>(value);
2915 }
2916
2917 Type *Byte16::getType()
2918 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002919 return T(Ice::IceType_v16i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002920 }
2921
2922 Type *SByte16::getType()
2923 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002924 return T(Ice::IceType_v16i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002925 }
2926
Nicolas Capens16b5f152016-10-13 13:39:01 -04002927 Short2::Short2(RValue<Short4> cast)
2928 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05002929 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens16b5f152016-10-13 13:39:01 -04002930 }
2931
2932 Type *Short2::getType()
2933 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002934 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04002935 }
2936
2937 UShort2::UShort2(RValue<UShort4> cast)
2938 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05002939 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens16b5f152016-10-13 13:39:01 -04002940 }
2941
2942 Type *UShort2::getType()
2943 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002944 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04002945 }
2946
Nicolas Capens598f8d82016-09-26 15:09:10 -04002947 Short4::Short4(RValue<Int> cast)
2948 {
Nicolas Capensd4227962016-11-09 14:24:25 -05002949 Value *vector = loadValue();
Nicolas Capensbf22bbf2017-01-13 17:37:45 -05002950 Value *element = Nucleus::createTrunc(cast.value, Short::getType());
2951 Value *insert = Nucleus::createInsertElement(vector, element, 0);
Nicolas Capensd4227962016-11-09 14:24:25 -05002952 Value *swizzle = Swizzle(RValue<Short4>(insert), 0x00).value;
Nicolas Capens598f8d82016-09-26 15:09:10 -04002953
2954 storeValue(swizzle);
2955 }
2956
2957 Short4::Short4(RValue<Int4> cast)
2958 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08002959 int select[8] = {0, 2, 4, 6, 0, 2, 4, 6};
2960 Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType());
2961 Value *packed = Nucleus::createShuffleVector(short8, short8, select);
Nicolas Capensd4227962016-11-09 14:24:25 -05002962
2963 Value *int2 = RValue<Int2>(Int2(RValue<Int4>(packed))).value;
2964 Value *short4 = Nucleus::createBitCast(int2, Short4::getType());
2965
2966 storeValue(short4);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002967 }
2968
2969// Short4::Short4(RValue<Float> cast)
2970// {
2971// }
2972
2973 Short4::Short4(RValue<Float4> cast)
2974 {
2975 assert(false && "UNIMPLEMENTED");
2976 }
2977
Nicolas Capens598f8d82016-09-26 15:09:10 -04002978 Short4::Short4(short xyzw)
2979 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04002980 int64_t constantVector[4] = {xyzw, xyzw, xyzw, xyzw};
2981 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002982 }
2983
2984 Short4::Short4(short x, short y, short z, short w)
2985 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04002986 int64_t constantVector[4] = {x, y, z, w};
2987 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002988 }
2989
2990 Short4::Short4(RValue<Short4> rhs)
2991 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002992 storeValue(rhs.value);
2993 }
2994
2995 Short4::Short4(const Short4 &rhs)
2996 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002997 Value *value = rhs.loadValue();
2998 storeValue(value);
2999 }
3000
3001 Short4::Short4(const Reference<Short4> &rhs)
3002 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003003 Value *value = rhs.loadValue();
3004 storeValue(value);
3005 }
3006
3007 Short4::Short4(RValue<UShort4> rhs)
3008 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003009 storeValue(rhs.value);
3010 }
3011
3012 Short4::Short4(const UShort4 &rhs)
3013 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003014 storeValue(rhs.loadValue());
3015 }
3016
3017 Short4::Short4(const Reference<UShort4> &rhs)
3018 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003019 storeValue(rhs.loadValue());
3020 }
3021
Nicolas Capens96d4e092016-11-18 14:22:38 -05003022 RValue<Short4> Short4::operator=(RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003023 {
3024 storeValue(rhs.value);
3025
3026 return rhs;
3027 }
3028
Nicolas Capens96d4e092016-11-18 14:22:38 -05003029 RValue<Short4> Short4::operator=(const Short4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003030 {
3031 Value *value = rhs.loadValue();
3032 storeValue(value);
3033
3034 return RValue<Short4>(value);
3035 }
3036
Nicolas Capens96d4e092016-11-18 14:22:38 -05003037 RValue<Short4> Short4::operator=(const Reference<Short4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003038 {
3039 Value *value = rhs.loadValue();
3040 storeValue(value);
3041
3042 return RValue<Short4>(value);
3043 }
3044
Nicolas Capens96d4e092016-11-18 14:22:38 -05003045 RValue<Short4> Short4::operator=(RValue<UShort4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003046 {
3047 storeValue(rhs.value);
3048
3049 return RValue<Short4>(rhs);
3050 }
3051
Nicolas Capens96d4e092016-11-18 14:22:38 -05003052 RValue<Short4> Short4::operator=(const UShort4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003053 {
3054 Value *value = rhs.loadValue();
3055 storeValue(value);
3056
3057 return RValue<Short4>(value);
3058 }
3059
Nicolas Capens96d4e092016-11-18 14:22:38 -05003060 RValue<Short4> Short4::operator=(const Reference<UShort4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003061 {
3062 Value *value = rhs.loadValue();
3063 storeValue(value);
3064
3065 return RValue<Short4>(value);
3066 }
3067
3068 RValue<Short4> operator+(RValue<Short4> lhs, RValue<Short4> rhs)
3069 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003070 return RValue<Short4>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003071 }
3072
3073 RValue<Short4> operator-(RValue<Short4> lhs, RValue<Short4> rhs)
3074 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003075 return RValue<Short4>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003076 }
3077
3078 RValue<Short4> operator*(RValue<Short4> lhs, RValue<Short4> rhs)
3079 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003080 return RValue<Short4>(Nucleus::createMul(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003081 }
3082
3083// RValue<Short4> operator/(RValue<Short4> lhs, RValue<Short4> rhs)
3084// {
3085// return RValue<Short4>(Nucleus::createSDiv(lhs.value, rhs.value));
3086// }
3087
3088// RValue<Short4> operator%(RValue<Short4> lhs, RValue<Short4> rhs)
3089// {
3090// return RValue<Short4>(Nucleus::createSRem(lhs.value, rhs.value));
3091// }
3092
3093 RValue<Short4> operator&(RValue<Short4> lhs, RValue<Short4> rhs)
3094 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003095 return RValue<Short4>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003096 }
3097
3098 RValue<Short4> operator|(RValue<Short4> lhs, RValue<Short4> rhs)
3099 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003100 return RValue<Short4>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003101 }
3102
3103 RValue<Short4> operator^(RValue<Short4> lhs, RValue<Short4> rhs)
3104 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003105 return RValue<Short4>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003106 }
3107
3108 RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs)
3109 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003110 return RValue<Short4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003111 }
3112
3113 RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs)
3114 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003115 return RValue<Short4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003116 }
3117
Nicolas Capens96d4e092016-11-18 14:22:38 -05003118 RValue<Short4> operator+=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003119 {
3120 return lhs = lhs + rhs;
3121 }
3122
Nicolas Capens96d4e092016-11-18 14:22:38 -05003123 RValue<Short4> operator-=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003124 {
3125 return lhs = lhs - rhs;
3126 }
3127
Nicolas Capens96d4e092016-11-18 14:22:38 -05003128 RValue<Short4> operator*=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003129 {
3130 return lhs = lhs * rhs;
3131 }
3132
Nicolas Capens96d4e092016-11-18 14:22:38 -05003133// RValue<Short4> operator/=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003134// {
3135// return lhs = lhs / rhs;
3136// }
3137
Nicolas Capens96d4e092016-11-18 14:22:38 -05003138// RValue<Short4> operator%=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003139// {
3140// return lhs = lhs % rhs;
3141// }
3142
Nicolas Capens96d4e092016-11-18 14:22:38 -05003143 RValue<Short4> operator&=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003144 {
3145 return lhs = lhs & rhs;
3146 }
3147
Nicolas Capens96d4e092016-11-18 14:22:38 -05003148 RValue<Short4> operator|=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003149 {
3150 return lhs = lhs | rhs;
3151 }
3152
Nicolas Capens96d4e092016-11-18 14:22:38 -05003153 RValue<Short4> operator^=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003154 {
3155 return lhs = lhs ^ rhs;
3156 }
3157
Nicolas Capens96d4e092016-11-18 14:22:38 -05003158 RValue<Short4> operator<<=(Short4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003159 {
3160 return lhs = lhs << rhs;
3161 }
3162
Nicolas Capens96d4e092016-11-18 14:22:38 -05003163 RValue<Short4> operator>>=(Short4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003164 {
3165 return lhs = lhs >> rhs;
3166 }
3167
Nicolas Capens598f8d82016-09-26 15:09:10 -04003168// RValue<Short4> operator+(RValue<Short4> val)
3169// {
3170// return val;
3171// }
3172
3173 RValue<Short4> operator-(RValue<Short4> val)
3174 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05003175 return RValue<Short4>(Nucleus::createNeg(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003176 }
3177
3178 RValue<Short4> operator~(RValue<Short4> val)
3179 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05003180 return RValue<Short4>(Nucleus::createNot(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003181 }
3182
3183 RValue<Short4> RoundShort4(RValue<Float4> cast)
3184 {
Nicolas Capensd4227962016-11-09 14:24:25 -05003185 RValue<Int4> int4 = RoundInt(cast);
3186 return As<Short4>(Pack(int4, int4));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003187 }
3188
3189 RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y)
3190 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003191 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3192 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value, y.value);
3193 ::basicBlock->appendInst(cmp);
3194
3195 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3196 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3197 ::basicBlock->appendInst(select);
3198
3199 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003200 }
3201
3202 RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y)
3203 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003204 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3205 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value);
3206 ::basicBlock->appendInst(cmp);
3207
3208 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3209 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3210 ::basicBlock->appendInst(select);
3211
3212 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003213 }
3214
3215 RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y)
3216 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003217 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3218 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3219 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3220 auto paddsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3221 paddsw->addArg(x.value);
3222 paddsw->addArg(y.value);
3223 ::basicBlock->appendInst(paddsw);
3224
3225 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003226 }
3227
3228 RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y)
3229 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003230 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3231 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3232 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3233 auto psubsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3234 psubsw->addArg(x.value);
3235 psubsw->addArg(y.value);
3236 ::basicBlock->appendInst(psubsw);
3237
3238 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003239 }
3240
3241 RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y)
3242 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003243 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3244 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3245 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3246 auto pmulhw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3247 pmulhw->addArg(x.value);
3248 pmulhw->addArg(y.value);
3249 ::basicBlock->appendInst(pmulhw);
3250
Nicolas Capens5b41ba32016-12-08 14:34:00 -05003251 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003252 }
3253
3254 RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y)
3255 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003256 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3257 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyAddPairs, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3258 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3259 auto pmaddwd = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3260 pmaddwd->addArg(x.value);
3261 pmaddwd->addArg(y.value);
3262 ::basicBlock->appendInst(pmaddwd);
3263
3264 return RValue<Int2>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003265 }
3266
3267 RValue<SByte8> Pack(RValue<Short4> x, RValue<Short4> y)
3268 {
Nicolas Capensec54a172016-10-25 17:32:37 -04003269 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
3270 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3271 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3272 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3273 pack->addArg(x.value);
3274 pack->addArg(y.value);
3275 ::basicBlock->appendInst(pack);
3276
Nicolas Capens70dfff42016-10-27 10:20:28 -04003277 return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x88));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003278 }
3279
3280 RValue<Int2> UnpackLow(RValue<Short4> x, RValue<Short4> y)
3281 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04003282 int shuffle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; // Real type is v8i16
3283 return RValue<Int2>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003284 }
3285
3286 RValue<Int2> UnpackHigh(RValue<Short4> x, RValue<Short4> y)
3287 {
Nicolas Capens20e22c42016-10-25 17:32:37 -04003288 int shuffle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; // Real type is v8i16
3289 auto lowHigh = RValue<Short8>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
3290 return As<Int2>(Swizzle(As<Int4>(lowHigh), 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003291 }
3292
3293 RValue<Short4> Swizzle(RValue<Short4> x, unsigned char select)
3294 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04003295 // Real type is v8i16
3296 int shuffle[8] =
3297 {
3298 (select >> 0) & 0x03,
3299 (select >> 2) & 0x03,
3300 (select >> 4) & 0x03,
3301 (select >> 6) & 0x03,
3302 (select >> 0) & 0x03,
3303 (select >> 2) & 0x03,
3304 (select >> 4) & 0x03,
3305 (select >> 6) & 0x03,
3306 };
3307
3308 return RValue<Short4>(Nucleus::createShuffleVector(x.value, x.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003309 }
3310
3311 RValue<Short4> Insert(RValue<Short4> val, RValue<Short> element, int i)
3312 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05003313 return RValue<Short4>(Nucleus::createInsertElement(val.value, element.value, i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003314 }
3315
3316 RValue<Short> Extract(RValue<Short4> val, int i)
3317 {
Nicolas Capens0133d0f2017-01-16 16:25:08 -05003318 return RValue<Short>(Nucleus::createExtractElement(val.value, Short::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003319 }
3320
3321 RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y)
3322 {
Nicolas Capens2f970b62016-11-08 14:28:59 -05003323 return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003324 }
3325
3326 RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y)
3327 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003328 return RValue<Short4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003329 }
3330
3331 Type *Short4::getType()
3332 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003333 return T(Type_v4i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003334 }
3335
3336 UShort4::UShort4(RValue<Int4> cast)
3337 {
3338 *this = Short4(cast);
3339 }
3340
3341 UShort4::UShort4(RValue<Float4> cast, bool saturate)
3342 {
Nicolas Capensd4227962016-11-09 14:24:25 -05003343 if(saturate)
3344 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003345 if(CPUID::SSE4_1)
Nicolas Capensd4227962016-11-09 14:24:25 -05003346 {
3347 Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation
3348 *this = As<Short4>(Pack(As<UInt4>(int4), As<UInt4>(int4)));
3349 }
3350 else
3351 {
3352 *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000))));
3353 }
3354 }
3355 else
3356 {
3357 *this = Short4(Int4(cast));
3358 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04003359 }
3360
Nicolas Capens598f8d82016-09-26 15:09:10 -04003361 UShort4::UShort4(unsigned short xyzw)
3362 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003363 int64_t constantVector[4] = {xyzw, xyzw, xyzw, xyzw};
3364 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003365 }
3366
3367 UShort4::UShort4(unsigned short x, unsigned short y, unsigned short z, unsigned short w)
3368 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003369 int64_t constantVector[4] = {x, y, z, w};
3370 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003371 }
3372
3373 UShort4::UShort4(RValue<UShort4> rhs)
3374 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003375 storeValue(rhs.value);
3376 }
3377
3378 UShort4::UShort4(const UShort4 &rhs)
3379 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003380 Value *value = rhs.loadValue();
3381 storeValue(value);
3382 }
3383
3384 UShort4::UShort4(const Reference<UShort4> &rhs)
3385 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003386 Value *value = rhs.loadValue();
3387 storeValue(value);
3388 }
3389
3390 UShort4::UShort4(RValue<Short4> rhs)
3391 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003392 storeValue(rhs.value);
3393 }
3394
3395 UShort4::UShort4(const Short4 &rhs)
3396 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003397 Value *value = rhs.loadValue();
3398 storeValue(value);
3399 }
3400
3401 UShort4::UShort4(const Reference<Short4> &rhs)
3402 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003403 Value *value = rhs.loadValue();
3404 storeValue(value);
3405 }
3406
Nicolas Capens96d4e092016-11-18 14:22:38 -05003407 RValue<UShort4> UShort4::operator=(RValue<UShort4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003408 {
3409 storeValue(rhs.value);
3410
3411 return rhs;
3412 }
3413
Nicolas Capens96d4e092016-11-18 14:22:38 -05003414 RValue<UShort4> UShort4::operator=(const UShort4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003415 {
3416 Value *value = rhs.loadValue();
3417 storeValue(value);
3418
3419 return RValue<UShort4>(value);
3420 }
3421
Nicolas Capens96d4e092016-11-18 14:22:38 -05003422 RValue<UShort4> UShort4::operator=(const Reference<UShort4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003423 {
3424 Value *value = rhs.loadValue();
3425 storeValue(value);
3426
3427 return RValue<UShort4>(value);
3428 }
3429
Nicolas Capens96d4e092016-11-18 14:22:38 -05003430 RValue<UShort4> UShort4::operator=(RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003431 {
3432 storeValue(rhs.value);
3433
3434 return RValue<UShort4>(rhs);
3435 }
3436
Nicolas Capens96d4e092016-11-18 14:22:38 -05003437 RValue<UShort4> UShort4::operator=(const Short4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003438 {
3439 Value *value = rhs.loadValue();
3440 storeValue(value);
3441
3442 return RValue<UShort4>(value);
3443 }
3444
Nicolas Capens96d4e092016-11-18 14:22:38 -05003445 RValue<UShort4> UShort4::operator=(const Reference<Short4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003446 {
3447 Value *value = rhs.loadValue();
3448 storeValue(value);
3449
3450 return RValue<UShort4>(value);
3451 }
3452
3453 RValue<UShort4> operator+(RValue<UShort4> lhs, RValue<UShort4> rhs)
3454 {
Nicolas Capens5b41ba32016-12-08 14:34:00 -05003455 return RValue<UShort4>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003456 }
3457
3458 RValue<UShort4> operator-(RValue<UShort4> lhs, RValue<UShort4> rhs)
3459 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003460 return RValue<UShort4>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003461 }
3462
3463 RValue<UShort4> operator*(RValue<UShort4> lhs, RValue<UShort4> rhs)
3464 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003465 return RValue<UShort4>(Nucleus::createMul(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003466 }
3467
Nicolas Capens16b5f152016-10-13 13:39:01 -04003468 RValue<UShort4> operator&(RValue<UShort4> lhs, RValue<UShort4> rhs)
3469 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003470 return RValue<UShort4>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003471 }
3472
3473 RValue<UShort4> operator|(RValue<UShort4> lhs, RValue<UShort4> rhs)
3474 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003475 return RValue<UShort4>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003476 }
3477
3478 RValue<UShort4> operator^(RValue<UShort4> lhs, RValue<UShort4> rhs)
3479 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003480 return RValue<UShort4>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003481 }
3482
Nicolas Capens598f8d82016-09-26 15:09:10 -04003483 RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs)
3484 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003485 return RValue<UShort4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003486 }
3487
3488 RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs)
3489 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003490 return RValue<UShort4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003491 }
3492
Nicolas Capens96d4e092016-11-18 14:22:38 -05003493 RValue<UShort4> operator<<=(UShort4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003494 {
3495 return lhs = lhs << rhs;
3496 }
3497
Nicolas Capens96d4e092016-11-18 14:22:38 -05003498 RValue<UShort4> operator>>=(UShort4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003499 {
3500 return lhs = lhs >> rhs;
3501 }
3502
Nicolas Capens598f8d82016-09-26 15:09:10 -04003503 RValue<UShort4> operator~(RValue<UShort4> val)
3504 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05003505 return RValue<UShort4>(Nucleus::createNot(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003506 }
3507
3508 RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y)
3509 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003510 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3511 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value, y.value);
3512 ::basicBlock->appendInst(cmp);
3513
3514 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3515 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3516 ::basicBlock->appendInst(select);
3517
3518 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003519 }
3520
3521 RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y)
3522 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003523 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3524 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value);
3525 ::basicBlock->appendInst(cmp);
3526
3527 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3528 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3529 ::basicBlock->appendInst(select);
3530
3531 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003532 }
3533
3534 RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y)
3535 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003536 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3537 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3538 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3539 auto paddusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3540 paddusw->addArg(x.value);
3541 paddusw->addArg(y.value);
3542 ::basicBlock->appendInst(paddusw);
3543
3544 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003545 }
3546
3547 RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y)
3548 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003549 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3550 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3551 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3552 auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3553 psubusw->addArg(x.value);
3554 psubusw->addArg(y.value);
3555 ::basicBlock->appendInst(psubusw);
3556
3557 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003558 }
3559
3560 RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y)
3561 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003562 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3563 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3564 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3565 auto pmulhuw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3566 pmulhuw->addArg(x.value);
3567 pmulhuw->addArg(y.value);
3568 ::basicBlock->appendInst(pmulhuw);
3569
3570 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003571 }
3572
3573 RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)
3574 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003575 assert(false && "UNIMPLEMENTED"); return RValue<UShort4>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003576 }
3577
3578 RValue<Byte8> Pack(RValue<UShort4> x, RValue<UShort4> y)
3579 {
Nicolas Capensec54a172016-10-25 17:32:37 -04003580 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
3581 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3582 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3583 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3584 pack->addArg(x.value);
3585 pack->addArg(y.value);
3586 ::basicBlock->appendInst(pack);
3587
Nicolas Capens70dfff42016-10-27 10:20:28 -04003588 return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x88));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003589 }
3590
3591 Type *UShort4::getType()
3592 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003593 return T(Type_v4i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003594 }
3595
Nicolas Capens3e7062b2017-01-17 14:01:33 -05003596 Short8::Short8(short c)
3597 {
3598 int64_t constantVector[8] = {c, c, c, c, c, c, c, c};
3599 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3600 }
3601
Nicolas Capens598f8d82016-09-26 15:09:10 -04003602 Short8::Short8(short c0, short c1, short c2, short c3, short c4, short c5, short c6, short c7)
3603 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003604 int64_t constantVector[8] = {c0, c1, c2, c3, c4, c5, c6, c7};
3605 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003606 }
3607
3608 Short8::Short8(RValue<Short8> rhs)
3609 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003610 storeValue(rhs.value);
3611 }
3612
3613 Short8::Short8(const Reference<Short8> &rhs)
3614 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003615 Value *value = rhs.loadValue();
3616 storeValue(value);
3617 }
3618
3619 Short8::Short8(RValue<Short4> lo, RValue<Short4> hi)
3620 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05003621 int shuffle[8] = {0, 1, 2, 3, 8, 9, 10, 11}; // Real type is v8i16
3622 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
3623
3624 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003625 }
3626
3627 RValue<Short8> operator+(RValue<Short8> lhs, RValue<Short8> rhs)
3628 {
3629 return RValue<Short8>(Nucleus::createAdd(lhs.value, rhs.value));
3630 }
3631
3632 RValue<Short8> operator&(RValue<Short8> lhs, RValue<Short8> rhs)
3633 {
3634 return RValue<Short8>(Nucleus::createAnd(lhs.value, rhs.value));
3635 }
3636
3637 RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs)
3638 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003639 return RValue<Short8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003640 }
3641
3642 RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs)
3643 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003644 return RValue<Short8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003645 }
3646
3647 RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)
3648 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003649 assert(false && "UNIMPLEMENTED"); return RValue<Int4>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003650 }
3651
3652 RValue<Int4> Abs(RValue<Int4> x)
3653 {
Nicolas Capens84272242016-11-09 13:31:06 -05003654 auto negative = x >> 31;
3655 return (x ^ negative) - negative;
Nicolas Capens598f8d82016-09-26 15:09:10 -04003656 }
3657
3658 RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)
3659 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003660 assert(false && "UNIMPLEMENTED"); return RValue<Short8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003661 }
3662
3663 Type *Short8::getType()
3664 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04003665 return T(Ice::IceType_v8i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003666 }
3667
Nicolas Capens3e7062b2017-01-17 14:01:33 -05003668 UShort8::UShort8(unsigned short c)
3669 {
3670 int64_t constantVector[8] = {c, c, c, c, c, c, c, c};
3671 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3672 }
3673
Nicolas Capens598f8d82016-09-26 15:09:10 -04003674 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)
3675 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003676 int64_t constantVector[8] = {c0, c1, c2, c3, c4, c5, c6, c7};
3677 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003678 }
3679
3680 UShort8::UShort8(RValue<UShort8> rhs)
3681 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003682 storeValue(rhs.value);
3683 }
3684
3685 UShort8::UShort8(const Reference<UShort8> &rhs)
3686 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003687 Value *value = rhs.loadValue();
3688 storeValue(value);
3689 }
3690
3691 UShort8::UShort8(RValue<UShort4> lo, RValue<UShort4> hi)
3692 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05003693 int shuffle[8] = {0, 1, 2, 3, 8, 9, 10, 11}; // Real type is v8i16
3694 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
3695
3696 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003697 }
3698
Nicolas Capens96d4e092016-11-18 14:22:38 -05003699 RValue<UShort8> UShort8::operator=(RValue<UShort8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003700 {
3701 storeValue(rhs.value);
3702
3703 return rhs;
3704 }
3705
Nicolas Capens96d4e092016-11-18 14:22:38 -05003706 RValue<UShort8> UShort8::operator=(const UShort8 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003707 {
3708 Value *value = rhs.loadValue();
3709 storeValue(value);
3710
3711 return RValue<UShort8>(value);
3712 }
3713
Nicolas Capens96d4e092016-11-18 14:22:38 -05003714 RValue<UShort8> UShort8::operator=(const Reference<UShort8> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003715 {
3716 Value *value = rhs.loadValue();
3717 storeValue(value);
3718
3719 return RValue<UShort8>(value);
3720 }
3721
3722 RValue<UShort8> operator&(RValue<UShort8> lhs, RValue<UShort8> rhs)
3723 {
3724 return RValue<UShort8>(Nucleus::createAnd(lhs.value, rhs.value));
3725 }
3726
3727 RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs)
3728 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003729 return RValue<UShort8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003730 }
3731
3732 RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs)
3733 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003734 return RValue<UShort8>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003735 }
3736
3737 RValue<UShort8> operator+(RValue<UShort8> lhs, RValue<UShort8> rhs)
3738 {
3739 return RValue<UShort8>(Nucleus::createAdd(lhs.value, rhs.value));
3740 }
3741
3742 RValue<UShort8> operator*(RValue<UShort8> lhs, RValue<UShort8> rhs)
3743 {
3744 return RValue<UShort8>(Nucleus::createMul(lhs.value, rhs.value));
3745 }
3746
Nicolas Capens96d4e092016-11-18 14:22:38 -05003747 RValue<UShort8> operator+=(UShort8 &lhs, RValue<UShort8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003748 {
3749 return lhs = lhs + rhs;
3750 }
3751
3752 RValue<UShort8> operator~(RValue<UShort8> val)
3753 {
3754 return RValue<UShort8>(Nucleus::createNot(val.value));
3755 }
3756
3757 RValue<UShort8> Swizzle(RValue<UShort8> x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7)
3758 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003759 assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003760 }
3761
3762 RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)
3763 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003764 assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003765 }
3766
3767 // FIXME: Implement as Shuffle(x, y, Select(i0, ..., i16)) and Shuffle(x, y, SELECT_PACK_REPEAT(element))
3768// RValue<UShort8> PackRepeat(RValue<Byte16> x, RValue<Byte16> y, int element)
3769// {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003770// assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003771// }
3772
3773 Type *UShort8::getType()
3774 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04003775 return T(Ice::IceType_v8i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003776 }
3777
3778 Int::Int(Argument<Int> argument)
3779 {
3780 storeValue(argument.value);
3781 }
3782
3783 Int::Int(RValue<Byte> cast)
3784 {
3785 Value *integer = Nucleus::createZExt(cast.value, Int::getType());
3786
3787 storeValue(integer);
3788 }
3789
3790 Int::Int(RValue<SByte> cast)
3791 {
3792 Value *integer = Nucleus::createSExt(cast.value, Int::getType());
3793
3794 storeValue(integer);
3795 }
3796
3797 Int::Int(RValue<Short> cast)
3798 {
3799 Value *integer = Nucleus::createSExt(cast.value, Int::getType());
3800
3801 storeValue(integer);
3802 }
3803
3804 Int::Int(RValue<UShort> cast)
3805 {
3806 Value *integer = Nucleus::createZExt(cast.value, Int::getType());
3807
3808 storeValue(integer);
3809 }
3810
3811 Int::Int(RValue<Int2> cast)
3812 {
3813 *this = Extract(cast, 0);
3814 }
3815
3816 Int::Int(RValue<Long> cast)
3817 {
3818 Value *integer = Nucleus::createTrunc(cast.value, Int::getType());
3819
3820 storeValue(integer);
3821 }
3822
3823 Int::Int(RValue<Float> cast)
3824 {
3825 Value *integer = Nucleus::createFPToSI(cast.value, Int::getType());
3826
3827 storeValue(integer);
3828 }
3829
Nicolas Capens598f8d82016-09-26 15:09:10 -04003830 Int::Int(int x)
3831 {
3832 storeValue(Nucleus::createConstantInt(x));
3833 }
3834
3835 Int::Int(RValue<Int> rhs)
3836 {
3837 storeValue(rhs.value);
3838 }
3839
3840 Int::Int(RValue<UInt> rhs)
3841 {
3842 storeValue(rhs.value);
3843 }
3844
3845 Int::Int(const Int &rhs)
3846 {
3847 Value *value = rhs.loadValue();
3848 storeValue(value);
3849 }
3850
3851 Int::Int(const Reference<Int> &rhs)
3852 {
3853 Value *value = rhs.loadValue();
3854 storeValue(value);
3855 }
3856
3857 Int::Int(const UInt &rhs)
3858 {
3859 Value *value = rhs.loadValue();
3860 storeValue(value);
3861 }
3862
3863 Int::Int(const Reference<UInt> &rhs)
3864 {
3865 Value *value = rhs.loadValue();
3866 storeValue(value);
3867 }
3868
Nicolas Capens96d4e092016-11-18 14:22:38 -05003869 RValue<Int> Int::operator=(int rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003870 {
3871 return RValue<Int>(storeValue(Nucleus::createConstantInt(rhs)));
3872 }
3873
Nicolas Capens96d4e092016-11-18 14:22:38 -05003874 RValue<Int> Int::operator=(RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003875 {
3876 storeValue(rhs.value);
3877
3878 return rhs;
3879 }
3880
Nicolas Capens96d4e092016-11-18 14:22:38 -05003881 RValue<Int> Int::operator=(RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003882 {
3883 storeValue(rhs.value);
3884
3885 return RValue<Int>(rhs);
3886 }
3887
Nicolas Capens96d4e092016-11-18 14:22:38 -05003888 RValue<Int> Int::operator=(const Int &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003889 {
3890 Value *value = rhs.loadValue();
3891 storeValue(value);
3892
3893 return RValue<Int>(value);
3894 }
3895
Nicolas Capens96d4e092016-11-18 14:22:38 -05003896 RValue<Int> Int::operator=(const Reference<Int> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003897 {
3898 Value *value = rhs.loadValue();
3899 storeValue(value);
3900
3901 return RValue<Int>(value);
3902 }
3903
Nicolas Capens96d4e092016-11-18 14:22:38 -05003904 RValue<Int> Int::operator=(const UInt &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003905 {
3906 Value *value = rhs.loadValue();
3907 storeValue(value);
3908
3909 return RValue<Int>(value);
3910 }
3911
Nicolas Capens96d4e092016-11-18 14:22:38 -05003912 RValue<Int> Int::operator=(const Reference<UInt> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003913 {
3914 Value *value = rhs.loadValue();
3915 storeValue(value);
3916
3917 return RValue<Int>(value);
3918 }
3919
3920 RValue<Int> operator+(RValue<Int> lhs, RValue<Int> rhs)
3921 {
3922 return RValue<Int>(Nucleus::createAdd(lhs.value, rhs.value));
3923 }
3924
3925 RValue<Int> operator-(RValue<Int> lhs, RValue<Int> rhs)
3926 {
3927 return RValue<Int>(Nucleus::createSub(lhs.value, rhs.value));
3928 }
3929
3930 RValue<Int> operator*(RValue<Int> lhs, RValue<Int> rhs)
3931 {
3932 return RValue<Int>(Nucleus::createMul(lhs.value, rhs.value));
3933 }
3934
3935 RValue<Int> operator/(RValue<Int> lhs, RValue<Int> rhs)
3936 {
3937 return RValue<Int>(Nucleus::createSDiv(lhs.value, rhs.value));
3938 }
3939
3940 RValue<Int> operator%(RValue<Int> lhs, RValue<Int> rhs)
3941 {
3942 return RValue<Int>(Nucleus::createSRem(lhs.value, rhs.value));
3943 }
3944
3945 RValue<Int> operator&(RValue<Int> lhs, RValue<Int> rhs)
3946 {
3947 return RValue<Int>(Nucleus::createAnd(lhs.value, rhs.value));
3948 }
3949
3950 RValue<Int> operator|(RValue<Int> lhs, RValue<Int> rhs)
3951 {
3952 return RValue<Int>(Nucleus::createOr(lhs.value, rhs.value));
3953 }
3954
3955 RValue<Int> operator^(RValue<Int> lhs, RValue<Int> rhs)
3956 {
3957 return RValue<Int>(Nucleus::createXor(lhs.value, rhs.value));
3958 }
3959
3960 RValue<Int> operator<<(RValue<Int> lhs, RValue<Int> rhs)
3961 {
3962 return RValue<Int>(Nucleus::createShl(lhs.value, rhs.value));
3963 }
3964
3965 RValue<Int> operator>>(RValue<Int> lhs, RValue<Int> rhs)
3966 {
3967 return RValue<Int>(Nucleus::createAShr(lhs.value, rhs.value));
3968 }
3969
Nicolas Capens96d4e092016-11-18 14:22:38 -05003970 RValue<Int> operator+=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003971 {
3972 return lhs = lhs + rhs;
3973 }
3974
Nicolas Capens96d4e092016-11-18 14:22:38 -05003975 RValue<Int> operator-=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003976 {
3977 return lhs = lhs - rhs;
3978 }
3979
Nicolas Capens96d4e092016-11-18 14:22:38 -05003980 RValue<Int> operator*=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003981 {
3982 return lhs = lhs * rhs;
3983 }
3984
Nicolas Capens96d4e092016-11-18 14:22:38 -05003985 RValue<Int> operator/=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003986 {
3987 return lhs = lhs / rhs;
3988 }
3989
Nicolas Capens96d4e092016-11-18 14:22:38 -05003990 RValue<Int> operator%=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003991 {
3992 return lhs = lhs % rhs;
3993 }
3994
Nicolas Capens96d4e092016-11-18 14:22:38 -05003995 RValue<Int> operator&=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003996 {
3997 return lhs = lhs & rhs;
3998 }
3999
Nicolas Capens96d4e092016-11-18 14:22:38 -05004000 RValue<Int> operator|=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004001 {
4002 return lhs = lhs | rhs;
4003 }
4004
Nicolas Capens96d4e092016-11-18 14:22:38 -05004005 RValue<Int> operator^=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004006 {
4007 return lhs = lhs ^ rhs;
4008 }
4009
Nicolas Capens96d4e092016-11-18 14:22:38 -05004010 RValue<Int> operator<<=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004011 {
4012 return lhs = lhs << rhs;
4013 }
4014
Nicolas Capens96d4e092016-11-18 14:22:38 -05004015 RValue<Int> operator>>=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004016 {
4017 return lhs = lhs >> rhs;
4018 }
4019
4020 RValue<Int> operator+(RValue<Int> val)
4021 {
4022 return val;
4023 }
4024
4025 RValue<Int> operator-(RValue<Int> val)
4026 {
4027 return RValue<Int>(Nucleus::createNeg(val.value));
4028 }
4029
4030 RValue<Int> operator~(RValue<Int> val)
4031 {
4032 return RValue<Int>(Nucleus::createNot(val.value));
4033 }
4034
Nicolas Capens96d4e092016-11-18 14:22:38 -05004035 RValue<Int> operator++(Int &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004036 {
Nicolas Capens5b41ba32016-12-08 14:34:00 -05004037 RValue<Int> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05004038 val += 1;
4039 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004040 }
4041
Nicolas Capens96d4e092016-11-18 14:22:38 -05004042 const Int &operator++(Int &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004043 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004044 val += 1;
4045 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004046 }
4047
Nicolas Capens96d4e092016-11-18 14:22:38 -05004048 RValue<Int> operator--(Int &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004049 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004050 RValue<Int> res = val;
4051 val -= 1;
4052 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004053 }
4054
Nicolas Capens96d4e092016-11-18 14:22:38 -05004055 const Int &operator--(Int &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004056 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004057 val -= 1;
4058 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004059 }
4060
4061 RValue<Bool> operator<(RValue<Int> lhs, RValue<Int> rhs)
4062 {
4063 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
4064 }
4065
4066 RValue<Bool> operator<=(RValue<Int> lhs, RValue<Int> rhs)
4067 {
4068 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
4069 }
4070
4071 RValue<Bool> operator>(RValue<Int> lhs, RValue<Int> rhs)
4072 {
4073 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
4074 }
4075
4076 RValue<Bool> operator>=(RValue<Int> lhs, RValue<Int> rhs)
4077 {
4078 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
4079 }
4080
4081 RValue<Bool> operator!=(RValue<Int> lhs, RValue<Int> rhs)
4082 {
4083 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
4084 }
4085
4086 RValue<Bool> operator==(RValue<Int> lhs, RValue<Int> rhs)
4087 {
4088 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
4089 }
4090
4091 RValue<Int> Max(RValue<Int> x, RValue<Int> y)
4092 {
4093 return IfThenElse(x > y, x, y);
4094 }
4095
4096 RValue<Int> Min(RValue<Int> x, RValue<Int> y)
4097 {
4098 return IfThenElse(x < y, x, y);
4099 }
4100
4101 RValue<Int> Clamp(RValue<Int> x, RValue<Int> min, RValue<Int> max)
4102 {
4103 return Min(Max(x, min), max);
4104 }
4105
4106 RValue<Int> RoundInt(RValue<Float> cast)
4107 {
Nicolas Capensa8086512016-11-07 17:32:17 -05004108 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
Nicolas Capens9ca48d52017-01-14 12:52:55 -05004109 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
4110 auto target = ::context->getConstantUndef(Ice::IceType_i32);
4111 auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
4112 nearbyint->addArg(cast.value);
4113 ::basicBlock->appendInst(nearbyint);
Nicolas Capensa8086512016-11-07 17:32:17 -05004114
4115 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004116 }
4117
4118 Type *Int::getType()
4119 {
4120 return T(Ice::IceType_i32);
4121 }
4122
4123 Long::Long(RValue<Int> cast)
4124 {
4125 Value *integer = Nucleus::createSExt(cast.value, Long::getType());
4126
4127 storeValue(integer);
4128 }
4129
4130 Long::Long(RValue<UInt> cast)
4131 {
4132 Value *integer = Nucleus::createZExt(cast.value, Long::getType());
4133
4134 storeValue(integer);
4135 }
4136
Nicolas Capens598f8d82016-09-26 15:09:10 -04004137 Long::Long(RValue<Long> rhs)
4138 {
4139 storeValue(rhs.value);
4140 }
4141
Nicolas Capens96d4e092016-11-18 14:22:38 -05004142 RValue<Long> Long::operator=(int64_t rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004143 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04004144 return RValue<Long>(storeValue(Nucleus::createConstantLong(rhs)));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004145 }
4146
Nicolas Capens96d4e092016-11-18 14:22:38 -05004147 RValue<Long> Long::operator=(RValue<Long> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004148 {
4149 storeValue(rhs.value);
4150
4151 return rhs;
4152 }
4153
Nicolas Capens96d4e092016-11-18 14:22:38 -05004154 RValue<Long> Long::operator=(const Long &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004155 {
4156 Value *value = rhs.loadValue();
4157 storeValue(value);
4158
4159 return RValue<Long>(value);
4160 }
4161
Nicolas Capens96d4e092016-11-18 14:22:38 -05004162 RValue<Long> Long::operator=(const Reference<Long> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004163 {
4164 Value *value = rhs.loadValue();
4165 storeValue(value);
4166
4167 return RValue<Long>(value);
4168 }
4169
4170 RValue<Long> operator+(RValue<Long> lhs, RValue<Long> rhs)
4171 {
4172 return RValue<Long>(Nucleus::createAdd(lhs.value, rhs.value));
4173 }
4174
4175 RValue<Long> operator-(RValue<Long> lhs, RValue<Long> rhs)
4176 {
4177 return RValue<Long>(Nucleus::createSub(lhs.value, rhs.value));
4178 }
4179
Nicolas Capens96d4e092016-11-18 14:22:38 -05004180 RValue<Long> operator+=(Long &lhs, RValue<Long> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004181 {
4182 return lhs = lhs + rhs;
4183 }
4184
Nicolas Capens96d4e092016-11-18 14:22:38 -05004185 RValue<Long> operator-=(Long &lhs, RValue<Long> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004186 {
4187 return lhs = lhs - rhs;
4188 }
4189
4190 RValue<Long> AddAtomic(RValue<Pointer<Long> > x, RValue<Long> y)
4191 {
4192 return RValue<Long>(Nucleus::createAtomicAdd(x.value, y.value));
4193 }
4194
4195 Type *Long::getType()
4196 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04004197 return T(Ice::IceType_i64);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004198 }
4199
Nicolas Capens598f8d82016-09-26 15:09:10 -04004200 UInt::UInt(Argument<UInt> argument)
4201 {
4202 storeValue(argument.value);
4203 }
4204
4205 UInt::UInt(RValue<UShort> cast)
4206 {
4207 Value *integer = Nucleus::createZExt(cast.value, UInt::getType());
4208
4209 storeValue(integer);
4210 }
4211
4212 UInt::UInt(RValue<Long> cast)
4213 {
4214 Value *integer = Nucleus::createTrunc(cast.value, UInt::getType());
4215
4216 storeValue(integer);
4217 }
4218
4219 UInt::UInt(RValue<Float> cast)
4220 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05004221 // Smallest positive value representable in UInt, but not in Int
4222 const unsigned int ustart = 0x80000000u;
4223 const float ustartf = float(ustart);
4224
4225 // If the value is negative, store 0, otherwise store the result of the conversion
4226 storeValue((~(As<Int>(cast) >> 31) &
4227 // Check if the value can be represented as an Int
4228 IfThenElse(cast >= ustartf,
4229 // If the value is too large, subtract ustart and re-add it after conversion.
4230 As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)),
4231 // Otherwise, just convert normally
4232 Int(cast))).value);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004233 }
4234
Nicolas Capens598f8d82016-09-26 15:09:10 -04004235 UInt::UInt(int x)
4236 {
4237 storeValue(Nucleus::createConstantInt(x));
4238 }
4239
4240 UInt::UInt(unsigned int x)
4241 {
4242 storeValue(Nucleus::createConstantInt(x));
4243 }
4244
4245 UInt::UInt(RValue<UInt> rhs)
4246 {
4247 storeValue(rhs.value);
4248 }
4249
4250 UInt::UInt(RValue<Int> rhs)
4251 {
4252 storeValue(rhs.value);
4253 }
4254
4255 UInt::UInt(const UInt &rhs)
4256 {
4257 Value *value = rhs.loadValue();
4258 storeValue(value);
4259 }
4260
4261 UInt::UInt(const Reference<UInt> &rhs)
4262 {
4263 Value *value = rhs.loadValue();
4264 storeValue(value);
4265 }
4266
4267 UInt::UInt(const Int &rhs)
4268 {
4269 Value *value = rhs.loadValue();
4270 storeValue(value);
4271 }
4272
4273 UInt::UInt(const Reference<Int> &rhs)
4274 {
4275 Value *value = rhs.loadValue();
4276 storeValue(value);
4277 }
4278
Nicolas Capens96d4e092016-11-18 14:22:38 -05004279 RValue<UInt> UInt::operator=(unsigned int rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004280 {
4281 return RValue<UInt>(storeValue(Nucleus::createConstantInt(rhs)));
4282 }
4283
Nicolas Capens96d4e092016-11-18 14:22:38 -05004284 RValue<UInt> UInt::operator=(RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004285 {
4286 storeValue(rhs.value);
4287
4288 return rhs;
4289 }
4290
Nicolas Capens96d4e092016-11-18 14:22:38 -05004291 RValue<UInt> UInt::operator=(RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004292 {
4293 storeValue(rhs.value);
4294
4295 return RValue<UInt>(rhs);
4296 }
4297
Nicolas Capens96d4e092016-11-18 14:22:38 -05004298 RValue<UInt> UInt::operator=(const UInt &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004299 {
4300 Value *value = rhs.loadValue();
4301 storeValue(value);
4302
4303 return RValue<UInt>(value);
4304 }
4305
Nicolas Capens96d4e092016-11-18 14:22:38 -05004306 RValue<UInt> UInt::operator=(const Reference<UInt> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004307 {
4308 Value *value = rhs.loadValue();
4309 storeValue(value);
4310
4311 return RValue<UInt>(value);
4312 }
4313
Nicolas Capens96d4e092016-11-18 14:22:38 -05004314 RValue<UInt> UInt::operator=(const Int &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004315 {
4316 Value *value = rhs.loadValue();
4317 storeValue(value);
4318
4319 return RValue<UInt>(value);
4320 }
4321
Nicolas Capens96d4e092016-11-18 14:22:38 -05004322 RValue<UInt> UInt::operator=(const Reference<Int> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004323 {
4324 Value *value = rhs.loadValue();
4325 storeValue(value);
4326
4327 return RValue<UInt>(value);
4328 }
4329
4330 RValue<UInt> operator+(RValue<UInt> lhs, RValue<UInt> rhs)
4331 {
4332 return RValue<UInt>(Nucleus::createAdd(lhs.value, rhs.value));
4333 }
4334
4335 RValue<UInt> operator-(RValue<UInt> lhs, RValue<UInt> rhs)
4336 {
4337 return RValue<UInt>(Nucleus::createSub(lhs.value, rhs.value));
4338 }
4339
4340 RValue<UInt> operator*(RValue<UInt> lhs, RValue<UInt> rhs)
4341 {
4342 return RValue<UInt>(Nucleus::createMul(lhs.value, rhs.value));
4343 }
4344
4345 RValue<UInt> operator/(RValue<UInt> lhs, RValue<UInt> rhs)
4346 {
4347 return RValue<UInt>(Nucleus::createUDiv(lhs.value, rhs.value));
4348 }
4349
4350 RValue<UInt> operator%(RValue<UInt> lhs, RValue<UInt> rhs)
4351 {
4352 return RValue<UInt>(Nucleus::createURem(lhs.value, rhs.value));
4353 }
4354
4355 RValue<UInt> operator&(RValue<UInt> lhs, RValue<UInt> rhs)
4356 {
4357 return RValue<UInt>(Nucleus::createAnd(lhs.value, rhs.value));
4358 }
4359
4360 RValue<UInt> operator|(RValue<UInt> lhs, RValue<UInt> rhs)
4361 {
4362 return RValue<UInt>(Nucleus::createOr(lhs.value, rhs.value));
4363 }
4364
4365 RValue<UInt> operator^(RValue<UInt> lhs, RValue<UInt> rhs)
4366 {
4367 return RValue<UInt>(Nucleus::createXor(lhs.value, rhs.value));
4368 }
4369
4370 RValue<UInt> operator<<(RValue<UInt> lhs, RValue<UInt> rhs)
4371 {
4372 return RValue<UInt>(Nucleus::createShl(lhs.value, rhs.value));
4373 }
4374
4375 RValue<UInt> operator>>(RValue<UInt> lhs, RValue<UInt> rhs)
4376 {
4377 return RValue<UInt>(Nucleus::createLShr(lhs.value, rhs.value));
4378 }
4379
Nicolas Capens96d4e092016-11-18 14:22:38 -05004380 RValue<UInt> operator+=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004381 {
4382 return lhs = lhs + rhs;
4383 }
4384
Nicolas Capens96d4e092016-11-18 14:22:38 -05004385 RValue<UInt> operator-=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004386 {
4387 return lhs = lhs - rhs;
4388 }
4389
Nicolas Capens96d4e092016-11-18 14:22:38 -05004390 RValue<UInt> operator*=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004391 {
4392 return lhs = lhs * rhs;
4393 }
4394
Nicolas Capens96d4e092016-11-18 14:22:38 -05004395 RValue<UInt> operator/=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004396 {
4397 return lhs = lhs / rhs;
4398 }
4399
Nicolas Capens96d4e092016-11-18 14:22:38 -05004400 RValue<UInt> operator%=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004401 {
4402 return lhs = lhs % rhs;
4403 }
4404
Nicolas Capens96d4e092016-11-18 14:22:38 -05004405 RValue<UInt> operator&=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004406 {
4407 return lhs = lhs & rhs;
4408 }
4409
Nicolas Capens96d4e092016-11-18 14:22:38 -05004410 RValue<UInt> operator|=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004411 {
4412 return lhs = lhs | rhs;
4413 }
4414
Nicolas Capens96d4e092016-11-18 14:22:38 -05004415 RValue<UInt> operator^=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004416 {
4417 return lhs = lhs ^ rhs;
4418 }
4419
Nicolas Capens96d4e092016-11-18 14:22:38 -05004420 RValue<UInt> operator<<=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004421 {
4422 return lhs = lhs << rhs;
4423 }
4424
Nicolas Capens96d4e092016-11-18 14:22:38 -05004425 RValue<UInt> operator>>=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004426 {
4427 return lhs = lhs >> rhs;
4428 }
4429
4430 RValue<UInt> operator+(RValue<UInt> val)
4431 {
4432 return val;
4433 }
4434
4435 RValue<UInt> operator-(RValue<UInt> val)
4436 {
4437 return RValue<UInt>(Nucleus::createNeg(val.value));
4438 }
4439
4440 RValue<UInt> operator~(RValue<UInt> val)
4441 {
4442 return RValue<UInt>(Nucleus::createNot(val.value));
4443 }
4444
Nicolas Capens96d4e092016-11-18 14:22:38 -05004445 RValue<UInt> operator++(UInt &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004446 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004447 RValue<UInt> res = val;
4448 val += 1;
4449 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004450 }
4451
Nicolas Capens96d4e092016-11-18 14:22:38 -05004452 const UInt &operator++(UInt &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004453 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004454 val += 1;
4455 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004456 }
4457
Nicolas Capens96d4e092016-11-18 14:22:38 -05004458 RValue<UInt> operator--(UInt &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004459 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004460 RValue<UInt> res = val;
4461 val -= 1;
4462 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004463 }
4464
Nicolas Capens96d4e092016-11-18 14:22:38 -05004465 const UInt &operator--(UInt &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004466 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004467 val -= 1;
4468 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004469 }
4470
4471 RValue<UInt> Max(RValue<UInt> x, RValue<UInt> y)
4472 {
4473 return IfThenElse(x > y, x, y);
4474 }
4475
4476 RValue<UInt> Min(RValue<UInt> x, RValue<UInt> y)
4477 {
4478 return IfThenElse(x < y, x, y);
4479 }
4480
4481 RValue<UInt> Clamp(RValue<UInt> x, RValue<UInt> min, RValue<UInt> max)
4482 {
4483 return Min(Max(x, min), max);
4484 }
4485
4486 RValue<Bool> operator<(RValue<UInt> lhs, RValue<UInt> rhs)
4487 {
4488 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
4489 }
4490
4491 RValue<Bool> operator<=(RValue<UInt> lhs, RValue<UInt> rhs)
4492 {
4493 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
4494 }
4495
4496 RValue<Bool> operator>(RValue<UInt> lhs, RValue<UInt> rhs)
4497 {
4498 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
4499 }
4500
4501 RValue<Bool> operator>=(RValue<UInt> lhs, RValue<UInt> rhs)
4502 {
4503 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
4504 }
4505
4506 RValue<Bool> operator!=(RValue<UInt> lhs, RValue<UInt> rhs)
4507 {
4508 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
4509 }
4510
4511 RValue<Bool> operator==(RValue<UInt> lhs, RValue<UInt> rhs)
4512 {
4513 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
4514 }
4515
4516// RValue<UInt> RoundUInt(RValue<Float> cast)
4517// {
Nicolas Capensc37252c2016-09-28 16:11:54 -04004518// assert(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004519// }
4520
4521 Type *UInt::getType()
4522 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04004523 return T(Ice::IceType_i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004524 }
4525
4526// Int2::Int2(RValue<Int> cast)
4527// {
4528// Value *extend = Nucleus::createZExt(cast.value, Long::getType());
4529// Value *vector = Nucleus::createBitCast(extend, Int2::getType());
4530//
4531// Constant *shuffle[2];
4532// shuffle[0] = Nucleus::createConstantInt(0);
4533// shuffle[1] = Nucleus::createConstantInt(0);
4534//
4535// Value *replicate = Nucleus::createShuffleVector(vector, UndefValue::get(Int2::getType()), Nucleus::createConstantVector(shuffle, 2));
4536//
4537// storeValue(replicate);
4538// }
4539
4540 Int2::Int2(RValue<Int4> cast)
4541 {
Nicolas Capens22008782016-10-20 01:11:47 -04004542 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004543 }
4544
Nicolas Capens598f8d82016-09-26 15:09:10 -04004545 Int2::Int2(int x, int y)
4546 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004547 int64_t constantVector[2] = {x, y};
4548 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004549 }
4550
4551 Int2::Int2(RValue<Int2> rhs)
4552 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004553 storeValue(rhs.value);
4554 }
4555
4556 Int2::Int2(const Int2 &rhs)
4557 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004558 Value *value = rhs.loadValue();
4559 storeValue(value);
4560 }
4561
4562 Int2::Int2(const Reference<Int2> &rhs)
4563 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004564 Value *value = rhs.loadValue();
4565 storeValue(value);
4566 }
4567
4568 Int2::Int2(RValue<Int> lo, RValue<Int> hi)
4569 {
Nicolas Capensd4227962016-11-09 14:24:25 -05004570 int shuffle[4] = {0, 4, 1, 5};
4571 Value *packed = Nucleus::createShuffleVector(Int4(lo).loadValue(), Int4(hi).loadValue(), shuffle);
4572
4573 storeValue(Nucleus::createBitCast(packed, Int2::getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004574 }
4575
Nicolas Capens96d4e092016-11-18 14:22:38 -05004576 RValue<Int2> Int2::operator=(RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004577 {
4578 storeValue(rhs.value);
4579
4580 return rhs;
4581 }
4582
Nicolas Capens96d4e092016-11-18 14:22:38 -05004583 RValue<Int2> Int2::operator=(const Int2 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004584 {
4585 Value *value = rhs.loadValue();
4586 storeValue(value);
4587
4588 return RValue<Int2>(value);
4589 }
4590
Nicolas Capens96d4e092016-11-18 14:22:38 -05004591 RValue<Int2> Int2::operator=(const Reference<Int2> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004592 {
4593 Value *value = rhs.loadValue();
4594 storeValue(value);
4595
4596 return RValue<Int2>(value);
4597 }
4598
4599 RValue<Int2> operator+(RValue<Int2> lhs, RValue<Int2> rhs)
4600 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004601 return RValue<Int2>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004602 }
4603
4604 RValue<Int2> operator-(RValue<Int2> lhs, RValue<Int2> rhs)
4605 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004606 return RValue<Int2>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004607 }
4608
4609// RValue<Int2> operator*(RValue<Int2> lhs, RValue<Int2> rhs)
4610// {
4611// return RValue<Int2>(Nucleus::createMul(lhs.value, rhs.value));
4612// }
4613
4614// RValue<Int2> operator/(RValue<Int2> lhs, RValue<Int2> rhs)
4615// {
4616// return RValue<Int2>(Nucleus::createSDiv(lhs.value, rhs.value));
4617// }
4618
4619// RValue<Int2> operator%(RValue<Int2> lhs, RValue<Int2> rhs)
4620// {
4621// return RValue<Int2>(Nucleus::createSRem(lhs.value, rhs.value));
4622// }
4623
4624 RValue<Int2> operator&(RValue<Int2> lhs, RValue<Int2> rhs)
4625 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004626 return RValue<Int2>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004627 }
4628
4629 RValue<Int2> operator|(RValue<Int2> lhs, RValue<Int2> rhs)
4630 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004631 return RValue<Int2>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004632 }
4633
4634 RValue<Int2> operator^(RValue<Int2> lhs, RValue<Int2> rhs)
4635 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004636 return RValue<Int2>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004637 }
4638
4639 RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs)
4640 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004641 return RValue<Int2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004642 }
4643
4644 RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs)
4645 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004646 return RValue<Int2>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004647 }
4648
Nicolas Capens96d4e092016-11-18 14:22:38 -05004649 RValue<Int2> operator+=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004650 {
4651 return lhs = lhs + rhs;
4652 }
4653
Nicolas Capens96d4e092016-11-18 14:22:38 -05004654 RValue<Int2> operator-=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004655 {
4656 return lhs = lhs - rhs;
4657 }
4658
Nicolas Capens96d4e092016-11-18 14:22:38 -05004659// RValue<Int2> operator*=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004660// {
4661// return lhs = lhs * rhs;
4662// }
4663
Nicolas Capens96d4e092016-11-18 14:22:38 -05004664// RValue<Int2> operator/=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004665// {
4666// return lhs = lhs / rhs;
4667// }
4668
Nicolas Capens96d4e092016-11-18 14:22:38 -05004669// RValue<Int2> operator%=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004670// {
4671// return lhs = lhs % rhs;
4672// }
4673
Nicolas Capens96d4e092016-11-18 14:22:38 -05004674 RValue<Int2> operator&=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004675 {
4676 return lhs = lhs & rhs;
4677 }
4678
Nicolas Capens96d4e092016-11-18 14:22:38 -05004679 RValue<Int2> operator|=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004680 {
4681 return lhs = lhs | rhs;
4682 }
4683
Nicolas Capens96d4e092016-11-18 14:22:38 -05004684 RValue<Int2> operator^=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004685 {
4686 return lhs = lhs ^ rhs;
4687 }
4688
Nicolas Capens96d4e092016-11-18 14:22:38 -05004689 RValue<Int2> operator<<=(Int2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004690 {
4691 return lhs = lhs << rhs;
4692 }
4693
Nicolas Capens96d4e092016-11-18 14:22:38 -05004694 RValue<Int2> operator>>=(Int2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004695 {
4696 return lhs = lhs >> rhs;
4697 }
4698
Nicolas Capens598f8d82016-09-26 15:09:10 -04004699// RValue<Int2> operator+(RValue<Int2> val)
4700// {
4701// return val;
4702// }
4703
4704// RValue<Int2> operator-(RValue<Int2> val)
4705// {
4706// return RValue<Int2>(Nucleus::createNeg(val.value));
4707// }
4708
4709 RValue<Int2> operator~(RValue<Int2> val)
4710 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05004711 return RValue<Int2>(Nucleus::createNot(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004712 }
4713
Nicolas Capens45f187a2016-12-02 15:30:56 -05004714 RValue<Short4> UnpackLow(RValue<Int2> x, RValue<Int2> y)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004715 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05004716 int shuffle[4] = {0, 4, 1, 5}; // Real type is v4i32
4717 return As<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004718 }
4719
Nicolas Capens45f187a2016-12-02 15:30:56 -05004720 RValue<Short4> UnpackHigh(RValue<Int2> x, RValue<Int2> y)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004721 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08004722 int shuffle[4] = {0, 4, 1, 5}; // Real type is v4i32
Nicolas Capensc70a1162016-12-03 00:16:14 -05004723 auto lowHigh = RValue<Int4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
4724 return As<Short4>(Swizzle(lowHigh, 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004725 }
4726
4727 RValue<Int> Extract(RValue<Int2> val, int i)
4728 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05004729 return RValue<Int>(Nucleus::createExtractElement(val.value, Int::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004730 }
4731
4732 RValue<Int2> Insert(RValue<Int2> val, RValue<Int> element, int i)
4733 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05004734 return RValue<Int2>(Nucleus::createInsertElement(val.value, element.value, i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004735 }
4736
4737 Type *Int2::getType()
4738 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004739 return T(Type_v2i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004740 }
4741
Nicolas Capens598f8d82016-09-26 15:09:10 -04004742 UInt2::UInt2(unsigned int x, unsigned int y)
4743 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004744 int64_t constantVector[2] = {x, y};
4745 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004746 }
4747
4748 UInt2::UInt2(RValue<UInt2> rhs)
4749 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004750 storeValue(rhs.value);
4751 }
4752
4753 UInt2::UInt2(const UInt2 &rhs)
4754 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004755 Value *value = rhs.loadValue();
4756 storeValue(value);
4757 }
4758
4759 UInt2::UInt2(const Reference<UInt2> &rhs)
4760 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004761 Value *value = rhs.loadValue();
4762 storeValue(value);
4763 }
4764
Nicolas Capens96d4e092016-11-18 14:22:38 -05004765 RValue<UInt2> UInt2::operator=(RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004766 {
4767 storeValue(rhs.value);
4768
4769 return rhs;
4770 }
4771
Nicolas Capens96d4e092016-11-18 14:22:38 -05004772 RValue<UInt2> UInt2::operator=(const UInt2 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004773 {
4774 Value *value = rhs.loadValue();
4775 storeValue(value);
4776
4777 return RValue<UInt2>(value);
4778 }
4779
Nicolas Capens96d4e092016-11-18 14:22:38 -05004780 RValue<UInt2> UInt2::operator=(const Reference<UInt2> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004781 {
4782 Value *value = rhs.loadValue();
4783 storeValue(value);
4784
4785 return RValue<UInt2>(value);
4786 }
4787
4788 RValue<UInt2> operator+(RValue<UInt2> lhs, RValue<UInt2> rhs)
4789 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004790 return RValue<UInt2>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004791 }
4792
4793 RValue<UInt2> operator-(RValue<UInt2> lhs, RValue<UInt2> rhs)
4794 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004795 return RValue<UInt2>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004796 }
4797
4798// RValue<UInt2> operator*(RValue<UInt2> lhs, RValue<UInt2> rhs)
4799// {
4800// return RValue<UInt2>(Nucleus::createMul(lhs.value, rhs.value));
4801// }
4802
4803// RValue<UInt2> operator/(RValue<UInt2> lhs, RValue<UInt2> rhs)
4804// {
4805// return RValue<UInt2>(Nucleus::createUDiv(lhs.value, rhs.value));
4806// }
4807
4808// RValue<UInt2> operator%(RValue<UInt2> lhs, RValue<UInt2> rhs)
4809// {
4810// return RValue<UInt2>(Nucleus::createURem(lhs.value, rhs.value));
4811// }
4812
4813 RValue<UInt2> operator&(RValue<UInt2> lhs, RValue<UInt2> rhs)
4814 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004815 return RValue<UInt2>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004816 }
4817
4818 RValue<UInt2> operator|(RValue<UInt2> lhs, RValue<UInt2> rhs)
4819 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004820 return RValue<UInt2>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004821 }
4822
4823 RValue<UInt2> operator^(RValue<UInt2> lhs, RValue<UInt2> rhs)
4824 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004825 return RValue<UInt2>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004826 }
4827
4828 RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs)
4829 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004830 return RValue<UInt2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004831 }
4832
4833 RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs)
4834 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004835 return RValue<UInt2>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004836 }
4837
Nicolas Capens96d4e092016-11-18 14:22:38 -05004838 RValue<UInt2> operator+=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004839 {
4840 return lhs = lhs + rhs;
4841 }
4842
Nicolas Capens96d4e092016-11-18 14:22:38 -05004843 RValue<UInt2> operator-=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004844 {
4845 return lhs = lhs - rhs;
4846 }
4847
Nicolas Capens96d4e092016-11-18 14:22:38 -05004848// RValue<UInt2> operator*=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004849// {
4850// return lhs = lhs * rhs;
4851// }
4852
Nicolas Capens96d4e092016-11-18 14:22:38 -05004853// RValue<UInt2> operator/=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004854// {
4855// return lhs = lhs / rhs;
4856// }
4857
Nicolas Capens96d4e092016-11-18 14:22:38 -05004858// RValue<UInt2> operator%=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004859// {
4860// return lhs = lhs % rhs;
4861// }
4862
Nicolas Capens96d4e092016-11-18 14:22:38 -05004863 RValue<UInt2> operator&=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004864 {
4865 return lhs = lhs & rhs;
4866 }
4867
Nicolas Capens96d4e092016-11-18 14:22:38 -05004868 RValue<UInt2> operator|=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004869 {
4870 return lhs = lhs | rhs;
4871 }
4872
Nicolas Capens96d4e092016-11-18 14:22:38 -05004873 RValue<UInt2> operator^=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004874 {
4875 return lhs = lhs ^ rhs;
4876 }
4877
Nicolas Capens96d4e092016-11-18 14:22:38 -05004878 RValue<UInt2> operator<<=(UInt2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004879 {
4880 return lhs = lhs << rhs;
4881 }
4882
Nicolas Capens96d4e092016-11-18 14:22:38 -05004883 RValue<UInt2> operator>>=(UInt2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004884 {
4885 return lhs = lhs >> rhs;
4886 }
4887
Nicolas Capens598f8d82016-09-26 15:09:10 -04004888// RValue<UInt2> operator+(RValue<UInt2> val)
4889// {
4890// return val;
4891// }
4892
4893// RValue<UInt2> operator-(RValue<UInt2> val)
4894// {
4895// return RValue<UInt2>(Nucleus::createNeg(val.value));
4896// }
4897
4898 RValue<UInt2> operator~(RValue<UInt2> val)
4899 {
4900 return RValue<UInt2>(Nucleus::createNot(val.value));
4901 }
4902
4903 Type *UInt2::getType()
4904 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04004905 return T(Type_v2i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004906 }
4907
4908 Int4::Int4(RValue<Byte4> cast)
4909 {
Nicolas Capensd4227962016-11-09 14:24:25 -05004910 Value *x = Nucleus::createBitCast(cast.value, Int::getType());
4911 Value *a = Nucleus::createInsertElement(loadValue(), x, 0);
4912
4913 Value *e;
4914 int swizzle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23};
4915 Value *b = Nucleus::createBitCast(a, Byte16::getType());
4916 Value *c = Nucleus::createShuffleVector(b, V(Nucleus::createNullValue(Byte16::getType())), swizzle);
4917
4918 int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11};
4919 Value *d = Nucleus::createBitCast(c, Short8::getType());
4920 e = Nucleus::createShuffleVector(d, V(Nucleus::createNullValue(Short8::getType())), swizzle2);
4921
4922 Value *f = Nucleus::createBitCast(e, Int4::getType());
4923 storeValue(f);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004924 }
4925
4926 Int4::Int4(RValue<SByte4> cast)
4927 {
Nicolas Capensd4227962016-11-09 14:24:25 -05004928 Value *x = Nucleus::createBitCast(cast.value, Int::getType());
4929 Value *a = Nucleus::createInsertElement(loadValue(), x, 0);
4930
4931 Value *e;
4932 int swizzle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7};
4933 Value *b = Nucleus::createBitCast(a, Byte16::getType());
4934 Value *c = Nucleus::createShuffleVector(b, b, swizzle);
4935
4936 int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3};
4937 Value *d = Nucleus::createBitCast(c, Short8::getType());
4938 e = Nucleus::createShuffleVector(d, d, swizzle2);
4939
4940 Value *f = Nucleus::createBitCast(e, Int4::getType());
Nicolas Capens15060bb2016-12-05 22:17:19 -05004941 Value *g = Nucleus::createAShr(f, V(::context->getConstantInt32(24)));
Nicolas Capensd4227962016-11-09 14:24:25 -05004942 storeValue(g);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004943 }
4944
4945 Int4::Int4(RValue<Float4> cast)
4946 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004947 Value *xyzw = Nucleus::createFPToSI(cast.value, Int4::getType());
4948
4949 storeValue(xyzw);
4950 }
4951
4952 Int4::Int4(RValue<Short4> cast)
4953 {
Nicolas Capensd4227962016-11-09 14:24:25 -05004954 int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3};
4955 Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle);
4956 Value *d = Nucleus::createBitCast(c, Int4::getType());
Nicolas Capens15060bb2016-12-05 22:17:19 -05004957 Value *e = Nucleus::createAShr(d, V(::context->getConstantInt32(16)));
Nicolas Capensd4227962016-11-09 14:24:25 -05004958 storeValue(e);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004959 }
4960
4961 Int4::Int4(RValue<UShort4> cast)
4962 {
Nicolas Capensd4227962016-11-09 14:24:25 -05004963 int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11};
4964 Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle);
4965 Value *d = Nucleus::createBitCast(c, Int4::getType());
4966 storeValue(d);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004967 }
4968
Nicolas Capens598f8d82016-09-26 15:09:10 -04004969 Int4::Int4(int xyzw)
4970 {
4971 constant(xyzw, xyzw, xyzw, xyzw);
4972 }
4973
4974 Int4::Int4(int x, int yzw)
4975 {
4976 constant(x, yzw, yzw, yzw);
4977 }
4978
4979 Int4::Int4(int x, int y, int zw)
4980 {
4981 constant(x, y, zw, zw);
4982 }
4983
4984 Int4::Int4(int x, int y, int z, int w)
4985 {
4986 constant(x, y, z, w);
4987 }
4988
4989 void Int4::constant(int x, int y, int z, int w)
4990 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04004991 int64_t constantVector[4] = {x, y, z, w};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004992 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004993 }
4994
4995 Int4::Int4(RValue<Int4> rhs)
4996 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004997 storeValue(rhs.value);
4998 }
4999
5000 Int4::Int4(const Int4 &rhs)
5001 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005002 Value *value = rhs.loadValue();
5003 storeValue(value);
5004 }
5005
5006 Int4::Int4(const Reference<Int4> &rhs)
5007 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005008 Value *value = rhs.loadValue();
5009 storeValue(value);
5010 }
5011
5012 Int4::Int4(RValue<UInt4> rhs)
5013 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005014 storeValue(rhs.value);
5015 }
5016
5017 Int4::Int4(const UInt4 &rhs)
5018 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005019 Value *value = rhs.loadValue();
5020 storeValue(value);
5021 }
5022
5023 Int4::Int4(const Reference<UInt4> &rhs)
5024 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005025 Value *value = rhs.loadValue();
5026 storeValue(value);
5027 }
5028
5029 Int4::Int4(RValue<Int2> lo, RValue<Int2> hi)
5030 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05005031 int shuffle[4] = {0, 1, 4, 5}; // Real type is v4i32
5032 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
5033
5034 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005035 }
5036
5037 Int4::Int4(RValue<Int> rhs)
5038 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08005039 Value *vector = Nucleus::createBitCast(rhs.value, Int4::getType());
Nicolas Capensd4227962016-11-09 14:24:25 -05005040
5041 int swizzle[4] = {0, 0, 0, 0};
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08005042 Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle);
Nicolas Capensd4227962016-11-09 14:24:25 -05005043
5044 storeValue(replicate);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005045 }
5046
5047 Int4::Int4(const Int &rhs)
5048 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005049 *this = RValue<Int>(rhs.loadValue());
5050 }
5051
5052 Int4::Int4(const Reference<Int> &rhs)
5053 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005054 *this = RValue<Int>(rhs.loadValue());
5055 }
5056
Nicolas Capens96d4e092016-11-18 14:22:38 -05005057 RValue<Int4> Int4::operator=(RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005058 {
5059 storeValue(rhs.value);
5060
5061 return rhs;
5062 }
5063
Nicolas Capens96d4e092016-11-18 14:22:38 -05005064 RValue<Int4> Int4::operator=(const Int4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005065 {
5066 Value *value = rhs.loadValue();
5067 storeValue(value);
5068
5069 return RValue<Int4>(value);
5070 }
5071
Nicolas Capens96d4e092016-11-18 14:22:38 -05005072 RValue<Int4> Int4::operator=(const Reference<Int4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005073 {
5074 Value *value = rhs.loadValue();
5075 storeValue(value);
5076
5077 return RValue<Int4>(value);
5078 }
5079
5080 RValue<Int4> operator+(RValue<Int4> lhs, RValue<Int4> rhs)
5081 {
5082 return RValue<Int4>(Nucleus::createAdd(lhs.value, rhs.value));
5083 }
5084
5085 RValue<Int4> operator-(RValue<Int4> lhs, RValue<Int4> rhs)
5086 {
5087 return RValue<Int4>(Nucleus::createSub(lhs.value, rhs.value));
5088 }
5089
5090 RValue<Int4> operator*(RValue<Int4> lhs, RValue<Int4> rhs)
5091 {
5092 return RValue<Int4>(Nucleus::createMul(lhs.value, rhs.value));
5093 }
5094
5095 RValue<Int4> operator/(RValue<Int4> lhs, RValue<Int4> rhs)
5096 {
5097 return RValue<Int4>(Nucleus::createSDiv(lhs.value, rhs.value));
5098 }
5099
5100 RValue<Int4> operator%(RValue<Int4> lhs, RValue<Int4> rhs)
5101 {
5102 return RValue<Int4>(Nucleus::createSRem(lhs.value, rhs.value));
5103 }
5104
5105 RValue<Int4> operator&(RValue<Int4> lhs, RValue<Int4> rhs)
5106 {
5107 return RValue<Int4>(Nucleus::createAnd(lhs.value, rhs.value));
5108 }
5109
5110 RValue<Int4> operator|(RValue<Int4> lhs, RValue<Int4> rhs)
5111 {
5112 return RValue<Int4>(Nucleus::createOr(lhs.value, rhs.value));
5113 }
5114
5115 RValue<Int4> operator^(RValue<Int4> lhs, RValue<Int4> rhs)
5116 {
5117 return RValue<Int4>(Nucleus::createXor(lhs.value, rhs.value));
5118 }
5119
5120 RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs)
5121 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005122 return RValue<Int4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005123 }
5124
5125 RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs)
5126 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005127 return RValue<Int4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005128 }
5129
5130 RValue<Int4> operator<<(RValue<Int4> lhs, RValue<Int4> rhs)
5131 {
5132 return RValue<Int4>(Nucleus::createShl(lhs.value, rhs.value));
5133 }
5134
5135 RValue<Int4> operator>>(RValue<Int4> lhs, RValue<Int4> rhs)
5136 {
5137 return RValue<Int4>(Nucleus::createAShr(lhs.value, rhs.value));
5138 }
5139
Nicolas Capens96d4e092016-11-18 14:22:38 -05005140 RValue<Int4> operator+=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005141 {
5142 return lhs = lhs + rhs;
5143 }
5144
Nicolas Capens96d4e092016-11-18 14:22:38 -05005145 RValue<Int4> operator-=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005146 {
5147 return lhs = lhs - rhs;
5148 }
5149
Nicolas Capens96d4e092016-11-18 14:22:38 -05005150 RValue<Int4> operator*=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005151 {
5152 return lhs = lhs * rhs;
5153 }
5154
Nicolas Capens96d4e092016-11-18 14:22:38 -05005155// RValue<Int4> operator/=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005156// {
5157// return lhs = lhs / rhs;
5158// }
5159
Nicolas Capens96d4e092016-11-18 14:22:38 -05005160// RValue<Int4> operator%=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005161// {
5162// return lhs = lhs % rhs;
5163// }
5164
Nicolas Capens96d4e092016-11-18 14:22:38 -05005165 RValue<Int4> operator&=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005166 {
5167 return lhs = lhs & rhs;
5168 }
5169
Nicolas Capens96d4e092016-11-18 14:22:38 -05005170 RValue<Int4> operator|=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005171 {
5172 return lhs = lhs | rhs;
5173 }
5174
Nicolas Capens96d4e092016-11-18 14:22:38 -05005175 RValue<Int4> operator^=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005176 {
5177 return lhs = lhs ^ rhs;
5178 }
5179
Nicolas Capens96d4e092016-11-18 14:22:38 -05005180 RValue<Int4> operator<<=(Int4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005181 {
5182 return lhs = lhs << rhs;
5183 }
5184
Nicolas Capens96d4e092016-11-18 14:22:38 -05005185 RValue<Int4> operator>>=(Int4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005186 {
5187 return lhs = lhs >> rhs;
5188 }
5189
5190 RValue<Int4> operator+(RValue<Int4> val)
5191 {
5192 return val;
5193 }
5194
5195 RValue<Int4> operator-(RValue<Int4> val)
5196 {
5197 return RValue<Int4>(Nucleus::createNeg(val.value));
5198 }
5199
5200 RValue<Int4> operator~(RValue<Int4> val)
5201 {
5202 return RValue<Int4>(Nucleus::createNot(val.value));
5203 }
5204
5205 RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y)
5206 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005207 return RValue<Int4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005208 }
5209
5210 RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y)
5211 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005212 return RValue<Int4>(Nucleus::createICmpSLT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005213 }
5214
5215 RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y)
5216 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005217 return RValue<Int4>(Nucleus::createICmpSLE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005218 }
5219
5220 RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y)
5221 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005222 return RValue<Int4>(Nucleus::createICmpNE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005223 }
5224
5225 RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y)
5226 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005227 return RValue<Int4>(Nucleus::createICmpSGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005228 }
5229
5230 RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y)
5231 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005232 return RValue<Int4>(Nucleus::createICmpSGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005233 }
5234
5235 RValue<Int4> Max(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::Sle, 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> Min(RValue<Int4> x, RValue<Int4> y)
5249 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005250 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5251 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value);
5252 ::basicBlock->appendInst(cmp);
5253
5254 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5255 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5256 ::basicBlock->appendInst(select);
5257
5258 return RValue<Int4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005259 }
5260
5261 RValue<Int4> RoundInt(RValue<Float4> cast)
5262 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005263 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
Nicolas Capens9ca48d52017-01-14 12:52:55 -05005264 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5265 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5266 auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5267 nearbyint->addArg(cast.value);
5268 ::basicBlock->appendInst(nearbyint);
Nicolas Capensa8086512016-11-07 17:32:17 -05005269
5270 return RValue<Int4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005271 }
5272
5273 RValue<Short8> Pack(RValue<Int4> x, RValue<Int4> y)
5274 {
Nicolas Capensec54a172016-10-25 17:32:37 -04005275 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
5276 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5277 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5278 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
5279 pack->addArg(x.value);
5280 pack->addArg(y.value);
5281 ::basicBlock->appendInst(pack);
5282
5283 return RValue<Short8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005284 }
5285
5286 RValue<Int> Extract(RValue<Int4> x, int i)
5287 {
Nicolas Capense95d5342016-09-30 11:37:28 -04005288 return RValue<Int>(Nucleus::createExtractElement(x.value, Int::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005289 }
5290
5291 RValue<Int4> Insert(RValue<Int4> x, RValue<Int> element, int i)
5292 {
5293 return RValue<Int4>(Nucleus::createInsertElement(x.value, element.value, i));
5294 }
5295
5296 RValue<Int> SignMask(RValue<Int4> x)
5297 {
Nicolas Capensf2cb9df2016-10-21 17:26:13 -04005298 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
5299 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5300 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5301 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5302 movmsk->addArg(x.value);
5303 ::basicBlock->appendInst(movmsk);
5304
5305 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005306 }
5307
5308 RValue<Int4> Swizzle(RValue<Int4> x, unsigned char select)
5309 {
Nicolas Capense95d5342016-09-30 11:37:28 -04005310 return RValue<Int4>(createSwizzle4(x.value, select));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005311 }
5312
5313 Type *Int4::getType()
5314 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04005315 return T(Ice::IceType_v4i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005316 }
5317
5318 UInt4::UInt4(RValue<Float4> cast)
5319 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05005320 // Smallest positive value representable in UInt, but not in Int
5321 const unsigned int ustart = 0x80000000u;
5322 const float ustartf = float(ustart);
5323
5324 // Check if the value can be represented as an Int
5325 Int4 uiValue = CmpNLT(cast, Float4(ustartf));
5326 // If the value is too large, subtract ustart and re-add it after conversion.
5327 uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) |
5328 // Otherwise, just convert normally
5329 (~uiValue & Int4(cast));
5330 // If the value is negative, store 0, otherwise store the result of the conversion
5331 storeValue((~(As<Int4>(cast) >> 31) & uiValue).value);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005332 }
5333
Nicolas Capens598f8d82016-09-26 15:09:10 -04005334 UInt4::UInt4(int xyzw)
5335 {
5336 constant(xyzw, xyzw, xyzw, xyzw);
5337 }
5338
5339 UInt4::UInt4(int x, int yzw)
5340 {
5341 constant(x, yzw, yzw, yzw);
5342 }
5343
5344 UInt4::UInt4(int x, int y, int zw)
5345 {
5346 constant(x, y, zw, zw);
5347 }
5348
5349 UInt4::UInt4(int x, int y, int z, int w)
5350 {
5351 constant(x, y, z, w);
5352 }
5353
5354 void UInt4::constant(int x, int y, int z, int w)
5355 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04005356 int64_t constantVector[4] = {x, y, z, w};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04005357 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005358 }
5359
5360 UInt4::UInt4(RValue<UInt4> rhs)
5361 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005362 storeValue(rhs.value);
5363 }
5364
5365 UInt4::UInt4(const UInt4 &rhs)
5366 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005367 Value *value = rhs.loadValue();
5368 storeValue(value);
5369 }
5370
5371 UInt4::UInt4(const Reference<UInt4> &rhs)
5372 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005373 Value *value = rhs.loadValue();
5374 storeValue(value);
5375 }
5376
5377 UInt4::UInt4(RValue<Int4> rhs)
5378 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005379 storeValue(rhs.value);
5380 }
5381
5382 UInt4::UInt4(const Int4 &rhs)
5383 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005384 Value *value = rhs.loadValue();
5385 storeValue(value);
5386 }
5387
5388 UInt4::UInt4(const Reference<Int4> &rhs)
5389 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005390 Value *value = rhs.loadValue();
5391 storeValue(value);
5392 }
5393
5394 UInt4::UInt4(RValue<UInt2> lo, RValue<UInt2> hi)
5395 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05005396 int shuffle[4] = {0, 1, 4, 5}; // Real type is v4i32
5397 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
5398
5399 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005400 }
5401
Nicolas Capens96d4e092016-11-18 14:22:38 -05005402 RValue<UInt4> UInt4::operator=(RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005403 {
5404 storeValue(rhs.value);
5405
5406 return rhs;
5407 }
5408
Nicolas Capens96d4e092016-11-18 14:22:38 -05005409 RValue<UInt4> UInt4::operator=(const UInt4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005410 {
5411 Value *value = rhs.loadValue();
5412 storeValue(value);
5413
5414 return RValue<UInt4>(value);
5415 }
5416
Nicolas Capens96d4e092016-11-18 14:22:38 -05005417 RValue<UInt4> UInt4::operator=(const Reference<UInt4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005418 {
5419 Value *value = rhs.loadValue();
5420 storeValue(value);
5421
5422 return RValue<UInt4>(value);
5423 }
5424
5425 RValue<UInt4> operator+(RValue<UInt4> lhs, RValue<UInt4> rhs)
5426 {
5427 return RValue<UInt4>(Nucleus::createAdd(lhs.value, rhs.value));
5428 }
5429
5430 RValue<UInt4> operator-(RValue<UInt4> lhs, RValue<UInt4> rhs)
5431 {
5432 return RValue<UInt4>(Nucleus::createSub(lhs.value, rhs.value));
5433 }
5434
5435 RValue<UInt4> operator*(RValue<UInt4> lhs, RValue<UInt4> rhs)
5436 {
5437 return RValue<UInt4>(Nucleus::createMul(lhs.value, rhs.value));
5438 }
5439
5440 RValue<UInt4> operator/(RValue<UInt4> lhs, RValue<UInt4> rhs)
5441 {
5442 return RValue<UInt4>(Nucleus::createUDiv(lhs.value, rhs.value));
5443 }
5444
5445 RValue<UInt4> operator%(RValue<UInt4> lhs, RValue<UInt4> rhs)
5446 {
5447 return RValue<UInt4>(Nucleus::createURem(lhs.value, rhs.value));
5448 }
5449
5450 RValue<UInt4> operator&(RValue<UInt4> lhs, RValue<UInt4> rhs)
5451 {
5452 return RValue<UInt4>(Nucleus::createAnd(lhs.value, rhs.value));
5453 }
5454
5455 RValue<UInt4> operator|(RValue<UInt4> lhs, RValue<UInt4> rhs)
5456 {
5457 return RValue<UInt4>(Nucleus::createOr(lhs.value, rhs.value));
5458 }
5459
5460 RValue<UInt4> operator^(RValue<UInt4> lhs, RValue<UInt4> rhs)
5461 {
5462 return RValue<UInt4>(Nucleus::createXor(lhs.value, rhs.value));
5463 }
5464
5465 RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs)
5466 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005467 return RValue<UInt4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005468 }
5469
5470 RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs)
5471 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005472 return RValue<UInt4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005473 }
5474
5475 RValue<UInt4> operator<<(RValue<UInt4> lhs, RValue<UInt4> rhs)
5476 {
5477 return RValue<UInt4>(Nucleus::createShl(lhs.value, rhs.value));
5478 }
5479
5480 RValue<UInt4> operator>>(RValue<UInt4> lhs, RValue<UInt4> rhs)
5481 {
5482 return RValue<UInt4>(Nucleus::createLShr(lhs.value, rhs.value));
5483 }
5484
Nicolas Capens96d4e092016-11-18 14:22:38 -05005485 RValue<UInt4> operator+=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005486 {
5487 return lhs = lhs + rhs;
5488 }
5489
Nicolas Capens96d4e092016-11-18 14:22:38 -05005490 RValue<UInt4> operator-=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005491 {
5492 return lhs = lhs - rhs;
5493 }
5494
Nicolas Capens96d4e092016-11-18 14:22:38 -05005495 RValue<UInt4> operator*=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005496 {
5497 return lhs = lhs * rhs;
5498 }
5499
Nicolas Capens96d4e092016-11-18 14:22:38 -05005500// RValue<UInt4> operator/=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005501// {
5502// return lhs = lhs / rhs;
5503// }
5504
Nicolas Capens96d4e092016-11-18 14:22:38 -05005505// RValue<UInt4> operator%=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005506// {
5507// return lhs = lhs % rhs;
5508// }
5509
Nicolas Capens96d4e092016-11-18 14:22:38 -05005510 RValue<UInt4> operator&=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005511 {
5512 return lhs = lhs & rhs;
5513 }
5514
Nicolas Capens96d4e092016-11-18 14:22:38 -05005515 RValue<UInt4> operator|=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005516 {
5517 return lhs = lhs | rhs;
5518 }
5519
Nicolas Capens96d4e092016-11-18 14:22:38 -05005520 RValue<UInt4> operator^=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005521 {
5522 return lhs = lhs ^ rhs;
5523 }
5524
Nicolas Capens96d4e092016-11-18 14:22:38 -05005525 RValue<UInt4> operator<<=(UInt4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005526 {
5527 return lhs = lhs << rhs;
5528 }
5529
Nicolas Capens96d4e092016-11-18 14:22:38 -05005530 RValue<UInt4> operator>>=(UInt4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005531 {
5532 return lhs = lhs >> rhs;
5533 }
5534
5535 RValue<UInt4> operator+(RValue<UInt4> val)
5536 {
5537 return val;
5538 }
5539
5540 RValue<UInt4> operator-(RValue<UInt4> val)
5541 {
5542 return RValue<UInt4>(Nucleus::createNeg(val.value));
5543 }
5544
5545 RValue<UInt4> operator~(RValue<UInt4> val)
5546 {
5547 return RValue<UInt4>(Nucleus::createNot(val.value));
5548 }
5549
5550 RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y)
5551 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005552 return RValue<UInt4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005553 }
5554
5555 RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y)
5556 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005557 return RValue<UInt4>(Nucleus::createICmpULT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005558 }
5559
5560 RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y)
5561 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005562 return RValue<UInt4>(Nucleus::createICmpULE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005563 }
5564
5565 RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y)
5566 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005567 return RValue<UInt4>(Nucleus::createICmpNE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005568 }
5569
5570 RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y)
5571 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005572 return RValue<UInt4>(Nucleus::createICmpUGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005573 }
5574
5575 RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y)
5576 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005577 return RValue<UInt4>(Nucleus::createICmpUGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005578 }
5579
5580 RValue<UInt4> Max(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::Ule, 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<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y)
5594 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005595 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5596 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value);
5597 ::basicBlock->appendInst(cmp);
5598
5599 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5600 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5601 ::basicBlock->appendInst(select);
5602
5603 return RValue<UInt4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005604 }
5605
5606 RValue<UShort8> Pack(RValue<UInt4> x, RValue<UInt4> y)
5607 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05005608 if(CPUID::SSE4_1)
5609 {
5610 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
5611 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5612 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5613 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
5614 pack->addArg(x.value);
5615 pack->addArg(y.value);
5616 ::basicBlock->appendInst(pack);
Nicolas Capensec54a172016-10-25 17:32:37 -04005617
Nicolas Capens9ca48d52017-01-14 12:52:55 -05005618 return RValue<UShort8>(V(result));
5619 }
5620 else
5621 {
5622 RValue<Int4> sx = As<Int4>(x);
5623 RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000);
5624
5625 RValue<Int4> sy = As<Int4>(y);
5626 RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000);
5627
5628 return As<UShort8>(Pack(bx, by) + Short8(0x8000u));
5629 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04005630 }
5631
5632 Type *UInt4::getType()
5633 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04005634 return T(Ice::IceType_v4i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005635 }
5636
5637 Float::Float(RValue<Int> cast)
5638 {
5639 Value *integer = Nucleus::createSIToFP(cast.value, Float::getType());
5640
5641 storeValue(integer);
5642 }
5643
Nicolas Capens598f8d82016-09-26 15:09:10 -04005644 Float::Float(float x)
5645 {
5646 storeValue(Nucleus::createConstantFloat(x));
5647 }
5648
5649 Float::Float(RValue<Float> rhs)
5650 {
5651 storeValue(rhs.value);
5652 }
5653
5654 Float::Float(const Float &rhs)
5655 {
5656 Value *value = rhs.loadValue();
5657 storeValue(value);
5658 }
5659
5660 Float::Float(const Reference<Float> &rhs)
5661 {
5662 Value *value = rhs.loadValue();
5663 storeValue(value);
5664 }
5665
Nicolas Capens96d4e092016-11-18 14:22:38 -05005666 RValue<Float> Float::operator=(RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005667 {
5668 storeValue(rhs.value);
5669
5670 return rhs;
5671 }
5672
Nicolas Capens96d4e092016-11-18 14:22:38 -05005673 RValue<Float> Float::operator=(const Float &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005674 {
5675 Value *value = rhs.loadValue();
5676 storeValue(value);
5677
5678 return RValue<Float>(value);
5679 }
5680
Nicolas Capens96d4e092016-11-18 14:22:38 -05005681 RValue<Float> Float::operator=(const Reference<Float> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005682 {
5683 Value *value = rhs.loadValue();
5684 storeValue(value);
5685
5686 return RValue<Float>(value);
5687 }
5688
5689 RValue<Float> operator+(RValue<Float> lhs, RValue<Float> rhs)
5690 {
5691 return RValue<Float>(Nucleus::createFAdd(lhs.value, rhs.value));
5692 }
5693
5694 RValue<Float> operator-(RValue<Float> lhs, RValue<Float> rhs)
5695 {
5696 return RValue<Float>(Nucleus::createFSub(lhs.value, rhs.value));
5697 }
5698
5699 RValue<Float> operator*(RValue<Float> lhs, RValue<Float> rhs)
5700 {
5701 return RValue<Float>(Nucleus::createFMul(lhs.value, rhs.value));
5702 }
5703
5704 RValue<Float> operator/(RValue<Float> lhs, RValue<Float> rhs)
5705 {
5706 return RValue<Float>(Nucleus::createFDiv(lhs.value, rhs.value));
5707 }
5708
Nicolas Capens96d4e092016-11-18 14:22:38 -05005709 RValue<Float> operator+=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005710 {
5711 return lhs = lhs + rhs;
5712 }
5713
Nicolas Capens96d4e092016-11-18 14:22:38 -05005714 RValue<Float> operator-=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005715 {
5716 return lhs = lhs - rhs;
5717 }
5718
Nicolas Capens96d4e092016-11-18 14:22:38 -05005719 RValue<Float> operator*=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005720 {
5721 return lhs = lhs * rhs;
5722 }
5723
Nicolas Capens96d4e092016-11-18 14:22:38 -05005724 RValue<Float> operator/=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005725 {
5726 return lhs = lhs / rhs;
5727 }
5728
5729 RValue<Float> operator+(RValue<Float> val)
5730 {
5731 return val;
5732 }
5733
5734 RValue<Float> operator-(RValue<Float> val)
5735 {
5736 return RValue<Float>(Nucleus::createFNeg(val.value));
5737 }
5738
5739 RValue<Bool> operator<(RValue<Float> lhs, RValue<Float> rhs)
5740 {
5741 return RValue<Bool>(Nucleus::createFCmpOLT(lhs.value, rhs.value));
5742 }
5743
5744 RValue<Bool> operator<=(RValue<Float> lhs, RValue<Float> rhs)
5745 {
5746 return RValue<Bool>(Nucleus::createFCmpOLE(lhs.value, rhs.value));
5747 }
5748
5749 RValue<Bool> operator>(RValue<Float> lhs, RValue<Float> rhs)
5750 {
5751 return RValue<Bool>(Nucleus::createFCmpOGT(lhs.value, rhs.value));
5752 }
5753
5754 RValue<Bool> operator>=(RValue<Float> lhs, RValue<Float> rhs)
5755 {
5756 return RValue<Bool>(Nucleus::createFCmpOGE(lhs.value, rhs.value));
5757 }
5758
5759 RValue<Bool> operator!=(RValue<Float> lhs, RValue<Float> rhs)
5760 {
5761 return RValue<Bool>(Nucleus::createFCmpONE(lhs.value, rhs.value));
5762 }
5763
5764 RValue<Bool> operator==(RValue<Float> lhs, RValue<Float> rhs)
5765 {
5766 return RValue<Bool>(Nucleus::createFCmpOEQ(lhs.value, rhs.value));
5767 }
5768
5769 RValue<Float> Abs(RValue<Float> x)
5770 {
5771 return IfThenElse(x > 0.0f, x, -x);
5772 }
5773
5774 RValue<Float> Max(RValue<Float> x, RValue<Float> y)
5775 {
5776 return IfThenElse(x > y, x, y);
5777 }
5778
5779 RValue<Float> Min(RValue<Float> x, RValue<Float> y)
5780 {
5781 return IfThenElse(x < y, x, y);
5782 }
5783
5784 RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2)
5785 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04005786 return 1.0f / x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005787 }
5788
5789 RValue<Float> RcpSqrt_pp(RValue<Float> x)
5790 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04005791 return Rcp_pp(Sqrt(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005792 }
5793
5794 RValue<Float> Sqrt(RValue<Float> x)
5795 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04005796 Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32);
5797 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5798 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5799 auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5800 sqrt->addArg(x.value);
5801 ::basicBlock->appendInst(sqrt);
5802
5803 return RValue<Float>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005804 }
5805
5806 RValue<Float> Round(RValue<Float> x)
5807 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005808 return Float4(Round(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005809 }
5810
5811 RValue<Float> Trunc(RValue<Float> x)
5812 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005813 return Float4(Trunc(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005814 }
5815
5816 RValue<Float> Frac(RValue<Float> x)
5817 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005818 return Float4(Frac(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005819 }
5820
5821 RValue<Float> Floor(RValue<Float> x)
5822 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005823 return Float4(Floor(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005824 }
5825
5826 RValue<Float> Ceil(RValue<Float> x)
5827 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005828 return Float4(Ceil(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005829 }
5830
5831 Type *Float::getType()
5832 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04005833 return T(Ice::IceType_f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005834 }
5835
5836 Float2::Float2(RValue<Float4> cast)
5837 {
Nicolas Capens22008782016-10-20 01:11:47 -04005838 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005839 }
5840
5841 Type *Float2::getType()
5842 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04005843 return T(Type_v2f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005844 }
5845
Nicolas Capensa25311a2017-01-16 17:19:00 -05005846 Float4::Float4(RValue<Byte4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005847 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005848 Value *a = Int4(cast).loadValue();
5849 Value *xyzw = Nucleus::createSIToFP(a, Float4::getType());
5850
5851 storeValue(xyzw);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005852 }
5853
Nicolas Capensa25311a2017-01-16 17:19:00 -05005854 Float4::Float4(RValue<SByte4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005855 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005856 Value *a = Int4(cast).loadValue();
5857 Value *xyzw = Nucleus::createSIToFP(a, Float4::getType());
5858
5859 storeValue(xyzw);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005860 }
5861
Nicolas Capensa25311a2017-01-16 17:19:00 -05005862 Float4::Float4(RValue<Short4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005863 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005864 Int4 c(cast);
5865 storeValue(Nucleus::createSIToFP(RValue<Int4>(c).value, Float4::getType()));
5866 }
5867
Nicolas Capensa25311a2017-01-16 17:19:00 -05005868 Float4::Float4(RValue<UShort4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005869 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005870 Int4 c(cast);
5871 storeValue(Nucleus::createSIToFP(RValue<Int4>(c).value, Float4::getType()));
5872 }
5873
Nicolas Capensa25311a2017-01-16 17:19:00 -05005874 Float4::Float4(RValue<Int4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005875 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005876 Value *xyzw = Nucleus::createSIToFP(cast.value, Float4::getType());
5877
5878 storeValue(xyzw);
5879 }
5880
Nicolas Capensa25311a2017-01-16 17:19:00 -05005881 Float4::Float4(RValue<UInt4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005882 {
Nicolas Capens96445fe2016-12-15 14:45:13 -05005883 RValue<Float4> result = Float4(Int4(cast & UInt4(0x7FFFFFFF))) +
5884 As<Float4>((As<Int4>(cast) >> 31) & As<Int4>(Float4(0x80000000u)));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005885
Nicolas Capens96445fe2016-12-15 14:45:13 -05005886 storeValue(result.value);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005887 }
5888
Nicolas Capensa25311a2017-01-16 17:19:00 -05005889 Float4::Float4() : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005890 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005891 }
5892
Nicolas Capensa25311a2017-01-16 17:19:00 -05005893 Float4::Float4(float xyzw) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005894 {
5895 constant(xyzw, xyzw, xyzw, xyzw);
5896 }
5897
Nicolas Capensa25311a2017-01-16 17:19:00 -05005898 Float4::Float4(float x, float yzw) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005899 {
5900 constant(x, yzw, yzw, yzw);
5901 }
5902
Nicolas Capensa25311a2017-01-16 17:19:00 -05005903 Float4::Float4(float x, float y, float zw) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005904 {
5905 constant(x, y, zw, zw);
5906 }
5907
Nicolas Capensa25311a2017-01-16 17:19:00 -05005908 Float4::Float4(float x, float y, float z, float w) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005909 {
5910 constant(x, y, z, w);
5911 }
5912
5913 void Float4::constant(float x, float y, float z, float w)
5914 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04005915 double constantVector[4] = {x, y, z, w};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04005916 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005917 }
5918
Nicolas Capensa25311a2017-01-16 17:19:00 -05005919 Float4::Float4(RValue<Float4> rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005920 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005921 storeValue(rhs.value);
5922 }
5923
Nicolas Capensa25311a2017-01-16 17:19:00 -05005924 Float4::Float4(const Float4 &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005925 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005926 Value *value = rhs.loadValue();
5927 storeValue(value);
5928 }
5929
Nicolas Capensa25311a2017-01-16 17:19:00 -05005930 Float4::Float4(const Reference<Float4> &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005931 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005932 Value *value = rhs.loadValue();
5933 storeValue(value);
5934 }
5935
Nicolas Capensa25311a2017-01-16 17:19:00 -05005936 Float4::Float4(RValue<Float> rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005937 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08005938 Value *vector = Nucleus::createBitCast(rhs.value, Float4::getType());
Nicolas Capensd4227962016-11-09 14:24:25 -05005939
5940 int swizzle[4] = {0, 0, 0, 0};
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08005941 Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle);
Nicolas Capensd4227962016-11-09 14:24:25 -05005942
5943 storeValue(replicate);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005944 }
5945
Nicolas Capensa25311a2017-01-16 17:19:00 -05005946 Float4::Float4(const Float &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005947 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005948 *this = RValue<Float>(rhs.loadValue());
5949 }
5950
Nicolas Capensa25311a2017-01-16 17:19:00 -05005951 Float4::Float4(const Reference<Float> &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005952 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005953 *this = RValue<Float>(rhs.loadValue());
5954 }
5955
Nicolas Capens96d4e092016-11-18 14:22:38 -05005956 RValue<Float4> Float4::operator=(float x)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005957 {
5958 return *this = Float4(x, x, x, x);
5959 }
5960
Nicolas Capens96d4e092016-11-18 14:22:38 -05005961 RValue<Float4> Float4::operator=(RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005962 {
5963 storeValue(rhs.value);
5964
5965 return rhs;
5966 }
5967
Nicolas Capens96d4e092016-11-18 14:22:38 -05005968 RValue<Float4> Float4::operator=(const Float4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005969 {
5970 Value *value = rhs.loadValue();
5971 storeValue(value);
5972
5973 return RValue<Float4>(value);
5974 }
5975
Nicolas Capens96d4e092016-11-18 14:22:38 -05005976 RValue<Float4> Float4::operator=(const Reference<Float4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005977 {
5978 Value *value = rhs.loadValue();
5979 storeValue(value);
5980
5981 return RValue<Float4>(value);
5982 }
5983
Nicolas Capens96d4e092016-11-18 14:22:38 -05005984 RValue<Float4> Float4::operator=(RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005985 {
5986 return *this = Float4(rhs);
5987 }
5988
Nicolas Capens96d4e092016-11-18 14:22:38 -05005989 RValue<Float4> Float4::operator=(const Float &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005990 {
5991 return *this = Float4(rhs);
5992 }
5993
Nicolas Capens96d4e092016-11-18 14:22:38 -05005994 RValue<Float4> Float4::operator=(const Reference<Float> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005995 {
5996 return *this = Float4(rhs);
5997 }
5998
5999 RValue<Float4> operator+(RValue<Float4> lhs, RValue<Float4> rhs)
6000 {
6001 return RValue<Float4>(Nucleus::createFAdd(lhs.value, rhs.value));
6002 }
6003
6004 RValue<Float4> operator-(RValue<Float4> lhs, RValue<Float4> rhs)
6005 {
6006 return RValue<Float4>(Nucleus::createFSub(lhs.value, rhs.value));
6007 }
6008
6009 RValue<Float4> operator*(RValue<Float4> lhs, RValue<Float4> rhs)
6010 {
6011 return RValue<Float4>(Nucleus::createFMul(lhs.value, rhs.value));
6012 }
6013
6014 RValue<Float4> operator/(RValue<Float4> lhs, RValue<Float4> rhs)
6015 {
6016 return RValue<Float4>(Nucleus::createFDiv(lhs.value, rhs.value));
6017 }
6018
6019 RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs)
6020 {
6021 return RValue<Float4>(Nucleus::createFRem(lhs.value, rhs.value));
6022 }
6023
Nicolas Capens96d4e092016-11-18 14:22:38 -05006024 RValue<Float4> operator+=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006025 {
6026 return lhs = lhs + rhs;
6027 }
6028
Nicolas Capens96d4e092016-11-18 14:22:38 -05006029 RValue<Float4> operator-=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006030 {
6031 return lhs = lhs - rhs;
6032 }
6033
Nicolas Capens96d4e092016-11-18 14:22:38 -05006034 RValue<Float4> operator*=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006035 {
6036 return lhs = lhs * rhs;
6037 }
6038
Nicolas Capens96d4e092016-11-18 14:22:38 -05006039 RValue<Float4> operator/=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006040 {
6041 return lhs = lhs / rhs;
6042 }
6043
Nicolas Capens96d4e092016-11-18 14:22:38 -05006044 RValue<Float4> operator%=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006045 {
6046 return lhs = lhs % rhs;
6047 }
6048
6049 RValue<Float4> operator+(RValue<Float4> val)
6050 {
6051 return val;
6052 }
6053
6054 RValue<Float4> operator-(RValue<Float4> val)
6055 {
6056 return RValue<Float4>(Nucleus::createFNeg(val.value));
6057 }
6058
6059 RValue<Float4> Abs(RValue<Float4> x)
6060 {
Nicolas Capens84272242016-11-09 13:31:06 -05006061 Value *vector = Nucleus::createBitCast(x.value, Int4::getType());
6062 int64_t constantVector[4] = {0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF};
6063 Value *result = Nucleus::createAnd(vector, V(Nucleus::createConstantVector(constantVector, Int4::getType())));
6064
6065 return As<Float4>(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006066 }
6067
6068 RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y)
6069 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006070 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05006071 auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Ogt, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006072 ::basicBlock->appendInst(cmp);
6073
6074 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05006075 auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006076 ::basicBlock->appendInst(select);
6077
6078 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006079 }
6080
6081 RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y)
6082 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006083 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05006084 auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Olt, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006085 ::basicBlock->appendInst(cmp);
6086
6087 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05006088 auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006089 ::basicBlock->appendInst(select);
6090
6091 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006092 }
6093
6094 RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2)
6095 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04006096 return Float4(1.0f) / x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04006097 }
6098
6099 RValue<Float4> RcpSqrt_pp(RValue<Float4> x)
6100 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04006101 return Rcp_pp(Sqrt(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006102 }
6103
6104 RValue<Float4> Sqrt(RValue<Float4> x)
6105 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04006106 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6107 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6108 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6109 auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
6110 sqrt->addArg(x.value);
6111 ::basicBlock->appendInst(sqrt);
6112
6113 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006114 }
6115
Nicolas Capensc94ab742016-11-08 15:15:31 -05006116 RValue<Float4> Insert(RValue<Float4> x, RValue<Float> element, int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006117 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05006118 return RValue<Float4>(Nucleus::createInsertElement(x.value, element.value, i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006119 }
6120
6121 RValue<Float> Extract(RValue<Float4> x, int i)
6122 {
Nicolas Capense95d5342016-09-30 11:37:28 -04006123 return RValue<Float>(Nucleus::createExtractElement(x.value, Float::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006124 }
6125
6126 RValue<Float4> Swizzle(RValue<Float4> x, unsigned char select)
6127 {
Nicolas Capense95d5342016-09-30 11:37:28 -04006128 return RValue<Float4>(createSwizzle4(x.value, select));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006129 }
6130
6131 RValue<Float4> ShuffleLowHigh(RValue<Float4> x, RValue<Float4> y, unsigned char imm)
6132 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04006133 int shuffle[4] =
6134 {
6135 ((imm >> 0) & 0x03) + 0,
6136 ((imm >> 2) & 0x03) + 0,
6137 ((imm >> 4) & 0x03) + 4,
6138 ((imm >> 6) & 0x03) + 4,
6139 };
6140
6141 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006142 }
6143
6144 RValue<Float4> UnpackLow(RValue<Float4> x, RValue<Float4> y)
6145 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04006146 int shuffle[4] = {0, 4, 1, 5};
6147 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006148 }
6149
6150 RValue<Float4> UnpackHigh(RValue<Float4> x, RValue<Float4> y)
6151 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04006152 int shuffle[4] = {2, 6, 3, 7};
6153 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006154 }
6155
6156 RValue<Float4> Mask(Float4 &lhs, RValue<Float4> rhs, unsigned char select)
6157 {
6158 Value *vector = lhs.loadValue();
Nicolas Capensa4c30b02016-11-08 15:43:17 -05006159 Value *result = createMask4(vector, rhs.value, select);
6160 lhs.storeValue(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006161
Nicolas Capensa4c30b02016-11-08 15:43:17 -05006162 return RValue<Float4>(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006163 }
6164
6165 RValue<Int> SignMask(RValue<Float4> x)
6166 {
Nicolas Capensf2cb9df2016-10-21 17:26:13 -04006167 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
6168 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6169 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6170 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
6171 movmsk->addArg(x.value);
6172 ::basicBlock->appendInst(movmsk);
6173
6174 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006175 }
6176
6177 RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y)
6178 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006179 return RValue<Int4>(Nucleus::createFCmpOEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006180 }
6181
6182 RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y)
6183 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006184 return RValue<Int4>(Nucleus::createFCmpOLT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006185 }
6186
6187 RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y)
6188 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006189 return RValue<Int4>(Nucleus::createFCmpOLE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006190 }
6191
6192 RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y)
6193 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006194 return RValue<Int4>(Nucleus::createFCmpONE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006195 }
6196
6197 RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y)
6198 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006199 return RValue<Int4>(Nucleus::createFCmpOGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006200 }
6201
6202 RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y)
6203 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006204 return RValue<Int4>(Nucleus::createFCmpOGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006205 }
6206
6207 RValue<Float4> Round(RValue<Float4> x)
6208 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006209 if(CPUID::SSE4_1)
6210 {
6211 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6212 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6213 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6214 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6215 round->addArg(x.value);
6216 round->addArg(::context->getConstantInt32(0));
6217 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006218
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006219 return RValue<Float4>(V(result));
6220 }
6221 else
6222 {
6223 return Float4(RoundInt(x));
6224 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006225 }
6226
6227 RValue<Float4> Trunc(RValue<Float4> x)
6228 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006229 if(CPUID::SSE4_1)
6230 {
6231 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6232 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6233 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6234 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6235 round->addArg(x.value);
6236 round->addArg(::context->getConstantInt32(3));
6237 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006238
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006239 return RValue<Float4>(V(result));
6240 }
6241 else
6242 {
6243 return Float4(Int4(x));
6244 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006245 }
6246
6247 RValue<Float4> Frac(RValue<Float4> x)
6248 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006249 if(CPUID::SSE4_1)
6250 {
6251 return x - Floor(x);
6252 }
6253 else
6254 {
6255 Float4 frc = x - Float4(Int4(x)); // Signed fractional part
6256
6257 return frc + As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1, 1, 1, 1)));
6258 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006259 }
6260
6261 RValue<Float4> Floor(RValue<Float4> x)
6262 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006263 if(CPUID::SSE4_1)
6264 {
6265 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6266 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6267 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6268 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6269 round->addArg(x.value);
6270 round->addArg(::context->getConstantInt32(1));
6271 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006272
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006273 return RValue<Float4>(V(result));
6274 }
6275 else
6276 {
6277 return x - Frac(x);
6278 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006279 }
6280
6281 RValue<Float4> Ceil(RValue<Float4> x)
6282 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006283 if(CPUID::SSE4_1)
6284 {
6285 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6286 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6287 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6288 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6289 round->addArg(x.value);
6290 round->addArg(::context->getConstantInt32(2));
6291 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006292
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006293 return RValue<Float4>(V(result));
6294 }
6295 else
6296 {
6297 return -Floor(-x);
6298 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006299 }
6300
6301 Type *Float4::getType()
6302 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04006303 return T(Ice::IceType_v4f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006304 }
6305
6306 RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, int offset)
6307 {
Nicolas Capens8820f642016-09-30 04:42:43 -04006308 return lhs + RValue<Int>(Nucleus::createConstantInt(offset));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006309 }
6310
6311 RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<Int> offset)
6312 {
Nicolas Capensd294def2017-01-26 17:44:37 -08006313 return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, false));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006314 }
6315
6316 RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<UInt> offset)
6317 {
Nicolas Capensd294def2017-01-26 17:44:37 -08006318 return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, true));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006319 }
6320
Nicolas Capens96d4e092016-11-18 14:22:38 -05006321 RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, int offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006322 {
6323 return lhs = lhs + offset;
6324 }
6325
Nicolas Capens96d4e092016-11-18 14:22:38 -05006326 RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, RValue<Int> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006327 {
6328 return lhs = lhs + offset;
6329 }
6330
Nicolas Capens96d4e092016-11-18 14:22:38 -05006331 RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, RValue<UInt> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006332 {
6333 return lhs = lhs + offset;
6334 }
6335
6336 RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, int offset)
6337 {
6338 return lhs + -offset;
6339 }
6340
6341 RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, RValue<Int> offset)
6342 {
6343 return lhs + -offset;
6344 }
6345
6346 RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, RValue<UInt> offset)
6347 {
6348 return lhs + -offset;
6349 }
6350
Nicolas Capens96d4e092016-11-18 14:22:38 -05006351 RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, int offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006352 {
6353 return lhs = lhs - offset;
6354 }
6355
Nicolas Capens96d4e092016-11-18 14:22:38 -05006356 RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, RValue<Int> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006357 {
6358 return lhs = lhs - offset;
6359 }
6360
Nicolas Capens96d4e092016-11-18 14:22:38 -05006361 RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, RValue<UInt> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006362 {
6363 return lhs = lhs - offset;
6364 }
6365
6366 void Return()
6367 {
6368 Nucleus::createRetVoid();
6369 Nucleus::setInsertBlock(Nucleus::createBasicBlock());
6370 Nucleus::createUnreachable();
6371 }
6372
Nicolas Capenseb253d02016-11-18 14:40:40 -05006373 void Return(RValue<Int> ret)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006374 {
Nicolas Capenseb253d02016-11-18 14:40:40 -05006375 Nucleus::createRet(ret.value);
Nicolas Capensfdcca2d2016-10-20 11:31:36 -04006376 Nucleus::setInsertBlock(Nucleus::createBasicBlock());
6377 Nucleus::createUnreachable();
Nicolas Capens598f8d82016-09-26 15:09:10 -04006378 }
6379
Nicolas Capens598f8d82016-09-26 15:09:10 -04006380 bool branch(RValue<Bool> cmp, BasicBlock *bodyBB, BasicBlock *endBB)
6381 {
6382 Nucleus::createCondBr(cmp.value, bodyBB, endBB);
6383 Nucleus::setInsertBlock(bodyBB);
6384
6385 return true;
6386 }
6387
Nicolas Capens598f8d82016-09-26 15:09:10 -04006388 RValue<Long> Ticks()
6389 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04006390 assert(false && "UNIMPLEMENTED"); return RValue<Long>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006391 }
6392}