/xbmc/visualizations/Milkdrop/vis_milkdrop/evallib/cfunc.c

http://github.com/xbmc/xbmc · C · 879 lines · 692 code · 100 blank · 87 comment · 3 complexity · 081fdaea8fe6ef357c09b52b6eab9005 MD5 · raw file

  1. /*
  2. LICENSE
  3. -------
  4. Copyright 2005 Nullsoft, Inc.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. * Neither the name of Nullsoft nor the names of its contributors may be used to
  14. endorse or promote products derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  21. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include <windows.h>
  25. //#include <xtl.h>
  26. #include <stdio.h>
  27. #include <math.h>
  28. #include "Compiler.h"
  29. #include "eval.h"
  30. #pragma warning(disable: 4312)
  31. static float g_cmpaddtab[2]={0.0,1.0};
  32. static double g_closefact = 0.00001, g_half=0.5;
  33. extern double *nextBlock;
  34. // FUNCTION CALL TEMPLATES - THESE ARE NEVER ACTUALLY CALLED,
  35. // BUT COPIED AND MODIFIED.
  36. //---------------------------------------------------------------------------------------------------------------
  37. __declspec ( naked ) void _asm_simpleValue(void)
  38. {
  39. __asm
  40. {
  41. mov eax, 0FFFFFFFFh;
  42. ret
  43. }
  44. }
  45. __declspec ( naked ) void _asm_simpleValue_end(void){}
  46. //---------------------------------------------------------------------------------------------------------------
  47. __declspec ( naked ) void _asm_function3(void)
  48. {
  49. __asm
  50. {
  51. mov ecx, 0FFFFFFFFh
  52. call ecx
  53. push eax
  54. mov ecx, 0FFFFFFFFh
  55. call ecx
  56. push eax
  57. mov ecx, 0FFFFFFFFh
  58. call ecx
  59. pop ebx
  60. pop ecx
  61. mov edx, 0FFFFFFFFh
  62. jmp edx
  63. }
  64. }
  65. __declspec ( naked ) void _asm_function3_end(void){}
  66. //---------------------------------------------------------------------------------------------------------------
  67. __declspec ( naked ) void _asm_function2(void)
  68. {
  69. __asm
  70. {
  71. mov ecx, 0FFFFFFFFh
  72. call ecx
  73. push eax
  74. mov ecx, 0FFFFFFFFh
  75. call ecx
  76. pop ebx
  77. mov ecx, 0FFFFFFFFh
  78. jmp ecx
  79. }
  80. }
  81. __declspec ( naked ) void _asm_function2_end(void){}
  82. //---------------------------------------------------------------------------------------------------------------
  83. __declspec ( naked ) void _asm_function1(void)
  84. {
  85. __asm
  86. {
  87. mov ecx, 0FFFFFFFFh
  88. call ecx
  89. mov ecx, 0FFFFFFFFh
  90. jmp ecx
  91. }
  92. }
  93. __declspec ( naked ) void _asm_function1_end(void){}
  94. // END FUNCTION CALL TEMPLATES
  95. // our registers
  96. static int a,b;
  97. static double *res;
  98. #define isnonzero(x) (fabs(x) > g_closefact)
  99. //---------------------------------------------------------------------------------------------------------------
  100. static double _rand(double x)
  101. {
  102. if (x < 1.0) x=1.0;
  103. return (double)(rand()%(int)max(x,1.0));
  104. }
  105. //---------------------------------------------------------------------------------------------------------------
  106. static double _band(double var, double var2)
  107. {
  108. return isnonzero(var) && isnonzero(var2) ? 1 : 0;
  109. }
  110. //---------------------------------------------------------------------------------------------------------------
  111. static double _bor(double var, double var2)
  112. {
  113. return isnonzero(var) || isnonzero(var2) ? 1 : 0;
  114. }
  115. //---------------------------------------------------------------------------------------------------------------
  116. static double _sig(double x, double constraint)
  117. {
  118. double t = (1+exp(-x*constraint));
  119. return isnonzero(t) ? 1.0/t : 0;
  120. }
  121. //---------------------------------------------------------------------------------------------------------------
  122. __declspec ( naked ) void _asm_int(void)
  123. {
  124. __asm mov dword ptr a, eax
  125. res = nextBlock++;
  126. _asm mov edx, DWORD PTR a
  127. _asm fld QWORD PTR [edx]
  128. _asm mov edx, DWORD PTR res
  129. _asm fistp DWORD PTR [edx]
  130. _asm fild DWORD PTR[edx]
  131. _asm fstp QWORD PTR [edx]
  132. _asm mov eax, res
  133. _asm ret
  134. /*
  135. MrC - The old version uses _ftol2_sse which stomps over our stack
  136. *res = (double) ((int)(*((double*)a)));
  137. __asm
  138. {
  139. mov eax, res
  140. ret
  141. }
  142. */
  143. }
  144. //---------------------------------------------------------------------------------------------------------------
  145. __declspec ( naked ) void _asm_asin(void)
  146. {
  147. __asm mov dword ptr a, eax
  148. res = nextBlock++;
  149. *res = asin(*(double*)a);
  150. __asm
  151. {
  152. mov eax, res
  153. ret
  154. }
  155. }
  156. //---------------------------------------------------------------------------------------------------------------
  157. __declspec ( naked ) void _asm_acos(void)
  158. {
  159. __asm mov dword ptr a, eax
  160. res = nextBlock++;
  161. *res = acos(*(double*)a);
  162. __asm
  163. {
  164. mov eax, res
  165. ret
  166. }
  167. }
  168. //---------------------------------------------------------------------------------------------------------------
  169. __declspec ( naked ) void _asm_atan(void)
  170. {
  171. __asm mov dword ptr a, eax
  172. res = nextBlock++;
  173. *res = atan(*(double*)a);
  174. __asm
  175. {
  176. mov eax, res
  177. ret
  178. }
  179. }
  180. //---------------------------------------------------------------------------------------------------------------
  181. __declspec ( naked ) void _asm_atan2(void)
  182. {
  183. __asm
  184. {
  185. mov dword ptr a, eax
  186. mov dword ptr b, ebx
  187. }
  188. res = nextBlock++;
  189. *res = atan2(*(double*)b, *(double*)a);
  190. __asm
  191. {
  192. mov eax, res
  193. ret
  194. }
  195. }
  196. //---------------------------------------------------------------------------------------------------------------
  197. __declspec ( naked ) void _asm_sig(void)
  198. {
  199. __asm
  200. {
  201. mov dword ptr a, eax
  202. mov dword ptr b, ebx
  203. }
  204. res = nextBlock++;
  205. *res = _sig(*(double*)b, *(double*)a);
  206. __asm
  207. {
  208. mov eax, dword ptr res
  209. ret
  210. }
  211. }
  212. //---------------------------------------------------------------------------------------------------------------
  213. __declspec ( naked ) void _asm_rand(void)
  214. {
  215. __asm
  216. {
  217. mov dword ptr a, eax
  218. }
  219. res = nextBlock++;
  220. *res = _rand(*(double*)a);
  221. __asm
  222. {
  223. mov eax, res
  224. ret
  225. }
  226. }
  227. //---------------------------------------------------------------------------------------------------------------
  228. __declspec ( naked ) void _asm_band(void)
  229. {
  230. __asm
  231. {
  232. mov dword ptr a, eax
  233. mov dword ptr b, ebx
  234. }
  235. res = nextBlock++;
  236. *res = _band(*(double*)b, *(double*)a);
  237. __asm
  238. {
  239. mov eax, res
  240. ret
  241. }
  242. }
  243. //---------------------------------------------------------------------------------------------------------------
  244. __declspec ( naked ) void _asm_bor(void)
  245. {
  246. __asm
  247. {
  248. mov dword ptr a, eax
  249. mov dword ptr b, ebx
  250. }
  251. res = nextBlock++;
  252. *res = _bor(*(double*)b, *(double*)a);
  253. __asm
  254. {
  255. mov eax, res
  256. ret
  257. }
  258. }
  259. //---------------------------------------------------------------------------------------------------------------
  260. __declspec ( naked ) void _asm_pow(void)
  261. {
  262. __asm
  263. {
  264. mov dword ptr a, eax
  265. mov dword ptr b, ebx
  266. }
  267. res = nextBlock++;
  268. *res = pow(*(double*)b, *(double*)a);
  269. __asm
  270. {
  271. mov eax, res
  272. ret
  273. }
  274. }
  275. //---------------------------------------------------------------------------------------------------------------
  276. __declspec ( naked ) void _asm_exp(void)
  277. {
  278. __asm mov dword ptr a, eax
  279. res = nextBlock++;
  280. *res = exp(*(double*)a);
  281. __asm
  282. {
  283. mov eax, res
  284. ret
  285. }
  286. }
  287. // these below are all asm, no loops, radness
  288. //---------------------------------------------------------------------------------------------------------------
  289. __declspec ( naked ) void _asm_sin(void)
  290. {
  291. __asm
  292. {
  293. fld qword ptr [eax]
  294. mov eax, nextBlock
  295. fsin
  296. fstp qword ptr [eax]
  297. add eax, 8
  298. mov nextBlock, eax
  299. sub eax, 8
  300. ret
  301. }
  302. }
  303. //---------------------------------------------------------------------------------------------------------------
  304. __declspec ( naked ) void _asm_cos(void)
  305. {
  306. __asm
  307. {
  308. fld qword ptr [eax]
  309. mov eax, nextBlock
  310. fcos
  311. fstp qword ptr [eax]
  312. add eax, 8
  313. mov nextBlock, eax
  314. sub eax, 8
  315. ret
  316. }
  317. }
  318. //---------------------------------------------------------------------------------------------------------------
  319. __declspec ( naked ) void _asm_tan(void)
  320. {
  321. __asm
  322. {
  323. fld qword ptr [eax]
  324. mov eax, nextBlock
  325. fsincos
  326. fdiv
  327. fstp qword ptr [eax]
  328. add eax, 8
  329. mov nextBlock, eax
  330. sub eax, 8
  331. ret
  332. }
  333. }
  334. //---------------------------------------------------------------------------------------------------------------
  335. __declspec ( naked ) void _asm_sqr(void)
  336. {
  337. __asm
  338. {
  339. fld qword ptr [eax]
  340. fld st(0)
  341. mov eax, nextBlock
  342. fmul
  343. fstp qword ptr [eax]
  344. add eax, 8
  345. mov nextBlock, eax
  346. sub eax, 8
  347. ret
  348. }
  349. }
  350. //---------------------------------------------------------------------------------------------------------------
  351. __declspec ( naked ) void _asm_sqrt(void)
  352. {
  353. __asm
  354. {
  355. fld qword ptr [eax]
  356. mov eax, nextBlock
  357. fabs
  358. fsqrt
  359. fstp qword ptr [eax]
  360. add eax, 8
  361. mov nextBlock, eax
  362. sub eax, 8
  363. ret
  364. }
  365. }
  366. //---------------------------------------------------------------------------------------------------------------
  367. __declspec ( naked ) void _asm_log(void)
  368. {
  369. __asm
  370. {
  371. fld1
  372. fldl2e
  373. fdiv
  374. fld qword ptr [eax]
  375. mov eax, nextBlock
  376. fyl2x
  377. fstp qword ptr [eax]
  378. add eax, 8
  379. mov nextBlock, eax
  380. sub eax, 8
  381. ret
  382. }
  383. }
  384. //---------------------------------------------------------------------------------------------------------------
  385. __declspec ( naked ) void _asm_log10(void)
  386. {
  387. __asm
  388. {
  389. fld1
  390. fldl2t
  391. fdiv
  392. fld qword ptr [eax]
  393. mov eax, nextBlock
  394. fyl2x
  395. fstp qword ptr [eax]
  396. add eax, 8
  397. mov nextBlock, eax
  398. sub eax, 8
  399. ret
  400. }
  401. }
  402. //---------------------------------------------------------------------------------------------------------------
  403. __declspec ( naked ) void _asm_abs(void)
  404. {
  405. __asm
  406. {
  407. fld qword ptr [eax]
  408. mov eax, nextBlock
  409. fabs
  410. fstp qword ptr [eax]
  411. add eax, 8
  412. mov nextBlock, eax
  413. sub eax, 8
  414. ret
  415. }
  416. }
  417. //---------------------------------------------------------------------------------------------------------------
  418. __declspec ( naked ) void _asm_assign(void)
  419. {
  420. __asm
  421. {
  422. mov ecx, [eax]
  423. mov edx, [eax+4]
  424. mov [ebx], ecx
  425. mov [ebx+4], edx
  426. ret
  427. }
  428. }
  429. //---------------------------------------------------------------------------------------------------------------
  430. __declspec ( naked ) void _asm_add(void)
  431. {
  432. __asm
  433. {
  434. fld qword ptr [eax]
  435. mov eax, nextBlock
  436. fld qword ptr [ebx]
  437. fadd
  438. fstp qword ptr [eax]
  439. add eax, 8
  440. mov nextBlock, eax
  441. sub eax, 8
  442. ret
  443. }
  444. }
  445. //---------------------------------------------------------------------------------------------------------------
  446. __declspec ( naked ) void _asm_sub(void)
  447. {
  448. __asm
  449. {
  450. fld qword ptr [ebx]
  451. fld qword ptr [eax]
  452. mov eax, nextBlock
  453. fsub
  454. fstp qword ptr [eax]
  455. add eax, 8
  456. mov nextBlock, eax
  457. sub eax, 8
  458. ret
  459. }
  460. }
  461. //---------------------------------------------------------------------------------------------------------------
  462. __declspec ( naked ) void _asm_mul(void)
  463. {
  464. __asm
  465. {
  466. fld qword ptr [ebx]
  467. fld qword ptr [eax]
  468. mov eax, nextBlock
  469. fmul
  470. fstp qword ptr [eax]
  471. add eax, 8
  472. mov nextBlock, eax
  473. sub eax, 8
  474. ret
  475. }
  476. }
  477. //---------------------------------------------------------------------------------------------------------------
  478. __declspec ( naked ) void _asm_div(void)
  479. {
  480. __asm
  481. {
  482. fld qword ptr [ebx]
  483. fdiv qword ptr [eax]
  484. mov eax, nextBlock
  485. fstp qword ptr [eax]
  486. add eax, 8
  487. mov nextBlock, eax
  488. sub eax, 8
  489. ret
  490. }
  491. }
  492. //---------------------------------------------------------------------------------------------------------------
  493. __declspec ( naked ) void _asm_mod(void)
  494. {
  495. __asm
  496. {
  497. fld qword ptr [ebx]
  498. fld qword ptr [eax]
  499. fsub dword ptr [g_cmpaddtab+4]
  500. fabs
  501. fadd qword ptr [eax]
  502. fadd dword ptr [g_cmpaddtab+4]
  503. fmul qword ptr [g_half]
  504. mov ebx, nextBlock
  505. fistp dword ptr [ebx]
  506. fistp dword ptr [ebx+4]
  507. mov eax, [ebx+4]
  508. xor edx, edx
  509. div dword ptr [ebx]
  510. mov [ebx], edx
  511. fild dword ptr [ebx]
  512. fstp qword ptr [ebx]
  513. mov eax, ebx
  514. add ebx, 8
  515. mov nextBlock, ebx // eax is still good
  516. ret
  517. }
  518. }
  519. //---------------------------------------------------------------------------------------------------------------
  520. __declspec ( naked ) void _asm_or(void)
  521. {
  522. __asm
  523. {
  524. fld qword ptr [ebx]
  525. fld qword ptr [eax]
  526. mov eax, nextBlock
  527. fistp qword ptr [eax]
  528. fistp qword ptr [eax+8]
  529. mov ebx, [eax]
  530. or [eax+4], ebx
  531. mov ebx, [eax+8]
  532. or [eax+12], ebx
  533. fild qword ptr [eax]
  534. fstp qword ptr [eax]
  535. add eax, 8
  536. mov nextBlock, eax
  537. sub eax, 8
  538. ret
  539. }
  540. }
  541. //---------------------------------------------------------------------------------------------------------------
  542. __declspec ( naked ) void _asm_and(void)
  543. {
  544. __asm
  545. {
  546. fld qword ptr [ebx]
  547. fld qword ptr [eax]
  548. mov eax, nextBlock
  549. fistp qword ptr [eax]
  550. fistp qword ptr [eax+8]
  551. mov ebx, [eax]
  552. and [eax+4], ebx
  553. mov ebx, [eax+8]
  554. and [eax+12], ebx
  555. fild qword ptr [eax]
  556. fstp qword ptr [eax]
  557. add eax, 8
  558. mov nextBlock, eax
  559. sub eax, 8
  560. ret
  561. }
  562. }
  563. //---------------------------------------------------------------------------------------------------------------
  564. __declspec ( naked ) void _asm_uplus(void)
  565. {
  566. __asm
  567. {
  568. mov ebx, nextBlock
  569. mov ecx, [eax]
  570. mov [ebx], ecx
  571. mov ecx, [eax+4]
  572. mov [ebx+4], ecx
  573. mov eax, ebx
  574. add ebx, 8
  575. mov nextBlock, ebx
  576. ret
  577. }
  578. }
  579. //---------------------------------------------------------------------------------------------------------------
  580. __declspec ( naked ) void _asm_uminus(void)
  581. {
  582. __asm
  583. {
  584. mov ebx, nextBlock
  585. mov ecx, [eax]
  586. mov [ebx], ecx
  587. mov ecx, [eax+4]
  588. xor ecx, 0x80000000
  589. mov [ebx+4], ecx
  590. mov eax, ebx
  591. add ebx, 8
  592. mov nextBlock, ebx
  593. ret
  594. }
  595. }
  596. //---------------------------------------------------------------------------------------------------------------
  597. __declspec ( naked ) void _asm_sign(void)
  598. {
  599. __asm
  600. {
  601. fld qword ptr [eax]
  602. fld st(0)
  603. fabs
  604. mov eax, nextBlock
  605. fld qword ptr [g_closefact]
  606. fadd
  607. fdiv
  608. fstp qword ptr [eax]
  609. add eax, 8
  610. mov nextBlock, eax
  611. sub eax, 8
  612. ret
  613. }
  614. }
  615. //---------------------------------------------------------------------------------------------------------------
  616. __declspec ( naked ) void _asm_bnot(void)
  617. {
  618. __asm
  619. {
  620. mov ebx, nextBlock
  621. fld qword ptr [g_closefact]
  622. fld qword ptr [eax]
  623. fabs
  624. fcompp
  625. fstsw ax
  626. shr eax, 6
  627. and eax, (1<<2)
  628. add eax, offset g_cmpaddtab
  629. fld dword ptr [eax]
  630. fstp qword ptr [ebx]
  631. mov eax, ebx
  632. add ebx, 8
  633. mov nextBlock, ebx
  634. ret
  635. }
  636. }
  637. //---------------------------------------------------------------------------------------------------------------
  638. __declspec ( naked ) void _asm_if(void)
  639. {
  640. __asm
  641. {
  642. fld qword ptr [eax]
  643. fld qword ptr [ebx]
  644. fld qword ptr [g_closefact]
  645. fld qword ptr [ecx]
  646. fabs
  647. fcompp
  648. fstsw ax
  649. mov ebx, nextBlock
  650. fstp qword ptr [ebx]
  651. fstp qword ptr [ebx+8]
  652. shr eax, 5
  653. and eax, (1<<3)
  654. add eax, ebx
  655. fld qword ptr [eax]
  656. fstp qword ptr [ebx]
  657. mov eax, ebx
  658. add ebx, 8
  659. mov nextBlock, ebx
  660. ret
  661. }
  662. }
  663. //---------------------------------------------------------------------------------------------------------------
  664. __declspec ( naked ) void _asm_equal(void)
  665. {
  666. __asm
  667. {
  668. fld qword ptr [g_closefact]
  669. fld qword ptr [eax]
  670. fld qword ptr [ebx]
  671. fsub
  672. fabs
  673. fcompp
  674. fstsw ax
  675. shr eax, 6
  676. and eax, (1<<2)
  677. add eax, offset g_cmpaddtab
  678. fld dword ptr [eax]
  679. mov eax, nextBlock
  680. fstp qword ptr [eax]
  681. mov ebx, eax
  682. add ebx, 8
  683. mov nextBlock, ebx
  684. ret
  685. }
  686. }
  687. //---------------------------------------------------------------------------------------------------------------
  688. __declspec ( naked ) void _asm_below(void)
  689. {
  690. __asm
  691. {
  692. fld qword ptr [eax]
  693. fld qword ptr [ebx]
  694. mov ebx, nextBlock
  695. fcompp
  696. fstsw ax
  697. shr eax, 6
  698. and eax, (1<<2)
  699. add eax, offset g_cmpaddtab
  700. fld dword ptr [eax]
  701. fstp qword ptr [ebx]
  702. mov eax, ebx
  703. add ebx, 8
  704. mov nextBlock, ebx
  705. ret
  706. }
  707. }
  708. //---------------------------------------------------------------------------------------------------------------
  709. __declspec ( naked ) void _asm_above(void)
  710. {
  711. __asm
  712. {
  713. fld qword ptr [ebx]
  714. fld qword ptr [eax]
  715. mov ebx, nextBlock
  716. fcompp
  717. fstsw ax
  718. shr eax, 6
  719. and eax, (1<<2)
  720. add eax, offset g_cmpaddtab
  721. fld dword ptr [eax]
  722. fstp qword ptr [ebx]
  723. mov eax, ebx
  724. add ebx, 8
  725. mov nextBlock, ebx
  726. ret
  727. }
  728. }
  729. __declspec ( naked ) void _asm_min(void)
  730. {
  731. __asm
  732. {
  733. fld qword ptr [eax]
  734. fld qword ptr [ebx]
  735. fld st(1)
  736. mov eax, nextBlock
  737. fld st(1)
  738. add eax, 8
  739. fsub
  740. mov nextBlock, eax
  741. fabs // stack contains fabs(1-2),1,2
  742. fchs
  743. sub eax, 8
  744. fadd
  745. fadd
  746. fld qword ptr [g_half]
  747. fmul
  748. fstp qword ptr [eax]
  749. ret
  750. }
  751. }
  752. __declspec ( naked ) void _asm_max(void)
  753. {
  754. __asm
  755. {
  756. fld qword ptr [eax]
  757. fld qword ptr [ebx]
  758. fld st(1)
  759. mov eax, nextBlock
  760. fld st(1)
  761. add eax, 8
  762. fsub
  763. mov nextBlock, eax
  764. fabs // stack contains fabs(1-2),1,2
  765. sub eax, 8
  766. fadd
  767. fadd
  768. fld qword ptr [g_half]
  769. fmul
  770. fstp qword ptr [eax]
  771. ret
  772. }
  773. }