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