blob: 6fbd7a5318ba61d1668237899c4f20e5add2d2cf [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 Capens584088c2017-01-26 16:05:18 -0800852 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::LoadSubVector, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
853 auto target = ::context->getConstantUndef(Ice::IceType_i32);
854 auto load = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
855 load->addArg(ptr);
856 load->addArg(::context->getConstantInt32(typeSize(type)));
857 ::basicBlock->appendInst(load);
Nicolas Capens23d99a42016-09-30 14:57:16 -0400858 }
859 else
860 {
861 auto load = Ice::InstLoad::create(::function, result, ptr, align);
862 ::basicBlock->appendInst(load);
863 }
864
865 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400866 }
867
Nicolas Capens6d738712016-09-30 04:15:22 -0400868 Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400869 {
Nicolas Capens23d99a42016-09-30 14:57:16 -0400870 int valueType = (int)reinterpret_cast<intptr_t>(type);
871
872 if(valueType & EmulatedBits)
873 {
Nicolas Capens584088c2017-01-26 16:05:18 -0800874 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::StoreSubVector, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T};
875 auto target = ::context->getConstantUndef(Ice::IceType_i32);
876 auto store = Ice::InstIntrinsicCall::create(::function, 3, nullptr, target, intrinsic);
877 store->addArg(value);
878 store->addArg(ptr);
879 store->addArg(::context->getConstantInt32(typeSize(type)));
880 ::basicBlock->appendInst(store);
Nicolas Capens23d99a42016-09-30 14:57:16 -0400881 }
882 else
883 {
884 assert(T(value->getType()) == type);
885
886 auto store = Ice::InstStore::create(::function, value, ptr, align);
887 ::basicBlock->appendInst(store);
888 }
889
Nicolas Capens598f8d82016-09-26 15:09:10 -0400890 return value;
891 }
892
Nicolas Capensd294def2017-01-26 17:44:37 -0800893 Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400894 {
Nicolas Capens8820f642016-09-30 04:42:43 -0400895 assert(index->getType() == Ice::IceType_i32);
896
Nicolas Capens15060bb2016-12-05 22:17:19 -0500897 if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index))
898 {
Nicolas Capens584088c2017-01-26 16:05:18 -0800899 int32_t offset = constant->getValue() * (int)typeSize(type);
Nicolas Capens15060bb2016-12-05 22:17:19 -0500900
901 if(offset == 0)
902 {
903 return ptr;
904 }
905
906 return createAdd(ptr, createConstantInt(offset));
907 }
908
Nicolas Capens8820f642016-09-30 04:42:43 -0400909 if(!Ice::isByteSizedType(T(type)))
910 {
Nicolas Capens584088c2017-01-26 16:05:18 -0800911 index = createMul(index, createConstantInt((int)typeSize(type)));
Nicolas Capens8820f642016-09-30 04:42:43 -0400912 }
913
914 if(sizeof(void*) == 8)
915 {
Nicolas Capensd294def2017-01-26 17:44:37 -0800916 if(unsignedIndex)
917 {
918 index = createZExt(index, T(Ice::IceType_i64));
919 }
920 else
921 {
922 index = createSExt(index, T(Ice::IceType_i64));
923 }
Nicolas Capens8820f642016-09-30 04:42:43 -0400924 }
925
926 return createAdd(ptr, index);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400927 }
928
929 Value *Nucleus::createAtomicAdd(Value *ptr, Value *value)
930 {
931 assert(false && "UNIMPLEMENTED"); return nullptr;
932 }
933
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400934 static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType)
935 {
Nicolas Capens23d99a42016-09-30 14:57:16 -0400936 if(v->getType() == T(destType))
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400937 {
938 return v;
939 }
940
941 Ice::Variable *result = ::function->makeVariable(T(destType));
942 Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v);
943 ::basicBlock->appendInst(cast);
944
945 return V(result);
946 }
947
Nicolas Capens598f8d82016-09-26 15:09:10 -0400948 Value *Nucleus::createTrunc(Value *v, Type *destType)
949 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400950 return createCast(Ice::InstCast::Trunc, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400951 }
952
953 Value *Nucleus::createZExt(Value *v, Type *destType)
954 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400955 return createCast(Ice::InstCast::Zext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400956 }
957
958 Value *Nucleus::createSExt(Value *v, Type *destType)
959 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400960 return createCast(Ice::InstCast::Sext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400961 }
962
963 Value *Nucleus::createFPToSI(Value *v, Type *destType)
964 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400965 return createCast(Ice::InstCast::Fptosi, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400966 }
967
Nicolas Capens598f8d82016-09-26 15:09:10 -0400968 Value *Nucleus::createSIToFP(Value *v, Type *destType)
969 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400970 return createCast(Ice::InstCast::Sitofp, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400971 }
972
973 Value *Nucleus::createFPTrunc(Value *v, Type *destType)
974 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400975 return createCast(Ice::InstCast::Fptrunc, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400976 }
977
978 Value *Nucleus::createFPExt(Value *v, Type *destType)
979 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -0400980 return createCast(Ice::InstCast::Fpext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400981 }
982
983 Value *Nucleus::createBitCast(Value *v, Type *destType)
984 {
Nicolas Capens2d8c3702017-07-25 13:56:46 -0400985 // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need
986 // support for casting between scalars and wide vectors. For platforms where this is not supported,
987 // emulate them by writing to the stack and reading back as the destination type.
988 if(emulateMismatchedBitCast)
989 {
990 if(!Ice::isVectorType(v->getType()) && Ice::isVectorType(T(destType)))
991 {
992 Value *address = allocateStackVariable(destType);
993 createStore(v, address, T(v->getType()));
994 return createLoad(address, destType);
995 }
996 else if(Ice::isVectorType(v->getType()) && !Ice::isVectorType(T(destType)))
997 {
998 Value *address = allocateStackVariable(T(v->getType()));
999 createStore(v, address, T(v->getType()));
1000 return createLoad(address, destType);
1001 }
1002 }
1003
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001004 return createCast(Ice::InstCast::Bitcast, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001005 }
1006
Nicolas Capens43dc6292016-10-20 00:01:38 -04001007 static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001008 {
Nicolas Capens611642a2016-09-28 16:45:04 -04001009 assert(lhs->getType() == rhs->getType());
1010
Nicolas Capens43dc6292016-10-20 00:01:38 -04001011 auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType());
1012 auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs);
Nicolas Capens611642a2016-09-28 16:45:04 -04001013 ::basicBlock->appendInst(cmp);
1014
1015 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001016 }
1017
Nicolas Capens43dc6292016-10-20 00:01:38 -04001018 Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs)
1019 {
1020 return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs);
1021 }
1022
1023 Value *Nucleus::createICmpNE(Value *lhs, Value *rhs)
1024 {
1025 return createIntCompare(Ice::InstIcmp::Ne, lhs, rhs);
1026 }
1027
1028 Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs)
1029 {
1030 return createIntCompare(Ice::InstIcmp::Ugt, lhs, rhs);
1031 }
1032
1033 Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs)
1034 {
1035 return createIntCompare(Ice::InstIcmp::Uge, lhs, rhs);
1036 }
1037
1038 Value *Nucleus::createICmpULT(Value *lhs, Value *rhs)
1039 {
1040 return createIntCompare(Ice::InstIcmp::Ult, lhs, rhs);
1041 }
1042
1043 Value *Nucleus::createICmpULE(Value *lhs, Value *rhs)
1044 {
1045 return createIntCompare(Ice::InstIcmp::Ule, lhs, rhs);
1046 }
1047
1048 Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs)
1049 {
1050 return createIntCompare(Ice::InstIcmp::Sgt, lhs, rhs);
1051 }
1052
1053 Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs)
1054 {
1055 return createIntCompare(Ice::InstIcmp::Sge, lhs, rhs);
1056 }
1057
1058 Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs)
1059 {
1060 return createIntCompare(Ice::InstIcmp::Slt, lhs, rhs);
1061 }
1062
Nicolas Capens598f8d82016-09-26 15:09:10 -04001063 Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs)
1064 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001065 return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs);
1066 }
1067
1068 static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs)
1069 {
1070 assert(lhs->getType() == rhs->getType());
1071 assert(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32);
1072
1073 auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32);
1074 auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs);
1075 ::basicBlock->appendInst(cmp);
1076
1077 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001078 }
1079
1080 Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs)
1081 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001082 return createFloatCompare(Ice::InstFcmp::Oeq, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001083 }
1084
1085 Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs)
1086 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001087 return createFloatCompare(Ice::InstFcmp::Ogt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001088 }
1089
1090 Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs)
1091 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001092 return createFloatCompare(Ice::InstFcmp::Oge, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001093 }
1094
1095 Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs)
1096 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001097 return createFloatCompare(Ice::InstFcmp::Olt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001098 }
1099
1100 Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs)
1101 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001102 return createFloatCompare(Ice::InstFcmp::Ole, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001103 }
1104
1105 Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs)
1106 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001107 return createFloatCompare(Ice::InstFcmp::One, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001108 }
1109
1110 Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs)
1111 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001112 return createFloatCompare(Ice::InstFcmp::Ord, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001113 }
1114
1115 Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs)
1116 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001117 return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001118 }
1119
1120 Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs)
1121 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001122 return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001123 }
1124
1125 Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs)
1126 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001127 return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001128 }
1129
1130 Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs)
1131 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001132 return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001133 }
1134
1135 Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs)
1136 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001137 return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001138 }
1139
1140 Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs)
1141 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001142 return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001143 }
1144
1145 Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs)
1146 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001147 return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001148 }
1149
Nicolas Capense95d5342016-09-30 11:37:28 -04001150 Value *Nucleus::createExtractElement(Value *vector, Type *type, int index)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001151 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001152 auto result = ::function->makeVariable(T(type));
1153 auto extract = Ice::InstExtractElement::create(::function, result, vector, ::context->getConstantInt32(index));
1154 ::basicBlock->appendInst(extract);
1155
1156 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001157 }
1158
1159 Value *Nucleus::createInsertElement(Value *vector, Value *element, int index)
1160 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001161 auto result = ::function->makeVariable(vector->getType());
1162 auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index));
1163 ::basicBlock->appendInst(insert);
1164
1165 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001166 }
1167
Nicolas Capense89cd582016-09-30 14:23:47 -04001168 Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001169 {
Nicolas Capens619c0ab2016-09-30 14:46:24 -04001170 assert(V1->getType() == V2->getType());
1171
1172 int size = Ice::typeNumElements(V1->getType());
1173 auto result = ::function->makeVariable(V1->getType());
1174 auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2);
1175
1176 for(int i = 0; i < size; i++)
1177 {
1178 shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i])));
1179 }
1180
1181 ::basicBlock->appendInst(shuffle);
1182
1183 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001184 }
1185
1186 Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse)
1187 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04001188 assert(ifTrue->getType() == ifFalse->getType());
1189
1190 auto result = ::function->makeVariable(ifTrue->getType());
1191 auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse);
1192 ::basicBlock->appendInst(select);
1193
1194 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001195 }
1196
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001197 SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001198 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001199 auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch);
1200 ::basicBlock->appendInst(switchInst);
1201
1202 return reinterpret_cast<SwitchCases*>(switchInst);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001203 }
1204
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001205 void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001206 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001207 switchCases->addBranch(label, label, branch);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001208 }
1209
1210 void Nucleus::createUnreachable()
1211 {
Nicolas Capensfdcca2d2016-10-20 11:31:36 -04001212 Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function);
1213 ::basicBlock->appendInst(unreachable);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001214 }
1215
Nicolas Capense95d5342016-09-30 11:37:28 -04001216 static Value *createSwizzle4(Value *val, unsigned char select)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001217 {
Nicolas Capens619c0ab2016-09-30 14:46:24 -04001218 int swizzle[4] =
1219 {
1220 (select >> 0) & 0x03,
1221 (select >> 2) & 0x03,
1222 (select >> 4) & 0x03,
1223 (select >> 6) & 0x03,
1224 };
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001225
Nicolas Capens619c0ab2016-09-30 14:46:24 -04001226 return Nucleus::createShuffleVector(val, val, swizzle);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001227 }
1228
Nicolas Capense95d5342016-09-30 11:37:28 -04001229 static Value *createMask4(Value *lhs, Value *rhs, unsigned char select)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001230 {
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001231 int64_t mask[4] = {0, 0, 0, 0};
1232
1233 mask[(select >> 0) & 0x03] = -1;
1234 mask[(select >> 2) & 0x03] = -1;
1235 mask[(select >> 4) & 0x03] = -1;
1236 mask[(select >> 6) & 0x03] = -1;
1237
1238 Value *condition = Nucleus::createConstantVector(mask, T(Ice::IceType_v4i1));
1239 Value *result = Nucleus::createSelect(condition, rhs, lhs);
1240
1241 return result;
Nicolas Capens598f8d82016-09-26 15:09:10 -04001242 }
1243
Nicolas Capens598f8d82016-09-26 15:09:10 -04001244 Type *Nucleus::getPointerType(Type *ElementType)
1245 {
Nicolas Capense12780d2016-09-27 14:18:07 -04001246 if(sizeof(void*) == 8)
1247 {
1248 return T(Ice::IceType_i64);
1249 }
1250 else
1251 {
1252 return T(Ice::IceType_i32);
1253 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04001254 }
1255
Nicolas Capens13ac2322016-10-13 14:52:12 -04001256 Value *Nucleus::createNullValue(Type *Ty)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001257 {
Nicolas Capens73dd7a22016-10-20 13:20:34 -04001258 if(Ice::isVectorType(T(Ty)))
1259 {
Nicolas Capens30385f02017-04-18 13:03:47 -04001260 assert(Ice::typeNumElements(T(Ty)) <= 16);
1261 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 -04001262 return createConstantVector(c, Ty);
1263 }
1264 else
1265 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001266 return V(::context->getConstantZero(T(Ty)));
Nicolas Capens73dd7a22016-10-20 13:20:34 -04001267 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04001268 }
1269
Nicolas Capens13ac2322016-10-13 14:52:12 -04001270 Value *Nucleus::createConstantLong(int64_t i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001271 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001272 return V(::context->getConstantInt64(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001273 }
1274
Nicolas Capens13ac2322016-10-13 14:52:12 -04001275 Value *Nucleus::createConstantInt(int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001276 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001277 return V(::context->getConstantInt32(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001278 }
1279
Nicolas Capens13ac2322016-10-13 14:52:12 -04001280 Value *Nucleus::createConstantInt(unsigned int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001281 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001282 return V(::context->getConstantInt32(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001283 }
1284
Nicolas Capens13ac2322016-10-13 14:52:12 -04001285 Value *Nucleus::createConstantBool(bool b)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001286 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001287 return V(::context->getConstantInt1(b));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001288 }
1289
Nicolas Capens13ac2322016-10-13 14:52:12 -04001290 Value *Nucleus::createConstantByte(signed char i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001291 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001292 return V(::context->getConstantInt8(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001293 }
1294
Nicolas Capens13ac2322016-10-13 14:52:12 -04001295 Value *Nucleus::createConstantByte(unsigned char i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001296 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001297 return V(::context->getConstantInt8(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001298 }
1299
Nicolas Capens13ac2322016-10-13 14:52:12 -04001300 Value *Nucleus::createConstantShort(short i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001301 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001302 return V(::context->getConstantInt16(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001303 }
1304
Nicolas Capens13ac2322016-10-13 14:52:12 -04001305 Value *Nucleus::createConstantShort(unsigned short i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001306 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001307 return V(::context->getConstantInt16(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001308 }
1309
Nicolas Capens13ac2322016-10-13 14:52:12 -04001310 Value *Nucleus::createConstantFloat(float x)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001311 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001312 return V(::context->getConstantFloat(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001313 }
1314
Nicolas Capens13ac2322016-10-13 14:52:12 -04001315 Value *Nucleus::createNullPointer(Type *Ty)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001316 {
Nicolas Capensa29d6532016-12-05 21:38:09 -05001317 return createNullValue(T(sizeof(void*) == 8 ? Ice::IceType_i64 : Ice::IceType_i32));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001318 }
1319
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001320 Value *Nucleus::createConstantVector(const int64_t *constants, Type *type)
Nicolas Capens13ac2322016-10-13 14:52:12 -04001321 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001322 const int vectorSize = 16;
1323 assert(Ice::typeWidthInBytes(T(type)) == vectorSize);
1324 const int alignment = vectorSize;
1325 auto globalPool = ::function->getGlobalPool();
1326
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001327 const int64_t *i = constants;
1328 const double *f = reinterpret_cast<const double*>(constants);
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001329 Ice::VariableDeclaration::DataInitializer *dataInitializer = nullptr;
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001330
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001331 switch((int)reinterpret_cast<intptr_t>(type))
1332 {
1333 case Ice::IceType_v4i32:
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001334 case Ice::IceType_v4i1:
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001335 {
1336 const int initializer[4] = {(int)i[0], (int)i[1], (int)i[2], (int)i[3]};
1337 static_assert(sizeof(initializer) == vectorSize, "!");
1338 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1339 }
1340 break;
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001341 case Ice::IceType_v4f32:
1342 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001343 const float initializer[4] = {(float)f[0], (float)f[1], (float)f[2], (float)f[3]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001344 static_assert(sizeof(initializer) == vectorSize, "!");
1345 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1346 }
1347 break;
1348 case Ice::IceType_v8i16:
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001349 case Ice::IceType_v8i1:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001350 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001351 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 -04001352 static_assert(sizeof(initializer) == vectorSize, "!");
1353 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1354 }
1355 break;
1356 case Ice::IceType_v16i8:
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001357 case Ice::IceType_v16i1:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001358 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001359 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 -04001360 static_assert(sizeof(initializer) == vectorSize, "!");
1361 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1362 }
1363 break;
1364 case Type_v2i32:
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001365 {
1366 const int initializer[4] = {(int)i[0], (int)i[1], (int)i[0], (int)i[1]};
1367 static_assert(sizeof(initializer) == vectorSize, "!");
1368 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1369 }
1370 break;
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001371 case Type_v2f32:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001372 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001373 const float initializer[4] = {(float)f[0], (float)f[1], (float)f[0], (float)f[1]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001374 static_assert(sizeof(initializer) == vectorSize, "!");
1375 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1376 }
1377 break;
1378 case Type_v4i16:
1379 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001380 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 -04001381 static_assert(sizeof(initializer) == vectorSize, "!");
1382 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1383 }
1384 break;
1385 case Type_v8i8:
1386 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001387 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 -04001388 static_assert(sizeof(initializer) == vectorSize, "!");
1389 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1390 }
1391 break;
1392 case Type_v4i8:
1393 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001394 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 -04001395 static_assert(sizeof(initializer) == vectorSize, "!");
1396 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1397 }
1398 break;
1399 default:
1400 assert(false && "Unknown constant vector type" && type);
1401 }
1402
1403 auto name = Ice::GlobalString::createWithoutString(::context);
1404 auto *variableDeclaration = Ice::VariableDeclaration::create(globalPool);
1405 variableDeclaration->setName(name);
1406 variableDeclaration->setAlignment(alignment);
1407 variableDeclaration->setIsConstant(true);
1408 variableDeclaration->addInitializer(dataInitializer);
Nicolas Capens87852e12016-11-24 14:45:06 -05001409
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001410 ::function->addGlobal(variableDeclaration);
1411
1412 constexpr int32_t offset = 0;
1413 Ice::Operand *ptr = ::context->getConstantSym(offset, name);
1414
1415 Ice::Variable *result = ::function->makeVariable(T(type));
1416 auto load = Ice::InstLoad::create(::function, result, ptr, alignment);
1417 ::basicBlock->appendInst(load);
1418
1419 return V(result);
Nicolas Capens13ac2322016-10-13 14:52:12 -04001420 }
1421
1422 Value *Nucleus::createConstantVector(const double *constants, Type *type)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001423 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001424 return createConstantVector((const int64_t*)constants, type);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001425 }
1426
1427 Type *Void::getType()
1428 {
1429 return T(Ice::IceType_void);
1430 }
1431
Nicolas Capens598f8d82016-09-26 15:09:10 -04001432 Bool::Bool(Argument<Bool> argument)
1433 {
1434 storeValue(argument.value);
1435 }
1436
Nicolas Capens598f8d82016-09-26 15:09:10 -04001437 Bool::Bool(bool x)
1438 {
1439 storeValue(Nucleus::createConstantBool(x));
1440 }
1441
1442 Bool::Bool(RValue<Bool> rhs)
1443 {
1444 storeValue(rhs.value);
1445 }
1446
1447 Bool::Bool(const Bool &rhs)
1448 {
1449 Value *value = rhs.loadValue();
1450 storeValue(value);
1451 }
1452
1453 Bool::Bool(const Reference<Bool> &rhs)
1454 {
1455 Value *value = rhs.loadValue();
1456 storeValue(value);
1457 }
1458
Nicolas Capens96d4e092016-11-18 14:22:38 -05001459 RValue<Bool> Bool::operator=(RValue<Bool> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001460 {
1461 storeValue(rhs.value);
1462
1463 return rhs;
1464 }
1465
Nicolas Capens96d4e092016-11-18 14:22:38 -05001466 RValue<Bool> Bool::operator=(const Bool &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001467 {
1468 Value *value = rhs.loadValue();
1469 storeValue(value);
1470
1471 return RValue<Bool>(value);
1472 }
1473
Nicolas Capens96d4e092016-11-18 14:22:38 -05001474 RValue<Bool> Bool::operator=(const Reference<Bool> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001475 {
1476 Value *value = rhs.loadValue();
1477 storeValue(value);
1478
1479 return RValue<Bool>(value);
1480 }
1481
1482 RValue<Bool> operator!(RValue<Bool> val)
1483 {
1484 return RValue<Bool>(Nucleus::createNot(val.value));
1485 }
1486
1487 RValue<Bool> operator&&(RValue<Bool> lhs, RValue<Bool> rhs)
1488 {
1489 return RValue<Bool>(Nucleus::createAnd(lhs.value, rhs.value));
1490 }
1491
1492 RValue<Bool> operator||(RValue<Bool> lhs, RValue<Bool> rhs)
1493 {
1494 return RValue<Bool>(Nucleus::createOr(lhs.value, rhs.value));
1495 }
1496
1497 Type *Bool::getType()
1498 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001499 return T(Ice::IceType_i1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001500 }
1501
1502 Byte::Byte(Argument<Byte> argument)
1503 {
1504 storeValue(argument.value);
1505 }
1506
1507 Byte::Byte(RValue<Int> cast)
1508 {
1509 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1510
1511 storeValue(integer);
1512 }
1513
1514 Byte::Byte(RValue<UInt> cast)
1515 {
1516 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1517
1518 storeValue(integer);
1519 }
1520
1521 Byte::Byte(RValue<UShort> cast)
1522 {
1523 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1524
1525 storeValue(integer);
1526 }
1527
Nicolas Capens598f8d82016-09-26 15:09:10 -04001528 Byte::Byte(int x)
1529 {
1530 storeValue(Nucleus::createConstantByte((unsigned char)x));
1531 }
1532
1533 Byte::Byte(unsigned char x)
1534 {
1535 storeValue(Nucleus::createConstantByte(x));
1536 }
1537
1538 Byte::Byte(RValue<Byte> rhs)
1539 {
1540 storeValue(rhs.value);
1541 }
1542
1543 Byte::Byte(const Byte &rhs)
1544 {
1545 Value *value = rhs.loadValue();
1546 storeValue(value);
1547 }
1548
1549 Byte::Byte(const Reference<Byte> &rhs)
1550 {
1551 Value *value = rhs.loadValue();
1552 storeValue(value);
1553 }
1554
Nicolas Capens96d4e092016-11-18 14:22:38 -05001555 RValue<Byte> Byte::operator=(RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001556 {
1557 storeValue(rhs.value);
1558
1559 return rhs;
1560 }
1561
Nicolas Capens96d4e092016-11-18 14:22:38 -05001562 RValue<Byte> Byte::operator=(const Byte &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001563 {
1564 Value *value = rhs.loadValue();
1565 storeValue(value);
1566
1567 return RValue<Byte>(value);
1568 }
1569
Nicolas Capens96d4e092016-11-18 14:22:38 -05001570 RValue<Byte> Byte::operator=(const Reference<Byte> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001571 {
1572 Value *value = rhs.loadValue();
1573 storeValue(value);
1574
1575 return RValue<Byte>(value);
1576 }
1577
1578 RValue<Byte> operator+(RValue<Byte> lhs, RValue<Byte> rhs)
1579 {
1580 return RValue<Byte>(Nucleus::createAdd(lhs.value, rhs.value));
1581 }
1582
1583 RValue<Byte> operator-(RValue<Byte> lhs, RValue<Byte> rhs)
1584 {
1585 return RValue<Byte>(Nucleus::createSub(lhs.value, rhs.value));
1586 }
1587
1588 RValue<Byte> operator*(RValue<Byte> lhs, RValue<Byte> rhs)
1589 {
1590 return RValue<Byte>(Nucleus::createMul(lhs.value, rhs.value));
1591 }
1592
1593 RValue<Byte> operator/(RValue<Byte> lhs, RValue<Byte> rhs)
1594 {
1595 return RValue<Byte>(Nucleus::createUDiv(lhs.value, rhs.value));
1596 }
1597
1598 RValue<Byte> operator%(RValue<Byte> lhs, RValue<Byte> rhs)
1599 {
1600 return RValue<Byte>(Nucleus::createURem(lhs.value, rhs.value));
1601 }
1602
1603 RValue<Byte> operator&(RValue<Byte> lhs, RValue<Byte> rhs)
1604 {
1605 return RValue<Byte>(Nucleus::createAnd(lhs.value, rhs.value));
1606 }
1607
1608 RValue<Byte> operator|(RValue<Byte> lhs, RValue<Byte> rhs)
1609 {
1610 return RValue<Byte>(Nucleus::createOr(lhs.value, rhs.value));
1611 }
1612
1613 RValue<Byte> operator^(RValue<Byte> lhs, RValue<Byte> rhs)
1614 {
1615 return RValue<Byte>(Nucleus::createXor(lhs.value, rhs.value));
1616 }
1617
1618 RValue<Byte> operator<<(RValue<Byte> lhs, RValue<Byte> rhs)
1619 {
1620 return RValue<Byte>(Nucleus::createShl(lhs.value, rhs.value));
1621 }
1622
1623 RValue<Byte> operator>>(RValue<Byte> lhs, RValue<Byte> rhs)
1624 {
1625 return RValue<Byte>(Nucleus::createLShr(lhs.value, rhs.value));
1626 }
1627
Nicolas Capens96d4e092016-11-18 14:22:38 -05001628 RValue<Byte> operator+=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001629 {
1630 return lhs = lhs + rhs;
1631 }
1632
Nicolas Capens96d4e092016-11-18 14:22:38 -05001633 RValue<Byte> operator-=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001634 {
1635 return lhs = lhs - rhs;
1636 }
1637
Nicolas Capens96d4e092016-11-18 14:22:38 -05001638 RValue<Byte> operator*=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001639 {
1640 return lhs = lhs * rhs;
1641 }
1642
Nicolas Capens96d4e092016-11-18 14:22:38 -05001643 RValue<Byte> operator/=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001644 {
1645 return lhs = lhs / rhs;
1646 }
1647
Nicolas Capens96d4e092016-11-18 14:22:38 -05001648 RValue<Byte> operator%=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001649 {
1650 return lhs = lhs % rhs;
1651 }
1652
Nicolas Capens96d4e092016-11-18 14:22:38 -05001653 RValue<Byte> operator&=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001654 {
1655 return lhs = lhs & rhs;
1656 }
1657
Nicolas Capens96d4e092016-11-18 14:22:38 -05001658 RValue<Byte> operator|=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001659 {
1660 return lhs = lhs | rhs;
1661 }
1662
Nicolas Capens96d4e092016-11-18 14:22:38 -05001663 RValue<Byte> operator^=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001664 {
1665 return lhs = lhs ^ rhs;
1666 }
1667
Nicolas Capens96d4e092016-11-18 14:22:38 -05001668 RValue<Byte> operator<<=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001669 {
1670 return lhs = lhs << rhs;
1671 }
1672
Nicolas Capens96d4e092016-11-18 14:22:38 -05001673 RValue<Byte> operator>>=(Byte &lhs, RValue<Byte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001674 {
1675 return lhs = lhs >> rhs;
1676 }
1677
1678 RValue<Byte> operator+(RValue<Byte> val)
1679 {
1680 return val;
1681 }
1682
1683 RValue<Byte> operator-(RValue<Byte> val)
1684 {
1685 return RValue<Byte>(Nucleus::createNeg(val.value));
1686 }
1687
1688 RValue<Byte> operator~(RValue<Byte> val)
1689 {
1690 return RValue<Byte>(Nucleus::createNot(val.value));
1691 }
1692
Nicolas Capens96d4e092016-11-18 14:22:38 -05001693 RValue<Byte> operator++(Byte &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001694 {
1695 RValue<Byte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001696 val += Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001697 return res;
1698 }
1699
Nicolas Capens96d4e092016-11-18 14:22:38 -05001700 const Byte &operator++(Byte &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001701 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001702 val += Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001703 return val;
1704 }
1705
Nicolas Capens96d4e092016-11-18 14:22:38 -05001706 RValue<Byte> operator--(Byte &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001707 {
1708 RValue<Byte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001709 val -= Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001710 return res;
1711 }
1712
Nicolas Capens96d4e092016-11-18 14:22:38 -05001713 const Byte &operator--(Byte &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001714 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001715 val -= Byte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001716 return val;
1717 }
1718
1719 RValue<Bool> operator<(RValue<Byte> lhs, RValue<Byte> rhs)
1720 {
1721 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
1722 }
1723
1724 RValue<Bool> operator<=(RValue<Byte> lhs, RValue<Byte> rhs)
1725 {
1726 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
1727 }
1728
1729 RValue<Bool> operator>(RValue<Byte> lhs, RValue<Byte> rhs)
1730 {
1731 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
1732 }
1733
1734 RValue<Bool> operator>=(RValue<Byte> lhs, RValue<Byte> rhs)
1735 {
1736 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
1737 }
1738
1739 RValue<Bool> operator!=(RValue<Byte> lhs, RValue<Byte> rhs)
1740 {
1741 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
1742 }
1743
1744 RValue<Bool> operator==(RValue<Byte> lhs, RValue<Byte> rhs)
1745 {
1746 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
1747 }
1748
1749 Type *Byte::getType()
1750 {
Nicolas Capens6d738712016-09-30 04:15:22 -04001751 return T(Ice::IceType_i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001752 }
1753
1754 SByte::SByte(Argument<SByte> argument)
1755 {
1756 storeValue(argument.value);
1757 }
1758
1759 SByte::SByte(RValue<Int> cast)
1760 {
1761 Value *integer = Nucleus::createTrunc(cast.value, SByte::getType());
1762
1763 storeValue(integer);
1764 }
1765
1766 SByte::SByte(RValue<Short> cast)
1767 {
1768 Value *integer = Nucleus::createTrunc(cast.value, SByte::getType());
1769
1770 storeValue(integer);
1771 }
1772
Nicolas Capens598f8d82016-09-26 15:09:10 -04001773 SByte::SByte(signed char x)
1774 {
1775 storeValue(Nucleus::createConstantByte(x));
1776 }
1777
1778 SByte::SByte(RValue<SByte> rhs)
1779 {
1780 storeValue(rhs.value);
1781 }
1782
1783 SByte::SByte(const SByte &rhs)
1784 {
1785 Value *value = rhs.loadValue();
1786 storeValue(value);
1787 }
1788
1789 SByte::SByte(const Reference<SByte> &rhs)
1790 {
1791 Value *value = rhs.loadValue();
1792 storeValue(value);
1793 }
1794
Nicolas Capens96d4e092016-11-18 14:22:38 -05001795 RValue<SByte> SByte::operator=(RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001796 {
1797 storeValue(rhs.value);
1798
1799 return rhs;
1800 }
1801
Nicolas Capens96d4e092016-11-18 14:22:38 -05001802 RValue<SByte> SByte::operator=(const SByte &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001803 {
1804 Value *value = rhs.loadValue();
1805 storeValue(value);
1806
1807 return RValue<SByte>(value);
1808 }
1809
Nicolas Capens96d4e092016-11-18 14:22:38 -05001810 RValue<SByte> SByte::operator=(const Reference<SByte> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001811 {
1812 Value *value = rhs.loadValue();
1813 storeValue(value);
1814
1815 return RValue<SByte>(value);
1816 }
1817
1818 RValue<SByte> operator+(RValue<SByte> lhs, RValue<SByte> rhs)
1819 {
1820 return RValue<SByte>(Nucleus::createAdd(lhs.value, rhs.value));
1821 }
1822
1823 RValue<SByte> operator-(RValue<SByte> lhs, RValue<SByte> rhs)
1824 {
1825 return RValue<SByte>(Nucleus::createSub(lhs.value, rhs.value));
1826 }
1827
1828 RValue<SByte> operator*(RValue<SByte> lhs, RValue<SByte> rhs)
1829 {
1830 return RValue<SByte>(Nucleus::createMul(lhs.value, rhs.value));
1831 }
1832
1833 RValue<SByte> operator/(RValue<SByte> lhs, RValue<SByte> rhs)
1834 {
1835 return RValue<SByte>(Nucleus::createSDiv(lhs.value, rhs.value));
1836 }
1837
1838 RValue<SByte> operator%(RValue<SByte> lhs, RValue<SByte> rhs)
1839 {
1840 return RValue<SByte>(Nucleus::createSRem(lhs.value, rhs.value));
1841 }
1842
1843 RValue<SByte> operator&(RValue<SByte> lhs, RValue<SByte> rhs)
1844 {
1845 return RValue<SByte>(Nucleus::createAnd(lhs.value, rhs.value));
1846 }
1847
1848 RValue<SByte> operator|(RValue<SByte> lhs, RValue<SByte> rhs)
1849 {
1850 return RValue<SByte>(Nucleus::createOr(lhs.value, rhs.value));
1851 }
1852
1853 RValue<SByte> operator^(RValue<SByte> lhs, RValue<SByte> rhs)
1854 {
1855 return RValue<SByte>(Nucleus::createXor(lhs.value, rhs.value));
1856 }
1857
1858 RValue<SByte> operator<<(RValue<SByte> lhs, RValue<SByte> rhs)
1859 {
1860 return RValue<SByte>(Nucleus::createShl(lhs.value, rhs.value));
1861 }
1862
1863 RValue<SByte> operator>>(RValue<SByte> lhs, RValue<SByte> rhs)
1864 {
1865 return RValue<SByte>(Nucleus::createAShr(lhs.value, rhs.value));
1866 }
1867
Nicolas Capens96d4e092016-11-18 14:22:38 -05001868 RValue<SByte> operator+=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001869 {
1870 return lhs = lhs + rhs;
1871 }
1872
Nicolas Capens96d4e092016-11-18 14:22:38 -05001873 RValue<SByte> operator-=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001874 {
1875 return lhs = lhs - rhs;
1876 }
1877
Nicolas Capens96d4e092016-11-18 14:22:38 -05001878 RValue<SByte> operator*=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001879 {
1880 return lhs = lhs * rhs;
1881 }
1882
Nicolas Capens96d4e092016-11-18 14:22:38 -05001883 RValue<SByte> operator/=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001884 {
1885 return lhs = lhs / rhs;
1886 }
1887
Nicolas Capens96d4e092016-11-18 14:22:38 -05001888 RValue<SByte> operator%=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001889 {
1890 return lhs = lhs % rhs;
1891 }
1892
Nicolas Capens96d4e092016-11-18 14:22:38 -05001893 RValue<SByte> operator&=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001894 {
1895 return lhs = lhs & rhs;
1896 }
1897
Nicolas Capens96d4e092016-11-18 14:22:38 -05001898 RValue<SByte> operator|=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001899 {
1900 return lhs = lhs | rhs;
1901 }
1902
Nicolas Capens96d4e092016-11-18 14:22:38 -05001903 RValue<SByte> operator^=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001904 {
1905 return lhs = lhs ^ rhs;
1906 }
1907
Nicolas Capens96d4e092016-11-18 14:22:38 -05001908 RValue<SByte> operator<<=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001909 {
1910 return lhs = lhs << rhs;
1911 }
1912
Nicolas Capens96d4e092016-11-18 14:22:38 -05001913 RValue<SByte> operator>>=(SByte &lhs, RValue<SByte> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001914 {
1915 return lhs = lhs >> rhs;
1916 }
1917
1918 RValue<SByte> operator+(RValue<SByte> val)
1919 {
1920 return val;
1921 }
1922
1923 RValue<SByte> operator-(RValue<SByte> val)
1924 {
1925 return RValue<SByte>(Nucleus::createNeg(val.value));
1926 }
1927
1928 RValue<SByte> operator~(RValue<SByte> val)
1929 {
1930 return RValue<SByte>(Nucleus::createNot(val.value));
1931 }
1932
Nicolas Capens96d4e092016-11-18 14:22:38 -05001933 RValue<SByte> operator++(SByte &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001934 {
1935 RValue<SByte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001936 val += SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001937 return res;
1938 }
1939
Nicolas Capens96d4e092016-11-18 14:22:38 -05001940 const SByte &operator++(SByte &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04001941 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001942 val += SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001943 return val;
1944 }
1945
Nicolas Capens96d4e092016-11-18 14:22:38 -05001946 RValue<SByte> operator--(SByte &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001947 {
1948 RValue<SByte> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05001949 val -= SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001950 return res;
1951 }
1952
Nicolas Capens96d4e092016-11-18 14:22:38 -05001953 const SByte &operator--(SByte &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04001954 {
Nicolas Capensd1229402016-11-07 16:05:22 -05001955 val -= SByte(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001956 return val;
1957 }
1958
1959 RValue<Bool> operator<(RValue<SByte> lhs, RValue<SByte> rhs)
1960 {
1961 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
1962 }
1963
1964 RValue<Bool> operator<=(RValue<SByte> lhs, RValue<SByte> rhs)
1965 {
1966 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
1967 }
1968
1969 RValue<Bool> operator>(RValue<SByte> lhs, RValue<SByte> rhs)
1970 {
1971 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
1972 }
1973
1974 RValue<Bool> operator>=(RValue<SByte> lhs, RValue<SByte> rhs)
1975 {
1976 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
1977 }
1978
1979 RValue<Bool> operator!=(RValue<SByte> lhs, RValue<SByte> rhs)
1980 {
1981 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
1982 }
1983
1984 RValue<Bool> operator==(RValue<SByte> lhs, RValue<SByte> rhs)
1985 {
1986 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
1987 }
1988
1989 Type *SByte::getType()
1990 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001991 return T(Ice::IceType_i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001992 }
1993
1994 Short::Short(Argument<Short> argument)
1995 {
1996 storeValue(argument.value);
1997 }
1998
1999 Short::Short(RValue<Int> cast)
2000 {
2001 Value *integer = Nucleus::createTrunc(cast.value, Short::getType());
2002
2003 storeValue(integer);
2004 }
2005
Nicolas Capens598f8d82016-09-26 15:09:10 -04002006 Short::Short(short x)
2007 {
2008 storeValue(Nucleus::createConstantShort(x));
2009 }
2010
2011 Short::Short(RValue<Short> rhs)
2012 {
2013 storeValue(rhs.value);
2014 }
2015
2016 Short::Short(const Short &rhs)
2017 {
2018 Value *value = rhs.loadValue();
2019 storeValue(value);
2020 }
2021
2022 Short::Short(const Reference<Short> &rhs)
2023 {
2024 Value *value = rhs.loadValue();
2025 storeValue(value);
2026 }
2027
Nicolas Capens96d4e092016-11-18 14:22:38 -05002028 RValue<Short> Short::operator=(RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002029 {
2030 storeValue(rhs.value);
2031
2032 return rhs;
2033 }
2034
Nicolas Capens96d4e092016-11-18 14:22:38 -05002035 RValue<Short> Short::operator=(const Short &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002036 {
2037 Value *value = rhs.loadValue();
2038 storeValue(value);
2039
2040 return RValue<Short>(value);
2041 }
2042
Nicolas Capens96d4e092016-11-18 14:22:38 -05002043 RValue<Short> Short::operator=(const Reference<Short> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002044 {
2045 Value *value = rhs.loadValue();
2046 storeValue(value);
2047
2048 return RValue<Short>(value);
2049 }
2050
2051 RValue<Short> operator+(RValue<Short> lhs, RValue<Short> rhs)
2052 {
2053 return RValue<Short>(Nucleus::createAdd(lhs.value, rhs.value));
2054 }
2055
2056 RValue<Short> operator-(RValue<Short> lhs, RValue<Short> rhs)
2057 {
2058 return RValue<Short>(Nucleus::createSub(lhs.value, rhs.value));
2059 }
2060
2061 RValue<Short> operator*(RValue<Short> lhs, RValue<Short> rhs)
2062 {
2063 return RValue<Short>(Nucleus::createMul(lhs.value, rhs.value));
2064 }
2065
2066 RValue<Short> operator/(RValue<Short> lhs, RValue<Short> rhs)
2067 {
2068 return RValue<Short>(Nucleus::createSDiv(lhs.value, rhs.value));
2069 }
2070
2071 RValue<Short> operator%(RValue<Short> lhs, RValue<Short> rhs)
2072 {
2073 return RValue<Short>(Nucleus::createSRem(lhs.value, rhs.value));
2074 }
2075
2076 RValue<Short> operator&(RValue<Short> lhs, RValue<Short> rhs)
2077 {
2078 return RValue<Short>(Nucleus::createAnd(lhs.value, rhs.value));
2079 }
2080
2081 RValue<Short> operator|(RValue<Short> lhs, RValue<Short> rhs)
2082 {
2083 return RValue<Short>(Nucleus::createOr(lhs.value, rhs.value));
2084 }
2085
2086 RValue<Short> operator^(RValue<Short> lhs, RValue<Short> rhs)
2087 {
2088 return RValue<Short>(Nucleus::createXor(lhs.value, rhs.value));
2089 }
2090
2091 RValue<Short> operator<<(RValue<Short> lhs, RValue<Short> rhs)
2092 {
2093 return RValue<Short>(Nucleus::createShl(lhs.value, rhs.value));
2094 }
2095
2096 RValue<Short> operator>>(RValue<Short> lhs, RValue<Short> rhs)
2097 {
2098 return RValue<Short>(Nucleus::createAShr(lhs.value, rhs.value));
2099 }
2100
Nicolas Capens96d4e092016-11-18 14:22:38 -05002101 RValue<Short> operator+=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002102 {
2103 return lhs = lhs + rhs;
2104 }
2105
Nicolas Capens96d4e092016-11-18 14:22:38 -05002106 RValue<Short> operator-=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002107 {
2108 return lhs = lhs - rhs;
2109 }
2110
Nicolas Capens96d4e092016-11-18 14:22:38 -05002111 RValue<Short> operator*=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002112 {
2113 return lhs = lhs * rhs;
2114 }
2115
Nicolas Capens96d4e092016-11-18 14:22:38 -05002116 RValue<Short> operator/=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002117 {
2118 return lhs = lhs / rhs;
2119 }
2120
Nicolas Capens96d4e092016-11-18 14:22:38 -05002121 RValue<Short> operator%=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002122 {
2123 return lhs = lhs % rhs;
2124 }
2125
Nicolas Capens96d4e092016-11-18 14:22:38 -05002126 RValue<Short> operator&=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002127 {
2128 return lhs = lhs & rhs;
2129 }
2130
Nicolas Capens96d4e092016-11-18 14:22:38 -05002131 RValue<Short> operator|=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002132 {
2133 return lhs = lhs | rhs;
2134 }
2135
Nicolas Capens96d4e092016-11-18 14:22:38 -05002136 RValue<Short> operator^=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002137 {
2138 return lhs = lhs ^ rhs;
2139 }
2140
Nicolas Capens96d4e092016-11-18 14:22:38 -05002141 RValue<Short> operator<<=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002142 {
2143 return lhs = lhs << rhs;
2144 }
2145
Nicolas Capens96d4e092016-11-18 14:22:38 -05002146 RValue<Short> operator>>=(Short &lhs, RValue<Short> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002147 {
2148 return lhs = lhs >> rhs;
2149 }
2150
2151 RValue<Short> operator+(RValue<Short> val)
2152 {
2153 return val;
2154 }
2155
2156 RValue<Short> operator-(RValue<Short> val)
2157 {
2158 return RValue<Short>(Nucleus::createNeg(val.value));
2159 }
2160
2161 RValue<Short> operator~(RValue<Short> val)
2162 {
2163 return RValue<Short>(Nucleus::createNot(val.value));
2164 }
2165
Nicolas Capens96d4e092016-11-18 14:22:38 -05002166 RValue<Short> operator++(Short &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002167 {
2168 RValue<Short> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002169 val += Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002170 return res;
2171 }
2172
Nicolas Capens96d4e092016-11-18 14:22:38 -05002173 const Short &operator++(Short &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002174 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002175 val += Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002176 return val;
2177 }
2178
Nicolas Capens96d4e092016-11-18 14:22:38 -05002179 RValue<Short> operator--(Short &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002180 {
2181 RValue<Short> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002182 val -= Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002183 return res;
2184 }
2185
Nicolas Capens96d4e092016-11-18 14:22:38 -05002186 const Short &operator--(Short &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002187 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002188 val -= Short(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002189 return val;
2190 }
2191
2192 RValue<Bool> operator<(RValue<Short> lhs, RValue<Short> rhs)
2193 {
2194 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
2195 }
2196
2197 RValue<Bool> operator<=(RValue<Short> lhs, RValue<Short> rhs)
2198 {
2199 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
2200 }
2201
2202 RValue<Bool> operator>(RValue<Short> lhs, RValue<Short> rhs)
2203 {
2204 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
2205 }
2206
2207 RValue<Bool> operator>=(RValue<Short> lhs, RValue<Short> rhs)
2208 {
2209 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
2210 }
2211
2212 RValue<Bool> operator!=(RValue<Short> lhs, RValue<Short> rhs)
2213 {
2214 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
2215 }
2216
2217 RValue<Bool> operator==(RValue<Short> lhs, RValue<Short> rhs)
2218 {
2219 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
2220 }
2221
2222 Type *Short::getType()
2223 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002224 return T(Ice::IceType_i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002225 }
2226
2227 UShort::UShort(Argument<UShort> argument)
2228 {
2229 storeValue(argument.value);
2230 }
2231
2232 UShort::UShort(RValue<UInt> cast)
2233 {
2234 Value *integer = Nucleus::createTrunc(cast.value, UShort::getType());
2235
2236 storeValue(integer);
2237 }
2238
2239 UShort::UShort(RValue<Int> cast)
2240 {
2241 Value *integer = Nucleus::createTrunc(cast.value, UShort::getType());
2242
2243 storeValue(integer);
2244 }
2245
Nicolas Capens598f8d82016-09-26 15:09:10 -04002246 UShort::UShort(unsigned short x)
2247 {
2248 storeValue(Nucleus::createConstantShort(x));
2249 }
2250
2251 UShort::UShort(RValue<UShort> rhs)
2252 {
2253 storeValue(rhs.value);
2254 }
2255
2256 UShort::UShort(const UShort &rhs)
2257 {
2258 Value *value = rhs.loadValue();
2259 storeValue(value);
2260 }
2261
2262 UShort::UShort(const Reference<UShort> &rhs)
2263 {
2264 Value *value = rhs.loadValue();
2265 storeValue(value);
2266 }
2267
Nicolas Capens96d4e092016-11-18 14:22:38 -05002268 RValue<UShort> UShort::operator=(RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002269 {
2270 storeValue(rhs.value);
2271
2272 return rhs;
2273 }
2274
Nicolas Capens96d4e092016-11-18 14:22:38 -05002275 RValue<UShort> UShort::operator=(const UShort &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002276 {
2277 Value *value = rhs.loadValue();
2278 storeValue(value);
2279
2280 return RValue<UShort>(value);
2281 }
2282
Nicolas Capens96d4e092016-11-18 14:22:38 -05002283 RValue<UShort> UShort::operator=(const Reference<UShort> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002284 {
2285 Value *value = rhs.loadValue();
2286 storeValue(value);
2287
2288 return RValue<UShort>(value);
2289 }
2290
2291 RValue<UShort> operator+(RValue<UShort> lhs, RValue<UShort> rhs)
2292 {
2293 return RValue<UShort>(Nucleus::createAdd(lhs.value, rhs.value));
2294 }
2295
2296 RValue<UShort> operator-(RValue<UShort> lhs, RValue<UShort> rhs)
2297 {
2298 return RValue<UShort>(Nucleus::createSub(lhs.value, rhs.value));
2299 }
2300
2301 RValue<UShort> operator*(RValue<UShort> lhs, RValue<UShort> rhs)
2302 {
2303 return RValue<UShort>(Nucleus::createMul(lhs.value, rhs.value));
2304 }
2305
2306 RValue<UShort> operator/(RValue<UShort> lhs, RValue<UShort> rhs)
2307 {
2308 return RValue<UShort>(Nucleus::createUDiv(lhs.value, rhs.value));
2309 }
2310
2311 RValue<UShort> operator%(RValue<UShort> lhs, RValue<UShort> rhs)
2312 {
2313 return RValue<UShort>(Nucleus::createURem(lhs.value, rhs.value));
2314 }
2315
2316 RValue<UShort> operator&(RValue<UShort> lhs, RValue<UShort> rhs)
2317 {
2318 return RValue<UShort>(Nucleus::createAnd(lhs.value, rhs.value));
2319 }
2320
2321 RValue<UShort> operator|(RValue<UShort> lhs, RValue<UShort> rhs)
2322 {
2323 return RValue<UShort>(Nucleus::createOr(lhs.value, rhs.value));
2324 }
2325
2326 RValue<UShort> operator^(RValue<UShort> lhs, RValue<UShort> rhs)
2327 {
2328 return RValue<UShort>(Nucleus::createXor(lhs.value, rhs.value));
2329 }
2330
2331 RValue<UShort> operator<<(RValue<UShort> lhs, RValue<UShort> rhs)
2332 {
2333 return RValue<UShort>(Nucleus::createShl(lhs.value, rhs.value));
2334 }
2335
2336 RValue<UShort> operator>>(RValue<UShort> lhs, RValue<UShort> rhs)
2337 {
2338 return RValue<UShort>(Nucleus::createLShr(lhs.value, rhs.value));
2339 }
2340
Nicolas Capens96d4e092016-11-18 14:22:38 -05002341 RValue<UShort> operator+=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002342 {
2343 return lhs = lhs + rhs;
2344 }
2345
Nicolas Capens96d4e092016-11-18 14:22:38 -05002346 RValue<UShort> operator-=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002347 {
2348 return lhs = lhs - rhs;
2349 }
2350
Nicolas Capens96d4e092016-11-18 14:22:38 -05002351 RValue<UShort> operator*=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002352 {
2353 return lhs = lhs * rhs;
2354 }
2355
Nicolas Capens96d4e092016-11-18 14:22:38 -05002356 RValue<UShort> operator/=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002357 {
2358 return lhs = lhs / rhs;
2359 }
2360
Nicolas Capens96d4e092016-11-18 14:22:38 -05002361 RValue<UShort> operator%=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002362 {
2363 return lhs = lhs % rhs;
2364 }
2365
Nicolas Capens96d4e092016-11-18 14:22:38 -05002366 RValue<UShort> operator&=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002367 {
2368 return lhs = lhs & rhs;
2369 }
2370
Nicolas Capens96d4e092016-11-18 14:22:38 -05002371 RValue<UShort> operator|=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002372 {
2373 return lhs = lhs | rhs;
2374 }
2375
Nicolas Capens96d4e092016-11-18 14:22:38 -05002376 RValue<UShort> operator^=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002377 {
2378 return lhs = lhs ^ rhs;
2379 }
2380
Nicolas Capens96d4e092016-11-18 14:22:38 -05002381 RValue<UShort> operator<<=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002382 {
2383 return lhs = lhs << rhs;
2384 }
2385
Nicolas Capens96d4e092016-11-18 14:22:38 -05002386 RValue<UShort> operator>>=(UShort &lhs, RValue<UShort> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002387 {
2388 return lhs = lhs >> rhs;
2389 }
2390
2391 RValue<UShort> operator+(RValue<UShort> val)
2392 {
2393 return val;
2394 }
2395
2396 RValue<UShort> operator-(RValue<UShort> val)
2397 {
2398 return RValue<UShort>(Nucleus::createNeg(val.value));
2399 }
2400
2401 RValue<UShort> operator~(RValue<UShort> val)
2402 {
2403 return RValue<UShort>(Nucleus::createNot(val.value));
2404 }
2405
Nicolas Capens96d4e092016-11-18 14:22:38 -05002406 RValue<UShort> operator++(UShort &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002407 {
2408 RValue<UShort> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002409 val += UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002410 return res;
2411 }
2412
Nicolas Capens96d4e092016-11-18 14:22:38 -05002413 const UShort &operator++(UShort &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002414 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002415 val += UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002416 return val;
2417 }
2418
Nicolas Capens96d4e092016-11-18 14:22:38 -05002419 RValue<UShort> operator--(UShort &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002420 {
2421 RValue<UShort> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002422 val -= UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002423 return res;
2424 }
2425
Nicolas Capens96d4e092016-11-18 14:22:38 -05002426 const UShort &operator--(UShort &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002427 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002428 val -= UShort(1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002429 return val;
2430 }
2431
2432 RValue<Bool> operator<(RValue<UShort> lhs, RValue<UShort> rhs)
2433 {
2434 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
2435 }
2436
2437 RValue<Bool> operator<=(RValue<UShort> lhs, RValue<UShort> rhs)
2438 {
2439 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
2440 }
2441
2442 RValue<Bool> operator>(RValue<UShort> lhs, RValue<UShort> rhs)
2443 {
2444 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
2445 }
2446
2447 RValue<Bool> operator>=(RValue<UShort> lhs, RValue<UShort> rhs)
2448 {
2449 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
2450 }
2451
2452 RValue<Bool> operator!=(RValue<UShort> lhs, RValue<UShort> rhs)
2453 {
2454 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
2455 }
2456
2457 RValue<Bool> operator==(RValue<UShort> lhs, RValue<UShort> rhs)
2458 {
2459 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
2460 }
2461
2462 Type *UShort::getType()
2463 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002464 return T(Ice::IceType_i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002465 }
2466
Nicolas Capens16b5f152016-10-13 13:39:01 -04002467 Byte4::Byte4(RValue<Byte8> cast)
2468 {
Nicolas Capens16b5f152016-10-13 13:39:01 -04002469 storeValue(Nucleus::createBitCast(cast.value, getType()));
2470 }
2471
2472 Byte4::Byte4(const Reference<Byte4> &rhs)
2473 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002474 Value *value = rhs.loadValue();
2475 storeValue(value);
Nicolas Capens16b5f152016-10-13 13:39:01 -04002476 }
2477
Nicolas Capens598f8d82016-09-26 15:09:10 -04002478 Type *Byte4::getType()
2479 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002480 return T(Type_v4i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002481 }
2482
2483 Type *SByte4::getType()
2484 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002485 return T(Type_v4i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002486 }
2487
Nicolas Capens598f8d82016-09-26 15:09:10 -04002488 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)
2489 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04002490 int64_t constantVector[8] = {x0, x1, x2, x3, x4, x5, x6, x7};
2491 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002492 }
2493
2494 Byte8::Byte8(RValue<Byte8> rhs)
2495 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002496 storeValue(rhs.value);
2497 }
2498
2499 Byte8::Byte8(const Byte8 &rhs)
2500 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002501 Value *value = rhs.loadValue();
2502 storeValue(value);
2503 }
2504
2505 Byte8::Byte8(const Reference<Byte8> &rhs)
2506 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002507 Value *value = rhs.loadValue();
2508 storeValue(value);
2509 }
2510
Nicolas Capens96d4e092016-11-18 14:22:38 -05002511 RValue<Byte8> Byte8::operator=(RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002512 {
2513 storeValue(rhs.value);
2514
2515 return rhs;
2516 }
2517
Nicolas Capens96d4e092016-11-18 14:22:38 -05002518 RValue<Byte8> Byte8::operator=(const Byte8 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002519 {
2520 Value *value = rhs.loadValue();
2521 storeValue(value);
2522
2523 return RValue<Byte8>(value);
2524 }
2525
Nicolas Capens96d4e092016-11-18 14:22:38 -05002526 RValue<Byte8> Byte8::operator=(const Reference<Byte8> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002527 {
2528 Value *value = rhs.loadValue();
2529 storeValue(value);
2530
2531 return RValue<Byte8>(value);
2532 }
2533
2534 RValue<Byte8> operator+(RValue<Byte8> lhs, RValue<Byte8> rhs)
2535 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002536 return RValue<Byte8>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002537 }
2538
2539 RValue<Byte8> operator-(RValue<Byte8> lhs, RValue<Byte8> rhs)
2540 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002541 return RValue<Byte8>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002542 }
2543
2544// RValue<Byte8> operator*(RValue<Byte8> lhs, RValue<Byte8> rhs)
2545// {
2546// return RValue<Byte8>(Nucleus::createMul(lhs.value, rhs.value));
2547// }
2548
2549// RValue<Byte8> operator/(RValue<Byte8> lhs, RValue<Byte8> rhs)
2550// {
2551// return RValue<Byte8>(Nucleus::createUDiv(lhs.value, rhs.value));
2552// }
2553
2554// RValue<Byte8> operator%(RValue<Byte8> lhs, RValue<Byte8> rhs)
2555// {
2556// return RValue<Byte8>(Nucleus::createURem(lhs.value, rhs.value));
2557// }
2558
2559 RValue<Byte8> operator&(RValue<Byte8> lhs, RValue<Byte8> rhs)
2560 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002561 return RValue<Byte8>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002562 }
2563
2564 RValue<Byte8> operator|(RValue<Byte8> lhs, RValue<Byte8> rhs)
2565 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002566 return RValue<Byte8>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002567 }
2568
2569 RValue<Byte8> operator^(RValue<Byte8> lhs, RValue<Byte8> rhs)
2570 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002571 return RValue<Byte8>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002572 }
2573
2574// RValue<Byte8> operator<<(RValue<Byte8> lhs, unsigned char rhs)
2575// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002576// return RValue<Byte8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002577// }
2578
2579// RValue<Byte8> operator>>(RValue<Byte8> lhs, unsigned char rhs)
2580// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002581// return RValue<Byte8>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002582// }
2583
Nicolas Capens96d4e092016-11-18 14:22:38 -05002584 RValue<Byte8> operator+=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002585 {
2586 return lhs = lhs + rhs;
2587 }
2588
Nicolas Capens96d4e092016-11-18 14:22:38 -05002589 RValue<Byte8> operator-=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002590 {
2591 return lhs = lhs - rhs;
2592 }
2593
Nicolas Capens96d4e092016-11-18 14:22:38 -05002594// RValue<Byte8> operator*=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002595// {
2596// return lhs = lhs * rhs;
2597// }
2598
Nicolas Capens96d4e092016-11-18 14:22:38 -05002599// RValue<Byte8> operator/=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002600// {
2601// return lhs = lhs / rhs;
2602// }
2603
Nicolas Capens96d4e092016-11-18 14:22:38 -05002604// RValue<Byte8> operator%=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002605// {
2606// return lhs = lhs % rhs;
2607// }
2608
Nicolas Capens96d4e092016-11-18 14:22:38 -05002609 RValue<Byte8> operator&=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002610 {
2611 return lhs = lhs & rhs;
2612 }
2613
Nicolas Capens96d4e092016-11-18 14:22:38 -05002614 RValue<Byte8> operator|=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002615 {
2616 return lhs = lhs | rhs;
2617 }
2618
Nicolas Capens96d4e092016-11-18 14:22:38 -05002619 RValue<Byte8> operator^=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002620 {
2621 return lhs = lhs ^ rhs;
2622 }
2623
Nicolas Capens96d4e092016-11-18 14:22:38 -05002624// RValue<Byte8> operator<<=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002625// {
2626// return lhs = lhs << rhs;
2627// }
2628
Nicolas Capens96d4e092016-11-18 14:22:38 -05002629// RValue<Byte8> operator>>=(Byte8 &lhs, RValue<Byte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002630// {
2631// return lhs = lhs >> rhs;
2632// }
2633
2634// RValue<Byte8> operator+(RValue<Byte8> val)
2635// {
2636// return val;
2637// }
2638
2639// RValue<Byte8> operator-(RValue<Byte8> val)
2640// {
2641// return RValue<Byte8>(Nucleus::createNeg(val.value));
2642// }
2643
2644 RValue<Byte8> operator~(RValue<Byte8> val)
2645 {
2646 return RValue<Byte8>(Nucleus::createNot(val.value));
2647 }
2648
2649 RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y)
2650 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002651 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2652 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2653 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2654 auto paddusb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2655 paddusb->addArg(x.value);
2656 paddusb->addArg(y.value);
2657 ::basicBlock->appendInst(paddusb);
2658
2659 return RValue<Byte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002660 }
2661
2662 RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y)
2663 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002664 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2665 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2666 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2667 auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2668 psubusw->addArg(x.value);
2669 psubusw->addArg(y.value);
2670 ::basicBlock->appendInst(psubusw);
2671
2672 return RValue<Byte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002673 }
2674
2675 RValue<Short4> Unpack(RValue<Byte4> x)
2676 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04002677 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 -04002678 return As<Short4>(Nucleus::createShuffleVector(x.value, x.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002679 }
2680
Nicolas Capens411273e2017-01-26 15:13:36 -08002681 RValue<Short4> Unpack(RValue<Byte4> x, RValue<Byte4> y)
2682 {
2683 return UnpackLow(As<Byte8>(x), As<Byte8>(y));
2684 }
2685
Nicolas Capens598f8d82016-09-26 15:09:10 -04002686 RValue<Short4> UnpackLow(RValue<Byte8> x, RValue<Byte8> y)
2687 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04002688 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 -04002689 return As<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002690 }
2691
2692 RValue<Short4> UnpackHigh(RValue<Byte8> x, RValue<Byte8> y)
2693 {
Nicolas Capens20e22c42016-10-25 17:32:37 -04002694 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
2695 auto lowHigh = RValue<Byte16>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
2696 return As<Short4>(Swizzle(As<Int4>(lowHigh), 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002697 }
2698
2699 RValue<Int> SignMask(RValue<Byte8> x)
2700 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002701 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
2702 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2703 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2704 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
2705 movmsk->addArg(x.value);
2706 ::basicBlock->appendInst(movmsk);
2707
2708 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002709 }
2710
2711// RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y)
2712// {
Nicolas Capens2f970b62016-11-08 14:28:59 -05002713// return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002714// }
2715
2716 RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y)
2717 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002718 return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002719 }
2720
2721 Type *Byte8::getType()
2722 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002723 return T(Type_v8i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002724 }
2725
Nicolas Capens598f8d82016-09-26 15:09:10 -04002726 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)
2727 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04002728 int64_t constantVector[8] = { x0, x1, x2, x3, x4, x5, x6, x7 };
2729 Value *vector = V(Nucleus::createConstantVector(constantVector, getType()));
2730
2731 storeValue(Nucleus::createBitCast(vector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002732 }
2733
Nicolas Capens598f8d82016-09-26 15:09:10 -04002734 SByte8::SByte8(RValue<SByte8> rhs)
2735 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002736 storeValue(rhs.value);
2737 }
2738
2739 SByte8::SByte8(const SByte8 &rhs)
2740 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002741 Value *value = rhs.loadValue();
2742 storeValue(value);
2743 }
2744
2745 SByte8::SByte8(const Reference<SByte8> &rhs)
2746 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002747 Value *value = rhs.loadValue();
2748 storeValue(value);
2749 }
2750
Nicolas Capens96d4e092016-11-18 14:22:38 -05002751 RValue<SByte8> SByte8::operator=(RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002752 {
2753 storeValue(rhs.value);
2754
2755 return rhs;
2756 }
2757
Nicolas Capens96d4e092016-11-18 14:22:38 -05002758 RValue<SByte8> SByte8::operator=(const SByte8 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002759 {
2760 Value *value = rhs.loadValue();
2761 storeValue(value);
2762
2763 return RValue<SByte8>(value);
2764 }
2765
Nicolas Capens96d4e092016-11-18 14:22:38 -05002766 RValue<SByte8> SByte8::operator=(const Reference<SByte8> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002767 {
2768 Value *value = rhs.loadValue();
2769 storeValue(value);
2770
2771 return RValue<SByte8>(value);
2772 }
2773
2774 RValue<SByte8> operator+(RValue<SByte8> lhs, RValue<SByte8> rhs)
2775 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002776 return RValue<SByte8>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002777 }
2778
2779 RValue<SByte8> operator-(RValue<SByte8> lhs, RValue<SByte8> rhs)
2780 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04002781 return RValue<SByte8>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002782 }
2783
2784// RValue<SByte8> operator*(RValue<SByte8> lhs, RValue<SByte8> rhs)
2785// {
2786// return RValue<SByte8>(Nucleus::createMul(lhs.value, rhs.value));
2787// }
2788
2789// RValue<SByte8> operator/(RValue<SByte8> lhs, RValue<SByte8> rhs)
2790// {
2791// return RValue<SByte8>(Nucleus::createSDiv(lhs.value, rhs.value));
2792// }
2793
2794// RValue<SByte8> operator%(RValue<SByte8> lhs, RValue<SByte8> rhs)
2795// {
2796// return RValue<SByte8>(Nucleus::createSRem(lhs.value, rhs.value));
2797// }
2798
2799 RValue<SByte8> operator&(RValue<SByte8> lhs, RValue<SByte8> rhs)
2800 {
2801 return RValue<SByte8>(Nucleus::createAnd(lhs.value, rhs.value));
2802 }
2803
2804 RValue<SByte8> operator|(RValue<SByte8> lhs, RValue<SByte8> rhs)
2805 {
2806 return RValue<SByte8>(Nucleus::createOr(lhs.value, rhs.value));
2807 }
2808
2809 RValue<SByte8> operator^(RValue<SByte8> lhs, RValue<SByte8> rhs)
2810 {
2811 return RValue<SByte8>(Nucleus::createXor(lhs.value, rhs.value));
2812 }
2813
2814// RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs)
2815// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002816// return RValue<SByte8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002817// }
2818
2819// RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs)
2820// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05002821// return RValue<SByte8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002822// }
2823
Nicolas Capens96d4e092016-11-18 14:22:38 -05002824 RValue<SByte8> operator+=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002825 {
2826 return lhs = lhs + rhs;
2827 }
2828
Nicolas Capens96d4e092016-11-18 14:22:38 -05002829 RValue<SByte8> operator-=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002830 {
2831 return lhs = lhs - rhs;
2832 }
2833
Nicolas Capens96d4e092016-11-18 14:22:38 -05002834// RValue<SByte8> operator*=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002835// {
2836// return lhs = lhs * rhs;
2837// }
2838
Nicolas Capens96d4e092016-11-18 14:22:38 -05002839// RValue<SByte8> operator/=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002840// {
2841// return lhs = lhs / rhs;
2842// }
2843
Nicolas Capens96d4e092016-11-18 14:22:38 -05002844// RValue<SByte8> operator%=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002845// {
2846// return lhs = lhs % rhs;
2847// }
2848
Nicolas Capens96d4e092016-11-18 14:22:38 -05002849 RValue<SByte8> operator&=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002850 {
2851 return lhs = lhs & rhs;
2852 }
2853
Nicolas Capens96d4e092016-11-18 14:22:38 -05002854 RValue<SByte8> operator|=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002855 {
2856 return lhs = lhs | rhs;
2857 }
2858
Nicolas Capens96d4e092016-11-18 14:22:38 -05002859 RValue<SByte8> operator^=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002860 {
2861 return lhs = lhs ^ rhs;
2862 }
2863
Nicolas Capens96d4e092016-11-18 14:22:38 -05002864// RValue<SByte8> operator<<=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002865// {
2866// return lhs = lhs << rhs;
2867// }
2868
Nicolas Capens96d4e092016-11-18 14:22:38 -05002869// RValue<SByte8> operator>>=(SByte8 &lhs, RValue<SByte8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002870// {
2871// return lhs = lhs >> rhs;
2872// }
2873
2874// RValue<SByte8> operator+(RValue<SByte8> val)
2875// {
2876// return val;
2877// }
2878
2879// RValue<SByte8> operator-(RValue<SByte8> val)
2880// {
2881// return RValue<SByte8>(Nucleus::createNeg(val.value));
2882// }
2883
2884 RValue<SByte8> operator~(RValue<SByte8> val)
2885 {
2886 return RValue<SByte8>(Nucleus::createNot(val.value));
2887 }
2888
2889 RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y)
2890 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002891 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2892 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2893 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2894 auto paddsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2895 paddsb->addArg(x.value);
2896 paddsb->addArg(y.value);
2897 ::basicBlock->appendInst(paddsb);
2898
2899 return RValue<SByte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002900 }
2901
2902 RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y)
2903 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05002904 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2905 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2906 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2907 auto psubsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2908 psubsb->addArg(x.value);
2909 psubsb->addArg(y.value);
2910 ::basicBlock->appendInst(psubsb);
2911
2912 return RValue<SByte8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002913 }
2914
2915 RValue<Short4> UnpackLow(RValue<SByte8> x, RValue<SByte8> y)
2916 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04002917 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 -04002918 return As<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002919 }
2920
2921 RValue<Short4> UnpackHigh(RValue<SByte8> x, RValue<SByte8> y)
2922 {
Nicolas Capens20e22c42016-10-25 17:32:37 -04002923 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8
2924 auto lowHigh = RValue<Byte16>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
2925 return As<Short4>(Swizzle(As<Int4>(lowHigh), 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002926 }
2927
2928 RValue<Int> SignMask(RValue<SByte8> x)
2929 {
Nicolas Capensf2cb9df2016-10-21 17:26:13 -04002930 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
2931 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2932 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2933 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
2934 movmsk->addArg(x.value);
2935 ::basicBlock->appendInst(movmsk);
2936
2937 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002938 }
2939
2940 RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y)
2941 {
Nicolas Capens2f970b62016-11-08 14:28:59 -05002942 return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002943 }
2944
2945 RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y)
2946 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002947 return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002948 }
2949
2950 Type *SByte8::getType()
2951 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002952 return T(Type_v8i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002953 }
2954
2955 Byte16::Byte16(RValue<Byte16> rhs)
2956 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002957 storeValue(rhs.value);
2958 }
2959
2960 Byte16::Byte16(const Byte16 &rhs)
2961 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002962 Value *value = rhs.loadValue();
2963 storeValue(value);
2964 }
2965
2966 Byte16::Byte16(const Reference<Byte16> &rhs)
2967 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04002968 Value *value = rhs.loadValue();
2969 storeValue(value);
2970 }
2971
Nicolas Capens96d4e092016-11-18 14:22:38 -05002972 RValue<Byte16> Byte16::operator=(RValue<Byte16> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002973 {
2974 storeValue(rhs.value);
2975
2976 return rhs;
2977 }
2978
Nicolas Capens96d4e092016-11-18 14:22:38 -05002979 RValue<Byte16> Byte16::operator=(const Byte16 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002980 {
2981 Value *value = rhs.loadValue();
2982 storeValue(value);
2983
2984 return RValue<Byte16>(value);
2985 }
2986
Nicolas Capens96d4e092016-11-18 14:22:38 -05002987 RValue<Byte16> Byte16::operator=(const Reference<Byte16> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002988 {
2989 Value *value = rhs.loadValue();
2990 storeValue(value);
2991
2992 return RValue<Byte16>(value);
2993 }
2994
2995 Type *Byte16::getType()
2996 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002997 return T(Ice::IceType_v16i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002998 }
2999
3000 Type *SByte16::getType()
3001 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003002 return T(Ice::IceType_v16i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003003 }
3004
Nicolas Capens16b5f152016-10-13 13:39:01 -04003005 Short2::Short2(RValue<Short4> cast)
3006 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05003007 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003008 }
3009
3010 Type *Short2::getType()
3011 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003012 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04003013 }
3014
3015 UShort2::UShort2(RValue<UShort4> cast)
3016 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05003017 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003018 }
3019
3020 Type *UShort2::getType()
3021 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003022 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04003023 }
3024
Nicolas Capens598f8d82016-09-26 15:09:10 -04003025 Short4::Short4(RValue<Int> cast)
3026 {
Nicolas Capensd4227962016-11-09 14:24:25 -05003027 Value *vector = loadValue();
Nicolas Capensbf22bbf2017-01-13 17:37:45 -05003028 Value *element = Nucleus::createTrunc(cast.value, Short::getType());
3029 Value *insert = Nucleus::createInsertElement(vector, element, 0);
Nicolas Capensd4227962016-11-09 14:24:25 -05003030 Value *swizzle = Swizzle(RValue<Short4>(insert), 0x00).value;
Nicolas Capens598f8d82016-09-26 15:09:10 -04003031
3032 storeValue(swizzle);
3033 }
3034
3035 Short4::Short4(RValue<Int4> cast)
3036 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08003037 int select[8] = {0, 2, 4, 6, 0, 2, 4, 6};
3038 Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType());
3039 Value *packed = Nucleus::createShuffleVector(short8, short8, select);
Nicolas Capensd4227962016-11-09 14:24:25 -05003040
Nicolas Capensbea4dce2017-07-24 16:54:44 -04003041 Value *int2 = RValue<Int2>(Int2(As<Int4>(packed))).value;
Nicolas Capensd4227962016-11-09 14:24:25 -05003042 Value *short4 = Nucleus::createBitCast(int2, Short4::getType());
3043
3044 storeValue(short4);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003045 }
3046
3047// Short4::Short4(RValue<Float> cast)
3048// {
3049// }
3050
3051 Short4::Short4(RValue<Float4> cast)
3052 {
3053 assert(false && "UNIMPLEMENTED");
3054 }
3055
Nicolas Capens598f8d82016-09-26 15:09:10 -04003056 Short4::Short4(short xyzw)
3057 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003058 int64_t constantVector[4] = {xyzw, xyzw, xyzw, xyzw};
3059 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003060 }
3061
3062 Short4::Short4(short x, short y, short z, short w)
3063 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003064 int64_t constantVector[4] = {x, y, z, w};
3065 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003066 }
3067
3068 Short4::Short4(RValue<Short4> rhs)
3069 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003070 storeValue(rhs.value);
3071 }
3072
3073 Short4::Short4(const Short4 &rhs)
3074 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003075 Value *value = rhs.loadValue();
3076 storeValue(value);
3077 }
3078
3079 Short4::Short4(const Reference<Short4> &rhs)
3080 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003081 Value *value = rhs.loadValue();
3082 storeValue(value);
3083 }
3084
3085 Short4::Short4(RValue<UShort4> rhs)
3086 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003087 storeValue(rhs.value);
3088 }
3089
3090 Short4::Short4(const UShort4 &rhs)
3091 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003092 storeValue(rhs.loadValue());
3093 }
3094
3095 Short4::Short4(const Reference<UShort4> &rhs)
3096 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003097 storeValue(rhs.loadValue());
3098 }
3099
Nicolas Capens96d4e092016-11-18 14:22:38 -05003100 RValue<Short4> Short4::operator=(RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003101 {
3102 storeValue(rhs.value);
3103
3104 return rhs;
3105 }
3106
Nicolas Capens96d4e092016-11-18 14:22:38 -05003107 RValue<Short4> Short4::operator=(const Short4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003108 {
3109 Value *value = rhs.loadValue();
3110 storeValue(value);
3111
3112 return RValue<Short4>(value);
3113 }
3114
Nicolas Capens96d4e092016-11-18 14:22:38 -05003115 RValue<Short4> Short4::operator=(const Reference<Short4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003116 {
3117 Value *value = rhs.loadValue();
3118 storeValue(value);
3119
3120 return RValue<Short4>(value);
3121 }
3122
Nicolas Capens96d4e092016-11-18 14:22:38 -05003123 RValue<Short4> Short4::operator=(RValue<UShort4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003124 {
3125 storeValue(rhs.value);
3126
3127 return RValue<Short4>(rhs);
3128 }
3129
Nicolas Capens96d4e092016-11-18 14:22:38 -05003130 RValue<Short4> Short4::operator=(const UShort4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003131 {
3132 Value *value = rhs.loadValue();
3133 storeValue(value);
3134
3135 return RValue<Short4>(value);
3136 }
3137
Nicolas Capens96d4e092016-11-18 14:22:38 -05003138 RValue<Short4> Short4::operator=(const Reference<UShort4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003139 {
3140 Value *value = rhs.loadValue();
3141 storeValue(value);
3142
3143 return RValue<Short4>(value);
3144 }
3145
3146 RValue<Short4> operator+(RValue<Short4> lhs, RValue<Short4> rhs)
3147 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003148 return RValue<Short4>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003149 }
3150
3151 RValue<Short4> operator-(RValue<Short4> lhs, RValue<Short4> rhs)
3152 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003153 return RValue<Short4>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003154 }
3155
3156 RValue<Short4> operator*(RValue<Short4> lhs, RValue<Short4> rhs)
3157 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003158 return RValue<Short4>(Nucleus::createMul(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003159 }
3160
3161// RValue<Short4> operator/(RValue<Short4> lhs, RValue<Short4> rhs)
3162// {
3163// return RValue<Short4>(Nucleus::createSDiv(lhs.value, rhs.value));
3164// }
3165
3166// RValue<Short4> operator%(RValue<Short4> lhs, RValue<Short4> rhs)
3167// {
3168// return RValue<Short4>(Nucleus::createSRem(lhs.value, rhs.value));
3169// }
3170
3171 RValue<Short4> operator&(RValue<Short4> lhs, RValue<Short4> rhs)
3172 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003173 return RValue<Short4>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003174 }
3175
3176 RValue<Short4> operator|(RValue<Short4> lhs, RValue<Short4> rhs)
3177 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003178 return RValue<Short4>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003179 }
3180
3181 RValue<Short4> operator^(RValue<Short4> lhs, RValue<Short4> rhs)
3182 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003183 return RValue<Short4>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003184 }
3185
3186 RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs)
3187 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003188 return RValue<Short4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003189 }
3190
3191 RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs)
3192 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003193 return RValue<Short4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003194 }
3195
Nicolas Capens96d4e092016-11-18 14:22:38 -05003196 RValue<Short4> operator+=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003197 {
3198 return lhs = lhs + rhs;
3199 }
3200
Nicolas Capens96d4e092016-11-18 14:22:38 -05003201 RValue<Short4> operator-=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003202 {
3203 return lhs = lhs - rhs;
3204 }
3205
Nicolas Capens96d4e092016-11-18 14:22:38 -05003206 RValue<Short4> operator*=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003207 {
3208 return lhs = lhs * rhs;
3209 }
3210
Nicolas Capens96d4e092016-11-18 14:22:38 -05003211// RValue<Short4> operator/=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003212// {
3213// return lhs = lhs / rhs;
3214// }
3215
Nicolas Capens96d4e092016-11-18 14:22:38 -05003216// RValue<Short4> operator%=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003217// {
3218// return lhs = lhs % rhs;
3219// }
3220
Nicolas Capens96d4e092016-11-18 14:22:38 -05003221 RValue<Short4> operator&=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003222 {
3223 return lhs = lhs & rhs;
3224 }
3225
Nicolas Capens96d4e092016-11-18 14:22:38 -05003226 RValue<Short4> operator|=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003227 {
3228 return lhs = lhs | rhs;
3229 }
3230
Nicolas Capens96d4e092016-11-18 14:22:38 -05003231 RValue<Short4> operator^=(Short4 &lhs, RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003232 {
3233 return lhs = lhs ^ rhs;
3234 }
3235
Nicolas Capens96d4e092016-11-18 14:22:38 -05003236 RValue<Short4> operator<<=(Short4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003237 {
3238 return lhs = lhs << rhs;
3239 }
3240
Nicolas Capens96d4e092016-11-18 14:22:38 -05003241 RValue<Short4> operator>>=(Short4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003242 {
3243 return lhs = lhs >> rhs;
3244 }
3245
Nicolas Capens598f8d82016-09-26 15:09:10 -04003246// RValue<Short4> operator+(RValue<Short4> val)
3247// {
3248// return val;
3249// }
3250
3251 RValue<Short4> operator-(RValue<Short4> val)
3252 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05003253 return RValue<Short4>(Nucleus::createNeg(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003254 }
3255
3256 RValue<Short4> operator~(RValue<Short4> val)
3257 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05003258 return RValue<Short4>(Nucleus::createNot(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003259 }
3260
3261 RValue<Short4> RoundShort4(RValue<Float4> cast)
3262 {
Nicolas Capensd4227962016-11-09 14:24:25 -05003263 RValue<Int4> int4 = RoundInt(cast);
3264 return As<Short4>(Pack(int4, int4));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003265 }
3266
3267 RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y)
3268 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003269 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3270 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value, y.value);
3271 ::basicBlock->appendInst(cmp);
3272
3273 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3274 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3275 ::basicBlock->appendInst(select);
3276
3277 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003278 }
3279
3280 RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y)
3281 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003282 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3283 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value);
3284 ::basicBlock->appendInst(cmp);
3285
3286 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3287 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3288 ::basicBlock->appendInst(select);
3289
3290 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003291 }
3292
3293 RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y)
3294 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003295 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3296 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3297 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3298 auto paddsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3299 paddsw->addArg(x.value);
3300 paddsw->addArg(y.value);
3301 ::basicBlock->appendInst(paddsw);
3302
3303 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003304 }
3305
3306 RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y)
3307 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003308 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3309 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3310 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3311 auto psubsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3312 psubsw->addArg(x.value);
3313 psubsw->addArg(y.value);
3314 ::basicBlock->appendInst(psubsw);
3315
3316 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003317 }
3318
3319 RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y)
3320 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003321 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3322 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3323 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3324 auto pmulhw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3325 pmulhw->addArg(x.value);
3326 pmulhw->addArg(y.value);
3327 ::basicBlock->appendInst(pmulhw);
3328
Nicolas Capens5b41ba32016-12-08 14:34:00 -05003329 return RValue<Short4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003330 }
3331
3332 RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y)
3333 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003334 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3335 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyAddPairs, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3336 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3337 auto pmaddwd = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3338 pmaddwd->addArg(x.value);
3339 pmaddwd->addArg(y.value);
3340 ::basicBlock->appendInst(pmaddwd);
3341
Nicolas Capensbea4dce2017-07-24 16:54:44 -04003342 return As<Int2>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003343 }
3344
3345 RValue<SByte8> Pack(RValue<Short4> x, RValue<Short4> y)
3346 {
Nicolas Capensec54a172016-10-25 17:32:37 -04003347 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
3348 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3349 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3350 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3351 pack->addArg(x.value);
3352 pack->addArg(y.value);
3353 ::basicBlock->appendInst(pack);
3354
Nicolas Capens70dfff42016-10-27 10:20:28 -04003355 return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x88));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003356 }
3357
3358 RValue<Int2> UnpackLow(RValue<Short4> x, RValue<Short4> y)
3359 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04003360 int shuffle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; // Real type is v8i16
Nicolas Capensbea4dce2017-07-24 16:54:44 -04003361 return As<Int2>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003362 }
3363
3364 RValue<Int2> UnpackHigh(RValue<Short4> x, RValue<Short4> y)
3365 {
Nicolas Capens20e22c42016-10-25 17:32:37 -04003366 int shuffle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; // Real type is v8i16
3367 auto lowHigh = RValue<Short8>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
3368 return As<Int2>(Swizzle(As<Int4>(lowHigh), 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003369 }
3370
3371 RValue<Short4> Swizzle(RValue<Short4> x, unsigned char select)
3372 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04003373 // Real type is v8i16
3374 int shuffle[8] =
3375 {
3376 (select >> 0) & 0x03,
3377 (select >> 2) & 0x03,
3378 (select >> 4) & 0x03,
3379 (select >> 6) & 0x03,
3380 (select >> 0) & 0x03,
3381 (select >> 2) & 0x03,
3382 (select >> 4) & 0x03,
3383 (select >> 6) & 0x03,
3384 };
3385
3386 return RValue<Short4>(Nucleus::createShuffleVector(x.value, x.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003387 }
3388
3389 RValue<Short4> Insert(RValue<Short4> val, RValue<Short> element, int i)
3390 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05003391 return RValue<Short4>(Nucleus::createInsertElement(val.value, element.value, i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003392 }
3393
3394 RValue<Short> Extract(RValue<Short4> val, int i)
3395 {
Nicolas Capens0133d0f2017-01-16 16:25:08 -05003396 return RValue<Short>(Nucleus::createExtractElement(val.value, Short::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003397 }
3398
3399 RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y)
3400 {
Nicolas Capens2f970b62016-11-08 14:28:59 -05003401 return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003402 }
3403
3404 RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y)
3405 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003406 return RValue<Short4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003407 }
3408
3409 Type *Short4::getType()
3410 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003411 return T(Type_v4i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003412 }
3413
3414 UShort4::UShort4(RValue<Int4> cast)
3415 {
3416 *this = Short4(cast);
3417 }
3418
3419 UShort4::UShort4(RValue<Float4> cast, bool saturate)
3420 {
Nicolas Capensd4227962016-11-09 14:24:25 -05003421 if(saturate)
3422 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003423 if(CPUID::SSE4_1)
Nicolas Capensd4227962016-11-09 14:24:25 -05003424 {
3425 Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation
3426 *this = As<Short4>(Pack(As<UInt4>(int4), As<UInt4>(int4)));
3427 }
3428 else
3429 {
3430 *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000))));
3431 }
3432 }
3433 else
3434 {
3435 *this = Short4(Int4(cast));
3436 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04003437 }
3438
Nicolas Capens598f8d82016-09-26 15:09:10 -04003439 UShort4::UShort4(unsigned short xyzw)
3440 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003441 int64_t constantVector[4] = {xyzw, xyzw, xyzw, xyzw};
3442 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003443 }
3444
3445 UShort4::UShort4(unsigned short x, unsigned short y, unsigned short z, unsigned short w)
3446 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003447 int64_t constantVector[4] = {x, y, z, w};
3448 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003449 }
3450
3451 UShort4::UShort4(RValue<UShort4> rhs)
3452 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003453 storeValue(rhs.value);
3454 }
3455
3456 UShort4::UShort4(const UShort4 &rhs)
3457 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003458 Value *value = rhs.loadValue();
3459 storeValue(value);
3460 }
3461
3462 UShort4::UShort4(const Reference<UShort4> &rhs)
3463 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003464 Value *value = rhs.loadValue();
3465 storeValue(value);
3466 }
3467
3468 UShort4::UShort4(RValue<Short4> rhs)
3469 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003470 storeValue(rhs.value);
3471 }
3472
3473 UShort4::UShort4(const Short4 &rhs)
3474 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003475 Value *value = rhs.loadValue();
3476 storeValue(value);
3477 }
3478
3479 UShort4::UShort4(const Reference<Short4> &rhs)
3480 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003481 Value *value = rhs.loadValue();
3482 storeValue(value);
3483 }
3484
Nicolas Capens96d4e092016-11-18 14:22:38 -05003485 RValue<UShort4> UShort4::operator=(RValue<UShort4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003486 {
3487 storeValue(rhs.value);
3488
3489 return rhs;
3490 }
3491
Nicolas Capens96d4e092016-11-18 14:22:38 -05003492 RValue<UShort4> UShort4::operator=(const UShort4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003493 {
3494 Value *value = rhs.loadValue();
3495 storeValue(value);
3496
3497 return RValue<UShort4>(value);
3498 }
3499
Nicolas Capens96d4e092016-11-18 14:22:38 -05003500 RValue<UShort4> UShort4::operator=(const Reference<UShort4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003501 {
3502 Value *value = rhs.loadValue();
3503 storeValue(value);
3504
3505 return RValue<UShort4>(value);
3506 }
3507
Nicolas Capens96d4e092016-11-18 14:22:38 -05003508 RValue<UShort4> UShort4::operator=(RValue<Short4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003509 {
3510 storeValue(rhs.value);
3511
3512 return RValue<UShort4>(rhs);
3513 }
3514
Nicolas Capens96d4e092016-11-18 14:22:38 -05003515 RValue<UShort4> UShort4::operator=(const Short4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003516 {
3517 Value *value = rhs.loadValue();
3518 storeValue(value);
3519
3520 return RValue<UShort4>(value);
3521 }
3522
Nicolas Capens96d4e092016-11-18 14:22:38 -05003523 RValue<UShort4> UShort4::operator=(const Reference<Short4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003524 {
3525 Value *value = rhs.loadValue();
3526 storeValue(value);
3527
3528 return RValue<UShort4>(value);
3529 }
3530
3531 RValue<UShort4> operator+(RValue<UShort4> lhs, RValue<UShort4> rhs)
3532 {
Nicolas Capens5b41ba32016-12-08 14:34:00 -05003533 return RValue<UShort4>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003534 }
3535
3536 RValue<UShort4> operator-(RValue<UShort4> lhs, RValue<UShort4> rhs)
3537 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003538 return RValue<UShort4>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003539 }
3540
3541 RValue<UShort4> operator*(RValue<UShort4> lhs, RValue<UShort4> rhs)
3542 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003543 return RValue<UShort4>(Nucleus::createMul(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003544 }
3545
Nicolas Capens16b5f152016-10-13 13:39:01 -04003546 RValue<UShort4> operator&(RValue<UShort4> lhs, RValue<UShort4> rhs)
3547 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003548 return RValue<UShort4>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003549 }
3550
3551 RValue<UShort4> operator|(RValue<UShort4> lhs, RValue<UShort4> rhs)
3552 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003553 return RValue<UShort4>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003554 }
3555
3556 RValue<UShort4> operator^(RValue<UShort4> lhs, RValue<UShort4> rhs)
3557 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04003558 return RValue<UShort4>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens16b5f152016-10-13 13:39:01 -04003559 }
3560
Nicolas Capens598f8d82016-09-26 15:09:10 -04003561 RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs)
3562 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003563 return RValue<UShort4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003564 }
3565
3566 RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs)
3567 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003568 return RValue<UShort4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003569 }
3570
Nicolas Capens96d4e092016-11-18 14:22:38 -05003571 RValue<UShort4> operator<<=(UShort4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003572 {
3573 return lhs = lhs << rhs;
3574 }
3575
Nicolas Capens96d4e092016-11-18 14:22:38 -05003576 RValue<UShort4> operator>>=(UShort4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003577 {
3578 return lhs = lhs >> rhs;
3579 }
3580
Nicolas Capens598f8d82016-09-26 15:09:10 -04003581 RValue<UShort4> operator~(RValue<UShort4> val)
3582 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05003583 return RValue<UShort4>(Nucleus::createNot(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003584 }
3585
3586 RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y)
3587 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003588 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3589 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value, y.value);
3590 ::basicBlock->appendInst(cmp);
3591
3592 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3593 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3594 ::basicBlock->appendInst(select);
3595
3596 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003597 }
3598
3599 RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y)
3600 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003601 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3602 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value);
3603 ::basicBlock->appendInst(cmp);
3604
3605 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3606 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3607 ::basicBlock->appendInst(select);
3608
3609 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003610 }
3611
3612 RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y)
3613 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003614 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3615 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3616 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3617 auto paddusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3618 paddusw->addArg(x.value);
3619 paddusw->addArg(y.value);
3620 ::basicBlock->appendInst(paddusw);
3621
3622 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003623 }
3624
3625 RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y)
3626 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003627 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3628 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3629 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3630 auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3631 psubusw->addArg(x.value);
3632 psubusw->addArg(y.value);
3633 ::basicBlock->appendInst(psubusw);
3634
3635 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003636 }
3637
3638 RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y)
3639 {
Nicolas Capensc71bed22016-11-07 22:25:14 -05003640 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3641 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3642 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3643 auto pmulhuw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3644 pmulhuw->addArg(x.value);
3645 pmulhuw->addArg(y.value);
3646 ::basicBlock->appendInst(pmulhuw);
3647
3648 return RValue<UShort4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003649 }
3650
3651 RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)
3652 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003653 assert(false && "UNIMPLEMENTED"); return RValue<UShort4>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003654 }
3655
3656 RValue<Byte8> Pack(RValue<UShort4> x, RValue<UShort4> y)
3657 {
Nicolas Capensec54a172016-10-25 17:32:37 -04003658 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
3659 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3660 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3661 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3662 pack->addArg(x.value);
3663 pack->addArg(y.value);
3664 ::basicBlock->appendInst(pack);
3665
Nicolas Capens70dfff42016-10-27 10:20:28 -04003666 return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x88));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003667 }
3668
3669 Type *UShort4::getType()
3670 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04003671 return T(Type_v4i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003672 }
3673
Nicolas Capens3e7062b2017-01-17 14:01:33 -05003674 Short8::Short8(short c)
3675 {
3676 int64_t constantVector[8] = {c, c, c, c, c, c, c, c};
3677 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3678 }
3679
Nicolas Capens598f8d82016-09-26 15:09:10 -04003680 Short8::Short8(short c0, short c1, short c2, short c3, short c4, short c5, short c6, short c7)
3681 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003682 int64_t constantVector[8] = {c0, c1, c2, c3, c4, c5, c6, c7};
3683 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003684 }
3685
3686 Short8::Short8(RValue<Short8> rhs)
3687 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003688 storeValue(rhs.value);
3689 }
3690
3691 Short8::Short8(const Reference<Short8> &rhs)
3692 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003693 Value *value = rhs.loadValue();
3694 storeValue(value);
3695 }
3696
3697 Short8::Short8(RValue<Short4> lo, RValue<Short4> hi)
3698 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05003699 int shuffle[8] = {0, 1, 2, 3, 8, 9, 10, 11}; // Real type is v8i16
3700 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
3701
3702 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003703 }
3704
3705 RValue<Short8> operator+(RValue<Short8> lhs, RValue<Short8> rhs)
3706 {
3707 return RValue<Short8>(Nucleus::createAdd(lhs.value, rhs.value));
3708 }
3709
3710 RValue<Short8> operator&(RValue<Short8> lhs, RValue<Short8> rhs)
3711 {
3712 return RValue<Short8>(Nucleus::createAnd(lhs.value, rhs.value));
3713 }
3714
3715 RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs)
3716 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003717 return RValue<Short8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003718 }
3719
3720 RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs)
3721 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003722 return RValue<Short8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003723 }
3724
3725 RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)
3726 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003727 assert(false && "UNIMPLEMENTED"); return RValue<Int4>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003728 }
3729
3730 RValue<Int4> Abs(RValue<Int4> x)
3731 {
Nicolas Capens84272242016-11-09 13:31:06 -05003732 auto negative = x >> 31;
3733 return (x ^ negative) - negative;
Nicolas Capens598f8d82016-09-26 15:09:10 -04003734 }
3735
3736 RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)
3737 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003738 assert(false && "UNIMPLEMENTED"); return RValue<Short8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003739 }
3740
3741 Type *Short8::getType()
3742 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04003743 return T(Ice::IceType_v8i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003744 }
3745
Nicolas Capens3e7062b2017-01-17 14:01:33 -05003746 UShort8::UShort8(unsigned short c)
3747 {
3748 int64_t constantVector[8] = {c, c, c, c, c, c, c, c};
3749 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3750 }
3751
Nicolas Capens598f8d82016-09-26 15:09:10 -04003752 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)
3753 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04003754 int64_t constantVector[8] = {c0, c1, c2, c3, c4, c5, c6, c7};
3755 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003756 }
3757
3758 UShort8::UShort8(RValue<UShort8> rhs)
3759 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003760 storeValue(rhs.value);
3761 }
3762
3763 UShort8::UShort8(const Reference<UShort8> &rhs)
3764 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04003765 Value *value = rhs.loadValue();
3766 storeValue(value);
3767 }
3768
3769 UShort8::UShort8(RValue<UShort4> lo, RValue<UShort4> hi)
3770 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05003771 int shuffle[8] = {0, 1, 2, 3, 8, 9, 10, 11}; // Real type is v8i16
3772 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
3773
3774 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003775 }
3776
Nicolas Capens96d4e092016-11-18 14:22:38 -05003777 RValue<UShort8> UShort8::operator=(RValue<UShort8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003778 {
3779 storeValue(rhs.value);
3780
3781 return rhs;
3782 }
3783
Nicolas Capens96d4e092016-11-18 14:22:38 -05003784 RValue<UShort8> UShort8::operator=(const UShort8 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003785 {
3786 Value *value = rhs.loadValue();
3787 storeValue(value);
3788
3789 return RValue<UShort8>(value);
3790 }
3791
Nicolas Capens96d4e092016-11-18 14:22:38 -05003792 RValue<UShort8> UShort8::operator=(const Reference<UShort8> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003793 {
3794 Value *value = rhs.loadValue();
3795 storeValue(value);
3796
3797 return RValue<UShort8>(value);
3798 }
3799
3800 RValue<UShort8> operator&(RValue<UShort8> lhs, RValue<UShort8> rhs)
3801 {
3802 return RValue<UShort8>(Nucleus::createAnd(lhs.value, rhs.value));
3803 }
3804
3805 RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs)
3806 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003807 return RValue<UShort8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003808 }
3809
3810 RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs)
3811 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05003812 return RValue<UShort8>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003813 }
3814
3815 RValue<UShort8> operator+(RValue<UShort8> lhs, RValue<UShort8> rhs)
3816 {
3817 return RValue<UShort8>(Nucleus::createAdd(lhs.value, rhs.value));
3818 }
3819
3820 RValue<UShort8> operator*(RValue<UShort8> lhs, RValue<UShort8> rhs)
3821 {
3822 return RValue<UShort8>(Nucleus::createMul(lhs.value, rhs.value));
3823 }
3824
Nicolas Capens96d4e092016-11-18 14:22:38 -05003825 RValue<UShort8> operator+=(UShort8 &lhs, RValue<UShort8> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003826 {
3827 return lhs = lhs + rhs;
3828 }
3829
3830 RValue<UShort8> operator~(RValue<UShort8> val)
3831 {
3832 return RValue<UShort8>(Nucleus::createNot(val.value));
3833 }
3834
3835 RValue<UShort8> Swizzle(RValue<UShort8> x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7)
3836 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003837 assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003838 }
3839
3840 RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)
3841 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003842 assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003843 }
3844
3845 // FIXME: Implement as Shuffle(x, y, Select(i0, ..., i16)) and Shuffle(x, y, SELECT_PACK_REPEAT(element))
3846// RValue<UShort8> PackRepeat(RValue<Byte16> x, RValue<Byte16> y, int element)
3847// {
Nicolas Capensc37252c2016-09-28 16:11:54 -04003848// assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003849// }
3850
3851 Type *UShort8::getType()
3852 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04003853 return T(Ice::IceType_v8i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003854 }
3855
3856 Int::Int(Argument<Int> argument)
3857 {
3858 storeValue(argument.value);
3859 }
3860
3861 Int::Int(RValue<Byte> cast)
3862 {
3863 Value *integer = Nucleus::createZExt(cast.value, Int::getType());
3864
3865 storeValue(integer);
3866 }
3867
3868 Int::Int(RValue<SByte> cast)
3869 {
3870 Value *integer = Nucleus::createSExt(cast.value, Int::getType());
3871
3872 storeValue(integer);
3873 }
3874
3875 Int::Int(RValue<Short> cast)
3876 {
3877 Value *integer = Nucleus::createSExt(cast.value, Int::getType());
3878
3879 storeValue(integer);
3880 }
3881
3882 Int::Int(RValue<UShort> cast)
3883 {
3884 Value *integer = Nucleus::createZExt(cast.value, Int::getType());
3885
3886 storeValue(integer);
3887 }
3888
3889 Int::Int(RValue<Int2> cast)
3890 {
3891 *this = Extract(cast, 0);
3892 }
3893
3894 Int::Int(RValue<Long> cast)
3895 {
3896 Value *integer = Nucleus::createTrunc(cast.value, Int::getType());
3897
3898 storeValue(integer);
3899 }
3900
3901 Int::Int(RValue<Float> cast)
3902 {
3903 Value *integer = Nucleus::createFPToSI(cast.value, Int::getType());
3904
3905 storeValue(integer);
3906 }
3907
Nicolas Capens598f8d82016-09-26 15:09:10 -04003908 Int::Int(int x)
3909 {
3910 storeValue(Nucleus::createConstantInt(x));
3911 }
3912
3913 Int::Int(RValue<Int> rhs)
3914 {
3915 storeValue(rhs.value);
3916 }
3917
3918 Int::Int(RValue<UInt> rhs)
3919 {
3920 storeValue(rhs.value);
3921 }
3922
3923 Int::Int(const Int &rhs)
3924 {
3925 Value *value = rhs.loadValue();
3926 storeValue(value);
3927 }
3928
3929 Int::Int(const Reference<Int> &rhs)
3930 {
3931 Value *value = rhs.loadValue();
3932 storeValue(value);
3933 }
3934
3935 Int::Int(const UInt &rhs)
3936 {
3937 Value *value = rhs.loadValue();
3938 storeValue(value);
3939 }
3940
3941 Int::Int(const Reference<UInt> &rhs)
3942 {
3943 Value *value = rhs.loadValue();
3944 storeValue(value);
3945 }
3946
Nicolas Capens96d4e092016-11-18 14:22:38 -05003947 RValue<Int> Int::operator=(int rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003948 {
3949 return RValue<Int>(storeValue(Nucleus::createConstantInt(rhs)));
3950 }
3951
Nicolas Capens96d4e092016-11-18 14:22:38 -05003952 RValue<Int> Int::operator=(RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003953 {
3954 storeValue(rhs.value);
3955
3956 return rhs;
3957 }
3958
Nicolas Capens96d4e092016-11-18 14:22:38 -05003959 RValue<Int> Int::operator=(RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003960 {
3961 storeValue(rhs.value);
3962
3963 return RValue<Int>(rhs);
3964 }
3965
Nicolas Capens96d4e092016-11-18 14:22:38 -05003966 RValue<Int> Int::operator=(const Int &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003967 {
3968 Value *value = rhs.loadValue();
3969 storeValue(value);
3970
3971 return RValue<Int>(value);
3972 }
3973
Nicolas Capens96d4e092016-11-18 14:22:38 -05003974 RValue<Int> Int::operator=(const Reference<Int> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003975 {
3976 Value *value = rhs.loadValue();
3977 storeValue(value);
3978
3979 return RValue<Int>(value);
3980 }
3981
Nicolas Capens96d4e092016-11-18 14:22:38 -05003982 RValue<Int> Int::operator=(const UInt &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003983 {
3984 Value *value = rhs.loadValue();
3985 storeValue(value);
3986
3987 return RValue<Int>(value);
3988 }
3989
Nicolas Capens96d4e092016-11-18 14:22:38 -05003990 RValue<Int> Int::operator=(const Reference<UInt> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003991 {
3992 Value *value = rhs.loadValue();
3993 storeValue(value);
3994
3995 return RValue<Int>(value);
3996 }
3997
3998 RValue<Int> operator+(RValue<Int> lhs, RValue<Int> rhs)
3999 {
4000 return RValue<Int>(Nucleus::createAdd(lhs.value, rhs.value));
4001 }
4002
4003 RValue<Int> operator-(RValue<Int> lhs, RValue<Int> rhs)
4004 {
4005 return RValue<Int>(Nucleus::createSub(lhs.value, rhs.value));
4006 }
4007
4008 RValue<Int> operator*(RValue<Int> lhs, RValue<Int> rhs)
4009 {
4010 return RValue<Int>(Nucleus::createMul(lhs.value, rhs.value));
4011 }
4012
4013 RValue<Int> operator/(RValue<Int> lhs, RValue<Int> rhs)
4014 {
4015 return RValue<Int>(Nucleus::createSDiv(lhs.value, rhs.value));
4016 }
4017
4018 RValue<Int> operator%(RValue<Int> lhs, RValue<Int> rhs)
4019 {
4020 return RValue<Int>(Nucleus::createSRem(lhs.value, rhs.value));
4021 }
4022
4023 RValue<Int> operator&(RValue<Int> lhs, RValue<Int> rhs)
4024 {
4025 return RValue<Int>(Nucleus::createAnd(lhs.value, rhs.value));
4026 }
4027
4028 RValue<Int> operator|(RValue<Int> lhs, RValue<Int> rhs)
4029 {
4030 return RValue<Int>(Nucleus::createOr(lhs.value, rhs.value));
4031 }
4032
4033 RValue<Int> operator^(RValue<Int> lhs, RValue<Int> rhs)
4034 {
4035 return RValue<Int>(Nucleus::createXor(lhs.value, rhs.value));
4036 }
4037
4038 RValue<Int> operator<<(RValue<Int> lhs, RValue<Int> rhs)
4039 {
4040 return RValue<Int>(Nucleus::createShl(lhs.value, rhs.value));
4041 }
4042
4043 RValue<Int> operator>>(RValue<Int> lhs, RValue<Int> rhs)
4044 {
4045 return RValue<Int>(Nucleus::createAShr(lhs.value, rhs.value));
4046 }
4047
Nicolas Capens96d4e092016-11-18 14:22:38 -05004048 RValue<Int> operator+=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004049 {
4050 return lhs = lhs + rhs;
4051 }
4052
Nicolas Capens96d4e092016-11-18 14:22:38 -05004053 RValue<Int> operator-=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004054 {
4055 return lhs = lhs - rhs;
4056 }
4057
Nicolas Capens96d4e092016-11-18 14:22:38 -05004058 RValue<Int> operator*=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004059 {
4060 return lhs = lhs * rhs;
4061 }
4062
Nicolas Capens96d4e092016-11-18 14:22:38 -05004063 RValue<Int> operator/=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004064 {
4065 return lhs = lhs / rhs;
4066 }
4067
Nicolas Capens96d4e092016-11-18 14:22:38 -05004068 RValue<Int> operator%=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004069 {
4070 return lhs = lhs % rhs;
4071 }
4072
Nicolas Capens96d4e092016-11-18 14:22:38 -05004073 RValue<Int> operator&=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004074 {
4075 return lhs = lhs & rhs;
4076 }
4077
Nicolas Capens96d4e092016-11-18 14:22:38 -05004078 RValue<Int> operator|=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004079 {
4080 return lhs = lhs | rhs;
4081 }
4082
Nicolas Capens96d4e092016-11-18 14:22:38 -05004083 RValue<Int> operator^=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004084 {
4085 return lhs = lhs ^ rhs;
4086 }
4087
Nicolas Capens96d4e092016-11-18 14:22:38 -05004088 RValue<Int> operator<<=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004089 {
4090 return lhs = lhs << rhs;
4091 }
4092
Nicolas Capens96d4e092016-11-18 14:22:38 -05004093 RValue<Int> operator>>=(Int &lhs, RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004094 {
4095 return lhs = lhs >> rhs;
4096 }
4097
4098 RValue<Int> operator+(RValue<Int> val)
4099 {
4100 return val;
4101 }
4102
4103 RValue<Int> operator-(RValue<Int> val)
4104 {
4105 return RValue<Int>(Nucleus::createNeg(val.value));
4106 }
4107
4108 RValue<Int> operator~(RValue<Int> val)
4109 {
4110 return RValue<Int>(Nucleus::createNot(val.value));
4111 }
4112
Nicolas Capens96d4e092016-11-18 14:22:38 -05004113 RValue<Int> operator++(Int &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004114 {
Nicolas Capens5b41ba32016-12-08 14:34:00 -05004115 RValue<Int> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05004116 val += 1;
4117 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004118 }
4119
Nicolas Capens96d4e092016-11-18 14:22:38 -05004120 const Int &operator++(Int &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004121 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004122 val += 1;
4123 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004124 }
4125
Nicolas Capens96d4e092016-11-18 14:22:38 -05004126 RValue<Int> operator--(Int &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004127 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004128 RValue<Int> res = val;
4129 val -= 1;
4130 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004131 }
4132
Nicolas Capens96d4e092016-11-18 14:22:38 -05004133 const Int &operator--(Int &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004134 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004135 val -= 1;
4136 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004137 }
4138
4139 RValue<Bool> operator<(RValue<Int> lhs, RValue<Int> rhs)
4140 {
4141 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
4142 }
4143
4144 RValue<Bool> operator<=(RValue<Int> lhs, RValue<Int> rhs)
4145 {
4146 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
4147 }
4148
4149 RValue<Bool> operator>(RValue<Int> lhs, RValue<Int> rhs)
4150 {
4151 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
4152 }
4153
4154 RValue<Bool> operator>=(RValue<Int> lhs, RValue<Int> rhs)
4155 {
4156 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
4157 }
4158
4159 RValue<Bool> operator!=(RValue<Int> lhs, RValue<Int> rhs)
4160 {
4161 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
4162 }
4163
4164 RValue<Bool> operator==(RValue<Int> lhs, RValue<Int> rhs)
4165 {
4166 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
4167 }
4168
4169 RValue<Int> Max(RValue<Int> x, RValue<Int> y)
4170 {
4171 return IfThenElse(x > y, x, y);
4172 }
4173
4174 RValue<Int> Min(RValue<Int> x, RValue<Int> y)
4175 {
4176 return IfThenElse(x < y, x, y);
4177 }
4178
4179 RValue<Int> Clamp(RValue<Int> x, RValue<Int> min, RValue<Int> max)
4180 {
4181 return Min(Max(x, min), max);
4182 }
4183
4184 RValue<Int> RoundInt(RValue<Float> cast)
4185 {
Nicolas Capensf7b75882017-04-26 09:30:47 -04004186 if(emulateIntrinsics)
4187 {
4188 // Push the fractional part off the mantissa. Accurate up to +/-2^22.
4189 return Int((cast + Float(0x00C00000)) - Float(0x00C00000));
4190 }
4191 else
4192 {
4193 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
4194 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
4195 auto target = ::context->getConstantUndef(Ice::IceType_i32);
4196 auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
4197 nearbyint->addArg(cast.value);
4198 ::basicBlock->appendInst(nearbyint);
Nicolas Capensa8086512016-11-07 17:32:17 -05004199
Nicolas Capensf7b75882017-04-26 09:30:47 -04004200 return RValue<Int>(V(result));
4201 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04004202 }
4203
4204 Type *Int::getType()
4205 {
4206 return T(Ice::IceType_i32);
4207 }
4208
4209 Long::Long(RValue<Int> cast)
4210 {
4211 Value *integer = Nucleus::createSExt(cast.value, Long::getType());
4212
4213 storeValue(integer);
4214 }
4215
4216 Long::Long(RValue<UInt> cast)
4217 {
4218 Value *integer = Nucleus::createZExt(cast.value, Long::getType());
4219
4220 storeValue(integer);
4221 }
4222
Nicolas Capens598f8d82016-09-26 15:09:10 -04004223 Long::Long(RValue<Long> rhs)
4224 {
4225 storeValue(rhs.value);
4226 }
4227
Nicolas Capens96d4e092016-11-18 14:22:38 -05004228 RValue<Long> Long::operator=(int64_t rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004229 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04004230 return RValue<Long>(storeValue(Nucleus::createConstantLong(rhs)));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004231 }
4232
Nicolas Capens96d4e092016-11-18 14:22:38 -05004233 RValue<Long> Long::operator=(RValue<Long> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004234 {
4235 storeValue(rhs.value);
4236
4237 return rhs;
4238 }
4239
Nicolas Capens96d4e092016-11-18 14:22:38 -05004240 RValue<Long> Long::operator=(const Long &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004241 {
4242 Value *value = rhs.loadValue();
4243 storeValue(value);
4244
4245 return RValue<Long>(value);
4246 }
4247
Nicolas Capens96d4e092016-11-18 14:22:38 -05004248 RValue<Long> Long::operator=(const Reference<Long> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004249 {
4250 Value *value = rhs.loadValue();
4251 storeValue(value);
4252
4253 return RValue<Long>(value);
4254 }
4255
4256 RValue<Long> operator+(RValue<Long> lhs, RValue<Long> rhs)
4257 {
4258 return RValue<Long>(Nucleus::createAdd(lhs.value, rhs.value));
4259 }
4260
4261 RValue<Long> operator-(RValue<Long> lhs, RValue<Long> rhs)
4262 {
4263 return RValue<Long>(Nucleus::createSub(lhs.value, rhs.value));
4264 }
4265
Nicolas Capens96d4e092016-11-18 14:22:38 -05004266 RValue<Long> operator+=(Long &lhs, RValue<Long> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004267 {
4268 return lhs = lhs + rhs;
4269 }
4270
Nicolas Capens96d4e092016-11-18 14:22:38 -05004271 RValue<Long> operator-=(Long &lhs, RValue<Long> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004272 {
4273 return lhs = lhs - rhs;
4274 }
4275
4276 RValue<Long> AddAtomic(RValue<Pointer<Long> > x, RValue<Long> y)
4277 {
4278 return RValue<Long>(Nucleus::createAtomicAdd(x.value, y.value));
4279 }
4280
4281 Type *Long::getType()
4282 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04004283 return T(Ice::IceType_i64);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004284 }
4285
Nicolas Capens598f8d82016-09-26 15:09:10 -04004286 UInt::UInt(Argument<UInt> argument)
4287 {
4288 storeValue(argument.value);
4289 }
4290
4291 UInt::UInt(RValue<UShort> cast)
4292 {
4293 Value *integer = Nucleus::createZExt(cast.value, UInt::getType());
4294
4295 storeValue(integer);
4296 }
4297
4298 UInt::UInt(RValue<Long> cast)
4299 {
4300 Value *integer = Nucleus::createTrunc(cast.value, UInt::getType());
4301
4302 storeValue(integer);
4303 }
4304
4305 UInt::UInt(RValue<Float> cast)
4306 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05004307 // Smallest positive value representable in UInt, but not in Int
4308 const unsigned int ustart = 0x80000000u;
4309 const float ustartf = float(ustart);
4310
4311 // If the value is negative, store 0, otherwise store the result of the conversion
4312 storeValue((~(As<Int>(cast) >> 31) &
4313 // Check if the value can be represented as an Int
4314 IfThenElse(cast >= ustartf,
4315 // If the value is too large, subtract ustart and re-add it after conversion.
4316 As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)),
4317 // Otherwise, just convert normally
4318 Int(cast))).value);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004319 }
4320
Nicolas Capens598f8d82016-09-26 15:09:10 -04004321 UInt::UInt(int x)
4322 {
4323 storeValue(Nucleus::createConstantInt(x));
4324 }
4325
4326 UInt::UInt(unsigned int x)
4327 {
4328 storeValue(Nucleus::createConstantInt(x));
4329 }
4330
4331 UInt::UInt(RValue<UInt> rhs)
4332 {
4333 storeValue(rhs.value);
4334 }
4335
4336 UInt::UInt(RValue<Int> rhs)
4337 {
4338 storeValue(rhs.value);
4339 }
4340
4341 UInt::UInt(const UInt &rhs)
4342 {
4343 Value *value = rhs.loadValue();
4344 storeValue(value);
4345 }
4346
4347 UInt::UInt(const Reference<UInt> &rhs)
4348 {
4349 Value *value = rhs.loadValue();
4350 storeValue(value);
4351 }
4352
4353 UInt::UInt(const Int &rhs)
4354 {
4355 Value *value = rhs.loadValue();
4356 storeValue(value);
4357 }
4358
4359 UInt::UInt(const Reference<Int> &rhs)
4360 {
4361 Value *value = rhs.loadValue();
4362 storeValue(value);
4363 }
4364
Nicolas Capens96d4e092016-11-18 14:22:38 -05004365 RValue<UInt> UInt::operator=(unsigned int rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004366 {
4367 return RValue<UInt>(storeValue(Nucleus::createConstantInt(rhs)));
4368 }
4369
Nicolas Capens96d4e092016-11-18 14:22:38 -05004370 RValue<UInt> UInt::operator=(RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004371 {
4372 storeValue(rhs.value);
4373
4374 return rhs;
4375 }
4376
Nicolas Capens96d4e092016-11-18 14:22:38 -05004377 RValue<UInt> UInt::operator=(RValue<Int> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004378 {
4379 storeValue(rhs.value);
4380
4381 return RValue<UInt>(rhs);
4382 }
4383
Nicolas Capens96d4e092016-11-18 14:22:38 -05004384 RValue<UInt> UInt::operator=(const UInt &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004385 {
4386 Value *value = rhs.loadValue();
4387 storeValue(value);
4388
4389 return RValue<UInt>(value);
4390 }
4391
Nicolas Capens96d4e092016-11-18 14:22:38 -05004392 RValue<UInt> UInt::operator=(const Reference<UInt> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004393 {
4394 Value *value = rhs.loadValue();
4395 storeValue(value);
4396
4397 return RValue<UInt>(value);
4398 }
4399
Nicolas Capens96d4e092016-11-18 14:22:38 -05004400 RValue<UInt> UInt::operator=(const Int &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004401 {
4402 Value *value = rhs.loadValue();
4403 storeValue(value);
4404
4405 return RValue<UInt>(value);
4406 }
4407
Nicolas Capens96d4e092016-11-18 14:22:38 -05004408 RValue<UInt> UInt::operator=(const Reference<Int> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004409 {
4410 Value *value = rhs.loadValue();
4411 storeValue(value);
4412
4413 return RValue<UInt>(value);
4414 }
4415
4416 RValue<UInt> operator+(RValue<UInt> lhs, RValue<UInt> rhs)
4417 {
4418 return RValue<UInt>(Nucleus::createAdd(lhs.value, rhs.value));
4419 }
4420
4421 RValue<UInt> operator-(RValue<UInt> lhs, RValue<UInt> rhs)
4422 {
4423 return RValue<UInt>(Nucleus::createSub(lhs.value, rhs.value));
4424 }
4425
4426 RValue<UInt> operator*(RValue<UInt> lhs, RValue<UInt> rhs)
4427 {
4428 return RValue<UInt>(Nucleus::createMul(lhs.value, rhs.value));
4429 }
4430
4431 RValue<UInt> operator/(RValue<UInt> lhs, RValue<UInt> rhs)
4432 {
4433 return RValue<UInt>(Nucleus::createUDiv(lhs.value, rhs.value));
4434 }
4435
4436 RValue<UInt> operator%(RValue<UInt> lhs, RValue<UInt> rhs)
4437 {
4438 return RValue<UInt>(Nucleus::createURem(lhs.value, rhs.value));
4439 }
4440
4441 RValue<UInt> operator&(RValue<UInt> lhs, RValue<UInt> rhs)
4442 {
4443 return RValue<UInt>(Nucleus::createAnd(lhs.value, rhs.value));
4444 }
4445
4446 RValue<UInt> operator|(RValue<UInt> lhs, RValue<UInt> rhs)
4447 {
4448 return RValue<UInt>(Nucleus::createOr(lhs.value, rhs.value));
4449 }
4450
4451 RValue<UInt> operator^(RValue<UInt> lhs, RValue<UInt> rhs)
4452 {
4453 return RValue<UInt>(Nucleus::createXor(lhs.value, rhs.value));
4454 }
4455
4456 RValue<UInt> operator<<(RValue<UInt> lhs, RValue<UInt> rhs)
4457 {
4458 return RValue<UInt>(Nucleus::createShl(lhs.value, rhs.value));
4459 }
4460
4461 RValue<UInt> operator>>(RValue<UInt> lhs, RValue<UInt> rhs)
4462 {
4463 return RValue<UInt>(Nucleus::createLShr(lhs.value, rhs.value));
4464 }
4465
Nicolas Capens96d4e092016-11-18 14:22:38 -05004466 RValue<UInt> operator+=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004467 {
4468 return lhs = lhs + rhs;
4469 }
4470
Nicolas Capens96d4e092016-11-18 14:22:38 -05004471 RValue<UInt> operator-=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004472 {
4473 return lhs = lhs - rhs;
4474 }
4475
Nicolas Capens96d4e092016-11-18 14:22:38 -05004476 RValue<UInt> operator*=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004477 {
4478 return lhs = lhs * rhs;
4479 }
4480
Nicolas Capens96d4e092016-11-18 14:22:38 -05004481 RValue<UInt> operator/=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004482 {
4483 return lhs = lhs / rhs;
4484 }
4485
Nicolas Capens96d4e092016-11-18 14:22:38 -05004486 RValue<UInt> operator%=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004487 {
4488 return lhs = lhs % rhs;
4489 }
4490
Nicolas Capens96d4e092016-11-18 14:22:38 -05004491 RValue<UInt> operator&=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004492 {
4493 return lhs = lhs & rhs;
4494 }
4495
Nicolas Capens96d4e092016-11-18 14:22:38 -05004496 RValue<UInt> operator|=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004497 {
4498 return lhs = lhs | rhs;
4499 }
4500
Nicolas Capens96d4e092016-11-18 14:22:38 -05004501 RValue<UInt> operator^=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004502 {
4503 return lhs = lhs ^ rhs;
4504 }
4505
Nicolas Capens96d4e092016-11-18 14:22:38 -05004506 RValue<UInt> operator<<=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004507 {
4508 return lhs = lhs << rhs;
4509 }
4510
Nicolas Capens96d4e092016-11-18 14:22:38 -05004511 RValue<UInt> operator>>=(UInt &lhs, RValue<UInt> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004512 {
4513 return lhs = lhs >> rhs;
4514 }
4515
4516 RValue<UInt> operator+(RValue<UInt> val)
4517 {
4518 return val;
4519 }
4520
4521 RValue<UInt> operator-(RValue<UInt> val)
4522 {
4523 return RValue<UInt>(Nucleus::createNeg(val.value));
4524 }
4525
4526 RValue<UInt> operator~(RValue<UInt> val)
4527 {
4528 return RValue<UInt>(Nucleus::createNot(val.value));
4529 }
4530
Nicolas Capens96d4e092016-11-18 14:22:38 -05004531 RValue<UInt> operator++(UInt &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004532 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004533 RValue<UInt> res = val;
4534 val += 1;
4535 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004536 }
4537
Nicolas Capens96d4e092016-11-18 14:22:38 -05004538 const UInt &operator++(UInt &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04004539 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004540 val += 1;
4541 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004542 }
4543
Nicolas Capens96d4e092016-11-18 14:22:38 -05004544 RValue<UInt> operator--(UInt &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004545 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004546 RValue<UInt> res = val;
4547 val -= 1;
4548 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004549 }
4550
Nicolas Capens96d4e092016-11-18 14:22:38 -05004551 const UInt &operator--(UInt &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04004552 {
Nicolas Capensd1229402016-11-07 16:05:22 -05004553 val -= 1;
4554 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04004555 }
4556
4557 RValue<UInt> Max(RValue<UInt> x, RValue<UInt> y)
4558 {
4559 return IfThenElse(x > y, x, y);
4560 }
4561
4562 RValue<UInt> Min(RValue<UInt> x, RValue<UInt> y)
4563 {
4564 return IfThenElse(x < y, x, y);
4565 }
4566
4567 RValue<UInt> Clamp(RValue<UInt> x, RValue<UInt> min, RValue<UInt> max)
4568 {
4569 return Min(Max(x, min), max);
4570 }
4571
4572 RValue<Bool> operator<(RValue<UInt> lhs, RValue<UInt> rhs)
4573 {
4574 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
4575 }
4576
4577 RValue<Bool> operator<=(RValue<UInt> lhs, RValue<UInt> rhs)
4578 {
4579 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
4580 }
4581
4582 RValue<Bool> operator>(RValue<UInt> lhs, RValue<UInt> rhs)
4583 {
4584 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
4585 }
4586
4587 RValue<Bool> operator>=(RValue<UInt> lhs, RValue<UInt> rhs)
4588 {
4589 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
4590 }
4591
4592 RValue<Bool> operator!=(RValue<UInt> lhs, RValue<UInt> rhs)
4593 {
4594 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
4595 }
4596
4597 RValue<Bool> operator==(RValue<UInt> lhs, RValue<UInt> rhs)
4598 {
4599 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
4600 }
4601
4602// RValue<UInt> RoundUInt(RValue<Float> cast)
4603// {
Nicolas Capensc37252c2016-09-28 16:11:54 -04004604// assert(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004605// }
4606
4607 Type *UInt::getType()
4608 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04004609 return T(Ice::IceType_i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004610 }
4611
4612// Int2::Int2(RValue<Int> cast)
4613// {
4614// Value *extend = Nucleus::createZExt(cast.value, Long::getType());
4615// Value *vector = Nucleus::createBitCast(extend, Int2::getType());
4616//
4617// Constant *shuffle[2];
4618// shuffle[0] = Nucleus::createConstantInt(0);
4619// shuffle[1] = Nucleus::createConstantInt(0);
4620//
4621// Value *replicate = Nucleus::createShuffleVector(vector, UndefValue::get(Int2::getType()), Nucleus::createConstantVector(shuffle, 2));
4622//
4623// storeValue(replicate);
4624// }
4625
4626 Int2::Int2(RValue<Int4> cast)
4627 {
Nicolas Capens22008782016-10-20 01:11:47 -04004628 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004629 }
4630
Nicolas Capens598f8d82016-09-26 15:09:10 -04004631 Int2::Int2(int x, int y)
4632 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004633 int64_t constantVector[2] = {x, y};
4634 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004635 }
4636
4637 Int2::Int2(RValue<Int2> rhs)
4638 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004639 storeValue(rhs.value);
4640 }
4641
4642 Int2::Int2(const Int2 &rhs)
4643 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004644 Value *value = rhs.loadValue();
4645 storeValue(value);
4646 }
4647
4648 Int2::Int2(const Reference<Int2> &rhs)
4649 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004650 Value *value = rhs.loadValue();
4651 storeValue(value);
4652 }
4653
4654 Int2::Int2(RValue<Int> lo, RValue<Int> hi)
4655 {
Nicolas Capensd4227962016-11-09 14:24:25 -05004656 int shuffle[4] = {0, 4, 1, 5};
4657 Value *packed = Nucleus::createShuffleVector(Int4(lo).loadValue(), Int4(hi).loadValue(), shuffle);
4658
4659 storeValue(Nucleus::createBitCast(packed, Int2::getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004660 }
4661
Nicolas Capens96d4e092016-11-18 14:22:38 -05004662 RValue<Int2> Int2::operator=(RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004663 {
4664 storeValue(rhs.value);
4665
4666 return rhs;
4667 }
4668
Nicolas Capens96d4e092016-11-18 14:22:38 -05004669 RValue<Int2> Int2::operator=(const Int2 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004670 {
4671 Value *value = rhs.loadValue();
4672 storeValue(value);
4673
4674 return RValue<Int2>(value);
4675 }
4676
Nicolas Capens96d4e092016-11-18 14:22:38 -05004677 RValue<Int2> Int2::operator=(const Reference<Int2> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004678 {
4679 Value *value = rhs.loadValue();
4680 storeValue(value);
4681
4682 return RValue<Int2>(value);
4683 }
4684
4685 RValue<Int2> operator+(RValue<Int2> lhs, RValue<Int2> rhs)
4686 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004687 return RValue<Int2>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004688 }
4689
4690 RValue<Int2> operator-(RValue<Int2> lhs, RValue<Int2> rhs)
4691 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004692 return RValue<Int2>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004693 }
4694
4695// RValue<Int2> operator*(RValue<Int2> lhs, RValue<Int2> rhs)
4696// {
4697// return RValue<Int2>(Nucleus::createMul(lhs.value, rhs.value));
4698// }
4699
4700// RValue<Int2> operator/(RValue<Int2> lhs, RValue<Int2> rhs)
4701// {
4702// return RValue<Int2>(Nucleus::createSDiv(lhs.value, rhs.value));
4703// }
4704
4705// RValue<Int2> operator%(RValue<Int2> lhs, RValue<Int2> rhs)
4706// {
4707// return RValue<Int2>(Nucleus::createSRem(lhs.value, rhs.value));
4708// }
4709
4710 RValue<Int2> operator&(RValue<Int2> lhs, RValue<Int2> rhs)
4711 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004712 return RValue<Int2>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004713 }
4714
4715 RValue<Int2> operator|(RValue<Int2> lhs, RValue<Int2> rhs)
4716 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004717 return RValue<Int2>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004718 }
4719
4720 RValue<Int2> operator^(RValue<Int2> lhs, RValue<Int2> rhs)
4721 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004722 return RValue<Int2>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004723 }
4724
4725 RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs)
4726 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004727 return RValue<Int2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004728 }
4729
4730 RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs)
4731 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004732 return RValue<Int2>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004733 }
4734
Nicolas Capens96d4e092016-11-18 14:22:38 -05004735 RValue<Int2> operator+=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004736 {
4737 return lhs = lhs + rhs;
4738 }
4739
Nicolas Capens96d4e092016-11-18 14:22:38 -05004740 RValue<Int2> operator-=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004741 {
4742 return lhs = lhs - rhs;
4743 }
4744
Nicolas Capens96d4e092016-11-18 14:22:38 -05004745// RValue<Int2> operator*=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004746// {
4747// return lhs = lhs * rhs;
4748// }
4749
Nicolas Capens96d4e092016-11-18 14:22:38 -05004750// RValue<Int2> operator/=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004751// {
4752// return lhs = lhs / rhs;
4753// }
4754
Nicolas Capens96d4e092016-11-18 14:22:38 -05004755// RValue<Int2> operator%=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004756// {
4757// return lhs = lhs % rhs;
4758// }
4759
Nicolas Capens96d4e092016-11-18 14:22:38 -05004760 RValue<Int2> operator&=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004761 {
4762 return lhs = lhs & rhs;
4763 }
4764
Nicolas Capens96d4e092016-11-18 14:22:38 -05004765 RValue<Int2> operator|=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004766 {
4767 return lhs = lhs | rhs;
4768 }
4769
Nicolas Capens96d4e092016-11-18 14:22:38 -05004770 RValue<Int2> operator^=(Int2 &lhs, RValue<Int2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004771 {
4772 return lhs = lhs ^ rhs;
4773 }
4774
Nicolas Capens96d4e092016-11-18 14:22:38 -05004775 RValue<Int2> operator<<=(Int2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004776 {
4777 return lhs = lhs << rhs;
4778 }
4779
Nicolas Capens96d4e092016-11-18 14:22:38 -05004780 RValue<Int2> operator>>=(Int2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004781 {
4782 return lhs = lhs >> rhs;
4783 }
4784
Nicolas Capens598f8d82016-09-26 15:09:10 -04004785// RValue<Int2> operator+(RValue<Int2> val)
4786// {
4787// return val;
4788// }
4789
4790// RValue<Int2> operator-(RValue<Int2> val)
4791// {
4792// return RValue<Int2>(Nucleus::createNeg(val.value));
4793// }
4794
4795 RValue<Int2> operator~(RValue<Int2> val)
4796 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -05004797 return RValue<Int2>(Nucleus::createNot(val.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004798 }
4799
Nicolas Capens45f187a2016-12-02 15:30:56 -05004800 RValue<Short4> UnpackLow(RValue<Int2> x, RValue<Int2> y)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004801 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05004802 int shuffle[4] = {0, 4, 1, 5}; // Real type is v4i32
4803 return As<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004804 }
4805
Nicolas Capens45f187a2016-12-02 15:30:56 -05004806 RValue<Short4> UnpackHigh(RValue<Int2> x, RValue<Int2> y)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004807 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08004808 int shuffle[4] = {0, 4, 1, 5}; // Real type is v4i32
Nicolas Capensc70a1162016-12-03 00:16:14 -05004809 auto lowHigh = RValue<Int4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
4810 return As<Short4>(Swizzle(lowHigh, 0xEE));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004811 }
4812
4813 RValue<Int> Extract(RValue<Int2> val, int i)
4814 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05004815 return RValue<Int>(Nucleus::createExtractElement(val.value, Int::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004816 }
4817
4818 RValue<Int2> Insert(RValue<Int2> val, RValue<Int> element, int i)
4819 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05004820 return RValue<Int2>(Nucleus::createInsertElement(val.value, element.value, i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004821 }
4822
4823 Type *Int2::getType()
4824 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004825 return T(Type_v2i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004826 }
4827
Nicolas Capens598f8d82016-09-26 15:09:10 -04004828 UInt2::UInt2(unsigned int x, unsigned int y)
4829 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04004830 int64_t constantVector[2] = {x, y};
4831 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004832 }
4833
4834 UInt2::UInt2(RValue<UInt2> rhs)
4835 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004836 storeValue(rhs.value);
4837 }
4838
4839 UInt2::UInt2(const UInt2 &rhs)
4840 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004841 Value *value = rhs.loadValue();
4842 storeValue(value);
4843 }
4844
4845 UInt2::UInt2(const Reference<UInt2> &rhs)
4846 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04004847 Value *value = rhs.loadValue();
4848 storeValue(value);
4849 }
4850
Nicolas Capens96d4e092016-11-18 14:22:38 -05004851 RValue<UInt2> UInt2::operator=(RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004852 {
4853 storeValue(rhs.value);
4854
4855 return rhs;
4856 }
4857
Nicolas Capens96d4e092016-11-18 14:22:38 -05004858 RValue<UInt2> UInt2::operator=(const UInt2 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004859 {
4860 Value *value = rhs.loadValue();
4861 storeValue(value);
4862
4863 return RValue<UInt2>(value);
4864 }
4865
Nicolas Capens96d4e092016-11-18 14:22:38 -05004866 RValue<UInt2> UInt2::operator=(const Reference<UInt2> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004867 {
4868 Value *value = rhs.loadValue();
4869 storeValue(value);
4870
4871 return RValue<UInt2>(value);
4872 }
4873
4874 RValue<UInt2> operator+(RValue<UInt2> lhs, RValue<UInt2> rhs)
4875 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004876 return RValue<UInt2>(Nucleus::createAdd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004877 }
4878
4879 RValue<UInt2> operator-(RValue<UInt2> lhs, RValue<UInt2> rhs)
4880 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004881 return RValue<UInt2>(Nucleus::createSub(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004882 }
4883
4884// RValue<UInt2> operator*(RValue<UInt2> lhs, RValue<UInt2> rhs)
4885// {
4886// return RValue<UInt2>(Nucleus::createMul(lhs.value, rhs.value));
4887// }
4888
4889// RValue<UInt2> operator/(RValue<UInt2> lhs, RValue<UInt2> rhs)
4890// {
4891// return RValue<UInt2>(Nucleus::createUDiv(lhs.value, rhs.value));
4892// }
4893
4894// RValue<UInt2> operator%(RValue<UInt2> lhs, RValue<UInt2> rhs)
4895// {
4896// return RValue<UInt2>(Nucleus::createURem(lhs.value, rhs.value));
4897// }
4898
4899 RValue<UInt2> operator&(RValue<UInt2> lhs, RValue<UInt2> rhs)
4900 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004901 return RValue<UInt2>(Nucleus::createAnd(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004902 }
4903
4904 RValue<UInt2> operator|(RValue<UInt2> lhs, RValue<UInt2> rhs)
4905 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004906 return RValue<UInt2>(Nucleus::createOr(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004907 }
4908
4909 RValue<UInt2> operator^(RValue<UInt2> lhs, RValue<UInt2> rhs)
4910 {
Nicolas Capensc4c431d2016-10-21 15:30:29 -04004911 return RValue<UInt2>(Nucleus::createXor(lhs.value, rhs.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004912 }
4913
4914 RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs)
4915 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004916 return RValue<UInt2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004917 }
4918
4919 RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs)
4920 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05004921 return RValue<UInt2>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04004922 }
4923
Nicolas Capens96d4e092016-11-18 14:22:38 -05004924 RValue<UInt2> operator+=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004925 {
4926 return lhs = lhs + rhs;
4927 }
4928
Nicolas Capens96d4e092016-11-18 14:22:38 -05004929 RValue<UInt2> operator-=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004930 {
4931 return lhs = lhs - rhs;
4932 }
4933
Nicolas Capens96d4e092016-11-18 14:22:38 -05004934// RValue<UInt2> operator*=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004935// {
4936// return lhs = lhs * rhs;
4937// }
4938
Nicolas Capens96d4e092016-11-18 14:22:38 -05004939// RValue<UInt2> operator/=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004940// {
4941// return lhs = lhs / rhs;
4942// }
4943
Nicolas Capens96d4e092016-11-18 14:22:38 -05004944// RValue<UInt2> operator%=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004945// {
4946// return lhs = lhs % rhs;
4947// }
4948
Nicolas Capens96d4e092016-11-18 14:22:38 -05004949 RValue<UInt2> operator&=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004950 {
4951 return lhs = lhs & rhs;
4952 }
4953
Nicolas Capens96d4e092016-11-18 14:22:38 -05004954 RValue<UInt2> operator|=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004955 {
4956 return lhs = lhs | rhs;
4957 }
4958
Nicolas Capens96d4e092016-11-18 14:22:38 -05004959 RValue<UInt2> operator^=(UInt2 &lhs, RValue<UInt2> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004960 {
4961 return lhs = lhs ^ rhs;
4962 }
4963
Nicolas Capens96d4e092016-11-18 14:22:38 -05004964 RValue<UInt2> operator<<=(UInt2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004965 {
4966 return lhs = lhs << rhs;
4967 }
4968
Nicolas Capens96d4e092016-11-18 14:22:38 -05004969 RValue<UInt2> operator>>=(UInt2 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04004970 {
4971 return lhs = lhs >> rhs;
4972 }
4973
Nicolas Capens598f8d82016-09-26 15:09:10 -04004974// RValue<UInt2> operator+(RValue<UInt2> val)
4975// {
4976// return val;
4977// }
4978
4979// RValue<UInt2> operator-(RValue<UInt2> val)
4980// {
4981// return RValue<UInt2>(Nucleus::createNeg(val.value));
4982// }
4983
4984 RValue<UInt2> operator~(RValue<UInt2> val)
4985 {
4986 return RValue<UInt2>(Nucleus::createNot(val.value));
4987 }
4988
4989 Type *UInt2::getType()
4990 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04004991 return T(Type_v2i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04004992 }
4993
4994 Int4::Int4(RValue<Byte4> cast)
4995 {
Nicolas Capensd4227962016-11-09 14:24:25 -05004996 Value *x = Nucleus::createBitCast(cast.value, Int::getType());
4997 Value *a = Nucleus::createInsertElement(loadValue(), x, 0);
4998
4999 Value *e;
5000 int swizzle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23};
5001 Value *b = Nucleus::createBitCast(a, Byte16::getType());
5002 Value *c = Nucleus::createShuffleVector(b, V(Nucleus::createNullValue(Byte16::getType())), swizzle);
5003
5004 int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11};
5005 Value *d = Nucleus::createBitCast(c, Short8::getType());
5006 e = Nucleus::createShuffleVector(d, V(Nucleus::createNullValue(Short8::getType())), swizzle2);
5007
5008 Value *f = Nucleus::createBitCast(e, Int4::getType());
5009 storeValue(f);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005010 }
5011
5012 Int4::Int4(RValue<SByte4> cast)
5013 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005014 Value *x = Nucleus::createBitCast(cast.value, Int::getType());
5015 Value *a = Nucleus::createInsertElement(loadValue(), x, 0);
5016
5017 Value *e;
5018 int swizzle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7};
5019 Value *b = Nucleus::createBitCast(a, Byte16::getType());
5020 Value *c = Nucleus::createShuffleVector(b, b, swizzle);
5021
5022 int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3};
5023 Value *d = Nucleus::createBitCast(c, Short8::getType());
5024 e = Nucleus::createShuffleVector(d, d, swizzle2);
5025
5026 Value *f = Nucleus::createBitCast(e, Int4::getType());
Nicolas Capens15060bb2016-12-05 22:17:19 -05005027 Value *g = Nucleus::createAShr(f, V(::context->getConstantInt32(24)));
Nicolas Capensd4227962016-11-09 14:24:25 -05005028 storeValue(g);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005029 }
5030
5031 Int4::Int4(RValue<Float4> cast)
5032 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005033 Value *xyzw = Nucleus::createFPToSI(cast.value, Int4::getType());
5034
5035 storeValue(xyzw);
5036 }
5037
5038 Int4::Int4(RValue<Short4> cast)
5039 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005040 int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3};
5041 Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle);
5042 Value *d = Nucleus::createBitCast(c, Int4::getType());
Nicolas Capens15060bb2016-12-05 22:17:19 -05005043 Value *e = Nucleus::createAShr(d, V(::context->getConstantInt32(16)));
Nicolas Capensd4227962016-11-09 14:24:25 -05005044 storeValue(e);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005045 }
5046
5047 Int4::Int4(RValue<UShort4> cast)
5048 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005049 int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11};
5050 Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle);
5051 Value *d = Nucleus::createBitCast(c, Int4::getType());
5052 storeValue(d);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005053 }
5054
Nicolas Capens598f8d82016-09-26 15:09:10 -04005055 Int4::Int4(int xyzw)
5056 {
5057 constant(xyzw, xyzw, xyzw, xyzw);
5058 }
5059
5060 Int4::Int4(int x, int yzw)
5061 {
5062 constant(x, yzw, yzw, yzw);
5063 }
5064
5065 Int4::Int4(int x, int y, int zw)
5066 {
5067 constant(x, y, zw, zw);
5068 }
5069
5070 Int4::Int4(int x, int y, int z, int w)
5071 {
5072 constant(x, y, z, w);
5073 }
5074
5075 void Int4::constant(int x, int y, int z, int w)
5076 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04005077 int64_t constantVector[4] = {x, y, z, w};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04005078 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005079 }
5080
5081 Int4::Int4(RValue<Int4> rhs)
5082 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005083 storeValue(rhs.value);
5084 }
5085
5086 Int4::Int4(const Int4 &rhs)
5087 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005088 Value *value = rhs.loadValue();
5089 storeValue(value);
5090 }
5091
5092 Int4::Int4(const Reference<Int4> &rhs)
5093 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005094 Value *value = rhs.loadValue();
5095 storeValue(value);
5096 }
5097
5098 Int4::Int4(RValue<UInt4> rhs)
5099 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005100 storeValue(rhs.value);
5101 }
5102
5103 Int4::Int4(const UInt4 &rhs)
5104 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005105 Value *value = rhs.loadValue();
5106 storeValue(value);
5107 }
5108
5109 Int4::Int4(const Reference<UInt4> &rhs)
5110 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005111 Value *value = rhs.loadValue();
5112 storeValue(value);
5113 }
5114
5115 Int4::Int4(RValue<Int2> lo, RValue<Int2> hi)
5116 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05005117 int shuffle[4] = {0, 1, 4, 5}; // Real type is v4i32
5118 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
5119
5120 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005121 }
5122
5123 Int4::Int4(RValue<Int> rhs)
5124 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08005125 Value *vector = Nucleus::createBitCast(rhs.value, Int4::getType());
Nicolas Capensd4227962016-11-09 14:24:25 -05005126
5127 int swizzle[4] = {0, 0, 0, 0};
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08005128 Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle);
Nicolas Capensd4227962016-11-09 14:24:25 -05005129
5130 storeValue(replicate);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005131 }
5132
5133 Int4::Int4(const Int &rhs)
5134 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005135 *this = RValue<Int>(rhs.loadValue());
5136 }
5137
5138 Int4::Int4(const Reference<Int> &rhs)
5139 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005140 *this = RValue<Int>(rhs.loadValue());
5141 }
5142
Nicolas Capens96d4e092016-11-18 14:22:38 -05005143 RValue<Int4> Int4::operator=(RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005144 {
5145 storeValue(rhs.value);
5146
5147 return rhs;
5148 }
5149
Nicolas Capens96d4e092016-11-18 14:22:38 -05005150 RValue<Int4> Int4::operator=(const Int4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005151 {
5152 Value *value = rhs.loadValue();
5153 storeValue(value);
5154
5155 return RValue<Int4>(value);
5156 }
5157
Nicolas Capens96d4e092016-11-18 14:22:38 -05005158 RValue<Int4> Int4::operator=(const Reference<Int4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005159 {
5160 Value *value = rhs.loadValue();
5161 storeValue(value);
5162
5163 return RValue<Int4>(value);
5164 }
5165
5166 RValue<Int4> operator+(RValue<Int4> lhs, RValue<Int4> rhs)
5167 {
5168 return RValue<Int4>(Nucleus::createAdd(lhs.value, rhs.value));
5169 }
5170
5171 RValue<Int4> operator-(RValue<Int4> lhs, RValue<Int4> rhs)
5172 {
5173 return RValue<Int4>(Nucleus::createSub(lhs.value, rhs.value));
5174 }
5175
5176 RValue<Int4> operator*(RValue<Int4> lhs, RValue<Int4> rhs)
5177 {
5178 return RValue<Int4>(Nucleus::createMul(lhs.value, rhs.value));
5179 }
5180
5181 RValue<Int4> operator/(RValue<Int4> lhs, RValue<Int4> rhs)
5182 {
5183 return RValue<Int4>(Nucleus::createSDiv(lhs.value, rhs.value));
5184 }
5185
5186 RValue<Int4> operator%(RValue<Int4> lhs, RValue<Int4> rhs)
5187 {
5188 return RValue<Int4>(Nucleus::createSRem(lhs.value, rhs.value));
5189 }
5190
5191 RValue<Int4> operator&(RValue<Int4> lhs, RValue<Int4> rhs)
5192 {
5193 return RValue<Int4>(Nucleus::createAnd(lhs.value, rhs.value));
5194 }
5195
5196 RValue<Int4> operator|(RValue<Int4> lhs, RValue<Int4> rhs)
5197 {
5198 return RValue<Int4>(Nucleus::createOr(lhs.value, rhs.value));
5199 }
5200
5201 RValue<Int4> operator^(RValue<Int4> lhs, RValue<Int4> rhs)
5202 {
5203 return RValue<Int4>(Nucleus::createXor(lhs.value, rhs.value));
5204 }
5205
5206 RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs)
5207 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005208 return RValue<Int4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005209 }
5210
5211 RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs)
5212 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005213 return RValue<Int4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005214 }
5215
5216 RValue<Int4> operator<<(RValue<Int4> lhs, RValue<Int4> rhs)
5217 {
5218 return RValue<Int4>(Nucleus::createShl(lhs.value, rhs.value));
5219 }
5220
5221 RValue<Int4> operator>>(RValue<Int4> lhs, RValue<Int4> rhs)
5222 {
5223 return RValue<Int4>(Nucleus::createAShr(lhs.value, rhs.value));
5224 }
5225
Nicolas Capens96d4e092016-11-18 14:22:38 -05005226 RValue<Int4> operator+=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005227 {
5228 return lhs = lhs + rhs;
5229 }
5230
Nicolas Capens96d4e092016-11-18 14:22:38 -05005231 RValue<Int4> operator-=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005232 {
5233 return lhs = lhs - rhs;
5234 }
5235
Nicolas Capens96d4e092016-11-18 14:22:38 -05005236 RValue<Int4> operator*=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005237 {
5238 return lhs = lhs * rhs;
5239 }
5240
Nicolas Capens96d4e092016-11-18 14:22:38 -05005241// RValue<Int4> operator/=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005242// {
5243// return lhs = lhs / rhs;
5244// }
5245
Nicolas Capens96d4e092016-11-18 14:22:38 -05005246// RValue<Int4> operator%=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005247// {
5248// return lhs = lhs % rhs;
5249// }
5250
Nicolas Capens96d4e092016-11-18 14:22:38 -05005251 RValue<Int4> operator&=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005252 {
5253 return lhs = lhs & rhs;
5254 }
5255
Nicolas Capens96d4e092016-11-18 14:22:38 -05005256 RValue<Int4> operator|=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005257 {
5258 return lhs = lhs | rhs;
5259 }
5260
Nicolas Capens96d4e092016-11-18 14:22:38 -05005261 RValue<Int4> operator^=(Int4 &lhs, RValue<Int4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005262 {
5263 return lhs = lhs ^ rhs;
5264 }
5265
Nicolas Capens96d4e092016-11-18 14:22:38 -05005266 RValue<Int4> operator<<=(Int4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005267 {
5268 return lhs = lhs << rhs;
5269 }
5270
Nicolas Capens96d4e092016-11-18 14:22:38 -05005271 RValue<Int4> operator>>=(Int4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005272 {
5273 return lhs = lhs >> rhs;
5274 }
5275
5276 RValue<Int4> operator+(RValue<Int4> val)
5277 {
5278 return val;
5279 }
5280
5281 RValue<Int4> operator-(RValue<Int4> val)
5282 {
5283 return RValue<Int4>(Nucleus::createNeg(val.value));
5284 }
5285
5286 RValue<Int4> operator~(RValue<Int4> val)
5287 {
5288 return RValue<Int4>(Nucleus::createNot(val.value));
5289 }
5290
5291 RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y)
5292 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005293 return RValue<Int4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005294 }
5295
5296 RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y)
5297 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005298 return RValue<Int4>(Nucleus::createICmpSLT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005299 }
5300
5301 RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y)
5302 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005303 return RValue<Int4>(Nucleus::createICmpSLE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005304 }
5305
5306 RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y)
5307 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005308 return RValue<Int4>(Nucleus::createICmpNE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005309 }
5310
5311 RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y)
5312 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005313 return RValue<Int4>(Nucleus::createICmpSGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005314 }
5315
5316 RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y)
5317 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005318 return RValue<Int4>(Nucleus::createICmpSGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005319 }
5320
5321 RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y)
5322 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005323 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5324 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value, y.value);
5325 ::basicBlock->appendInst(cmp);
5326
5327 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5328 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5329 ::basicBlock->appendInst(select);
5330
5331 return RValue<Int4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005332 }
5333
5334 RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y)
5335 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005336 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5337 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value);
5338 ::basicBlock->appendInst(cmp);
5339
5340 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5341 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5342 ::basicBlock->appendInst(select);
5343
5344 return RValue<Int4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005345 }
5346
5347 RValue<Int4> RoundInt(RValue<Float4> cast)
5348 {
Nicolas Capensf7b75882017-04-26 09:30:47 -04005349 if(emulateIntrinsics)
5350 {
5351 // Push the fractional part off the mantissa. Accurate up to +/-2^22.
5352 return Int4((cast + Float4(0x00C00000)) - Float4(0x00C00000));
5353 }
5354 else
5355 {
5356 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5357 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5358 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5359 auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5360 nearbyint->addArg(cast.value);
5361 ::basicBlock->appendInst(nearbyint);
Nicolas Capensa8086512016-11-07 17:32:17 -05005362
Nicolas Capensf7b75882017-04-26 09:30:47 -04005363 return RValue<Int4>(V(result));
5364 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04005365 }
5366
5367 RValue<Short8> Pack(RValue<Int4> x, RValue<Int4> y)
5368 {
Nicolas Capensec54a172016-10-25 17:32:37 -04005369 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
5370 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5371 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5372 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
5373 pack->addArg(x.value);
5374 pack->addArg(y.value);
5375 ::basicBlock->appendInst(pack);
5376
5377 return RValue<Short8>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005378 }
5379
5380 RValue<Int> Extract(RValue<Int4> x, int i)
5381 {
Nicolas Capense95d5342016-09-30 11:37:28 -04005382 return RValue<Int>(Nucleus::createExtractElement(x.value, Int::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005383 }
5384
5385 RValue<Int4> Insert(RValue<Int4> x, RValue<Int> element, int i)
5386 {
5387 return RValue<Int4>(Nucleus::createInsertElement(x.value, element.value, i));
5388 }
5389
5390 RValue<Int> SignMask(RValue<Int4> x)
5391 {
Nicolas Capensf2cb9df2016-10-21 17:26:13 -04005392 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
5393 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5394 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5395 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5396 movmsk->addArg(x.value);
5397 ::basicBlock->appendInst(movmsk);
5398
5399 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005400 }
5401
5402 RValue<Int4> Swizzle(RValue<Int4> x, unsigned char select)
5403 {
Nicolas Capense95d5342016-09-30 11:37:28 -04005404 return RValue<Int4>(createSwizzle4(x.value, select));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005405 }
5406
5407 Type *Int4::getType()
5408 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04005409 return T(Ice::IceType_v4i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005410 }
5411
5412 UInt4::UInt4(RValue<Float4> cast)
5413 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05005414 // Smallest positive value representable in UInt, but not in Int
5415 const unsigned int ustart = 0x80000000u;
5416 const float ustartf = float(ustart);
5417
5418 // Check if the value can be represented as an Int
5419 Int4 uiValue = CmpNLT(cast, Float4(ustartf));
5420 // If the value is too large, subtract ustart and re-add it after conversion.
5421 uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) |
5422 // Otherwise, just convert normally
5423 (~uiValue & Int4(cast));
5424 // If the value is negative, store 0, otherwise store the result of the conversion
5425 storeValue((~(As<Int4>(cast) >> 31) & uiValue).value);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005426 }
5427
Nicolas Capens598f8d82016-09-26 15:09:10 -04005428 UInt4::UInt4(int xyzw)
5429 {
5430 constant(xyzw, xyzw, xyzw, xyzw);
5431 }
5432
5433 UInt4::UInt4(int x, int yzw)
5434 {
5435 constant(x, yzw, yzw, yzw);
5436 }
5437
5438 UInt4::UInt4(int x, int y, int zw)
5439 {
5440 constant(x, y, zw, zw);
5441 }
5442
5443 UInt4::UInt4(int x, int y, int z, int w)
5444 {
5445 constant(x, y, z, w);
5446 }
5447
5448 void UInt4::constant(int x, int y, int z, int w)
5449 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04005450 int64_t constantVector[4] = {x, y, z, w};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04005451 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005452 }
5453
5454 UInt4::UInt4(RValue<UInt4> rhs)
5455 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005456 storeValue(rhs.value);
5457 }
5458
5459 UInt4::UInt4(const UInt4 &rhs)
5460 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005461 Value *value = rhs.loadValue();
5462 storeValue(value);
5463 }
5464
5465 UInt4::UInt4(const Reference<UInt4> &rhs)
5466 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005467 Value *value = rhs.loadValue();
5468 storeValue(value);
5469 }
5470
5471 UInt4::UInt4(RValue<Int4> rhs)
5472 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005473 storeValue(rhs.value);
5474 }
5475
5476 UInt4::UInt4(const Int4 &rhs)
5477 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005478 Value *value = rhs.loadValue();
5479 storeValue(value);
5480 }
5481
5482 UInt4::UInt4(const Reference<Int4> &rhs)
5483 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005484 Value *value = rhs.loadValue();
5485 storeValue(value);
5486 }
5487
5488 UInt4::UInt4(RValue<UInt2> lo, RValue<UInt2> hi)
5489 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05005490 int shuffle[4] = {0, 1, 4, 5}; // Real type is v4i32
5491 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
5492
5493 storeValue(packed);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005494 }
5495
Nicolas Capens96d4e092016-11-18 14:22:38 -05005496 RValue<UInt4> UInt4::operator=(RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005497 {
5498 storeValue(rhs.value);
5499
5500 return rhs;
5501 }
5502
Nicolas Capens96d4e092016-11-18 14:22:38 -05005503 RValue<UInt4> UInt4::operator=(const UInt4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005504 {
5505 Value *value = rhs.loadValue();
5506 storeValue(value);
5507
5508 return RValue<UInt4>(value);
5509 }
5510
Nicolas Capens96d4e092016-11-18 14:22:38 -05005511 RValue<UInt4> UInt4::operator=(const Reference<UInt4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005512 {
5513 Value *value = rhs.loadValue();
5514 storeValue(value);
5515
5516 return RValue<UInt4>(value);
5517 }
5518
5519 RValue<UInt4> operator+(RValue<UInt4> lhs, RValue<UInt4> rhs)
5520 {
5521 return RValue<UInt4>(Nucleus::createAdd(lhs.value, rhs.value));
5522 }
5523
5524 RValue<UInt4> operator-(RValue<UInt4> lhs, RValue<UInt4> rhs)
5525 {
5526 return RValue<UInt4>(Nucleus::createSub(lhs.value, rhs.value));
5527 }
5528
5529 RValue<UInt4> operator*(RValue<UInt4> lhs, RValue<UInt4> rhs)
5530 {
5531 return RValue<UInt4>(Nucleus::createMul(lhs.value, rhs.value));
5532 }
5533
5534 RValue<UInt4> operator/(RValue<UInt4> lhs, RValue<UInt4> rhs)
5535 {
5536 return RValue<UInt4>(Nucleus::createUDiv(lhs.value, rhs.value));
5537 }
5538
5539 RValue<UInt4> operator%(RValue<UInt4> lhs, RValue<UInt4> rhs)
5540 {
5541 return RValue<UInt4>(Nucleus::createURem(lhs.value, rhs.value));
5542 }
5543
5544 RValue<UInt4> operator&(RValue<UInt4> lhs, RValue<UInt4> rhs)
5545 {
5546 return RValue<UInt4>(Nucleus::createAnd(lhs.value, rhs.value));
5547 }
5548
5549 RValue<UInt4> operator|(RValue<UInt4> lhs, RValue<UInt4> rhs)
5550 {
5551 return RValue<UInt4>(Nucleus::createOr(lhs.value, rhs.value));
5552 }
5553
5554 RValue<UInt4> operator^(RValue<UInt4> lhs, RValue<UInt4> rhs)
5555 {
5556 return RValue<UInt4>(Nucleus::createXor(lhs.value, rhs.value));
5557 }
5558
5559 RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs)
5560 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005561 return RValue<UInt4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005562 }
5563
5564 RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs)
5565 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05005566 return RValue<UInt4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005567 }
5568
5569 RValue<UInt4> operator<<(RValue<UInt4> lhs, RValue<UInt4> rhs)
5570 {
5571 return RValue<UInt4>(Nucleus::createShl(lhs.value, rhs.value));
5572 }
5573
5574 RValue<UInt4> operator>>(RValue<UInt4> lhs, RValue<UInt4> rhs)
5575 {
5576 return RValue<UInt4>(Nucleus::createLShr(lhs.value, rhs.value));
5577 }
5578
Nicolas Capens96d4e092016-11-18 14:22:38 -05005579 RValue<UInt4> operator+=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005580 {
5581 return lhs = lhs + rhs;
5582 }
5583
Nicolas Capens96d4e092016-11-18 14:22:38 -05005584 RValue<UInt4> operator-=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005585 {
5586 return lhs = lhs - rhs;
5587 }
5588
Nicolas Capens96d4e092016-11-18 14:22:38 -05005589 RValue<UInt4> operator*=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005590 {
5591 return lhs = lhs * rhs;
5592 }
5593
Nicolas Capens96d4e092016-11-18 14:22:38 -05005594// RValue<UInt4> operator/=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005595// {
5596// return lhs = lhs / rhs;
5597// }
5598
Nicolas Capens96d4e092016-11-18 14:22:38 -05005599// RValue<UInt4> operator%=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005600// {
5601// return lhs = lhs % rhs;
5602// }
5603
Nicolas Capens96d4e092016-11-18 14:22:38 -05005604 RValue<UInt4> operator&=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005605 {
5606 return lhs = lhs & rhs;
5607 }
5608
Nicolas Capens96d4e092016-11-18 14:22:38 -05005609 RValue<UInt4> operator|=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005610 {
5611 return lhs = lhs | rhs;
5612 }
5613
Nicolas Capens96d4e092016-11-18 14:22:38 -05005614 RValue<UInt4> operator^=(UInt4 &lhs, RValue<UInt4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005615 {
5616 return lhs = lhs ^ rhs;
5617 }
5618
Nicolas Capens96d4e092016-11-18 14:22:38 -05005619 RValue<UInt4> operator<<=(UInt4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005620 {
5621 return lhs = lhs << rhs;
5622 }
5623
Nicolas Capens96d4e092016-11-18 14:22:38 -05005624 RValue<UInt4> operator>>=(UInt4 &lhs, unsigned char rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005625 {
5626 return lhs = lhs >> rhs;
5627 }
5628
5629 RValue<UInt4> operator+(RValue<UInt4> val)
5630 {
5631 return val;
5632 }
5633
5634 RValue<UInt4> operator-(RValue<UInt4> val)
5635 {
5636 return RValue<UInt4>(Nucleus::createNeg(val.value));
5637 }
5638
5639 RValue<UInt4> operator~(RValue<UInt4> val)
5640 {
5641 return RValue<UInt4>(Nucleus::createNot(val.value));
5642 }
5643
5644 RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y)
5645 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005646 return RValue<UInt4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005647 }
5648
5649 RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y)
5650 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005651 return RValue<UInt4>(Nucleus::createICmpULT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005652 }
5653
5654 RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y)
5655 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005656 return RValue<UInt4>(Nucleus::createICmpULE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005657 }
5658
5659 RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y)
5660 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005661 return RValue<UInt4>(Nucleus::createICmpNE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005662 }
5663
5664 RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y)
5665 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005666 return RValue<UInt4>(Nucleus::createICmpUGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005667 }
5668
5669 RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y)
5670 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05005671 return RValue<UInt4>(Nucleus::createICmpUGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005672 }
5673
5674 RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y)
5675 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005676 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5677 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value, y.value);
5678 ::basicBlock->appendInst(cmp);
5679
5680 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5681 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5682 ::basicBlock->appendInst(select);
5683
5684 return RValue<UInt4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005685 }
5686
5687 RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y)
5688 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04005689 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5690 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value);
5691 ::basicBlock->appendInst(cmp);
5692
5693 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5694 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5695 ::basicBlock->appendInst(select);
5696
5697 return RValue<UInt4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005698 }
5699
5700 RValue<UShort8> Pack(RValue<UInt4> x, RValue<UInt4> y)
5701 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05005702 if(CPUID::SSE4_1)
5703 {
5704 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
5705 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5706 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5707 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
5708 pack->addArg(x.value);
5709 pack->addArg(y.value);
5710 ::basicBlock->appendInst(pack);
Nicolas Capensec54a172016-10-25 17:32:37 -04005711
Nicolas Capens9ca48d52017-01-14 12:52:55 -05005712 return RValue<UShort8>(V(result));
5713 }
5714 else
5715 {
5716 RValue<Int4> sx = As<Int4>(x);
5717 RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000);
5718
5719 RValue<Int4> sy = As<Int4>(y);
5720 RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000);
5721
5722 return As<UShort8>(Pack(bx, by) + Short8(0x8000u));
5723 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04005724 }
5725
5726 Type *UInt4::getType()
5727 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04005728 return T(Ice::IceType_v4i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005729 }
5730
5731 Float::Float(RValue<Int> cast)
5732 {
5733 Value *integer = Nucleus::createSIToFP(cast.value, Float::getType());
5734
5735 storeValue(integer);
5736 }
5737
Alexis Hetucfd96322017-07-24 14:44:33 -04005738 Float::Float(RValue<UInt> cast)
5739 {
5740 RValue<Float> result = Float(Int(cast & UInt(0x7FFFFFFF))) +
5741 As<Float>((As<Int>(cast) >> 31) & As<Int>(Float(0x80000000u)));
5742
5743 storeValue(result.value);
5744 }
5745
Nicolas Capens598f8d82016-09-26 15:09:10 -04005746 Float::Float(float x)
5747 {
5748 storeValue(Nucleus::createConstantFloat(x));
5749 }
5750
5751 Float::Float(RValue<Float> rhs)
5752 {
5753 storeValue(rhs.value);
5754 }
5755
5756 Float::Float(const Float &rhs)
5757 {
5758 Value *value = rhs.loadValue();
5759 storeValue(value);
5760 }
5761
5762 Float::Float(const Reference<Float> &rhs)
5763 {
5764 Value *value = rhs.loadValue();
5765 storeValue(value);
5766 }
5767
Nicolas Capens96d4e092016-11-18 14:22:38 -05005768 RValue<Float> Float::operator=(RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005769 {
5770 storeValue(rhs.value);
5771
5772 return rhs;
5773 }
5774
Nicolas Capens96d4e092016-11-18 14:22:38 -05005775 RValue<Float> Float::operator=(const Float &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005776 {
5777 Value *value = rhs.loadValue();
5778 storeValue(value);
5779
5780 return RValue<Float>(value);
5781 }
5782
Nicolas Capens96d4e092016-11-18 14:22:38 -05005783 RValue<Float> Float::operator=(const Reference<Float> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005784 {
5785 Value *value = rhs.loadValue();
5786 storeValue(value);
5787
5788 return RValue<Float>(value);
5789 }
5790
5791 RValue<Float> operator+(RValue<Float> lhs, RValue<Float> rhs)
5792 {
5793 return RValue<Float>(Nucleus::createFAdd(lhs.value, rhs.value));
5794 }
5795
5796 RValue<Float> operator-(RValue<Float> lhs, RValue<Float> rhs)
5797 {
5798 return RValue<Float>(Nucleus::createFSub(lhs.value, rhs.value));
5799 }
5800
5801 RValue<Float> operator*(RValue<Float> lhs, RValue<Float> rhs)
5802 {
5803 return RValue<Float>(Nucleus::createFMul(lhs.value, rhs.value));
5804 }
5805
5806 RValue<Float> operator/(RValue<Float> lhs, RValue<Float> rhs)
5807 {
5808 return RValue<Float>(Nucleus::createFDiv(lhs.value, rhs.value));
5809 }
5810
Nicolas Capens96d4e092016-11-18 14:22:38 -05005811 RValue<Float> operator+=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005812 {
5813 return lhs = lhs + rhs;
5814 }
5815
Nicolas Capens96d4e092016-11-18 14:22:38 -05005816 RValue<Float> operator-=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005817 {
5818 return lhs = lhs - rhs;
5819 }
5820
Nicolas Capens96d4e092016-11-18 14:22:38 -05005821 RValue<Float> operator*=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005822 {
5823 return lhs = lhs * rhs;
5824 }
5825
Nicolas Capens96d4e092016-11-18 14:22:38 -05005826 RValue<Float> operator/=(Float &lhs, RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005827 {
5828 return lhs = lhs / rhs;
5829 }
5830
5831 RValue<Float> operator+(RValue<Float> val)
5832 {
5833 return val;
5834 }
5835
5836 RValue<Float> operator-(RValue<Float> val)
5837 {
5838 return RValue<Float>(Nucleus::createFNeg(val.value));
5839 }
5840
5841 RValue<Bool> operator<(RValue<Float> lhs, RValue<Float> rhs)
5842 {
5843 return RValue<Bool>(Nucleus::createFCmpOLT(lhs.value, rhs.value));
5844 }
5845
5846 RValue<Bool> operator<=(RValue<Float> lhs, RValue<Float> rhs)
5847 {
5848 return RValue<Bool>(Nucleus::createFCmpOLE(lhs.value, rhs.value));
5849 }
5850
5851 RValue<Bool> operator>(RValue<Float> lhs, RValue<Float> rhs)
5852 {
5853 return RValue<Bool>(Nucleus::createFCmpOGT(lhs.value, rhs.value));
5854 }
5855
5856 RValue<Bool> operator>=(RValue<Float> lhs, RValue<Float> rhs)
5857 {
5858 return RValue<Bool>(Nucleus::createFCmpOGE(lhs.value, rhs.value));
5859 }
5860
5861 RValue<Bool> operator!=(RValue<Float> lhs, RValue<Float> rhs)
5862 {
5863 return RValue<Bool>(Nucleus::createFCmpONE(lhs.value, rhs.value));
5864 }
5865
5866 RValue<Bool> operator==(RValue<Float> lhs, RValue<Float> rhs)
5867 {
5868 return RValue<Bool>(Nucleus::createFCmpOEQ(lhs.value, rhs.value));
5869 }
5870
5871 RValue<Float> Abs(RValue<Float> x)
5872 {
5873 return IfThenElse(x > 0.0f, x, -x);
5874 }
5875
5876 RValue<Float> Max(RValue<Float> x, RValue<Float> y)
5877 {
5878 return IfThenElse(x > y, x, y);
5879 }
5880
5881 RValue<Float> Min(RValue<Float> x, RValue<Float> y)
5882 {
5883 return IfThenElse(x < y, x, y);
5884 }
5885
5886 RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2)
5887 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04005888 return 1.0f / x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005889 }
5890
5891 RValue<Float> RcpSqrt_pp(RValue<Float> x)
5892 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04005893 return Rcp_pp(Sqrt(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005894 }
5895
5896 RValue<Float> Sqrt(RValue<Float> x)
5897 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04005898 Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32);
5899 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5900 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5901 auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5902 sqrt->addArg(x.value);
5903 ::basicBlock->appendInst(sqrt);
5904
5905 return RValue<Float>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005906 }
5907
5908 RValue<Float> Round(RValue<Float> x)
5909 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005910 return Float4(Round(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005911 }
5912
5913 RValue<Float> Trunc(RValue<Float> x)
5914 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005915 return Float4(Trunc(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005916 }
5917
5918 RValue<Float> Frac(RValue<Float> x)
5919 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005920 return Float4(Frac(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005921 }
5922
5923 RValue<Float> Floor(RValue<Float> x)
5924 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005925 return Float4(Floor(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005926 }
5927
5928 RValue<Float> Ceil(RValue<Float> x)
5929 {
Nicolas Capensa8086512016-11-07 17:32:17 -05005930 return Float4(Ceil(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04005931 }
5932
5933 Type *Float::getType()
5934 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04005935 return T(Ice::IceType_f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005936 }
5937
5938 Float2::Float2(RValue<Float4> cast)
5939 {
Nicolas Capens22008782016-10-20 01:11:47 -04005940 storeValue(Nucleus::createBitCast(cast.value, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005941 }
5942
5943 Type *Float2::getType()
5944 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04005945 return T(Type_v2f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005946 }
5947
Nicolas Capensa25311a2017-01-16 17:19:00 -05005948 Float4::Float4(RValue<Byte4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005949 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005950 Value *a = Int4(cast).loadValue();
5951 Value *xyzw = Nucleus::createSIToFP(a, Float4::getType());
5952
5953 storeValue(xyzw);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005954 }
5955
Nicolas Capensa25311a2017-01-16 17:19:00 -05005956 Float4::Float4(RValue<SByte4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005957 {
Nicolas Capensd4227962016-11-09 14:24:25 -05005958 Value *a = Int4(cast).loadValue();
5959 Value *xyzw = Nucleus::createSIToFP(a, Float4::getType());
5960
5961 storeValue(xyzw);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005962 }
5963
Nicolas Capensa25311a2017-01-16 17:19:00 -05005964 Float4::Float4(RValue<Short4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005965 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005966 Int4 c(cast);
5967 storeValue(Nucleus::createSIToFP(RValue<Int4>(c).value, Float4::getType()));
5968 }
5969
Nicolas Capensa25311a2017-01-16 17:19:00 -05005970 Float4::Float4(RValue<UShort4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005971 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005972 Int4 c(cast);
5973 storeValue(Nucleus::createSIToFP(RValue<Int4>(c).value, Float4::getType()));
5974 }
5975
Nicolas Capensa25311a2017-01-16 17:19:00 -05005976 Float4::Float4(RValue<Int4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005977 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005978 Value *xyzw = Nucleus::createSIToFP(cast.value, Float4::getType());
5979
5980 storeValue(xyzw);
5981 }
5982
Nicolas Capensa25311a2017-01-16 17:19:00 -05005983 Float4::Float4(RValue<UInt4> cast) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005984 {
Nicolas Capens96445fe2016-12-15 14:45:13 -05005985 RValue<Float4> result = Float4(Int4(cast & UInt4(0x7FFFFFFF))) +
5986 As<Float4>((As<Int4>(cast) >> 31) & As<Int4>(Float4(0x80000000u)));
Nicolas Capens598f8d82016-09-26 15:09:10 -04005987
Nicolas Capens96445fe2016-12-15 14:45:13 -05005988 storeValue(result.value);
Nicolas Capens598f8d82016-09-26 15:09:10 -04005989 }
5990
Nicolas Capensa25311a2017-01-16 17:19:00 -05005991 Float4::Float4() : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005992 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04005993 }
5994
Nicolas Capensa25311a2017-01-16 17:19:00 -05005995 Float4::Float4(float xyzw) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04005996 {
5997 constant(xyzw, xyzw, xyzw, xyzw);
5998 }
5999
Nicolas Capensa25311a2017-01-16 17:19:00 -05006000 Float4::Float4(float x, float yzw) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006001 {
6002 constant(x, yzw, yzw, yzw);
6003 }
6004
Nicolas Capensa25311a2017-01-16 17:19:00 -05006005 Float4::Float4(float x, float y, float zw) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006006 {
6007 constant(x, y, zw, zw);
6008 }
6009
Nicolas Capensa25311a2017-01-16 17:19:00 -05006010 Float4::Float4(float x, float y, float z, float w) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006011 {
6012 constant(x, y, z, w);
6013 }
6014
6015 void Float4::constant(float x, float y, float z, float w)
6016 {
Nicolas Capens13ac2322016-10-13 14:52:12 -04006017 double constantVector[4] = {x, y, z, w};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04006018 storeValue(Nucleus::createConstantVector(constantVector, getType()));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006019 }
6020
Nicolas Capensa25311a2017-01-16 17:19:00 -05006021 Float4::Float4(RValue<Float4> rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006022 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006023 storeValue(rhs.value);
6024 }
6025
Nicolas Capensa25311a2017-01-16 17:19:00 -05006026 Float4::Float4(const Float4 &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006027 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006028 Value *value = rhs.loadValue();
6029 storeValue(value);
6030 }
6031
Nicolas Capensa25311a2017-01-16 17:19:00 -05006032 Float4::Float4(const Reference<Float4> &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006033 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006034 Value *value = rhs.loadValue();
6035 storeValue(value);
6036 }
6037
Nicolas Capensa25311a2017-01-16 17:19:00 -05006038 Float4::Float4(RValue<Float> rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006039 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08006040 Value *vector = Nucleus::createBitCast(rhs.value, Float4::getType());
Nicolas Capensd4227962016-11-09 14:24:25 -05006041
6042 int swizzle[4] = {0, 0, 0, 0};
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08006043 Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle);
Nicolas Capensd4227962016-11-09 14:24:25 -05006044
6045 storeValue(replicate);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006046 }
6047
Nicolas Capensa25311a2017-01-16 17:19:00 -05006048 Float4::Float4(const Float &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006049 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006050 *this = RValue<Float>(rhs.loadValue());
6051 }
6052
Nicolas Capensa25311a2017-01-16 17:19:00 -05006053 Float4::Float4(const Reference<Float> &rhs) : FloatXYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006054 {
Nicolas Capens598f8d82016-09-26 15:09:10 -04006055 *this = RValue<Float>(rhs.loadValue());
6056 }
6057
Nicolas Capens96d4e092016-11-18 14:22:38 -05006058 RValue<Float4> Float4::operator=(float x)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006059 {
6060 return *this = Float4(x, x, x, x);
6061 }
6062
Nicolas Capens96d4e092016-11-18 14:22:38 -05006063 RValue<Float4> Float4::operator=(RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006064 {
6065 storeValue(rhs.value);
6066
6067 return rhs;
6068 }
6069
Nicolas Capens96d4e092016-11-18 14:22:38 -05006070 RValue<Float4> Float4::operator=(const Float4 &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006071 {
6072 Value *value = rhs.loadValue();
6073 storeValue(value);
6074
6075 return RValue<Float4>(value);
6076 }
6077
Nicolas Capens96d4e092016-11-18 14:22:38 -05006078 RValue<Float4> Float4::operator=(const Reference<Float4> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006079 {
6080 Value *value = rhs.loadValue();
6081 storeValue(value);
6082
6083 return RValue<Float4>(value);
6084 }
6085
Nicolas Capens96d4e092016-11-18 14:22:38 -05006086 RValue<Float4> Float4::operator=(RValue<Float> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006087 {
6088 return *this = Float4(rhs);
6089 }
6090
Nicolas Capens96d4e092016-11-18 14:22:38 -05006091 RValue<Float4> Float4::operator=(const Float &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006092 {
6093 return *this = Float4(rhs);
6094 }
6095
Nicolas Capens96d4e092016-11-18 14:22:38 -05006096 RValue<Float4> Float4::operator=(const Reference<Float> &rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006097 {
6098 return *this = Float4(rhs);
6099 }
6100
6101 RValue<Float4> operator+(RValue<Float4> lhs, RValue<Float4> rhs)
6102 {
6103 return RValue<Float4>(Nucleus::createFAdd(lhs.value, rhs.value));
6104 }
6105
6106 RValue<Float4> operator-(RValue<Float4> lhs, RValue<Float4> rhs)
6107 {
6108 return RValue<Float4>(Nucleus::createFSub(lhs.value, rhs.value));
6109 }
6110
6111 RValue<Float4> operator*(RValue<Float4> lhs, RValue<Float4> rhs)
6112 {
6113 return RValue<Float4>(Nucleus::createFMul(lhs.value, rhs.value));
6114 }
6115
6116 RValue<Float4> operator/(RValue<Float4> lhs, RValue<Float4> rhs)
6117 {
6118 return RValue<Float4>(Nucleus::createFDiv(lhs.value, rhs.value));
6119 }
6120
6121 RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs)
6122 {
6123 return RValue<Float4>(Nucleus::createFRem(lhs.value, rhs.value));
6124 }
6125
Nicolas Capens96d4e092016-11-18 14:22:38 -05006126 RValue<Float4> operator+=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006127 {
6128 return lhs = lhs + rhs;
6129 }
6130
Nicolas Capens96d4e092016-11-18 14:22:38 -05006131 RValue<Float4> operator-=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006132 {
6133 return lhs = lhs - rhs;
6134 }
6135
Nicolas Capens96d4e092016-11-18 14:22:38 -05006136 RValue<Float4> operator*=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006137 {
6138 return lhs = lhs * rhs;
6139 }
6140
Nicolas Capens96d4e092016-11-18 14:22:38 -05006141 RValue<Float4> operator/=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006142 {
6143 return lhs = lhs / rhs;
6144 }
6145
Nicolas Capens96d4e092016-11-18 14:22:38 -05006146 RValue<Float4> operator%=(Float4 &lhs, RValue<Float4> rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006147 {
6148 return lhs = lhs % rhs;
6149 }
6150
6151 RValue<Float4> operator+(RValue<Float4> val)
6152 {
6153 return val;
6154 }
6155
6156 RValue<Float4> operator-(RValue<Float4> val)
6157 {
6158 return RValue<Float4>(Nucleus::createFNeg(val.value));
6159 }
6160
6161 RValue<Float4> Abs(RValue<Float4> x)
6162 {
Nicolas Capens84272242016-11-09 13:31:06 -05006163 Value *vector = Nucleus::createBitCast(x.value, Int4::getType());
6164 int64_t constantVector[4] = {0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF};
6165 Value *result = Nucleus::createAnd(vector, V(Nucleus::createConstantVector(constantVector, Int4::getType())));
6166
6167 return As<Float4>(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006168 }
6169
6170 RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y)
6171 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006172 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05006173 auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Ogt, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006174 ::basicBlock->appendInst(cmp);
6175
6176 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05006177 auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006178 ::basicBlock->appendInst(select);
6179
6180 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006181 }
6182
6183 RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y)
6184 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006185 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05006186 auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Olt, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006187 ::basicBlock->appendInst(cmp);
6188
6189 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05006190 auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04006191 ::basicBlock->appendInst(select);
6192
6193 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006194 }
6195
6196 RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2)
6197 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04006198 return Float4(1.0f) / x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04006199 }
6200
6201 RValue<Float4> RcpSqrt_pp(RValue<Float4> x)
6202 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04006203 return Rcp_pp(Sqrt(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006204 }
6205
6206 RValue<Float4> Sqrt(RValue<Float4> x)
6207 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04006208 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6209 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6210 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6211 auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
6212 sqrt->addArg(x.value);
6213 ::basicBlock->appendInst(sqrt);
6214
6215 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006216 }
6217
Nicolas Capensc94ab742016-11-08 15:15:31 -05006218 RValue<Float4> Insert(RValue<Float4> x, RValue<Float> element, int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006219 {
Nicolas Capensc94ab742016-11-08 15:15:31 -05006220 return RValue<Float4>(Nucleus::createInsertElement(x.value, element.value, i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006221 }
6222
6223 RValue<Float> Extract(RValue<Float4> x, int i)
6224 {
Nicolas Capense95d5342016-09-30 11:37:28 -04006225 return RValue<Float>(Nucleus::createExtractElement(x.value, Float::getType(), i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006226 }
6227
6228 RValue<Float4> Swizzle(RValue<Float4> x, unsigned char select)
6229 {
Nicolas Capense95d5342016-09-30 11:37:28 -04006230 return RValue<Float4>(createSwizzle4(x.value, select));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006231 }
6232
6233 RValue<Float4> ShuffleLowHigh(RValue<Float4> x, RValue<Float4> y, unsigned char imm)
6234 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04006235 int shuffle[4] =
6236 {
6237 ((imm >> 0) & 0x03) + 0,
6238 ((imm >> 2) & 0x03) + 0,
6239 ((imm >> 4) & 0x03) + 4,
6240 ((imm >> 6) & 0x03) + 4,
6241 };
6242
6243 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006244 }
6245
6246 RValue<Float4> UnpackLow(RValue<Float4> x, RValue<Float4> y)
6247 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04006248 int shuffle[4] = {0, 4, 1, 5};
6249 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006250 }
6251
6252 RValue<Float4> UnpackHigh(RValue<Float4> x, RValue<Float4> y)
6253 {
Nicolas Capens37fbece2016-10-21 15:08:56 -04006254 int shuffle[4] = {2, 6, 3, 7};
6255 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006256 }
6257
6258 RValue<Float4> Mask(Float4 &lhs, RValue<Float4> rhs, unsigned char select)
6259 {
6260 Value *vector = lhs.loadValue();
Nicolas Capensa4c30b02016-11-08 15:43:17 -05006261 Value *result = createMask4(vector, rhs.value, select);
6262 lhs.storeValue(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006263
Nicolas Capensa4c30b02016-11-08 15:43:17 -05006264 return RValue<Float4>(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006265 }
6266
6267 RValue<Int> SignMask(RValue<Float4> x)
6268 {
Nicolas Capensf2cb9df2016-10-21 17:26:13 -04006269 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
6270 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6271 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6272 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
6273 movmsk->addArg(x.value);
6274 ::basicBlock->appendInst(movmsk);
6275
6276 return RValue<Int>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006277 }
6278
6279 RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y)
6280 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006281 return RValue<Int4>(Nucleus::createFCmpOEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006282 }
6283
6284 RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y)
6285 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006286 return RValue<Int4>(Nucleus::createFCmpOLT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006287 }
6288
6289 RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y)
6290 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006291 return RValue<Int4>(Nucleus::createFCmpOLE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006292 }
6293
6294 RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y)
6295 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006296 return RValue<Int4>(Nucleus::createFCmpONE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006297 }
6298
6299 RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y)
6300 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006301 return RValue<Int4>(Nucleus::createFCmpOGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006302 }
6303
6304 RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y)
6305 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05006306 return RValue<Int4>(Nucleus::createFCmpOGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006307 }
6308
6309 RValue<Float4> Round(RValue<Float4> x)
6310 {
Nicolas Capensf7b75882017-04-26 09:30:47 -04006311 if(emulateIntrinsics)
6312 {
6313 // Push the fractional part off the mantissa. Accurate up to +/-2^22.
6314 return (x + Float4(0x00C00000)) - Float4(0x00C00000);
6315 }
6316 else if(CPUID::SSE4_1)
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006317 {
6318 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6319 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6320 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6321 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6322 round->addArg(x.value);
6323 round->addArg(::context->getConstantInt32(0));
6324 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006325
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006326 return RValue<Float4>(V(result));
6327 }
6328 else
6329 {
6330 return Float4(RoundInt(x));
6331 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006332 }
6333
6334 RValue<Float4> Trunc(RValue<Float4> x)
6335 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006336 if(CPUID::SSE4_1)
6337 {
6338 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6339 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6340 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6341 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6342 round->addArg(x.value);
6343 round->addArg(::context->getConstantInt32(3));
6344 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006345
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006346 return RValue<Float4>(V(result));
6347 }
6348 else
6349 {
6350 return Float4(Int4(x));
6351 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006352 }
6353
6354 RValue<Float4> Frac(RValue<Float4> x)
6355 {
Nicolas Capensb9230422017-07-17 10:27:33 -04006356 Float4 frc;
6357
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006358 if(CPUID::SSE4_1)
6359 {
Nicolas Capensb9230422017-07-17 10:27:33 -04006360 frc = x - Floor(x);
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006361 }
6362 else
6363 {
Nicolas Capensb9230422017-07-17 10:27:33 -04006364 frc = x - Float4(Int4(x)); // Signed fractional part.
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006365
Nicolas Capensb9230422017-07-17 10:27:33 -04006366 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 -05006367 }
Nicolas Capensb9230422017-07-17 10:27:33 -04006368
6369 // x - floor(x) can be 1.0 for very small negative x.
6370 // Clamp against the value just below 1.0.
6371 return Min(frc, As<Float4>(Int4(0x3F7FFFFF)));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006372 }
6373
6374 RValue<Float4> Floor(RValue<Float4> x)
6375 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006376 if(CPUID::SSE4_1)
6377 {
6378 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6379 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6380 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6381 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6382 round->addArg(x.value);
6383 round->addArg(::context->getConstantInt32(1));
6384 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006385
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006386 return RValue<Float4>(V(result));
6387 }
6388 else
6389 {
6390 return x - Frac(x);
6391 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006392 }
6393
6394 RValue<Float4> Ceil(RValue<Float4> x)
6395 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006396 if(CPUID::SSE4_1)
6397 {
6398 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6399 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6400 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6401 auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6402 round->addArg(x.value);
6403 round->addArg(::context->getConstantInt32(2));
6404 ::basicBlock->appendInst(round);
Nicolas Capensa8086512016-11-07 17:32:17 -05006405
Nicolas Capens9ca48d52017-01-14 12:52:55 -05006406 return RValue<Float4>(V(result));
6407 }
6408 else
6409 {
6410 return -Floor(-x);
6411 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04006412 }
6413
6414 Type *Float4::getType()
6415 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04006416 return T(Ice::IceType_v4f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006417 }
6418
6419 RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, int offset)
6420 {
Nicolas Capens8820f642016-09-30 04:42:43 -04006421 return lhs + RValue<Int>(Nucleus::createConstantInt(offset));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006422 }
6423
6424 RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<Int> offset)
6425 {
Nicolas Capensd294def2017-01-26 17:44:37 -08006426 return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, false));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006427 }
6428
6429 RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<UInt> offset)
6430 {
Nicolas Capensd294def2017-01-26 17:44:37 -08006431 return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, true));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006432 }
6433
Nicolas Capens96d4e092016-11-18 14:22:38 -05006434 RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, int offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006435 {
6436 return lhs = lhs + offset;
6437 }
6438
Nicolas Capens96d4e092016-11-18 14:22:38 -05006439 RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, RValue<Int> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006440 {
6441 return lhs = lhs + offset;
6442 }
6443
Nicolas Capens96d4e092016-11-18 14:22:38 -05006444 RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, RValue<UInt> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006445 {
6446 return lhs = lhs + offset;
6447 }
6448
6449 RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, int offset)
6450 {
6451 return lhs + -offset;
6452 }
6453
6454 RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, RValue<Int> offset)
6455 {
6456 return lhs + -offset;
6457 }
6458
6459 RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, RValue<UInt> offset)
6460 {
6461 return lhs + -offset;
6462 }
6463
Nicolas Capens96d4e092016-11-18 14:22:38 -05006464 RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, int offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006465 {
6466 return lhs = lhs - offset;
6467 }
6468
Nicolas Capens96d4e092016-11-18 14:22:38 -05006469 RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, RValue<Int> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006470 {
6471 return lhs = lhs - offset;
6472 }
6473
Nicolas Capens96d4e092016-11-18 14:22:38 -05006474 RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, RValue<UInt> offset)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006475 {
6476 return lhs = lhs - offset;
6477 }
6478
6479 void Return()
6480 {
6481 Nucleus::createRetVoid();
6482 Nucleus::setInsertBlock(Nucleus::createBasicBlock());
6483 Nucleus::createUnreachable();
6484 }
6485
Nicolas Capenseb253d02016-11-18 14:40:40 -05006486 void Return(RValue<Int> ret)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006487 {
Nicolas Capenseb253d02016-11-18 14:40:40 -05006488 Nucleus::createRet(ret.value);
Nicolas Capensfdcca2d2016-10-20 11:31:36 -04006489 Nucleus::setInsertBlock(Nucleus::createBasicBlock());
6490 Nucleus::createUnreachable();
Nicolas Capens598f8d82016-09-26 15:09:10 -04006491 }
6492
Nicolas Capensf4eec2f2017-05-24 15:46:48 -04006493 void branch(RValue<Bool> cmp, BasicBlock *bodyBB, BasicBlock *endBB)
Nicolas Capens598f8d82016-09-26 15:09:10 -04006494 {
6495 Nucleus::createCondBr(cmp.value, bodyBB, endBB);
6496 Nucleus::setInsertBlock(bodyBB);
Nicolas Capens598f8d82016-09-26 15:09:10 -04006497 }
6498
Nicolas Capens598f8d82016-09-26 15:09:10 -04006499 RValue<Long> Ticks()
6500 {
Nicolas Capensc37252c2016-09-28 16:11:54 -04006501 assert(false && "UNIMPLEMENTED"); return RValue<Long>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04006502 }
6503}