8042dc93e8
0140eeff Merge branch 'dev' 1efe14b2 change the original behavior of SetError 83c89c7a rename and fix indent 8be7ca93 Merge branch 'sbogusev-master' into dev 070b4c09 make l_err() inline with block scope static TLS l_error 9a4e6579 v5.97 d0ced1bc XBYAK_ONLY_CLASS_CPU is for only util::Cpu bb967ae7 replace uint32 with uint32_t etc. c306b8e5 update to v5.95 605e4224 use noexcept if C++11 or later 7a17c2c8 remove warning 5dfa4462 use constexpr if c++14 or later 18c9caaa Merge branch 'densamoilov-fix-mov-interface' into dev 3966ba9d fix mov interface be492be1 change the behavior of push((byte|word), imm) to cast imm to int8_t/int16_t d9696b54 Merge pull request #102 from igorsafo/master ea73267f Cpu: make getNumCores constant ff0b10e9 Merge pull request #101 from densamoilov/use-thread_local-when-supported 0c4eafc3 use thread_local for XBYAK_TLS when supported c1aea35e CodeGenerator::reset() calls ClearError() b4df97b1 Merge branch 'cursey-no-winsock2-header' 6a47bb0e v5.94 9a1749e6 define WIN32_LEAN_AND_MEAN for including winsock2.h after xbyak.h 42dddb74 Remove #include <winsock2.h> 615b85fa update doc 9cd796a9 rename XBYAK_NOEXCEPTION to XBYAK_NO_EXCEPTION 7cdf227f use static to avoid multiple instance 38a28dec test_nm.bat supports noexcept 0fdffc6b XBYAK_NOEXCEPTION for -fno-exceptions eda6e2a3 v5.92 5c26c8bb mov(rax, imm64) on 32-bit env with XBYAK64 6208e3ae throw exception if not supported amx sibmem 2 c6737d14 mov amx insts from avx512 34ea5c16 throw exception if not supported amx sibmem 6f93fe35 fix test of sizeof(Operand) 5b89c3b2 remove T_TMM 5ce32858 gen_amx.cpp is merged into gen_avx512.cpp fe4f965f remove my alias for tmm registers 92f904d8 bit_ contains 8192 98b51da9 extend mnemonics with Intel(R) AMX ISA 8d1b4c9e add generation of Intel(R) AMX ISA mnemonics 8ded45d1 add support of Intel(R) AMX ISA b23c4b02 v5.912 ffe32a60 Merge branch 'rsdubtso-master' e7b7fd2f use MAP_JIT on macOS regardless of Xcode version 82b70e66 v5.911 ; XBYAK_USE_MMAP_ALLOCATOR is defined 2f6d9e34 fix test for mac a7d10a1e add link to GitHub Sponsor 96076265 accept k0 mask register (it means no mask) 7e3167e4 kmov{b,w,d,q} throws for unsupported reg f487d7b7 Merge pull request #91 from marcelotrevisani/patch-1 dc9e6a79 Possibility to specify a different PREFIX 5fc69fc8 remove warning of test e69e0b42 fix typo of type of Zmi 34f797e8 perf does not recognize too short function name 6cc0f4df Consider max defined as a macro on Windows 5722393d fix for zeroed-out 0xb leaf 6a4459a8 Merge branch 'tyfkda-feature/fix-segfault-in-calc' 47922ed9 Fix segmentation fault in calc sample 8f696e93 add test_avx512 to bat 00114d79 add .travis.yml a29fa27b refactor test 508b543c fix error of vfpclasspd 0d54f1b1 fix for windows 4da8fd4e add setDefaultJmpNEAR da7f7317 revert to the behavior before v5.84 if -fno-operator-names is defined 7dac9f61 update to v5.85 fe639332 enable MAP_JIT only if mojave or later 4443d791 specify MAP_JIT mmap flag on macOS 20ee4c2d update doc ca0e8395 [changed] XBYAK_NO_OP_NAMES is defined f32836da remove exit(1) a1e9adf2 v5.82 08b8b1ba Support AMD Zen New Instructions. 2501ba9a remove *.user and *.vcproj 5c2ea988 Merge branch 'jrmwng-feature/upgrade-to-vs2017/jrmwng' 35847f7a Merge branch 'feature/upgrade-to-vs2017/jrmwng' of https://github.com/jrmwng/xbyak into jrmwng-feature/upgrade-to-vs2017/jrmwng ef267775 address "warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/SAFESEH' specification" 4a6c59bb address a conflict of sharing intermediate directory by different projects 9577cbf3 inherit "some output locations" from parent or project defaults 6c5f7186 upgrade projects from VS2018 to VS2017 4ca0434b v5.81 72b4e95d add lds/lss/les/lfs/lgs cc8f037c fix ; move ERR_INTERNAL to the end 9e9ec1c3 add repe, repne, repne, prez eea0edc3 add some fpu mnemonics 06235fa6 add loop/loope/loopne 7fc0c2bb add enter/leave 9fa2ef3c add in_, out_ df208648 add lods{b,w,d,q}, outs{b,w,d} 4672d2cb add int3, int_, into 431977cb add pushfq, popfq 81c4749f syscall, sysenter, sysexit, sysret 1f1b53c4 add clflushopt, fldenv, fnstw b765db33 Profiler uses append mode 44dc3546 add Profiler class 42949334 update version to v5.802 91cb919b Merge branch 'vpirogov-master' a6452f82 fixed avx512_bf16 detection f41da5aa tweak ; vcvtneps2bf16 calls opCvt2 b12460ba [sample] fix typo of quantize.cpp b22f5881 add set_opt.bat for test on Windows f402faad add vp2intersectd/vp2intersectq 4cfd5208 add avx512_bf16 4033564c fix vcmppd/vcmpps for ptr_b git-subtree-dir: externals/xbyak git-subtree-split: 0140eeff1fffcf5069dea3abb57095695320971c
229 lines
5.6 KiB
C++
229 lines
5.6 KiB
C++
/*
|
|
@author herumi
|
|
|
|
tiny calculator
|
|
This program generates a function to calc the value of
|
|
polynomial given by user in run-time.
|
|
use boost::spirit::classic
|
|
see calc2.cpp for new version of boost::spirit
|
|
*/
|
|
#include <stdio.h>
|
|
#include <sstream>
|
|
#include <map>
|
|
#define XBYAK_NO_OP_NAMES
|
|
#include "xbyak/xbyak.h"
|
|
#ifdef _MSC_VER
|
|
#pragma warning(disable : 4127) // for boost(constant condition)
|
|
#pragma warning(disable : 4512) // for boost
|
|
#endif
|
|
#include <boost/spirit/include/classic_file_iterator.hpp>
|
|
#include <boost/spirit/include/classic_core.hpp>
|
|
#include <boost/bind.hpp>
|
|
|
|
enum Error {
|
|
UNDEFINED_VARIABLE = 1
|
|
};
|
|
|
|
/*
|
|
JIT assemble of given polynomial for VC or gcc
|
|
*/
|
|
class FuncGen : public Xbyak::CodeGenerator {
|
|
public:
|
|
typedef std::map<std::string, int> Map;
|
|
private:
|
|
enum {
|
|
MAX_CONST_NUM = 32
|
|
};
|
|
double constTbl_[MAX_CONST_NUM];
|
|
size_t constTblPos_;
|
|
int regIdx_;
|
|
Map varMap_; // map var name to index
|
|
#ifdef XBYAK32
|
|
const Xbyak::Reg32& valTbl_;
|
|
const Xbyak::Reg32& tbl_;
|
|
#else
|
|
const Xbyak::Reg64& valTbl_;
|
|
const Xbyak::Reg64& tbl_;
|
|
#endif
|
|
public:
|
|
/*
|
|
@param y [out] the value of f(var)
|
|
@param var [in] table of input variables
|
|
func(double *y, const double var[]);
|
|
@note func does not return double to avoid difference of compiler
|
|
*/
|
|
FuncGen(const std::vector<std::string>& varTbl)
|
|
: constTblPos_(0)
|
|
, regIdx_(-1)
|
|
#ifdef XBYAK32
|
|
, valTbl_(eax)
|
|
, tbl_(edx)
|
|
#elif defined(XBYAK64_WIN)
|
|
, valTbl_(rcx)
|
|
, tbl_(rdx)
|
|
#else
|
|
, valTbl_(rdi)
|
|
, tbl_(rsi)
|
|
#endif
|
|
{
|
|
#ifdef XBYAK32
|
|
mov(valTbl_, ptr[esp+8]); // eax == varTbl
|
|
mov(tbl_, (size_t)constTbl_);
|
|
#else
|
|
#ifdef XBYAK64_WIN
|
|
movaps(ptr [rsp + 8], xm6); // save xm6, xm7
|
|
movaps(ptr [rsp + 8 + 16], xm7);
|
|
#endif
|
|
mov(tbl_, (size_t)constTbl_);
|
|
#endif
|
|
for (int i = 0, n = static_cast<int>(varTbl.size()); i < n; i++) {
|
|
varMap_[varTbl[i]] = i;
|
|
}
|
|
}
|
|
// use edx
|
|
void genPush(double n)
|
|
{
|
|
if (constTblPos_ >= MAX_CONST_NUM) throw;
|
|
constTbl_[constTblPos_] = n;
|
|
if (regIdx_ == 7) throw;
|
|
movsd(Xbyak::Xmm(++regIdx_), ptr[tbl_ + (int)(constTblPos_ * sizeof(double))]);
|
|
constTblPos_++;
|
|
}
|
|
// use eax
|
|
void genVal(const char *begin, const char *end)
|
|
{
|
|
std::string var(begin, end);
|
|
if (varMap_.find(var) == varMap_.end()) throw UNDEFINED_VARIABLE;
|
|
if (regIdx_ == 7) throw;
|
|
movsd(Xbyak::Xmm(++regIdx_), ptr[valTbl_ + varMap_[var] * sizeof(double)]);
|
|
}
|
|
void genAdd(const char*, const char*)
|
|
{
|
|
addsd(Xbyak::Xmm(regIdx_ - 1), Xbyak::Xmm(regIdx_)); regIdx_--;
|
|
}
|
|
void genSub(const char*, const char*)
|
|
{
|
|
subsd(Xbyak::Xmm(regIdx_ - 1), Xbyak::Xmm(regIdx_)); regIdx_--;
|
|
}
|
|
void genMul(const char*, const char*)
|
|
{
|
|
mulsd(Xbyak::Xmm(regIdx_ - 1), Xbyak::Xmm(regIdx_)); regIdx_--;
|
|
}
|
|
void genDiv(const char*, const char*)
|
|
{
|
|
divsd(Xbyak::Xmm(regIdx_ - 1), Xbyak::Xmm(regIdx_)); regIdx_--;
|
|
}
|
|
void complete()
|
|
{
|
|
#ifdef XBYAK32
|
|
mov(eax, ptr [esp + 4]); // eax = valTbl
|
|
movsd(ptr [eax], xm0);
|
|
#else
|
|
#ifdef XBYAK64_WIN
|
|
movaps(xm6, ptr [rsp + 8]);
|
|
movaps(xm7, ptr [rsp + 8 + 16]);
|
|
#endif
|
|
#endif
|
|
ret();
|
|
}
|
|
};
|
|
|
|
struct Grammar : public boost::spirit::classic::grammar<Grammar> {
|
|
FuncGen& f_;
|
|
Grammar(FuncGen& f) : f_(f) { }
|
|
template<typename ScannerT>
|
|
struct definition {
|
|
boost::spirit::classic::rule<ScannerT> poly0, poly1, poly2, var;
|
|
|
|
definition(const Grammar& self)
|
|
{
|
|
using namespace boost;
|
|
using namespace boost::spirit::classic;
|
|
|
|
poly0 = poly1 >> *(('+' >> poly1)[bind(&FuncGen::genAdd, ref(self.f_), _1, _2)]
|
|
| ('-' >> poly1)[bind(&FuncGen::genSub, ref(self.f_), _1, _2)]);
|
|
poly1 = poly2 >> *(('*' >> poly2)[bind(&FuncGen::genMul, ref(self.f_), _1, _2)]
|
|
| ('/' >> poly2)[bind(&FuncGen::genDiv, ref(self.f_), _1, _2)]);
|
|
var = (+alpha_p)[bind(&FuncGen::genVal, ref(self.f_), _1, _2)];
|
|
poly2 = real_p[bind(&FuncGen::genPush, ref(self.f_), _1)]
|
|
| var
|
|
| '(' >> poly0 >> ')';
|
|
}
|
|
const boost::spirit::classic::rule<ScannerT>& start() const { return poly0; }
|
|
};
|
|
};
|
|
|
|
void put(const std::vector<double>& x)
|
|
{
|
|
for (size_t i = 0, n = x.size(); i < n; i++) {
|
|
if (i > 0) printf(", ");
|
|
printf("%f", x[i]);
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
if (argc <= 2) {
|
|
fprintf(stderr, "calc \"var1 var2 ...\" \"function of var\"\n");
|
|
fprintf(stderr, "eg. calc x \"x*x\"\n");
|
|
fprintf(stderr, "eg. calc \"x y z\" \"x*x + y - z\"\n");
|
|
return 1;
|
|
}
|
|
const char *poly = argv[2];
|
|
try {
|
|
std::vector<std::string> varTbl;
|
|
|
|
// get varTbl from argv[1]
|
|
{
|
|
std::istringstream is(argv[1]);
|
|
int i = 0;
|
|
printf("varTbl = { ");
|
|
while (is) {
|
|
std::string var;
|
|
is >> var;
|
|
if (var.empty()) break;
|
|
printf("%s:%d, ", var.c_str(), i);
|
|
varTbl.push_back(var);
|
|
i++;
|
|
}
|
|
printf("}\n");
|
|
}
|
|
FuncGen funcGen(varTbl);
|
|
Grammar calc(funcGen);
|
|
boost::spirit::classic::parse_info<> r = parse(poly, calc, boost::spirit::classic::space_p);
|
|
if (!r.full) {
|
|
printf("err poly=%s\n", poly);
|
|
return 1;
|
|
}
|
|
funcGen.complete();
|
|
std::vector<double> valTbl;
|
|
valTbl.resize(varTbl.size());
|
|
#ifdef XBYAK32
|
|
puts("32bit mode");
|
|
void (*func)(double *ret, const double *valTbl) = funcGen.getCode<void (*)(double *, const double*)>();
|
|
#else
|
|
puts("64bit mode");
|
|
double (*func)(const double *valTbl) = funcGen.getCode<double (*)(const double*)>();
|
|
#endif
|
|
for (int i = 0; i < 10; i++) {
|
|
for (size_t j = 0, n = valTbl.size(); j < n; j++) {
|
|
valTbl[j] = rand() % 7;
|
|
}
|
|
double y;
|
|
#ifdef XBYAK32
|
|
func(&y, &valTbl[0]);
|
|
#else
|
|
y = func(&valTbl[0]);
|
|
#endif
|
|
printf("f("); put(valTbl); printf(")=%f\n", y);
|
|
}
|
|
} catch (std::exception& e) {
|
|
printf("ERR:%s\n", e.what());
|
|
} catch (Error err) {
|
|
printf("ERR:%d\n", err);
|
|
} catch (...) {
|
|
printf("unknown error\n");
|
|
}
|
|
|
|
return 0;
|
|
}
|