PageRenderTime 43ms CodeModel.GetById 13ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

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