start.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include <Assembly.h>
  2. #include <functional>
  3. #include <iostream>
  4. #include <malloc.h>
  5. #include <windows.h>
  6. #include <Zeit.h>
  7. using namespace Framework;
  8. using namespace Assembly;
  9. typedef int (*valF)(void*);
  10. typedef int (*compF)();
  11. template<typename T> class Val
  12. {
  13. protected:
  14. AssemblyBlock block;
  15. T* self;
  16. public:
  17. Val(T* self)
  18. : self(self)
  19. {}
  20. inline int getValueInline()
  21. {
  22. return self->getValue2();
  23. }
  24. virtual int getValue() = 0;
  25. virtual valF compileFP() = 0;
  26. virtual std::function<int()> compileStdF() = 0;
  27. compF compileToAssembly()
  28. {
  29. internalCompileAssembly();
  30. block.optimize();
  31. return block.compileToFunction<compF>();
  32. }
  33. virtual AssemblyBlock& internalCompileAssembly() = 0;
  34. inline AssemblyBlock& getBlock()
  35. {
  36. return block;
  37. }
  38. };
  39. class RandVal : public Val<RandVal>
  40. {
  41. public:
  42. RandVal()
  43. : Val(this)
  44. {}
  45. int getValue() override
  46. {
  47. return rand();
  48. }
  49. int getValue2()
  50. {
  51. return rand();
  52. }
  53. valF compileFP() override
  54. {
  55. return [](void* v) { return rand(); };
  56. }
  57. std::function<int()> compileStdF() override
  58. {
  59. return []() { return rand(); };
  60. }
  61. AssemblyBlock& internalCompileAssembly() override
  62. {
  63. block.addCall(rand);
  64. return block;
  65. }
  66. };
  67. class ConstVal : public Val<ConstVal>
  68. {
  69. private:
  70. int value;
  71. public:
  72. ConstVal(int value)
  73. : Val(this),
  74. value(value)
  75. {}
  76. int getValue() override
  77. {
  78. return value;
  79. }
  80. inline int getValue2()
  81. {
  82. return value;
  83. }
  84. valF compileFP() override
  85. {
  86. return [](void* v) { return ((ConstVal*)v)->value; };
  87. }
  88. std::function<int()> compileStdF() override
  89. {
  90. const int& value = this->value;
  91. return [value]() { return value; };
  92. }
  93. AssemblyBlock& internalCompileAssembly() override
  94. {
  95. block.addLoadValue(&value, RAX);
  96. return block;
  97. }
  98. };
  99. template<typename T1, typename T2> class AdditionVal
  100. : public Val<AdditionVal<T1, T2>>
  101. {
  102. private:
  103. Val<T1>* a;
  104. Val<T2>* b;
  105. valF af;
  106. valF bf;
  107. std::function<int()> saf;
  108. std::function<int()> sbf;
  109. public:
  110. AdditionVal(Val<T1>* a, Val<T2>* b)
  111. : Val<AdditionVal<T1, T2>>(this),
  112. a(a),
  113. b(b),
  114. af(a->compileFP()),
  115. bf(b->compileFP()),
  116. saf(a->compileStdF()),
  117. sbf(b->compileStdF())
  118. {}
  119. int getValue() override
  120. {
  121. return a->getValue() + b->getValue();
  122. }
  123. inline int getValue2()
  124. {
  125. return a->getValueInline() + b->getValueInline();
  126. }
  127. valF compileFP() override
  128. {
  129. return [](void* v) {
  130. AdditionVal* av = (AdditionVal*)v;
  131. return av->af(av->a) + av->bf(av->b);
  132. };
  133. }
  134. std::function<int()> compileStdF() override
  135. {
  136. const std::function<int()>& aFunc = saf;
  137. const std::function<int()>& bFunc = sbf;
  138. return [aFunc, bFunc]() { return aFunc() + bFunc(); };
  139. }
  140. AssemblyBlock& internalCompileAssembly() override
  141. {
  142. GPRegister result = RAX;
  143. GPRegister result2 = RAX;
  144. FPRegister resultFP;
  145. this->getBlock().addBlock(
  146. &a->internalCompileAssembly(), {}, {}, &result, &resultFP);
  147. this->getBlock().addBlock(
  148. &b->internalCompileAssembly(), {result}, {}, &result2, &resultFP);
  149. this->getBlock().addInstruction(new Instruction(ADD,
  150. {new GPRegisterArgument(result, LOWER32),
  151. new GPRegisterArgument(result2, LOWER32)}));
  152. if (result != RAX)
  153. {
  154. this->getBlock().addMoveValue(result, RAX, LOWER32);
  155. }
  156. return this->getBlock();
  157. }
  158. };
  159. extern "C"
  160. {
  161. extern int getVal(void*);
  162. }
  163. template<typename T> __declspec(noinline) int get(T* t)
  164. {
  165. return t->getValueInline();
  166. }
  167. int main()
  168. {
  169. RandVal* r = new RandVal();
  170. auto test
  171. = new AdditionVal<AdditionVal<AdditionVal<RandVal, ConstVal>, ConstVal>,
  172. AdditionVal<ConstVal, ConstVal>>(
  173. new AdditionVal<AdditionVal<RandVal, ConstVal>, ConstVal>(
  174. new AdditionVal<RandVal, ConstVal>(r, new ConstVal(2)),
  175. new ConstVal(3)),
  176. new AdditionVal<ConstVal, ConstVal>(
  177. new ConstVal(4), new ConstVal(5)));
  178. ZeitMesser zeitMesser;
  179. srand(0);
  180. zeitMesser.messungStart();
  181. __int64 res = 0;
  182. for (int i = 0; i < 100000000; i++)
  183. {
  184. res += get(test);
  185. }
  186. zeitMesser.messungEnde();
  187. std::cout << "inlined Time: " << zeitMesser.getSekunden() << "s"
  188. << " result: " << res << std::endl;
  189. srand(0);
  190. zeitMesser.messungStart();
  191. res = 0;
  192. for (int i = 0; i < 100000000; i++)
  193. {
  194. res += test->getValue();
  195. }
  196. zeitMesser.messungEnde();
  197. std::cout << "getValue() Time: " << zeitMesser.getSekunden() << "s"
  198. << " result: " << res << std::endl;
  199. res = 0;
  200. srand(0);
  201. auto val = test->compileFP();
  202. zeitMesser.messungStart();
  203. for (int i = 0; i < 100000000; i++)
  204. {
  205. res += val(test);
  206. }
  207. zeitMesser.messungEnde();
  208. std::cout << "compile() Time: " << zeitMesser.getSekunden() << "s"
  209. << " result: " << res << std::endl;
  210. res = 0;
  211. auto stdf = test->compileStdF();
  212. srand(0);
  213. zeitMesser.messungStart();
  214. for (int i = 0; i < 100000000; i++)
  215. {
  216. res += stdf();
  217. }
  218. zeitMesser.messungEnde();
  219. std::cout << "compileStdF() Time: " << zeitMesser.getSekunden() << "s"
  220. << " result: " << res << std::endl;
  221. res = 0;
  222. auto assembly = test->compileToAssembly();
  223. srand(0);
  224. zeitMesser.messungStart();
  225. for (int i = 0; i < 100000000; i++)
  226. {
  227. res += assembly();
  228. }
  229. zeitMesser.messungEnde();
  230. std::cout << "compileToAssembly() Time: " << zeitMesser.getSekunden() << "s"
  231. << " result: " << res << std::endl;
  232. res = 0;
  233. int i2 = 2;
  234. int i3 = 3;
  235. int i4 = 4;
  236. int i5 = 5;
  237. AssemblyBlock fastest;
  238. fastest.addMoveValue(RCX, 2);
  239. fastest.addMoveValue(RDX, 3);
  240. fastest.addMoveValue(R8, 4);
  241. fastest.addMoveValue(R9, 5);
  242. fastest.addCall(rand);
  243. fastest.addInstruction(new Instruction(ADD,
  244. {new GPRegisterArgument(RAX, LOWER32),
  245. new GPRegisterArgument(RCX, LOWER32)}));
  246. fastest.addInstruction(new Instruction(ADD,
  247. {new GPRegisterArgument(RAX, LOWER32),
  248. new GPRegisterArgument(RDX, LOWER32)}));
  249. fastest.addInstruction(new Instruction(ADD,
  250. {new GPRegisterArgument(RAX, LOWER32),
  251. new GPRegisterArgument(R8, LOWER32)}));
  252. fastest.addInstruction(new Instruction(ADD,
  253. {new GPRegisterArgument(RAX, LOWER32),
  254. new GPRegisterArgument(R9, LOWER32)}));
  255. auto f = fastest.compileToFunction<compF>();
  256. zeitMesser.messungStart();
  257. srand(0);
  258. for (int i = 0; i < 100000000; i++)
  259. {
  260. res += assembly();
  261. }
  262. zeitMesser.messungEnde();
  263. srand(0);
  264. std::cout << "fastest Time: " << zeitMesser.getSekunden() << "s"
  265. << " result: " << res << std::endl;
  266. res = 0;
  267. zeitMesser.messungStart();
  268. for (int i = 0; i < 100000000; i++)
  269. {
  270. res += getVal(rand);
  271. }
  272. zeitMesser.messungEnde();
  273. std::cout << "fastest Time: " << zeitMesser.getSekunden() << "s"
  274. << " result: " << res << std::endl;
  275. }