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