src/stdlib/SDL_stdlib.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:27:32 +0000
changeset 2859 99210400e8b9
parent 2781 5651642f4a78
child 3253 5d7ef5970073
permissions -rw-r--r--
Updated copyright date
     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 #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 _ftol2_sse()
    94 {
    95     _ftol();
    96 }
    97 
    98 /* 64-bit math operators for 32-bit systems */
    99 void
   100 __declspec(naked)
   101 _allmul()
   102 {
   103     /* *INDENT-OFF* */
   104     __asm {
   105         push        ebp
   106         mov         ebp,esp
   107         push        edi
   108         push        esi
   109         push        ebx
   110         sub         esp,0Ch
   111         mov         eax,dword ptr [ebp+10h]
   112         mov         edi,dword ptr [ebp+8]
   113         mov         ebx,eax
   114         mov         esi,eax
   115         sar         esi,1Fh
   116         mov         eax,dword ptr [ebp+8]
   117         mul         ebx
   118         imul        edi,esi
   119         mov         ecx,edx
   120         mov         dword ptr [ebp-18h],eax
   121         mov         edx,dword ptr [ebp+0Ch]
   122         add         ecx,edi
   123         imul        ebx,edx
   124         mov         eax,dword ptr [ebp-18h]
   125         lea         ebx,[ebx+ecx]
   126         mov         dword ptr [ebp-14h],ebx
   127         mov         edx,dword ptr [ebp-14h]
   128         add         esp,0Ch
   129         pop         ebx
   130         pop         esi
   131         pop         edi
   132         pop         ebp
   133         ret
   134     }
   135     /* *INDENT-ON* */
   136 }
   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 
   226 void
   227 __declspec(naked)
   228 _aulldiv()
   229 {
   230     /* *INDENT-OFF* */
   231     __asm {
   232         push        ebx
   233         push        esi
   234         mov         eax,dword ptr [esp+18h]
   235         or          eax,eax
   236         jne         L1
   237         mov         ecx,dword ptr [esp+14h]
   238         mov         eax,dword ptr [esp+10h]
   239         xor         edx,edx
   240         div         ecx
   241         mov         ebx,eax
   242         mov         eax,dword ptr [esp+0Ch]
   243         div         ecx
   244         mov         edx,ebx
   245         jmp         L2
   246 L1:
   247         mov         ecx,eax
   248         mov         ebx,dword ptr [esp+14h]
   249         mov         edx,dword ptr [esp+10h]
   250         mov         eax,dword ptr [esp+0Ch]
   251 L3:
   252         shr         ecx,1
   253         rcr         ebx,1
   254         shr         edx,1
   255         rcr         eax,1
   256         or          ecx,ecx
   257         jne         L3
   258         div         ebx
   259         mov         esi,eax
   260         mul         dword ptr [esp+18h]
   261         mov         ecx,eax
   262         mov         eax,dword ptr [esp+14h]
   263         mul         esi
   264         add         edx,ecx
   265         jb          L4
   266         cmp         edx,dword ptr [esp+10h]
   267         ja          L4
   268         jb          L5
   269         cmp         eax,dword ptr [esp+0Ch]
   270         jbe         L5
   271 L4:
   272         dec         esi
   273 L5:
   274         xor         edx,edx
   275         mov         eax,esi
   276 L2:
   277         pop         esi
   278         pop         ebx
   279         ret         10h
   280     }
   281     /* *INDENT-ON* */
   282 }
   283 
   284 void
   285 __declspec(naked)
   286 _allrem()
   287 {
   288     /* *INDENT-OFF* */
   289     __asm {
   290         push        ebx
   291         push        edi
   292         xor         edi,edi
   293         mov         eax,dword ptr [esp+10h]
   294         or          eax,eax
   295         jge         L1
   296         inc         edi
   297         mov         edx,dword ptr [esp+0Ch]
   298         neg         eax
   299         neg         edx
   300         sbb         eax,0
   301         mov         dword ptr [esp+10h],eax
   302         mov         dword ptr [esp+0Ch],edx
   303 L1:
   304         mov         eax,dword ptr [esp+18h]
   305         or          eax,eax
   306         jge         L2
   307         mov         edx,dword ptr [esp+14h]
   308         neg         eax
   309         neg         edx
   310         sbb         eax,0
   311         mov         dword ptr [esp+18h],eax
   312         mov         dword ptr [esp+14h],edx
   313 L2:
   314         or          eax,eax
   315         jne         L3
   316         mov         ecx,dword ptr [esp+14h]
   317         mov         eax,dword ptr [esp+10h]
   318         xor         edx,edx
   319         div         ecx
   320         mov         eax,dword ptr [esp+0Ch]
   321         div         ecx
   322         mov         eax,edx
   323         xor         edx,edx
   324         dec         edi
   325         jns         L4
   326         jmp         L8
   327 L3:
   328         mov         ebx,eax
   329         mov         ecx,dword ptr [esp+14h]
   330         mov         edx,dword ptr [esp+10h]
   331         mov         eax,dword ptr [esp+0Ch]
   332 L5:
   333         shr         ebx,1
   334         rcr         ecx,1
   335         shr         edx,1
   336         rcr         eax,1
   337         or          ebx,ebx
   338         jne         L5
   339         div         ecx
   340         mov         ecx,eax
   341         mul         dword ptr [esp+18h]
   342         xchg        eax,ecx
   343         mul         dword ptr [esp+14h]
   344         add         edx,ecx
   345         jb          L6
   346         cmp         edx,dword ptr [esp+10h]
   347         ja          L6
   348         jb          L7
   349         cmp         eax,dword ptr [esp+0Ch]
   350         jbe         L7
   351 L6:
   352         sub         eax,dword ptr [esp+14h]
   353         sbb         edx,dword ptr [esp+18h]
   354 L7:
   355         sub         eax,dword ptr [esp+0Ch]
   356         sbb         edx,dword ptr [esp+10h]
   357         dec         edi
   358         jns         L8
   359 L4:
   360         neg         edx
   361         neg         eax
   362         sbb         edx,0
   363 L8:
   364         pop         edi
   365         pop         ebx
   366         ret         10h
   367     }
   368     /* *INDENT-ON* */
   369 }
   370 
   371 void
   372 __declspec(naked)
   373 _aullrem()
   374 {
   375     /* *INDENT-OFF* */
   376     __asm {
   377         push        ebx
   378         mov         eax,dword ptr [esp+14h]
   379         or          eax,eax
   380         jne         L1
   381         mov         ecx,dword ptr [esp+10h]
   382         mov         eax,dword ptr [esp+0Ch]
   383         xor         edx,edx
   384         div         ecx
   385         mov         eax,dword ptr [esp+8]
   386         div         ecx
   387         mov         eax,edx
   388         xor         edx,edx
   389         jmp         L2
   390 L1:
   391         mov         ecx,eax
   392         mov         ebx,dword ptr [esp+10h]
   393         mov         edx,dword ptr [esp+0Ch]
   394         mov         eax,dword ptr [esp+8]
   395 L3:
   396         shr         ecx,1
   397         rcr         ebx,1
   398         shr         edx,1
   399         rcr         eax,1
   400         or          ecx,ecx
   401         jne         L3
   402         div         ebx
   403         mov         ecx,eax
   404         mul         dword ptr [esp+14h]
   405         xchg        eax,ecx
   406         mul         dword ptr [esp+10h]
   407         add         edx,ecx
   408         jb          L4
   409         cmp         edx,dword ptr [esp+0Ch]
   410         ja          L4
   411         jb          L5
   412         cmp         eax,dword ptr [esp+8]
   413         jbe         L5
   414 L4:
   415         sub         eax,dword ptr [esp+10h]
   416         sbb         edx,dword ptr [esp+14h]
   417 L5:
   418         sub         eax,dword ptr [esp+8]
   419         sbb         edx,dword ptr [esp+0Ch]
   420         neg         edx
   421         neg         eax
   422         sbb         edx,0
   423 L2:
   424         pop         ebx
   425         ret         10h
   426     }
   427     /* *INDENT-ON* */
   428 }
   429 
   430 void
   431 __declspec(naked)
   432 _alldvrm()
   433 {
   434     /* *INDENT-OFF* */
   435     __asm {
   436         push        edi
   437         push        esi
   438         push        ebp
   439         xor         edi,edi
   440         xor         ebp,ebp
   441         mov         eax,dword ptr [esp+14h]
   442         or          eax,eax
   443         jge         L1
   444         inc         edi
   445         inc         ebp
   446         mov         edx,dword ptr [esp+10h]
   447         neg         eax
   448         neg         edx
   449         sbb         eax,0
   450         mov         dword ptr [esp+14h],eax
   451         mov         dword ptr [esp+10h],edx
   452 L1:
   453         mov         eax,dword ptr [esp+1Ch]
   454         or          eax,eax
   455         jge         L2
   456         inc         edi
   457         mov         edx,dword ptr [esp+18h]
   458         neg         eax
   459         neg         edx
   460         sbb         eax,0
   461         mov         dword ptr [esp+1Ch],eax
   462         mov         dword ptr [esp+18h],edx
   463 L2:
   464         or          eax,eax
   465         jne         L3
   466         mov         ecx,dword ptr [esp+18h]
   467         mov         eax,dword ptr [esp+14h]
   468         xor         edx,edx
   469         div         ecx
   470         mov         ebx,eax
   471         mov         eax,dword ptr [esp+10h]
   472         div         ecx
   473         mov         esi,eax
   474         mov         eax,ebx
   475         mul         dword ptr [esp+18h]
   476         mov         ecx,eax
   477         mov         eax,esi
   478         mul         dword ptr [esp+18h]
   479         add         edx,ecx
   480         jmp         L4
   481 L3:
   482         mov         ebx,eax
   483         mov         ecx,dword ptr [esp+18h]
   484         mov         edx,dword ptr [esp+14h]
   485         mov         eax,dword ptr [esp+10h]
   486 L5:
   487         shr         ebx,1
   488         rcr         ecx,1
   489         shr         edx,1
   490         rcr         eax,1
   491         or          ebx,ebx
   492         jne         L5
   493         div         ecx
   494         mov         esi,eax
   495         mul         dword ptr [esp+1Ch]
   496         mov         ecx,eax
   497         mov         eax,dword ptr [esp+18h]
   498         mul         esi
   499         add         edx,ecx
   500         jb          L6
   501         cmp         edx,dword ptr [esp+14h]
   502         ja          L6
   503         jb          L7
   504         cmp         eax,dword ptr [esp+10h]
   505         jbe         L7
   506 L6:
   507         dec         esi
   508         sub         eax,dword ptr [esp+18h]
   509         sbb         edx,dword ptr [esp+1Ch]
   510 L7:
   511         xor         ebx,ebx
   512 L4:
   513         sub         eax,dword ptr [esp+10h]
   514         sbb         edx,dword ptr [esp+14h]
   515         dec         ebp
   516         jns         L9
   517         neg         edx
   518         neg         eax
   519         sbb         edx,0
   520 L9:
   521         mov         ecx,edx
   522         mov         edx,ebx
   523         mov         ebx,ecx
   524         mov         ecx,eax
   525         mov         eax,esi
   526         dec         edi
   527         jne         L8
   528         neg         edx
   529         neg         eax
   530         sbb         edx,0
   531 L8:
   532         pop         ebp
   533         pop         esi
   534         pop         edi
   535         ret         10h
   536     }
   537     /* *INDENT-ON* */
   538 }
   539 
   540 void
   541 __declspec(naked)
   542 _aulldvrm()
   543 {
   544     /* *INDENT-OFF* */
   545     __asm {
   546         push        esi
   547         mov         eax,dword ptr [esp+14h]
   548         or          eax,eax
   549         jne         L1
   550         mov         ecx,dword ptr [esp+10h]
   551         mov         eax,dword ptr [esp+0Ch]
   552         xor         edx,edx
   553         div         ecx
   554         mov         ebx,eax
   555         mov         eax,dword ptr [esp+8]
   556         div         ecx
   557         mov         esi,eax
   558         mov         eax,ebx
   559         mul         dword ptr [esp+10h]
   560         mov         ecx,eax
   561         mov         eax,esi
   562         mul         dword ptr [esp+10h]
   563         add         edx,ecx
   564         jmp         L2
   565 L1:
   566         mov         ecx,eax
   567         mov         ebx,dword ptr [esp+10h]
   568         mov         edx,dword ptr [esp+0Ch]
   569         mov         eax,dword ptr [esp+8]
   570 L3:
   571         shr         ecx,1
   572         rcr         ebx,1
   573         shr         edx,1
   574         rcr         eax,1
   575         or          ecx,ecx
   576         jne         L3
   577         div         ebx
   578         mov         esi,eax
   579         mul         dword ptr [esp+14h]
   580         mov         ecx,eax
   581         mov         eax,dword ptr [esp+10h]
   582         mul         esi
   583         add         edx,ecx
   584         jb          L4
   585         cmp         edx,dword ptr [esp+0Ch]
   586         ja          L4
   587         jb          L5
   588         cmp         eax,dword ptr [esp+8]
   589         jbe         L5
   590 L4:
   591         dec         esi
   592         sub         eax,dword ptr [esp+10h]
   593         sbb         edx,dword ptr [esp+14h]
   594 L5:
   595         xor         ebx,ebx
   596 L2:
   597         sub         eax,dword ptr [esp+8]
   598         sbb         edx,dword ptr [esp+0Ch]
   599         neg         edx
   600         neg         eax
   601         sbb         edx,0
   602         mov         ecx,edx
   603         mov         edx,ebx
   604         mov         ebx,ecx
   605         mov         ecx,eax
   606         mov         eax,esi
   607         pop         esi
   608         ret         10h
   609     }
   610     /* *INDENT-ON* */
   611 }
   612 
   613 void
   614 __declspec(naked)
   615 _allshl()
   616 {
   617     /* *INDENT-OFF* */
   618     __asm {
   619         cmp         cl,40h
   620         jae         RETZERO
   621         cmp         cl,20h
   622         jae         MORE32
   623         shld        edx,eax,cl
   624         shl         eax,cl
   625         ret
   626 MORE32:
   627         mov         edx,eax
   628         xor         eax,eax
   629         and         cl,1Fh
   630         shl         edx,cl
   631         ret
   632 RETZERO:
   633         xor         eax,eax
   634         xor         edx,edx
   635         ret
   636     }
   637     /* *INDENT-ON* */
   638 }
   639 
   640 void
   641 __declspec(naked)
   642 _allshr()
   643 {
   644     /* *INDENT-OFF* */
   645     __asm {
   646         cmp         cl,40h
   647         jae         RETZERO
   648         cmp         cl,20h
   649         jae         MORE32
   650         shrd        eax,edx,cl
   651         sar         edx,cl
   652         ret
   653 MORE32:
   654         mov         eax,edx
   655         xor         edx,edx
   656         and         cl,1Fh
   657         sar         eax,cl
   658         ret
   659 RETZERO:
   660         xor         eax,eax
   661         xor         edx,edx
   662         ret
   663     }
   664     /* *INDENT-ON* */
   665 }
   666 
   667 void
   668 __declspec(naked)
   669 _aullshr()
   670 {
   671     /* *INDENT-OFF* */
   672     __asm {
   673         cmp         cl,40h
   674         jae         RETZERO
   675         cmp         cl,20h
   676         jae         MORE32
   677         shrd        eax,edx,cl
   678         shr         edx,cl
   679         ret
   680 MORE32:
   681         mov         eax,edx
   682         xor         edx,edx
   683         and         cl,1Fh
   684         shr         eax,cl
   685         ret
   686 RETZERO:
   687         xor         eax,eax
   688         xor         edx,edx
   689         ret
   690     }
   691     /* *INDENT-ON* */
   692 }
   693 
   694 #endif /* MSC_VER */
   695 
   696 #endif /* !HAVE_LIBC */
   697 
   698 /* vi: set ts=4 sw=4 expandtab: */