This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_stdlib.c
executable file
·743 lines (712 loc) · 18.3 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2011 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
22
#include "SDL_config.h"
23
24
25
/* This file contains portable stdlib functions for SDL */
26
#include "SDL_stdinc.h"
27
28
29
30
#ifndef HAVE_LIBC
/* These are some C runtime intrinsics that need to be defined */
31
#if defined(_MSC_VER)
32
33
34
#ifndef __FLTUSED__
#define __FLTUSED__
35
__declspec(selectany) int _fltused = 1;
36
37
#endif
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
/* The optimizer on Visual Studio 2010 generates memcpy() calls */
#if _MSC_VER == 1600 && defined(_WIN64) && !defined(_DEBUG)
#include <intrin.h>
#pragma function(memcpy)
void * memcpy ( void * destination, const void * source, size_t num )
{
const Uint8 *src = (const Uint8 *)source;
Uint8 *dst = (Uint8 *)destination;
size_t i;
/* All WIN64 architectures have SSE, right? */
if (!((uintptr_t) src & 15) && !((uintptr_t) dst & 15)) {
__m128 values[4];
for (i = num / 64; i--;) {
_mm_prefetch(src, _MM_HINT_NTA);
values[0] = *(__m128 *) (src + 0);
values[1] = *(__m128 *) (src + 16);
values[2] = *(__m128 *) (src + 32);
values[3] = *(__m128 *) (src + 48);
_mm_stream_ps((float *) (dst + 0), values[0]);
_mm_stream_ps((float *) (dst + 16), values[1]);
_mm_stream_ps((float *) (dst + 32), values[2]);
_mm_stream_ps((float *) (dst + 48), values[3]);
src += 64;
dst += 64;
}
num &= 63;
}
while (num--) {
*dst++ = *src++;
}
return destination;
}
#endif /* _MSC_VER == 1600 && defined(_WIN64) && !defined(_DEBUG) */
75
#ifdef _M_IX86
76
77
78
79
80
81
82
void
__declspec(naked)
_chkstk()
{
}
83
/* Float to long */
84
85
86
void
__declspec(naked)
_ftol()
87
{
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* *INDENT-OFF* */
__asm {
push ebp
mov ebp,esp
sub esp,20h
and esp,0FFFFFFF0h
fld st(0)
fst dword ptr [esp+18h]
fistp qword ptr [esp+10h]
fild qword ptr [esp+10h]
mov edx,dword ptr [esp+18h]
mov eax,dword ptr [esp+10h]
test eax,eax
je integer_QnaN_or_zero
102
arg_is_not_integer_QnaN:
103
104
105
106
107
108
109
110
111
112
113
fsubp st(1),st
test edx,edx
jns positive
fstp dword ptr [esp]
mov ecx,dword ptr [esp]
xor ecx,80000000h
add ecx,7FFFFFFFh
adc eax,0
mov edx,dword ptr [esp+14h]
adc edx,0
jmp localexit
114
positive:
115
116
117
118
119
120
121
fstp dword ptr [esp]
mov ecx,dword ptr [esp]
add ecx,7FFFFFFFh
sbb eax,0
mov edx,dword ptr [esp+14h]
sbb edx,0
jmp localexit
122
integer_QnaN_or_zero:
123
124
125
126
127
mov edx,dword ptr [esp+14h]
test edx,7FFFFFFFh
jne arg_is_not_integer_QnaN
fstp dword ptr [esp+18h]
fstp dword ptr [esp+18h]
128
localexit:
129
130
131
132
leave
ret
}
/* *INDENT-ON* */
133
}
134
135
136
void
_ftol2_sse()
137
{
138
_ftol();
139
140
}
141
/* 64-bit math operators for 32-bit systems */
142
143
144
void
__declspec(naked)
_allmul()
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
/* *INDENT-OFF* */
__asm {
push ebp
mov ebp,esp
push edi
push esi
push ebx
sub esp,0Ch
mov eax,dword ptr [ebp+10h]
mov edi,dword ptr [ebp+8]
mov ebx,eax
mov esi,eax
sar esi,1Fh
mov eax,dword ptr [ebp+8]
mul ebx
imul edi,esi
mov ecx,edx
mov dword ptr [ebp-18h],eax
mov edx,dword ptr [ebp+0Ch]
add ecx,edi
imul ebx,edx
mov eax,dword ptr [ebp-18h]
lea ebx,[ebx+ecx]
mov dword ptr [ebp-14h],ebx
mov edx,dword ptr [ebp-14h]
add esp,0Ch
pop ebx
pop esi
pop edi
pop ebp
ret
}
/* *INDENT-ON* */
179
}
180
181
182
183
void
__declspec(naked)
_alldiv()
184
{
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* *INDENT-OFF* */
__asm {
push edi
push esi
push ebx
xor edi,edi
mov eax,dword ptr [esp+14h]
or eax,eax
jge L1
inc edi
mov edx,dword ptr [esp+10h]
neg eax
neg edx
sbb eax,0
mov dword ptr [esp+14h],eax
mov dword ptr [esp+10h],edx
201
L1:
202
203
204
205
206
207
208
209
210
211
mov eax,dword ptr [esp+1Ch]
or eax,eax
jge L2
inc edi
mov edx,dword ptr [esp+18h]
neg eax
neg edx
sbb eax,0
mov dword ptr [esp+1Ch],eax
mov dword ptr [esp+18h],edx
212
L2:
213
214
215
216
217
218
219
220
221
222
223
or eax,eax
jne L3
mov ecx,dword ptr [esp+18h]
mov eax,dword ptr [esp+14h]
xor edx,edx
div ecx
mov ebx,eax
mov eax,dword ptr [esp+10h]
div ecx
mov edx,ebx
jmp L4
224
L3:
225
226
227
228
mov ebx,eax
mov ecx,dword ptr [esp+18h]
mov edx,dword ptr [esp+14h]
mov eax,dword ptr [esp+10h]
229
L5:
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
shr ebx,1
rcr ecx,1
shr edx,1
rcr eax,1
or ebx,ebx
jne L5
div ecx
mov esi,eax
mul dword ptr [esp+1Ch]
mov ecx,eax
mov eax,dword ptr [esp+18h]
mul esi
add edx,ecx
jb L6
cmp edx,dword ptr [esp+14h]
ja L6
jb L7
cmp eax,dword ptr [esp+10h]
jbe L7
249
L6:
250
dec esi
251
L7:
252
253
xor edx,edx
mov eax,esi
254
L4:
255
256
257
258
259
dec edi
jne L8
neg edx
neg eax
sbb edx,0
260
L8:
261
262
263
264
265
266
pop ebx
pop esi
pop edi
ret 10h
}
/* *INDENT-ON* */
267
}
268
269
270
271
void
__declspec(naked)
_aulldiv()
272
{
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* *INDENT-OFF* */
__asm {
push ebx
push esi
mov eax,dword ptr [esp+18h]
or eax,eax
jne L1
mov ecx,dword ptr [esp+14h]
mov eax,dword ptr [esp+10h]
xor edx,edx
div ecx
mov ebx,eax
mov eax,dword ptr [esp+0Ch]
div ecx
mov edx,ebx
jmp L2
289
L1:
290
291
292
293
mov ecx,eax
mov ebx,dword ptr [esp+14h]
mov edx,dword ptr [esp+10h]
mov eax,dword ptr [esp+0Ch]
294
L3:
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
shr ecx,1
rcr ebx,1
shr edx,1
rcr eax,1
or ecx,ecx
jne L3
div ebx
mov esi,eax
mul dword ptr [esp+18h]
mov ecx,eax
mov eax,dword ptr [esp+14h]
mul esi
add edx,ecx
jb L4
cmp edx,dword ptr [esp+10h]
ja L4
jb L5
cmp eax,dword ptr [esp+0Ch]
jbe L5
314
L4:
315
dec esi
316
L5:
317
318
xor edx,edx
mov eax,esi
319
L2:
320
321
322
323
324
pop esi
pop ebx
ret 10h
}
/* *INDENT-ON* */
325
}
326
327
328
329
void
__declspec(naked)
_allrem()
330
{
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* *INDENT-OFF* */
__asm {
push ebx
push edi
xor edi,edi
mov eax,dword ptr [esp+10h]
or eax,eax
jge L1
inc edi
mov edx,dword ptr [esp+0Ch]
neg eax
neg edx
sbb eax,0
mov dword ptr [esp+10h],eax
mov dword ptr [esp+0Ch],edx
346
L1:
347
348
349
350
351
352
353
354
355
mov eax,dword ptr [esp+18h]
or eax,eax
jge L2
mov edx,dword ptr [esp+14h]
neg eax
neg edx
sbb eax,0
mov dword ptr [esp+18h],eax
mov dword ptr [esp+14h],edx
356
L2:
357
358
359
360
361
362
363
364
365
366
367
368
369
or eax,eax
jne L3
mov ecx,dword ptr [esp+14h]
mov eax,dword ptr [esp+10h]
xor edx,edx
div ecx
mov eax,dword ptr [esp+0Ch]
div ecx
mov eax,edx
xor edx,edx
dec edi
jns L4
jmp L8
370
L3:
371
372
373
374
mov ebx,eax
mov ecx,dword ptr [esp+14h]
mov edx,dword ptr [esp+10h]
mov eax,dword ptr [esp+0Ch]
375
L5:
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
shr ebx,1
rcr ecx,1
shr edx,1
rcr eax,1
or ebx,ebx
jne L5
div ecx
mov ecx,eax
mul dword ptr [esp+18h]
xchg eax,ecx
mul dword ptr [esp+14h]
add edx,ecx
jb L6
cmp edx,dword ptr [esp+10h]
ja L6
jb L7
cmp eax,dword ptr [esp+0Ch]
jbe L7
394
L6:
395
396
sub eax,dword ptr [esp+14h]
sbb edx,dword ptr [esp+18h]
397
L7:
398
399
400
401
sub eax,dword ptr [esp+0Ch]
sbb edx,dword ptr [esp+10h]
dec edi
jns L8
402
L4:
403
404
405
neg edx
neg eax
sbb edx,0
406
L8:
407
408
409
410
411
pop edi
pop ebx
ret 10h
}
/* *INDENT-ON* */
412
}
413
414
415
416
void
__declspec(naked)
_aullrem()
417
{
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* *INDENT-OFF* */
__asm {
push ebx
mov eax,dword ptr [esp+14h]
or eax,eax
jne L1
mov ecx,dword ptr [esp+10h]
mov eax,dword ptr [esp+0Ch]
xor edx,edx
div ecx
mov eax,dword ptr [esp+8]
div ecx
mov eax,edx
xor edx,edx
jmp L2
433
L1:
434
435
436
437
mov ecx,eax
mov ebx,dword ptr [esp+10h]
mov edx,dword ptr [esp+0Ch]
mov eax,dword ptr [esp+8]
438
L3:
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
shr ecx,1
rcr ebx,1
shr edx,1
rcr eax,1
or ecx,ecx
jne L3
div ebx
mov ecx,eax
mul dword ptr [esp+14h]
xchg eax,ecx
mul dword ptr [esp+10h]
add edx,ecx
jb L4
cmp edx,dword ptr [esp+0Ch]
ja L4
jb L5
cmp eax,dword ptr [esp+8]
jbe L5
457
L4:
458
459
sub eax,dword ptr [esp+10h]
sbb edx,dword ptr [esp+14h]
460
L5:
461
462
463
464
465
sub eax,dword ptr [esp+8]
sbb edx,dword ptr [esp+0Ch]
neg edx
neg eax
sbb edx,0
466
L2:
467
468
469
470
pop ebx
ret 10h
}
/* *INDENT-ON* */
471
}
472
473
474
475
void
__declspec(naked)
_alldvrm()
476
{
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* *INDENT-OFF* */
__asm {
push edi
push esi
push ebp
xor edi,edi
xor ebp,ebp
mov eax,dword ptr [esp+14h]
or eax,eax
jge L1
inc edi
inc ebp
mov edx,dword ptr [esp+10h]
neg eax
neg edx
sbb eax,0
mov dword ptr [esp+14h],eax
mov dword ptr [esp+10h],edx
495
L1:
496
497
498
499
500
501
502
503
504
505
mov eax,dword ptr [esp+1Ch]
or eax,eax
jge L2
inc edi
mov edx,dword ptr [esp+18h]
neg eax
neg edx
sbb eax,0
mov dword ptr [esp+1Ch],eax
mov dword ptr [esp+18h],edx
506
L2:
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
or eax,eax
jne L3
mov ecx,dword ptr [esp+18h]
mov eax,dword ptr [esp+14h]
xor edx,edx
div ecx
mov ebx,eax
mov eax,dword ptr [esp+10h]
div ecx
mov esi,eax
mov eax,ebx
mul dword ptr [esp+18h]
mov ecx,eax
mov eax,esi
mul dword ptr [esp+18h]
add edx,ecx
jmp L4
524
L3:
525
526
527
528
mov ebx,eax
mov ecx,dword ptr [esp+18h]
mov edx,dword ptr [esp+14h]
mov eax,dword ptr [esp+10h]
529
L5:
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
shr ebx,1
rcr ecx,1
shr edx,1
rcr eax,1
or ebx,ebx
jne L5
div ecx
mov esi,eax
mul dword ptr [esp+1Ch]
mov ecx,eax
mov eax,dword ptr [esp+18h]
mul esi
add edx,ecx
jb L6
cmp edx,dword ptr [esp+14h]
ja L6
jb L7
cmp eax,dword ptr [esp+10h]
jbe L7
549
L6:
550
551
552
dec esi
sub eax,dword ptr [esp+18h]
sbb edx,dword ptr [esp+1Ch]
553
L7:
554
xor ebx,ebx
555
L4:
556
557
558
559
560
561
562
sub eax,dword ptr [esp+10h]
sbb edx,dword ptr [esp+14h]
dec ebp
jns L9
neg edx
neg eax
sbb edx,0
563
L9:
564
565
566
567
568
569
570
571
572
573
mov ecx,edx
mov edx,ebx
mov ebx,ecx
mov ecx,eax
mov eax,esi
dec edi
jne L8
neg edx
neg eax
sbb edx,0
574
L8:
575
576
577
578
579
580
pop ebp
pop esi
pop edi
ret 10h
}
/* *INDENT-ON* */
581
}
582
583
584
585
void
__declspec(naked)
_aulldvrm()
586
{
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/* *INDENT-OFF* */
__asm {
push esi
mov eax,dword ptr [esp+14h]
or eax,eax
jne L1
mov ecx,dword ptr [esp+10h]
mov eax,dword ptr [esp+0Ch]
xor edx,edx
div ecx
mov ebx,eax
mov eax,dword ptr [esp+8]
div ecx
mov esi,eax
mov eax,ebx
mul dword ptr [esp+10h]
mov ecx,eax
mov eax,esi
mul dword ptr [esp+10h]
add edx,ecx
jmp L2
608
L1:
609
610
611
612
mov ecx,eax
mov ebx,dword ptr [esp+10h]
mov edx,dword ptr [esp+0Ch]
mov eax,dword ptr [esp+8]
613
L3:
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
shr ecx,1
rcr ebx,1
shr edx,1
rcr eax,1
or ecx,ecx
jne L3
div ebx
mov esi,eax
mul dword ptr [esp+14h]
mov ecx,eax
mov eax,dword ptr [esp+10h]
mul esi
add edx,ecx
jb L4
cmp edx,dword ptr [esp+0Ch]
ja L4
jb L5
cmp eax,dword ptr [esp+8]
jbe L5
633
L4:
634
635
636
dec esi
sub eax,dword ptr [esp+10h]
sbb edx,dword ptr [esp+14h]
637
L5:
638
xor ebx,ebx
639
L2:
640
641
642
643
644
645
646
647
648
649
650
651
652
653
sub eax,dword ptr [esp+8]
sbb edx,dword ptr [esp+0Ch]
neg edx
neg eax
sbb edx,0
mov ecx,edx
mov edx,ebx
mov ebx,ecx
mov ecx,eax
mov eax,esi
pop esi
ret 10h
}
/* *INDENT-ON* */
654
}
655
656
657
658
void
__declspec(naked)
_allshl()
659
{
660
661
662
663
664
665
666
667
668
/* *INDENT-OFF* */
__asm {
cmp cl,40h
jae RETZERO
cmp cl,20h
jae MORE32
shld edx,eax,cl
shl eax,cl
ret
669
MORE32:
670
671
672
673
674
mov edx,eax
xor eax,eax
and cl,1Fh
shl edx,cl
ret
675
RETZERO:
676
677
678
679
680
xor eax,eax
xor edx,edx
ret
}
/* *INDENT-ON* */
681
}
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
void
__declspec(naked)
_allshr()
{
/* *INDENT-OFF* */
__asm {
cmp cl,40h
jae RETZERO
cmp cl,20h
jae MORE32
shrd eax,edx,cl
sar edx,cl
ret
MORE32:
mov eax,edx
xor edx,edx
and cl,1Fh
sar eax,cl
ret
RETZERO:
xor eax,eax
xor edx,edx
ret
}
/* *INDENT-ON* */
}
710
711
712
void
__declspec(naked)
_aullshr()
713
{
714
715
716
717
718
719
720
721
722
/* *INDENT-OFF* */
__asm {
cmp cl,40h
jae RETZERO
cmp cl,20h
jae MORE32
shrd eax,edx,cl
shr edx,cl
ret
723
MORE32:
724
725
726
727
728
mov eax,edx
xor edx,edx
and cl,1Fh
shr eax,cl
ret
729
RETZERO:
730
731
732
733
734
xor eax,eax
xor edx,edx
ret
}
/* *INDENT-ON* */
735
736
}
737
#endif /* _M_IX86 */
738
739
740
#endif /* MSC_VER */
741
#endif /* !HAVE_LIBC */
742
743
/* vi: set ts=4 sw=4 expandtab: */