blob: 1a0ac08c16a0a02b6c2afb32334a79a22f4392b4 [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:
Nicolas Capensf7b75882017-04-26 09:30:47 -040080 const static bool ARM;
Nicolas Capensccd5ecb2017-01-14 12:52:55 -050081 const static bool SSE4_1;
82
83 private:
84 static void cpuid(int registers[4], int info)
85 {
Nicolas Capens47dc8672017-04-25 12:54:39 -040086 #if defined(__i386__) || defined(__x86_64__)
87 #if defined(_WIN32)
88 __cpuid(registers, info);
89 #else
90 __asm volatile("cpuid": "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]): "a" (info));
91 #endif
Nicolas Capensccd5ecb2017-01-14 12:52:55 -050092 #else
Nicolas Capens47dc8672017-04-25 12:54:39 -040093 registers[0] = 0;
94 registers[1] = 0;
95 registers[2] = 0;
96 registers[3] = 0;
Nicolas Capensccd5ecb2017-01-14 12:52:55 -050097 #endif
98 }
99
Nicolas Capensf7b75882017-04-26 09:30:47 -0400100 static bool detectARM()
101 {
102 #if defined(__arm__)
103 return true;
104 #elif defined(__i386__) || defined(__x86_64__)
105 return false;
106 #else
107 #error "Unknown architecture"
108 #endif
109 }
110
Nicolas Capensccd5ecb2017-01-14 12:52:55 -0500111 static bool detectSSE4_1()
112 {
Nicolas Capens47dc8672017-04-25 12:54:39 -0400113 #if defined(__i386__) || defined(__x86_64__)
114 int registers[4];
115 cpuid(registers, 1);
116 return (registers[2] & 0x00080000) != 0;
117 #else
118 return false;
119 #endif
Nicolas Capensccd5ecb2017-01-14 12:52:55 -0500120 }
121 };
122
Nicolas Capensf7b75882017-04-26 09:30:47 -0400123 const bool CPUID::ARM = CPUID::detectARM();
Nicolas Capensccd5ecb2017-01-14 12:52:55 -0500124 const bool CPUID::SSE4_1 = CPUID::detectSSE4_1();
Nicolas Capensf7b75882017-04-26 09:30:47 -0400125 const bool emulateIntrinsics = CPUID::ARM;
Nicolas Capens2d8c3702017-07-25 13:56:46 -0400126 const bool emulateMismatchedBitCast = CPUID::ARM;
Nicolas Capensccd5ecb2017-01-14 12:52:55 -0500127}
128
Nicolas Capens598f8d82016-09-26 15:09:10 -0400129namespace sw
130{
Nicolas Capens23d99a42016-09-30 14:57:16 -0400131 enum EmulatedType
132 {
133 EmulatedShift = 16,
134 EmulatedV2 = 2 << EmulatedShift,
135 EmulatedV4 = 4 << EmulatedShift,
136 EmulatedV8 = 8 << EmulatedShift,
137 EmulatedBits = EmulatedV2 | EmulatedV4 | EmulatedV8,
138
139 Type_v2i32 = Ice::IceType_v4i32 | EmulatedV2,
140 Type_v4i16 = Ice::IceType_v8i16 | EmulatedV4,
141 Type_v2i16 = Ice::IceType_v8i16 | EmulatedV2,
142 Type_v8i8 = Ice::IceType_v16i8 | EmulatedV8,
143 Type_v4i8 = Ice::IceType_v16i8 | EmulatedV4,
Nicolas Capens4cfd4572016-10-20 01:00:19 -0400144 Type_v2f32 = Ice::IceType_v4f32 | EmulatedV2,
Nicolas Capens23d99a42016-09-30 14:57:16 -0400145 };
146
Nicolas Capens15060bb2016-12-05 22:17:19 -0500147 class Value : public Ice::Operand {};
Nicolas Capensb98fe5c2016-11-09 12:24:06 -0500148 class SwitchCases : public Ice::InstSwitch {};
Nicolas Capens598f8d82016-09-26 15:09:10 -0400149 class BasicBlock : public Ice::CfgNode {};
150
151 Ice::Type T(Type *t)
152 {
Alexis Hetu113e33a2017-01-19 10:49:19 -0500153 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 -0400154 return (Ice::Type)(reinterpret_cast<std::intptr_t>(t) & ~EmulatedBits);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400155 }
156
157 Type *T(Ice::Type t)
158 {
159 return reinterpret_cast<Type*>(t);
160 }
161
Nicolas Capens23d99a42016-09-30 14:57:16 -0400162 Type *T(EmulatedType t)
163 {
164 return reinterpret_cast<Type*>(t);
165 }
166
Nicolas Capens15060bb2016-12-05 22:17:19 -0500167 Value *V(Ice::Operand *v)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400168 {
169 return reinterpret_cast<Value*>(v);
170 }
171
Nicolas Capens611642a2016-09-28 16:45:04 -0400172 BasicBlock *B(Ice::CfgNode *b)
173 {
174 return reinterpret_cast<BasicBlock*>(b);
175 }
176
Nicolas Capens584088c2017-01-26 16:05:18 -0800177 static size_t typeSize(Type *type)
178 {
179 if(reinterpret_cast<std::intptr_t>(type) & EmulatedBits)
180 {
181 switch(reinterpret_cast<std::intptr_t>(type))
182 {
183 case Type_v2i32: return 8;
184 case Type_v4i16: return 8;
185 case Type_v2i16: return 4;
186 case Type_v8i8: return 8;
187 case Type_v4i8: return 4;
188 case Type_v2f32: return 8;
189 default: assert(false);
190 }
191 }
192
193 return Ice::typeWidthInBytes(T(type));
194 }
195
Nicolas Capens598f8d82016-09-26 15:09:10 -0400196 Optimization optimization[10] = {InstructionCombining, Disabled};
197
Nicolas Capens66478362016-10-13 15:36:36 -0400198 using ElfHeader = std::conditional<sizeof(void*) == 8, Elf64_Ehdr, Elf32_Ehdr>::type;
199 using SectionHeader = std::conditional<sizeof(void*) == 8, Elf64_Shdr, Elf32_Shdr>::type;
200
201 inline const SectionHeader *sectionHeader(const ElfHeader *elfHeader)
202 {
203 return reinterpret_cast<const SectionHeader*>((intptr_t)elfHeader + elfHeader->e_shoff);
204 }
Nicolas Capens87852e12016-11-24 14:45:06 -0500205
Nicolas Capens66478362016-10-13 15:36:36 -0400206 inline const SectionHeader *elfSection(const ElfHeader *elfHeader, int index)
207 {
208 return &sectionHeader(elfHeader)[index];
209 }
210
211 static void *relocateSymbol(const ElfHeader *elfHeader, const Elf32_Rel &relocation, const SectionHeader &relocationTable)
212 {
213 const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info);
Nicolas Capens87852e12016-11-24 14:45:06 -0500214
Nicolas Capens66478362016-10-13 15:36:36 -0400215 intptr_t address = (intptr_t)elfHeader + target->sh_offset;
216 int32_t *patchSite = (int*)(address + relocation.r_offset);
217 uint32_t index = relocation.getSymbol();
218 int table = relocationTable.sh_link;
219 void *symbolValue = nullptr;
Nicolas Capens87852e12016-11-24 14:45:06 -0500220
Nicolas Capens66478362016-10-13 15:36:36 -0400221 if(index != SHN_UNDEF)
222 {
223 if(table == SHN_UNDEF) return nullptr;
224 const SectionHeader *symbolTable = elfSection(elfHeader, table);
Nicolas Capens87852e12016-11-24 14:45:06 -0500225
Nicolas Capens66478362016-10-13 15:36:36 -0400226 uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize;
227 if(index >= symtab_entries)
228 {
229 assert(index < symtab_entries && "Symbol Index out of range");
230 return nullptr;
231 }
Nicolas Capens87852e12016-11-24 14:45:06 -0500232
Nicolas Capens66478362016-10-13 15:36:36 -0400233 intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset;
234 Elf32_Sym &symbol = ((Elf32_Sym*)symbolAddress)[index];
235 uint16_t section = symbol.st_shndx;
236
237 if(section != SHN_UNDEF && section < SHN_LORESERVE)
238 {
239 const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx);
240 symbolValue = reinterpret_cast<void*>((intptr_t)elfHeader + symbol.st_value + target->sh_offset);
241 }
242 else
243 {
244 return nullptr;
245 }
246 }
247
Nicolas Capensf110e4d2017-05-03 15:33:49 -0400248 if(CPUID::ARM)
249 {
250 switch(relocation.getType())
251 {
252 case R_ARM_NONE:
253 // No relocation
254 break;
255 case R_ARM_MOVW_ABS_NC:
256 {
257 uint32_t thumb = 0; // Calls to Thumb code not supported.
258 uint32_t lo = (uint32_t)(intptr_t)symbolValue | thumb;
259 *patchSite = (*patchSite & 0xFFF0F000) | ((lo & 0xF000) << 4) | (lo & 0x0FFF);
260 }
261 break;
262 case R_ARM_MOVT_ABS:
263 {
264 uint32_t hi = (uint32_t)(intptr_t)(symbolValue) >> 16;
265 *patchSite = (*patchSite & 0xFFF0F000) | ((hi & 0xF000) << 4) | (hi & 0x0FFF);
266 }
267 break;
268 default:
269 assert(false && "Unsupported relocation type");
270 return nullptr;
271 }
272 }
273 else
274 {
Nicolas Capens30cd7d42017-04-25 15:17:25 -0400275 switch(relocation.getType())
276 {
277 case R_386_NONE:
278 // No relocation
279 break;
280 case R_386_32:
281 *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite);
282 break;
283 // case R_386_PC32:
284 // *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite);
285 // break;
286 default:
287 assert(false && "Unsupported relocation type");
288 return nullptr;
289 }
Nicolas Capensf110e4d2017-05-03 15:33:49 -0400290 }
291
Nicolas Capens66478362016-10-13 15:36:36 -0400292
293 return symbolValue;
294 }
295
296 static void *relocateSymbol(const ElfHeader *elfHeader, const Elf64_Rela &relocation, const SectionHeader &relocationTable)
297 {
298 const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info);
Nicolas Capens87852e12016-11-24 14:45:06 -0500299
Nicolas Capens66478362016-10-13 15:36:36 -0400300 intptr_t address = (intptr_t)elfHeader + target->sh_offset;
301 int32_t *patchSite = (int*)(address + relocation.r_offset);
302 uint32_t index = relocation.getSymbol();
303 int table = relocationTable.sh_link;
304 void *symbolValue = nullptr;
305
306 if(index != SHN_UNDEF)
307 {
308 if(table == SHN_UNDEF) return nullptr;
309 const SectionHeader *symbolTable = elfSection(elfHeader, table);
Nicolas Capens87852e12016-11-24 14:45:06 -0500310
Nicolas Capens66478362016-10-13 15:36:36 -0400311 uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize;
312 if(index >= symtab_entries)
313 {
314 assert(index < symtab_entries && "Symbol Index out of range");
315 return nullptr;
316 }
Nicolas Capens87852e12016-11-24 14:45:06 -0500317
Nicolas Capens66478362016-10-13 15:36:36 -0400318 intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset;
319 Elf64_Sym &symbol = ((Elf64_Sym*)symbolAddress)[index];
320 uint16_t section = symbol.st_shndx;
321
322 if(section != SHN_UNDEF && section < SHN_LORESERVE)
323 {
324 const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx);
325 symbolValue = reinterpret_cast<void*>((intptr_t)elfHeader + symbol.st_value + target->sh_offset);
326 }
327 else
328 {
329 return nullptr;
330 }
331 }
332
Nicolas Capensf110e4d2017-05-03 15:33:49 -0400333 switch(relocation.getType())
334 {
335 case R_X86_64_NONE:
336 // No relocation
337 break;
338 case R_X86_64_64:
339 *(int64_t*)patchSite = (int64_t)((intptr_t)symbolValue + *(int64_t*)patchSite) + relocation.r_addend;
340 break;
341 case R_X86_64_PC32:
342 *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite) + relocation.r_addend;
343 break;
344 case R_X86_64_32S:
345 *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite) + relocation.r_addend;
346 break;
347 default:
348 assert(false && "Unsupported relocation type");
349 return nullptr;
350 }
Nicolas Capens66478362016-10-13 15:36:36 -0400351
352 return symbolValue;
353 }
354
Nicolas Capens1cc44382017-04-25 10:52:16 -0400355 void *loadImage(uint8_t *const elfImage, size_t &codeSize)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400356 {
Nicolas Capens598f8d82016-09-26 15:09:10 -0400357 ElfHeader *elfHeader = (ElfHeader*)elfImage;
358
359 if(!elfHeader->checkMagic())
360 {
361 return nullptr;
362 }
363
Nicolas Capens66478362016-10-13 15:36:36 -0400364 // Expect ELF bitness to match platform
Nicolas Capens65047112016-11-07 13:01:07 -0500365 assert(sizeof(void*) == 8 ? elfHeader->getFileClass() == ELFCLASS64 : elfHeader->getFileClass() == ELFCLASS32);
Nicolas Capens30cd7d42017-04-25 15:17:25 -0400366 #if defined(__i386__)
367 assert(sizeof(void*) == 4 && elfHeader->e_machine == EM_386);
368 #elif defined(__x86_64__)
369 assert(sizeof(void*) == 8 && elfHeader->e_machine == EM_X86_64);
370 #elif defined(__arm__)
371 assert(sizeof(void*) == 4 && elfHeader->e_machine == EM_ARM);
372 #else
373 #error "Unsupported platform"
374 #endif
Nicolas Capens66478362016-10-13 15:36:36 -0400375
Nicolas Capens598f8d82016-09-26 15:09:10 -0400376 SectionHeader *sectionHeader = (SectionHeader*)(elfImage + elfHeader->e_shoff);
377 void *entry = nullptr;
378
379 for(int i = 0; i < elfHeader->e_shnum; i++)
380 {
Nicolas Capens66478362016-10-13 15:36:36 -0400381 if(sectionHeader[i].sh_type == SHT_PROGBITS)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400382 {
Nicolas Capens66478362016-10-13 15:36:36 -0400383 if(sectionHeader[i].sh_flags & SHF_EXECINSTR)
384 {
385 entry = elfImage + sectionHeader[i].sh_offset;
Nicolas Capens1cc44382017-04-25 10:52:16 -0400386 codeSize = sectionHeader[i].sh_size;
Nicolas Capens66478362016-10-13 15:36:36 -0400387 }
388 }
389 else if(sectionHeader[i].sh_type == SHT_REL)
390 {
391 assert(sizeof(void*) == 4 && "UNIMPLEMENTED"); // Only expected/implemented for 32-bit code
392
Alexis Hetu113e33a2017-01-19 10:49:19 -0500393 for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++)
Nicolas Capens66478362016-10-13 15:36:36 -0400394 {
395 const Elf32_Rel &relocation = ((const Elf32_Rel*)(elfImage + sectionHeader[i].sh_offset))[index];
Alexis Hetu113e33a2017-01-19 10:49:19 -0500396 relocateSymbol(elfHeader, relocation, sectionHeader[i]);
Nicolas Capens66478362016-10-13 15:36:36 -0400397 }
398 }
399 else if(sectionHeader[i].sh_type == SHT_RELA)
400 {
401 assert(sizeof(void*) == 8 && "UNIMPLEMENTED"); // Only expected/implemented for 64-bit code
402
Alexis Hetu113e33a2017-01-19 10:49:19 -0500403 for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++)
Nicolas Capens66478362016-10-13 15:36:36 -0400404 {
405 const Elf64_Rela &relocation = ((const Elf64_Rela*)(elfImage + sectionHeader[i].sh_offset))[index];
Alexis Hetu113e33a2017-01-19 10:49:19 -0500406 relocateSymbol(elfHeader, relocation, sectionHeader[i]);
Nicolas Capens66478362016-10-13 15:36:36 -0400407 }
Nicolas Capens598f8d82016-09-26 15:09:10 -0400408 }
409 }
410
411 return entry;
412 }
413
414 template<typename T>
415 struct ExecutableAllocator
416 {
417 ExecutableAllocator() {};
418 template<class U> ExecutableAllocator(const ExecutableAllocator<U> &other) {};
419
420 using value_type = T;
421 using size_type = std::size_t;
422
423 T *allocate(size_type n)
424 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500425 #if defined(_WIN32)
426 return (T*)VirtualAlloc(NULL, sizeof(T) * n, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
427 #else
428 return (T*)mmap(nullptr, sizeof(T) * n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
429 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400430 }
431
432 void deallocate(T *p, size_type n)
433 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500434 #if defined(_WIN32)
435 VirtualFree(p, 0, MEM_RELEASE);
436 #else
437 munmap(p, sizeof(T) * n);
438 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400439 }
440 };
441
442 class ELFMemoryStreamer : public Ice::ELFStreamer, public Routine
443 {
444 ELFMemoryStreamer(const ELFMemoryStreamer &) = delete;
445 ELFMemoryStreamer &operator=(const ELFMemoryStreamer &) = delete;
446
447 public:
Nicolas Capens58274b52016-10-19 23:45:19 -0400448 ELFMemoryStreamer() : Routine(), entry(nullptr)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400449 {
450 position = 0;
451 buffer.reserve(0x1000);
452 }
453
Nicolas Capens81aa97b2017-06-27 17:08:08 -0400454 ~ELFMemoryStreamer() override
Nicolas Capens598f8d82016-09-26 15:09:10 -0400455 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500456 #if defined(_WIN32)
457 if(buffer.size() != 0)
458 {
459 DWORD exeProtection;
460 VirtualProtect(&buffer[0], buffer.size(), oldProtection, &exeProtection);
461 }
462 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400463 }
464
465 void write8(uint8_t Value) override
466 {
467 if(position == (uint64_t)buffer.size())
468 {
469 buffer.push_back(Value);
470 position++;
471 }
472 else if(position < (uint64_t)buffer.size())
473 {
474 buffer[position] = Value;
475 position++;
476 }
477 else assert(false && "UNIMPLEMENTED");
478 }
479
480 void writeBytes(llvm::StringRef Bytes) override
481 {
482 std::size_t oldSize = buffer.size();
483 buffer.resize(oldSize + Bytes.size());
484 memcpy(&buffer[oldSize], Bytes.begin(), Bytes.size());
485 position += Bytes.size();
486 }
487
488 uint64_t tell() const override { return position; }
489
490 void seek(uint64_t Off) override { position = Off; }
491
492 const void *getEntry() override
493 {
Nicolas Capens58274b52016-10-19 23:45:19 -0400494 if(!entry)
495 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500496 position = std::numeric_limits<std::size_t>::max(); // Can't stream more data after this
Nicolas Capens598f8d82016-09-26 15:09:10 -0400497
Nicolas Capens1cc44382017-04-25 10:52:16 -0400498 size_t codeSize = 0;
499 entry = loadImage(&buffer[0], codeSize);
500
501 #if defined(_WIN32)
Nicolas Capense745f5a2017-05-29 10:00:32 -0400502 VirtualProtect(&buffer[0], buffer.size(), PAGE_EXECUTE_READ, &oldProtection);
Nicolas Capens1cc44382017-04-25 10:52:16 -0400503 FlushInstructionCache(GetCurrentProcess(), NULL, 0);
504 #else
Nicolas Capense745f5a2017-05-29 10:00:32 -0400505 mprotect(&buffer[0], buffer.size(), PROT_READ | PROT_EXEC);
Nicolas Capens1cc44382017-04-25 10:52:16 -0400506 __builtin___clear_cache((char*)entry, (char*)entry + codeSize);
507 #endif
Nicolas Capens58274b52016-10-19 23:45:19 -0400508 }
509
510 return entry;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400511 }
512
513 private:
Nicolas Capens58274b52016-10-19 23:45:19 -0400514 void *entry;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400515 std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer;
516 std::size_t position;
Nicolas Capensbd65da92017-01-05 16:31:06 -0500517
518 #if defined(_WIN32)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400519 DWORD oldProtection;
Nicolas Capensbd65da92017-01-05 16:31:06 -0500520 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400521 };
522
523 Nucleus::Nucleus()
524 {
525 ::codegenMutex.lock(); // Reactor is currently not thread safe
526
Nicolas Capens66478362016-10-13 15:36:36 -0400527 Ice::ClFlags &Flags = Ice::ClFlags::Flags;
528 Ice::ClFlags::getParsedClFlags(Flags);
529
Nicolas Capens30cd7d42017-04-25 15:17:25 -0400530 #if defined(__arm__)
531 Flags.setTargetArch(Ice::Target_ARM32);
532 Flags.setTargetInstructionSet(Ice::ARM32InstructionSet_HWDivArm);
533 #else // x86
534 Flags.setTargetArch(sizeof(void*) == 8 ? Ice::Target_X8664 : Ice::Target_X8632);
535 Flags.setTargetInstructionSet(CPUID::SSE4_1 ? Ice::X86InstructionSet_SSE4_1 : Ice::X86InstructionSet_SSE2);
536 #endif
Nicolas Capens66478362016-10-13 15:36:36 -0400537 Flags.setOutFileType(Ice::FT_Elf);
538 Flags.setOptLevel(Ice::Opt_2);
539 Flags.setApplicationBinaryInterface(Ice::ABI_Platform);
Nicolas Capens30cd7d42017-04-25 15:17:25 -0400540 Flags.setVerbose(false ? Ice::IceV_Most : Ice::IceV_None);
541 Flags.setDisableHybridAssembly(true);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400542
Nicolas Capens65047112016-11-07 13:01:07 -0500543 static llvm::raw_os_ostream cout(std::cout);
544 static llvm::raw_os_ostream cerr(std::cerr);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400545
546 if(false) // Write out to a file
547 {
548 std::error_code errorCode;
549 ::out = new Ice::Fdstream("out.o", errorCode, llvm::sys::fs::F_None);
550 ::elfFile = new Ice::ELFFileStreamer(*out);
Nicolas Capens65047112016-11-07 13:01:07 -0500551 ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfFile);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400552 }
553 else
554 {
555 ELFMemoryStreamer *elfMemory = new ELFMemoryStreamer();
Nicolas Capens65047112016-11-07 13:01:07 -0500556 ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfMemory);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400557 ::routine = elfMemory;
558 }
559 }
560
561 Nucleus::~Nucleus()
562 {
Nicolas Capens619a8c52017-07-05 14:10:46 -0400563 delete ::routine;
564
Nicolas Capens598f8d82016-09-26 15:09:10 -0400565 delete ::allocator;
566 delete ::function;
567 delete ::context;
568
569 delete ::elfFile;
570 delete ::out;
571
572 ::codegenMutex.unlock();
573 }
574
575 Routine *Nucleus::acquireRoutine(const wchar_t *name, bool runOptimizations)
576 {
577 if(basicBlock->getInsts().empty() || basicBlock->getInsts().back().getKind() != Ice::Inst::Ret)
578 {
579 createRetVoid();
580 }
581
582 std::wstring wideName(name);
583 std::string asciiName(wideName.begin(), wideName.end());
584 ::function->setFunctionName(Ice::GlobalString::createWithString(::context, asciiName));
585
Nicolas Capens2ae9d742016-11-24 14:43:05 -0500586 optimize();
587
Nicolas Capens598f8d82016-09-26 15:09:10 -0400588 ::function->translate();
Nicolas Capensde19f392016-10-19 10:29:49 -0400589 assert(!::function->hasError());
590
Nicolas Capens83a6bb92017-07-05 15:04:00 -0400591 auto globals = ::function->getGlobalInits();
Nicolas Capens66478362016-10-13 15:36:36 -0400592
593 if(globals && !globals->empty())
594 {
Nicolas Capens83a6bb92017-07-05 15:04:00 -0400595 ::context->getGlobals()->merge(globals.get());
Nicolas Capens66478362016-10-13 15:36:36 -0400596 }
Nicolas Capens598f8d82016-09-26 15:09:10 -0400597
598 ::context->emitFileHeader();
599 ::function->emitIAS();
600 auto assembler = ::function->releaseAssembler();
Nicolas Capens66478362016-10-13 15:36:36 -0400601 auto objectWriter = ::context->getObjectWriter();
602 assembler->alignFunction();
603 objectWriter->writeFunctionCode(::function->getFunctionName(), false, assembler.get());
604 ::context->lowerGlobals("last");
Nicolas Capens73dd7a22016-10-20 13:20:34 -0400605 ::context->lowerConstants();
Nicolas Capensb98fe5c2016-11-09 12:24:06 -0500606 ::context->lowerJumpTables();
Nicolas Capens66478362016-10-13 15:36:36 -0400607 objectWriter->setUndefinedSyms(::context->getConstantExternSyms());
608 objectWriter->writeNonUserSections();
Nicolas Capens598f8d82016-09-26 15:09:10 -0400609
Nicolas Capens619a8c52017-07-05 14:10:46 -0400610 Routine *handoffRoutine = ::routine;
611 ::routine = nullptr;
612
613 return handoffRoutine;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400614 }
615
616 void Nucleus::optimize()
617 {
Nicolas Capens2ae9d742016-11-24 14:43:05 -0500618 sw::optimize(::function);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400619 }
620
621 Value *Nucleus::allocateStackVariable(Type *t, int arraySize)
622 {
623 Ice::Type type = T(t);
Nicolas Capensa8f98632016-10-20 11:25:55 -0400624 int typeSize = Ice::typeWidthInBytes(type);
625 int totalSize = typeSize * (arraySize ? arraySize : 1);
Nicolas Capense12780d2016-09-27 14:18:07 -0400626
Nicolas Capensa8f98632016-10-20 11:25:55 -0400627 auto bytes = Ice::ConstantInteger32::create(::context, type, totalSize);
Nicolas Capense12780d2016-09-27 14:18:07 -0400628 auto address = ::function->makeVariable(T(getPointerType(t)));
Nicolas Capensa8f98632016-10-20 11:25:55 -0400629 auto alloca = Ice::InstAlloca::create(::function, address, bytes, typeSize);
Nicolas Capense12780d2016-09-27 14:18:07 -0400630 ::function->getEntryNode()->getInsts().push_front(alloca);
631
632 return V(address);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400633 }
634
635 BasicBlock *Nucleus::createBasicBlock()
636 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400637 return B(::function->makeNode());
Nicolas Capens598f8d82016-09-26 15:09:10 -0400638 }
639
640 BasicBlock *Nucleus::getInsertBlock()
641 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400642 return B(::basicBlock);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400643 }
644
645 void Nucleus::setInsertBlock(BasicBlock *basicBlock)
646 {
Nicolas Capens9ed1a182016-10-24 09:52:23 -0400647 // assert(::basicBlock->getInsts().back().getTerminatorEdges().size() >= 0 && "Previous basic block must have a terminator");
Nicolas Capens611642a2016-09-28 16:45:04 -0400648 ::basicBlock = basicBlock;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400649 }
650
Nicolas Capens598f8d82016-09-26 15:09:10 -0400651 void Nucleus::createFunction(Type *ReturnType, std::vector<Type*> &Params)
652 {
653 uint32_t sequenceNumber = 0;
654 ::function = Ice::Cfg::create(::context, sequenceNumber).release();
655 ::allocator = new Ice::CfgLocalAllocatorScope(::function);
656
657 for(Type *type : Params)
658 {
659 Ice::Variable *arg = ::function->makeVariable(T(type));
660 ::function->addArg(arg);
661 }
662
663 Ice::CfgNode *node = ::function->makeNode();
664 ::function->setEntryNode(node);
665 ::basicBlock = node;
666 }
667
668 Value *Nucleus::getArgument(unsigned int index)
669 {
670 return V(::function->getArgs()[index]);
671 }
672
673 void Nucleus::createRetVoid()
674 {
Nicolas Capensfdcca2d2016-10-20 11:31:36 -0400675 Ice::InstRet *ret = Ice::InstRet::create(::function);
676 ::basicBlock->appendInst(ret);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400677 }
678
679 void Nucleus::createRet(Value *v)
680 {
Nicolas Capensfdcca2d2016-10-20 11:31:36 -0400681 Ice::InstRet *ret = Ice::InstRet::create(::function, v);
682 ::basicBlock->appendInst(ret);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400683 }
684
685 void Nucleus::createBr(BasicBlock *dest)
686 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400687 auto br = Ice::InstBr::create(::function, dest);
688 ::basicBlock->appendInst(br);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400689 }
690
691 void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse)
692 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400693 auto br = Ice::InstBr::create(::function, cond, ifTrue, ifFalse);
694 ::basicBlock->appendInst(br);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400695 }
696
Nicolas Capensf8360ba2017-01-25 11:35:00 -0800697 static bool isCommutative(Ice::InstArithmetic::OpKind op)
698 {
699 switch(op)
700 {
701 case Ice::InstArithmetic::Add:
702 case Ice::InstArithmetic::Fadd:
703 case Ice::InstArithmetic::Mul:
704 case Ice::InstArithmetic::Fmul:
705 case Ice::InstArithmetic::And:
706 case Ice::InstArithmetic::Or:
707 case Ice::InstArithmetic::Xor:
708 return true;
709 default:
710 return false;
711 }
712 }
713
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400714 static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs)
715 {
Nicolas Capens327f1df2016-10-21 14:26:34 -0400716 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 -0400717
Nicolas Capensf8360ba2017-01-25 11:35:00 -0800718 bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op);
719
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400720 Ice::Variable *result = ::function->makeVariable(lhs->getType());
Nicolas Capensf8360ba2017-01-25 11:35:00 -0800721 Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs);
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400722 ::basicBlock->appendInst(arithmetic);
723
724 return V(result);
725 }
726
Nicolas Capens598f8d82016-09-26 15:09:10 -0400727 Value *Nucleus::createAdd(Value *lhs, Value *rhs)
728 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400729 return createArithmetic(Ice::InstArithmetic::Add, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400730 }
731
732 Value *Nucleus::createSub(Value *lhs, Value *rhs)
733 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400734 return createArithmetic(Ice::InstArithmetic::Sub, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400735 }
736
737 Value *Nucleus::createMul(Value *lhs, Value *rhs)
738 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400739 return createArithmetic(Ice::InstArithmetic::Mul, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400740 }
741
742 Value *Nucleus::createUDiv(Value *lhs, Value *rhs)
743 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400744 return createArithmetic(Ice::InstArithmetic::Udiv, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400745 }
746
747 Value *Nucleus::createSDiv(Value *lhs, Value *rhs)
748 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400749 return createArithmetic(Ice::InstArithmetic::Sdiv, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400750 }
751
752 Value *Nucleus::createFAdd(Value *lhs, Value *rhs)
753 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400754 return createArithmetic(Ice::InstArithmetic::Fadd, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400755 }
756
757 Value *Nucleus::createFSub(Value *lhs, Value *rhs)
758 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400759 return createArithmetic(Ice::InstArithmetic::Fsub, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400760 }
761
762 Value *Nucleus::createFMul(Value *lhs, Value *rhs)
763 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400764 return createArithmetic(Ice::InstArithmetic::Fmul, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400765 }
766
767 Value *Nucleus::createFDiv(Value *lhs, Value *rhs)
768 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400769 return createArithmetic(Ice::InstArithmetic::Fdiv, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400770 }
771
772 Value *Nucleus::createURem(Value *lhs, Value *rhs)
773 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400774 return createArithmetic(Ice::InstArithmetic::Urem, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400775 }
776
777 Value *Nucleus::createSRem(Value *lhs, Value *rhs)
778 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400779 return createArithmetic(Ice::InstArithmetic::Srem, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400780 }
781
782 Value *Nucleus::createFRem(Value *lhs, Value *rhs)
783 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400784 return createArithmetic(Ice::InstArithmetic::Frem, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400785 }
786
787 Value *Nucleus::createShl(Value *lhs, Value *rhs)
788 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400789 return createArithmetic(Ice::InstArithmetic::Shl, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400790 }
791
792 Value *Nucleus::createLShr(Value *lhs, Value *rhs)
793 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400794 return createArithmetic(Ice::InstArithmetic::Lshr, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400795 }
796
797 Value *Nucleus::createAShr(Value *lhs, Value *rhs)
798 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400799 return createArithmetic(Ice::InstArithmetic::Ashr, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400800 }
801
802 Value *Nucleus::createAnd(Value *lhs, Value *rhs)
803 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400804 return createArithmetic(Ice::InstArithmetic::And, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400805 }
806
807 Value *Nucleus::createOr(Value *lhs, Value *rhs)
808 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400809 return createArithmetic(Ice::InstArithmetic::Or, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400810 }
811
812 Value *Nucleus::createXor(Value *lhs, Value *rhs)
813 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400814 return createArithmetic(Ice::InstArithmetic::Xor, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400815 }
816
817 Value *Nucleus::createNeg(Value *v)
818 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500819 return createSub(createNullValue(T(v->getType())), v);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400820 }
821
822 Value *Nucleus::createFNeg(Value *v)
823 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500824 double c[4] = {-0.0, -0.0, -0.0, -0.0};
825 Value *negativeZero = Ice::isVectorType(v->getType()) ?
826 createConstantVector(c, T(v->getType())) :
Nicolas Capens15060bb2016-12-05 22:17:19 -0500827 V(::context->getConstantFloat(-0.0f));
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500828
829 return createFSub(negativeZero, v);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400830 }
831
832 Value *Nucleus::createNot(Value *v)
833 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500834 if(Ice::isScalarIntegerType(v->getType()))
835 {
Nicolas Capens15060bb2016-12-05 22:17:19 -0500836 return createXor(v, V(::context->getConstantInt(v->getType(), -1)));
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500837 }
838 else // Vector
839 {
Nicolas Capensf34d1ac2017-05-08 17:06:11 -0400840 int64_t c[16] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500841 return createXor(v, createConstantVector(c, T(v->getType())));
842 }
Nicolas Capens598f8d82016-09-26 15:09:10 -0400843 }
844
Nicolas Capense12780d2016-09-27 14:18:07 -0400845 Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400846 {
Nicolas Capens23d99a42016-09-30 14:57:16 -0400847 int valueType = (int)reinterpret_cast<intptr_t>(type);
848 Ice::Variable *result = ::function->makeVariable(T(type));
849
850 if(valueType & EmulatedBits)
851 {
Nicolas Capens070d9f42017-04-26 13:36:33 -0400852 if(emulateIntrinsics)
853 {
854 if(typeSize(type) == 4)
855 {
856 auto pointer = RValue<Pointer<Byte>>(ptr);
857 Int x = *Pointer<Int>(pointer +1-1);
858
859 Int4 vector;
860 vector = Insert(vector, x, 0);
861
862 auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue());
863 ::basicBlock->appendInst(bitcast);
864 }
865 else if(typeSize(type) == 8)
866 {
867 auto pointer = RValue<Pointer<Byte>>(ptr);
868 Int x = *Pointer<Int>(pointer +1-1);
869 Int y = *Pointer<Int>(pointer + 4);
870
871 Int4 vector;
872 vector = Insert(vector, x, 0);
873 vector = Insert(vector, y, 1);
874
875 auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue());
876 ::basicBlock->appendInst(bitcast);
877 }
878 else assert(false);
879 }
880 else
881 {
882 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::LoadSubVector, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
883 auto target = ::context->getConstantUndef(Ice::IceType_i32);
884 auto load = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
885 load->addArg(ptr);
886 load->addArg(::context->getConstantInt32(typeSize(type)));
887 ::basicBlock->appendInst(load);
888 }
Nicolas Capens23d99a42016-09-30 14:57:16 -0400889 }
890 else
891 {
892 auto load = Ice::InstLoad::create(::function, result, ptr, align);
893 ::basicBlock->appendInst(load);
894 }
895
896 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400897 }
898
Nicolas Capens6d738712016-09-30 04:15:22 -0400899 Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400900 {
Nicolas Capens23d99a42016-09-30 14:57:16 -0400901 int valueType = (int)reinterpret_cast<intptr_t>(type);
902
903 if(valueType & EmulatedBits)
904 {
Nicolas Capens070d9f42017-04-26 13:36:33 -0400905 if(emulateIntrinsics)
906 {
907 if(typeSize(type) == 4)
908 {
909 Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32);
910 auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value);
911 ::basicBlock->appendInst(bitcast);
912
913 RValue<Int4> v(V(vector));
914
915 auto pointer = RValue<Pointer<Byte>>(ptr);
916 Int x = Extract(v, 0);
917 *Pointer<Int>(pointer) = x;
918 }
919 else if(typeSize(type) == 8)
920 {
921 Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32);
922 auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value);
923 ::basicBlock->appendInst(bitcast);
924
925 RValue<Int4> v(V(vector));
926
927 auto pointer = RValue<Pointer<Byte>>(ptr);
928 Int x = Extract(v, 0);
929 *Pointer<Int>(pointer) = x;
930 Int y = Extract(v, 1);
931 *Pointer<Int>(pointer + 4) = y;
932 }
933 else assert(false);
934 }
935 else
936 {
937 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::StoreSubVector, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T};
938 auto target = ::context->getConstantUndef(Ice::IceType_i32);
939 auto store = Ice::InstIntrinsicCall::create(::function, 3, nullptr, target, intrinsic);
940 store->addArg(value);
941 store->addArg(ptr);
942 store->addArg(::context->getConstantInt32(typeSize(type)));
943 ::basicBlock->appendInst(store);
944 }
Nicolas Capens23d99a42016-09-30 14:57:16 -0400945 }
946 else
947 {
948 assert(T(value->getType()) == type);
949
950 auto store = Ice::InstStore::create(::function, value, ptr, align);
951 ::basicBlock->appendInst(store);
952 }
953
Nicolas Capens598f8d82016-09-26 15:09:10 -0400954 return value;
955 }
956
Nicolas Capensd294def2017-01-26 17:44:37 -0800957 Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400958 {
Nicolas Capens8820f642016-09-30 04:42:43 -0400959 assert(index->getType() == Ice::IceType_i32);
960
Nicolas Capens15060bb2016-12-05 22:17:19 -0500961 if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index))
962 {
Nicolas Capens584088c2017-01-26 16:05:18 -0800963 int32_t offset = constant->getValue() * (int)typeSize(type);
Nicolas Capens15060bb2016-12-05 22:17:19 -0500964
965 if(offset == 0)
966 {
967 return ptr;
968 }
969
970 return createAdd(ptr, createConstantInt(offset));
971 }
972
Nicolas Capens8820f642016-09-30 04:42:43 -0400973 if(!Ice::isByteSizedType(T(type)))
974 {
Nicolas Capens584088c2017-01-26 16:05:18 -0800975 index = createMul(index, createConstantInt((int)typeSize(type)));
Nicolas Capens8820f642016-09-30 04:42:43 -0400976 }
977
978 if(sizeof(void*) == 8)
979 {
Nicolas Capensd294def2017-01-26 17:44:37 -0800980 if(unsignedIndex)
981 {
982 index = createZExt(index, T(Ice::IceType_i64));
983 }
984 else
985 {
986 index = createSExt(index, T(Ice::IceType_i64));
987 }
Nicolas Capens8820f642016-09-30 04:42:43 -0400988 }
989
990 return createAdd(ptr, index);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400991 }
992
993 Value *Nucleus::createAtomicAdd(Value *ptr, Value *value)
994 {
995 assert(false && "UNIMPLEMENTED"); return nullptr;
996 }
997
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400998 static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType)
999 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04001000 if(v->getType() == T(destType))
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001001 {
1002 return v;
1003 }
1004
1005 Ice::Variable *result = ::function->makeVariable(T(destType));
1006 Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v);
1007 ::basicBlock->appendInst(cast);
1008
1009 return V(result);
1010 }
1011
Nicolas Capens598f8d82016-09-26 15:09:10 -04001012 Value *Nucleus::createTrunc(Value *v, Type *destType)
1013 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001014 return createCast(Ice::InstCast::Trunc, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001015 }
1016
1017 Value *Nucleus::createZExt(Value *v, Type *destType)
1018 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001019 return createCast(Ice::InstCast::Zext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001020 }
1021
1022 Value *Nucleus::createSExt(Value *v, Type *destType)
1023 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001024 return createCast(Ice::InstCast::Sext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001025 }
1026
1027 Value *Nucleus::createFPToSI(Value *v, Type *destType)
1028 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001029 return createCast(Ice::InstCast::Fptosi, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001030 }
1031
Nicolas Capens598f8d82016-09-26 15:09:10 -04001032 Value *Nucleus::createSIToFP(Value *v, Type *destType)
1033 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001034 return createCast(Ice::InstCast::Sitofp, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001035 }
1036
1037 Value *Nucleus::createFPTrunc(Value *v, Type *destType)
1038 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001039 return createCast(Ice::InstCast::Fptrunc, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001040 }
1041
1042 Value *Nucleus::createFPExt(Value *v, Type *destType)
1043 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001044 return createCast(Ice::InstCast::Fpext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001045 }
1046
1047 Value *Nucleus::createBitCast(Value *v, Type *destType)
1048 {
Nicolas Capens2d8c3702017-07-25 13:56:46 -04001049 // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need
1050 // support for casting between scalars and wide vectors. For platforms where this is not supported,
1051 // emulate them by writing to the stack and reading back as the destination type.
1052 if(emulateMismatchedBitCast)
1053 {
1054 if(!Ice::isVectorType(v->getType()) && Ice::isVectorType(T(destType)))
1055 {
1056 Value *address = allocateStackVariable(destType);
1057 createStore(v, address, T(v->getType()));
1058 return createLoad(address, destType);
1059 }
1060 else if(Ice::isVectorType(v->getType()) && !Ice::isVectorType(T(destType)))
1061 {
1062 Value *address = allocateStackVariable(T(v->getType()));
1063 createStore(v, address, T(v->getType()));
1064 return createLoad(address, destType);
1065 }
1066 }
1067
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001068 return createCast(Ice::InstCast::Bitcast, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001069 }
1070
Nicolas Capens43dc6292016-10-20 00:01:38 -04001071 static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001072 {
Nicolas Capens611642a2016-09-28 16:45:04 -04001073 assert(lhs->getType() == rhs->getType());
1074
Nicolas Capens43dc6292016-10-20 00:01:38 -04001075 auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType());
1076 auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs);
Nicolas Capens611642a2016-09-28 16:45:04 -04001077 ::basicBlock->appendInst(cmp);
1078
1079 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001080 }
1081
Nicolas Capens43dc6292016-10-20 00:01:38 -04001082 Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs)
1083 {
1084 return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs);
1085 }
1086
1087 Value *Nucleus::createICmpNE(Value *lhs, Value *rhs)
1088 {
1089 return createIntCompare(Ice::InstIcmp::Ne, lhs, rhs);
1090 }
1091
1092 Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs)
1093 {
1094 return createIntCompare(Ice::InstIcmp::Ugt, lhs, rhs);
1095 }
1096
1097 Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs)
1098 {
1099 return createIntCompare(Ice::InstIcmp::Uge, lhs, rhs);
1100 }
1101
1102 Value *Nucleus::createICmpULT(Value *lhs, Value *rhs)
1103 {
1104 return createIntCompare(Ice::InstIcmp::Ult, lhs, rhs);
1105 }
1106
1107 Value *Nucleus::createICmpULE(Value *lhs, Value *rhs)
1108 {
1109 return createIntCompare(Ice::InstIcmp::Ule, lhs, rhs);
1110 }
1111
1112 Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs)
1113 {
1114 return createIntCompare(Ice::InstIcmp::Sgt, lhs, rhs);
1115 }
1116
1117 Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs)
1118 {
1119 return createIntCompare(Ice::InstIcmp::Sge, lhs, rhs);
1120 }
1121
1122 Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs)
1123 {
1124 return createIntCompare(Ice::InstIcmp::Slt, lhs, rhs);
1125 }
1126
Nicolas Capens598f8d82016-09-26 15:09:10 -04001127 Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs)
1128 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001129 return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs);
1130 }
1131
1132 static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs)
1133 {
1134 assert(lhs->getType() == rhs->getType());
1135 assert(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32);
1136
1137 auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32);
1138 auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs);
1139 ::basicBlock->appendInst(cmp);
1140
1141 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001142 }
1143
1144 Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs)
1145 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001146 return createFloatCompare(Ice::InstFcmp::Oeq, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001147 }
1148
1149 Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs)
1150 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001151 return createFloatCompare(Ice::InstFcmp::Ogt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001152 }
1153
1154 Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs)
1155 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001156 return createFloatCompare(Ice::InstFcmp::Oge, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001157 }
1158
1159 Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs)
1160 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001161 return createFloatCompare(Ice::InstFcmp::Olt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001162 }
1163
1164 Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs)
1165 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001166 return createFloatCompare(Ice::InstFcmp::Ole, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001167 }
1168
1169 Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs)
1170 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001171 return createFloatCompare(Ice::InstFcmp::One, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001172 }
1173
1174 Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs)
1175 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001176 return createFloatCompare(Ice::InstFcmp::Ord, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001177 }
1178
1179 Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs)
1180 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001181 return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001182 }
1183
1184 Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs)
1185 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001186 return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001187 }
1188
1189 Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs)
1190 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001191 return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001192 }
1193
1194 Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs)
1195 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001196 return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001197 }
1198
1199 Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs)
1200 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001201 return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001202 }
1203
1204 Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs)
1205 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001206 return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001207 }
1208
1209 Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs)
1210 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001211 return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001212 }
1213
Nicolas Capense95d5342016-09-30 11:37:28 -04001214 Value *Nucleus::createExtractElement(Value *vector, Type *type, int index)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001215 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001216 auto result = ::function->makeVariable(T(type));
1217 auto extract = Ice::InstExtractElement::create(::function, result, vector, ::context->getConstantInt32(index));
1218 ::basicBlock->appendInst(extract);
1219
1220 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001221 }
1222
1223 Value *Nucleus::createInsertElement(Value *vector, Value *element, int index)
1224 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001225 auto result = ::function->makeVariable(vector->getType());
1226 auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index));
1227 ::basicBlock->appendInst(insert);
1228
1229 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001230 }
1231
Nicolas Capense89cd582016-09-30 14:23:47 -04001232 Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001233 {
Nicolas Capens619c0ab2016-09-30 14:46:24 -04001234 assert(V1->getType() == V2->getType());
1235
1236 int size = Ice::typeNumElements(V1->getType());
1237 auto result = ::function->makeVariable(V1->getType());
1238 auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2);
1239
1240 for(int i = 0; i < size; i++)
1241 {
1242 shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i])));
1243 }
1244
1245 ::basicBlock->appendInst(shuffle);
1246
1247 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001248 }
1249
1250 Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse)
1251 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04001252 assert(ifTrue->getType() == ifFalse->getType());
1253
1254 auto result = ::function->makeVariable(ifTrue->getType());
1255 auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse);
1256 ::basicBlock->appendInst(select);
1257
1258 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001259 }
1260
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001261 SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001262 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001263 auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch);
1264 ::basicBlock->appendInst(switchInst);
1265
1266 return reinterpret_cast<SwitchCases*>(switchInst);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001267 }
1268
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001269 void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001270 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001271 switchCases->addBranch(label, label, branch);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001272 }
1273
1274 void Nucleus::createUnreachable()
1275 {
Nicolas Capensfdcca2d2016-10-20 11:31:36 -04001276 Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function);
1277 ::basicBlock->appendInst(unreachable);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001278 }
1279
Nicolas Capense95d5342016-09-30 11:37:28 -04001280 static Value *createSwizzle4(Value *val, unsigned char select)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001281 {
Nicolas Capens619c0ab2016-09-30 14:46:24 -04001282 int swizzle[4] =
1283 {
1284 (select >> 0) & 0x03,
1285 (select >> 2) & 0x03,
1286 (select >> 4) & 0x03,
1287 (select >> 6) & 0x03,
1288 };
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001289
Nicolas Capens619c0ab2016-09-30 14:46:24 -04001290 return Nucleus::createShuffleVector(val, val, swizzle);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001291 }
1292
Nicolas Capense95d5342016-09-30 11:37:28 -04001293 static Value *createMask4(Value *lhs, Value *rhs, unsigned char select)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001294 {
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001295 int64_t mask[4] = {0, 0, 0, 0};
1296
1297 mask[(select >> 0) & 0x03] = -1;
1298 mask[(select >> 2) & 0x03] = -1;
1299 mask[(select >> 4) & 0x03] = -1;
1300 mask[(select >> 6) & 0x03] = -1;
1301
1302 Value *condition = Nucleus::createConstantVector(mask, T(Ice::IceType_v4i1));
1303 Value *result = Nucleus::createSelect(condition, rhs, lhs);
1304
1305 return result;
Nicolas Capens598f8d82016-09-26 15:09:10 -04001306 }
1307
Nicolas Capens598f8d82016-09-26 15:09:10 -04001308 Type *Nucleus::getPointerType(Type *ElementType)
1309 {
Nicolas Capense12780d2016-09-27 14:18:07 -04001310 if(sizeof(void*) == 8)
1311 {
1312 return T(Ice::IceType_i64);
1313 }
1314 else
1315 {
1316 return T(Ice::IceType_i32);
1317 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04001318 }
1319
Nicolas Capens13ac2322016-10-13 14:52:12 -04001320 Value *Nucleus::createNullValue(Type *Ty)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001321 {
Nicolas Capens73dd7a22016-10-20 13:20:34 -04001322 if(Ice::isVectorType(T(Ty)))
1323 {
Nicolas Capens30385f02017-04-18 13:03:47 -04001324 assert(Ice::typeNumElements(T(Ty)) <= 16);
1325 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 -04001326 return createConstantVector(c, Ty);
1327 }
1328 else
1329 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001330 return V(::context->getConstantZero(T(Ty)));
Nicolas Capens73dd7a22016-10-20 13:20:34 -04001331 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04001332 }
1333
Nicolas Capens13ac2322016-10-13 14:52:12 -04001334 Value *Nucleus::createConstantLong(int64_t i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001335 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001336 return V(::context->getConstantInt64(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001337 }
1338
Nicolas Capens13ac2322016-10-13 14:52:12 -04001339 Value *Nucleus::createConstantInt(int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001340 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001341 return V(::context->getConstantInt32(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001342 }
1343
Nicolas Capens13ac2322016-10-13 14:52:12 -04001344 Value *Nucleus::createConstantInt(unsigned int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001345 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001346 return V(::context->getConstantInt32(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001347 }
1348
Nicolas Capens13ac2322016-10-13 14:52:12 -04001349 Value *Nucleus::createConstantBool(bool b)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001350 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001351 return V(::context->getConstantInt1(b));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001352 }
1353
Nicolas Capens13ac2322016-10-13 14:52:12 -04001354 Value *Nucleus::createConstantByte(signed char i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001355 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001356 return V(::context->getConstantInt8(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001357 }
1358
Nicolas Capens13ac2322016-10-13 14:52:12 -04001359 Value *Nucleus::createConstantByte(unsigned char i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001360 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001361 return V(::context->getConstantInt8(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001362 }
1363
Nicolas Capens13ac2322016-10-13 14:52:12 -04001364 Value *Nucleus::createConstantShort(short i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001365 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001366 return V(::context->getConstantInt16(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001367 }
1368
Nicolas Capens13ac2322016-10-13 14:52:12 -04001369 Value *Nucleus::createConstantShort(unsigned short i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001370 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001371 return V(::context->getConstantInt16(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001372 }
1373
Nicolas Capens13ac2322016-10-13 14:52:12 -04001374 Value *Nucleus::createConstantFloat(float x)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001375 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001376 return V(::context->getConstantFloat(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001377 }
1378
Nicolas Capens13ac2322016-10-13 14:52:12 -04001379 Value *Nucleus::createNullPointer(Type *Ty)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001380 {
Nicolas Capensa29d6532016-12-05 21:38:09 -05001381 return createNullValue(T(sizeof(void*) == 8 ? Ice::IceType_i64 : Ice::IceType_i32));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001382 }
1383
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001384 Value *Nucleus::createConstantVector(const int64_t *constants, Type *type)
Nicolas Capens13ac2322016-10-13 14:52:12 -04001385 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001386 const int vectorSize = 16;
1387 assert(Ice::typeWidthInBytes(T(type)) == vectorSize);
1388 const int alignment = vectorSize;
1389 auto globalPool = ::function->getGlobalPool();
1390
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001391 const int64_t *i = constants;
1392 const double *f = reinterpret_cast<const double*>(constants);
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001393 Ice::VariableDeclaration::DataInitializer *dataInitializer = nullptr;
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001394
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001395 switch((int)reinterpret_cast<intptr_t>(type))
1396 {
1397 case Ice::IceType_v4i32:
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001398 case Ice::IceType_v4i1:
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001399 {
1400 const int initializer[4] = {(int)i[0], (int)i[1], (int)i[2], (int)i[3]};
1401 static_assert(sizeof(initializer) == vectorSize, "!");
1402 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1403 }
1404 break;
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001405 case Ice::IceType_v4f32:
1406 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001407 const float initializer[4] = {(float)f[0], (float)f[1], (float)f[2], (float)f[3]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001408 static_assert(sizeof(initializer) == vectorSize, "!");
1409 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1410 }
1411 break;
1412 case Ice::IceType_v8i16:
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001413 case Ice::IceType_v8i1:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001414 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001415 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 -04001416 static_assert(sizeof(initializer) == vectorSize, "!");
1417 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1418 }
1419 break;
1420 case Ice::IceType_v16i8:
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001421 case Ice::IceType_v16i1:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001422 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001423 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 -04001424 static_assert(sizeof(initializer) == vectorSize, "!");
1425 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1426 }
1427 break;
1428 case Type_v2i32:
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001429 {
1430 const int initializer[4] = {(int)i[0], (int)i[1], (int)i[0], (int)i[1]};
1431 static_assert(sizeof(initializer) == vectorSize, "!");
1432 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1433 }
1434 break;
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001435 case Type_v2f32:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001436 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001437 const float initializer[4] = {(float)f[0], (float)f[1], (float)f[0], (float)f[1]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001438 static_assert(sizeof(initializer) == vectorSize, "!");
1439 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1440 }
1441 break;
1442 case Type_v4i16:
1443 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001444 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 -04001445 static_assert(sizeof(initializer) == vectorSize, "!");
1446 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1447 }
1448 break;
1449 case Type_v8i8:
1450 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001451 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 -04001452 static_assert(sizeof(initializer) == vectorSize, "!");
1453 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1454 }
1455 break;
1456 case Type_v4i8:
1457 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001458 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 -04001459 static_assert(sizeof(initializer) == vectorSize, "!");
1460 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1461 }
1462 break;
1463 default:
1464 assert(false && "Unknown constant vector type" && type);
1465 }
1466
1467 auto name = Ice::GlobalString::createWithoutString(::context);
1468 auto *variableDeclaration = Ice::VariableDeclaration::create(globalPool);
1469 variableDeclaration->setName(name);
1470 variableDeclaration->setAlignment(alignment);
1471 variableDeclaration->setIsConstant(true);
1472 variableDeclaration->addInitializer(dataInitializer);
Nicolas Capens87852e12016-11-24 14:45:06 -05001473
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001474 ::function->addGlobal(variableDeclaration);
1475
1476 constexpr int32_t offset = 0;
1477 Ice::Operand *ptr = ::context->getConstantSym(offset, name);
1478
1479 Ice::Variable *result = ::function->makeVariable(T(type));
1480 auto load = Ice::InstLoad::create(::function, result, ptr, alignment);
1481 ::basicBlock->appendInst(load);
1482
1483 return V(result);
Nicolas Capens13ac2322016-10-13 14:52:12 -04001484 }
1485
1486 Value *Nucleus::createConstantVector(const double *constants, Type *type)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001487 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001488 return createConstantVector((const int64_t*)constants, type);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001489 }
1490
1491 Type *Void::getType()
1492 {
1493 return T(Ice::IceType_void);
1494 }
1495
Nicolas Capens598f8d82016-09-26 15:09:10 -04001496 Bool::Bool(Argument<Bool> argument)
1497 {
1498 storeValue(argument.value);
1499 }
1500
Nicolas Capens598f8d82016-09-26 15:09:10 -04001501 Bool::Bool(bool x)
1502 {
1503 storeValue(Nucleus::createConstantBool(x));
1504 }
1505
1506 Bool::Bool(RValue<Bool> rhs)
1507 {
1508 storeValue(rhs.value);
1509 }
1510
1511 Bool::Bool(const Bool &rhs)
1512 {
1513 Value *value = rhs.loadValue();
1514 storeValue(value);
1515 }
1516
1517 Bool::Bool(const Reference<Bool> &rhs)
1518 {
1519 Value *value = rhs.loadValue();
1520 storeValue(value);
1521 }
1522
Nicolas Capens96d4e092016-11-18 14:22:38 -05001523 RValue<Bool> Bool::operator=(RValue<Bool> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001524 {
1525 storeValue(rhs.value);
1526
1527 return rhs;
1528 }
1529
Nicolas Capens96d4e092016-11-18 14:22:38 -05001530 RValue<Bool> Bool::operator=(const Bool &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001531 {
1532 Value *value = rhs.loadValue();
1533 storeValue(value);
1534
1535 return RValue<Bool>(value);
1536 }
1537
Nicolas Capens96d4e092016-11-18 14:22:38 -05001538 RValue<Bool> Bool::operator=(const Reference<Bool> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001539 {
1540 Value *value = rhs.loadValue();
1541 storeValue(value);
1542
1543 return RValue<Bool>(value);
1544 }
1545
1546 RValue<Bool> operator!(RValue<Bool> val)
1547 {
1548 return RValue<Bool>(Nucleus::createNot(val.value));
1549 }
1550
1551 RValue<Bool> operator&&(RValue<Bool> lhs, RValue<Bool> rhs)
1552 {
1553 return RValue<Bool>(Nucleus::createAnd(lhs.value, rhs.value));
1554 }
1555
1556 RValue<Bool> operator||(RValue<Bool> lhs, RValue<Bool> rhs)
1557 {
1558 return RValue<Bool>(Nucleus::createOr(lhs.value, rhs.value));
1559 }
1560
1561 Type *Bool::getType()
1562 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001563 return T(Ice::IceType_i1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001564 }
1565
1566 Byte::Byte(Argument<Byte> argument)
1567 {
1568 storeValue(argument.value);
1569 }
1570
1571 Byte::Byte(RValue<Int> cast)
1572 {
1573 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1574
1575 storeValue(integer);
1576 }
1577
1578 Byte::Byte(RValue<UInt> cast)
1579 {
1580 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1581
1582 storeValue(integer);
1583 }
1584
1585 Byte::Byte(RValue<UShort> cast)
1586 {
1587 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1588
1589 storeValue(integer);
1590 }
1591
Nicolas Capens598f8d82016-09-26 15:09:10 -04001592 Byte::Byte(int x)
1593 {
1594 storeValue(Nucleus::createConstantByte((unsigned char)x));
1595 }
1596
1597 Byte::Byte(unsigned char x)
1598 {
1599 storeValue(Nucleus::createConstantByte(x));
1600 }
1601
1602 Byte::Byte(RValue<Byte> rhs)
1603 {
1604 storeValue(rhs.value);
1605 }
1606
1607 Byte::Byte(const Byte &rhs)
1608 {
1609 Value *value = rhs.loadValue();
1610 storeValue(value);
1611 }
1612
1613 Byte::Byte(const Reference<Byte> &rhs)
1614 {
1615 Value *value = rhs.loadValue();
1616 storeValue(value);
1617 }
1618
Nicolas Capens96d4e092016-11-18 14:22:38 -05001619 RValue<Byte> Byte::operator=(RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001620 {
1621 storeValue(rhs.value);
1622
1623 return rhs;
1624 }
1625
Nicolas Capens96d4e092016-11-18 14:22:38 -05001626 RValue<Byte> Byte::operator=(const Byte &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001627 {
1628 Value *value = rhs.loadValue();
1629 storeValue(value);
1630
1631 return RValue<Byte>(value);
1632 }
1633
Nicolas Capens96d4e092016-11-18 14:22:38 -05001634 RValue<Byte> Byte::operator=(const Reference<Byte> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001635 {
1636 Value *value = rhs.loadValue();
1637 storeValue(value);
1638
1639 return RValue<Byte>(value);
1640 }
1641
1642 RValue<Byte> operator+(RValue<Byte> lhs, RValue<Byte> rhs)
1643 {
1644 return RValue<Byte>(Nucleus::createAdd(lhs.value, rhs.value));
1645 }
1646
1647 RValue<Byte> operator-(RValue<Byte> lhs, RValue<Byte> rhs)
1648 {
1649 return RValue<Byte>(Nucleus::createSub(lhs.value, rhs.value));
1650 }
1651
1652 RValue<Byte> operator*(RValue<Byte> lhs, RValue<Byte> rhs)
1653 {
1654 return RValue<Byte>(Nucleus::createMul(lhs.value, rhs.value));
1655 }
1656
1657 RValue<Byte> operator/(RValue<Byte> lhs, RValue<Byte> rhs)
1658 {
1659 return RValue<Byte>(Nucleus::createUDiv(lhs.value, rhs.value));
1660 }
1661
1662 RValue<Byte> operator%(RValue<Byte> lhs, RValue<Byte> rhs)
1663 {
1664 return RValue<Byte>(Nucleus::createURem(lhs.value, rhs.value));
1665 }
1666
1667 RValue<Byte> operator&(RValue<Byte> lhs, RValue<Byte> rhs)
1668 {
1669 return RValue<Byte>(Nucleus::createAnd(lhs.value, rhs.value));
1670 }
1671
1672 RValue<Byte> operator|(RValue<Byte> lhs, RValue<Byte> rhs)
1673 {
1674 return RValue<Byte>(Nucleus::createOr(lhs.value, rhs.value));
1675 }
1676
1677 RValue<Byte> operator^(RValue<Byte> lhs, RValue<Byte> rhs)
1678 {
1679 return RValue<Byte>(Nucleus::createXor(lhs.value, rhs.value));
1680 }
1681
1682 RValue<Byte> operator<<(RValue<Byte> lhs, RValue<Byte> rhs)
1683 {
1684 return RValue<Byte>(Nucleus::createShl(lhs.value, rhs.value));
1685 }
1686
1687 RValue<Byte> operator>>(RValue<Byte> lhs, RValue<Byte> rhs)
1688 {
1689 return RValue<Byte>(Nucleus::createLShr(lhs.value, rhs.value));
1690 }
1691
Nicolas Capens96d4e092016-11-18 14:22:38 -05001692 RValue<Byte> operator+=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001693 {
1694 return lhs = lhs + rhs;
1695 }
1696
Nicolas Capens96d4e092016-11-18 14:22:38 -05001697 RValue<Byte> operator-=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001698 {
1699 return lhs = lhs - rhs;
1700 }
1701
Nicolas Capens96d4e092016-11-18 14:22:38 -05001702 RValue<Byte> operator*=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001703 {
1704 return lhs = lhs * rhs;
1705 }
1706
Nicolas Capens96d4e092016-11-18 14:22:38 -05001707 RValue<Byte> operator/=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001708 {
1709 return lhs = lhs / rhs;
1710 }
1711
Nicolas Capens96d4e092016-11-18 14:22:38 -05001712 RValue<Byte> operator%=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001713 {
1714 return lhs = lhs % rhs;
1715 }
1716
Nicolas Capens96d4e092016-11-18 14:22:38 -05001717 RValue<Byte> operator&=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001718 {
1719 return lhs = lhs & rhs;
1720 }
1721
Nicolas Capens96d4e092016-11-18 14:22:38 -05001722 RValue<Byte> operator|=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001723 {
1724 return lhs = lhs | rhs;
1725 }
1726
Nicolas Capens96d4e092016-11-18 14:22:38 -05001727 RValue<Byte> operator^=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001728 {
1729 return lhs = lhs ^ rhs;
1730 }
1731
Nicolas Capens96d4e092016-11-18 14:22:38 -05001732 RValue<Byte> operator<<=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001733 {
1734 return lhs = lhs << rhs;
1735 }
1736
Nicolas Capens96d4e092016-11-18 14:22:38 -05001737 RValue<Byte> operator>>=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001738 {
1739 return lhs = lhs >> rhs;
1740 }
1741
1742 RValue<Byte> operator+(RValue<Byte> val)
1743 {
1744 return val;
1745 }
1746
1747 RValue<Byte> operator-(RValue<Byte> val)
1748 {
1749 return RValue<Byte>(Nucleus::createNeg(val.value));
1750 }
1751
1752 RValue<Byte> operator~(RValue<Byte> val)
1753 {
1754 return RValue<Byte>(Nucleus::createNot(val.value));
1755 }
1756
Nicolas Capens96d4e092016-11-18 14:22:38 -05001757 RValue<Byte> operator++(Byte &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001758 {
1759 RValue<Byte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001760 val += Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001761 return res;
1762 }
1763
Nicolas Capens96d4e092016-11-18 14:22:38 -05001764 const Byte &operator++(Byte &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001765 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001766 val += Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001767 return val;
1768 }
1769
Nicolas Capens96d4e092016-11-18 14:22:38 -05001770 RValue<Byte> operator--(Byte &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001771 {
1772 RValue<Byte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001773 val -= Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001774 return res;
1775 }
1776
Nicolas Capens96d4e092016-11-18 14:22:38 -05001777 const Byte &operator--(Byte &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001778 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001779 val -= Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001780 return val;
1781 }
1782
1783 RValue<Bool> operator<(RValue<Byte> lhs, RValue<Byte> rhs)
1784 {
1785 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
1786 }
1787
1788 RValue<Bool> operator<=(RValue<Byte> lhs, RValue<Byte> rhs)
1789 {
1790 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
1791 }
1792
1793 RValue<Bool> operator>(RValue<Byte> lhs, RValue<Byte> rhs)
1794 {
1795 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
1796 }
1797
1798 RValue<Bool> operator>=(RValue<Byte> lhs, RValue<Byte> rhs)
1799 {
1800 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
1801 }
1802
1803 RValue<Bool> operator!=(RValue<Byte> lhs, RValue<Byte> rhs)
1804 {
1805 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
1806 }
1807
1808 RValue<Bool> operator==(RValue<Byte> lhs, RValue<Byte> rhs)
1809 {
1810 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
1811 }
1812
1813 Type *Byte::getType()
1814 {
Nicolas Capens6d738712016-09-30 04:15:22 -04001815 return T(Ice::IceType_i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001816 }
1817
1818 SByte::SByte(Argument<SByte> argument)
1819 {
1820 storeValue(argument.value);
1821 }
1822
1823 SByte::SByte(RValue<Int> cast)
1824 {
1825 Value *integer = Nucleus::createTrunc(cast.value, SByte::getType());
1826
1827 storeValue(integer);
1828 }
1829
1830 SByte::SByte(RValue<Short> cast)
1831 {
1832 Value *integer = Nucleus::createTrunc(cast.value, SByte::getType());
1833
1834 storeValue(integer);
1835 }
1836
Nicolas Capens598f8d82016-09-26 15:09:10 -04001837 SByte::SByte(signed char x)
1838 {
1839 storeValue(Nucleus::createConstantByte(x));
1840 }
1841
1842 SByte::SByte(RValue<SByte> rhs)
1843 {
1844 storeValue(rhs.value);
1845 }
1846
1847 SByte::SByte(const SByte &rhs)
1848 {
1849 Value *value = rhs.loadValue();
1850 storeValue(value);
1851 }
1852
1853 SByte::SByte(const Reference<SByte> &rhs)
1854 {
1855 Value *value = rhs.loadValue();
1856 storeValue(value);
1857 }
1858
Nicolas Capens96d4e092016-11-18 14:22:38 -05001859 RValue<SByte> SByte::operator=(RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001860 {
1861 storeValue(rhs.value);
1862
1863 return rhs;
1864 }
1865
Nicolas Capens96d4e092016-11-18 14:22:38 -05001866 RValue<SByte> SByte::operator=(const SByte &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001867 {
1868 Value *value = rhs.loadValue();
1869 storeValue(value);
1870
1871 return RValue<SByte>(value);
1872 }
1873
Nicolas Capens96d4e092016-11-18 14:22:38 -05001874 RValue<SByte> SByte::operator=(const Reference<SByte> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001875 {
1876 Value *value = rhs.loadValue();
1877 storeValue(value);
1878
1879 return RValue<SByte>(value);
1880 }
1881
1882 RValue<SByte> operator+(RValue<SByte> lhs, RValue<SByte> rhs)
1883 {
1884 return RValue<SByte>(Nucleus::createAdd(lhs.value, rhs.value));
1885 }
1886
1887 RValue<SByte> operator-(RValue<SByte> lhs, RValue<SByte> rhs)
1888 {
1889 return RValue<SByte>(Nucleus::createSub(lhs.value, rhs.value));
1890 }
1891
1892 RValue<SByte> operator*(RValue<SByte> lhs, RValue<SByte> rhs)
1893 {
1894 return RValue<SByte>(Nucleus::createMul(lhs.value, rhs.value));
1895 }
1896
1897 RValue<SByte> operator/(RValue<SByte> lhs, RValue<SByte> rhs)
1898 {
1899 return RValue<SByte>(Nucleus::createSDiv(lhs.value, rhs.value));
1900 }
1901
1902 RValue<SByte> operator%(RValue<SByte> lhs, RValue<SByte> rhs)
1903 {
1904 return RValue<SByte>(Nucleus::createSRem(lhs.value, rhs.value));
1905 }
1906
1907 RValue<SByte> operator&(RValue<SByte> lhs, RValue<SByte> rhs)
1908 {
1909 return RValue<SByte>(Nucleus::createAnd(lhs.value, rhs.value));
1910 }
1911
1912 RValue<SByte> operator|(RValue<SByte> lhs, RValue<SByte> rhs)
1913 {
1914 return RValue<SByte>(Nucleus::createOr(lhs.value, rhs.value));
1915 }
1916
1917 RValue<SByte> operator^(RValue<SByte> lhs, RValue<SByte> rhs)
1918 {
1919 return RValue<SByte>(Nucleus::createXor(lhs.value, rhs.value));
1920 }
1921
1922 RValue<SByte> operator<<(RValue<SByte> lhs, RValue<SByte> rhs)
1923 {
1924 return RValue<SByte>(Nucleus::createShl(lhs.value, rhs.value));
1925 }
1926
1927 RValue<SByte> operator>>(RValue<SByte> lhs, RValue<SByte> rhs)
1928 {
1929 return RValue<SByte>(Nucleus::createAShr(lhs.value, rhs.value));
1930 }
1931
Nicolas Capens96d4e092016-11-18 14:22:38 -05001932 RValue<SByte> operator+=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001933 {
1934 return lhs = lhs + rhs;
1935 }
1936
Nicolas Capens96d4e092016-11-18 14:22:38 -05001937 RValue<SByte> operator-=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001938 {
1939 return lhs = lhs - rhs;
1940 }
1941
Nicolas Capens96d4e092016-11-18 14:22:38 -05001942 RValue<SByte> operator*=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001943 {
1944 return lhs = lhs * rhs;
1945 }
1946
Nicolas Capens96d4e092016-11-18 14:22:38 -05001947 RValue<SByte> operator/=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001948 {
1949 return lhs = lhs / rhs;
1950 }
1951
Nicolas Capens96d4e092016-11-18 14:22:38 -05001952 RValue<SByte> operator%=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001953 {
1954 return lhs = lhs % rhs;
1955 }
1956
Nicolas Capens96d4e092016-11-18 14:22:38 -05001957 RValue<SByte> operator&=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001958 {
1959 return lhs = lhs & rhs;
1960 }
1961
Nicolas Capens96d4e092016-11-18 14:22:38 -05001962 RValue<SByte> operator|=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001963 {
1964 return lhs = lhs | rhs;
1965 }
1966
Nicolas Capens96d4e092016-11-18 14:22:38 -05001967 RValue<SByte> operator^=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001968 {
1969 return lhs = lhs ^ rhs;
1970 }
1971
Nicolas Capens96d4e092016-11-18 14:22:38 -05001972 RValue<SByte> operator<<=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001973 {
1974 return lhs = lhs << rhs;
1975 }
1976
Nicolas Capens96d4e092016-11-18 14:22:38 -05001977 RValue<SByte> operator>>=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001978 {
1979 return lhs = lhs >> rhs;
1980 }
1981
1982 RValue<SByte> operator+(RValue<SByte> val)
1983 {
1984 return val;
1985 }
1986
1987 RValue<SByte> operator-(RValue<SByte> val)
1988 {
1989 return RValue<SByte>(Nucleus::createNeg(val.value));
1990 }
1991
1992 RValue<SByte> operator~(RValue<SByte> val)
1993 {
1994 return RValue<SByte>(Nucleus::createNot(val.value));
1995 }
1996
Nicolas Capens96d4e092016-11-18 14:22:38 -05001997 RValue<SByte> operator++(SByte &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001998 {
1999 RValue<SByte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002000 val += SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002001 return res;
2002 }
2003
Nicolas Capens96d4e092016-11-18 14:22:38 -05002004 const SByte &operator++(SByte &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002005 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002006 val += SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002007 return val;
2008 }
2009
Nicolas Capens96d4e092016-11-18 14:22:38 -05002010 RValue<SByte> operator--(SByte &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002011 {
2012 RValue<SByte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002013 val -= SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002014 return res;
2015 }
2016
Nicolas Capens96d4e092016-11-18 14:22:38 -05002017 const SByte &operator--(SByte &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002018 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002019 val -= SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002020 return val;
2021 }
2022
2023 RValue<Bool> operator<(RValue<SByte> lhs, RValue<SByte> rhs)
2024 {
2025 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
2026 }
2027
2028 RValue<Bool> operator<=(RValue<SByte> lhs, RValue<SByte> rhs)
2029 {
2030 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
2031 }
2032
2033 RValue<Bool> operator>(RValue<SByte> lhs, RValue<SByte> rhs)
2034 {
2035 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
2036 }
2037
2038 RValue<Bool> operator>=(RValue<SByte> lhs, RValue<SByte> rhs)
2039 {
2040 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
2041 }
2042
2043 RValue<Bool> operator!=(RValue<SByte> lhs, RValue<SByte> rhs)
2044 {
2045 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
2046 }
2047
2048 RValue<Bool> operator==(RValue<SByte> lhs, RValue<SByte> rhs)
2049 {
2050 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
2051 }
2052
2053 Type *SByte::getType()
2054 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002055 return T(Ice::IceType_i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002056 }
2057
2058 Short::Short(Argument<Short> argument)
2059 {
2060 storeValue(argument.value);
2061 }
2062
2063 Short::Short(RValue<Int> cast)
2064 {
2065 Value *integer = Nucleus::createTrunc(cast.value, Short::getType());
2066
2067 storeValue(integer);
2068 }
2069
Nicolas Capens598f8d82016-09-26 15:09:10 -04002070 Short::Short(short x)
2071 {
2072 storeValue(Nucleus::createConstantShort(x));
2073 }
2074
2075 Short::Short(RValue<Short> rhs)
2076 {
2077 storeValue(rhs.value);
2078 }
2079
2080 Short::Short(const Short &rhs)
2081 {
2082 Value *value = rhs.loadValue();
2083 storeValue(value);
2084 }
2085
2086 Short::Short(const Reference<Short> &rhs)
2087 {
2088 Value *value = rhs.loadValue();
2089 storeValue(value);
2090 }
2091
Nicolas Capens96d4e092016-11-18 14:22:38 -05002092 RValue<Short> Short::operator=(RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002093 {
2094 storeValue(rhs.value);
2095
2096 return rhs;
2097 }
2098
Nicolas Capens96d4e092016-11-18 14:22:38 -05002099 RValue<Short> Short::operator=(const Short &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002100 {
2101 Value *value = rhs.loadValue();
2102 storeValue(value);
2103
2104 return RValue<Short>(value);
2105 }
2106
Nicolas Capens96d4e092016-11-18 14:22:38 -05002107 RValue<Short> Short::operator=(const Reference<Short> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002108 {
2109 Value *value = rhs.loadValue();
2110 storeValue(value);
2111
2112 return RValue<Short>(value);
2113 }
2114
2115 RValue<Short> operator+(RValue<Short> lhs, RValue<Short> rhs)
2116 {
2117 return RValue<Short>(Nucleus::createAdd(lhs.value, rhs.value));
2118 }
2119
2120 RValue<Short> operator-(RValue<Short> lhs, RValue<Short> rhs)
2121 {
2122 return RValue<Short>(Nucleus::createSub(lhs.value, rhs.value));
2123 }
2124
2125 RValue<Short> operator*(RValue<Short> lhs, RValue<Short> rhs)
2126 {
2127 return RValue<Short>(Nucleus::createMul(lhs.value, rhs.value));
2128 }
2129
2130 RValue<Short> operator/(RValue<Short> lhs, RValue<Short> rhs)
2131 {
2132 return RValue<Short>(Nucleus::createSDiv(lhs.value, rhs.value));
2133 }
2134
2135 RValue<Short> operator%(RValue<Short> lhs, RValue<Short> rhs)
2136 {
2137 return RValue<Short>(Nucleus::createSRem(lhs.value, rhs.value));
2138 }
2139
2140 RValue<Short> operator&(RValue<Short> lhs, RValue<Short> rhs)
2141 {
2142 return RValue<Short>(Nucleus::createAnd(lhs.value, rhs.value));
2143 }
2144
2145 RValue<Short> operator|(RValue<Short> lhs, RValue<Short> rhs)
2146 {
2147 return RValue<Short>(Nucleus::createOr(lhs.value, rhs.value));
2148 }
2149
2150 RValue<Short> operator^(RValue<Short> lhs, RValue<Short> rhs)
2151 {
2152 return RValue<Short>(Nucleus::createXor(lhs.value, rhs.value));
2153 }
2154
2155 RValue<Short> operator<<(RValue<Short> lhs, RValue<Short> rhs)
2156 {
2157 return RValue<Short>(Nucleus::createShl(lhs.value, rhs.value));
2158 }
2159
2160 RValue<Short> operator>>(RValue<Short> lhs, RValue<Short> rhs)
2161 {
2162 return RValue<Short>(Nucleus::createAShr(lhs.value, rhs.value));
2163 }
2164
Nicolas Capens96d4e092016-11-18 14:22:38 -05002165 RValue<Short> operator+=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002166 {
2167 return lhs = lhs + rhs;
2168 }
2169
Nicolas Capens96d4e092016-11-18 14:22:38 -05002170 RValue<Short> operator-=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002171 {
2172 return lhs = lhs - rhs;
2173 }
2174
Nicolas Capens96d4e092016-11-18 14:22:38 -05002175 RValue<Short> operator*=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002176 {
2177 return lhs = lhs * rhs;
2178 }
2179
Nicolas Capens96d4e092016-11-18 14:22:38 -05002180 RValue<Short> operator/=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002181 {
2182 return lhs = lhs / rhs;
2183 }
2184
Nicolas Capens96d4e092016-11-18 14:22:38 -05002185 RValue<Short> operator%=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002186 {
2187 return lhs = lhs % rhs;
2188 }
2189
Nicolas Capens96d4e092016-11-18 14:22:38 -05002190 RValue<Short> operator&=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002191 {
2192 return lhs = lhs & rhs;
2193 }
2194
Nicolas Capens96d4e092016-11-18 14:22:38 -05002195 RValue<Short> operator|=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002196 {
2197 return lhs = lhs | rhs;
2198 }
2199
Nicolas Capens96d4e092016-11-18 14:22:38 -05002200 RValue<Short> operator^=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002201 {
2202 return lhs = lhs ^ rhs;
2203 }
2204
Nicolas Capens96d4e092016-11-18 14:22:38 -05002205 RValue<Short> operator<<=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002206 {
2207 return lhs = lhs << rhs;
2208 }
2209
Nicolas Capens96d4e092016-11-18 14:22:38 -05002210 RValue<Short> operator>>=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002211 {
2212 return lhs = lhs >> rhs;
2213 }
2214
2215 RValue<Short> operator+(RValue<Short> val)
2216 {
2217 return val;
2218 }
2219
2220 RValue<Short> operator-(RValue<Short> val)
2221 {
2222 return RValue<Short>(Nucleus::createNeg(val.value));
2223 }
2224
2225 RValue<Short> operator~(RValue<Short> val)
2226 {
2227 return RValue<Short>(Nucleus::createNot(val.value));
2228 }
2229
Nicolas Capens96d4e092016-11-18 14:22:38 -05002230 RValue<Short> operator++(Short &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002231 {
2232 RValue<Short> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002233 val += Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002234 return res;
2235 }
2236
Nicolas Capens96d4e092016-11-18 14:22:38 -05002237 const Short &operator++(Short &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002238 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002239 val += Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002240 return val;
2241 }
2242
Nicolas Capens96d4e092016-11-18 14:22:38 -05002243 RValue<Short> operator--(Short &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002244 {
2245 RValue<Short> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002246 val -= Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002247 return res;
2248 }
2249
Nicolas Capens96d4e092016-11-18 14:22:38 -05002250 const Short &operator--(Short &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002251 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002252 val -= Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002253 return val;
2254 }
2255
2256 RValue<Bool> operator<(RValue<Short> lhs, RValue<Short> rhs)
2257 {
2258 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
2259 }
2260
2261 RValue<Bool> operator<=(RValue<Short> lhs, RValue<Short> rhs)
2262 {
2263 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
2264 }
2265
2266 RValue<Bool> operator>(RValue<Short> lhs, RValue<Short> rhs)
2267 {
2268 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
2269 }
2270
2271 RValue<Bool> operator>=(RValue<Short> lhs, RValue<Short> rhs)
2272 {
2273 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
2274 }
2275
2276 RValue<Bool> operator!=(RValue<Short> lhs, RValue<Short> rhs)
2277 {
2278 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
2279 }
2280
2281 RValue<Bool> operator==(RValue<Short> lhs, RValue<Short> rhs)
2282 {
2283 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
2284 }
2285
2286 Type *Short::getType()
2287 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002288 return T(Ice::IceType_i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002289 }
2290
2291 UShort::UShort(Argument<UShort> argument)
2292 {
2293 storeValue(argument.value);
2294 }
2295
2296 UShort::UShort(RValue<UInt> cast)
2297 {
2298 Value *integer = Nucleus::createTrunc(cast.value, UShort::getType());
2299
2300 storeValue(integer);
2301 }
2302
2303 UShort::UShort(RValue<Int> cast)
2304 {
2305 Value *integer = Nucleus::createTrunc(cast.value, UShort::getType());
2306
2307 storeValue(integer);
2308 }
2309
Nicolas Capens598f8d82016-09-26 15:09:10 -04002310 UShort::UShort(unsigned short x)
2311 {
2312 storeValue(Nucleus::createConstantShort(x));
2313 }
2314
2315 UShort::UShort(RValue<UShort> rhs)
2316 {
2317 storeValue(rhs.value);
2318 }
2319
2320 UShort::UShort(const UShort &rhs)
2321 {
2322 Value *value = rhs.loadValue();
2323 storeValue(value);
2324 }
2325
2326 UShort::UShort(const Reference<UShort> &rhs)
2327 {
2328 Value *value = rhs.loadValue();
2329 storeValue(value);
2330 }
2331
Nicolas Capens96d4e092016-11-18 14:22:38 -05002332 RValue<UShort> UShort::operator=(RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002333 {
2334 storeValue(rhs.value);
2335
2336 return rhs;
2337 }
2338
Nicolas Capens96d4e092016-11-18 14:22:38 -05002339 RValue<UShort> UShort::operator=(const UShort &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002340 {
2341 Value *value = rhs.loadValue();
2342 storeValue(value);
2343
2344 return RValue<UShort>(value);
2345 }
2346
Nicolas Capens96d4e092016-11-18 14:22:38 -05002347 RValue<UShort> UShort::operator=(const Reference<UShort> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002348 {
2349 Value *value = rhs.loadValue();
2350 storeValue(value);
2351
2352 return RValue<UShort>(value);
2353 }
2354
2355 RValue<UShort> operator+(RValue<UShort> lhs, RValue<UShort> rhs)
2356 {
2357 return RValue<UShort>(Nucleus::createAdd(lhs.value, rhs.value));
2358 }
2359
2360 RValue<UShort> operator-(RValue<UShort> lhs, RValue<UShort> rhs)
2361 {
2362 return RValue<UShort>(Nucleus::createSub(lhs.value, rhs.value));
2363 }
2364
2365 RValue<UShort> operator*(RValue<UShort> lhs, RValue<UShort> rhs)
2366 {
2367 return RValue<UShort>(Nucleus::createMul(lhs.value, rhs.value));
2368 }
2369
2370 RValue<UShort> operator/(RValue<UShort> lhs, RValue<UShort> rhs)
2371 {
2372 return RValue<UShort>(Nucleus::createUDiv(lhs.value, rhs.value));
2373 }
2374
2375 RValue<UShort> operator%(RValue<UShort> lhs, RValue<UShort> rhs)
2376 {
2377 return RValue<UShort>(Nucleus::createURem(lhs.value, rhs.value));
2378 }
2379
2380 RValue<UShort> operator&(RValue<UShort> lhs, RValue<UShort> rhs)
2381 {
2382 return RValue<UShort>(Nucleus::createAnd(lhs.value, rhs.value));
2383 }
2384
2385 RValue<UShort> operator|(RValue<UShort> lhs, RValue<UShort> rhs)
2386 {
2387 return RValue<UShort>(Nucleus::createOr(lhs.value, rhs.value));
2388 }
2389
2390 RValue<UShort> operator^(RValue<UShort> lhs, RValue<UShort> rhs)
2391 {
2392 return RValue<UShort>(Nucleus::createXor(lhs.value, rhs.value));
2393 }
2394
2395 RValue<UShort> operator<<(RValue<UShort> lhs, RValue<UShort> rhs)
2396 {
2397 return RValue<UShort>(Nucleus::createShl(lhs.value, rhs.value));
2398 }
2399
2400 RValue<UShort> operator>>(RValue<UShort> lhs, RValue<UShort> rhs)
2401 {
2402 return RValue<UShort>(Nucleus::createLShr(lhs.value, rhs.value));
2403 }
2404
Nicolas Capens96d4e092016-11-18 14:22:38 -05002405 RValue<UShort> operator+=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002406 {
2407 return lhs = lhs + rhs;
2408 }
2409
Nicolas Capens96d4e092016-11-18 14:22:38 -05002410 RValue<UShort> operator-=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002411 {
2412 return lhs = lhs - rhs;
2413 }
2414
Nicolas Capens96d4e092016-11-18 14:22:38 -05002415 RValue<UShort> operator*=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002416 {
2417 return lhs = lhs * rhs;
2418 }
2419
Nicolas Capens96d4e092016-11-18 14:22:38 -05002420 RValue<UShort> operator/=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002421 {
2422 return lhs = lhs / rhs;
2423 }
2424
Nicolas Capens96d4e092016-11-18 14:22:38 -05002425 RValue<UShort> operator%=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002426 {
2427 return lhs = lhs % rhs;
2428 }
2429
Nicolas Capens96d4e092016-11-18 14:22:38 -05002430 RValue<UShort> operator&=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002431 {
2432 return lhs = lhs & rhs;
2433 }
2434
Nicolas Capens96d4e092016-11-18 14:22:38 -05002435 RValue<UShort> operator|=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002436 {
2437 return lhs = lhs | rhs;
2438 }
2439
Nicolas Capens96d4e092016-11-18 14:22:38 -05002440 RValue<UShort> operator^=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002441 {
2442 return lhs = lhs ^ rhs;
2443 }
2444
Nicolas Capens96d4e092016-11-18 14:22:38 -05002445 RValue<UShort> operator<<=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002446 {
2447 return lhs = lhs << rhs;
2448 }
2449
Nicolas Capens96d4e092016-11-18 14:22:38 -05002450 RValue<UShort> operator>>=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002451 {
2452 return lhs = lhs >> rhs;
2453 }
2454
2455 RValue<UShort> operator+(RValue<UShort> val)
2456 {
2457 return val;
2458 }
2459
2460 RValue<UShort> operator-(RValue<UShort> val)
2461 {
2462 return RValue<UShort>(Nucleus::createNeg(val.value));
2463 }
2464
2465 RValue<UShort> operator~(RValue<UShort> val)
2466 {
2467 return RValue<UShort>(Nucleus::createNot(val.value));
2468 }
2469
Nicolas Capens96d4e092016-11-18 14:22:38 -05002470 RValue<UShort> operator++(UShort &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002471 {
2472 RValue<UShort> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002473 val += UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002474 return res;
2475 }
2476
Nicolas Capens96d4e092016-11-18 14:22:38 -05002477 const UShort &operator++(UShort &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002478 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002479 val += UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002480 return val;
2481 }
2482
Nicolas Capens96d4e092016-11-18 14:22:38 -05002483 RValue<UShort> operator--(UShort &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002484 {
2485 RValue<UShort> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002486 val -= UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002487 return res;
2488 }
2489
Nicolas Capens96d4e092016-11-18 14:22:38 -05002490 const UShort &operator--(UShort &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002491 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002492 val -= UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002493 return val;
2494 }
2495
2496 RValue<Bool> operator<(RValue<UShort> lhs, RValue<UShort> rhs)
2497 {
2498 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
2499 }
2500
2501 RValue<Bool> operator<=(RValue<UShort> lhs, RValue<UShort> rhs)
2502 {
2503 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
2504 }
2505
2506 RValue<Bool> operator>(RValue<UShort> lhs, RValue<UShort> rhs)
2507 {
2508 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
2509 }
2510
2511 RValue<Bool> operator>=(RValue<UShort> lhs, RValue<UShort> rhs)
2512 {
2513 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
2514 }
2515
2516 RValue<Bool> operator!=(RValue<UShort> lhs, RValue<UShort> rhs)
2517 {
2518 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
2519 }
2520
2521 RValue<Bool> operator==(RValue<UShort> lhs, RValue<UShort> rhs)
2522 {
2523 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
2524 }
2525
2526 Type *UShort::getType()
2527 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002528 return T(Ice::IceType_i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002529 }
2530
Nicolas Capens16b5f152016-10-13 13:39:01 -04002531 Byte4::Byte4(RValue<Byte8> cast)
2532 {
Nicolas Capens16b5f152016-10-13 13:39:01 -04002533 storeValue(Nucleus::createBitCast(cast.value, getType()));
2534 }
2535
2536 Byte4::Byte4(const Reference<Byte4> &rhs)
2537 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002538 Value *value = rhs.loadValue();
2539 storeValue(value);
Nicolas Capens16b5f152016-10-13 13:39:01 -04002540 }
2541
Nicolas Capens598f8d82016-09-26 15:09:10 -04002542 Type *Byte4::getType()
2543 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002544 return T(Type_v4i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002545 }
2546
2547 Type *SByte4::getType()
2548 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002549 return T(Type_v4i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002550 }
2551
Nicolas Capens598f8d82016-09-26 15:09:10 -04002552 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)
2553 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04002554 int64_t constantVector[8] = {x0, x1, x2, x3, x4, x5, x6, x7};
2555 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002556 }
2557
2558 Byte8::Byte8(RValue<Byte8> rhs)
2559 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002560 storeValue(rhs.value);
2561 }
2562
2563 Byte8::Byte8(const Byte8 &rhs)
2564 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002565 Value *value = rhs.loadValue();
2566 storeValue(value);
2567 }
2568
2569 Byte8::Byte8(const Reference<Byte8> &rhs)
2570 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002571 Value *value = rhs.loadValue();
2572 storeValue(value);
2573 }
2574
Nicolas Capens96d4e092016-11-18 14:22:38 -05002575 RValue<Byte8> Byte8::operator=(RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002576 {
2577 storeValue(rhs.value);
2578
2579 return rhs;
2580 }
2581
Nicolas Capens96d4e092016-11-18 14:22:38 -05002582 RValue<Byte8> Byte8::operator=(const Byte8 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002583 {
2584 Value *value = rhs.loadValue();
2585 storeValue(value);
2586
2587 return RValue<Byte8>(value);
2588 }
2589
Nicolas Capens96d4e092016-11-18 14:22:38 -05002590 RValue<Byte8> Byte8::operator=(const Reference<Byte8> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002591 {
2592 Value *value = rhs.loadValue();
2593 storeValue(value);
2594
2595 return RValue<Byte8>(value);
2596 }
2597
2598 RValue<Byte8> operator+(RValue<Byte8> lhs, RValue<Byte8> rhs)
2599 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002600 return RValue<Byte8>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002601 }
2602
2603 RValue<Byte8> operator-(RValue<Byte8> lhs, RValue<Byte8> rhs)
2604 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002605 return RValue<Byte8>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002606 }
2607
2608// RValue<Byte8> operator*(RValue<Byte8> lhs, RValue<Byte8> rhs)
2609// {
2610// return RValue<Byte8>(Nucleus::createMul(lhs.value, rhs.value));
2611// }
2612
2613// RValue<Byte8> operator/(RValue<Byte8> lhs, RValue<Byte8> rhs)
2614// {
2615// return RValue<Byte8>(Nucleus::createUDiv(lhs.value, rhs.value));
2616// }
2617
2618// RValue<Byte8> operator%(RValue<Byte8> lhs, RValue<Byte8> rhs)
2619// {
2620// return RValue<Byte8>(Nucleus::createURem(lhs.value, rhs.value));
2621// }
2622
2623 RValue<Byte8> operator&(RValue<Byte8> lhs, RValue<Byte8> rhs)
2624 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002625 return RValue<Byte8>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002626 }
2627
2628 RValue<Byte8> operator|(RValue<Byte8> lhs, RValue<Byte8> rhs)
2629 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002630 return RValue<Byte8>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002631 }
2632
2633 RValue<Byte8> operator^(RValue<Byte8> lhs, RValue<Byte8> rhs)
2634 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002635 return RValue<Byte8>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002636 }
2637
2638// RValue<Byte8> operator<<(RValue<Byte8> lhs, unsigned char rhs)
2639// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002640// return RValue<Byte8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002641// }
2642
2643// RValue<Byte8> operator>>(RValue<Byte8> lhs, unsigned char rhs)
2644// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002645// return RValue<Byte8>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002646// }
2647
Nicolas Capens96d4e092016-11-18 14:22:38 -05002648 RValue<Byte8> operator+=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002649 {
2650 return lhs = lhs + rhs;
2651 }
2652
Nicolas Capens96d4e092016-11-18 14:22:38 -05002653 RValue<Byte8> operator-=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002654 {
2655 return lhs = lhs - rhs;
2656 }
2657
Nicolas Capens96d4e092016-11-18 14:22:38 -05002658// RValue<Byte8> operator*=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002659// {
2660// return lhs = lhs * rhs;
2661// }
2662
Nicolas Capens96d4e092016-11-18 14:22:38 -05002663// RValue<Byte8> operator/=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002664// {
2665// return lhs = lhs / rhs;
2666// }
2667
Nicolas Capens96d4e092016-11-18 14:22:38 -05002668// RValue<Byte8> operator%=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002669// {
2670// return lhs = lhs % rhs;
2671// }
2672
Nicolas Capens96d4e092016-11-18 14:22:38 -05002673 RValue<Byte8> operator&=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002674 {
2675 return lhs = lhs & rhs;
2676 }
2677
Nicolas Capens96d4e092016-11-18 14:22:38 -05002678 RValue<Byte8> operator|=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002679 {
2680 return lhs = lhs | rhs;
2681 }
2682
Nicolas Capens96d4e092016-11-18 14:22:38 -05002683 RValue<Byte8> operator^=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002684 {
2685 return lhs = lhs ^ rhs;
2686 }
2687
Nicolas Capens96d4e092016-11-18 14:22:38 -05002688// RValue<Byte8> operator<<=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002689// {
2690// return lhs = lhs << rhs;
2691// }
2692
Nicolas Capens96d4e092016-11-18 14:22:38 -05002693// RValue<Byte8> operator>>=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002694// {
2695// return lhs = lhs >> rhs;
2696// }
2697
2698// RValue<Byte8> operator+(RValue<Byte8> val)
2699// {
2700// return val;
2701// }
2702
2703// RValue<Byte8> operator-(RValue<Byte8> val)
2704// {
2705// return RValue<Byte8>(Nucleus::createNeg(val.value));
2706// }
2707
2708 RValue<Byte8> operator~(RValue<Byte8> val)
2709 {
2710 return RValue<Byte8>(Nucleus::createNot(val.value));
2711 }
2712
2713 RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y)
2714 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002715 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2716 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2717 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2718 auto paddusb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2719 paddusb->addArg(x.value);
2720 paddusb->addArg(y.value);
2721 ::basicBlock->appendInst(paddusb);
2722
2723 return RValue<Byte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002724 }
2725
2726 RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y)
2727 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002728 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2729 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2730 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2731 auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2732 psubusw->addArg(x.value);
2733 psubusw->addArg(y.value);
2734 ::basicBlock->appendInst(psubusw);
2735
2736 return RValue<Byte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002737 }
2738
2739 RValue<Short4> Unpack(RValue<Byte4> x)
2740 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04002741 int shuffle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}; // Real type is v16i8
Nicolas Capensbea4dce2017-07-24 16:54:44 -04002742 return As<Short4>(Nucleus::createShuffleVector(x.value, x.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002743 }
2744
Nicolas Capens411273e2017-01-26 15:13:36 -08002745 RValue<Short4> Unpack(RValue<Byte4> x, RValue<Byte4> y)
2746 {
2747 return UnpackLow(As<Byte8>(x), As<Byte8>(y));
2748 }
2749
Nicolas Capens598f8d82016-09-26 15:09:10 -04002750 RValue<Short4> UnpackLow(RValue<Byte8> x, RValue<Byte8> y)
2751 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04002752 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
Nicolas Capensbea4dce2017-07-24 16:54:44 -04002753 return As<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002754 }
2755
2756 RValue<Short4> UnpackHigh(RValue<Byte8> x, RValue<Byte8> y)
2757 {
Nicolas Capens20e22c42016-10-25 17:32:37 -04002758 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
2759 auto lowHigh = RValue<Byte16>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
2760 return As<Short4>(Swizzle(As<Int4>(lowHigh), 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002761 }
2762
2763 RValue<Int> SignMask(RValue<Byte8> x)
2764 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002765 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
2766 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2767 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2768 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
2769 movmsk->addArg(x.value);
2770 ::basicBlock->appendInst(movmsk);
2771
2772 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002773 }
2774
2775// RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y)
2776// {
Nicolas Capens2f970b62016-11-08 14:28:59 -05002777// return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002778// }
2779
2780 RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y)
2781 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002782 return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002783 }
2784
2785 Type *Byte8::getType()
2786 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002787 return T(Type_v8i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002788 }
2789
Nicolas Capens598f8d82016-09-26 15:09:10 -04002790 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)
2791 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04002792 int64_t constantVector[8] = { x0, x1, x2, x3, x4, x5, x6, x7 };
2793 Value *vector = V(Nucleus::createConstantVector(constantVector, getType()));
2794
2795 storeValue(Nucleus::createBitCast(vector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002796 }
2797
Nicolas Capens598f8d82016-09-26 15:09:10 -04002798 SByte8::SByte8(RValue<SByte8> rhs)
2799 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002800 storeValue(rhs.value);
2801 }
2802
2803 SByte8::SByte8(const SByte8 &rhs)
2804 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002805 Value *value = rhs.loadValue();
2806 storeValue(value);
2807 }
2808
2809 SByte8::SByte8(const Reference<SByte8> &rhs)
2810 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002811 Value *value = rhs.loadValue();
2812 storeValue(value);
2813 }
2814
Nicolas Capens96d4e092016-11-18 14:22:38 -05002815 RValue<SByte8> SByte8::operator=(RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002816 {
2817 storeValue(rhs.value);
2818
2819 return rhs;
2820 }
2821
Nicolas Capens96d4e092016-11-18 14:22:38 -05002822 RValue<SByte8> SByte8::operator=(const SByte8 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002823 {
2824 Value *value = rhs.loadValue();
2825 storeValue(value);
2826
2827 return RValue<SByte8>(value);
2828 }
2829
Nicolas Capens96d4e092016-11-18 14:22:38 -05002830 RValue<SByte8> SByte8::operator=(const Reference<SByte8> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002831 {
2832 Value *value = rhs.loadValue();
2833 storeValue(value);
2834
2835 return RValue<SByte8>(value);
2836 }
2837
2838 RValue<SByte8> operator+(RValue<SByte8> lhs, RValue<SByte8> rhs)
2839 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002840 return RValue<SByte8>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002841 }
2842
2843 RValue<SByte8> operator-(RValue<SByte8> lhs, RValue<SByte8> rhs)
2844 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002845 return RValue<SByte8>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002846 }
2847
2848// RValue<SByte8> operator*(RValue<SByte8> lhs, RValue<SByte8> rhs)
2849// {
2850// return RValue<SByte8>(Nucleus::createMul(lhs.value, rhs.value));
2851// }
2852
2853// RValue<SByte8> operator/(RValue<SByte8> lhs, RValue<SByte8> rhs)
2854// {
2855// return RValue<SByte8>(Nucleus::createSDiv(lhs.value, rhs.value));
2856// }
2857
2858// RValue<SByte8> operator%(RValue<SByte8> lhs, RValue<SByte8> rhs)
2859// {
2860// return RValue<SByte8>(Nucleus::createSRem(lhs.value, rhs.value));
2861// }
2862
2863 RValue<SByte8> operator&(RValue<SByte8> lhs, RValue<SByte8> rhs)
2864 {
2865 return RValue<SByte8>(Nucleus::createAnd(lhs.value, rhs.value));
2866 }
2867
2868 RValue<SByte8> operator|(RValue<SByte8> lhs, RValue<SByte8> rhs)
2869 {
2870 return RValue<SByte8>(Nucleus::createOr(lhs.value, rhs.value));
2871 }
2872
2873 RValue<SByte8> operator^(RValue<SByte8> lhs, RValue<SByte8> rhs)
2874 {
2875 return RValue<SByte8>(Nucleus::createXor(lhs.value, rhs.value));
2876 }
2877
2878// RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs)
2879// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002880// return RValue<SByte8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002881// }
2882
2883// RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs)
2884// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002885// return RValue<SByte8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002886// }
2887
Nicolas Capens96d4e092016-11-18 14:22:38 -05002888 RValue<SByte8> operator+=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002889 {
2890 return lhs = lhs + rhs;
2891 }
2892
Nicolas Capens96d4e092016-11-18 14:22:38 -05002893 RValue<SByte8> operator-=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002894 {
2895 return lhs = lhs - rhs;
2896 }
2897
Nicolas Capens96d4e092016-11-18 14:22:38 -05002898// RValue<SByte8> operator*=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002899// {
2900// return lhs = lhs * rhs;
2901// }
2902
Nicolas Capens96d4e092016-11-18 14:22:38 -05002903// RValue<SByte8> operator/=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002904// {
2905// return lhs = lhs / rhs;
2906// }
2907
Nicolas Capens96d4e092016-11-18 14:22:38 -05002908// RValue<SByte8> operator%=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002909// {
2910// return lhs = lhs % rhs;
2911// }
2912
Nicolas Capens96d4e092016-11-18 14:22:38 -05002913 RValue<SByte8> operator&=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002914 {
2915 return lhs = lhs & rhs;
2916 }
2917
Nicolas Capens96d4e092016-11-18 14:22:38 -05002918 RValue<SByte8> operator|=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002919 {
2920 return lhs = lhs | rhs;
2921 }
2922
Nicolas Capens96d4e092016-11-18 14:22:38 -05002923 RValue<SByte8> operator^=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002924 {
2925 return lhs = lhs ^ rhs;
2926 }
2927
Nicolas Capens96d4e092016-11-18 14:22:38 -05002928// RValue<SByte8> operator<<=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002929// {
2930// return lhs = lhs << rhs;
2931// }
2932
Nicolas Capens96d4e092016-11-18 14:22:38 -05002933// RValue<SByte8> operator>>=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002934// {
2935// return lhs = lhs >> rhs;
2936// }
2937
2938// RValue<SByte8> operator+(RValue<SByte8> val)
2939// {
2940// return val;
2941// }
2942
2943// RValue<SByte8> operator-(RValue<SByte8> val)
2944// {
2945// return RValue<SByte8>(Nucleus::createNeg(val.value));
2946// }
2947
2948 RValue<SByte8> operator~(RValue<SByte8> val)
2949 {
2950 return RValue<SByte8>(Nucleus::createNot(val.value));
2951 }
2952
2953 RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y)
2954 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002955 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2956 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2957 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2958 auto paddsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2959 paddsb->addArg(x.value);
2960 paddsb->addArg(y.value);
2961 ::basicBlock->appendInst(paddsb);
2962
2963 return RValue<SByte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002964 }
2965
2966 RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y)
2967 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002968 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2969 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2970 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2971 auto psubsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2972 psubsb->addArg(x.value);
2973 psubsb->addArg(y.value);
2974 ::basicBlock->appendInst(psubsb);
2975
2976 return RValue<SByte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002977 }
2978
2979 RValue<Short4> UnpackLow(RValue<SByte8> x, RValue<SByte8> y)
2980 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04002981 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
Nicolas Capensbea4dce2017-07-24 16:54:44 -04002982 return As<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002983 }
2984
2985 RValue<Short4> UnpackHigh(RValue<SByte8> x, RValue<SByte8> y)
2986 {
Nicolas Capens20e22c42016-10-25 17:32:37 -04002987 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
2988 auto lowHigh = RValue<Byte16>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
2989 return As<Short4>(Swizzle(As<Int4>(lowHigh), 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002990 }
2991
2992 RValue<Int> SignMask(RValue<SByte8> x)
2993 {
Nicolas Capensf2cb9df2016-10-21 17:26:13 -04002994 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
2995 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2996 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2997 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
2998 movmsk->addArg(x.value);
2999 ::basicBlock->appendInst(movmsk);
3000
3001 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003002 }
3003
3004 RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y)
3005 {
Nicolas Capens2f970b62016-11-08 14:28:59 -05003006 return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003007 }
3008
3009 RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y)
3010 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003011 return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003012 }
3013
3014 Type *SByte8::getType()
3015 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04003016 return T(Type_v8i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003017 }
3018
3019 Byte16::Byte16(RValue<Byte16> rhs)
3020 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003021 storeValue(rhs.value);
3022 }
3023
3024 Byte16::Byte16(const Byte16 &rhs)
3025 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003026 Value *value = rhs.loadValue();
3027 storeValue(value);
3028 }
3029
3030 Byte16::Byte16(const Reference<Byte16> &rhs)
3031 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003032 Value *value = rhs.loadValue();
3033 storeValue(value);
3034 }
3035
Nicolas Capens96d4e092016-11-18 14:22:38 -05003036 RValue<Byte16> Byte16::operator=(RValue<Byte16> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003037 {
3038 storeValue(rhs.value);
3039
3040 return rhs;
3041 }
3042
Nicolas Capens96d4e092016-11-18 14:22:38 -05003043 RValue<Byte16> Byte16::operator=(const Byte16 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003044 {
3045 Value *value = rhs.loadValue();
3046 storeValue(value);
3047
3048 return RValue<Byte16>(value);
3049 }
3050
Nicolas Capens96d4e092016-11-18 14:22:38 -05003051 RValue<Byte16> Byte16::operator=(const Reference<Byte16> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003052 {
3053 Value *value = rhs.loadValue();
3054 storeValue(value);
3055
3056 return RValue<Byte16>(value);
3057 }
3058
3059 Type *Byte16::getType()
3060 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003061 return T(Ice::IceType_v16i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003062 }
3063
3064 Type *SByte16::getType()
3065 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003066 return T(Ice::IceType_v16i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003067 }
3068
Nicolas Capens16b5f152016-10-13 13:39:01 -04003069 Short2::Short2(RValue<Short4> cast)
3070 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05003071 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003072 }
3073
3074 Type *Short2::getType()
3075 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003076 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04003077 }
3078
3079 UShort2::UShort2(RValue<UShort4> cast)
3080 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05003081 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003082 }
3083
3084 Type *UShort2::getType()
3085 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003086 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04003087 }
3088
Nicolas Capens598f8d82016-09-26 15:09:10 -04003089 Short4::Short4(RValue<Int> cast)
3090 {
Nicolas Capensd4227962016-11-09 14:24:25 -05003091 Value *vector = loadValue();
Nicolas Capensbf22bbf2017-01-13 17:37:45 -05003092 Value *element = Nucleus::createTrunc(cast.value, Short::getType());
3093 Value *insert = Nucleus::createInsertElement(vector, element, 0);
Nicolas Capensd4227962016-11-09 14:24:25 -05003094 Value *swizzle = Swizzle(RValue<Short4>(insert), 0x00).value;
Nicolas Capens598f8d82016-09-26 15:09:10 -04003095
3096 storeValue(swizzle);
3097 }
3098
3099 Short4::Short4(RValue<Int4> cast)
3100 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08003101 int select[8] = {0, 2, 4, 6, 0, 2, 4, 6};
3102 Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType());
3103 Value *packed = Nucleus::createShuffleVector(short8, short8, select);
Nicolas Capensd4227962016-11-09 14:24:25 -05003104
Nicolas Capensbea4dce2017-07-24 16:54:44 -04003105 Value *int2 = RValue<Int2>(Int2(As<Int4>(packed))).value;
Nicolas Capensd4227962016-11-09 14:24:25 -05003106 Value *short4 = Nucleus::createBitCast(int2, Short4::getType());
3107
3108 storeValue(short4);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003109 }
3110
3111// Short4::Short4(RValue<Float> cast)
3112// {
3113// }
3114
3115 Short4::Short4(RValue<Float4> cast)
3116 {
3117 assert(false && "UNIMPLEMENTED");
3118 }
3119
Nicolas Capens598f8d82016-09-26 15:09:10 -04003120 Short4::Short4(short xyzw)
3121 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003122 int64_t constantVector[4] = {xyzw, xyzw, xyzw, xyzw};
3123 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003124 }
3125
3126 Short4::Short4(short x, short y, short z, short w)
3127 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003128 int64_t constantVector[4] = {x, y, z, w};
3129 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003130 }
3131
3132 Short4::Short4(RValue<Short4> rhs)
3133 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003134 storeValue(rhs.value);
3135 }
3136
3137 Short4::Short4(const Short4 &rhs)
3138 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003139 Value *value = rhs.loadValue();
3140 storeValue(value);
3141 }
3142
3143 Short4::Short4(const Reference<Short4> &rhs)
3144 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003145 Value *value = rhs.loadValue();
3146 storeValue(value);
3147 }
3148
3149 Short4::Short4(RValue<UShort4> rhs)
3150 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003151 storeValue(rhs.value);
3152 }
3153
3154 Short4::Short4(const UShort4 &rhs)
3155 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003156 storeValue(rhs.loadValue());
3157 }
3158
3159 Short4::Short4(const Reference<UShort4> &rhs)
3160 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003161 storeValue(rhs.loadValue());
3162 }
3163
Nicolas Capens96d4e092016-11-18 14:22:38 -05003164 RValue<Short4> Short4::operator=(RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003165 {
3166 storeValue(rhs.value);
3167
3168 return rhs;
3169 }
3170
Nicolas Capens96d4e092016-11-18 14:22:38 -05003171 RValue<Short4> Short4::operator=(const Short4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003172 {
3173 Value *value = rhs.loadValue();
3174 storeValue(value);
3175
3176 return RValue<Short4>(value);
3177 }
3178
Nicolas Capens96d4e092016-11-18 14:22:38 -05003179 RValue<Short4> Short4::operator=(const Reference<Short4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003180 {
3181 Value *value = rhs.loadValue();
3182 storeValue(value);
3183
3184 return RValue<Short4>(value);
3185 }
3186
Nicolas Capens96d4e092016-11-18 14:22:38 -05003187 RValue<Short4> Short4::operator=(RValue<UShort4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003188 {
3189 storeValue(rhs.value);
3190
3191 return RValue<Short4>(rhs);
3192 }
3193
Nicolas Capens96d4e092016-11-18 14:22:38 -05003194 RValue<Short4> Short4::operator=(const UShort4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003195 {
3196 Value *value = rhs.loadValue();
3197 storeValue(value);
3198
3199 return RValue<Short4>(value);
3200 }
3201
Nicolas Capens96d4e092016-11-18 14:22:38 -05003202 RValue<Short4> Short4::operator=(const Reference<UShort4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003203 {
3204 Value *value = rhs.loadValue();
3205 storeValue(value);
3206
3207 return RValue<Short4>(value);
3208 }
3209
3210 RValue<Short4> operator+(RValue<Short4> lhs, RValue<Short4> rhs)
3211 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003212 return RValue<Short4>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003213 }
3214
3215 RValue<Short4> operator-(RValue<Short4> lhs, RValue<Short4> rhs)
3216 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003217 return RValue<Short4>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003218 }
3219
3220 RValue<Short4> operator*(RValue<Short4> lhs, RValue<Short4> rhs)
3221 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003222 return RValue<Short4>(Nucleus::createMul(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003223 }
3224
3225// RValue<Short4> operator/(RValue<Short4> lhs, RValue<Short4> rhs)
3226// {
3227// return RValue<Short4>(Nucleus::createSDiv(lhs.value, rhs.value));
3228// }
3229
3230// RValue<Short4> operator%(RValue<Short4> lhs, RValue<Short4> rhs)
3231// {
3232// return RValue<Short4>(Nucleus::createSRem(lhs.value, rhs.value));
3233// }
3234
3235 RValue<Short4> operator&(RValue<Short4> lhs, RValue<Short4> rhs)
3236 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003237 return RValue<Short4>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003238 }
3239
3240 RValue<Short4> operator|(RValue<Short4> lhs, RValue<Short4> rhs)
3241 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003242 return RValue<Short4>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003243 }
3244
3245 RValue<Short4> operator^(RValue<Short4> lhs, RValue<Short4> rhs)
3246 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003247 return RValue<Short4>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003248 }
3249
3250 RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs)
3251 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003252 return RValue<Short4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003253 }
3254
3255 RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs)
3256 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003257 return RValue<Short4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003258 }
3259
Nicolas Capens96d4e092016-11-18 14:22:38 -05003260 RValue<Short4> operator+=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003261 {
3262 return lhs = lhs + rhs;
3263 }
3264
Nicolas Capens96d4e092016-11-18 14:22:38 -05003265 RValue<Short4> operator-=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003266 {
3267 return lhs = lhs - rhs;
3268 }
3269
Nicolas Capens96d4e092016-11-18 14:22:38 -05003270 RValue<Short4> operator*=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003271 {
3272 return lhs = lhs * rhs;
3273 }
3274
Nicolas Capens96d4e092016-11-18 14:22:38 -05003275// RValue<Short4> operator/=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003276// {
3277// return lhs = lhs / rhs;
3278// }
3279
Nicolas Capens96d4e092016-11-18 14:22:38 -05003280// RValue<Short4> operator%=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003281// {
3282// return lhs = lhs % rhs;
3283// }
3284
Nicolas Capens96d4e092016-11-18 14:22:38 -05003285 RValue<Short4> operator&=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003286 {
3287 return lhs = lhs & rhs;
3288 }
3289
Nicolas Capens96d4e092016-11-18 14:22:38 -05003290 RValue<Short4> operator|=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003291 {
3292 return lhs = lhs | rhs;
3293 }
3294
Nicolas Capens96d4e092016-11-18 14:22:38 -05003295 RValue<Short4> operator^=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003296 {
3297 return lhs = lhs ^ rhs;
3298 }
3299
Nicolas Capens96d4e092016-11-18 14:22:38 -05003300 RValue<Short4> operator<<=(Short4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003301 {
3302 return lhs = lhs << rhs;
3303 }
3304
Nicolas Capens96d4e092016-11-18 14:22:38 -05003305 RValue<Short4> operator>>=(Short4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003306 {
3307 return lhs = lhs >> rhs;
3308 }
3309
Nicolas Capens598f8d82016-09-26 15:09:10 -04003310// RValue<Short4> operator+(RValue<Short4> val)
3311// {
3312// return val;
3313// }
3314
3315 RValue<Short4> operator-(RValue<Short4> val)
3316 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05003317 return RValue<Short4>(Nucleus::createNeg(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003318 }
3319
3320 RValue<Short4> operator~(RValue<Short4> val)
3321 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05003322 return RValue<Short4>(Nucleus::createNot(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003323 }
3324
3325 RValue<Short4> RoundShort4(RValue<Float4> cast)
3326 {
Nicolas Capensd4227962016-11-09 14:24:25 -05003327 RValue<Int4> int4 = RoundInt(cast);
3328 return As<Short4>(Pack(int4, int4));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003329 }
3330
3331 RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y)
3332 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003333 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3334 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value, y.value);
3335 ::basicBlock->appendInst(cmp);
3336
3337 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3338 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3339 ::basicBlock->appendInst(select);
3340
3341 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003342 }
3343
3344 RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y)
3345 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003346 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3347 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value);
3348 ::basicBlock->appendInst(cmp);
3349
3350 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3351 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3352 ::basicBlock->appendInst(select);
3353
3354 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003355 }
3356
3357 RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y)
3358 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003359 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3360 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3361 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3362 auto paddsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3363 paddsw->addArg(x.value);
3364 paddsw->addArg(y.value);
3365 ::basicBlock->appendInst(paddsw);
3366
3367 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003368 }
3369
3370 RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y)
3371 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003372 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3373 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3374 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3375 auto psubsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3376 psubsw->addArg(x.value);
3377 psubsw->addArg(y.value);
3378 ::basicBlock->appendInst(psubsw);
3379
3380 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003381 }
3382
3383 RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y)
3384 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003385 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3386 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3387 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3388 auto pmulhw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3389 pmulhw->addArg(x.value);
3390 pmulhw->addArg(y.value);
3391 ::basicBlock->appendInst(pmulhw);
3392
Nicolas Capens5b41ba32016-12-08 14:34:00 -05003393 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003394 }
3395
3396 RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y)
3397 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003398 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3399 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyAddPairs, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3400 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3401 auto pmaddwd = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3402 pmaddwd->addArg(x.value);
3403 pmaddwd->addArg(y.value);
3404 ::basicBlock->appendInst(pmaddwd);
3405
Nicolas Capensbea4dce2017-07-24 16:54:44 -04003406 return As<Int2>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003407 }
3408
3409 RValue<SByte8> Pack(RValue<Short4> x, RValue<Short4> y)
3410 {
Nicolas Capensec54a172016-10-25 17:32:37 -04003411 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
3412 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3413 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3414 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3415 pack->addArg(x.value);
3416 pack->addArg(y.value);
3417 ::basicBlock->appendInst(pack);
3418
Nicolas Capens70dfff42016-10-27 10:20:28 -04003419 return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x88));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003420 }
3421
3422 RValue<Int2> UnpackLow(RValue<Short4> x, RValue<Short4> y)
3423 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04003424 int shuffle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; // Real type is v8i16
Nicolas Capensbea4dce2017-07-24 16:54:44 -04003425 return As<Int2>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003426 }
3427
3428 RValue<Int2> UnpackHigh(RValue<Short4> x, RValue<Short4> y)
3429 {
Nicolas Capens20e22c42016-10-25 17:32:37 -04003430 int shuffle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; // Real type is v8i16
3431 auto lowHigh = RValue<Short8>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
3432 return As<Int2>(Swizzle(As<Int4>(lowHigh), 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003433 }
3434
3435 RValue<Short4> Swizzle(RValue<Short4> x, unsigned char select)
3436 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04003437 // Real type is v8i16
3438 int shuffle[8] =
3439 {
3440 (select >> 0) & 0x03,
3441 (select >> 2) & 0x03,
3442 (select >> 4) & 0x03,
3443 (select >> 6) & 0x03,
3444 (select >> 0) & 0x03,
3445 (select >> 2) & 0x03,
3446 (select >> 4) & 0x03,
3447 (select >> 6) & 0x03,
3448 };
3449
3450 return RValue<Short4>(Nucleus::createShuffleVector(x.value, x.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003451 }
3452
3453 RValue<Short4> Insert(RValue<Short4> val, RValue<Short> element, int i)
3454 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05003455 return RValue<Short4>(Nucleus::createInsertElement(val.value, element.value, i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003456 }
3457
3458 RValue<Short> Extract(RValue<Short4> val, int i)
3459 {
Nicolas Capens0133d0f2017-01-16 16:25:08 -05003460 return RValue<Short>(Nucleus::createExtractElement(val.value, Short::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003461 }
3462
3463 RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y)
3464 {
Nicolas Capens2f970b62016-11-08 14:28:59 -05003465 return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003466 }
3467
3468 RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y)
3469 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003470 return RValue<Short4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003471 }
3472
3473 Type *Short4::getType()
3474 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003475 return T(Type_v4i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003476 }
3477
3478 UShort4::UShort4(RValue<Int4> cast)
3479 {
3480 *this = Short4(cast);
3481 }
3482
3483 UShort4::UShort4(RValue<Float4> cast, bool saturate)
3484 {
Nicolas Capensd4227962016-11-09 14:24:25 -05003485 if(saturate)
3486 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003487 if(CPUID::SSE4_1)
Nicolas Capensd4227962016-11-09 14:24:25 -05003488 {
3489 Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation
3490 *this = As<Short4>(Pack(As<UInt4>(int4), As<UInt4>(int4)));
3491 }
3492 else
3493 {
3494 *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000))));
3495 }
3496 }
3497 else
3498 {
3499 *this = Short4(Int4(cast));
3500 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04003501 }
3502
Nicolas Capens598f8d82016-09-26 15:09:10 -04003503 UShort4::UShort4(unsigned short xyzw)
3504 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003505 int64_t constantVector[4] = {xyzw, xyzw, xyzw, xyzw};
3506 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003507 }
3508
3509 UShort4::UShort4(unsigned short x, unsigned short y, unsigned short z, unsigned short w)
3510 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003511 int64_t constantVector[4] = {x, y, z, w};
3512 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003513 }
3514
3515 UShort4::UShort4(RValue<UShort4> rhs)
3516 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003517 storeValue(rhs.value);
3518 }
3519
3520 UShort4::UShort4(const UShort4 &rhs)
3521 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003522 Value *value = rhs.loadValue();
3523 storeValue(value);
3524 }
3525
3526 UShort4::UShort4(const Reference<UShort4> &rhs)
3527 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003528 Value *value = rhs.loadValue();
3529 storeValue(value);
3530 }
3531
3532 UShort4::UShort4(RValue<Short4> rhs)
3533 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003534 storeValue(rhs.value);
3535 }
3536
3537 UShort4::UShort4(const Short4 &rhs)
3538 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003539 Value *value = rhs.loadValue();
3540 storeValue(value);
3541 }
3542
3543 UShort4::UShort4(const Reference<Short4> &rhs)
3544 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003545 Value *value = rhs.loadValue();
3546 storeValue(value);
3547 }
3548
Nicolas Capens96d4e092016-11-18 14:22:38 -05003549 RValue<UShort4> UShort4::operator=(RValue<UShort4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003550 {
3551 storeValue(rhs.value);
3552
3553 return rhs;
3554 }
3555
Nicolas Capens96d4e092016-11-18 14:22:38 -05003556 RValue<UShort4> UShort4::operator=(const UShort4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003557 {
3558 Value *value = rhs.loadValue();
3559 storeValue(value);
3560
3561 return RValue<UShort4>(value);
3562 }
3563
Nicolas Capens96d4e092016-11-18 14:22:38 -05003564 RValue<UShort4> UShort4::operator=(const Reference<UShort4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003565 {
3566 Value *value = rhs.loadValue();
3567 storeValue(value);
3568
3569 return RValue<UShort4>(value);
3570 }
3571
Nicolas Capens96d4e092016-11-18 14:22:38 -05003572 RValue<UShort4> UShort4::operator=(RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003573 {
3574 storeValue(rhs.value);
3575
3576 return RValue<UShort4>(rhs);
3577 }
3578
Nicolas Capens96d4e092016-11-18 14:22:38 -05003579 RValue<UShort4> UShort4::operator=(const Short4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003580 {
3581 Value *value = rhs.loadValue();
3582 storeValue(value);
3583
3584 return RValue<UShort4>(value);
3585 }
3586
Nicolas Capens96d4e092016-11-18 14:22:38 -05003587 RValue<UShort4> UShort4::operator=(const Reference<Short4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003588 {
3589 Value *value = rhs.loadValue();
3590 storeValue(value);
3591
3592 return RValue<UShort4>(value);
3593 }
3594
3595 RValue<UShort4> operator+(RValue<UShort4> lhs, RValue<UShort4> rhs)
3596 {
Nicolas Capens5b41ba32016-12-08 14:34:00 -05003597 return RValue<UShort4>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003598 }
3599
3600 RValue<UShort4> operator-(RValue<UShort4> lhs, RValue<UShort4> rhs)
3601 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003602 return RValue<UShort4>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003603 }
3604
3605 RValue<UShort4> operator*(RValue<UShort4> lhs, RValue<UShort4> rhs)
3606 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003607 return RValue<UShort4>(Nucleus::createMul(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003608 }
3609
Nicolas Capens16b5f152016-10-13 13:39:01 -04003610 RValue<UShort4> operator&(RValue<UShort4> lhs, RValue<UShort4> rhs)
3611 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003612 return RValue<UShort4>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003613 }
3614
3615 RValue<UShort4> operator|(RValue<UShort4> lhs, RValue<UShort4> rhs)
3616 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003617 return RValue<UShort4>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003618 }
3619
3620 RValue<UShort4> operator^(RValue<UShort4> lhs, RValue<UShort4> rhs)
3621 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003622 return RValue<UShort4>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003623 }
3624
Nicolas Capens598f8d82016-09-26 15:09:10 -04003625 RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs)
3626 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003627 return RValue<UShort4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003628 }
3629
3630 RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs)
3631 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003632 return RValue<UShort4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003633 }
3634
Nicolas Capens96d4e092016-11-18 14:22:38 -05003635 RValue<UShort4> operator<<=(UShort4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003636 {
3637 return lhs = lhs << rhs;
3638 }
3639
Nicolas Capens96d4e092016-11-18 14:22:38 -05003640 RValue<UShort4> operator>>=(UShort4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003641 {
3642 return lhs = lhs >> rhs;
3643 }
3644
Nicolas Capens598f8d82016-09-26 15:09:10 -04003645 RValue<UShort4> operator~(RValue<UShort4> val)
3646 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05003647 return RValue<UShort4>(Nucleus::createNot(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003648 }
3649
3650 RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y)
3651 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003652 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3653 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value, y.value);
3654 ::basicBlock->appendInst(cmp);
3655
3656 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3657 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3658 ::basicBlock->appendInst(select);
3659
3660 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003661 }
3662
3663 RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y)
3664 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003665 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3666 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value);
3667 ::basicBlock->appendInst(cmp);
3668
3669 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3670 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3671 ::basicBlock->appendInst(select);
3672
3673 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003674 }
3675
3676 RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y)
3677 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003678 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3679 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3680 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3681 auto paddusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3682 paddusw->addArg(x.value);
3683 paddusw->addArg(y.value);
3684 ::basicBlock->appendInst(paddusw);
3685
3686 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003687 }
3688
3689 RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y)
3690 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003691 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3692 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3693 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3694 auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3695 psubusw->addArg(x.value);
3696 psubusw->addArg(y.value);
3697 ::basicBlock->appendInst(psubusw);
3698
3699 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003700 }
3701
3702 RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y)
3703 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003704 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3705 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3706 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3707 auto pmulhuw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3708 pmulhuw->addArg(x.value);
3709 pmulhuw->addArg(y.value);
3710 ::basicBlock->appendInst(pmulhuw);
3711
3712 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003713 }
3714
3715 RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)
3716 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003717 assert(false && "UNIMPLEMENTED"); return RValue<UShort4>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003718 }
3719
3720 RValue<Byte8> Pack(RValue<UShort4> x, RValue<UShort4> y)
3721 {
Nicolas Capensec54a172016-10-25 17:32:37 -04003722 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
3723 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3724 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3725 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3726 pack->addArg(x.value);
3727 pack->addArg(y.value);
3728 ::basicBlock->appendInst(pack);
3729
Nicolas Capens70dfff42016-10-27 10:20:28 -04003730 return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x88));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003731 }
3732
3733 Type *UShort4::getType()
3734 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003735 return T(Type_v4i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003736 }
3737
Nicolas Capens3e7062b2017-01-17 14:01:33 -05003738 Short8::Short8(short c)
3739 {
3740 int64_t constantVector[8] = {c, c, c, c, c, c, c, c};
3741 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3742 }
3743
Nicolas Capens598f8d82016-09-26 15:09:10 -04003744 Short8::Short8(short c0, short c1, short c2, short c3, short c4, short c5, short c6, short c7)
3745 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003746 int64_t constantVector[8] = {c0, c1, c2, c3, c4, c5, c6, c7};
3747 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003748 }
3749
3750 Short8::Short8(RValue<Short8> rhs)
3751 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003752 storeValue(rhs.value);
3753 }
3754
3755 Short8::Short8(const Reference<Short8> &rhs)
3756 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003757 Value *value = rhs.loadValue();
3758 storeValue(value);
3759 }
3760
3761 Short8::Short8(RValue<Short4> lo, RValue<Short4> hi)
3762 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05003763 int shuffle[8] = {0, 1, 2, 3, 8, 9, 10, 11}; // Real type is v8i16
3764 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
3765
3766 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003767 }
3768
3769 RValue<Short8> operator+(RValue<Short8> lhs, RValue<Short8> rhs)
3770 {
3771 return RValue<Short8>(Nucleus::createAdd(lhs.value, rhs.value));
3772 }
3773
3774 RValue<Short8> operator&(RValue<Short8> lhs, RValue<Short8> rhs)
3775 {
3776 return RValue<Short8>(Nucleus::createAnd(lhs.value, rhs.value));
3777 }
3778
3779 RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs)
3780 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003781 return RValue<Short8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003782 }
3783
3784 RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs)
3785 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003786 return RValue<Short8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003787 }
3788
3789 RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)
3790 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003791 assert(false && "UNIMPLEMENTED"); return RValue<Int4>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003792 }
3793
3794 RValue<Int4> Abs(RValue<Int4> x)
3795 {
Nicolas Capens84272242016-11-09 13:31:06 -05003796 auto negative = x >> 31;
3797 return (x ^ negative) - negative;
Nicolas Capens598f8d82016-09-26 15:09:10 -04003798 }
3799
3800 RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)
3801 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003802 assert(false && "UNIMPLEMENTED"); return RValue<Short8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003803 }
3804
3805 Type *Short8::getType()
3806 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04003807 return T(Ice::IceType_v8i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003808 }
3809
Nicolas Capens3e7062b2017-01-17 14:01:33 -05003810 UShort8::UShort8(unsigned short c)
3811 {
3812 int64_t constantVector[8] = {c, c, c, c, c, c, c, c};
3813 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3814 }
3815
Nicolas Capens598f8d82016-09-26 15:09:10 -04003816 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)
3817 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003818 int64_t constantVector[8] = {c0, c1, c2, c3, c4, c5, c6, c7};
3819 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003820 }
3821
3822 UShort8::UShort8(RValue<UShort8> rhs)
3823 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003824 storeValue(rhs.value);
3825 }
3826
3827 UShort8::UShort8(const Reference<UShort8> &rhs)
3828 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003829 Value *value = rhs.loadValue();
3830 storeValue(value);
3831 }
3832
3833 UShort8::UShort8(RValue<UShort4> lo, RValue<UShort4> hi)
3834 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05003835 int shuffle[8] = {0, 1, 2, 3, 8, 9, 10, 11}; // Real type is v8i16
3836 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
3837
3838 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003839 }
3840
Nicolas Capens96d4e092016-11-18 14:22:38 -05003841 RValue<UShort8> UShort8::operator=(RValue<UShort8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003842 {
3843 storeValue(rhs.value);
3844
3845 return rhs;
3846 }
3847
Nicolas Capens96d4e092016-11-18 14:22:38 -05003848 RValue<UShort8> UShort8::operator=(const UShort8 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003849 {
3850 Value *value = rhs.loadValue();
3851 storeValue(value);
3852
3853 return RValue<UShort8>(value);
3854 }
3855
Nicolas Capens96d4e092016-11-18 14:22:38 -05003856 RValue<UShort8> UShort8::operator=(const Reference<UShort8> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003857 {
3858 Value *value = rhs.loadValue();
3859 storeValue(value);
3860
3861 return RValue<UShort8>(value);
3862 }
3863
3864 RValue<UShort8> operator&(RValue<UShort8> lhs, RValue<UShort8> rhs)
3865 {
3866 return RValue<UShort8>(Nucleus::createAnd(lhs.value, rhs.value));
3867 }
3868
3869 RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs)
3870 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003871 return RValue<UShort8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003872 }
3873
3874 RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs)
3875 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003876 return RValue<UShort8>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003877 }
3878
3879 RValue<UShort8> operator+(RValue<UShort8> lhs, RValue<UShort8> rhs)
3880 {
3881 return RValue<UShort8>(Nucleus::createAdd(lhs.value, rhs.value));
3882 }
3883
3884 RValue<UShort8> operator*(RValue<UShort8> lhs, RValue<UShort8> rhs)
3885 {
3886 return RValue<UShort8>(Nucleus::createMul(lhs.value, rhs.value));
3887 }
3888
Nicolas Capens96d4e092016-11-18 14:22:38 -05003889 RValue<UShort8> operator+=(UShort8 &lhs, RValue<UShort8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003890 {
3891 return lhs = lhs + rhs;
3892 }
3893
3894 RValue<UShort8> operator~(RValue<UShort8> val)
3895 {
3896 return RValue<UShort8>(Nucleus::createNot(val.value));
3897 }
3898
3899 RValue<UShort8> Swizzle(RValue<UShort8> x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7)
3900 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003901 assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003902 }
3903
3904 RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)
3905 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003906 assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003907 }
3908
3909 // FIXME: Implement as Shuffle(x, y, Select(i0, ..., i16)) and Shuffle(x, y, SELECT_PACK_REPEAT(element))
3910// RValue<UShort8> PackRepeat(RValue<Byte16> x, RValue<Byte16> y, int element)
3911// {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003912// assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003913// }
3914
3915 Type *UShort8::getType()
3916 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04003917 return T(Ice::IceType_v8i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003918 }
3919
3920 Int::Int(Argument<Int> argument)
3921 {
3922 storeValue(argument.value);
3923 }
3924
3925 Int::Int(RValue<Byte> cast)
3926 {
3927 Value *integer = Nucleus::createZExt(cast.value, Int::getType());
3928
3929 storeValue(integer);
3930 }
3931
3932 Int::Int(RValue<SByte> cast)
3933 {
3934 Value *integer = Nucleus::createSExt(cast.value, Int::getType());
3935
3936 storeValue(integer);
3937 }
3938
3939 Int::Int(RValue<Short> cast)
3940 {
3941 Value *integer = Nucleus::createSExt(cast.value, Int::getType());
3942
3943 storeValue(integer);
3944 }
3945
3946 Int::Int(RValue<UShort> cast)
3947 {
3948 Value *integer = Nucleus::createZExt(cast.value, Int::getType());
3949
3950 storeValue(integer);
3951 }
3952
3953 Int::Int(RValue<Int2> cast)
3954 {
3955 *this = Extract(cast, 0);
3956 }
3957
3958 Int::Int(RValue<Long> cast)
3959 {
3960 Value *integer = Nucleus::createTrunc(cast.value, Int::getType());
3961
3962 storeValue(integer);
3963 }
3964
3965 Int::Int(RValue<Float> cast)
3966 {
3967 Value *integer = Nucleus::createFPToSI(cast.value, Int::getType());
3968
3969 storeValue(integer);
3970 }
3971
Nicolas Capens598f8d82016-09-26 15:09:10 -04003972 Int::Int(int x)
3973 {
3974 storeValue(Nucleus::createConstantInt(x));
3975 }
3976
3977 Int::Int(RValue<Int> rhs)
3978 {
3979 storeValue(rhs.value);
3980 }
3981
3982 Int::Int(RValue<UInt> rhs)
3983 {
3984 storeValue(rhs.value);
3985 }
3986
3987 Int::Int(const Int &rhs)
3988 {
3989 Value *value = rhs.loadValue();
3990 storeValue(value);
3991 }
3992
3993 Int::Int(const Reference<Int> &rhs)
3994 {
3995 Value *value = rhs.loadValue();
3996 storeValue(value);
3997 }
3998
3999 Int::Int(const UInt &rhs)
4000 {
4001 Value *value = rhs.loadValue();
4002 storeValue(value);
4003 }
4004
4005 Int::Int(const Reference<UInt> &rhs)
4006 {
4007 Value *value = rhs.loadValue();
4008 storeValue(value);
4009 }
4010
Nicolas Capens96d4e092016-11-18 14:22:38 -05004011 RValue<Int> Int::operator=(int rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004012 {
4013 return RValue<Int>(storeValue(Nucleus::createConstantInt(rhs)));
4014 }
4015
Nicolas Capens96d4e092016-11-18 14:22:38 -05004016 RValue<Int> Int::operator=(RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004017 {
4018 storeValue(rhs.value);
4019
4020 return rhs;
4021 }
4022
Nicolas Capens96d4e092016-11-18 14:22:38 -05004023 RValue<Int> Int::operator=(RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004024 {
4025 storeValue(rhs.value);
4026
4027 return RValue<Int>(rhs);
4028 }
4029
Nicolas Capens96d4e092016-11-18 14:22:38 -05004030 RValue<Int> Int::operator=(const Int &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004031 {
4032 Value *value = rhs.loadValue();
4033 storeValue(value);
4034
4035 return RValue<Int>(value);
4036 }
4037
Nicolas Capens96d4e092016-11-18 14:22:38 -05004038 RValue<Int> Int::operator=(const Reference<Int> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004039 {
4040 Value *value = rhs.loadValue();
4041 storeValue(value);
4042
4043 return RValue<Int>(value);
4044 }
4045
Nicolas Capens96d4e092016-11-18 14:22:38 -05004046 RValue<Int> Int::operator=(const UInt &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004047 {
4048 Value *value = rhs.loadValue();
4049 storeValue(value);
4050
4051 return RValue<Int>(value);
4052 }
4053
Nicolas Capens96d4e092016-11-18 14:22:38 -05004054 RValue<Int> Int::operator=(const Reference<UInt> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004055 {
4056 Value *value = rhs.loadValue();
4057 storeValue(value);
4058
4059 return RValue<Int>(value);
4060 }
4061
4062 RValue<Int> operator+(RValue<Int> lhs, RValue<Int> rhs)
4063 {
4064 return RValue<Int>(Nucleus::createAdd(lhs.value, rhs.value));
4065 }
4066
4067 RValue<Int> operator-(RValue<Int> lhs, RValue<Int> rhs)
4068 {
4069 return RValue<Int>(Nucleus::createSub(lhs.value, rhs.value));
4070 }
4071
4072 RValue<Int> operator*(RValue<Int> lhs, RValue<Int> rhs)
4073 {
4074 return RValue<Int>(Nucleus::createMul(lhs.value, rhs.value));
4075 }
4076
4077 RValue<Int> operator/(RValue<Int> lhs, RValue<Int> rhs)
4078 {
4079 return RValue<Int>(Nucleus::createSDiv(lhs.value, rhs.value));
4080 }
4081
4082 RValue<Int> operator%(RValue<Int> lhs, RValue<Int> rhs)
4083 {
4084 return RValue<Int>(Nucleus::createSRem(lhs.value, rhs.value));
4085 }
4086
4087 RValue<Int> operator&(RValue<Int> lhs, RValue<Int> rhs)
4088 {
4089 return RValue<Int>(Nucleus::createAnd(lhs.value, rhs.value));
4090 }
4091
4092 RValue<Int> operator|(RValue<Int> lhs, RValue<Int> rhs)
4093 {
4094 return RValue<Int>(Nucleus::createOr(lhs.value, rhs.value));
4095 }
4096
4097 RValue<Int> operator^(RValue<Int> lhs, RValue<Int> rhs)
4098 {
4099 return RValue<Int>(Nucleus::createXor(lhs.value, rhs.value));
4100 }
4101
4102 RValue<Int> operator<<(RValue<Int> lhs, RValue<Int> rhs)
4103 {
4104 return RValue<Int>(Nucleus::createShl(lhs.value, rhs.value));
4105 }
4106
4107 RValue<Int> operator>>(RValue<Int> lhs, RValue<Int> rhs)
4108 {
4109 return RValue<Int>(Nucleus::createAShr(lhs.value, rhs.value));
4110 }
4111
Nicolas Capens96d4e092016-11-18 14:22:38 -05004112 RValue<Int> operator+=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004113 {
4114 return lhs = lhs + rhs;
4115 }
4116
Nicolas Capens96d4e092016-11-18 14:22:38 -05004117 RValue<Int> operator-=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004118 {
4119 return lhs = lhs - rhs;
4120 }
4121
Nicolas Capens96d4e092016-11-18 14:22:38 -05004122 RValue<Int> operator*=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004123 {
4124 return lhs = lhs * rhs;
4125 }
4126
Nicolas Capens96d4e092016-11-18 14:22:38 -05004127 RValue<Int> operator/=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004128 {
4129 return lhs = lhs / rhs;
4130 }
4131
Nicolas Capens96d4e092016-11-18 14:22:38 -05004132 RValue<Int> operator%=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004133 {
4134 return lhs = lhs % rhs;
4135 }
4136
Nicolas Capens96d4e092016-11-18 14:22:38 -05004137 RValue<Int> operator&=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004138 {
4139 return lhs = lhs & rhs;
4140 }
4141
Nicolas Capens96d4e092016-11-18 14:22:38 -05004142 RValue<Int> operator|=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004143 {
4144 return lhs = lhs | rhs;
4145 }
4146
Nicolas Capens96d4e092016-11-18 14:22:38 -05004147 RValue<Int> operator^=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004148 {
4149 return lhs = lhs ^ rhs;
4150 }
4151
Nicolas Capens96d4e092016-11-18 14:22:38 -05004152 RValue<Int> operator<<=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004153 {
4154 return lhs = lhs << rhs;
4155 }
4156
Nicolas Capens96d4e092016-11-18 14:22:38 -05004157 RValue<Int> operator>>=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004158 {
4159 return lhs = lhs >> rhs;
4160 }
4161
4162 RValue<Int> operator+(RValue<Int> val)
4163 {
4164 return val;
4165 }
4166
4167 RValue<Int> operator-(RValue<Int> val)
4168 {
4169 return RValue<Int>(Nucleus::createNeg(val.value));
4170 }
4171
4172 RValue<Int> operator~(RValue<Int> val)
4173 {
4174 return RValue<Int>(Nucleus::createNot(val.value));
4175 }
4176
Nicolas Capens96d4e092016-11-18 14:22:38 -05004177 RValue<Int> operator++(Int &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004178 {
Nicolas Capens5b41ba32016-12-08 14:34:00 -05004179 RValue<Int> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05004180 val += 1;
4181 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004182 }
4183
Nicolas Capens96d4e092016-11-18 14:22:38 -05004184 const Int &operator++(Int &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004185 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004186 val += 1;
4187 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004188 }
4189
Nicolas Capens96d4e092016-11-18 14:22:38 -05004190 RValue<Int> operator--(Int &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004191 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004192 RValue<Int> res = val;
4193 val -= 1;
4194 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004195 }
4196
Nicolas Capens96d4e092016-11-18 14:22:38 -05004197 const Int &operator--(Int &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004198 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004199 val -= 1;
4200 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004201 }
4202
4203 RValue<Bool> operator<(RValue<Int> lhs, RValue<Int> rhs)
4204 {
4205 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
4206 }
4207
4208 RValue<Bool> operator<=(RValue<Int> lhs, RValue<Int> rhs)
4209 {
4210 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
4211 }
4212
4213 RValue<Bool> operator>(RValue<Int> lhs, RValue<Int> rhs)
4214 {
4215 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
4216 }
4217
4218 RValue<Bool> operator>=(RValue<Int> lhs, RValue<Int> rhs)
4219 {
4220 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
4221 }
4222
4223 RValue<Bool> operator!=(RValue<Int> lhs, RValue<Int> rhs)
4224 {
4225 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
4226 }
4227
4228 RValue<Bool> operator==(RValue<Int> lhs, RValue<Int> rhs)
4229 {
4230 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
4231 }
4232
4233 RValue<Int> Max(RValue<Int> x, RValue<Int> y)
4234 {
4235 return IfThenElse(x > y, x, y);
4236 }
4237
4238 RValue<Int> Min(RValue<Int> x, RValue<Int> y)
4239 {
4240 return IfThenElse(x < y, x, y);
4241 }
4242
4243 RValue<Int> Clamp(RValue<Int> x, RValue<Int> min, RValue<Int> max)
4244 {
4245 return Min(Max(x, min), max);
4246 }
4247
4248 RValue<Int> RoundInt(RValue<Float> cast)
4249 {
Nicolas Capensf7b75882017-04-26 09:30:47 -04004250 if(emulateIntrinsics)
4251 {
4252 // Push the fractional part off the mantissa. Accurate up to +/-2^22.
4253 return Int((cast + Float(0x00C00000)) - Float(0x00C00000));
4254 }
4255 else
4256 {
4257 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
4258 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
4259 auto target = ::context->getConstantUndef(Ice::IceType_i32);
4260 auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
4261 nearbyint->addArg(cast.value);
4262 ::basicBlock->appendInst(nearbyint);
Nicolas Capensa8086512016-11-07 17:32:17 -05004263
Nicolas Capensf7b75882017-04-26 09:30:47 -04004264 return RValue<Int>(V(result));
4265 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04004266 }
4267
4268 Type *Int::getType()
4269 {
4270 return T(Ice::IceType_i32);
4271 }
4272
4273 Long::Long(RValue<Int> cast)
4274 {
4275 Value *integer = Nucleus::createSExt(cast.value, Long::getType());
4276
4277 storeValue(integer);
4278 }
4279
4280 Long::Long(RValue<UInt> cast)
4281 {
4282 Value *integer = Nucleus::createZExt(cast.value, Long::getType());
4283
4284 storeValue(integer);
4285 }
4286
Nicolas Capens598f8d82016-09-26 15:09:10 -04004287 Long::Long(RValue<Long> rhs)
4288 {
4289 storeValue(rhs.value);
4290 }
4291
Nicolas Capens96d4e092016-11-18 14:22:38 -05004292 RValue<Long> Long::operator=(int64_t rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004293 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04004294 return RValue<Long>(storeValue(Nucleus::createConstantLong(rhs)));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004295 }
4296
Nicolas Capens96d4e092016-11-18 14:22:38 -05004297 RValue<Long> Long::operator=(RValue<Long> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004298 {
4299 storeValue(rhs.value);
4300
4301 return rhs;
4302 }
4303
Nicolas Capens96d4e092016-11-18 14:22:38 -05004304 RValue<Long> Long::operator=(const Long &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004305 {
4306 Value *value = rhs.loadValue();
4307 storeValue(value);
4308
4309 return RValue<Long>(value);
4310 }
4311
Nicolas Capens96d4e092016-11-18 14:22:38 -05004312 RValue<Long> Long::operator=(const Reference<Long> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004313 {
4314 Value *value = rhs.loadValue();
4315 storeValue(value);
4316
4317 return RValue<Long>(value);
4318 }
4319
4320 RValue<Long> operator+(RValue<Long> lhs, RValue<Long> rhs)
4321 {
4322 return RValue<Long>(Nucleus::createAdd(lhs.value, rhs.value));
4323 }
4324
4325 RValue<Long> operator-(RValue<Long> lhs, RValue<Long> rhs)
4326 {
4327 return RValue<Long>(Nucleus::createSub(lhs.value, rhs.value));
4328 }
4329
Nicolas Capens96d4e092016-11-18 14:22:38 -05004330 RValue<Long> operator+=(Long &lhs, RValue<Long> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004331 {
4332 return lhs = lhs + rhs;
4333 }
4334
Nicolas Capens96d4e092016-11-18 14:22:38 -05004335 RValue<Long> operator-=(Long &lhs, RValue<Long> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004336 {
4337 return lhs = lhs - rhs;
4338 }
4339
4340 RValue<Long> AddAtomic(RValue<Pointer<Long> > x, RValue<Long> y)
4341 {
4342 return RValue<Long>(Nucleus::createAtomicAdd(x.value, y.value));
4343 }
4344
4345 Type *Long::getType()
4346 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04004347 return T(Ice::IceType_i64);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004348 }
4349
Nicolas Capens598f8d82016-09-26 15:09:10 -04004350 UInt::UInt(Argument<UInt> argument)
4351 {
4352 storeValue(argument.value);
4353 }
4354
4355 UInt::UInt(RValue<UShort> cast)
4356 {
4357 Value *integer = Nucleus::createZExt(cast.value, UInt::getType());
4358
4359 storeValue(integer);
4360 }
4361
4362 UInt::UInt(RValue<Long> cast)
4363 {
4364 Value *integer = Nucleus::createTrunc(cast.value, UInt::getType());
4365
4366 storeValue(integer);
4367 }
4368
4369 UInt::UInt(RValue<Float> cast)
4370 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05004371 // Smallest positive value representable in UInt, but not in Int
4372 const unsigned int ustart = 0x80000000u;
4373 const float ustartf = float(ustart);
4374
4375 // If the value is negative, store 0, otherwise store the result of the conversion
4376 storeValue((~(As<Int>(cast) >> 31) &
4377 // Check if the value can be represented as an Int
4378 IfThenElse(cast >= ustartf,
4379 // If the value is too large, subtract ustart and re-add it after conversion.
4380 As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)),
4381 // Otherwise, just convert normally
4382 Int(cast))).value);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004383 }
4384
Nicolas Capens598f8d82016-09-26 15:09:10 -04004385 UInt::UInt(int x)
4386 {
4387 storeValue(Nucleus::createConstantInt(x));
4388 }
4389
4390 UInt::UInt(unsigned int x)
4391 {
4392 storeValue(Nucleus::createConstantInt(x));
4393 }
4394
4395 UInt::UInt(RValue<UInt> rhs)
4396 {
4397 storeValue(rhs.value);
4398 }
4399
4400 UInt::UInt(RValue<Int> rhs)
4401 {
4402 storeValue(rhs.value);
4403 }
4404
4405 UInt::UInt(const UInt &rhs)
4406 {
4407 Value *value = rhs.loadValue();
4408 storeValue(value);
4409 }
4410
4411 UInt::UInt(const Reference<UInt> &rhs)
4412 {
4413 Value *value = rhs.loadValue();
4414 storeValue(value);
4415 }
4416
4417 UInt::UInt(const Int &rhs)
4418 {
4419 Value *value = rhs.loadValue();
4420 storeValue(value);
4421 }
4422
4423 UInt::UInt(const Reference<Int> &rhs)
4424 {
4425 Value *value = rhs.loadValue();
4426 storeValue(value);
4427 }
4428
Nicolas Capens96d4e092016-11-18 14:22:38 -05004429 RValue<UInt> UInt::operator=(unsigned int rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004430 {
4431 return RValue<UInt>(storeValue(Nucleus::createConstantInt(rhs)));
4432 }
4433
Nicolas Capens96d4e092016-11-18 14:22:38 -05004434 RValue<UInt> UInt::operator=(RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004435 {
4436 storeValue(rhs.value);
4437
4438 return rhs;
4439 }
4440
Nicolas Capens96d4e092016-11-18 14:22:38 -05004441 RValue<UInt> UInt::operator=(RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004442 {
4443 storeValue(rhs.value);
4444
4445 return RValue<UInt>(rhs);
4446 }
4447
Nicolas Capens96d4e092016-11-18 14:22:38 -05004448 RValue<UInt> UInt::operator=(const UInt &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004449 {
4450 Value *value = rhs.loadValue();
4451 storeValue(value);
4452
4453 return RValue<UInt>(value);
4454 }
4455
Nicolas Capens96d4e092016-11-18 14:22:38 -05004456 RValue<UInt> UInt::operator=(const Reference<UInt> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004457 {
4458 Value *value = rhs.loadValue();
4459 storeValue(value);
4460
4461 return RValue<UInt>(value);
4462 }
4463
Nicolas Capens96d4e092016-11-18 14:22:38 -05004464 RValue<UInt> UInt::operator=(const Int &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004465 {
4466 Value *value = rhs.loadValue();
4467 storeValue(value);
4468
4469 return RValue<UInt>(value);
4470 }
4471
Nicolas Capens96d4e092016-11-18 14:22:38 -05004472 RValue<UInt> UInt::operator=(const Reference<Int> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004473 {
4474 Value *value = rhs.loadValue();
4475 storeValue(value);
4476
4477 return RValue<UInt>(value);
4478 }
4479
4480 RValue<UInt> operator+(RValue<UInt> lhs, RValue<UInt> rhs)
4481 {
4482 return RValue<UInt>(Nucleus::createAdd(lhs.value, rhs.value));
4483 }
4484
4485 RValue<UInt> operator-(RValue<UInt> lhs, RValue<UInt> rhs)
4486 {
4487 return RValue<UInt>(Nucleus::createSub(lhs.value, rhs.value));
4488 }
4489
4490 RValue<UInt> operator*(RValue<UInt> lhs, RValue<UInt> rhs)
4491 {
4492 return RValue<UInt>(Nucleus::createMul(lhs.value, rhs.value));
4493 }
4494
4495 RValue<UInt> operator/(RValue<UInt> lhs, RValue<UInt> rhs)
4496 {
4497 return RValue<UInt>(Nucleus::createUDiv(lhs.value, rhs.value));
4498 }
4499
4500 RValue<UInt> operator%(RValue<UInt> lhs, RValue<UInt> rhs)
4501 {
4502 return RValue<UInt>(Nucleus::createURem(lhs.value, rhs.value));
4503 }
4504
4505 RValue<UInt> operator&(RValue<UInt> lhs, RValue<UInt> rhs)
4506 {
4507 return RValue<UInt>(Nucleus::createAnd(lhs.value, rhs.value));
4508 }
4509
4510 RValue<UInt> operator|(RValue<UInt> lhs, RValue<UInt> rhs)
4511 {
4512 return RValue<UInt>(Nucleus::createOr(lhs.value, rhs.value));
4513 }
4514
4515 RValue<UInt> operator^(RValue<UInt> lhs, RValue<UInt> rhs)
4516 {
4517 return RValue<UInt>(Nucleus::createXor(lhs.value, rhs.value));
4518 }
4519
4520 RValue<UInt> operator<<(RValue<UInt> lhs, RValue<UInt> rhs)
4521 {
4522 return RValue<UInt>(Nucleus::createShl(lhs.value, rhs.value));
4523 }
4524
4525 RValue<UInt> operator>>(RValue<UInt> lhs, RValue<UInt> rhs)
4526 {
4527 return RValue<UInt>(Nucleus::createLShr(lhs.value, rhs.value));
4528 }
4529
Nicolas Capens96d4e092016-11-18 14:22:38 -05004530 RValue<UInt> operator+=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004531 {
4532 return lhs = lhs + rhs;
4533 }
4534
Nicolas Capens96d4e092016-11-18 14:22:38 -05004535 RValue<UInt> operator-=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004536 {
4537 return lhs = lhs - rhs;
4538 }
4539
Nicolas Capens96d4e092016-11-18 14:22:38 -05004540 RValue<UInt> operator*=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004541 {
4542 return lhs = lhs * rhs;
4543 }
4544
Nicolas Capens96d4e092016-11-18 14:22:38 -05004545 RValue<UInt> operator/=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004546 {
4547 return lhs = lhs / rhs;
4548 }
4549
Nicolas Capens96d4e092016-11-18 14:22:38 -05004550 RValue<UInt> operator%=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004551 {
4552 return lhs = lhs % rhs;
4553 }
4554
Nicolas Capens96d4e092016-11-18 14:22:38 -05004555 RValue<UInt> operator&=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004556 {
4557 return lhs = lhs & rhs;
4558 }
4559
Nicolas Capens96d4e092016-11-18 14:22:38 -05004560 RValue<UInt> operator|=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004561 {
4562 return lhs = lhs | rhs;
4563 }
4564
Nicolas Capens96d4e092016-11-18 14:22:38 -05004565 RValue<UInt> operator^=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004566 {
4567 return lhs = lhs ^ rhs;
4568 }
4569
Nicolas Capens96d4e092016-11-18 14:22:38 -05004570 RValue<UInt> operator<<=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004571 {
4572 return lhs = lhs << rhs;
4573 }
4574
Nicolas Capens96d4e092016-11-18 14:22:38 -05004575 RValue<UInt> operator>>=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004576 {
4577 return lhs = lhs >> rhs;
4578 }
4579
4580 RValue<UInt> operator+(RValue<UInt> val)
4581 {
4582 return val;
4583 }
4584
4585 RValue<UInt> operator-(RValue<UInt> val)
4586 {
4587 return RValue<UInt>(Nucleus::createNeg(val.value));
4588 }
4589
4590 RValue<UInt> operator~(RValue<UInt> val)
4591 {
4592 return RValue<UInt>(Nucleus::createNot(val.value));
4593 }
4594
Nicolas Capens96d4e092016-11-18 14:22:38 -05004595 RValue<UInt> operator++(UInt &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004596 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004597 RValue<UInt> res = val;
4598 val += 1;
4599 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004600 }
4601
Nicolas Capens96d4e092016-11-18 14:22:38 -05004602 const UInt &operator++(UInt &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004603 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004604 val += 1;
4605 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004606 }
4607
Nicolas Capens96d4e092016-11-18 14:22:38 -05004608 RValue<UInt> operator--(UInt &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004609 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004610 RValue<UInt> res = val;
4611 val -= 1;
4612 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004613 }
4614
Nicolas Capens96d4e092016-11-18 14:22:38 -05004615 const UInt &operator--(UInt &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004616 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004617 val -= 1;
4618 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004619 }
4620
4621 RValue<UInt> Max(RValue<UInt> x, RValue<UInt> y)
4622 {
4623 return IfThenElse(x > y, x, y);
4624 }
4625
4626 RValue<UInt> Min(RValue<UInt> x, RValue<UInt> y)
4627 {
4628 return IfThenElse(x < y, x, y);
4629 }
4630
4631 RValue<UInt> Clamp(RValue<UInt> x, RValue<UInt> min, RValue<UInt> max)
4632 {
4633 return Min(Max(x, min), max);
4634 }
4635
4636 RValue<Bool> operator<(RValue<UInt> lhs, RValue<UInt> rhs)
4637 {
4638 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
4639 }
4640
4641 RValue<Bool> operator<=(RValue<UInt> lhs, RValue<UInt> rhs)
4642 {
4643 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
4644 }
4645
4646 RValue<Bool> operator>(RValue<UInt> lhs, RValue<UInt> rhs)
4647 {
4648 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
4649 }
4650
4651 RValue<Bool> operator>=(RValue<UInt> lhs, RValue<UInt> rhs)
4652 {
4653 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
4654 }
4655
4656 RValue<Bool> operator!=(RValue<UInt> lhs, RValue<UInt> rhs)
4657 {
4658 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
4659 }
4660
4661 RValue<Bool> operator==(RValue<UInt> lhs, RValue<UInt> rhs)
4662 {
4663 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
4664 }
4665
4666// RValue<UInt> RoundUInt(RValue<Float> cast)
4667// {
Nicolas Capensc37252c2016-09-28 16:11:54 -04004668// assert(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004669// }
4670
4671 Type *UInt::getType()
4672 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04004673 return T(Ice::IceType_i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004674 }
4675
4676// Int2::Int2(RValue<Int> cast)
4677// {
4678// Value *extend = Nucleus::createZExt(cast.value, Long::getType());
4679// Value *vector = Nucleus::createBitCast(extend, Int2::getType());
4680//
4681// Constant *shuffle[2];
4682// shuffle[0] = Nucleus::createConstantInt(0);
4683// shuffle[1] = Nucleus::createConstantInt(0);
4684//
4685// Value *replicate = Nucleus::createShuffleVector(vector, UndefValue::get(Int2::getType()), Nucleus::createConstantVector(shuffle, 2));
4686//
4687// storeValue(replicate);
4688// }
4689
4690 Int2::Int2(RValue<Int4> cast)
4691 {
Nicolas Capens22008782016-10-20 01:11:47 -04004692 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004693 }
4694
Nicolas Capens598f8d82016-09-26 15:09:10 -04004695 Int2::Int2(int x, int y)
4696 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004697 int64_t constantVector[2] = {x, y};
4698 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004699 }
4700
4701 Int2::Int2(RValue<Int2> rhs)
4702 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004703 storeValue(rhs.value);
4704 }
4705
4706 Int2::Int2(const Int2 &rhs)
4707 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004708 Value *value = rhs.loadValue();
4709 storeValue(value);
4710 }
4711
4712 Int2::Int2(const Reference<Int2> &rhs)
4713 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004714 Value *value = rhs.loadValue();
4715 storeValue(value);
4716 }
4717
4718 Int2::Int2(RValue<Int> lo, RValue<Int> hi)
4719 {
Nicolas Capensd4227962016-11-09 14:24:25 -05004720 int shuffle[4] = {0, 4, 1, 5};
4721 Value *packed = Nucleus::createShuffleVector(Int4(lo).loadValue(), Int4(hi).loadValue(), shuffle);
4722
4723 storeValue(Nucleus::createBitCast(packed, Int2::getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004724 }
4725
Nicolas Capens96d4e092016-11-18 14:22:38 -05004726 RValue<Int2> Int2::operator=(RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004727 {
4728 storeValue(rhs.value);
4729
4730 return rhs;
4731 }
4732
Nicolas Capens96d4e092016-11-18 14:22:38 -05004733 RValue<Int2> Int2::operator=(const Int2 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004734 {
4735 Value *value = rhs.loadValue();
4736 storeValue(value);
4737
4738 return RValue<Int2>(value);
4739 }
4740
Nicolas Capens96d4e092016-11-18 14:22:38 -05004741 RValue<Int2> Int2::operator=(const Reference<Int2> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004742 {
4743 Value *value = rhs.loadValue();
4744 storeValue(value);
4745
4746 return RValue<Int2>(value);
4747 }
4748
4749 RValue<Int2> operator+(RValue<Int2> lhs, RValue<Int2> rhs)
4750 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004751 return RValue<Int2>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004752 }
4753
4754 RValue<Int2> operator-(RValue<Int2> lhs, RValue<Int2> rhs)
4755 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004756 return RValue<Int2>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004757 }
4758
4759// RValue<Int2> operator*(RValue<Int2> lhs, RValue<Int2> rhs)
4760// {
4761// return RValue<Int2>(Nucleus::createMul(lhs.value, rhs.value));
4762// }
4763
4764// RValue<Int2> operator/(RValue<Int2> lhs, RValue<Int2> rhs)
4765// {
4766// return RValue<Int2>(Nucleus::createSDiv(lhs.value, rhs.value));
4767// }
4768
4769// RValue<Int2> operator%(RValue<Int2> lhs, RValue<Int2> rhs)
4770// {
4771// return RValue<Int2>(Nucleus::createSRem(lhs.value, rhs.value));
4772// }
4773
4774 RValue<Int2> operator&(RValue<Int2> lhs, RValue<Int2> rhs)
4775 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004776 return RValue<Int2>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004777 }
4778
4779 RValue<Int2> operator|(RValue<Int2> lhs, RValue<Int2> rhs)
4780 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004781 return RValue<Int2>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004782 }
4783
4784 RValue<Int2> operator^(RValue<Int2> lhs, RValue<Int2> rhs)
4785 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004786 return RValue<Int2>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004787 }
4788
4789 RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs)
4790 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004791 return RValue<Int2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004792 }
4793
4794 RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs)
4795 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004796 return RValue<Int2>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004797 }
4798
Nicolas Capens96d4e092016-11-18 14:22:38 -05004799 RValue<Int2> operator+=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004800 {
4801 return lhs = lhs + rhs;
4802 }
4803
Nicolas Capens96d4e092016-11-18 14:22:38 -05004804 RValue<Int2> operator-=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004805 {
4806 return lhs = lhs - rhs;
4807 }
4808
Nicolas Capens96d4e092016-11-18 14:22:38 -05004809// RValue<Int2> operator*=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004810// {
4811// return lhs = lhs * rhs;
4812// }
4813
Nicolas Capens96d4e092016-11-18 14:22:38 -05004814// RValue<Int2> operator/=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004815// {
4816// return lhs = lhs / rhs;
4817// }
4818
Nicolas Capens96d4e092016-11-18 14:22:38 -05004819// RValue<Int2> operator%=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004820// {
4821// return lhs = lhs % rhs;
4822// }
4823
Nicolas Capens96d4e092016-11-18 14:22:38 -05004824 RValue<Int2> operator&=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004825 {
4826 return lhs = lhs & rhs;
4827 }
4828
Nicolas Capens96d4e092016-11-18 14:22:38 -05004829 RValue<Int2> operator|=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004830 {
4831 return lhs = lhs | rhs;
4832 }
4833
Nicolas Capens96d4e092016-11-18 14:22:38 -05004834 RValue<Int2> operator^=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004835 {
4836 return lhs = lhs ^ rhs;
4837 }
4838
Nicolas Capens96d4e092016-11-18 14:22:38 -05004839 RValue<Int2> operator<<=(Int2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004840 {
4841 return lhs = lhs << rhs;
4842 }
4843
Nicolas Capens96d4e092016-11-18 14:22:38 -05004844 RValue<Int2> operator>>=(Int2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004845 {
4846 return lhs = lhs >> rhs;
4847 }
4848
Nicolas Capens598f8d82016-09-26 15:09:10 -04004849// RValue<Int2> operator+(RValue<Int2> val)
4850// {
4851// return val;
4852// }
4853
4854// RValue<Int2> operator-(RValue<Int2> val)
4855// {
4856// return RValue<Int2>(Nucleus::createNeg(val.value));
4857// }
4858
4859 RValue<Int2> operator~(RValue<Int2> val)
4860 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05004861 return RValue<Int2>(Nucleus::createNot(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004862 }
4863
Nicolas Capens45f187a2016-12-02 15:30:56 -05004864 RValue<Short4> UnpackLow(RValue<Int2> x, RValue<Int2> y)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004865 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05004866 int shuffle[4] = {0, 4, 1, 5}; // Real type is v4i32
4867 return As<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004868 }
4869
Nicolas Capens45f187a2016-12-02 15:30:56 -05004870 RValue<Short4> UnpackHigh(RValue<Int2> x, RValue<Int2> y)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004871 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08004872 int shuffle[4] = {0, 4, 1, 5}; // Real type is v4i32
Nicolas Capensc70a1162016-12-03 00:16:14 -05004873 auto lowHigh = RValue<Int4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
4874 return As<Short4>(Swizzle(lowHigh, 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004875 }
4876
4877 RValue<Int> Extract(RValue<Int2> val, int i)
4878 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05004879 return RValue<Int>(Nucleus::createExtractElement(val.value, Int::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004880 }
4881
4882 RValue<Int2> Insert(RValue<Int2> val, RValue<Int> element, int i)
4883 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05004884 return RValue<Int2>(Nucleus::createInsertElement(val.value, element.value, i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004885 }
4886
4887 Type *Int2::getType()
4888 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004889 return T(Type_v2i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004890 }
4891
Nicolas Capens598f8d82016-09-26 15:09:10 -04004892 UInt2::UInt2(unsigned int x, unsigned int y)
4893 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004894 int64_t constantVector[2] = {x, y};
4895 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004896 }
4897
4898 UInt2::UInt2(RValue<UInt2> rhs)
4899 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004900 storeValue(rhs.value);
4901 }
4902
4903 UInt2::UInt2(const UInt2 &rhs)
4904 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004905 Value *value = rhs.loadValue();
4906 storeValue(value);
4907 }
4908
4909 UInt2::UInt2(const Reference<UInt2> &rhs)
4910 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004911 Value *value = rhs.loadValue();
4912 storeValue(value);
4913 }
4914
Nicolas Capens96d4e092016-11-18 14:22:38 -05004915 RValue<UInt2> UInt2::operator=(RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004916 {
4917 storeValue(rhs.value);
4918
4919 return rhs;
4920 }
4921
Nicolas Capens96d4e092016-11-18 14:22:38 -05004922 RValue<UInt2> UInt2::operator=(const UInt2 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004923 {
4924 Value *value = rhs.loadValue();
4925 storeValue(value);
4926
4927 return RValue<UInt2>(value);
4928 }
4929
Nicolas Capens96d4e092016-11-18 14:22:38 -05004930 RValue<UInt2> UInt2::operator=(const Reference<UInt2> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004931 {
4932 Value *value = rhs.loadValue();
4933 storeValue(value);
4934
4935 return RValue<UInt2>(value);
4936 }
4937
4938 RValue<UInt2> operator+(RValue<UInt2> lhs, RValue<UInt2> rhs)
4939 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004940 return RValue<UInt2>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004941 }
4942
4943 RValue<UInt2> operator-(RValue<UInt2> lhs, RValue<UInt2> rhs)
4944 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004945 return RValue<UInt2>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004946 }
4947
4948// RValue<UInt2> operator*(RValue<UInt2> lhs, RValue<UInt2> rhs)
4949// {
4950// return RValue<UInt2>(Nucleus::createMul(lhs.value, rhs.value));
4951// }
4952
4953// RValue<UInt2> operator/(RValue<UInt2> lhs, RValue<UInt2> rhs)
4954// {
4955// return RValue<UInt2>(Nucleus::createUDiv(lhs.value, rhs.value));
4956// }
4957
4958// RValue<UInt2> operator%(RValue<UInt2> lhs, RValue<UInt2> rhs)
4959// {
4960// return RValue<UInt2>(Nucleus::createURem(lhs.value, rhs.value));
4961// }
4962
4963 RValue<UInt2> operator&(RValue<UInt2> lhs, RValue<UInt2> rhs)
4964 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004965 return RValue<UInt2>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004966 }
4967
4968 RValue<UInt2> operator|(RValue<UInt2> lhs, RValue<UInt2> rhs)
4969 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004970 return RValue<UInt2>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004971 }
4972
4973 RValue<UInt2> operator^(RValue<UInt2> lhs, RValue<UInt2> rhs)
4974 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004975 return RValue<UInt2>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004976 }
4977
4978 RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs)
4979 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004980 return RValue<UInt2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004981 }
4982
4983 RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs)
4984 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004985 return RValue<UInt2>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004986 }
4987
Nicolas Capens96d4e092016-11-18 14:22:38 -05004988 RValue<UInt2> operator+=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004989 {
4990 return lhs = lhs + rhs;
4991 }
4992
Nicolas Capens96d4e092016-11-18 14:22:38 -05004993 RValue<UInt2> operator-=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004994 {
4995 return lhs = lhs - rhs;
4996 }
4997
Nicolas Capens96d4e092016-11-18 14:22:38 -05004998// RValue<UInt2> operator*=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004999// {
5000// return lhs = lhs * rhs;
5001// }
5002
Nicolas Capens96d4e092016-11-18 14:22:38 -05005003// RValue<UInt2> operator/=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005004// {
5005// return lhs = lhs / rhs;
5006// }
5007
Nicolas Capens96d4e092016-11-18 14:22:38 -05005008// RValue<UInt2> operator%=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005009// {
5010// return lhs = lhs % rhs;
5011// }
5012
Nicolas Capens96d4e092016-11-18 14:22:38 -05005013 RValue<UInt2> operator&=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005014 {
5015 return lhs = lhs & rhs;
5016 }
5017
Nicolas Capens96d4e092016-11-18 14:22:38 -05005018 RValue<UInt2> operator|=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005019 {
5020 return lhs = lhs | rhs;
5021 }
5022
Nicolas Capens96d4e092016-11-18 14:22:38 -05005023 RValue<UInt2> operator^=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005024 {
5025 return lhs = lhs ^ rhs;
5026 }
5027
Nicolas Capens96d4e092016-11-18 14:22:38 -05005028 RValue<UInt2> operator<<=(UInt2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005029 {
5030 return lhs = lhs << rhs;
5031 }
5032
Nicolas Capens96d4e092016-11-18 14:22:38 -05005033 RValue<UInt2> operator>>=(UInt2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005034 {
5035 return lhs = lhs >> rhs;
5036 }
5037
Nicolas Capens598f8d82016-09-26 15:09:10 -04005038// RValue<UInt2> operator+(RValue<UInt2> val)
5039// {
5040// return val;
5041// }
5042
5043// RValue<UInt2> operator-(RValue<UInt2> val)
5044// {
5045// return RValue<UInt2>(Nucleus::createNeg(val.value));
5046// }
5047
5048 RValue<UInt2> operator~(RValue<UInt2> val)
5049 {
5050 return RValue<UInt2>(Nucleus::createNot(val.value));
5051 }
5052
5053 Type *UInt2::getType()
5054 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04005055 return T(Type_v2i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005056 }
5057
5058 Int4::Int4(RValue<Byte4> cast)
5059 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005060 Value *x = Nucleus::createBitCast(cast.value, Int::getType());
5061 Value *a = Nucleus::createInsertElement(loadValue(), x, 0);
5062
5063 Value *e;
5064 int swizzle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23};
5065 Value *b = Nucleus::createBitCast(a, Byte16::getType());
5066 Value *c = Nucleus::createShuffleVector(b, V(Nucleus::createNullValue(Byte16::getType())), swizzle);
5067
5068 int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11};
5069 Value *d = Nucleus::createBitCast(c, Short8::getType());
5070 e = Nucleus::createShuffleVector(d, V(Nucleus::createNullValue(Short8::getType())), swizzle2);
5071
5072 Value *f = Nucleus::createBitCast(e, Int4::getType());
5073 storeValue(f);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005074 }
5075
5076 Int4::Int4(RValue<SByte4> cast)
5077 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005078 Value *x = Nucleus::createBitCast(cast.value, Int::getType());
5079 Value *a = Nucleus::createInsertElement(loadValue(), x, 0);
5080
5081 Value *e;
5082 int swizzle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7};
5083 Value *b = Nucleus::createBitCast(a, Byte16::getType());
5084 Value *c = Nucleus::createShuffleVector(b, b, swizzle);
5085
5086 int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3};
5087 Value *d = Nucleus::createBitCast(c, Short8::getType());
5088 e = Nucleus::createShuffleVector(d, d, swizzle2);
5089
5090 Value *f = Nucleus::createBitCast(e, Int4::getType());
Nicolas Capens15060bb2016-12-05 22:17:19 -05005091 Value *g = Nucleus::createAShr(f, V(::context->getConstantInt32(24)));
Nicolas Capensd4227962016-11-09 14:24:25 -05005092 storeValue(g);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005093 }
5094
5095 Int4::Int4(RValue<Float4> cast)
5096 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005097 Value *xyzw = Nucleus::createFPToSI(cast.value, Int4::getType());
5098
5099 storeValue(xyzw);
5100 }
5101
5102 Int4::Int4(RValue<Short4> cast)
5103 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005104 int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3};
5105 Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle);
5106 Value *d = Nucleus::createBitCast(c, Int4::getType());
Nicolas Capens15060bb2016-12-05 22:17:19 -05005107 Value *e = Nucleus::createAShr(d, V(::context->getConstantInt32(16)));
Nicolas Capensd4227962016-11-09 14:24:25 -05005108 storeValue(e);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005109 }
5110
5111 Int4::Int4(RValue<UShort4> cast)
5112 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005113 int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11};
5114 Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle);
5115 Value *d = Nucleus::createBitCast(c, Int4::getType());
5116 storeValue(d);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005117 }
5118
Nicolas Capens598f8d82016-09-26 15:09:10 -04005119 Int4::Int4(int xyzw)
5120 {
5121 constant(xyzw, xyzw, xyzw, xyzw);
5122 }
5123
5124 Int4::Int4(int x, int yzw)
5125 {
5126 constant(x, yzw, yzw, yzw);
5127 }
5128
5129 Int4::Int4(int x, int y, int zw)
5130 {
5131 constant(x, y, zw, zw);
5132 }
5133
5134 Int4::Int4(int x, int y, int z, int w)
5135 {
5136 constant(x, y, z, w);
5137 }
5138
5139 void Int4::constant(int x, int y, int z, int w)
5140 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04005141 int64_t constantVector[4] = {x, y, z, w};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04005142 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005143 }
5144
5145 Int4::Int4(RValue<Int4> rhs)
5146 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005147 storeValue(rhs.value);
5148 }
5149
5150 Int4::Int4(const Int4 &rhs)
5151 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005152 Value *value = rhs.loadValue();
5153 storeValue(value);
5154 }
5155
5156 Int4::Int4(const Reference<Int4> &rhs)
5157 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005158 Value *value = rhs.loadValue();
5159 storeValue(value);
5160 }
5161
5162 Int4::Int4(RValue<UInt4> rhs)
5163 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005164 storeValue(rhs.value);
5165 }
5166
5167 Int4::Int4(const UInt4 &rhs)
5168 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005169 Value *value = rhs.loadValue();
5170 storeValue(value);
5171 }
5172
5173 Int4::Int4(const Reference<UInt4> &rhs)
5174 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005175 Value *value = rhs.loadValue();
5176 storeValue(value);
5177 }
5178
5179 Int4::Int4(RValue<Int2> lo, RValue<Int2> hi)
5180 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05005181 int shuffle[4] = {0, 1, 4, 5}; // Real type is v4i32
5182 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
5183
5184 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005185 }
5186
5187 Int4::Int4(RValue<Int> rhs)
5188 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08005189 Value *vector = Nucleus::createBitCast(rhs.value, Int4::getType());
Nicolas Capensd4227962016-11-09 14:24:25 -05005190
5191 int swizzle[4] = {0, 0, 0, 0};
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08005192 Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle);
Nicolas Capensd4227962016-11-09 14:24:25 -05005193
5194 storeValue(replicate);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005195 }
5196
5197 Int4::Int4(const Int &rhs)
5198 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005199 *this = RValue<Int>(rhs.loadValue());
5200 }
5201
5202 Int4::Int4(const Reference<Int> &rhs)
5203 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005204 *this = RValue<Int>(rhs.loadValue());
5205 }
5206
Nicolas Capens96d4e092016-11-18 14:22:38 -05005207 RValue<Int4> Int4::operator=(RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005208 {
5209 storeValue(rhs.value);
5210
5211 return rhs;
5212 }
5213
Nicolas Capens96d4e092016-11-18 14:22:38 -05005214 RValue<Int4> Int4::operator=(const Int4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005215 {
5216 Value *value = rhs.loadValue();
5217 storeValue(value);
5218
5219 return RValue<Int4>(value);
5220 }
5221
Nicolas Capens96d4e092016-11-18 14:22:38 -05005222 RValue<Int4> Int4::operator=(const Reference<Int4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005223 {
5224 Value *value = rhs.loadValue();
5225 storeValue(value);
5226
5227 return RValue<Int4>(value);
5228 }
5229
5230 RValue<Int4> operator+(RValue<Int4> lhs, RValue<Int4> rhs)
5231 {
5232 return RValue<Int4>(Nucleus::createAdd(lhs.value, rhs.value));
5233 }
5234
5235 RValue<Int4> operator-(RValue<Int4> lhs, RValue<Int4> rhs)
5236 {
5237 return RValue<Int4>(Nucleus::createSub(lhs.value, rhs.value));
5238 }
5239
5240 RValue<Int4> operator*(RValue<Int4> lhs, RValue<Int4> rhs)
5241 {
5242 return RValue<Int4>(Nucleus::createMul(lhs.value, rhs.value));
5243 }
5244
5245 RValue<Int4> operator/(RValue<Int4> lhs, RValue<Int4> rhs)
5246 {
5247 return RValue<Int4>(Nucleus::createSDiv(lhs.value, rhs.value));
5248 }
5249
5250 RValue<Int4> operator%(RValue<Int4> lhs, RValue<Int4> rhs)
5251 {
5252 return RValue<Int4>(Nucleus::createSRem(lhs.value, rhs.value));
5253 }
5254
5255 RValue<Int4> operator&(RValue<Int4> lhs, RValue<Int4> rhs)
5256 {
5257 return RValue<Int4>(Nucleus::createAnd(lhs.value, rhs.value));
5258 }
5259
5260 RValue<Int4> operator|(RValue<Int4> lhs, RValue<Int4> rhs)
5261 {
5262 return RValue<Int4>(Nucleus::createOr(lhs.value, rhs.value));
5263 }
5264
5265 RValue<Int4> operator^(RValue<Int4> lhs, RValue<Int4> rhs)
5266 {
5267 return RValue<Int4>(Nucleus::createXor(lhs.value, rhs.value));
5268 }
5269
5270 RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs)
5271 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005272 return RValue<Int4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005273 }
5274
5275 RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs)
5276 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005277 return RValue<Int4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005278 }
5279
5280 RValue<Int4> operator<<(RValue<Int4> lhs, RValue<Int4> rhs)
5281 {
5282 return RValue<Int4>(Nucleus::createShl(lhs.value, rhs.value));
5283 }
5284
5285 RValue<Int4> operator>>(RValue<Int4> lhs, RValue<Int4> rhs)
5286 {
5287 return RValue<Int4>(Nucleus::createAShr(lhs.value, rhs.value));
5288 }
5289
Nicolas Capens96d4e092016-11-18 14:22:38 -05005290 RValue<Int4> operator+=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005291 {
5292 return lhs = lhs + rhs;
5293 }
5294
Nicolas Capens96d4e092016-11-18 14:22:38 -05005295 RValue<Int4> operator-=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005296 {
5297 return lhs = lhs - rhs;
5298 }
5299
Nicolas Capens96d4e092016-11-18 14:22:38 -05005300 RValue<Int4> operator*=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005301 {
5302 return lhs = lhs * rhs;
5303 }
5304
Nicolas Capens96d4e092016-11-18 14:22:38 -05005305// RValue<Int4> operator/=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005306// {
5307// return lhs = lhs / rhs;
5308// }
5309
Nicolas Capens96d4e092016-11-18 14:22:38 -05005310// RValue<Int4> operator%=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005311// {
5312// return lhs = lhs % rhs;
5313// }
5314
Nicolas Capens96d4e092016-11-18 14:22:38 -05005315 RValue<Int4> operator&=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005316 {
5317 return lhs = lhs & rhs;
5318 }
5319
Nicolas Capens96d4e092016-11-18 14:22:38 -05005320 RValue<Int4> operator|=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005321 {
5322 return lhs = lhs | rhs;
5323 }
5324
Nicolas Capens96d4e092016-11-18 14:22:38 -05005325 RValue<Int4> operator^=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005326 {
5327 return lhs = lhs ^ rhs;
5328 }
5329
Nicolas Capens96d4e092016-11-18 14:22:38 -05005330 RValue<Int4> operator<<=(Int4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005331 {
5332 return lhs = lhs << rhs;
5333 }
5334
Nicolas Capens96d4e092016-11-18 14:22:38 -05005335 RValue<Int4> operator>>=(Int4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005336 {
5337 return lhs = lhs >> rhs;
5338 }
5339
5340 RValue<Int4> operator+(RValue<Int4> val)
5341 {
5342 return val;
5343 }
5344
5345 RValue<Int4> operator-(RValue<Int4> val)
5346 {
5347 return RValue<Int4>(Nucleus::createNeg(val.value));
5348 }
5349
5350 RValue<Int4> operator~(RValue<Int4> val)
5351 {
5352 return RValue<Int4>(Nucleus::createNot(val.value));
5353 }
5354
5355 RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y)
5356 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005357 return RValue<Int4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005358 }
5359
5360 RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y)
5361 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005362 return RValue<Int4>(Nucleus::createICmpSLT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005363 }
5364
5365 RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y)
5366 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005367 return RValue<Int4>(Nucleus::createICmpSLE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005368 }
5369
5370 RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y)
5371 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005372 return RValue<Int4>(Nucleus::createICmpNE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005373 }
5374
5375 RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y)
5376 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005377 return RValue<Int4>(Nucleus::createICmpSGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005378 }
5379
5380 RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y)
5381 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005382 return RValue<Int4>(Nucleus::createICmpSGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005383 }
5384
5385 RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y)
5386 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005387 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5388 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value, y.value);
5389 ::basicBlock->appendInst(cmp);
5390
5391 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5392 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5393 ::basicBlock->appendInst(select);
5394
5395 return RValue<Int4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005396 }
5397
5398 RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y)
5399 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005400 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5401 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value);
5402 ::basicBlock->appendInst(cmp);
5403
5404 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5405 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5406 ::basicBlock->appendInst(select);
5407
5408 return RValue<Int4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005409 }
5410
5411 RValue<Int4> RoundInt(RValue<Float4> cast)
5412 {
Nicolas Capensf7b75882017-04-26 09:30:47 -04005413 if(emulateIntrinsics)
5414 {
5415 // Push the fractional part off the mantissa. Accurate up to +/-2^22.
5416 return Int4((cast + Float4(0x00C00000)) - Float4(0x00C00000));
5417 }
5418 else
5419 {
5420 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5421 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5422 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5423 auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5424 nearbyint->addArg(cast.value);
5425 ::basicBlock->appendInst(nearbyint);
Nicolas Capensa8086512016-11-07 17:32:17 -05005426
Nicolas Capensf7b75882017-04-26 09:30:47 -04005427 return RValue<Int4>(V(result));
5428 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04005429 }
5430
5431 RValue<Short8> Pack(RValue<Int4> x, RValue<Int4> y)
5432 {
Nicolas Capensec54a172016-10-25 17:32:37 -04005433 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
5434 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5435 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5436 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
5437 pack->addArg(x.value);
5438 pack->addArg(y.value);
5439 ::basicBlock->appendInst(pack);
5440
5441 return RValue<Short8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005442 }
5443
5444 RValue<Int> Extract(RValue<Int4> x, int i)
5445 {
Nicolas Capense95d5342016-09-30 11:37:28 -04005446 return RValue<Int>(Nucleus::createExtractElement(x.value, Int::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005447 }
5448
5449 RValue<Int4> Insert(RValue<Int4> x, RValue<Int> element, int i)
5450 {
5451 return RValue<Int4>(Nucleus::createInsertElement(x.value, element.value, i));
5452 }
5453
5454 RValue<Int> SignMask(RValue<Int4> x)
5455 {
Nicolas Capensf2cb9df2016-10-21 17:26:13 -04005456 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
5457 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5458 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5459 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5460 movmsk->addArg(x.value);
5461 ::basicBlock->appendInst(movmsk);
5462
5463 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005464 }
5465
5466 RValue<Int4> Swizzle(RValue<Int4> x, unsigned char select)
5467 {
Nicolas Capense95d5342016-09-30 11:37:28 -04005468 return RValue<Int4>(createSwizzle4(x.value, select));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005469 }
5470
5471 Type *Int4::getType()
5472 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04005473 return T(Ice::IceType_v4i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005474 }
5475
5476 UInt4::UInt4(RValue<Float4> cast)
5477 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05005478 // Smallest positive value representable in UInt, but not in Int
5479 const unsigned int ustart = 0x80000000u;
5480 const float ustartf = float(ustart);
5481
5482 // Check if the value can be represented as an Int
5483 Int4 uiValue = CmpNLT(cast, Float4(ustartf));
5484 // If the value is too large, subtract ustart and re-add it after conversion.
5485 uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) |
5486 // Otherwise, just convert normally
5487 (~uiValue & Int4(cast));
5488 // If the value is negative, store 0, otherwise store the result of the conversion
5489 storeValue((~(As<Int4>(cast) >> 31) & uiValue).value);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005490 }
5491
Nicolas Capens598f8d82016-09-26 15:09:10 -04005492 UInt4::UInt4(int xyzw)
5493 {
5494 constant(xyzw, xyzw, xyzw, xyzw);
5495 }
5496
5497 UInt4::UInt4(int x, int yzw)
5498 {
5499 constant(x, yzw, yzw, yzw);
5500 }
5501
5502 UInt4::UInt4(int x, int y, int zw)
5503 {
5504 constant(x, y, zw, zw);
5505 }
5506
5507 UInt4::UInt4(int x, int y, int z, int w)
5508 {
5509 constant(x, y, z, w);
5510 }
5511
5512 void UInt4::constant(int x, int y, int z, int w)
5513 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04005514 int64_t constantVector[4] = {x, y, z, w};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04005515 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005516 }
5517
5518 UInt4::UInt4(RValue<UInt4> rhs)
5519 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005520 storeValue(rhs.value);
5521 }
5522
5523 UInt4::UInt4(const UInt4 &rhs)
5524 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005525 Value *value = rhs.loadValue();
5526 storeValue(value);
5527 }
5528
5529 UInt4::UInt4(const Reference<UInt4> &rhs)
5530 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005531 Value *value = rhs.loadValue();
5532 storeValue(value);
5533 }
5534
5535 UInt4::UInt4(RValue<Int4> rhs)
5536 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005537 storeValue(rhs.value);
5538 }
5539
5540 UInt4::UInt4(const Int4 &rhs)
5541 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005542 Value *value = rhs.loadValue();
5543 storeValue(value);
5544 }
5545
5546 UInt4::UInt4(const Reference<Int4> &rhs)
5547 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005548 Value *value = rhs.loadValue();
5549 storeValue(value);
5550 }
5551
5552 UInt4::UInt4(RValue<UInt2> lo, RValue<UInt2> hi)
5553 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05005554 int shuffle[4] = {0, 1, 4, 5}; // Real type is v4i32
5555 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
5556
5557 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005558 }
5559
Nicolas Capens96d4e092016-11-18 14:22:38 -05005560 RValue<UInt4> UInt4::operator=(RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005561 {
5562 storeValue(rhs.value);
5563
5564 return rhs;
5565 }
5566
Nicolas Capens96d4e092016-11-18 14:22:38 -05005567 RValue<UInt4> UInt4::operator=(const UInt4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005568 {
5569 Value *value = rhs.loadValue();
5570 storeValue(value);
5571
5572 return RValue<UInt4>(value);
5573 }
5574
Nicolas Capens96d4e092016-11-18 14:22:38 -05005575 RValue<UInt4> UInt4::operator=(const Reference<UInt4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005576 {
5577 Value *value = rhs.loadValue();
5578 storeValue(value);
5579
5580 return RValue<UInt4>(value);
5581 }
5582
5583 RValue<UInt4> operator+(RValue<UInt4> lhs, RValue<UInt4> rhs)
5584 {
5585 return RValue<UInt4>(Nucleus::createAdd(lhs.value, rhs.value));
5586 }
5587
5588 RValue<UInt4> operator-(RValue<UInt4> lhs, RValue<UInt4> rhs)
5589 {
5590 return RValue<UInt4>(Nucleus::createSub(lhs.value, rhs.value));
5591 }
5592
5593 RValue<UInt4> operator*(RValue<UInt4> lhs, RValue<UInt4> rhs)
5594 {
5595 return RValue<UInt4>(Nucleus::createMul(lhs.value, rhs.value));
5596 }
5597
5598 RValue<UInt4> operator/(RValue<UInt4> lhs, RValue<UInt4> rhs)
5599 {
5600 return RValue<UInt4>(Nucleus::createUDiv(lhs.value, rhs.value));
5601 }
5602
5603 RValue<UInt4> operator%(RValue<UInt4> lhs, RValue<UInt4> rhs)
5604 {
5605 return RValue<UInt4>(Nucleus::createURem(lhs.value, rhs.value));
5606 }
5607
5608 RValue<UInt4> operator&(RValue<UInt4> lhs, RValue<UInt4> rhs)
5609 {
5610 return RValue<UInt4>(Nucleus::createAnd(lhs.value, rhs.value));
5611 }
5612
5613 RValue<UInt4> operator|(RValue<UInt4> lhs, RValue<UInt4> rhs)
5614 {
5615 return RValue<UInt4>(Nucleus::createOr(lhs.value, rhs.value));
5616 }
5617
5618 RValue<UInt4> operator^(RValue<UInt4> lhs, RValue<UInt4> rhs)
5619 {
5620 return RValue<UInt4>(Nucleus::createXor(lhs.value, rhs.value));
5621 }
5622
5623 RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs)
5624 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005625 return RValue<UInt4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005626 }
5627
5628 RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs)
5629 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005630 return RValue<UInt4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005631 }
5632
5633 RValue<UInt4> operator<<(RValue<UInt4> lhs, RValue<UInt4> rhs)
5634 {
5635 return RValue<UInt4>(Nucleus::createShl(lhs.value, rhs.value));
5636 }
5637
5638 RValue<UInt4> operator>>(RValue<UInt4> lhs, RValue<UInt4> rhs)
5639 {
5640 return RValue<UInt4>(Nucleus::createLShr(lhs.value, rhs.value));
5641 }
5642
Nicolas Capens96d4e092016-11-18 14:22:38 -05005643 RValue<UInt4> operator+=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005644 {
5645 return lhs = lhs + rhs;
5646 }
5647
Nicolas Capens96d4e092016-11-18 14:22:38 -05005648 RValue<UInt4> operator-=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005649 {
5650 return lhs = lhs - rhs;
5651 }
5652
Nicolas Capens96d4e092016-11-18 14:22:38 -05005653 RValue<UInt4> operator*=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005654 {
5655 return lhs = lhs * rhs;
5656 }
5657
Nicolas Capens96d4e092016-11-18 14:22:38 -05005658// RValue<UInt4> operator/=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005659// {
5660// return lhs = lhs / rhs;
5661// }
5662
Nicolas Capens96d4e092016-11-18 14:22:38 -05005663// RValue<UInt4> operator%=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005664// {
5665// return lhs = lhs % rhs;
5666// }
5667
Nicolas Capens96d4e092016-11-18 14:22:38 -05005668 RValue<UInt4> operator&=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005669 {
5670 return lhs = lhs & rhs;
5671 }
5672
Nicolas Capens96d4e092016-11-18 14:22:38 -05005673 RValue<UInt4> operator|=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005674 {
5675 return lhs = lhs | rhs;
5676 }
5677
Nicolas Capens96d4e092016-11-18 14:22:38 -05005678 RValue<UInt4> operator^=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005679 {
5680 return lhs = lhs ^ rhs;
5681 }
5682
Nicolas Capens96d4e092016-11-18 14:22:38 -05005683 RValue<UInt4> operator<<=(UInt4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005684 {
5685 return lhs = lhs << rhs;
5686 }
5687
Nicolas Capens96d4e092016-11-18 14:22:38 -05005688 RValue<UInt4> operator>>=(UInt4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005689 {
5690 return lhs = lhs >> rhs;
5691 }
5692
5693 RValue<UInt4> operator+(RValue<UInt4> val)
5694 {
5695 return val;
5696 }
5697
5698 RValue<UInt4> operator-(RValue<UInt4> val)
5699 {
5700 return RValue<UInt4>(Nucleus::createNeg(val.value));
5701 }
5702
5703 RValue<UInt4> operator~(RValue<UInt4> val)
5704 {
5705 return RValue<UInt4>(Nucleus::createNot(val.value));
5706 }
5707
5708 RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y)
5709 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005710 return RValue<UInt4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005711 }
5712
5713 RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y)
5714 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005715 return RValue<UInt4>(Nucleus::createICmpULT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005716 }
5717
5718 RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y)
5719 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005720 return RValue<UInt4>(Nucleus::createICmpULE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005721 }
5722
5723 RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y)
5724 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005725 return RValue<UInt4>(Nucleus::createICmpNE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005726 }
5727
5728 RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y)
5729 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005730 return RValue<UInt4>(Nucleus::createICmpUGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005731 }
5732
5733 RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y)
5734 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005735 return RValue<UInt4>(Nucleus::createICmpUGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005736 }
5737
5738 RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y)
5739 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005740 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5741 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value, y.value);
5742 ::basicBlock->appendInst(cmp);
5743
5744 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5745 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5746 ::basicBlock->appendInst(select);
5747
5748 return RValue<UInt4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005749 }
5750
5751 RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y)
5752 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005753 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5754 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value);
5755 ::basicBlock->appendInst(cmp);
5756
5757 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5758 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5759 ::basicBlock->appendInst(select);
5760
5761 return RValue<UInt4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005762 }
5763
5764 RValue<UShort8> Pack(RValue<UInt4> x, RValue<UInt4> y)
5765 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05005766 if(CPUID::SSE4_1)
5767 {
5768 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
5769 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5770 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5771 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
5772 pack->addArg(x.value);
5773 pack->addArg(y.value);
5774 ::basicBlock->appendInst(pack);
Nicolas Capensec54a172016-10-25 17:32:37 -04005775
Nicolas Capens9ca48d52017-01-14 12:52:55 -05005776 return RValue<UShort8>(V(result));
5777 }
5778 else
5779 {
5780 RValue<Int4> sx = As<Int4>(x);
5781 RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000);
5782
5783 RValue<Int4> sy = As<Int4>(y);
5784 RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000);
5785
5786 return As<UShort8>(Pack(bx, by) + Short8(0x8000u));
5787 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04005788 }
5789
5790 Type *UInt4::getType()
5791 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04005792 return T(Ice::IceType_v4i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005793 }
5794
5795 Float::Float(RValue<Int> cast)
5796 {
5797 Value *integer = Nucleus::createSIToFP(cast.value, Float::getType());
5798
5799 storeValue(integer);
5800 }
5801
Alexis Hetucfd96322017-07-24 14:44:33 -04005802 Float::Float(RValue<UInt> cast)
5803 {
5804 RValue<Float> result = Float(Int(cast & UInt(0x7FFFFFFF))) +
5805 As<Float>((As<Int>(cast) >> 31) & As<Int>(Float(0x80000000u)));
5806
5807 storeValue(result.value);
5808 }
5809
Nicolas Capens598f8d82016-09-26 15:09:10 -04005810 Float::Float(float x)
5811 {
5812 storeValue(Nucleus::createConstantFloat(x));
5813 }
5814
5815 Float::Float(RValue<Float> rhs)
5816 {
5817 storeValue(rhs.value);
5818 }
5819
5820 Float::Float(const Float &rhs)
5821 {
5822 Value *value = rhs.loadValue();
5823 storeValue(value);
5824 }
5825
5826 Float::Float(const Reference<Float> &rhs)
5827 {
5828 Value *value = rhs.loadValue();
5829 storeValue(value);
5830 }
5831
Nicolas Capens96d4e092016-11-18 14:22:38 -05005832 RValue<Float> Float::operator=(RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005833 {
5834 storeValue(rhs.value);
5835
5836 return rhs;
5837 }
5838
Nicolas Capens96d4e092016-11-18 14:22:38 -05005839 RValue<Float> Float::operator=(const Float &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005840 {
5841 Value *value = rhs.loadValue();
5842 storeValue(value);
5843
5844 return RValue<Float>(value);
5845 }
5846
Nicolas Capens96d4e092016-11-18 14:22:38 -05005847 RValue<Float> Float::operator=(const Reference<Float> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005848 {
5849 Value *value = rhs.loadValue();
5850 storeValue(value);
5851
5852 return RValue<Float>(value);
5853 }
5854
5855 RValue<Float> operator+(RValue<Float> lhs, RValue<Float> rhs)
5856 {
5857 return RValue<Float>(Nucleus::createFAdd(lhs.value, rhs.value));
5858 }
5859
5860 RValue<Float> operator-(RValue<Float> lhs, RValue<Float> rhs)
5861 {
5862 return RValue<Float>(Nucleus::createFSub(lhs.value, rhs.value));
5863 }
5864
5865 RValue<Float> operator*(RValue<Float> lhs, RValue<Float> rhs)
5866 {
5867 return RValue<Float>(Nucleus::createFMul(lhs.value, rhs.value));
5868 }
5869
5870 RValue<Float> operator/(RValue<Float> lhs, RValue<Float> rhs)
5871 {
5872 return RValue<Float>(Nucleus::createFDiv(lhs.value, rhs.value));
5873 }
5874
Nicolas Capens96d4e092016-11-18 14:22:38 -05005875 RValue<Float> operator+=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005876 {
5877 return lhs = lhs + rhs;
5878 }
5879
Nicolas Capens96d4e092016-11-18 14:22:38 -05005880 RValue<Float> operator-=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005881 {
5882 return lhs = lhs - rhs;
5883 }
5884
Nicolas Capens96d4e092016-11-18 14:22:38 -05005885 RValue<Float> operator*=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005886 {
5887 return lhs = lhs * rhs;
5888 }
5889
Nicolas Capens96d4e092016-11-18 14:22:38 -05005890 RValue<Float> operator/=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005891 {
5892 return lhs = lhs / rhs;
5893 }
5894
5895 RValue<Float> operator+(RValue<Float> val)
5896 {
5897 return val;
5898 }
5899
5900 RValue<Float> operator-(RValue<Float> val)
5901 {
5902 return RValue<Float>(Nucleus::createFNeg(val.value));
5903 }
5904
5905 RValue<Bool> operator<(RValue<Float> lhs, RValue<Float> rhs)
5906 {
5907 return RValue<Bool>(Nucleus::createFCmpOLT(lhs.value, rhs.value));
5908 }
5909
5910 RValue<Bool> operator<=(RValue<Float> lhs, RValue<Float> rhs)
5911 {
5912 return RValue<Bool>(Nucleus::createFCmpOLE(lhs.value, rhs.value));
5913 }
5914
5915 RValue<Bool> operator>(RValue<Float> lhs, RValue<Float> rhs)
5916 {
5917 return RValue<Bool>(Nucleus::createFCmpOGT(lhs.value, rhs.value));
5918 }
5919
5920 RValue<Bool> operator>=(RValue<Float> lhs, RValue<Float> rhs)
5921 {
5922 return RValue<Bool>(Nucleus::createFCmpOGE(lhs.value, rhs.value));
5923 }
5924
5925 RValue<Bool> operator!=(RValue<Float> lhs, RValue<Float> rhs)
5926 {
5927 return RValue<Bool>(Nucleus::createFCmpONE(lhs.value, rhs.value));
5928 }
5929
5930 RValue<Bool> operator==(RValue<Float> lhs, RValue<Float> rhs)
5931 {
5932 return RValue<Bool>(Nucleus::createFCmpOEQ(lhs.value, rhs.value));
5933 }
5934
5935 RValue<Float> Abs(RValue<Float> x)
5936 {
5937 return IfThenElse(x > 0.0f, x, -x);
5938 }
5939
5940 RValue<Float> Max(RValue<Float> x, RValue<Float> y)
5941 {
5942 return IfThenElse(x > y, x, y);
5943 }
5944
5945 RValue<Float> Min(RValue<Float> x, RValue<Float> y)
5946 {
5947 return IfThenElse(x < y, x, y);
5948 }
5949
5950 RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2)
5951 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04005952 return 1.0f / x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005953 }
5954
5955 RValue<Float> RcpSqrt_pp(RValue<Float> x)
5956 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04005957 return Rcp_pp(Sqrt(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005958 }
5959
5960 RValue<Float> Sqrt(RValue<Float> x)
5961 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04005962 Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32);
5963 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5964 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5965 auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5966 sqrt->addArg(x.value);
5967 ::basicBlock->appendInst(sqrt);
5968
5969 return RValue<Float>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005970 }
5971
5972 RValue<Float> Round(RValue<Float> x)
5973 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005974 return Float4(Round(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005975 }
5976
5977 RValue<Float> Trunc(RValue<Float> x)
5978 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005979 return Float4(Trunc(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005980 }
5981
5982 RValue<Float> Frac(RValue<Float> x)
5983 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005984 return Float4(Frac(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005985 }
5986
5987 RValue<Float> Floor(RValue<Float> x)
5988 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005989 return Float4(Floor(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005990 }
5991
5992 RValue<Float> Ceil(RValue<Float> x)
5993 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005994 return Float4(Ceil(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005995 }
5996
5997 Type *Float::getType()
5998 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04005999 return T(Ice::IceType_f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006000 }
6001
6002 Float2::Float2(RValue<Float4> cast)
6003 {
Nicolas Capens22008782016-10-20 01:11:47 -04006004 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006005 }
6006
6007 Type *Float2::getType()
6008 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04006009 return T(Type_v2f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006010 }
6011
Nicolas Capensa25311a2017-01-16 17:19:00 -05006012 Float4::Float4(RValue<Byte4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006013 {
Nicolas Capensd4227962016-11-09 14:24:25 -05006014 Value *a = Int4(cast).loadValue();
6015 Value *xyzw = Nucleus::createSIToFP(a, Float4::getType());
6016
6017 storeValue(xyzw);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006018 }
6019
Nicolas Capensa25311a2017-01-16 17:19:00 -05006020 Float4::Float4(RValue<SByte4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006021 {
Nicolas Capensd4227962016-11-09 14:24:25 -05006022 Value *a = Int4(cast).loadValue();
6023 Value *xyzw = Nucleus::createSIToFP(a, Float4::getType());
6024
6025 storeValue(xyzw);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006026 }
6027
Nicolas Capensa25311a2017-01-16 17:19:00 -05006028 Float4::Float4(RValue<Short4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006029 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006030 Int4 c(cast);
6031 storeValue(Nucleus::createSIToFP(RValue<Int4>(c).value, Float4::getType()));
6032 }
6033
Nicolas Capensa25311a2017-01-16 17:19:00 -05006034 Float4::Float4(RValue<UShort4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006035 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006036 Int4 c(cast);
6037 storeValue(Nucleus::createSIToFP(RValue<Int4>(c).value, Float4::getType()));
6038 }
6039
Nicolas Capensa25311a2017-01-16 17:19:00 -05006040 Float4::Float4(RValue<Int4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006041 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006042 Value *xyzw = Nucleus::createSIToFP(cast.value, Float4::getType());
6043
6044 storeValue(xyzw);
6045 }
6046
Nicolas Capensa25311a2017-01-16 17:19:00 -05006047 Float4::Float4(RValue<UInt4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006048 {
Nicolas Capens96445fe2016-12-15 14:45:13 -05006049 RValue<Float4> result = Float4(Int4(cast & UInt4(0x7FFFFFFF))) +
6050 As<Float4>((As<Int4>(cast) >> 31) & As<Int4>(Float4(0x80000000u)));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006051
Nicolas Capens96445fe2016-12-15 14:45:13 -05006052 storeValue(result.value);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006053 }
6054
Nicolas Capensa25311a2017-01-16 17:19:00 -05006055 Float4::Float4() : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006056 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006057 }
6058
Nicolas Capensa25311a2017-01-16 17:19:00 -05006059 Float4::Float4(float xyzw) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006060 {
6061 constant(xyzw, xyzw, xyzw, xyzw);
6062 }
6063
Nicolas Capensa25311a2017-01-16 17:19:00 -05006064 Float4::Float4(float x, float yzw) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006065 {
6066 constant(x, yzw, yzw, yzw);
6067 }
6068
Nicolas Capensa25311a2017-01-16 17:19:00 -05006069 Float4::Float4(float x, float y, float zw) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006070 {
6071 constant(x, y, zw, zw);
6072 }
6073
Nicolas Capensa25311a2017-01-16 17:19:00 -05006074 Float4::Float4(float x, float y, float z, float w) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006075 {
6076 constant(x, y, z, w);
6077 }
6078
6079 void Float4::constant(float x, float y, float z, float w)
6080 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04006081 double constantVector[4] = {x, y, z, w};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04006082 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006083 }
6084
Nicolas Capensa25311a2017-01-16 17:19:00 -05006085 Float4::Float4(RValue<Float4> rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006086 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006087 storeValue(rhs.value);
6088 }
6089
Nicolas Capensa25311a2017-01-16 17:19:00 -05006090 Float4::Float4(const Float4 &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006091 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006092 Value *value = rhs.loadValue();
6093 storeValue(value);
6094 }
6095
Nicolas Capensa25311a2017-01-16 17:19:00 -05006096 Float4::Float4(const Reference<Float4> &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006097 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006098 Value *value = rhs.loadValue();
6099 storeValue(value);
6100 }
6101
Nicolas Capensa25311a2017-01-16 17:19:00 -05006102 Float4::Float4(RValue<Float> rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006103 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08006104 Value *vector = Nucleus::createBitCast(rhs.value, Float4::getType());
Nicolas Capensd4227962016-11-09 14:24:25 -05006105
6106 int swizzle[4] = {0, 0, 0, 0};
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08006107 Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle);
Nicolas Capensd4227962016-11-09 14:24:25 -05006108
6109 storeValue(replicate);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006110 }
6111
Nicolas Capensa25311a2017-01-16 17:19:00 -05006112 Float4::Float4(const Float &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006113 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006114 *this = RValue<Float>(rhs.loadValue());
6115 }
6116
Nicolas Capensa25311a2017-01-16 17:19:00 -05006117 Float4::Float4(const Reference<Float> &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006118 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006119 *this = RValue<Float>(rhs.loadValue());
6120 }
6121
Nicolas Capens96d4e092016-11-18 14:22:38 -05006122 RValue<Float4> Float4::operator=(float x)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006123 {
6124 return *this = Float4(x, x, x, x);
6125 }
6126
Nicolas Capens96d4e092016-11-18 14:22:38 -05006127 RValue<Float4> Float4::operator=(RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006128 {
6129 storeValue(rhs.value);
6130
6131 return rhs;
6132 }
6133
Nicolas Capens96d4e092016-11-18 14:22:38 -05006134 RValue<Float4> Float4::operator=(const Float4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006135 {
6136 Value *value = rhs.loadValue();
6137 storeValue(value);
6138
6139 return RValue<Float4>(value);
6140 }
6141
Nicolas Capens96d4e092016-11-18 14:22:38 -05006142 RValue<Float4> Float4::operator=(const Reference<Float4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006143 {
6144 Value *value = rhs.loadValue();
6145 storeValue(value);
6146
6147 return RValue<Float4>(value);
6148 }
6149
Nicolas Capens96d4e092016-11-18 14:22:38 -05006150 RValue<Float4> Float4::operator=(RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006151 {
6152 return *this = Float4(rhs);
6153 }
6154
Nicolas Capens96d4e092016-11-18 14:22:38 -05006155 RValue<Float4> Float4::operator=(const Float &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006156 {
6157 return *this = Float4(rhs);
6158 }
6159
Nicolas Capens96d4e092016-11-18 14:22:38 -05006160 RValue<Float4> Float4::operator=(const Reference<Float> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006161 {
6162 return *this = Float4(rhs);
6163 }
6164
6165 RValue<Float4> operator+(RValue<Float4> lhs, RValue<Float4> rhs)
6166 {
6167 return RValue<Float4>(Nucleus::createFAdd(lhs.value, rhs.value));
6168 }
6169
6170 RValue<Float4> operator-(RValue<Float4> lhs, RValue<Float4> rhs)
6171 {
6172 return RValue<Float4>(Nucleus::createFSub(lhs.value, rhs.value));
6173 }
6174
6175 RValue<Float4> operator*(RValue<Float4> lhs, RValue<Float4> rhs)
6176 {
6177 return RValue<Float4>(Nucleus::createFMul(lhs.value, rhs.value));
6178 }
6179
6180 RValue<Float4> operator/(RValue<Float4> lhs, RValue<Float4> rhs)
6181 {
6182 return RValue<Float4>(Nucleus::createFDiv(lhs.value, rhs.value));
6183 }
6184
6185 RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs)
6186 {
6187 return RValue<Float4>(Nucleus::createFRem(lhs.value, rhs.value));
6188 }
6189
Nicolas Capens96d4e092016-11-18 14:22:38 -05006190 RValue<Float4> operator+=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006191 {
6192 return lhs = lhs + rhs;
6193 }
6194
Nicolas Capens96d4e092016-11-18 14:22:38 -05006195 RValue<Float4> operator-=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006196 {
6197 return lhs = lhs - rhs;
6198 }
6199
Nicolas Capens96d4e092016-11-18 14:22:38 -05006200 RValue<Float4> operator*=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006201 {
6202 return lhs = lhs * rhs;
6203 }
6204
Nicolas Capens96d4e092016-11-18 14:22:38 -05006205 RValue<Float4> operator/=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006206 {
6207 return lhs = lhs / rhs;
6208 }
6209
Nicolas Capens96d4e092016-11-18 14:22:38 -05006210 RValue<Float4> operator%=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006211 {
6212 return lhs = lhs % rhs;
6213 }
6214
6215 RValue<Float4> operator+(RValue<Float4> val)
6216 {
6217 return val;
6218 }
6219
6220 RValue<Float4> operator-(RValue<Float4> val)
6221 {
6222 return RValue<Float4>(Nucleus::createFNeg(val.value));
6223 }
6224
6225 RValue<Float4> Abs(RValue<Float4> x)
6226 {
Nicolas Capens84272242016-11-09 13:31:06 -05006227 Value *vector = Nucleus::createBitCast(x.value, Int4::getType());
6228 int64_t constantVector[4] = {0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF};
6229 Value *result = Nucleus::createAnd(vector, V(Nucleus::createConstantVector(constantVector, Int4::getType())));
6230
6231 return As<Float4>(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006232 }
6233
6234 RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y)
6235 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006236 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05006237 auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Ogt, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006238 ::basicBlock->appendInst(cmp);
6239
6240 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05006241 auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006242 ::basicBlock->appendInst(select);
6243
6244 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006245 }
6246
6247 RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y)
6248 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006249 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05006250 auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Olt, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006251 ::basicBlock->appendInst(cmp);
6252
6253 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05006254 auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006255 ::basicBlock->appendInst(select);
6256
6257 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006258 }
6259
6260 RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2)
6261 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04006262 return Float4(1.0f) / x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04006263 }
6264
6265 RValue<Float4> RcpSqrt_pp(RValue<Float4> x)
6266 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04006267 return Rcp_pp(Sqrt(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006268 }
6269
6270 RValue<Float4> Sqrt(RValue<Float4> x)
6271 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04006272 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6273 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6274 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6275 auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
6276 sqrt->addArg(x.value);
6277 ::basicBlock->appendInst(sqrt);
6278
6279 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006280 }
6281
Nicolas Capensc94ab742016-11-08 15:15:31 -05006282 RValue<Float4> Insert(RValue<Float4> x, RValue<Float> element, int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006283 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05006284 return RValue<Float4>(Nucleus::createInsertElement(x.value, element.value, i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006285 }
6286
6287 RValue<Float> Extract(RValue<Float4> x, int i)
6288 {
Nicolas Capense95d5342016-09-30 11:37:28 -04006289 return RValue<Float>(Nucleus::createExtractElement(x.value, Float::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006290 }
6291
6292 RValue<Float4> Swizzle(RValue<Float4> x, unsigned char select)
6293 {
Nicolas Capense95d5342016-09-30 11:37:28 -04006294 return RValue<Float4>(createSwizzle4(x.value, select));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006295 }
6296
6297 RValue<Float4> ShuffleLowHigh(RValue<Float4> x, RValue<Float4> y, unsigned char imm)
6298 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04006299 int shuffle[4] =
6300 {
6301 ((imm >> 0) & 0x03) + 0,
6302 ((imm >> 2) & 0x03) + 0,
6303 ((imm >> 4) & 0x03) + 4,
6304 ((imm >> 6) & 0x03) + 4,
6305 };
6306
6307 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006308 }
6309
6310 RValue<Float4> UnpackLow(RValue<Float4> x, RValue<Float4> y)
6311 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04006312 int shuffle[4] = {0, 4, 1, 5};
6313 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006314 }
6315
6316 RValue<Float4> UnpackHigh(RValue<Float4> x, RValue<Float4> y)
6317 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04006318 int shuffle[4] = {2, 6, 3, 7};
6319 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006320 }
6321
6322 RValue<Float4> Mask(Float4 &lhs, RValue<Float4> rhs, unsigned char select)
6323 {
6324 Value *vector = lhs.loadValue();
Nicolas Capensa4c30b02016-11-08 15:43:17 -05006325 Value *result = createMask4(vector, rhs.value, select);
6326 lhs.storeValue(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006327
Nicolas Capensa4c30b02016-11-08 15:43:17 -05006328 return RValue<Float4>(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006329 }
6330
6331 RValue<Int> SignMask(RValue<Float4> x)
6332 {
Nicolas Capensf2cb9df2016-10-21 17:26:13 -04006333 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
6334 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6335 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6336 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
6337 movmsk->addArg(x.value);
6338 ::basicBlock->appendInst(movmsk);
6339
6340 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006341 }
6342
6343 RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y)
6344 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006345 return RValue<Int4>(Nucleus::createFCmpOEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006346 }
6347
6348 RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y)
6349 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006350 return RValue<Int4>(Nucleus::createFCmpOLT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006351 }
6352
6353 RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y)
6354 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006355 return RValue<Int4>(Nucleus::createFCmpOLE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006356 }
6357
6358 RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y)
6359 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006360 return RValue<Int4>(Nucleus::createFCmpONE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006361 }
6362
6363 RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y)
6364 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006365 return RValue<Int4>(Nucleus::createFCmpOGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006366 }
6367
6368 RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y)
6369 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006370 return RValue<Int4>(Nucleus::createFCmpOGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006371 }
6372
6373 RValue<Float4> Round(RValue<Float4> x)
6374 {
Nicolas Capensf7b75882017-04-26 09:30:47 -04006375 if(emulateIntrinsics)
6376 {
6377 // Push the fractional part off the mantissa. Accurate up to +/-2^22.
6378 return (x + Float4(0x00C00000)) - Float4(0x00C00000);
6379 }
6380 else if(CPUID::SSE4_1)
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006381 {
6382 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6383 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6384 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6385 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6386 round->addArg(x.value);
6387 round->addArg(::context->getConstantInt32(0));
6388 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006389
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006390 return RValue<Float4>(V(result));
6391 }
6392 else
6393 {
6394 return Float4(RoundInt(x));
6395 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006396 }
6397
6398 RValue<Float4> Trunc(RValue<Float4> x)
6399 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006400 if(CPUID::SSE4_1)
6401 {
6402 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6403 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6404 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6405 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6406 round->addArg(x.value);
6407 round->addArg(::context->getConstantInt32(3));
6408 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006409
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006410 return RValue<Float4>(V(result));
6411 }
6412 else
6413 {
6414 return Float4(Int4(x));
6415 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006416 }
6417
6418 RValue<Float4> Frac(RValue<Float4> x)
6419 {
Nicolas Capensb9230422017-07-17 10:27:33 -04006420 Float4 frc;
6421
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006422 if(CPUID::SSE4_1)
6423 {
Nicolas Capensb9230422017-07-17 10:27:33 -04006424 frc = x - Floor(x);
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006425 }
6426 else
6427 {
Nicolas Capensb9230422017-07-17 10:27:33 -04006428 frc = x - Float4(Int4(x)); // Signed fractional part.
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006429
Nicolas Capensb9230422017-07-17 10:27:33 -04006430 frc += As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1, 1, 1, 1))); // Add 1.0 if negative.
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006431 }
Nicolas Capensb9230422017-07-17 10:27:33 -04006432
6433 // x - floor(x) can be 1.0 for very small negative x.
6434 // Clamp against the value just below 1.0.
6435 return Min(frc, As<Float4>(Int4(0x3F7FFFFF)));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006436 }
6437
6438 RValue<Float4> Floor(RValue<Float4> x)
6439 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006440 if(CPUID::SSE4_1)
6441 {
6442 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6443 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6444 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6445 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6446 round->addArg(x.value);
6447 round->addArg(::context->getConstantInt32(1));
6448 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006449
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006450 return RValue<Float4>(V(result));
6451 }
6452 else
6453 {
6454 return x - Frac(x);
6455 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006456 }
6457
6458 RValue<Float4> Ceil(RValue<Float4> x)
6459 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006460 if(CPUID::SSE4_1)
6461 {
6462 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6463 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6464 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6465 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6466 round->addArg(x.value);
6467 round->addArg(::context->getConstantInt32(2));
6468 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006469
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006470 return RValue<Float4>(V(result));
6471 }
6472 else
6473 {
6474 return -Floor(-x);
6475 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006476 }
6477
6478 Type *Float4::getType()
6479 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04006480 return T(Ice::IceType_v4f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006481 }
6482
6483 RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, int offset)
6484 {
Nicolas Capens8820f642016-09-30 04:42:43 -04006485 return lhs + RValue<Int>(Nucleus::createConstantInt(offset));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006486 }
6487
6488 RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<Int> offset)
6489 {
Nicolas Capensd294def2017-01-26 17:44:37 -08006490 return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, false));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006491 }
6492
6493 RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<UInt> offset)
6494 {
Nicolas Capensd294def2017-01-26 17:44:37 -08006495 return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, true));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006496 }
6497
Nicolas Capens96d4e092016-11-18 14:22:38 -05006498 RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, int offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006499 {
6500 return lhs = lhs + offset;
6501 }
6502
Nicolas Capens96d4e092016-11-18 14:22:38 -05006503 RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, RValue<Int> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006504 {
6505 return lhs = lhs + offset;
6506 }
6507
Nicolas Capens96d4e092016-11-18 14:22:38 -05006508 RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, RValue<UInt> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006509 {
6510 return lhs = lhs + offset;
6511 }
6512
6513 RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, int offset)
6514 {
6515 return lhs + -offset;
6516 }
6517
6518 RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, RValue<Int> offset)
6519 {
6520 return lhs + -offset;
6521 }
6522
6523 RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, RValue<UInt> offset)
6524 {
6525 return lhs + -offset;
6526 }
6527
Nicolas Capens96d4e092016-11-18 14:22:38 -05006528 RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, int offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006529 {
6530 return lhs = lhs - offset;
6531 }
6532
Nicolas Capens96d4e092016-11-18 14:22:38 -05006533 RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, RValue<Int> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006534 {
6535 return lhs = lhs - offset;
6536 }
6537
Nicolas Capens96d4e092016-11-18 14:22:38 -05006538 RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, RValue<UInt> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006539 {
6540 return lhs = lhs - offset;
6541 }
6542
6543 void Return()
6544 {
6545 Nucleus::createRetVoid();
6546 Nucleus::setInsertBlock(Nucleus::createBasicBlock());
6547 Nucleus::createUnreachable();
6548 }
6549
Nicolas Capenseb253d02016-11-18 14:40:40 -05006550 void Return(RValue<Int> ret)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006551 {
Nicolas Capenseb253d02016-11-18 14:40:40 -05006552 Nucleus::createRet(ret.value);
Nicolas Capensfdcca2d2016-10-20 11:31:36 -04006553 Nucleus::setInsertBlock(Nucleus::createBasicBlock());
6554 Nucleus::createUnreachable();
Nicolas Capens598f8d82016-09-26 15:09:10 -04006555 }
6556
Nicolas Capensf4eec2f2017-05-24 15:46:48 -04006557 void branch(RValue<Bool> cmp, BasicBlock *bodyBB, BasicBlock *endBB)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006558 {
6559 Nucleus::createCondBr(cmp.value, bodyBB, endBB);
6560 Nucleus::setInsertBlock(bodyBB);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006561 }
6562
Nicolas Capens598f8d82016-09-26 15:09:10 -04006563 RValue<Long> Ticks()
6564 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04006565 assert(false && "UNIMPLEMENTED"); return RValue<Long>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006566 }
6567}