src/stdlib/SDL_stdlib.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:29:03 +0000
branchSDL-1.3
changeset 1663 11775724e3fe
parent 1662 782fd950bd46
child 1668 4da1ee79c9af
permissions -rw-r--r--
fine tuning indent output
     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 void
   139 __declspec (naked)
   140 _alldiv ()
   141 {
   142     /* *INDENT-OFF* */
   143     __asm {
   144         push        edi
   145         push        esi
   146         push        ebx
   147         xor         edi,edi
   148         mov         eax,dword ptr [esp+14h]
   149         or          eax,eax
   150         jge         L1
   151         inc         edi
   152         mov         edx,dword ptr [esp+10h]
   153         neg         eax
   154         neg         edx
   155         sbb         eax,0
   156         mov         dword ptr [esp+14h],eax
   157         mov         dword ptr [esp+10h],edx
   158 L1:
   159         mov         eax,dword ptr [esp+1Ch]
   160         or          eax,eax
   161         jge         L2
   162         inc         edi
   163         mov         edx,dword ptr [esp+18h]
   164         neg         eax
   165         neg         edx
   166         sbb         eax,0
   167         mov         dword ptr [esp+1Ch],eax
   168         mov         dword ptr [esp+18h],edx
   169 L2:
   170         or          eax,eax
   171         jne         L3
   172         mov         ecx,dword ptr [esp+18h]
   173         mov         eax,dword ptr [esp+14h]
   174         xor         edx,edx
   175         div         ecx
   176         mov         ebx,eax
   177         mov         eax,dword ptr [esp+10h]
   178         div         ecx
   179         mov         edx,ebx
   180         jmp         L4
   181 L3:
   182         mov         ebx,eax
   183         mov         ecx,dword ptr [esp+18h]
   184         mov         edx,dword ptr [esp+14h]
   185         mov         eax,dword ptr [esp+10h]
   186 L5:
   187         shr         ebx,1
   188         rcr         ecx,1
   189         shr         edx,1
   190         rcr         eax,1
   191         or          ebx,ebx
   192         jne         L5
   193         div         ecx
   194         mov         esi,eax
   195         mul         dword ptr [esp+1Ch]
   196         mov         ecx,eax
   197         mov         eax,dword ptr [esp+18h]
   198         mul         esi
   199         add         edx,ecx
   200         jb          L6
   201         cmp         edx,dword ptr [esp+14h]
   202         ja          L6
   203         jb          L7
   204         cmp         eax,dword ptr [esp+10h]
   205         jbe         L7
   206 L6:
   207         dec         esi
   208 L7:
   209         xor         edx,edx
   210         mov         eax,esi
   211 L4:
   212         dec         edi
   213         jne         L8
   214         neg         edx
   215         neg         eax
   216         sbb         edx,0
   217 L8:
   218         pop         ebx
   219         pop         esi
   220         pop         edi
   221         ret         10h
   222     }
   223     /* *INDENT-ON* */
   224 }
   225 void
   226 __declspec (naked)
   227 _aulldiv ()
   228 {
   229     /* *INDENT-OFF* */
   230     __asm {
   231         push        ebx
   232         push        esi
   233         mov         eax,dword ptr [esp+18h]
   234         or          eax,eax
   235         jne         L1
   236         mov         ecx,dword ptr [esp+14h]
   237         mov         eax,dword ptr [esp+10h]
   238         xor         edx,edx
   239         div         ecx
   240         mov         ebx,eax
   241         mov         eax,dword ptr [esp+0Ch]
   242         div         ecx
   243         mov         edx,ebx
   244         jmp         L2
   245 L1:
   246         mov         ecx,eax
   247         mov         ebx,dword ptr [esp+14h]
   248         mov         edx,dword ptr [esp+10h]
   249         mov         eax,dword ptr [esp+0Ch]
   250 L3:
   251         shr         ecx,1
   252         rcr         ebx,1
   253         shr         edx,1
   254         rcr         eax,1
   255         or          ecx,ecx
   256         jne         L3
   257         div         ebx
   258         mov         esi,eax
   259         mul         dword ptr [esp+18h]
   260         mov         ecx,eax
   261         mov         eax,dword ptr [esp+14h]
   262         mul         esi
   263         add         edx,ecx
   264         jb          L4
   265         cmp         edx,dword ptr [esp+10h]
   266         ja          L4
   267         jb          L5
   268         cmp         eax,dword ptr [esp+0Ch]
   269         jbe         L5
   270 L4:
   271         dec         esi
   272 L5:
   273         xor         edx,edx
   274         mov         eax,esi
   275 L2:
   276         pop         esi
   277         pop         ebx
   278         ret         10h
   279     }
   280     /* *INDENT-ON* */
   281 }
   282 void
   283 __declspec (naked)
   284 _allrem ()
   285 {
   286     /* *INDENT-OFF* */
   287     __asm {
   288         push        ebx
   289         push        edi
   290         xor         edi,edi
   291         mov         eax,dword ptr [esp+10h]
   292         or          eax,eax
   293         jge         L1
   294         inc         edi
   295         mov         edx,dword ptr [esp+0Ch]
   296         neg         eax
   297         neg         edx
   298         sbb         eax,0
   299         mov         dword ptr [esp+10h],eax
   300         mov         dword ptr [esp+0Ch],edx
   301 L1:
   302         mov         eax,dword ptr [esp+18h]
   303         or          eax,eax
   304         jge         L2
   305         mov         edx,dword ptr [esp+14h]
   306         neg         eax
   307         neg         edx
   308         sbb         eax,0
   309         mov         dword ptr [esp+18h],eax
   310         mov         dword ptr [esp+14h],edx
   311 L2:
   312         or          eax,eax
   313         jne         L3
   314         mov         ecx,dword ptr [esp+14h]
   315         mov         eax,dword ptr [esp+10h]
   316         xor         edx,edx
   317         div         ecx
   318         mov         eax,dword ptr [esp+0Ch]
   319         div         ecx
   320         mov         eax,edx
   321         xor         edx,edx
   322         dec         edi
   323         jns         L4
   324         jmp         L8
   325 L3:
   326         mov         ebx,eax
   327         mov         ecx,dword ptr [esp+14h]
   328         mov         edx,dword ptr [esp+10h]
   329         mov         eax,dword ptr [esp+0Ch]
   330 L5:
   331         shr         ebx,1
   332         rcr         ecx,1
   333         shr         edx,1
   334         rcr         eax,1
   335         or          ebx,ebx
   336         jne         L5
   337         div         ecx
   338         mov         ecx,eax
   339         mul         dword ptr [esp+18h]
   340         xchg        eax,ecx
   341         mul         dword ptr [esp+14h]
   342         add         edx,ecx
   343         jb          L6
   344         cmp         edx,dword ptr [esp+10h]
   345         ja          L6
   346         jb          L7
   347         cmp         eax,dword ptr [esp+0Ch]
   348         jbe         L7
   349 L6:
   350         sub         eax,dword ptr [esp+14h]
   351         sbb         edx,dword ptr [esp+18h]
   352 L7:
   353         sub         eax,dword ptr [esp+0Ch]
   354         sbb         edx,dword ptr [esp+10h]
   355         dec         edi
   356         jns         L8
   357 L4:
   358         neg         edx
   359         neg         eax
   360         sbb         edx,0
   361 L8:
   362         pop         edi
   363         pop         ebx
   364         ret         10h
   365     }
   366     /* *INDENT-ON* */
   367 }
   368 void
   369 __declspec (naked)
   370 _aullrem ()
   371 {
   372     /* *INDENT-OFF* */
   373     __asm {
   374         push        ebx
   375         mov         eax,dword ptr [esp+14h]
   376         or          eax,eax
   377         jne         L1
   378         mov         ecx,dword ptr [esp+10h]
   379         mov         eax,dword ptr [esp+0Ch]
   380         xor         edx,edx
   381         div         ecx
   382         mov         eax,dword ptr [esp+8]
   383         div         ecx
   384         mov         eax,edx
   385         xor         edx,edx
   386         jmp         L2
   387 L1:
   388         mov         ecx,eax
   389         mov         ebx,dword ptr [esp+10h]
   390         mov         edx,dword ptr [esp+0Ch]
   391         mov         eax,dword ptr [esp+8]
   392 L3:
   393         shr         ecx,1
   394         rcr         ebx,1
   395         shr         edx,1
   396         rcr         eax,1
   397         or          ecx,ecx
   398         jne         L3
   399         div         ebx
   400         mov         ecx,eax
   401         mul         dword ptr [esp+14h]
   402         xchg        eax,ecx
   403         mul         dword ptr [esp+10h]
   404         add         edx,ecx
   405         jb          L4
   406         cmp         edx,dword ptr [esp+0Ch]
   407         ja          L4
   408         jb          L5
   409         cmp         eax,dword ptr [esp+8]
   410         jbe         L5
   411 L4:
   412         sub         eax,dword ptr [esp+10h]
   413         sbb         edx,dword ptr [esp+14h]
   414 L5:
   415         sub         eax,dword ptr [esp+8]
   416         sbb         edx,dword ptr [esp+0Ch]
   417         neg         edx
   418         neg         eax
   419         sbb         edx,0
   420 L2:
   421         pop         ebx
   422         ret         10h
   423     }
   424     /* *INDENT-ON* */
   425 }
   426 void
   427 __declspec (naked)
   428 _alldvrm ()
   429 {
   430     /* *INDENT-OFF* */
   431     __asm {
   432         push        edi
   433         push        esi
   434         push        ebp
   435         xor         edi,edi
   436         xor         ebp,ebp
   437         mov         eax,dword ptr [esp+14h]
   438         or          eax,eax
   439         jge         L1
   440         inc         edi
   441         inc         ebp
   442         mov         edx,dword ptr [esp+10h]
   443         neg         eax
   444         neg         edx
   445         sbb         eax,0
   446         mov         dword ptr [esp+14h],eax
   447         mov         dword ptr [esp+10h],edx
   448 L1:
   449         mov         eax,dword ptr [esp+1Ch]
   450         or          eax,eax
   451         jge         L2
   452         inc         edi
   453         mov         edx,dword ptr [esp+18h]
   454         neg         eax
   455         neg         edx
   456         sbb         eax,0
   457         mov         dword ptr [esp+1Ch],eax
   458         mov         dword ptr [esp+18h],edx
   459 L2:
   460         or          eax,eax
   461         jne         L3
   462         mov         ecx,dword ptr [esp+18h]
   463         mov         eax,dword ptr [esp+14h]
   464         xor         edx,edx
   465         div         ecx
   466         mov         ebx,eax
   467         mov         eax,dword ptr [esp+10h]
   468         div         ecx
   469         mov         esi,eax
   470         mov         eax,ebx
   471         mul         dword ptr [esp+18h]
   472         mov         ecx,eax
   473         mov         eax,esi
   474         mul         dword ptr [esp+18h]
   475         add         edx,ecx
   476         jmp         L4
   477 L3:
   478         mov         ebx,eax
   479         mov         ecx,dword ptr [esp+18h]
   480         mov         edx,dword ptr [esp+14h]
   481         mov         eax,dword ptr [esp+10h]
   482 L5:
   483         shr         ebx,1
   484         rcr         ecx,1
   485         shr         edx,1
   486         rcr         eax,1
   487         or          ebx,ebx
   488         jne         L5
   489         div         ecx
   490         mov         esi,eax
   491         mul         dword ptr [esp+1Ch]
   492         mov         ecx,eax
   493         mov         eax,dword ptr [esp+18h]
   494         mul         esi
   495         add         edx,ecx
   496         jb          L6
   497         cmp         edx,dword ptr [esp+14h]
   498         ja          L6
   499         jb          L7
   500         cmp         eax,dword ptr [esp+10h]
   501         jbe         L7
   502 L6:
   503         dec         esi
   504         sub         eax,dword ptr [esp+18h]
   505         sbb         edx,dword ptr [esp+1Ch]
   506 L7:
   507         xor         ebx,ebx
   508 L4:
   509         sub         eax,dword ptr [esp+10h]
   510         sbb         edx,dword ptr [esp+14h]
   511         dec         ebp
   512         jns         L9
   513         neg         edx
   514         neg         eax
   515         sbb         edx,0
   516 L9:
   517         mov         ecx,edx
   518         mov         edx,ebx
   519         mov         ebx,ecx
   520         mov         ecx,eax
   521         mov         eax,esi
   522         dec         edi
   523         jne         L8
   524         neg         edx
   525         neg         eax
   526         sbb         edx,0
   527 L8:
   528         pop         ebp
   529         pop         esi
   530         pop         edi
   531         ret         10h
   532     }
   533     /* *INDENT-ON* */
   534 }
   535 void
   536 __declspec (naked)
   537 _aulldvrm ()
   538 {
   539     /* *INDENT-OFF* */
   540     __asm {
   541         push        esi
   542         mov         eax,dword ptr [esp+14h]
   543         or          eax,eax
   544         jne         L1
   545         mov         ecx,dword ptr [esp+10h]
   546         mov         eax,dword ptr [esp+0Ch]
   547         xor         edx,edx
   548         div         ecx
   549         mov         ebx,eax
   550         mov         eax,dword ptr [esp+8]
   551         div         ecx
   552         mov         esi,eax
   553         mov         eax,ebx
   554         mul         dword ptr [esp+10h]
   555         mov         ecx,eax
   556         mov         eax,esi
   557         mul         dword ptr [esp+10h]
   558         add         edx,ecx
   559         jmp         L2
   560 L1:
   561         mov         ecx,eax
   562         mov         ebx,dword ptr [esp+10h]
   563         mov         edx,dword ptr [esp+0Ch]
   564         mov         eax,dword ptr [esp+8]
   565 L3:
   566         shr         ecx,1
   567         rcr         ebx,1
   568         shr         edx,1
   569         rcr         eax,1
   570         or          ecx,ecx
   571         jne         L3
   572         div         ebx
   573         mov         esi,eax
   574         mul         dword ptr [esp+14h]
   575         mov         ecx,eax
   576         mov         eax,dword ptr [esp+10h]
   577         mul         esi
   578         add         edx,ecx
   579         jb          L4
   580         cmp         edx,dword ptr [esp+0Ch]
   581         ja          L4
   582         jb          L5
   583         cmp         eax,dword ptr [esp+8]
   584         jbe         L5
   585 L4:
   586         dec         esi
   587         sub         eax,dword ptr [esp+10h]
   588         sbb         edx,dword ptr [esp+14h]
   589 L5:
   590         xor         ebx,ebx
   591 L2:
   592         sub         eax,dword ptr [esp+8]
   593         sbb         edx,dword ptr [esp+0Ch]
   594         neg         edx
   595         neg         eax
   596         sbb         edx,0
   597         mov         ecx,edx
   598         mov         edx,ebx
   599         mov         ebx,ecx
   600         mov         ecx,eax
   601         mov         eax,esi
   602         pop         esi
   603         ret         10h
   604     }
   605     /* *INDENT-ON* */
   606 }
   607 void
   608 __declspec (naked)
   609 _allshl ()
   610 {
   611     /* *INDENT-OFF* */
   612     __asm {
   613         cmp         cl,40h
   614         jae         RETZERO
   615         cmp         cl,20h
   616         jae         MORE32
   617         shld        edx,eax,cl
   618         shl         eax,cl
   619         ret
   620 MORE32:
   621         mov         edx,eax
   622         xor         eax,eax
   623         and         cl,1Fh
   624         shl         edx,cl
   625         ret
   626 RETZERO:
   627         xor         eax,eax
   628         xor         edx,edx
   629         ret
   630     }
   631     /* *INDENT-ON* */
   632 }
   633 void
   634 __declspec (naked)
   635 _aullshr ()
   636 {
   637     /* *INDENT-OFF* */
   638     __asm {
   639         cmp         cl,40h
   640         jae         RETZERO
   641         cmp         cl,20h
   642         jae         MORE32
   643         shrd        eax,edx,cl
   644         shr         edx,cl
   645         ret
   646 MORE32:
   647         mov         eax,edx
   648         xor         edx,edx
   649         and         cl,1Fh
   650         shr         eax,cl
   651         ret
   652 RETZERO:
   653         xor         eax,eax
   654         xor         edx,edx
   655         ret
   656     }
   657     /* *INDENT-ON* */
   658 }
   659 
   660 #endif /* MSC_VER */
   661 
   662 #endif /* !HAVE_LIBC */
   663 
   664 /* vi: set ts=4 sw=4 expandtab: */