summaryrefslogtreecommitdiff
path: root/setedit/scrnsave/plasmlib/mixsurf.c
blob: de0b2129cc32330955d0f0cf0213a3bba09d2302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* Copyright (C) 1997,1998,1999,2000 by Salvador E. Tropea (SET),
   see copying file for details */
/**[txh]********************************************************************

  Module: Mix Surface
  Comments:
  This module is used to add bitmaps.

***************************************************************************/

#include <stdio.h>
#ifdef FAKE_ALLEGRO
#include <fakealle.h>
#else
#include <allegro.h>
#endif
#include "mixsurf.h"

MS_4struct MS_4;

/**[txh]********************************************************************

  Description:
  That's a very generic and very optimized four surfaces mixer.

***************************************************************************/

/* That's the C version used to generate the ASM version.
   The ASM is 9% faster because I removed 1 silly instruction in the inner
loop. Is a clear bug in the GCC optimizer.
   Some interesting numbers:
   Each mix is 80*200 dwords = 16000 passes. The inner loop have 9
instructions (144000), the outther 10 more (144800), suppose the rest is
40 instructions (144840) and I got 182 fps (to memory) => 26360880. Puaj! */
void MS_4addC(void)
{
 static long i;
 long j;
 unsigned *s;
 // Create the pointers to 4 surfaces
 unsigned *yy1=MS_4.s1;
 unsigned *yy2=MS_4.s2;
 unsigned *yy3=MS_4.s3;
 unsigned *yy4=MS_4.s4;
 s=(unsigned *)MS_4.dest;

 for (i=MS_4.sH; i; --i)
   {
    for (j=0; j<MS_4.sWw; s++,j++)
        *s=yy1[j]+yy2[j]+yy3[j]+yy4[j];
    yy1+=MS_4.mWw;
    yy2+=MS_4.mWw;
    yy3+=MS_4.mWw;
    yy4+=MS_4.mWw;
   }
}

void MS_4add(void)
{
 static int i;
 asm("					\n\
	pushl %%ebp                     \n\
                                        \n\
   // s1,s2,s3,s4                       \n\
	movl _MS_4,%%ebp                \n\
	movl _MS_4+4,%%edi              \n\
	movl _MS_4+8,%%esi              \n\
	movl _MS_4+12,%%ebx             \n\
                                        \n\
   // dest                              \n\
	movl _MS_4+16,%%ecx             \n\
   // sH, fuck just 1 register more demm Intel   \n\
	movl _MS_4+24,%%eax             \n\
	movl %%eax,%k0                  \n\
                                        \n\
LoopI:                                  \n\
   // j=0                               \n\
	xorl %%edx,%%edx                \n\
	.align 2,0x90                   \n\
                                        \n\
LoopJ:                                  \n\
	movl (%%ebp,%%edx,4),%%eax      \n\
	addl (%%edi,%%edx,4),%%eax      \n\
	addl (%%esi,%%edx,4),%%eax      \n\
	addl (%%ebx,%%edx,4),%%eax      \n\
	movl %%eax,(%%ecx)              \n\
        // I moved it here hoping the P5 will pair the 4 instructions \n\
        incl %%edx   			\n\
	addl $4,%%ecx                   \n\
	cmpl %%edx,_MS_4+20             \n\
	ja LoopJ                        \n\
                                        \n\
	movl _MS_4+32,%%eax             \n\
	addl %%eax,%%ebp                \n\
	addl %%eax,%%edi                \n\
	addl %%eax,%%esi                \n\
	addl %%eax,%%ebx                \n\
	decl %k0                        \n\
	jne  LoopI                      \n\
                                        \n\
	popl %%ebp                      \n\
   ": "=m" (i) : : "%eax","%ebx","%ecx","%edx","%esi","%edi");
}

/*void MS_4addCyrix(void)
{
 static int i;
 asm("                                  \n\
	pushl %%ebp                     \n\
                                        \n\
   // s1,s2,s3,s4                       \n\
	movl _MS_4,%%ebp                \n\
	movl _MS_4+4,%%ecx              \n\
	movl _MS_4+8,%%esi              \n\
	movl _MS_4+12,%%ebx             \n\
                                        \n\
   // dest                              \n\
	movl _MS_4+16,%%edi             \n\
   // sH, fuck just 1 register more demm Intel \n\
	movl _MS_4+24,%%eax             \n\
	movl %%eax,%k0                  \n\
                                        \n\
0:                                      \n\
   // j=0                               \n\
	xorl %%edx,%%edx                \n\
	.align 2,0x90                   \n\
                                        \n\
1:                                      \n\
	movl (%%ebp,%%edx,4),%%eax      \n\
	addl (%%ecx,%%edx,4),%%eax      \n\
	addl (%%esi,%%edx,4),%%eax      \n\
	addl (%%ebx,%%edx,4),%%eax      \n\
   stosl                                \n\
	incl %%edx                      \n\
	cmpl %%edx,_MS_4+20             \n\
	ja   1b                         \n\
                                        \n\
	movl _MS_4+32,%%eax             \n\
	addl %%eax,%%ebp                \n\
	addl %%eax,%%ecx                \n\
	addl %%eax,%%esi                \n\
	addl %%eax,%%ebx                \n\
	decl %k0                        \n\
	jne  0b                         \n\
                                        \n\
	popl %%ebp                      \n\
   ": "=m" (i) : : "%eax","%ebx","%ecx","%edx","%esi","%edi");
}*/


// That's as fast as the assembler version. To be honest is a little slower
// but you can't meassure the difference. I didn't tested the it in a P5.
// I think the ASM is faster there.
void MS_4addW(void)
{
 long i=0,j=0;
 unsigned *s;
 // Create the pointers to 4 surfaces
 unsigned *yy1=MS_4.s1;
 unsigned *yy2=MS_4.s2;
 unsigned *yy3=MS_4.s3;
 unsigned *yy4=MS_4.s4;
 s=(unsigned *)MS_4.dest;

 for (i=MS_4.sH; i; --i)
   {
    for (j=MS_4.sWw; j; s++,--j)
       *s=yy1[j]+yy2[j]+yy3[j]+yy4[j];
    yy1+=MS_4.mWw;
    yy2+=MS_4.mWw;
    yy3+=MS_4.mWw;
    yy4+=MS_4.mWw;
   }
}

void MS_3addC(void)
{
 long i,j;
 unsigned *s;
 unsigned *yy1=MS_4.s1;
 unsigned *yy2=MS_4.s2;
 unsigned *yy3=MS_4.s3;
 s=(unsigned *)MS_4.dest;

 for (i=MS_4.sH; i; --i)
   {
    for (j=0; j<MS_4.sWw; s++,j++)
        *s=yy1[j]+yy2[j]+yy3[j];
    yy1+=MS_4.mWw;
    yy2+=MS_4.mWw;
    yy3+=MS_4.mWw;
   }
}

void MS_3add(void)
{
 asm("                                   \n\
	pushl %%ebp                      \n\
                                         \n\
   // s1,s2,s3                           \n\
	movl _MS_4,%%ebp                 \n\
	movl _MS_4+4,%%edi               \n\
	movl _MS_4+8,%%esi               \n\
                                         \n\
   // dest                               \n\
	movl _MS_4+16,%%ecx              \n\
   // sH                                 \n\
	movl _MS_4+24,%%ebx              \n\
                                         \n\
LoopI2:                                  \n\
   // j=0                                \n\
	xorl %%edx,%%edx                 \n\
                                         \n\
LoopJ2:                                  \n\
	movl (%%ebp,%%edx,4),%%eax       \n\
	addl (%%edi,%%edx,4),%%eax       \n\
	addl (%%esi,%%edx,4),%%eax       \n\
	movl %%eax,(%%ecx)               \n\
        // I moved it here hoping the P5 will pair the 4 instruction\n\
        incl %%edx                       \n\
	addl $4,%%ecx                    \n\
	cmpl %%edx,_MS_4+20              \n\
	ja LoopJ2                        \n\
                                         \n\
	movl _MS_4+32,%%eax              \n\
	addl %%eax,%%ebp                 \n\
	addl %%eax,%%edi                 \n\
	addl %%eax,%%esi                 \n\
	decl %%ebx                       \n\
	jne  LoopI2                      \n\
                                         \n\
	popl %%ebp                       \n\
   ": : : "%eax","%ebx","%ecx","%edx","%esi","%edi");
}

// Here my meassure indicates C faster than ASM ?!
void MS_2addC(void)
{
 long i,j,k;
 unsigned *s;
 unsigned *yy1=MS_4.s1;
 unsigned *yy2=MS_4.s2;
 s=(unsigned *)MS_4.dest;

 for (i=MS_4.sH; i; --i)
   {
    for (j=0,k=MS_4.sWw; k; s++,j++,--k)
        *s=yy1[j]+yy2[j];
    yy1+=MS_4.mWw;
    yy2+=MS_4.mWw;
   }
}

void MS_2add(void)
{
 asm("                                          \n\
	pushl %%ebp                             \n\
                                                \n\
   // s1,s2,s3                                  \n\
	movl _MS_4,%%esi                        \n\
	movl _MS_4+4,%%edi                      \n\
                                                \n\
   // dest                                      \n\
	movl _MS_4+16,%%ecx                     \n\
   // sH                                        \n\
	movl _MS_4+24,%%edx                     \n\
                                                \n\
LoopI3:                                         \n\
   // j=0                                       \n\
	xorl %%eax,%%eax                        \n\
   movl _MS_4+20,%%ebp                          \n\
	.align 2,0x90                           \n\
                                                \n\
LoopJ3:                                         \n\
	movl (%%esi,%%eax,4),%%ebx              \n\
	addl (%%edi,%%eax,4),%%ebx              \n\
	movl %%ebx,(%%ecx)                      \n\
	incl %%eax                              \n\
	addl $4,%%ecx                           \n\
   decl %%ebp                                   \n\
	jne  LoopJ3                             \n\
                                                \n\
	movl _MS_4+32,%%eax                     \n\
	addl %%eax,%%esi                        \n\
	addl %%eax,%%edi                        \n\
	decl %%edx                              \n\
	jne  LoopI3                             \n\
                                                \n\
	popl %%ebp                              \n\
   ": : : "%eax","%ebx","%ecx","%edx","%edi","%esi");
}

/**[txh]********************************************************************

  Description:
  This function is used to invert a an image generated with a for loop that
increments and then blitted with a loop that decrements.@p
  I used it to speed up the routines but isn't much faster and needs
multiple of four X coordinates, so isn't smooth.

***************************************************************************/

void MS_InvertDWordScreen(int w, int h, unsigned char *b)
{
 int x,y,s1,s2,w2;
 unsigned *s=(unsigned *)b;
 unsigned aux;

 w2=w/4;
 w/=8;

 for (y=h; y; s+=w2,y--)
     for (s1=0,s2=w2-1,x=w; x; s1++,s2--,x--)
        {
         aux=s[s1];
         s[s1]=s[s2];
         s[s2]=aux;
        }
}

void MS_AddBmpsMem(BITMAP *source, BITMAP *dest, int source_x,
                   int source_y, int dest_x, int dest_y, int width,
                   int height)
{
 int y,x;
 char *src=source->line[source_y]+source_x;
 int wS=source->w-width;
 char *dst=dest->line[dest_y]+dest_x;
 int wD=dest->w-width;

 for (y=height; y; src+=wS,dst+=wD,--y)
     for (x=width; x; src++,dst++,--x)
         *dst+=*src;
}

void MS_CutBMPColorsAt(BITMAP *bmp, unsigned char cut, unsigned char val)
{
 int y,x;
 unsigned char *s=bmp->line[0];

 for (y=bmp->h; y; --y)
     for (x=bmp->w; x; s++,--x)
         *s=*s<cut ? 0 : val;
}

void MS_CutBMPColorsAtInv(BITMAP *bmp, unsigned char cut, unsigned char val)
{
 int y,x;
 unsigned char *s=bmp->line[0];

 for (y=bmp->h; y; --y)
     for (x=bmp->w; x; s++,--x)
         *s=*s<cut ? val : 0;
}

void MS_AddWithMaskMem(BITMAP *source, BITMAP *dest, int source_x,
                   int source_y, int dest_x, int dest_y, int width,
                   int height,int val)
{
 int y,x;
 char *src=source->line[source_y]+source_x;
 int wS=source->w-width;
 char *dst=dest->line[dest_y]+dest_x;
 int wD=dest->w-width;

 for (y=height; y; src+=wS,dst+=wD,--y)
     for (x=width; x; src++,dst++,--x)
         if (*src)
            *dst+=val;
}

inline void Flush(unsigned char **d,int count,int type)
{
 while (count>0)
   {
    if (count>127)
      {
       *((*d)++)=type | 127;
       count-=127;
      }
    else
      {
       *((*d)++)=type | count;
       count=0;
      }
   }
}

void MS_CompactMask(BITMAP *bmp)
{
 int x,y;
 int w=bmp->w,h=bmp->h,count=1;
 unsigned char *s=bmp->line[0],*d;
 int is_1;
 d=s;

 for (y=h; y; --y)
    {
     is_1=*s!=0;
     count=1;
     for (s++,x=1; x<w; s++,x++)
        {
         if (is_1)
           {
            if (*s)
              {
               count++;
              }
            else
              {
               Flush(&d,count,0x80);
               is_1=0;
               count=1;
              }
           }
         else
           {
            if (!*s)
               count++;
            else
              {
               Flush(&d,count,0);
               is_1=1;
               count=1;
              }
           }
        }
     if (is_1)
        Flush(&d,count,0x80);
     else
        Flush(&d,count,0);
     *(d++)=0;
    }
}

void MS_AddWithMaskMemC(char *source, BITMAP *dest, int dest_x,
                        int dest_y, int width, int height, int val)
{
 int y;
 char *src=source;
 char *dst=dest->line[dest_y]+dest_x;
 int wD=dest->w-width;
 unsigned char count;

 for (y=height; y; dst+=wD,--y)
    {
     while (*src)
       {
        if (*src & 0x80)
          {
#if 0
           count=*src & 0x7F;
           while (count--)
             *(dst++)+=val;
#else
           count=*src & 0x7F;
           do
             {
              *(dst++)+=val;
             }
           while (--count);
#endif
          }
        else
          dst+=*src;
        src++;
       }
     src++;
    }
}

void MS_DumpCompacted(BITMAP *bmp, char *name, char *file)
{
 int y;
 int h=bmp->h;
 FILE *f;
 unsigned char *src=bmp->line[0];

 f=fopen(file,"wt");
 if (!f) return;
 fprintf(f,"int %sW=%d,%sH=%d;\nunsigned char %sP[]={\n",name,bmp->w,name,bmp->h,name);

 for (y=0; y<h; y++)
    {
     while (*src)
       {
        fprintf(f,"%3d, ",*src);
        src++;
       }
     fprintf(f,"  0%c /* %d */\n",y==h-1 ? ' ' : ',',y);
     src++;
    }
 fprintf(f,"}; // Size: %ld Compression: %5.2f %%\n",src-bmp->line[0],(1.0-(src-bmp->line[0])/(double)(h*bmp->w))*100.0);
 fclose(f);
}

/* Menos del 3% de diferencia y no funciona! ver por que
void MS_AddWithMaskMem(BITMAP *source, BITMAP *dest, int source_x,
                   int source_y, int dest_x, int dest_y, int width,
                   int height,int val)
{
 char *src=source->line[source_y]+source_x;
 int wS=source->w-width;
 char *dst=dest->line[dest_y]+dest_x;
 int wD=dest->w-width;

 asm ("
   movl  %k1,%%ebp
	testl %%ecx,%%ecx
	je    5f
	testl %%edi,%%edi
	je    5f
	.align 2,0x90

1:
   movl  %%edi,%%eax
2:
	cmpb $0,(%%esi)
	je   3f
	addb %%bl,(%%edx)
3:
	incl %%esi
	incl %%edx
	decl %%eax
	jne  2b

4:
	addl %%ebp,%%esi
	addl %k0,%%edx
	decl %%ecx
	jne  1b
5:
 " : "=m" (wD)
   : "q" (wS), "c" (height), "D" (width), "S" (src), "b" (val), "d" (dst)
   : "%eax", "%ebp");
}*/