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