// Copyright 2016 The SwiftShader Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "Reactor.hpp" #include "x86.hpp" #include "CPUID.hpp" #include "Thread.hpp" #include "ExecutableMemory.hpp" #include "MutexLock.hpp" #undef min #undef max #if REACTOR_LLVM_VERSION < 7 #include "llvm/Analysis/LoopPass.h" #include "llvm/Constants.h" #include "llvm/Function.h" #include "llvm/GlobalVariable.h" #include "llvm/Intrinsics.h" #include "llvm/LLVMContext.h" #include "llvm/Module.h" #include "llvm/PassManager.h" #include "llvm/Support/IRBuilder.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Target/TargetData.h" #include "llvm/Target/TargetOptions.h" #include "llvm/Transforms/Scalar.h" #include "../lib/ExecutionEngine/JIT/JIT.h" #include "LLVMRoutine.hpp" #include "LLVMRoutineManager.hpp" #define ARGS(...) __VA_ARGS__ #else #include "llvm/Analysis/LoopPass.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/ExecutionEngine/JITSymbol.h" #include "llvm/ExecutionEngine/Orc/CompileUtils.h" #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" #include "llvm/ExecutionEngine/SectionMemoryManager.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Function.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Intrinsics.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" #include "llvm/Support/Error.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Target/TargetOptions.h" #include "llvm/Transforms/InstCombine/InstCombine.h" #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar/GVN.h" #include "LLVMRoutine.hpp" #define ARGS(...) {__VA_ARGS__} #define CreateCall2 CreateCall #define CreateCall3 CreateCall #include #endif #include #include #if defined(__i386__) || defined(__x86_64__) #include #endif #include #if defined(__x86_64__) && defined(_WIN32) extern "C" void X86CompilationCallback() { assert(false); // UNIMPLEMENTED } #endif #if REACTOR_LLVM_VERSION < 7 namespace llvm { extern bool JITEmitDebugInfo; } #endif namespace rr { class LLVMReactorJIT; } namespace { rr::LLVMReactorJIT *reactorJIT = nullptr; llvm::IRBuilder<> *builder = nullptr; llvm::LLVMContext *context = nullptr; llvm::Module *module = nullptr; llvm::Function *function = nullptr; rr::MutexLock codegenMutex; #if REACTOR_LLVM_VERSION >= 7 llvm::Value *lowerPAVG(llvm::Value *x, llvm::Value *y) { llvm::VectorType *ty = llvm::cast(x->getType()); llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); x = ::builder->CreateZExt(x, extTy); y = ::builder->CreateZExt(y, extTy); // (x + y + 1) >> 1 llvm::Constant *one = llvm::ConstantInt::get(extTy, 1); llvm::Value *res = ::builder->CreateAdd(x, y); res = ::builder->CreateAdd(res, one); res = ::builder->CreateLShr(res, one); return ::builder->CreateTrunc(res, ty); } llvm::Value *lowerPMINMAX(llvm::Value *x, llvm::Value *y, llvm::ICmpInst::Predicate pred) { return ::builder->CreateSelect(::builder->CreateICmp(pred, x, y), x, y); } llvm::Value *lowerPCMP(llvm::ICmpInst::Predicate pred, llvm::Value *x, llvm::Value *y, llvm::Type *dstTy) { return ::builder->CreateSExt(::builder->CreateICmp(pred, x, y), dstTy, ""); } #if defined(__i386__) || defined(__x86_64__) llvm::Value *lowerPMOV(llvm::Value *op, llvm::Type *dstType, bool sext) { llvm::VectorType *srcTy = llvm::cast(op->getType()); llvm::VectorType *dstTy = llvm::cast(dstType); llvm::Value *undef = llvm::UndefValue::get(srcTy); llvm::SmallVector mask(dstTy->getNumElements()); std::iota(mask.begin(), mask.end(), 0); llvm::Value *v = ::builder->CreateShuffleVector(op, undef, mask); return sext ? ::builder->CreateSExt(v, dstTy) : ::builder->CreateZExt(v, dstTy); } llvm::Value *lowerPABS(llvm::Value *v) { llvm::Value *zero = llvm::Constant::getNullValue(v->getType()); llvm::Value *cmp = ::builder->CreateICmp(llvm::ICmpInst::ICMP_SGT, v, zero); llvm::Value *neg = ::builder->CreateNeg(v); return ::builder->CreateSelect(cmp, v, neg); } #endif // defined(__i386__) || defined(__x86_64__) #if !defined(__i386__) && !defined(__x86_64__) llvm::Value *lowerPFMINMAX(llvm::Value *x, llvm::Value *y, llvm::FCmpInst::Predicate pred) { return ::builder->CreateSelect(::builder->CreateFCmp(pred, x, y), x, y); } llvm::Value *lowerRound(llvm::Value *x) { llvm::Function *nearbyint = llvm::Intrinsic::getDeclaration( ::module, llvm::Intrinsic::nearbyint, {x->getType()}); return ::builder->CreateCall(nearbyint, ARGS(x)); } llvm::Value *lowerRoundInt(llvm::Value *x, llvm::Type *ty) { return ::builder->CreateFPToSI(lowerRound(x), ty); } llvm::Value *lowerFloor(llvm::Value *x) { llvm::Function *floor = llvm::Intrinsic::getDeclaration( ::module, llvm::Intrinsic::floor, {x->getType()}); return ::builder->CreateCall(floor, ARGS(x)); } llvm::Value *lowerTrunc(llvm::Value *x) { llvm::Function *trunc = llvm::Intrinsic::getDeclaration( ::module, llvm::Intrinsic::trunc, {x->getType()}); return ::builder->CreateCall(trunc, ARGS(x)); } // Packed add/sub saturatation llvm::Value *lowerPSAT(llvm::Value *x, llvm::Value *y, bool isAdd, bool isSigned) { llvm::VectorType *ty = llvm::cast(x->getType()); llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); unsigned numBits = ty->getScalarSizeInBits(); llvm::Value *max, *min, *extX, *extY; if (isSigned) { max = llvm::ConstantInt::get(extTy, (1LL << (numBits - 1)) - 1, true); min = llvm::ConstantInt::get(extTy, (-1LL << (numBits - 1)), true); extX = ::builder->CreateSExt(x, extTy); extY = ::builder->CreateSExt(y, extTy); } else { assert(numBits <= 64); uint64_t maxVal = (numBits == 64) ? ~0ULL : (1ULL << numBits) - 1; max = llvm::ConstantInt::get(extTy, maxVal, false); min = llvm::ConstantInt::get(extTy, 0, false); extX = ::builder->CreateZExt(x, extTy); extY = ::builder->CreateZExt(y, extTy); } llvm::Value *res = isAdd ? ::builder->CreateAdd(extX, extY) : ::builder->CreateSub(extX, extY); res = lowerPMINMAX(res, min, llvm::ICmpInst::ICMP_SGT); res = lowerPMINMAX(res, max, llvm::ICmpInst::ICMP_SLT); return ::builder->CreateTrunc(res, ty); } llvm::Value *lowerPUADDSAT(llvm::Value *x, llvm::Value *y) { return lowerPSAT(x, y, true, false); } llvm::Value *lowerPSADDSAT(llvm::Value *x, llvm::Value *y) { return lowerPSAT(x, y, true, true); } llvm::Value *lowerPUSUBSAT(llvm::Value *x, llvm::Value *y) { return lowerPSAT(x, y, false, false); } llvm::Value *lowerPSSUBSAT(llvm::Value *x, llvm::Value *y) { return lowerPSAT(x, y, false, true); } llvm::Value *lowerSQRT(llvm::Value *x) { llvm::Function *sqrt = llvm::Intrinsic::getDeclaration( ::module, llvm::Intrinsic::sqrt, {x->getType()}); return ::builder->CreateCall(sqrt, ARGS(x)); } llvm::Value *lowerRCP(llvm::Value *x) { llvm::Type *ty = x->getType(); llvm::Constant *one; if (llvm::VectorType *vectorTy = llvm::dyn_cast(ty)) { one = llvm::ConstantVector::getSplat( vectorTy->getNumElements(), llvm::ConstantFP::get(vectorTy->getElementType(), 1)); } else { one = llvm::ConstantFP::get(ty, 1); } return ::builder->CreateFDiv(one, x); } llvm::Value *lowerRSQRT(llvm::Value *x) { return lowerRCP(lowerSQRT(x)); } llvm::Value *lowerVectorShl(llvm::Value *x, uint64_t scalarY) { llvm::VectorType *ty = llvm::cast(x->getType()); llvm::Value *y = llvm::ConstantVector::getSplat( ty->getNumElements(), llvm::ConstantInt::get(ty->getElementType(), scalarY)); return ::builder->CreateShl(x, y); } llvm::Value *lowerVectorAShr(llvm::Value *x, uint64_t scalarY) { llvm::VectorType *ty = llvm::cast(x->getType()); llvm::Value *y = llvm::ConstantVector::getSplat( ty->getNumElements(), llvm::ConstantInt::get(ty->getElementType(), scalarY)); return ::builder->CreateAShr(x, y); } llvm::Value *lowerVectorLShr(llvm::Value *x, uint64_t scalarY) { llvm::VectorType *ty = llvm::cast(x->getType()); llvm::Value *y = llvm::ConstantVector::getSplat( ty->getNumElements(), llvm::ConstantInt::get(ty->getElementType(), scalarY)); return ::builder->CreateLShr(x, y); } llvm::Value *lowerMulAdd(llvm::Value *x, llvm::Value *y) { llvm::VectorType *ty = llvm::cast(x->getType()); llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); llvm::Value *extX = ::builder->CreateSExt(x, extTy); llvm::Value *extY = ::builder->CreateSExt(y, extTy); llvm::Value *mult = ::builder->CreateMul(extX, extY); llvm::Value *undef = llvm::UndefValue::get(extTy); llvm::SmallVector evenIdx; llvm::SmallVector oddIdx; for (uint64_t i = 0, n = ty->getNumElements(); i < n; i += 2) { evenIdx.push_back(i); oddIdx.push_back(i + 1); } llvm::Value *lhs = ::builder->CreateShuffleVector(mult, undef, evenIdx); llvm::Value *rhs = ::builder->CreateShuffleVector(mult, undef, oddIdx); return ::builder->CreateAdd(lhs, rhs); } llvm::Value *lowerMulHigh(llvm::Value *x, llvm::Value *y, bool sext) { llvm::VectorType *ty = llvm::cast(x->getType()); llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); llvm::Value *extX, *extY; if (sext) { extX = ::builder->CreateSExt(x, extTy); extY = ::builder->CreateSExt(y, extTy); } else { extX = ::builder->CreateZExt(x, extTy); extY = ::builder->CreateZExt(y, extTy); } llvm::Value *mult = ::builder->CreateMul(extX, extY); llvm::IntegerType *intTy = llvm::cast(ty->getElementType()); llvm::Value *mulh = ::builder->CreateAShr(mult, intTy->getIntegerBitWidth()); return ::builder->CreateTrunc(mulh, ty); } llvm::Value *lowerPack(llvm::Value *x, llvm::Value *y, bool isSigned) { llvm::VectorType *srcTy = llvm::cast(x->getType()); llvm::VectorType *dstTy = llvm::VectorType::getTruncatedElementVectorType(srcTy); llvm::IntegerType *dstElemTy = llvm::cast(dstTy->getElementType()); uint64_t truncNumBits = dstElemTy->getIntegerBitWidth(); assert(truncNumBits < 64 && "shift 64 must be handled separately"); llvm::Constant *max, *min; if (isSigned) { max = llvm::ConstantInt::get(srcTy, (1LL << (truncNumBits - 1)) - 1, true); min = llvm::ConstantInt::get(srcTy, (-1LL << (truncNumBits - 1)), true); } else { max = llvm::ConstantInt::get(srcTy, (1ULL << truncNumBits) - 1, false); min = llvm::ConstantInt::get(srcTy, 0, false); } x = lowerPMINMAX(x, min, llvm::ICmpInst::ICMP_SGT); x = lowerPMINMAX(x, max, llvm::ICmpInst::ICMP_SLT); y = lowerPMINMAX(y, min, llvm::ICmpInst::ICMP_SGT); y = lowerPMINMAX(y, max, llvm::ICmpInst::ICMP_SLT); x = ::builder->CreateTrunc(x, dstTy); y = ::builder->CreateTrunc(y, dstTy); llvm::SmallVector index(srcTy->getNumElements() * 2); std::iota(index.begin(), index.end(), 0); return ::builder->CreateShuffleVector(x, y, index); } llvm::Value *lowerSignMask(llvm::Value *x, llvm::Type *retTy) { llvm::VectorType *ty = llvm::cast(x->getType()); llvm::Constant *zero = llvm::ConstantInt::get(ty, 0); llvm::Value *cmp = ::builder->CreateICmpSLT(x, zero); llvm::Value *ret = ::builder->CreateZExt( ::builder->CreateExtractElement(cmp, static_cast(0)), retTy); for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i) { llvm::Value *elem = ::builder->CreateZExt( ::builder->CreateExtractElement(cmp, i), retTy); ret = ::builder->CreateOr(ret, ::builder->CreateShl(elem, i)); } return ret; } llvm::Value *lowerFPSignMask(llvm::Value *x, llvm::Type *retTy) { llvm::VectorType *ty = llvm::cast(x->getType()); llvm::Constant *zero = llvm::ConstantFP::get(ty, 0); llvm::Value *cmp = ::builder->CreateFCmpULT(x, zero); llvm::Value *ret = ::builder->CreateZExt( ::builder->CreateExtractElement(cmp, static_cast(0)), retTy); for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i) { llvm::Value *elem = ::builder->CreateZExt( ::builder->CreateExtractElement(cmp, i), retTy); ret = ::builder->CreateOr(ret, ::builder->CreateShl(elem, i)); } return ret; } #endif // !defined(__i386__) && !defined(__x86_64__) #endif // REACTOR_LLVM_VERSION >= 7 } namespace rr { #if REACTOR_LLVM_VERSION < 7 class LLVMReactorJIT { private: std::string arch; llvm::SmallVector mattrs; llvm::ExecutionEngine *executionEngine; LLVMRoutineManager *routineManager; public: LLVMReactorJIT(const std::string &arch_, const llvm::SmallVectorImpl &mattrs_) : arch(arch_), mattrs(mattrs_.begin(), mattrs_.end()), executionEngine(nullptr), routineManager(nullptr) { } void startSession() { std::string error; ::module = new llvm::Module("", *::context); routineManager = new LLVMRoutineManager(); llvm::TargetMachine *targetMachine = llvm::EngineBuilder::selectTarget( ::module, arch, "", mattrs, llvm::Reloc::Default, llvm::CodeModel::JITDefault, &error); executionEngine = llvm::JIT::createJIT( ::module, &error, routineManager, llvm::CodeGenOpt::Aggressive, true, targetMachine); } void endSession() { delete executionEngine; executionEngine = nullptr; routineManager = nullptr; ::function = nullptr; ::module = nullptr; } LLVMRoutine *acquireRoutine(llvm::Function *func) { void *entry = executionEngine->getPointerToFunction(::function); return routineManager->acquireRoutine(entry); } void optimize(llvm::Module *module) { static llvm::PassManager *passManager = nullptr; if(!passManager) { passManager = new llvm::PassManager(); passManager->add(new llvm::TargetData(*executionEngine->getTargetData())); passManager->add(llvm::createScalarReplAggregatesPass()); for(int pass = 0; pass < 10 && optimization[pass] != Disabled; pass++) { switch(optimization[pass]) { case Disabled: break; case CFGSimplification: passManager->add(llvm::createCFGSimplificationPass()); break; case LICM: passManager->add(llvm::createLICMPass()); break; case AggressiveDCE: passManager->add(llvm::createAggressiveDCEPass()); break; case GVN: passManager->add(llvm::createGVNPass()); break; case InstructionCombining: passManager->add(llvm::createInstructionCombiningPass()); break; case Reassociate: passManager->add(llvm::createReassociatePass()); break; case DeadStoreElimination: passManager->add(llvm::createDeadStoreEliminationPass()); break; case SCCP: passManager->add(llvm::createSCCPPass()); break; case ScalarReplAggregates: passManager->add(llvm::createScalarReplAggregatesPass()); break; default: assert(false); } } } passManager->run(*::module); } }; #else class ExternalFunctionSymbolResolver { private: using FunctionMap = std::unordered_map; FunctionMap func_; public: ExternalFunctionSymbolResolver() { func_.emplace("floorf", reinterpret_cast(floorf)); func_.emplace("nearbyintf", reinterpret_cast(nearbyintf)); func_.emplace("truncf", reinterpret_cast(truncf)); } void *findSymbol(const std::string &name) const { FunctionMap::const_iterator it = func_.find(name); return (it != func_.end()) ? it->second : nullptr; } }; class LLVMReactorJIT { private: using ObjLayer = llvm::orc::RTDyldObjectLinkingLayer; using CompileLayer = llvm::orc::IRCompileLayer; llvm::orc::ExecutionSession session; ExternalFunctionSymbolResolver externalSymbolResolver; std::shared_ptr resolver; std::unique_ptr targetMachine; const llvm::DataLayout dataLayout; ObjLayer objLayer; CompileLayer compileLayer; size_t emittedFunctionsNum; public: LLVMReactorJIT(const char *arch, const llvm::SmallVectorImpl& mattrs, const llvm::TargetOptions &targetOpts): resolver(createLegacyLookupResolver( session, [this](const std::string &name) { void *func = externalSymbolResolver.findSymbol(name); if (func != nullptr) { return llvm::JITSymbol( reinterpret_cast(func), llvm::JITSymbolFlags::Absolute); } return objLayer.findSymbol(name, true); }, [](llvm::Error err) { if (err) { // TODO: Log the symbol resolution errors. return; } })), targetMachine(llvm::EngineBuilder() .setMArch(arch) .setMAttrs(mattrs) .setTargetOptions(targetOpts) .selectTarget()), dataLayout(targetMachine->createDataLayout()), objLayer( session, [this](llvm::orc::VModuleKey) { return ObjLayer::Resources{ std::make_shared(), resolver}; }), compileLayer(objLayer, llvm::orc::SimpleCompiler(*targetMachine)), emittedFunctionsNum(0) { } void startSession() { ::module = new llvm::Module("", *::context); } void endSession() { ::function = nullptr; ::module = nullptr; } LLVMRoutine *acquireRoutine(llvm::Function *func) { std::string name = "f" + llvm::Twine(emittedFunctionsNum++).str(); func->setName(name); func->setLinkage(llvm::GlobalValue::ExternalLinkage); func->setDoesNotThrow(); std::unique_ptr mod(::module); ::module = nullptr; mod->setDataLayout(dataLayout); auto moduleKey = session.allocateVModule(); llvm::cantFail(compileLayer.addModule(moduleKey, std::move(mod))); std::string mangledName; { llvm::raw_string_ostream mangledNameStream(mangledName); llvm::Mangler::getNameWithPrefix(mangledNameStream, name, dataLayout); } llvm::JITSymbol symbol = compileLayer.findSymbolIn(moduleKey, mangledName, false); llvm::Expected expectAddr = symbol.getAddress(); if(!expectAddr) { return nullptr; } void *addr = reinterpret_cast(static_cast(expectAddr.get())); return new LLVMRoutine(addr, releaseRoutineCallback, this, moduleKey); } void optimize(llvm::Module *module) { std::unique_ptr passManager( new llvm::legacy::PassManager()); passManager->add(llvm::createSROAPass()); for(int pass = 0; pass < 10 && optimization[pass] != Disabled; pass++) { switch(optimization[pass]) { case Disabled: break; case CFGSimplification: passManager->add(llvm::createCFGSimplificationPass()); break; case LICM: passManager->add(llvm::createLICMPass()); break; case AggressiveDCE: passManager->add(llvm::createAggressiveDCEPass()); break; case GVN: passManager->add(llvm::createGVNPass()); break; case InstructionCombining: passManager->add(llvm::createInstructionCombiningPass()); break; case Reassociate: passManager->add(llvm::createReassociatePass()); break; case DeadStoreElimination: passManager->add(llvm::createDeadStoreEliminationPass()); break; case SCCP: passManager->add(llvm::createSCCPPass()); break; case ScalarReplAggregates: passManager->add(llvm::createSROAPass()); break; default: assert(false); } } passManager->run(*::module); } private: void releaseRoutineModule(llvm::orc::VModuleKey moduleKey) { llvm::cantFail(compileLayer.removeModule(moduleKey)); } static void releaseRoutineCallback(LLVMReactorJIT *jit, uint64_t moduleKey) { jit->releaseRoutineModule(moduleKey); } }; #endif Optimization optimization[10] = {InstructionCombining, Disabled}; enum EmulatedType { Type_v2i32, Type_v4i16, Type_v2i16, Type_v8i8, Type_v4i8, Type_v2f32, EmulatedTypeCount }; llvm::Type *T(Type *t) { uintptr_t type = reinterpret_cast(t); if(type < EmulatedTypeCount) { // Use 128-bit vectors to implement logically shorter ones. switch(type) { case Type_v2i32: return T(Int4::getType()); case Type_v4i16: return T(Short8::getType()); case Type_v2i16: return T(Short8::getType()); case Type_v8i8: return T(Byte16::getType()); case Type_v4i8: return T(Byte16::getType()); case Type_v2f32: return T(Float4::getType()); default: assert(false); } } return reinterpret_cast(t); } inline Type *T(llvm::Type *t) { return reinterpret_cast(t); } Type *T(EmulatedType t) { return reinterpret_cast(t); } inline llvm::Value *V(Value *t) { return reinterpret_cast(t); } inline Value *V(llvm::Value *t) { return reinterpret_cast(t); } inline std::vector &T(std::vector &t) { return reinterpret_cast&>(t); } inline llvm::BasicBlock *B(BasicBlock *t) { return reinterpret_cast(t); } inline BasicBlock *B(llvm::BasicBlock *t) { return reinterpret_cast(t); } static size_t typeSize(Type *type) { uintptr_t t = reinterpret_cast(type); if(t < EmulatedTypeCount) { switch(t) { case Type_v2i32: return 8; case Type_v4i16: return 8; case Type_v2i16: return 4; case Type_v8i8: return 8; case Type_v4i8: return 4; case Type_v2f32: return 8; default: assert(false); } } return T(type)->getPrimitiveSizeInBits() / 8; } static unsigned int elementCount(Type *type) { uintptr_t t = reinterpret_cast(type); if(t < EmulatedTypeCount) { switch(t) { case Type_v2i32: return 2; case Type_v4i16: return 4; case Type_v2i16: return 2; case Type_v8i8: return 8; case Type_v4i8: return 4; case Type_v2f32: return 2; default: assert(false); } } return llvm::cast(T(type))->getNumElements(); } Nucleus::Nucleus() { ::codegenMutex.lock(); // Reactor and LLVM are currently not thread safe llvm::InitializeNativeTarget(); #if REACTOR_LLVM_VERSION >= 7 llvm::InitializeNativeTargetAsmPrinter(); llvm::InitializeNativeTargetAsmParser(); #endif if(!::context) { ::context = new llvm::LLVMContext(); } #if defined(__x86_64__) static const char arch[] = "x86-64"; #elif defined(__i386__) static const char arch[] = "x86"; #elif defined(__aarch64__) static const char arch[] = "arm64"; #elif defined(__arm__) static const char arch[] = "arm"; #elif defined(__mips__) #if defined(__mips64) static const char arch[] = "mips64el"; #else static const char arch[] = "mipsel"; #endif #else #error "unknown architecture" #endif llvm::SmallVector mattrs; #if defined(__i386__) || defined(__x86_64__) mattrs.push_back(CPUID::supportsMMX() ? "+mmx" : "-mmx"); mattrs.push_back(CPUID::supportsCMOV() ? "+cmov" : "-cmov"); mattrs.push_back(CPUID::supportsSSE() ? "+sse" : "-sse"); mattrs.push_back(CPUID::supportsSSE2() ? "+sse2" : "-sse2"); mattrs.push_back(CPUID::supportsSSE3() ? "+sse3" : "-sse3"); mattrs.push_back(CPUID::supportsSSSE3() ? "+ssse3" : "-ssse3"); #if REACTOR_LLVM_VERSION < 7 mattrs.push_back(CPUID::supportsSSE4_1() ? "+sse41" : "-sse41"); #else mattrs.push_back(CPUID::supportsSSE4_1() ? "+sse4.1" : "-sse4.1"); #endif #elif defined(__arm__) #if __ARM_ARCH >= 8 mattrs.push_back("+armv8-a"); #else // armv7-a requires compiler-rt routines; otherwise, compiled kernel // might fail to link. #endif #endif #if REACTOR_LLVM_VERSION < 7 llvm::JITEmitDebugInfo = false; llvm::UnsafeFPMath = true; // llvm::NoInfsFPMath = true; // llvm::NoNaNsFPMath = true; #else llvm::TargetOptions targetOpts; targetOpts.UnsafeFPMath = false; // targetOpts.NoInfsFPMath = true; // targetOpts.NoNaNsFPMath = true; #endif if(!::reactorJIT) { #if REACTOR_LLVM_VERSION < 7 ::reactorJIT = new LLVMReactorJIT(arch, mattrs); #else ::reactorJIT = new LLVMReactorJIT(arch, mattrs, targetOpts); #endif } ::reactorJIT->startSession(); if(!::builder) { ::builder = new llvm::IRBuilder<>(*::context); } } Nucleus::~Nucleus() { ::reactorJIT->endSession(); ::codegenMutex.unlock(); } Routine *Nucleus::acquireRoutine(const char *name, bool runOptimizations) { if(::builder->GetInsertBlock()->empty() || !::builder->GetInsertBlock()->back().isTerminator()) { llvm::Type *type = ::function->getReturnType(); if(type->isVoidTy()) { createRetVoid(); } else { createRet(V(llvm::UndefValue::get(type))); } } if(false) { #if REACTOR_LLVM_VERSION < 7 std::string error; llvm::raw_fd_ostream file((std::string(name) + "-llvm-dump-unopt.txt").c_str(), error); #else std::error_code error; llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-unopt.txt", error); #endif ::module->print(file, 0); } if(runOptimizations) { optimize(); } if(false) { #if REACTOR_LLVM_VERSION < 7 std::string error; llvm::raw_fd_ostream file((std::string(name) + "-llvm-dump-opt.txt").c_str(), error); #else std::error_code error; llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error); #endif ::module->print(file, 0); } LLVMRoutine *routine = ::reactorJIT->acquireRoutine(::function); return routine; } void Nucleus::optimize() { ::reactorJIT->optimize(::module); } Value *Nucleus::allocateStackVariable(Type *type, int arraySize) { // Need to allocate it in the entry block for mem2reg to work llvm::BasicBlock &entryBlock = ::function->getEntryBlock(); llvm::Instruction *declaration; if(arraySize) { #if REACTOR_LLVM_VERSION < 7 declaration = new llvm::AllocaInst(T(type), V(Nucleus::createConstantInt(arraySize))); #else declaration = new llvm::AllocaInst(T(type), 0, V(Nucleus::createConstantInt(arraySize))); #endif } else { #if REACTOR_LLVM_VERSION < 7 declaration = new llvm::AllocaInst(T(type), (llvm::Value*)nullptr); #else declaration = new llvm::AllocaInst(T(type), 0, (llvm::Value*)nullptr); #endif } entryBlock.getInstList().push_front(declaration); return V(declaration); } BasicBlock *Nucleus::createBasicBlock() { return B(llvm::BasicBlock::Create(*::context, "", ::function)); } BasicBlock *Nucleus::getInsertBlock() { return B(::builder->GetInsertBlock()); } void Nucleus::setInsertBlock(BasicBlock *basicBlock) { // assert(::builder->GetInsertBlock()->back().isTerminator()); ::builder->SetInsertPoint(B(basicBlock)); } void Nucleus::createFunction(Type *ReturnType, std::vector &Params) { llvm::FunctionType *functionType = llvm::FunctionType::get(T(ReturnType), T(Params), false); ::function = llvm::Function::Create(functionType, llvm::GlobalValue::InternalLinkage, "", ::module); ::function->setCallingConv(llvm::CallingConv::C); #if defined(_WIN32) && REACTOR_LLVM_VERSION >= 7 // FIXME(capn): // On Windows, stack memory is committed in increments of 4 kB pages, with the last page // having a trap which allows the OS to grow the stack. For functions with a stack frame // larger than 4 kB this can cause an issue when a variable is accessed beyond the guard // page. Therefore the compiler emits a call to __chkstk in the function prolog to probe // the stack and ensure all pages have been committed. This is currently broken in LLVM // JIT, but we can prevent emitting the stack probe call: ::function->addFnAttr("stack-probe-size", "1048576"); #endif ::builder->SetInsertPoint(llvm::BasicBlock::Create(*::context, "", ::function)); } Value *Nucleus::getArgument(unsigned int index) { llvm::Function::arg_iterator args = ::function->arg_begin(); while(index) { args++; index--; } return V(&*args); } void Nucleus::createRetVoid() { ::builder->CreateRetVoid(); } void Nucleus::createRet(Value *v) { ::builder->CreateRet(V(v)); } void Nucleus::createBr(BasicBlock *dest) { ::builder->CreateBr(B(dest)); } void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) { ::builder->CreateCondBr(V(cond), B(ifTrue), B(ifFalse)); } Value *Nucleus::createAdd(Value *lhs, Value *rhs) { return V(::builder->CreateAdd(V(lhs), V(rhs))); } Value *Nucleus::createSub(Value *lhs, Value *rhs) { return V(::builder->CreateSub(V(lhs), V(rhs))); } Value *Nucleus::createMul(Value *lhs, Value *rhs) { return V(::builder->CreateMul(V(lhs), V(rhs))); } Value *Nucleus::createUDiv(Value *lhs, Value *rhs) { return V(::builder->CreateUDiv(V(lhs), V(rhs))); } Value *Nucleus::createSDiv(Value *lhs, Value *rhs) { return V(::builder->CreateSDiv(V(lhs), V(rhs))); } Value *Nucleus::createFAdd(Value *lhs, Value *rhs) { return V(::builder->CreateFAdd(V(lhs), V(rhs))); } Value *Nucleus::createFSub(Value *lhs, Value *rhs) { return V(::builder->CreateFSub(V(lhs), V(rhs))); } Value *Nucleus::createFMul(Value *lhs, Value *rhs) { return V(::builder->CreateFMul(V(lhs), V(rhs))); } Value *Nucleus::createFDiv(Value *lhs, Value *rhs) { return V(::builder->CreateFDiv(V(lhs), V(rhs))); } Value *Nucleus::createURem(Value *lhs, Value *rhs) { return V(::builder->CreateURem(V(lhs), V(rhs))); } Value *Nucleus::createSRem(Value *lhs, Value *rhs) { return V(::builder->CreateSRem(V(lhs), V(rhs))); } Value *Nucleus::createFRem(Value *lhs, Value *rhs) { return V(::builder->CreateFRem(V(lhs), V(rhs))); } Value *Nucleus::createShl(Value *lhs, Value *rhs) { return V(::builder->CreateShl(V(lhs), V(rhs))); } Value *Nucleus::createLShr(Value *lhs, Value *rhs) { return V(::builder->CreateLShr(V(lhs), V(rhs))); } Value *Nucleus::createAShr(Value *lhs, Value *rhs) { return V(::builder->CreateAShr(V(lhs), V(rhs))); } Value *Nucleus::createAnd(Value *lhs, Value *rhs) { return V(::builder->CreateAnd(V(lhs), V(rhs))); } Value *Nucleus::createOr(Value *lhs, Value *rhs) { return V(::builder->CreateOr(V(lhs), V(rhs))); } Value *Nucleus::createXor(Value *lhs, Value *rhs) { return V(::builder->CreateXor(V(lhs), V(rhs))); } Value *Nucleus::createNeg(Value *v) { return V(::builder->CreateNeg(V(v))); } Value *Nucleus::createFNeg(Value *v) { return V(::builder->CreateFNeg(V(v))); } Value *Nucleus::createNot(Value *v) { return V(::builder->CreateNot(V(v))); } Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int alignment) { uintptr_t t = reinterpret_cast(type); if(t < EmulatedTypeCount) { switch(t) { case Type_v2i32: case Type_v4i16: case Type_v8i8: case Type_v2f32: return createBitCast( createInsertElement( V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::getType()), 2))), createLoad(createBitCast(ptr, Pointer::getType()), Long::getType(), isVolatile, alignment), 0), type); case Type_v2i16: case Type_v4i8: if(alignment != 0) // Not a local variable (all vectors are 128-bit). { Value *u = V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::getType()), 2))); Value *i = createLoad(createBitCast(ptr, Pointer::getType()), Int::getType(), isVolatile, alignment); i = createZExt(i, Long::getType()); Value *v = createInsertElement(u, i, 0); return createBitCast(v, type); } break; default: assert(false); } } assert(V(ptr)->getType()->getContainedType(0) == T(type)); return V(::builder->Insert(new llvm::LoadInst(V(ptr), "", isVolatile, alignment))); } Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int alignment) { uintptr_t t = reinterpret_cast(type); if(t < EmulatedTypeCount) { switch(t) { case Type_v2i32: case Type_v4i16: case Type_v8i8: case Type_v2f32: createStore( createExtractElement( createBitCast(value, T(llvm::VectorType::get(T(Long::getType()), 2))), Long::getType(), 0), createBitCast(ptr, Pointer::getType()), Long::getType(), isVolatile, alignment); return value; case Type_v2i16: case Type_v4i8: if(alignment != 0) // Not a local variable (all vectors are 128-bit). { createStore( createExtractElement(createBitCast(value, Int4::getType()), Int::getType(), 0), createBitCast(ptr, Pointer::getType()), Int::getType(), isVolatile, alignment); return value; } break; default: assert(false); } } assert(V(ptr)->getType()->getContainedType(0) == T(type)); ::builder->Insert(new llvm::StoreInst(V(value), V(ptr), isVolatile, alignment)); return value; } Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) { if(sizeof(void*) == 8) { if(unsignedIndex) { index = createZExt(index, Long::getType()); } else { index = createSExt(index, Long::getType()); } index = createMul(index, createConstantLong((int64_t)typeSize(type))); } else { index = createMul(index, createConstantInt((int)typeSize(type))); } assert(V(ptr)->getType()->getContainedType(0) == T(type)); return createBitCast( V(::builder->CreateGEP(V(createBitCast(ptr, T(llvm::PointerType::get(T(Byte::getType()), 0)))), V(index))), T(llvm::PointerType::get(T(type), 0))); } Value *Nucleus::createAtomicAdd(Value *ptr, Value *value) { return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Add, V(ptr), V(value), llvm::AtomicOrdering::SequentiallyConsistent)); } Value *Nucleus::createTrunc(Value *v, Type *destType) { return V(::builder->CreateTrunc(V(v), T(destType))); } Value *Nucleus::createZExt(Value *v, Type *destType) { return V(::builder->CreateZExt(V(v), T(destType))); } Value *Nucleus::createSExt(Value *v, Type *destType) { return V(::builder->CreateSExt(V(v), T(destType))); } Value *Nucleus::createFPToSI(Value *v, Type *destType) { return V(::builder->CreateFPToSI(V(v), T(destType))); } Value *Nucleus::createSIToFP(Value *v, Type *destType) { return V(::builder->CreateSIToFP(V(v), T(destType))); } Value *Nucleus::createFPTrunc(Value *v, Type *destType) { return V(::builder->CreateFPTrunc(V(v), T(destType))); } Value *Nucleus::createFPExt(Value *v, Type *destType) { return V(::builder->CreateFPExt(V(v), T(destType))); } Value *Nucleus::createBitCast(Value *v, Type *destType) { // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need // support for casting between scalars and wide vectors. Emulate them by writing to the stack and // reading back as the destination type. if(!V(v)->getType()->isVectorTy() && T(destType)->isVectorTy()) { Value *readAddress = allocateStackVariable(destType); Value *writeAddress = createBitCast(readAddress, T(llvm::PointerType::get(V(v)->getType(), 0))); createStore(v, writeAddress, T(V(v)->getType())); return createLoad(readAddress, destType); } else if(V(v)->getType()->isVectorTy() && !T(destType)->isVectorTy()) { Value *writeAddress = allocateStackVariable(T(V(v)->getType())); createStore(v, writeAddress, T(V(v)->getType())); Value *readAddress = createBitCast(writeAddress, T(llvm::PointerType::get(T(destType), 0))); return createLoad(readAddress, destType); } return V(::builder->CreateBitCast(V(v), T(destType))); } Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) { return V(::builder->CreateICmpEQ(V(lhs), V(rhs))); } Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) { return V(::builder->CreateICmpNE(V(lhs), V(rhs))); } Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) { return V(::builder->CreateICmpUGT(V(lhs), V(rhs))); } Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) { return V(::builder->CreateICmpUGE(V(lhs), V(rhs))); } Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) { return V(::builder->CreateICmpULT(V(lhs), V(rhs))); } Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) { return V(::builder->CreateICmpULE(V(lhs), V(rhs))); } Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) { return V(::builder->CreateICmpSGT(V(lhs), V(rhs))); } Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) { return V(::builder->CreateICmpSGE(V(lhs), V(rhs))); } Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) { return V(::builder->CreateICmpSLT(V(lhs), V(rhs))); } Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) { return V(::builder->CreateICmpSLE(V(lhs), V(rhs))); } Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpOEQ(V(lhs), V(rhs))); } Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpOGT(V(lhs), V(rhs))); } Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpOGE(V(lhs), V(rhs))); } Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpOLT(V(lhs), V(rhs))); } Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpOLE(V(lhs), V(rhs))); } Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpONE(V(lhs), V(rhs))); } Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpORD(V(lhs), V(rhs))); } Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpUNO(V(lhs), V(rhs))); } Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpUEQ(V(lhs), V(rhs))); } Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpUGT(V(lhs), V(rhs))); } Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpUGE(V(lhs), V(rhs))); } Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpULT(V(lhs), V(rhs))); } Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpULE(V(lhs), V(rhs))); } Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) { return V(::builder->CreateFCmpULE(V(lhs), V(rhs))); } Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) { assert(V(vector)->getType()->getContainedType(0) == T(type)); return V(::builder->CreateExtractElement(V(vector), V(createConstantInt(index)))); } Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) { return V(::builder->CreateInsertElement(V(vector), V(element), V(createConstantInt(index)))); } Value *Nucleus::createShuffleVector(Value *v1, Value *v2, const int *select) { int size = llvm::cast(V(v1)->getType())->getNumElements(); const int maxSize = 16; llvm::Constant *swizzle[maxSize]; assert(size <= maxSize); for(int i = 0; i < size; i++) { swizzle[i] = llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), select[i]); } llvm::Value *shuffle = llvm::ConstantVector::get(llvm::ArrayRef(swizzle, size)); return V(::builder->CreateShuffleVector(V(v1), V(v2), shuffle)); } Value *Nucleus::createSelect(Value *c, Value *ifTrue, Value *ifFalse) { return V(::builder->CreateSelect(V(c), V(ifTrue), V(ifFalse))); } SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) { return reinterpret_cast(::builder->CreateSwitch(V(control), B(defaultBranch), numCases)); } void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) { llvm::SwitchInst *sw = reinterpret_cast(switchCases); sw->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), label, true), B(branch)); } void Nucleus::createUnreachable() { ::builder->CreateUnreachable(); } static Value *createSwizzle4(Value *val, unsigned char select) { int swizzle[4] = { (select >> 0) & 0x03, (select >> 2) & 0x03, (select >> 4) & 0x03, (select >> 6) & 0x03, }; return Nucleus::createShuffleVector(val, val, swizzle); } static Value *createMask4(Value *lhs, Value *rhs, unsigned char select) { bool mask[4] = {false, false, false, false}; mask[(select >> 0) & 0x03] = true; mask[(select >> 2) & 0x03] = true; mask[(select >> 4) & 0x03] = true; mask[(select >> 6) & 0x03] = true; int swizzle[4] = { mask[0] ? 4 : 0, mask[1] ? 5 : 1, mask[2] ? 6 : 2, mask[3] ? 7 : 3, }; return Nucleus::createShuffleVector(lhs, rhs, swizzle); } Type *Nucleus::getPointerType(Type *ElementType) { return T(llvm::PointerType::get(T(ElementType), 0)); } Value *Nucleus::createNullValue(Type *Ty) { return V(llvm::Constant::getNullValue(T(Ty))); } Value *Nucleus::createConstantLong(int64_t i) { return V(llvm::ConstantInt::get(llvm::Type::getInt64Ty(*::context), i, true)); } Value *Nucleus::createConstantInt(int i) { return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), i, true)); } Value *Nucleus::createConstantInt(unsigned int i) { return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), i, false)); } Value *Nucleus::createConstantBool(bool b) { return V(llvm::ConstantInt::get(llvm::Type::getInt1Ty(*::context), b)); } Value *Nucleus::createConstantByte(signed char i) { return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(*::context), i, true)); } Value *Nucleus::createConstantByte(unsigned char i) { return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(*::context), i, false)); } Value *Nucleus::createConstantShort(short i) { return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(*::context), i, true)); } Value *Nucleus::createConstantShort(unsigned short i) { return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(*::context), i, false)); } Value *Nucleus::createConstantFloat(float x) { return V(llvm::ConstantFP::get(T(Float::getType()), x)); } Value *Nucleus::createNullPointer(Type *Ty) { return V(llvm::ConstantPointerNull::get(llvm::PointerType::get(T(Ty), 0))); } Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) { assert(llvm::isa(T(type))); const int numConstants = elementCount(type); // Number of provided constants for the (emulated) type. const int numElements = llvm::cast(T(type))->getNumElements(); // Number of elements of the underlying vector type. assert(numElements <= 16 && numConstants <= numElements); llvm::Constant *constantVector[16]; for(int i = 0; i < numElements; i++) { constantVector[i] = llvm::ConstantInt::get(T(type)->getContainedType(0), constants[i % numConstants]); } return V(llvm::ConstantVector::get(llvm::ArrayRef(constantVector, numElements))); } Value *Nucleus::createConstantVector(const double *constants, Type *type) { assert(llvm::isa(T(type))); const int numConstants = elementCount(type); // Number of provided constants for the (emulated) type. const int numElements = llvm::cast(T(type))->getNumElements(); // Number of elements of the underlying vector type. assert(numElements <= 8 && numConstants <= numElements); llvm::Constant *constantVector[8]; for(int i = 0; i < numElements; i++) { constantVector[i] = llvm::ConstantFP::get(T(type)->getContainedType(0), constants[i % numConstants]); } return V(llvm::ConstantVector::get(llvm::ArrayRef(constantVector, numElements))); } Type *Void::getType() { return T(llvm::Type::getVoidTy(*::context)); } Bool::Bool(Argument argument) { storeValue(argument.value); } Bool::Bool(bool x) { storeValue(Nucleus::createConstantBool(x)); } Bool::Bool(RValue rhs) { storeValue(rhs.value); } Bool::Bool(const Bool &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Bool::Bool(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } RValue Bool::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue Bool::operator=(const Bool &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Bool::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator!(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue operator&&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator||(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } Type *Bool::getType() { return T(llvm::Type::getInt1Ty(*::context)); } Byte::Byte(Argument argument) { storeValue(argument.value); } Byte::Byte(RValue cast) { Value *integer = Nucleus::createTrunc(cast.value, Byte::getType()); storeValue(integer); } Byte::Byte(RValue cast) { Value *integer = Nucleus::createTrunc(cast.value, Byte::getType()); storeValue(integer); } Byte::Byte(RValue cast) { Value *integer = Nucleus::createTrunc(cast.value, Byte::getType()); storeValue(integer); } Byte::Byte(int x) { storeValue(Nucleus::createConstantByte((unsigned char)x)); } Byte::Byte(unsigned char x) { storeValue(Nucleus::createConstantByte(x)); } Byte::Byte(RValue rhs) { storeValue(rhs.value); } Byte::Byte(const Byte &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Byte::Byte(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } RValue Byte::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue Byte::operator=(const Byte &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Byte::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } RValue operator*(RValue lhs, RValue rhs) { return RValue(Nucleus::createMul(lhs.value, rhs.value)); } RValue operator/(RValue lhs, RValue rhs) { return RValue(Nucleus::createUDiv(lhs.value, rhs.value)); } RValue operator%(RValue lhs, RValue rhs) { return RValue(Nucleus::createURem(lhs.value, rhs.value)); } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, RValue rhs) { return RValue(Nucleus::createShl(lhs.value, rhs.value)); } RValue operator>>(RValue lhs, RValue rhs) { return RValue(Nucleus::createLShr(lhs.value, rhs.value)); } RValue operator+=(Byte &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(Byte &lhs, RValue rhs) { return lhs = lhs - rhs; } RValue operator*=(Byte &lhs, RValue rhs) { return lhs = lhs * rhs; } RValue operator/=(Byte &lhs, RValue rhs) { return lhs = lhs / rhs; } RValue operator%=(Byte &lhs, RValue rhs) { return lhs = lhs % rhs; } RValue operator&=(Byte &lhs, RValue rhs) { return lhs = lhs & rhs; } RValue operator|=(Byte &lhs, RValue rhs) { return lhs = lhs | rhs; } RValue operator^=(Byte &lhs, RValue rhs) { return lhs = lhs ^ rhs; } RValue operator<<=(Byte &lhs, RValue rhs) { return lhs = lhs << rhs; } RValue operator>>=(Byte &lhs, RValue rhs) { return lhs = lhs >> rhs; } RValue operator+(RValue val) { return val; } RValue operator-(RValue val) { return RValue(Nucleus::createNeg(val.value)); } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue operator++(Byte &val, int) // Post-increment { RValue res = val; Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantByte((unsigned char)1)); val.storeValue(inc); return res; } const Byte &operator++(Byte &val) // Pre-increment { Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantByte((unsigned char)1)); val.storeValue(inc); return val; } RValue operator--(Byte &val, int) // Post-decrement { RValue res = val; Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantByte((unsigned char)1)); val.storeValue(inc); return res; } const Byte &operator--(Byte &val) // Pre-decrement { Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantByte((unsigned char)1)); val.storeValue(inc); return val; } RValue operator<(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpULT(lhs.value, rhs.value)); } RValue operator<=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpULE(lhs.value, rhs.value)); } RValue operator>(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpUGT(lhs.value, rhs.value)); } RValue operator>=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpUGE(lhs.value, rhs.value)); } RValue operator!=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpNE(lhs.value, rhs.value)); } RValue operator==(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpEQ(lhs.value, rhs.value)); } Type *Byte::getType() { return T(llvm::Type::getInt8Ty(*::context)); } SByte::SByte(Argument argument) { storeValue(argument.value); } SByte::SByte(RValue cast) { Value *integer = Nucleus::createTrunc(cast.value, SByte::getType()); storeValue(integer); } SByte::SByte(RValue cast) { Value *integer = Nucleus::createTrunc(cast.value, SByte::getType()); storeValue(integer); } SByte::SByte(signed char x) { storeValue(Nucleus::createConstantByte(x)); } SByte::SByte(RValue rhs) { storeValue(rhs.value); } SByte::SByte(const SByte &rhs) { Value *value = rhs.loadValue(); storeValue(value); } SByte::SByte(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } RValue SByte::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue SByte::operator=(const SByte &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue SByte::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } RValue operator*(RValue lhs, RValue rhs) { return RValue(Nucleus::createMul(lhs.value, rhs.value)); } RValue operator/(RValue lhs, RValue rhs) { return RValue(Nucleus::createSDiv(lhs.value, rhs.value)); } RValue operator%(RValue lhs, RValue rhs) { return RValue(Nucleus::createSRem(lhs.value, rhs.value)); } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, RValue rhs) { return RValue(Nucleus::createShl(lhs.value, rhs.value)); } RValue operator>>(RValue lhs, RValue rhs) { return RValue(Nucleus::createAShr(lhs.value, rhs.value)); } RValue operator+=(SByte &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(SByte &lhs, RValue rhs) { return lhs = lhs - rhs; } RValue operator*=(SByte &lhs, RValue rhs) { return lhs = lhs * rhs; } RValue operator/=(SByte &lhs, RValue rhs) { return lhs = lhs / rhs; } RValue operator%=(SByte &lhs, RValue rhs) { return lhs = lhs % rhs; } RValue operator&=(SByte &lhs, RValue rhs) { return lhs = lhs & rhs; } RValue operator|=(SByte &lhs, RValue rhs) { return lhs = lhs | rhs; } RValue operator^=(SByte &lhs, RValue rhs) { return lhs = lhs ^ rhs; } RValue operator<<=(SByte &lhs, RValue rhs) { return lhs = lhs << rhs; } RValue operator>>=(SByte &lhs, RValue rhs) { return lhs = lhs >> rhs; } RValue operator+(RValue val) { return val; } RValue operator-(RValue val) { return RValue(Nucleus::createNeg(val.value)); } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue operator++(SByte &val, int) // Post-increment { RValue res = val; Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantByte((signed char)1)); val.storeValue(inc); return res; } const SByte &operator++(SByte &val) // Pre-increment { Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantByte((signed char)1)); val.storeValue(inc); return val; } RValue operator--(SByte &val, int) // Post-decrement { RValue res = val; Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantByte((signed char)1)); val.storeValue(inc); return res; } const SByte &operator--(SByte &val) // Pre-decrement { Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantByte((signed char)1)); val.storeValue(inc); return val; } RValue operator<(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpSLT(lhs.value, rhs.value)); } RValue operator<=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpSLE(lhs.value, rhs.value)); } RValue operator>(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpSGT(lhs.value, rhs.value)); } RValue operator>=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpSGE(lhs.value, rhs.value)); } RValue operator!=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpNE(lhs.value, rhs.value)); } RValue operator==(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpEQ(lhs.value, rhs.value)); } Type *SByte::getType() { return T(llvm::Type::getInt8Ty(*::context)); } Short::Short(Argument argument) { storeValue(argument.value); } Short::Short(RValue cast) { Value *integer = Nucleus::createTrunc(cast.value, Short::getType()); storeValue(integer); } Short::Short(short x) { storeValue(Nucleus::createConstantShort(x)); } Short::Short(RValue rhs) { storeValue(rhs.value); } Short::Short(const Short &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Short::Short(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } RValue Short::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue Short::operator=(const Short &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Short::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } RValue operator*(RValue lhs, RValue rhs) { return RValue(Nucleus::createMul(lhs.value, rhs.value)); } RValue operator/(RValue lhs, RValue rhs) { return RValue(Nucleus::createSDiv(lhs.value, rhs.value)); } RValue operator%(RValue lhs, RValue rhs) { return RValue(Nucleus::createSRem(lhs.value, rhs.value)); } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, RValue rhs) { return RValue(Nucleus::createShl(lhs.value, rhs.value)); } RValue operator>>(RValue lhs, RValue rhs) { return RValue(Nucleus::createAShr(lhs.value, rhs.value)); } RValue operator+=(Short &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(Short &lhs, RValue rhs) { return lhs = lhs - rhs; } RValue operator*=(Short &lhs, RValue rhs) { return lhs = lhs * rhs; } RValue operator/=(Short &lhs, RValue rhs) { return lhs = lhs / rhs; } RValue operator%=(Short &lhs, RValue rhs) { return lhs = lhs % rhs; } RValue operator&=(Short &lhs, RValue rhs) { return lhs = lhs & rhs; } RValue operator|=(Short &lhs, RValue rhs) { return lhs = lhs | rhs; } RValue operator^=(Short &lhs, RValue rhs) { return lhs = lhs ^ rhs; } RValue operator<<=(Short &lhs, RValue rhs) { return lhs = lhs << rhs; } RValue operator>>=(Short &lhs, RValue rhs) { return lhs = lhs >> rhs; } RValue operator+(RValue val) { return val; } RValue operator-(RValue val) { return RValue(Nucleus::createNeg(val.value)); } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue operator++(Short &val, int) // Post-increment { RValue res = val; Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantShort((short)1)); val.storeValue(inc); return res; } const Short &operator++(Short &val) // Pre-increment { Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantShort((short)1)); val.storeValue(inc); return val; } RValue operator--(Short &val, int) // Post-decrement { RValue res = val; Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantShort((short)1)); val.storeValue(inc); return res; } const Short &operator--(Short &val) // Pre-decrement { Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantShort((short)1)); val.storeValue(inc); return val; } RValue operator<(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpSLT(lhs.value, rhs.value)); } RValue operator<=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpSLE(lhs.value, rhs.value)); } RValue operator>(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpSGT(lhs.value, rhs.value)); } RValue operator>=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpSGE(lhs.value, rhs.value)); } RValue operator!=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpNE(lhs.value, rhs.value)); } RValue operator==(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpEQ(lhs.value, rhs.value)); } Type *Short::getType() { return T(llvm::Type::getInt16Ty(*::context)); } UShort::UShort(Argument argument) { storeValue(argument.value); } UShort::UShort(RValue cast) { Value *integer = Nucleus::createTrunc(cast.value, UShort::getType()); storeValue(integer); } UShort::UShort(RValue cast) { Value *integer = Nucleus::createTrunc(cast.value, UShort::getType()); storeValue(integer); } UShort::UShort(unsigned short x) { storeValue(Nucleus::createConstantShort(x)); } UShort::UShort(RValue rhs) { storeValue(rhs.value); } UShort::UShort(const UShort &rhs) { Value *value = rhs.loadValue(); storeValue(value); } UShort::UShort(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } RValue UShort::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue UShort::operator=(const UShort &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue UShort::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } RValue operator*(RValue lhs, RValue rhs) { return RValue(Nucleus::createMul(lhs.value, rhs.value)); } RValue operator/(RValue lhs, RValue rhs) { return RValue(Nucleus::createUDiv(lhs.value, rhs.value)); } RValue operator%(RValue lhs, RValue rhs) { return RValue(Nucleus::createURem(lhs.value, rhs.value)); } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, RValue rhs) { return RValue(Nucleus::createShl(lhs.value, rhs.value)); } RValue operator>>(RValue lhs, RValue rhs) { return RValue(Nucleus::createLShr(lhs.value, rhs.value)); } RValue operator+=(UShort &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(UShort &lhs, RValue rhs) { return lhs = lhs - rhs; } RValue operator*=(UShort &lhs, RValue rhs) { return lhs = lhs * rhs; } RValue operator/=(UShort &lhs, RValue rhs) { return lhs = lhs / rhs; } RValue operator%=(UShort &lhs, RValue rhs) { return lhs = lhs % rhs; } RValue operator&=(UShort &lhs, RValue rhs) { return lhs = lhs & rhs; } RValue operator|=(UShort &lhs, RValue rhs) { return lhs = lhs | rhs; } RValue operator^=(UShort &lhs, RValue rhs) { return lhs = lhs ^ rhs; } RValue operator<<=(UShort &lhs, RValue rhs) { return lhs = lhs << rhs; } RValue operator>>=(UShort &lhs, RValue rhs) { return lhs = lhs >> rhs; } RValue operator+(RValue val) { return val; } RValue operator-(RValue val) { return RValue(Nucleus::createNeg(val.value)); } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue operator++(UShort &val, int) // Post-increment { RValue res = val; Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantShort((unsigned short)1)); val.storeValue(inc); return res; } const UShort &operator++(UShort &val) // Pre-increment { Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantShort((unsigned short)1)); val.storeValue(inc); return val; } RValue operator--(UShort &val, int) // Post-decrement { RValue res = val; Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantShort((unsigned short)1)); val.storeValue(inc); return res; } const UShort &operator--(UShort &val) // Pre-decrement { Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantShort((unsigned short)1)); val.storeValue(inc); return val; } RValue operator<(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpULT(lhs.value, rhs.value)); } RValue operator<=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpULE(lhs.value, rhs.value)); } RValue operator>(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpUGT(lhs.value, rhs.value)); } RValue operator>=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpUGE(lhs.value, rhs.value)); } RValue operator!=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpNE(lhs.value, rhs.value)); } RValue operator==(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpEQ(lhs.value, rhs.value)); } Type *UShort::getType() { return T(llvm::Type::getInt16Ty(*::context)); } Byte4::Byte4(RValue cast) { storeValue(Nucleus::createBitCast(cast.value, getType())); } Byte4::Byte4(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Type *Byte4::getType() { return T(Type_v4i8); } Type *SByte4::getType() { return T(Type_v4i8); } 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) { int64_t constantVector[8] = {x0, x1, x2, x3, x4, x5, x6, x7}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } Byte8::Byte8(RValue rhs) { storeValue(rhs.value); } Byte8::Byte8(const Byte8 &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Byte8::Byte8(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } RValue Byte8::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue Byte8::operator=(const Byte8 &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Byte8::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } // RValue operator*(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createMul(lhs.value, rhs.value)); // } // RValue operator/(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createUDiv(lhs.value, rhs.value)); // } // RValue operator%(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createURem(lhs.value, rhs.value)); // } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } // RValue operator<<(RValue lhs, unsigned char rhs) // { // return RValue(Nucleus::createShl(lhs.value, rhs.value)); // } // RValue operator>>(RValue lhs, unsigned char rhs) // { // return RValue(Nucleus::createLShr(lhs.value, rhs.value)); // } RValue operator+=(Byte8 &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(Byte8 &lhs, RValue rhs) { return lhs = lhs - rhs; } // RValue operator*=(Byte8 &lhs, RValue rhs) // { // return lhs = lhs * rhs; // } // RValue operator/=(Byte8 &lhs, RValue rhs) // { // return lhs = lhs / rhs; // } // RValue operator%=(Byte8 &lhs, RValue rhs) // { // return lhs = lhs % rhs; // } RValue operator&=(Byte8 &lhs, RValue rhs) { return lhs = lhs & rhs; } RValue operator|=(Byte8 &lhs, RValue rhs) { return lhs = lhs | rhs; } RValue operator^=(Byte8 &lhs, RValue rhs) { return lhs = lhs ^ rhs; } // RValue operator<<=(Byte8 &lhs, RValue rhs) // { // return lhs = lhs << rhs; // } // RValue operator>>=(Byte8 &lhs, RValue rhs) // { // return lhs = lhs >> rhs; // } // RValue operator+(RValue val) // { // return val; // } // RValue operator-(RValue val) // { // return RValue(Nucleus::createNeg(val.value)); // } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue AddSat(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::paddusb(x, y); #else return As(V(lowerPUADDSAT(V(x.value), V(y.value)))); #endif } RValue SubSat(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::psubusb(x, y); #else return As(V(lowerPUSUBSAT(V(x.value), V(y.value)))); #endif } RValue Unpack(RValue x) { int shuffle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}; // Real type is v16i8 return As(Nucleus::createShuffleVector(x.value, x.value, shuffle)); } RValue Unpack(RValue x, RValue y) { return UnpackLow(As(x), As(y)); } RValue UnpackLow(RValue x, RValue y) { int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8 return As(Nucleus::createShuffleVector(x.value, y.value, shuffle)); } RValue UnpackHigh(RValue x, RValue y) { int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8 auto lowHigh = RValue(Nucleus::createShuffleVector(x.value, y.value, shuffle)); return As(Swizzle(As(lowHigh), 0xEE)); } RValue SignMask(RValue x) { #if defined(__i386__) || defined(__x86_64__) return x86::pmovmskb(x); #else return As(V(lowerSignMask(V(x.value), T(Int::getType())))); #endif } // RValue CmpGT(RValue x, RValue y) // { //#if defined(__i386__) || defined(__x86_64__) // return x86::pcmpgtb(x, y); // FIXME: Signedness //#else // return As(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); //#endif // } RValue CmpEQ(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pcmpeqb(x, y); #else return As(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); #endif } Type *Byte8::getType() { return T(Type_v8i8); } 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) { int64_t constantVector[8] = {x0, x1, x2, x3, x4, x5, x6, x7}; Value *vector = Nucleus::createConstantVector(constantVector, getType()); storeValue(Nucleus::createBitCast(vector, getType())); } SByte8::SByte8(RValue rhs) { storeValue(rhs.value); } SByte8::SByte8(const SByte8 &rhs) { Value *value = rhs.loadValue(); storeValue(value); } SByte8::SByte8(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } RValue SByte8::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue SByte8::operator=(const SByte8 &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue SByte8::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } // RValue operator*(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createMul(lhs.value, rhs.value)); // } // RValue operator/(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createSDiv(lhs.value, rhs.value)); // } // RValue operator%(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createSRem(lhs.value, rhs.value)); // } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } // RValue operator<<(RValue lhs, unsigned char rhs) // { // return RValue(Nucleus::createShl(lhs.value, rhs.value)); // } // RValue operator>>(RValue lhs, unsigned char rhs) // { // return RValue(Nucleus::createAShr(lhs.value, rhs.value)); // } RValue operator+=(SByte8 &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(SByte8 &lhs, RValue rhs) { return lhs = lhs - rhs; } // RValue operator*=(SByte8 &lhs, RValue rhs) // { // return lhs = lhs * rhs; // } // RValue operator/=(SByte8 &lhs, RValue rhs) // { // return lhs = lhs / rhs; // } // RValue operator%=(SByte8 &lhs, RValue rhs) // { // return lhs = lhs % rhs; // } RValue operator&=(SByte8 &lhs, RValue rhs) { return lhs = lhs & rhs; } RValue operator|=(SByte8 &lhs, RValue rhs) { return lhs = lhs | rhs; } RValue operator^=(SByte8 &lhs, RValue rhs) { return lhs = lhs ^ rhs; } // RValue operator<<=(SByte8 &lhs, RValue rhs) // { // return lhs = lhs << rhs; // } // RValue operator>>=(SByte8 &lhs, RValue rhs) // { // return lhs = lhs >> rhs; // } // RValue operator+(RValue val) // { // return val; // } // RValue operator-(RValue val) // { // return RValue(Nucleus::createNeg(val.value)); // } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue AddSat(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::paddsb(x, y); #else return As(V(lowerPSADDSAT(V(x.value), V(y.value)))); #endif } RValue SubSat(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::psubsb(x, y); #else return As(V(lowerPSSUBSAT(V(x.value), V(y.value)))); #endif } RValue UnpackLow(RValue x, RValue y) { int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8 return As(Nucleus::createShuffleVector(x.value, y.value, shuffle)); } RValue UnpackHigh(RValue x, RValue y) { int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; // Real type is v16i8 auto lowHigh = RValue(Nucleus::createShuffleVector(x.value, y.value, shuffle)); return As(Swizzle(As(lowHigh), 0xEE)); } RValue SignMask(RValue x) { #if defined(__i386__) || defined(__x86_64__) return x86::pmovmskb(As(x)); #else return As(V(lowerSignMask(V(x.value), T(Int::getType())))); #endif } RValue CmpGT(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pcmpgtb(x, y); #else return As(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); #endif } RValue CmpEQ(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pcmpeqb(As(x), As(y)); #else return As(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); #endif } Type *SByte8::getType() { return T(Type_v8i8); } Byte16::Byte16(RValue rhs) { storeValue(rhs.value); } Byte16::Byte16(const Byte16 &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Byte16::Byte16(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } RValue Byte16::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue Byte16::operator=(const Byte16 &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Byte16::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } Type *Byte16::getType() { return T(llvm::VectorType::get(T(Byte::getType()), 16)); } Type *SByte16::getType() { return T(llvm::VectorType::get(T(SByte::getType()), 16)); } Short2::Short2(RValue cast) { storeValue(Nucleus::createBitCast(cast.value, getType())); } Type *Short2::getType() { return T(Type_v2i16); } UShort2::UShort2(RValue cast) { storeValue(Nucleus::createBitCast(cast.value, getType())); } Type *UShort2::getType() { return T(Type_v2i16); } Short4::Short4(RValue cast) { Value *vector = loadValue(); Value *element = Nucleus::createTrunc(cast.value, Short::getType()); Value *insert = Nucleus::createInsertElement(vector, element, 0); Value *swizzle = Swizzle(RValue(insert), 0x00).value; storeValue(swizzle); } Short4::Short4(RValue cast) { int select[8] = {0, 2, 4, 6, 0, 2, 4, 6}; Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType()); Value *packed = Nucleus::createShuffleVector(short8, short8, select); Value *short4 = As(Int2(As(packed))).value; storeValue(short4); } // Short4::Short4(RValue cast) // { // } Short4::Short4(RValue cast) { Int4 v4i32 = Int4(cast); #if defined(__i386__) || defined(__x86_64__) v4i32 = As(x86::packssdw(v4i32, v4i32)); #else Value *v = v4i32.loadValue(); v4i32 = As(V(lowerPack(V(v), V(v), true))); #endif storeValue(As(Int2(v4i32)).value); } Short4::Short4(short xyzw) { int64_t constantVector[4] = {xyzw, xyzw, xyzw, xyzw}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } Short4::Short4(short x, short y, short z, short w) { int64_t constantVector[4] = {x, y, z, w}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } Short4::Short4(RValue rhs) { storeValue(rhs.value); } Short4::Short4(const Short4 &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Short4::Short4(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Short4::Short4(RValue rhs) { storeValue(rhs.value); } Short4::Short4(const UShort4 &rhs) { storeValue(rhs.loadValue()); } Short4::Short4(const Reference &rhs) { storeValue(rhs.loadValue()); } RValue Short4::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue Short4::operator=(const Short4 &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Short4::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Short4::operator=(RValue rhs) { storeValue(rhs.value); return RValue(rhs); } RValue Short4::operator=(const UShort4 &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Short4::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } RValue operator*(RValue lhs, RValue rhs) { return RValue(Nucleus::createMul(lhs.value, rhs.value)); } // RValue operator/(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createSDiv(lhs.value, rhs.value)); // } // RValue operator%(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createSRem(lhs.value, rhs.value)); // } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) // return RValue(Nucleus::createShl(lhs.value, rhs.value)); return x86::psllw(lhs, rhs); #else return As(V(lowerVectorShl(V(lhs.value), rhs))); #endif } RValue operator>>(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) return x86::psraw(lhs, rhs); #else return As(V(lowerVectorAShr(V(lhs.value), rhs))); #endif } RValue operator+=(Short4 &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(Short4 &lhs, RValue rhs) { return lhs = lhs - rhs; } RValue operator*=(Short4 &lhs, RValue rhs) { return lhs = lhs * rhs; } // RValue operator/=(Short4 &lhs, RValue rhs) // { // return lhs = lhs / rhs; // } // RValue operator%=(Short4 &lhs, RValue rhs) // { // return lhs = lhs % rhs; // } RValue operator&=(Short4 &lhs, RValue rhs) { return lhs = lhs & rhs; } RValue operator|=(Short4 &lhs, RValue rhs) { return lhs = lhs | rhs; } RValue operator^=(Short4 &lhs, RValue rhs) { return lhs = lhs ^ rhs; } RValue operator<<=(Short4 &lhs, unsigned char rhs) { return lhs = lhs << rhs; } RValue operator>>=(Short4 &lhs, unsigned char rhs) { return lhs = lhs >> rhs; } // RValue operator+(RValue val) // { // return val; // } RValue operator-(RValue val) { return RValue(Nucleus::createNeg(val.value)); } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue RoundShort4(RValue cast) { RValue int4 = RoundInt(cast); return As(PackSigned(int4, int4)); } RValue Max(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pmaxsw(x, y); #else return RValue(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); #endif } RValue Min(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pminsw(x, y); #else return RValue(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); #endif } RValue AddSat(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::paddsw(x, y); #else return As(V(lowerPSADDSAT(V(x.value), V(y.value)))); #endif } RValue SubSat(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::psubsw(x, y); #else return As(V(lowerPSSUBSAT(V(x.value), V(y.value)))); #endif } RValue MulHigh(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pmulhw(x, y); #else return As(V(lowerMulHigh(V(x.value), V(y.value), true))); #endif } RValue MulAdd(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pmaddwd(x, y); #else return As(V(lowerMulAdd(V(x.value), V(y.value)))); #endif } RValue PackSigned(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) auto result = x86::packsswb(x, y); #else auto result = V(lowerPack(V(x.value), V(y.value), true)); #endif return As(Swizzle(As(result), 0x88)); } RValue PackUnsigned(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) auto result = x86::packuswb(x, y); #else auto result = V(lowerPack(V(x.value), V(y.value), false)); #endif return As(Swizzle(As(result), 0x88)); } RValue UnpackLow(RValue x, RValue y) { int shuffle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; // Real type is v8i16 return As(Nucleus::createShuffleVector(x.value, y.value, shuffle)); } RValue UnpackHigh(RValue x, RValue y) { int shuffle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; // Real type is v8i16 auto lowHigh = RValue(Nucleus::createShuffleVector(x.value, y.value, shuffle)); return As(Swizzle(As(lowHigh), 0xEE)); } RValue Swizzle(RValue x, unsigned char select) { // Real type is v8i16 int shuffle[8] = { (select >> 0) & 0x03, (select >> 2) & 0x03, (select >> 4) & 0x03, (select >> 6) & 0x03, (select >> 0) & 0x03, (select >> 2) & 0x03, (select >> 4) & 0x03, (select >> 6) & 0x03, }; return As(Nucleus::createShuffleVector(x.value, x.value, shuffle)); } RValue Insert(RValue val, RValue element, int i) { return RValue(Nucleus::createInsertElement(val.value, element.value, i)); } RValue Extract(RValue val, int i) { return RValue(Nucleus::createExtractElement(val.value, Short::getType(), i)); } RValue CmpGT(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pcmpgtw(x, y); #else return As(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType())))); #endif } RValue CmpEQ(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pcmpeqw(x, y); #else return As(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType())))); #endif } Type *Short4::getType() { return T(Type_v4i16); } UShort4::UShort4(RValue cast) { *this = Short4(cast); } UShort4::UShort4(RValue cast, bool saturate) { if(saturate) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation *this = As(PackUnsigned(int4, int4)); } else #endif { *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); } } else { *this = Short4(Int4(cast)); } } UShort4::UShort4(unsigned short xyzw) { int64_t constantVector[4] = {xyzw, xyzw, xyzw, xyzw}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } UShort4::UShort4(unsigned short x, unsigned short y, unsigned short z, unsigned short w) { int64_t constantVector[4] = {x, y, z, w}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } UShort4::UShort4(RValue rhs) { storeValue(rhs.value); } UShort4::UShort4(const UShort4 &rhs) { Value *value = rhs.loadValue(); storeValue(value); } UShort4::UShort4(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } UShort4::UShort4(RValue rhs) { storeValue(rhs.value); } UShort4::UShort4(const Short4 &rhs) { Value *value = rhs.loadValue(); storeValue(value); } UShort4::UShort4(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } RValue UShort4::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue UShort4::operator=(const UShort4 &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue UShort4::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue UShort4::operator=(RValue rhs) { storeValue(rhs.value); return RValue(rhs); } RValue UShort4::operator=(const Short4 &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue UShort4::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } RValue operator*(RValue lhs, RValue rhs) { return RValue(Nucleus::createMul(lhs.value, rhs.value)); } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) // return RValue(Nucleus::createShl(lhs.value, rhs.value)); return As(x86::psllw(As(lhs), rhs)); #else return As(V(lowerVectorShl(V(lhs.value), rhs))); #endif } RValue operator>>(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) // return RValue(Nucleus::createLShr(lhs.value, rhs.value)); return x86::psrlw(lhs, rhs); #else return As(V(lowerVectorLShr(V(lhs.value), rhs))); #endif } RValue operator<<=(UShort4 &lhs, unsigned char rhs) { return lhs = lhs << rhs; } RValue operator>>=(UShort4 &lhs, unsigned char rhs) { return lhs = lhs >> rhs; } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue Max(RValue x, RValue y) { return RValue(Max(As(x) - Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u), As(y) - Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u)) + Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u)); } RValue Min(RValue x, RValue y) { return RValue(Min(As(x) - Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u), As(y) - Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u)) + Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u)); } RValue AddSat(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::paddusw(x, y); #else return As(V(lowerPUADDSAT(V(x.value), V(y.value)))); #endif } RValue SubSat(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::psubusw(x, y); #else return As(V(lowerPUSUBSAT(V(x.value), V(y.value)))); #endif } RValue MulHigh(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pmulhuw(x, y); #else return As(V(lowerMulHigh(V(x.value), V(y.value), false))); #endif } RValue Average(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pavgw(x, y); #else return As(V(lowerPAVG(V(x.value), V(y.value)))); #endif } Type *UShort4::getType() { return T(Type_v4i16); } Short8::Short8(short c) { int64_t constantVector[8] = {c, c, c, c, c, c, c, c}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } Short8::Short8(short c0, short c1, short c2, short c3, short c4, short c5, short c6, short c7) { int64_t constantVector[8] = {c0, c1, c2, c3, c4, c5, c6, c7}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } Short8::Short8(RValue rhs) { storeValue(rhs.value); } Short8::Short8(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Short8::Short8(RValue lo, RValue hi) { int shuffle[8] = {0, 1, 2, 3, 8, 9, 10, 11}; // Real type is v8i16 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle); storeValue(packed); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) return x86::psllw(lhs, rhs); #else return As(V(lowerVectorShl(V(lhs.value), rhs))); #endif } RValue operator>>(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) return x86::psraw(lhs, rhs); #else return As(V(lowerVectorAShr(V(lhs.value), rhs))); #endif } RValue MulAdd(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pmaddwd(x, y); #else return As(V(lowerMulAdd(V(x.value), V(y.value)))); #endif } RValue Abs(RValue x) { auto negative = x >> 31; return (x ^ negative) - negative; } RValue MulHigh(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pmulhw(x, y); #else return As(V(lowerMulHigh(V(x.value), V(y.value), true))); #endif } Type *Short8::getType() { return T(llvm::VectorType::get(T(Short::getType()), 8)); } UShort8::UShort8(unsigned short c) { int64_t constantVector[8] = {c, c, c, c, c, c, c, c}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } 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) { int64_t constantVector[8] = {c0, c1, c2, c3, c4, c5, c6, c7}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } UShort8::UShort8(RValue rhs) { storeValue(rhs.value); } UShort8::UShort8(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } UShort8::UShort8(RValue lo, RValue hi) { int shuffle[8] = {0, 1, 2, 3, 8, 9, 10, 11}; // Real type is v8i16 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle); storeValue(packed); } RValue UShort8::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue UShort8::operator=(const UShort8 &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue UShort8::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) return As(x86::psllw(As(lhs), rhs)); #else return As(V(lowerVectorShl(V(lhs.value), rhs))); #endif } RValue operator>>(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) return x86::psrlw(lhs, rhs); // FIXME: Fallback required #else return As(V(lowerVectorLShr(V(lhs.value), rhs))); #endif } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator*(RValue lhs, RValue rhs) { return RValue(Nucleus::createMul(lhs.value, rhs.value)); } RValue operator+=(UShort8 &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue Swizzle(RValue x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7) { int pshufb[16] = { select0 + 0, select0 + 1, select1 + 0, select1 + 1, select2 + 0, select2 + 1, select3 + 0, select3 + 1, select4 + 0, select4 + 1, select5 + 0, select5 + 1, select6 + 0, select6 + 1, select7 + 0, select7 + 1, }; Value *byte16 = Nucleus::createBitCast(x.value, Byte16::getType()); Value *shuffle = Nucleus::createShuffleVector(byte16, byte16, pshufb); Value *short8 = Nucleus::createBitCast(shuffle, UShort8::getType()); return RValue(short8); } RValue MulHigh(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::pmulhuw(x, y); #else return As(V(lowerMulHigh(V(x.value), V(y.value), false))); #endif } Type *UShort8::getType() { return T(llvm::VectorType::get(T(UShort::getType()), 8)); } Int::Int(Argument argument) { storeValue(argument.value); } Int::Int(RValue cast) { Value *integer = Nucleus::createZExt(cast.value, Int::getType()); storeValue(integer); } Int::Int(RValue cast) { Value *integer = Nucleus::createSExt(cast.value, Int::getType()); storeValue(integer); } Int::Int(RValue cast) { Value *integer = Nucleus::createSExt(cast.value, Int::getType()); storeValue(integer); } Int::Int(RValue cast) { Value *integer = Nucleus::createZExt(cast.value, Int::getType()); storeValue(integer); } Int::Int(RValue cast) { *this = Extract(cast, 0); } Int::Int(RValue cast) { Value *integer = Nucleus::createTrunc(cast.value, Int::getType()); storeValue(integer); } Int::Int(RValue cast) { Value *integer = Nucleus::createFPToSI(cast.value, Int::getType()); storeValue(integer); } Int::Int(int x) { storeValue(Nucleus::createConstantInt(x)); } Int::Int(RValue rhs) { storeValue(rhs.value); } Int::Int(RValue rhs) { storeValue(rhs.value); } Int::Int(const Int &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Int::Int(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Int::Int(const UInt &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Int::Int(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } RValue Int::operator=(int rhs) { return RValue(storeValue(Nucleus::createConstantInt(rhs))); } RValue Int::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue Int::operator=(RValue rhs) { storeValue(rhs.value); return RValue(rhs); } RValue Int::operator=(const Int &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Int::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Int::operator=(const UInt &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Int::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } RValue operator*(RValue lhs, RValue rhs) { return RValue(Nucleus::createMul(lhs.value, rhs.value)); } RValue operator/(RValue lhs, RValue rhs) { return RValue(Nucleus::createSDiv(lhs.value, rhs.value)); } RValue operator%(RValue lhs, RValue rhs) { return RValue(Nucleus::createSRem(lhs.value, rhs.value)); } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, RValue rhs) { return RValue(Nucleus::createShl(lhs.value, rhs.value)); } RValue operator>>(RValue lhs, RValue rhs) { return RValue(Nucleus::createAShr(lhs.value, rhs.value)); } RValue operator+=(Int &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(Int &lhs, RValue rhs) { return lhs = lhs - rhs; } RValue operator*=(Int &lhs, RValue rhs) { return lhs = lhs * rhs; } RValue operator/=(Int &lhs, RValue rhs) { return lhs = lhs / rhs; } RValue operator%=(Int &lhs, RValue rhs) { return lhs = lhs % rhs; } RValue operator&=(Int &lhs, RValue rhs) { return lhs = lhs & rhs; } RValue operator|=(Int &lhs, RValue rhs) { return lhs = lhs | rhs; } RValue operator^=(Int &lhs, RValue rhs) { return lhs = lhs ^ rhs; } RValue operator<<=(Int &lhs, RValue rhs) { return lhs = lhs << rhs; } RValue operator>>=(Int &lhs, RValue rhs) { return lhs = lhs >> rhs; } RValue operator+(RValue val) { return val; } RValue operator-(RValue val) { return RValue(Nucleus::createNeg(val.value)); } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue operator++(Int &val, int) // Post-increment { RValue res = val; Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); val.storeValue(inc); return res; } const Int &operator++(Int &val) // Pre-increment { Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); val.storeValue(inc); return val; } RValue operator--(Int &val, int) // Post-decrement { RValue res = val; Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); val.storeValue(inc); return res; } const Int &operator--(Int &val) // Pre-decrement { Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); val.storeValue(inc); return val; } RValue operator<(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpSLT(lhs.value, rhs.value)); } RValue operator<=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpSLE(lhs.value, rhs.value)); } RValue operator>(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpSGT(lhs.value, rhs.value)); } RValue operator>=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpSGE(lhs.value, rhs.value)); } RValue operator!=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpNE(lhs.value, rhs.value)); } RValue operator==(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpEQ(lhs.value, rhs.value)); } RValue Max(RValue x, RValue y) { return IfThenElse(x > y, x, y); } RValue Min(RValue x, RValue y) { return IfThenElse(x < y, x, y); } RValue Clamp(RValue x, RValue min, RValue max) { return Min(Max(x, min), max); } RValue RoundInt(RValue cast) { #if defined(__i386__) || defined(__x86_64__) return x86::cvtss2si(cast); #else return RValue(V(lowerRoundInt(V(cast.value), T(Int::getType())))); #endif } Type *Int::getType() { return T(llvm::Type::getInt32Ty(*::context)); } Long::Long(RValue cast) { Value *integer = Nucleus::createSExt(cast.value, Long::getType()); storeValue(integer); } Long::Long(RValue cast) { Value *integer = Nucleus::createZExt(cast.value, Long::getType()); storeValue(integer); } Long::Long(RValue rhs) { storeValue(rhs.value); } RValue Long::operator=(int64_t rhs) { return RValue(storeValue(Nucleus::createConstantLong(rhs))); } RValue Long::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue Long::operator=(const Long &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Long::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } RValue operator+=(Long &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(Long &lhs, RValue rhs) { return lhs = lhs - rhs; } RValue AddAtomic(RValue > x, RValue y) { return RValue(Nucleus::createAtomicAdd(x.value, y.value)); } Type *Long::getType() { return T(llvm::Type::getInt64Ty(*::context)); } UInt::UInt(Argument argument) { storeValue(argument.value); } UInt::UInt(RValue cast) { Value *integer = Nucleus::createZExt(cast.value, UInt::getType()); storeValue(integer); } UInt::UInt(RValue cast) { Value *integer = Nucleus::createTrunc(cast.value, UInt::getType()); storeValue(integer); } UInt::UInt(RValue cast) { // Note: createFPToUI is broken, must perform conversion using createFPtoSI // Value *integer = Nucleus::createFPToUI(cast.value, UInt::getType()); // Smallest positive value representable in UInt, but not in Int const unsigned int ustart = 0x80000000u; const float ustartf = float(ustart); // If the value is negative, store 0, otherwise store the result of the conversion storeValue((~(As(cast) >> 31) & // Check if the value can be represented as an Int IfThenElse(cast >= ustartf, // If the value is too large, subtract ustart and re-add it after conversion. As(As(Int(cast - Float(ustartf))) + UInt(ustart)), // Otherwise, just convert normally Int(cast))).value); } UInt::UInt(int x) { storeValue(Nucleus::createConstantInt(x)); } UInt::UInt(unsigned int x) { storeValue(Nucleus::createConstantInt(x)); } UInt::UInt(RValue rhs) { storeValue(rhs.value); } UInt::UInt(RValue rhs) { storeValue(rhs.value); } UInt::UInt(const UInt &rhs) { Value *value = rhs.loadValue(); storeValue(value); } UInt::UInt(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } UInt::UInt(const Int &rhs) { Value *value = rhs.loadValue(); storeValue(value); } UInt::UInt(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } RValue UInt::operator=(unsigned int rhs) { return RValue(storeValue(Nucleus::createConstantInt(rhs))); } RValue UInt::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue UInt::operator=(RValue rhs) { storeValue(rhs.value); return RValue(rhs); } RValue UInt::operator=(const UInt &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue UInt::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue UInt::operator=(const Int &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue UInt::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } RValue operator*(RValue lhs, RValue rhs) { return RValue(Nucleus::createMul(lhs.value, rhs.value)); } RValue operator/(RValue lhs, RValue rhs) { return RValue(Nucleus::createUDiv(lhs.value, rhs.value)); } RValue operator%(RValue lhs, RValue rhs) { return RValue(Nucleus::createURem(lhs.value, rhs.value)); } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, RValue rhs) { return RValue(Nucleus::createShl(lhs.value, rhs.value)); } RValue operator>>(RValue lhs, RValue rhs) { return RValue(Nucleus::createLShr(lhs.value, rhs.value)); } RValue operator+=(UInt &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(UInt &lhs, RValue rhs) { return lhs = lhs - rhs; } RValue operator*=(UInt &lhs, RValue rhs) { return lhs = lhs * rhs; } RValue operator/=(UInt &lhs, RValue rhs) { return lhs = lhs / rhs; } RValue operator%=(UInt &lhs, RValue rhs) { return lhs = lhs % rhs; } RValue operator&=(UInt &lhs, RValue rhs) { return lhs = lhs & rhs; } RValue operator|=(UInt &lhs, RValue rhs) { return lhs = lhs | rhs; } RValue operator^=(UInt &lhs, RValue rhs) { return lhs = lhs ^ rhs; } RValue operator<<=(UInt &lhs, RValue rhs) { return lhs = lhs << rhs; } RValue operator>>=(UInt &lhs, RValue rhs) { return lhs = lhs >> rhs; } RValue operator+(RValue val) { return val; } RValue operator-(RValue val) { return RValue(Nucleus::createNeg(val.value)); } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue operator++(UInt &val, int) // Post-increment { RValue res = val; Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); val.storeValue(inc); return res; } const UInt &operator++(UInt &val) // Pre-increment { Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); val.storeValue(inc); return val; } RValue operator--(UInt &val, int) // Post-decrement { RValue res = val; Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); val.storeValue(inc); return res; } const UInt &operator--(UInt &val) // Pre-decrement { Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); val.storeValue(inc); return val; } RValue Max(RValue x, RValue y) { return IfThenElse(x > y, x, y); } RValue Min(RValue x, RValue y) { return IfThenElse(x < y, x, y); } RValue Clamp(RValue x, RValue min, RValue max) { return Min(Max(x, min), max); } RValue operator<(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpULT(lhs.value, rhs.value)); } RValue operator<=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpULE(lhs.value, rhs.value)); } RValue operator>(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpUGT(lhs.value, rhs.value)); } RValue operator>=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpUGE(lhs.value, rhs.value)); } RValue operator!=(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpNE(lhs.value, rhs.value)); } RValue operator==(RValue lhs, RValue rhs) { return RValue(Nucleus::createICmpEQ(lhs.value, rhs.value)); } // RValue RoundUInt(RValue cast) // { //#if defined(__i386__) || defined(__x86_64__) // return x86::cvtss2si(val); // FIXME: Unsigned //#else // return IfThenElse(cast > 0.0f, Int(cast + 0.5f), Int(cast - 0.5f)); //#endif // } Type *UInt::getType() { return T(llvm::Type::getInt32Ty(*::context)); } // Int2::Int2(RValue cast) // { // Value *extend = Nucleus::createZExt(cast.value, Long::getType()); // Value *vector = Nucleus::createBitCast(extend, Int2::getType()); // // int shuffle[2] = {0, 0}; // Value *replicate = Nucleus::createShuffleVector(vector, vector, shuffle); // // storeValue(replicate); // } Int2::Int2(RValue cast) { storeValue(Nucleus::createBitCast(cast.value, getType())); } Int2::Int2(int x, int y) { int64_t constantVector[2] = {x, y}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } Int2::Int2(RValue rhs) { storeValue(rhs.value); } Int2::Int2(const Int2 &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Int2::Int2(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Int2::Int2(RValue lo, RValue hi) { int shuffle[4] = {0, 4, 1, 5}; Value *packed = Nucleus::createShuffleVector(Int4(lo).loadValue(), Int4(hi).loadValue(), shuffle); storeValue(Nucleus::createBitCast(packed, Int2::getType())); } RValue Int2::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue Int2::operator=(const Int2 &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Int2::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } // RValue operator*(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createMul(lhs.value, rhs.value)); // } // RValue operator/(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createSDiv(lhs.value, rhs.value)); // } // RValue operator%(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createSRem(lhs.value, rhs.value)); // } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) // return RValue(Nucleus::createShl(lhs.value, rhs.value)); return x86::pslld(lhs, rhs); #else return As(V(lowerVectorShl(V(lhs.value), rhs))); #endif } RValue operator>>(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) // return RValue(Nucleus::createAShr(lhs.value, rhs.value)); return x86::psrad(lhs, rhs); #else return As(V(lowerVectorAShr(V(lhs.value), rhs))); #endif } RValue operator+=(Int2 &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(Int2 &lhs, RValue rhs) { return lhs = lhs - rhs; } // RValue operator*=(Int2 &lhs, RValue rhs) // { // return lhs = lhs * rhs; // } // RValue operator/=(Int2 &lhs, RValue rhs) // { // return lhs = lhs / rhs; // } // RValue operator%=(Int2 &lhs, RValue rhs) // { // return lhs = lhs % rhs; // } RValue operator&=(Int2 &lhs, RValue rhs) { return lhs = lhs & rhs; } RValue operator|=(Int2 &lhs, RValue rhs) { return lhs = lhs | rhs; } RValue operator^=(Int2 &lhs, RValue rhs) { return lhs = lhs ^ rhs; } RValue operator<<=(Int2 &lhs, unsigned char rhs) { return lhs = lhs << rhs; } RValue operator>>=(Int2 &lhs, unsigned char rhs) { return lhs = lhs >> rhs; } // RValue operator+(RValue val) // { // return val; // } // RValue operator-(RValue val) // { // return RValue(Nucleus::createNeg(val.value)); // } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue UnpackLow(RValue x, RValue y) { int shuffle[4] = {0, 4, 1, 5}; // Real type is v4i32 return As(Nucleus::createShuffleVector(x.value, y.value, shuffle)); } RValue UnpackHigh(RValue x, RValue y) { int shuffle[4] = {0, 4, 1, 5}; // Real type is v4i32 auto lowHigh = RValue(Nucleus::createShuffleVector(x.value, y.value, shuffle)); return As(Swizzle(lowHigh, 0xEE)); } RValue Extract(RValue val, int i) { return RValue(Nucleus::createExtractElement(val.value, Int::getType(), i)); } RValue Insert(RValue val, RValue element, int i) { return RValue(Nucleus::createInsertElement(val.value, element.value, i)); } Type *Int2::getType() { return T(Type_v2i32); } UInt2::UInt2(unsigned int x, unsigned int y) { int64_t constantVector[2] = {x, y}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } UInt2::UInt2(RValue rhs) { storeValue(rhs.value); } UInt2::UInt2(const UInt2 &rhs) { Value *value = rhs.loadValue(); storeValue(value); } UInt2::UInt2(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } RValue UInt2::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue UInt2::operator=(const UInt2 &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue UInt2::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } // RValue operator*(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createMul(lhs.value, rhs.value)); // } // RValue operator/(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createUDiv(lhs.value, rhs.value)); // } // RValue operator%(RValue lhs, RValue rhs) // { // return RValue(Nucleus::createURem(lhs.value, rhs.value)); // } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) // return RValue(Nucleus::createShl(lhs.value, rhs.value)); return As(x86::pslld(As(lhs), rhs)); #else return As(V(lowerVectorShl(V(lhs.value), rhs))); #endif } RValue operator>>(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) // return RValue(Nucleus::createLShr(lhs.value, rhs.value)); return x86::psrld(lhs, rhs); #else return As(V(lowerVectorLShr(V(lhs.value), rhs))); #endif } RValue operator+=(UInt2 &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(UInt2 &lhs, RValue rhs) { return lhs = lhs - rhs; } // RValue operator*=(UInt2 &lhs, RValue rhs) // { // return lhs = lhs * rhs; // } // RValue operator/=(UInt2 &lhs, RValue rhs) // { // return lhs = lhs / rhs; // } // RValue operator%=(UInt2 &lhs, RValue rhs) // { // return lhs = lhs % rhs; // } RValue operator&=(UInt2 &lhs, RValue rhs) { return lhs = lhs & rhs; } RValue operator|=(UInt2 &lhs, RValue rhs) { return lhs = lhs | rhs; } RValue operator^=(UInt2 &lhs, RValue rhs) { return lhs = lhs ^ rhs; } RValue operator<<=(UInt2 &lhs, unsigned char rhs) { return lhs = lhs << rhs; } RValue operator>>=(UInt2 &lhs, unsigned char rhs) { return lhs = lhs >> rhs; } // RValue operator+(RValue val) // { // return val; // } // RValue operator-(RValue val) // { // return RValue(Nucleus::createNeg(val.value)); // } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } Type *UInt2::getType() { return T(Type_v2i32); } Int4::Int4() : XYZW(this) { } Int4::Int4(RValue cast) : XYZW(this) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { *this = x86::pmovzxbd(As(cast)); } else #endif { int swizzle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; Value *a = Nucleus::createBitCast(cast.value, Byte16::getType()); Value *b = Nucleus::createShuffleVector(a, Nucleus::createNullValue(Byte16::getType()), swizzle); int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11}; Value *c = Nucleus::createBitCast(b, Short8::getType()); Value *d = Nucleus::createShuffleVector(c, Nucleus::createNullValue(Short8::getType()), swizzle2); *this = As(d); } } Int4::Int4(RValue cast) : XYZW(this) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { *this = x86::pmovsxbd(As(cast)); } else #endif { int swizzle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}; Value *a = Nucleus::createBitCast(cast.value, Byte16::getType()); Value *b = Nucleus::createShuffleVector(a, a, swizzle); int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3}; Value *c = Nucleus::createBitCast(b, Short8::getType()); Value *d = Nucleus::createShuffleVector(c, c, swizzle2); *this = As(d) >> 24; } } Int4::Int4(RValue cast) : XYZW(this) { Value *xyzw = Nucleus::createFPToSI(cast.value, Int4::getType()); storeValue(xyzw); } Int4::Int4(RValue cast) : XYZW(this) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { *this = x86::pmovsxwd(As(cast)); } else #endif { int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3}; Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle); *this = As(c) >> 16; } } Int4::Int4(RValue cast) : XYZW(this) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { *this = x86::pmovzxwd(As(cast)); } else #endif { int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle); *this = As(c); } } Int4::Int4(int xyzw) : XYZW(this) { constant(xyzw, xyzw, xyzw, xyzw); } Int4::Int4(int x, int yzw) : XYZW(this) { constant(x, yzw, yzw, yzw); } Int4::Int4(int x, int y, int zw) : XYZW(this) { constant(x, y, zw, zw); } Int4::Int4(int x, int y, int z, int w) : XYZW(this) { constant(x, y, z, w); } void Int4::constant(int x, int y, int z, int w) { int64_t constantVector[4] = {x, y, z, w}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } Int4::Int4(RValue rhs) : XYZW(this) { storeValue(rhs.value); } Int4::Int4(const Int4 &rhs) : XYZW(this) { Value *value = rhs.loadValue(); storeValue(value); } Int4::Int4(const Reference &rhs) : XYZW(this) { Value *value = rhs.loadValue(); storeValue(value); } Int4::Int4(RValue rhs) : XYZW(this) { storeValue(rhs.value); } Int4::Int4(const UInt4 &rhs) : XYZW(this) { Value *value = rhs.loadValue(); storeValue(value); } Int4::Int4(const Reference &rhs) : XYZW(this) { Value *value = rhs.loadValue(); storeValue(value); } Int4::Int4(RValue lo, RValue hi) : XYZW(this) { int shuffle[4] = {0, 1, 4, 5}; // Real type is v4i32 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle); storeValue(packed); } Int4::Int4(RValue rhs) : XYZW(this) { Value *vector = loadValue(); Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); int swizzle[4] = {0, 0, 0, 0}; Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); storeValue(replicate); } Int4::Int4(const Int &rhs) : XYZW(this) { *this = RValue(rhs.loadValue()); } Int4::Int4(const Reference &rhs) : XYZW(this) { *this = RValue(rhs.loadValue()); } RValue Int4::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue Int4::operator=(const Int4 &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Int4::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } RValue operator*(RValue lhs, RValue rhs) { return RValue(Nucleus::createMul(lhs.value, rhs.value)); } RValue operator/(RValue lhs, RValue rhs) { return RValue(Nucleus::createSDiv(lhs.value, rhs.value)); } RValue operator%(RValue lhs, RValue rhs) { return RValue(Nucleus::createSRem(lhs.value, rhs.value)); } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) return x86::pslld(lhs, rhs); #else return As(V(lowerVectorShl(V(lhs.value), rhs))); #endif } RValue operator>>(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) return x86::psrad(lhs, rhs); #else return As(V(lowerVectorAShr(V(lhs.value), rhs))); #endif } RValue operator<<(RValue lhs, RValue rhs) { return RValue(Nucleus::createShl(lhs.value, rhs.value)); } RValue operator>>(RValue lhs, RValue rhs) { return RValue(Nucleus::createAShr(lhs.value, rhs.value)); } RValue operator+=(Int4 &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(Int4 &lhs, RValue rhs) { return lhs = lhs - rhs; } RValue operator*=(Int4 &lhs, RValue rhs) { return lhs = lhs * rhs; } // RValue operator/=(Int4 &lhs, RValue rhs) // { // return lhs = lhs / rhs; // } // RValue operator%=(Int4 &lhs, RValue rhs) // { // return lhs = lhs % rhs; // } RValue operator&=(Int4 &lhs, RValue rhs) { return lhs = lhs & rhs; } RValue operator|=(Int4 &lhs, RValue rhs) { return lhs = lhs | rhs; } RValue operator^=(Int4 &lhs, RValue rhs) { return lhs = lhs ^ rhs; } RValue operator<<=(Int4 &lhs, unsigned char rhs) { return lhs = lhs << rhs; } RValue operator>>=(Int4 &lhs, unsigned char rhs) { return lhs = lhs >> rhs; } RValue operator+(RValue val) { return val; } RValue operator-(RValue val) { return RValue(Nucleus::createNeg(val.value)); } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue CmpEQ(RValue x, RValue y) { // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 // Restore the following line when LLVM is updated to a version where this issue is fixed. // return RValue(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())); return RValue(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); } RValue CmpLT(RValue x, RValue y) { // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 // Restore the following line when LLVM is updated to a version where this issue is fixed. // return RValue(Nucleus::createSExt(Nucleus::createICmpSLT(x.value, y.value), Int4::getType())); return RValue(Nucleus::createSExt(Nucleus::createICmpSGE(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); } RValue CmpLE(RValue x, RValue y) { // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 // Restore the following line when LLVM is updated to a version where this issue is fixed. // return RValue(Nucleus::createSExt(Nucleus::createICmpSLE(x.value, y.value), Int4::getType())); return RValue(Nucleus::createSExt(Nucleus::createICmpSGT(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); } RValue CmpNEQ(RValue x, RValue y) { // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 // Restore the following line when LLVM is updated to a version where this issue is fixed. // return RValue(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())); return RValue(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); } RValue CmpNLT(RValue x, RValue y) { // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 // Restore the following line when LLVM is updated to a version where this issue is fixed. // return RValue(Nucleus::createSExt(Nucleus::createICmpSGE(x.value, y.value), Int4::getType())); return RValue(Nucleus::createSExt(Nucleus::createICmpSLT(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); } RValue CmpNLE(RValue x, RValue y) { // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 // Restore the following line when LLVM is updated to a version where this issue is fixed. // return RValue(Nucleus::createSExt(Nucleus::createICmpSGT(x.value, y.value), Int4::getType())); return RValue(Nucleus::createSExt(Nucleus::createICmpSLE(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); } RValue Max(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { return x86::pmaxsd(x, y); } else #endif { RValue greater = CmpNLE(x, y); return (x & greater) | (y & ~greater); } } RValue Min(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { return x86::pminsd(x, y); } else #endif { RValue less = CmpLT(x, y); return (x & less) | (y & ~less); } } RValue RoundInt(RValue cast) { #if defined(__i386__) || defined(__x86_64__) return x86::cvtps2dq(cast); #else return As(V(lowerRoundInt(V(cast.value), T(Int4::getType())))); #endif } RValue PackSigned(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::packssdw(x, y); #else return As(V(lowerPack(V(x.value), V(y.value), true))); #endif } RValue PackUnsigned(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::packusdw(x, y); #else return As(V(lowerPack(V(x.value), V(y.value), false))); #endif } RValue Extract(RValue x, int i) { return RValue(Nucleus::createExtractElement(x.value, Int::getType(), i)); } RValue Insert(RValue x, RValue element, int i) { return RValue(Nucleus::createInsertElement(x.value, element.value, i)); } RValue SignMask(RValue x) { #if defined(__i386__) || defined(__x86_64__) return x86::movmskps(As(x)); #else return As(V(lowerSignMask(V(x.value), T(Int::getType())))); #endif } RValue Swizzle(RValue x, unsigned char select) { return RValue(createSwizzle4(x.value, select)); } Type *Int4::getType() { return T(llvm::VectorType::get(T(Int::getType()), 4)); } UInt4::UInt4() : XYZW(this) { } UInt4::UInt4(RValue cast) : XYZW(this) { // Note: createFPToUI is broken, must perform conversion using createFPtoSI // Value *xyzw = Nucleus::createFPToUI(cast.value, UInt4::getType()); // Smallest positive value representable in UInt, but not in Int const unsigned int ustart = 0x80000000u; const float ustartf = float(ustart); // Check if the value can be represented as an Int Int4 uiValue = CmpNLT(cast, Float4(ustartf)); // If the value is too large, subtract ustart and re-add it after conversion. uiValue = (uiValue & As(As(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | // Otherwise, just convert normally (~uiValue & Int4(cast)); // If the value is negative, store 0, otherwise store the result of the conversion storeValue((~(As(cast) >> 31) & uiValue).value); } UInt4::UInt4(int xyzw) : XYZW(this) { constant(xyzw, xyzw, xyzw, xyzw); } UInt4::UInt4(int x, int yzw) : XYZW(this) { constant(x, yzw, yzw, yzw); } UInt4::UInt4(int x, int y, int zw) : XYZW(this) { constant(x, y, zw, zw); } UInt4::UInt4(int x, int y, int z, int w) : XYZW(this) { constant(x, y, z, w); } void UInt4::constant(int x, int y, int z, int w) { int64_t constantVector[4] = {x, y, z, w}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } UInt4::UInt4(RValue rhs) : XYZW(this) { storeValue(rhs.value); } UInt4::UInt4(const UInt4 &rhs) : XYZW(this) { Value *value = rhs.loadValue(); storeValue(value); } UInt4::UInt4(const Reference &rhs) : XYZW(this) { Value *value = rhs.loadValue(); storeValue(value); } UInt4::UInt4(RValue rhs) : XYZW(this) { storeValue(rhs.value); } UInt4::UInt4(const Int4 &rhs) : XYZW(this) { Value *value = rhs.loadValue(); storeValue(value); } UInt4::UInt4(const Reference &rhs) : XYZW(this) { Value *value = rhs.loadValue(); storeValue(value); } UInt4::UInt4(RValue lo, RValue hi) : XYZW(this) { int shuffle[4] = {0, 1, 4, 5}; // Real type is v4i32 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle); storeValue(packed); } RValue UInt4::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue UInt4::operator=(const UInt4 &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue UInt4::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createSub(lhs.value, rhs.value)); } RValue operator*(RValue lhs, RValue rhs) { return RValue(Nucleus::createMul(lhs.value, rhs.value)); } RValue operator/(RValue lhs, RValue rhs) { return RValue(Nucleus::createUDiv(lhs.value, rhs.value)); } RValue operator%(RValue lhs, RValue rhs) { return RValue(Nucleus::createURem(lhs.value, rhs.value)); } RValue operator&(RValue lhs, RValue rhs) { return RValue(Nucleus::createAnd(lhs.value, rhs.value)); } RValue operator|(RValue lhs, RValue rhs) { return RValue(Nucleus::createOr(lhs.value, rhs.value)); } RValue operator^(RValue lhs, RValue rhs) { return RValue(Nucleus::createXor(lhs.value, rhs.value)); } RValue operator<<(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) return As(x86::pslld(As(lhs), rhs)); #else return As(V(lowerVectorShl(V(lhs.value), rhs))); #endif } RValue operator>>(RValue lhs, unsigned char rhs) { #if defined(__i386__) || defined(__x86_64__) return x86::psrld(lhs, rhs); #else return As(V(lowerVectorLShr(V(lhs.value), rhs))); #endif } RValue operator<<(RValue lhs, RValue rhs) { return RValue(Nucleus::createShl(lhs.value, rhs.value)); } RValue operator>>(RValue lhs, RValue rhs) { return RValue(Nucleus::createLShr(lhs.value, rhs.value)); } RValue operator+=(UInt4 &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(UInt4 &lhs, RValue rhs) { return lhs = lhs - rhs; } RValue operator*=(UInt4 &lhs, RValue rhs) { return lhs = lhs * rhs; } // RValue operator/=(UInt4 &lhs, RValue rhs) // { // return lhs = lhs / rhs; // } // RValue operator%=(UInt4 &lhs, RValue rhs) // { // return lhs = lhs % rhs; // } RValue operator&=(UInt4 &lhs, RValue rhs) { return lhs = lhs & rhs; } RValue operator|=(UInt4 &lhs, RValue rhs) { return lhs = lhs | rhs; } RValue operator^=(UInt4 &lhs, RValue rhs) { return lhs = lhs ^ rhs; } RValue operator<<=(UInt4 &lhs, unsigned char rhs) { return lhs = lhs << rhs; } RValue operator>>=(UInt4 &lhs, unsigned char rhs) { return lhs = lhs >> rhs; } RValue operator+(RValue val) { return val; } RValue operator-(RValue val) { return RValue(Nucleus::createNeg(val.value)); } RValue operator~(RValue val) { return RValue(Nucleus::createNot(val.value)); } RValue CmpEQ(RValue x, RValue y) { // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 // Restore the following line when LLVM is updated to a version where this issue is fixed. // return RValue(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())); return RValue(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())) ^ UInt4(0xFFFFFFFF); } RValue CmpLT(RValue x, RValue y) { return RValue(Nucleus::createSExt(Nucleus::createICmpULT(x.value, y.value), Int4::getType())); } RValue CmpLE(RValue x, RValue y) { // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 // Restore the following line when LLVM is updated to a version where this issue is fixed. // return RValue(Nucleus::createSExt(Nucleus::createICmpULE(x.value, y.value), Int4::getType())); return RValue(Nucleus::createSExt(Nucleus::createICmpUGT(x.value, y.value), Int4::getType())) ^ UInt4(0xFFFFFFFF); } RValue CmpNEQ(RValue x, RValue y) { return RValue(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())); } RValue CmpNLT(RValue x, RValue y) { // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 // Restore the following line when LLVM is updated to a version where this issue is fixed. // return RValue(Nucleus::createSExt(Nucleus::createICmpUGE(x.value, y.value), Int4::getType())); return RValue(Nucleus::createSExt(Nucleus::createICmpULT(x.value, y.value), Int4::getType())) ^ UInt4(0xFFFFFFFF); } RValue CmpNLE(RValue x, RValue y) { return RValue(Nucleus::createSExt(Nucleus::createICmpUGT(x.value, y.value), Int4::getType())); } RValue Max(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { return x86::pmaxud(x, y); } else #endif { RValue greater = CmpNLE(x, y); return (x & greater) | (y & ~greater); } } RValue Min(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { return x86::pminud(x, y); } else #endif { RValue less = CmpLT(x, y); return (x & less) | (y & ~less); } } Type *UInt4::getType() { return T(llvm::VectorType::get(T(UInt::getType()), 4)); } Half::Half(RValue cast) { UInt fp32i = As(cast); UInt abs = fp32i & 0x7FFFFFFF; UShort fp16i((fp32i & 0x80000000) >> 16); // sign If(abs > 0x47FFEFFF) // Infinity { fp16i |= UShort(0x7FFF); } Else { If(abs < 0x38800000) // Denormal { Int mantissa = (abs & 0x007FFFFF) | 0x00800000; Int e = 113 - (abs >> 23); abs = IfThenElse(e < 24, mantissa >> e, Int(0)); fp16i |= UShort((abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13); } Else { fp16i |= UShort((abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13); } } storeValue(fp16i.loadValue()); } Type *Half::getType() { return T(llvm::Type::getInt16Ty(*::context)); } Float::Float(RValue cast) { Value *integer = Nucleus::createSIToFP(cast.value, Float::getType()); storeValue(integer); } Float::Float(RValue cast) { RValue result = Float(Int(cast & UInt(0x7FFFFFFF))) + As((As(cast) >> 31) & As(Float(0x80000000u))); storeValue(result.value); } Float::Float(RValue cast) { Int fp16i(As(cast)); Int s = (fp16i >> 15) & 0x00000001; Int e = (fp16i >> 10) & 0x0000001F; Int m = fp16i & 0x000003FF; UInt fp32i(s << 31); If(e == 0) { If(m != 0) { While((m & 0x00000400) == 0) { m <<= 1; e -= 1; } fp32i |= As(((e + (127 - 15) + 1) << 23) | ((m & ~0x00000400) << 13)); } } Else { fp32i |= As(((e + (127 - 15)) << 23) | (m << 13)); } storeValue(As(fp32i).value); } Float::Float(float x) { storeValue(Nucleus::createConstantFloat(x)); } Float::Float(RValue rhs) { storeValue(rhs.value); } Float::Float(const Float &rhs) { Value *value = rhs.loadValue(); storeValue(value); } Float::Float(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); } RValue Float::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue Float::operator=(const Float &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Float::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createFAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createFSub(lhs.value, rhs.value)); } RValue operator*(RValue lhs, RValue rhs) { return RValue(Nucleus::createFMul(lhs.value, rhs.value)); } RValue operator/(RValue lhs, RValue rhs) { return RValue(Nucleus::createFDiv(lhs.value, rhs.value)); } RValue operator+=(Float &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(Float &lhs, RValue rhs) { return lhs = lhs - rhs; } RValue operator*=(Float &lhs, RValue rhs) { return lhs = lhs * rhs; } RValue operator/=(Float &lhs, RValue rhs) { return lhs = lhs / rhs; } RValue operator+(RValue val) { return val; } RValue operator-(RValue val) { return RValue(Nucleus::createFNeg(val.value)); } RValue operator<(RValue lhs, RValue rhs) { return RValue(Nucleus::createFCmpOLT(lhs.value, rhs.value)); } RValue operator<=(RValue lhs, RValue rhs) { return RValue(Nucleus::createFCmpOLE(lhs.value, rhs.value)); } RValue operator>(RValue lhs, RValue rhs) { return RValue(Nucleus::createFCmpOGT(lhs.value, rhs.value)); } RValue operator>=(RValue lhs, RValue rhs) { return RValue(Nucleus::createFCmpOGE(lhs.value, rhs.value)); } RValue operator!=(RValue lhs, RValue rhs) { return RValue(Nucleus::createFCmpONE(lhs.value, rhs.value)); } RValue operator==(RValue lhs, RValue rhs) { return RValue(Nucleus::createFCmpOEQ(lhs.value, rhs.value)); } RValue Abs(RValue x) { return IfThenElse(x > 0.0f, x, -x); } RValue Max(RValue x, RValue y) { return IfThenElse(x > y, x, y); } RValue Min(RValue x, RValue y) { return IfThenElse(x < y, x, y); } RValue Rcp_pp(RValue x, bool exactAtPow2) { #if defined(__i386__) || defined(__x86_64__) if(exactAtPow2) { // rcpss uses a piecewise-linear approximation which minimizes the relative error // but is not exact at power-of-two values. Rectify by multiplying by the inverse. return x86::rcpss(x) * Float(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f)))); } return x86::rcpss(x); #else return As(V(lowerRCP(V(x.value)))); #endif } RValue RcpSqrt_pp(RValue x) { #if defined(__i386__) || defined(__x86_64__) return x86::rsqrtss(x); #else return As(V(lowerRSQRT(V(x.value)))); #endif } RValue Sqrt(RValue x) { #if defined(__i386__) || defined(__x86_64__) return x86::sqrtss(x); #else return As(V(lowerSQRT(V(x.value)))); #endif } RValue Round(RValue x) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { return x86::roundss(x, 0); } else { return Float4(Round(Float4(x))).x; } #else return RValue(V(lowerRound(V(x.value)))); #endif } RValue Trunc(RValue x) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { return x86::roundss(x, 3); } else { return Float(Int(x)); // Rounded toward zero } #else return RValue(V(lowerTrunc(V(x.value)))); #endif } RValue Frac(RValue x) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { return x - x86::floorss(x); } else { return Float4(Frac(Float4(x))).x; } #else // x - floor(x) can be 1.0 for very small negative x. // Clamp against the value just below 1.0. return Min(x - Floor(x), As(Int(0x3F7FFFFF))); #endif } RValue Floor(RValue x) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { return x86::floorss(x); } else { return Float4(Floor(Float4(x))).x; } #else return RValue(V(lowerFloor(V(x.value)))); #endif } RValue Ceil(RValue x) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { return x86::ceilss(x); } else #endif { return Float4(Ceil(Float4(x))).x; } } Type *Float::getType() { return T(llvm::Type::getFloatTy(*::context)); } Float2::Float2(RValue cast) { storeValue(Nucleus::createBitCast(cast.value, getType())); } Type *Float2::getType() { return T(Type_v2f32); } Float4::Float4(RValue cast) : XYZW(this) { Value *a = Int4(cast).loadValue(); Value *xyzw = Nucleus::createSIToFP(a, Float4::getType()); storeValue(xyzw); } Float4::Float4(RValue cast) : XYZW(this) { Value *a = Int4(cast).loadValue(); Value *xyzw = Nucleus::createSIToFP(a, Float4::getType()); storeValue(xyzw); } Float4::Float4(RValue cast) : XYZW(this) { Int4 c(cast); storeValue(Nucleus::createSIToFP(RValue(c).value, Float4::getType())); } Float4::Float4(RValue cast) : XYZW(this) { Int4 c(cast); storeValue(Nucleus::createSIToFP(RValue(c).value, Float4::getType())); } Float4::Float4(RValue cast) : XYZW(this) { Value *xyzw = Nucleus::createSIToFP(cast.value, Float4::getType()); storeValue(xyzw); } Float4::Float4(RValue cast) : XYZW(this) { RValue result = Float4(Int4(cast & UInt4(0x7FFFFFFF))) + As((As(cast) >> 31) & As(Float4(0x80000000u))); storeValue(result.value); } Float4::Float4() : XYZW(this) { } Float4::Float4(float xyzw) : XYZW(this) { constant(xyzw, xyzw, xyzw, xyzw); } Float4::Float4(float x, float yzw) : XYZW(this) { constant(x, yzw, yzw, yzw); } Float4::Float4(float x, float y, float zw) : XYZW(this) { constant(x, y, zw, zw); } Float4::Float4(float x, float y, float z, float w) : XYZW(this) { constant(x, y, z, w); } void Float4::constant(float x, float y, float z, float w) { double constantVector[4] = {x, y, z, w}; storeValue(Nucleus::createConstantVector(constantVector, getType())); } Float4::Float4(RValue rhs) : XYZW(this) { storeValue(rhs.value); } Float4::Float4(const Float4 &rhs) : XYZW(this) { Value *value = rhs.loadValue(); storeValue(value); } Float4::Float4(const Reference &rhs) : XYZW(this) { Value *value = rhs.loadValue(); storeValue(value); } Float4::Float4(RValue rhs) : XYZW(this) { Value *vector = loadValue(); Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); int swizzle[4] = {0, 0, 0, 0}; Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); storeValue(replicate); } Float4::Float4(const Float &rhs) : XYZW(this) { *this = RValue(rhs.loadValue()); } Float4::Float4(const Reference &rhs) : XYZW(this) { *this = RValue(rhs.loadValue()); } RValue Float4::operator=(float x) { return *this = Float4(x, x, x, x); } RValue Float4::operator=(RValue rhs) { storeValue(rhs.value); return rhs; } RValue Float4::operator=(const Float4 &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Float4::operator=(const Reference &rhs) { Value *value = rhs.loadValue(); storeValue(value); return RValue(value); } RValue Float4::operator=(RValue rhs) { return *this = Float4(rhs); } RValue Float4::operator=(const Float &rhs) { return *this = Float4(rhs); } RValue Float4::operator=(const Reference &rhs) { return *this = Float4(rhs); } RValue operator+(RValue lhs, RValue rhs) { return RValue(Nucleus::createFAdd(lhs.value, rhs.value)); } RValue operator-(RValue lhs, RValue rhs) { return RValue(Nucleus::createFSub(lhs.value, rhs.value)); } RValue operator*(RValue lhs, RValue rhs) { return RValue(Nucleus::createFMul(lhs.value, rhs.value)); } RValue operator/(RValue lhs, RValue rhs) { return RValue(Nucleus::createFDiv(lhs.value, rhs.value)); } RValue operator%(RValue lhs, RValue rhs) { return RValue(Nucleus::createFRem(lhs.value, rhs.value)); } RValue operator+=(Float4 &lhs, RValue rhs) { return lhs = lhs + rhs; } RValue operator-=(Float4 &lhs, RValue rhs) { return lhs = lhs - rhs; } RValue operator*=(Float4 &lhs, RValue rhs) { return lhs = lhs * rhs; } RValue operator/=(Float4 &lhs, RValue rhs) { return lhs = lhs / rhs; } RValue operator%=(Float4 &lhs, RValue rhs) { return lhs = lhs % rhs; } RValue operator+(RValue val) { return val; } RValue operator-(RValue val) { return RValue(Nucleus::createFNeg(val.value)); } RValue Abs(RValue x) { Value *vector = Nucleus::createBitCast(x.value, Int4::getType()); int64_t constantVector[4] = {0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF}; Value *result = Nucleus::createAnd(vector, Nucleus::createConstantVector(constantVector, Int4::getType())); return As(result); } RValue Max(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::maxps(x, y); #else return As(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OGT))); #endif } RValue Min(RValue x, RValue y) { #if defined(__i386__) || defined(__x86_64__) return x86::minps(x, y); #else return As(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OLT))); #endif } RValue Rcp_pp(RValue x, bool exactAtPow2) { #if defined(__i386__) || defined(__x86_64__) if(exactAtPow2) { // rcpps uses a piecewise-linear approximation which minimizes the relative error // but is not exact at power-of-two values. Rectify by multiplying by the inverse. return x86::rcpps(x) * Float4(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f)))); } return x86::rcpps(x); #else return As(V(lowerRCP(V(x.value)))); #endif } RValue RcpSqrt_pp(RValue x) { #if defined(__i386__) || defined(__x86_64__) return x86::rsqrtps(x); #else return As(V(lowerRSQRT(V(x.value)))); #endif } RValue Sqrt(RValue x) { #if defined(__i386__) || defined(__x86_64__) return x86::sqrtps(x); #else return As(V(lowerSQRT(V(x.value)))); #endif } RValue Insert(RValue x, RValue element, int i) { return RValue(Nucleus::createInsertElement(x.value, element.value, i)); } RValue Extract(RValue x, int i) { return RValue(Nucleus::createExtractElement(x.value, Float::getType(), i)); } RValue Swizzle(RValue x, unsigned char select) { return RValue(createSwizzle4(x.value, select)); } RValue ShuffleLowHigh(RValue x, RValue y, unsigned char imm) { int shuffle[4] = { ((imm >> 0) & 0x03) + 0, ((imm >> 2) & 0x03) + 0, ((imm >> 4) & 0x03) + 4, ((imm >> 6) & 0x03) + 4, }; return RValue(Nucleus::createShuffleVector(x.value, y.value, shuffle)); } RValue UnpackLow(RValue x, RValue y) { int shuffle[4] = {0, 4, 1, 5}; return RValue(Nucleus::createShuffleVector(x.value, y.value, shuffle)); } RValue UnpackHigh(RValue x, RValue y) { int shuffle[4] = {2, 6, 3, 7}; return RValue(Nucleus::createShuffleVector(x.value, y.value, shuffle)); } RValue Mask(Float4 &lhs, RValue rhs, unsigned char select) { Value *vector = lhs.loadValue(); Value *result = createMask4(vector, rhs.value, select); lhs.storeValue(result); return RValue(result); } RValue SignMask(RValue x) { #if defined(__i386__) || defined(__x86_64__) return x86::movmskps(x); #else return As(V(lowerFPSignMask(V(x.value), T(Int::getType())))); #endif } RValue CmpEQ(RValue x, RValue y) { // return As(x86::cmpeqps(x, y)); return RValue(Nucleus::createSExt(Nucleus::createFCmpOEQ(x.value, y.value), Int4::getType())); } RValue CmpLT(RValue x, RValue y) { // return As(x86::cmpltps(x, y)); return RValue(Nucleus::createSExt(Nucleus::createFCmpOLT(x.value, y.value), Int4::getType())); } RValue CmpLE(RValue x, RValue y) { // return As(x86::cmpleps(x, y)); return RValue(Nucleus::createSExt(Nucleus::createFCmpOLE(x.value, y.value), Int4::getType())); } RValue CmpNEQ(RValue x, RValue y) { // return As(x86::cmpneqps(x, y)); return RValue(Nucleus::createSExt(Nucleus::createFCmpONE(x.value, y.value), Int4::getType())); } RValue CmpNLT(RValue x, RValue y) { // return As(x86::cmpnltps(x, y)); return RValue(Nucleus::createSExt(Nucleus::createFCmpOGE(x.value, y.value), Int4::getType())); } RValue CmpNLE(RValue x, RValue y) { // return As(x86::cmpnleps(x, y)); return RValue(Nucleus::createSExt(Nucleus::createFCmpOGT(x.value, y.value), Int4::getType())); } RValue IsInf(RValue x) { return CmpEQ(As(x) & Int4(0x7FFFFFFF), Int4(0x7F800000)); } RValue IsNan(RValue x) { return ~CmpEQ(x, x); } RValue Round(RValue x) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { return x86::roundps(x, 0); } else { return Float4(RoundInt(x)); } #else return RValue(V(lowerRound(V(x.value)))); #endif } RValue Trunc(RValue x) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { return x86::roundps(x, 3); } else { return Float4(Int4(x)); } #else return RValue(V(lowerTrunc(V(x.value)))); #endif } RValue Frac(RValue x) { Float4 frc; #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { frc = x - Floor(x); } else { frc = x - Float4(Int4(x)); // Signed fractional part. frc += As(As(CmpNLE(Float4(0.0f), frc)) & As(Float4(1.0f))); // Add 1.0 if negative. } #else frc = x - Floor(x); #endif // x - floor(x) can be 1.0 for very small negative x. // Clamp against the value just below 1.0. return Min(frc, As(Int4(0x3F7FFFFF))); } RValue Floor(RValue x) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { return x86::floorps(x); } else { return x - Frac(x); } #else return RValue(V(lowerFloor(V(x.value)))); #endif } RValue Ceil(RValue x) { #if defined(__i386__) || defined(__x86_64__) if(CPUID::supportsSSE4_1()) { return x86::ceilps(x); } else #endif { return -Floor(-x); } } Type *Float4::getType() { return T(llvm::VectorType::get(T(Float::getType()), 4)); } RValue> operator+(RValue> lhs, int offset) { return lhs + RValue(Nucleus::createConstantInt(offset)); } RValue> operator+(RValue> lhs, RValue offset) { return RValue>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, false)); } RValue> operator+(RValue> lhs, RValue offset) { return RValue>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, true)); } RValue> operator+=(Pointer &lhs, int offset) { return lhs = lhs + offset; } RValue> operator+=(Pointer &lhs, RValue offset) { return lhs = lhs + offset; } RValue> operator+=(Pointer &lhs, RValue offset) { return lhs = lhs + offset; } RValue> operator-(RValue> lhs, int offset) { return lhs + -offset; } RValue> operator-(RValue> lhs, RValue offset) { return lhs + -offset; } RValue> operator-(RValue> lhs, RValue offset) { return lhs + -offset; } RValue> operator-=(Pointer &lhs, int offset) { return lhs = lhs - offset; } RValue> operator-=(Pointer &lhs, RValue offset) { return lhs = lhs - offset; } RValue> operator-=(Pointer &lhs, RValue offset) { return lhs = lhs - offset; } void Return() { Nucleus::createRetVoid(); Nucleus::setInsertBlock(Nucleus::createBasicBlock()); Nucleus::createUnreachable(); } void Return(RValue ret) { Nucleus::createRet(ret.value); Nucleus::setInsertBlock(Nucleus::createBasicBlock()); Nucleus::createUnreachable(); } void branch(RValue cmp, BasicBlock *bodyBB, BasicBlock *endBB) { Nucleus::createCondBr(cmp.value, bodyBB, endBB); Nucleus::setInsertBlock(bodyBB); } RValue Ticks() { llvm::Function *rdtsc = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::readcyclecounter); return RValue(V(::builder->CreateCall(rdtsc))); } } namespace rr { #if defined(__i386__) || defined(__x86_64__) namespace x86 { RValue cvtss2si(RValue val) { llvm::Function *cvtss2si = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_cvtss2si); Float4 vector; vector.x = val; return RValue(V(::builder->CreateCall(cvtss2si, ARGS(V(RValue(vector).value))))); } RValue cvtps2dq(RValue val) { llvm::Function *cvtps2dq = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_cvtps2dq); return RValue(V(::builder->CreateCall(cvtps2dq, ARGS(V(val.value))))); } RValue rcpss(RValue val) { llvm::Function *rcpss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rcp_ss); Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0); return RValue(Nucleus::createExtractElement(V(::builder->CreateCall(rcpss, ARGS(V(vector)))), Float::getType(), 0)); } RValue sqrtss(RValue val) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *sqrtss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_sqrt_ss); Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0); return RValue(Nucleus::createExtractElement(V(::builder->CreateCall(sqrtss, ARGS(V(vector)))), Float::getType(), 0)); #else llvm::Function *sqrt = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::sqrt, {V(val.value)->getType()}); return RValue(V(::builder->CreateCall(sqrt, ARGS(V(val.value))))); #endif } RValue rsqrtss(RValue val) { llvm::Function *rsqrtss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rsqrt_ss); Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0); return RValue(Nucleus::createExtractElement(V(::builder->CreateCall(rsqrtss, ARGS(V(vector)))), Float::getType(), 0)); } RValue rcpps(RValue val) { llvm::Function *rcpps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rcp_ps); return RValue(V(::builder->CreateCall(rcpps, ARGS(V(val.value))))); } RValue sqrtps(RValue val) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *sqrtps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_sqrt_ps); #else llvm::Function *sqrtps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::sqrt, {V(val.value)->getType()}); #endif return RValue(V(::builder->CreateCall(sqrtps, ARGS(V(val.value))))); } RValue rsqrtps(RValue val) { llvm::Function *rsqrtps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rsqrt_ps); return RValue(V(::builder->CreateCall(rsqrtps, ARGS(V(val.value))))); } RValue maxps(RValue x, RValue y) { llvm::Function *maxps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_max_ps); return RValue(V(::builder->CreateCall2(maxps, ARGS(V(x.value), V(y.value))))); } RValue minps(RValue x, RValue y) { llvm::Function *minps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_min_ps); return RValue(V(::builder->CreateCall2(minps, ARGS(V(x.value), V(y.value))))); } RValue roundss(RValue val, unsigned char imm) { llvm::Function *roundss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_round_ss); Value *undef = V(llvm::UndefValue::get(T(Float4::getType()))); Value *vector = Nucleus::createInsertElement(undef, val.value, 0); return RValue(Nucleus::createExtractElement(V(::builder->CreateCall3(roundss, ARGS(V(undef), V(vector), V(Nucleus::createConstantInt(imm))))), Float::getType(), 0)); } RValue floorss(RValue val) { return roundss(val, 1); } RValue ceilss(RValue val) { return roundss(val, 2); } RValue roundps(RValue val, unsigned char imm) { llvm::Function *roundps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_round_ps); return RValue(V(::builder->CreateCall2(roundps, ARGS(V(val.value), V(Nucleus::createConstantInt(imm)))))); } RValue floorps(RValue val) { return roundps(val, 1); } RValue ceilps(RValue val) { return roundps(val, 2); } RValue pabsd(RValue x) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pabsd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_ssse3_pabs_d_128); return RValue(V(::builder->CreateCall(pabsd, ARGS(V(x.value))))); #else return RValue(V(lowerPABS(V(x.value)))); #endif } RValue paddsw(RValue x, RValue y) { llvm::Function *paddsw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_padds_w); return As(V(::builder->CreateCall2(paddsw, ARGS(V(x.value), V(y.value))))); } RValue psubsw(RValue x, RValue y) { llvm::Function *psubsw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubs_w); return As(V(::builder->CreateCall2(psubsw, ARGS(V(x.value), V(y.value))))); } RValue paddusw(RValue x, RValue y) { llvm::Function *paddusw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_paddus_w); return As(V(::builder->CreateCall2(paddusw, ARGS(V(x.value), V(y.value))))); } RValue psubusw(RValue x, RValue y) { llvm::Function *psubusw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubus_w); return As(V(::builder->CreateCall2(psubusw, ARGS(V(x.value), V(y.value))))); } RValue paddsb(RValue x, RValue y) { llvm::Function *paddsb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_padds_b); return As(V(::builder->CreateCall2(paddsb, ARGS(V(x.value), V(y.value))))); } RValue psubsb(RValue x, RValue y) { llvm::Function *psubsb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubs_b); return As(V(::builder->CreateCall2(psubsb, ARGS(V(x.value), V(y.value))))); } RValue paddusb(RValue x, RValue y) { llvm::Function *paddusb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_paddus_b); return As(V(::builder->CreateCall2(paddusb, ARGS(V(x.value), V(y.value))))); } RValue psubusb(RValue x, RValue y) { llvm::Function *psubusb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubus_b); return As(V(::builder->CreateCall2(psubusb, ARGS(V(x.value), V(y.value))))); } RValue pavgw(RValue x, RValue y) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pavgw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pavg_w); return As(V(::builder->CreateCall2(pavgw, ARGS(V(x.value), V(y.value))))); #else return As(V(lowerPAVG(V(x.value), V(y.value)))); #endif } RValue pmaxsw(RValue x, RValue y) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pmaxsw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmaxs_w); return As(V(::builder->CreateCall2(pmaxsw, ARGS(V(x.value), V(y.value))))); #else return As(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); #endif } RValue pminsw(RValue x, RValue y) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pminsw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmins_w); return As(V(::builder->CreateCall2(pminsw, ARGS(V(x.value), V(y.value))))); #else return As(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); #endif } RValue pcmpgtw(RValue x, RValue y) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pcmpgtw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pcmpgt_w); return As(V(::builder->CreateCall2(pcmpgtw, ARGS(V(x.value), V(y.value))))); #else return As(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType())))); #endif } RValue pcmpeqw(RValue x, RValue y) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pcmpeqw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pcmpeq_w); return As(V(::builder->CreateCall2(pcmpeqw, ARGS(V(x.value), V(y.value))))); #else return As(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType())))); #endif } RValue pcmpgtb(RValue x, RValue y) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pcmpgtb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pcmpgt_b); return As(V(::builder->CreateCall2(pcmpgtb, ARGS(V(x.value), V(y.value))))); #else return As(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); #endif } RValue pcmpeqb(RValue x, RValue y) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pcmpeqb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pcmpeq_b); return As(V(::builder->CreateCall2(pcmpeqb, ARGS(V(x.value), V(y.value))))); #else return As(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); #endif } RValue packssdw(RValue x, RValue y) { llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packssdw_128); return As(V(::builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value))))); } RValue packssdw(RValue x, RValue y) { llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packssdw_128); return RValue(V(::builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value))))); } RValue packsswb(RValue x, RValue y) { llvm::Function *packsswb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packsswb_128); return As(V(::builder->CreateCall2(packsswb, ARGS(V(x.value), V(y.value))))); } RValue packuswb(RValue x, RValue y) { llvm::Function *packuswb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packuswb_128); return As(V(::builder->CreateCall2(packuswb, ARGS(V(x.value), V(y.value))))); } RValue packusdw(RValue x, RValue y) { if(CPUID::supportsSSE4_1()) { llvm::Function *packusdw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_packusdw); return RValue(V(::builder->CreateCall2(packusdw, ARGS(V(x.value), V(y.value))))); } else { RValue bx = (x & ~(x >> 31)) - Int4(0x8000); RValue by = (y & ~(y >> 31)) - Int4(0x8000); return As(packssdw(bx, by) + Short8(0x8000u)); } } RValue psrlw(RValue x, unsigned char y) { llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_w); return As(V(::builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); } RValue psrlw(RValue x, unsigned char y) { llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_w); return RValue(V(::builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); } RValue psraw(RValue x, unsigned char y) { llvm::Function *psraw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_w); return As(V(::builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); } RValue psraw(RValue x, unsigned char y) { llvm::Function *psraw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_w); return RValue(V(::builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); } RValue psllw(RValue x, unsigned char y) { llvm::Function *psllw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_w); return As(V(::builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); } RValue psllw(RValue x, unsigned char y) { llvm::Function *psllw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_w); return RValue(V(::builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); } RValue pslld(RValue x, unsigned char y) { llvm::Function *pslld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_d); return As(V(::builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); } RValue pslld(RValue x, unsigned char y) { llvm::Function *pslld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_d); return RValue(V(::builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); } RValue psrad(RValue x, unsigned char y) { llvm::Function *psrad = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_d); return As(V(::builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); } RValue psrad(RValue x, unsigned char y) { llvm::Function *psrad = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_d); return RValue(V(::builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); } RValue psrld(RValue x, unsigned char y) { llvm::Function *psrld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_d); return As(V(::builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); } RValue psrld(RValue x, unsigned char y) { llvm::Function *psrld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_d); return RValue(V(::builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); } RValue pmaxsd(RValue x, RValue y) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pmaxsd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pmaxsd); return RValue(V(::builder->CreateCall2(pmaxsd, ARGS(V(x.value), V(y.value))))); #else return RValue(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); #endif } RValue pminsd(RValue x, RValue y) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pminsd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pminsd); return RValue(V(::builder->CreateCall2(pminsd, ARGS(V(x.value), V(y.value))))); #else return RValue(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); #endif } RValue pmaxud(RValue x, RValue y) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pmaxud = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pmaxud); return RValue(V(::builder->CreateCall2(pmaxud, ARGS(V(x.value), V(y.value))))); #else return RValue(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_UGT))); #endif } RValue pminud(RValue x, RValue y) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pminud = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pminud); return RValue(V(::builder->CreateCall2(pminud, ARGS(V(x.value), V(y.value))))); #else return RValue(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_ULT))); #endif } RValue pmulhw(RValue x, RValue y) { llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulh_w); return As(V(::builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value))))); } RValue pmulhuw(RValue x, RValue y) { llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulhu_w); return As(V(::builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value))))); } RValue pmaddwd(RValue x, RValue y) { llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmadd_wd); return As(V(::builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value))))); } RValue pmulhw(RValue x, RValue y) { llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulh_w); return RValue(V(::builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value))))); } RValue pmulhuw(RValue x, RValue y) { llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulhu_w); return RValue(V(::builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value))))); } RValue pmaddwd(RValue x, RValue y) { llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmadd_wd); return RValue(V(::builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value))))); } RValue movmskps(RValue x) { llvm::Function *movmskps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_movmsk_ps); return RValue(V(::builder->CreateCall(movmskps, ARGS(V(x.value))))); } RValue pmovmskb(RValue x) { llvm::Function *pmovmskb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmovmskb_128); return RValue(V(::builder->CreateCall(pmovmskb, ARGS(V(x.value))))) & 0xFF; } RValue pmovzxbd(RValue x) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pmovzxbd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pmovzxbd); return RValue(V(::builder->CreateCall(pmovzxbd, ARGS(V(x.value))))); #else return RValue(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); #endif } RValue pmovsxbd(RValue x) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pmovsxbd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pmovsxbd); return RValue(V(::builder->CreateCall(pmovsxbd, ARGS(V(x.value))))); #else return RValue(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); #endif } RValue pmovzxwd(RValue x) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pmovzxwd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pmovzxwd); return RValue(V(::builder->CreateCall(pmovzxwd, ARGS(V(x.value))))); #else return RValue(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); #endif } RValue pmovsxwd(RValue x) { #if REACTOR_LLVM_VERSION < 7 llvm::Function *pmovsxwd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pmovsxwd); return RValue(V(::builder->CreateCall(pmovsxwd, ARGS(V(x.value))))); #else return RValue(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); #endif } } #endif // defined(__i386__) || defined(__x86_64__) }