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