src/stdlib/SDL_stdlib.c
author Sam Lantinga
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1433 bb6839704ed6
child 1663 11775724e3fe
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
     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     __asm {
    46         push ebp mov ebp, esp sub esp, 20 h and esp, 0F FFFFFF0h fld st (0)
    47         fst dword ptr[esp + 18 h]
    48         fistp qword ptr[esp + 10 h]
    49         fild qword ptr[esp + 10 h]
    50         mov edx, dword ptr[esp + 18 h]
    51         mov eax, dword ptr[esp + 10 h]
    52         test eax, eax
    53             je integer_QnaN_or_zero
    54             arg_is_not_integer_QnaN:fsubp st (1), st
    55             test edx, edx jns positive fstp dword ptr[esp]
    56         mov ecx, dword ptr[esp]
    57         xor ecx, 80000000 h
    58             add ecx, 7F FFFFFFh adc eax, 0 mov edx, dword ptr[esp + 14 h]
    59         adc edx, 0 jmp localexit positive:fstp dword ptr[esp]
    60         mov ecx, dword ptr[esp]
    61         add ecx, 7F FFFFFFh sbb eax, 0 mov edx, dword ptr[esp + 14 h]
    62         sbb edx, 0
    63             jmp localexit integer_QnaN_or_zero:mov edx, dword ptr[esp + 14 h]
    64         test edx, 7F FFFFFFh
    65             jne arg_is_not_integer_QnaN fstp dword ptr[esp + 18 h]
    66     fstp dword ptr[esp + 18 h] localexit:leave ret}
    67 }
    68 void
    69 __declspec (naked)
    70 _ftol2_sse ()
    71 {
    72     _ftol ();
    73 }
    74 
    75 /* 64-bit math operators for 32-bit systems */
    76 void
    77 __declspec (naked)
    78 _allmul ()
    79 {
    80     __asm {
    81         push ebp
    82             mov ebp, esp
    83             push edi
    84             push esi push ebx sub esp, 0 Ch mov eax, dword ptr[ebp + 10 h]
    85         mov edi, dword ptr[ebp + 8]
    86         mov ebx, eax mov esi, eax sar esi, 1F h mov eax, dword ptr[ebp + 8]
    87         mul ebx
    88             imul edi, esi
    89             mov ecx, edx
    90             mov dword ptr[ebp - 18 h], eax mov edx, dword ptr[ebp + 0 Ch]
    91         add ecx, edi imul ebx, edx mov eax, dword ptr[ebp - 18 h]
    92         lea ebx,[ebx + ecx]
    93         mov dword ptr[ebp - 14 h], ebx mov edx, dword ptr[ebp - 14 h]
    94     add esp, 0 Ch pop ebx pop esi pop edi pop ebp ret}
    95 }
    96 void
    97 __declspec (naked)
    98 _alldiv ()
    99 {
   100     __asm {
   101         push edi push esi push ebx xor edi, edi mov eax, dword ptr[esp + 14 h]
   102         or eax, eax jge L1 inc edi mov edx, dword ptr[esp + 10 h]
   103         neg eax
   104             neg edx
   105             sbb eax, 0
   106             mov dword ptr[esp + 14 h], eax
   107             mov dword ptr[esp + 10 h], edx L1:mov eax, dword ptr[esp + 1 Ch]
   108         or eax, eax jge L2 inc edi mov edx, dword ptr[esp + 18 h]
   109         neg eax
   110             neg edx
   111             sbb eax, 0
   112             mov dword ptr[esp + 1 Ch], eax
   113             mov dword ptr[esp + 18 h], edx
   114             L2:or eax, eax jne L3 mov ecx, dword ptr[esp + 18 h]
   115         mov eax, dword ptr[esp + 14 h]
   116         xor edx, edx div ecx mov ebx, eax mov eax, dword ptr[esp + 10 h]
   117         div ecx
   118             mov edx, ebx jmp L4 L3:mov ebx, eax mov ecx, dword ptr[esp + 18 h]
   119         mov edx, dword ptr[esp + 14 h]
   120         mov eax, dword ptr[esp + 10 h]
   121 
   122 
   123 
   124 
   125 
   126 
   127          
   128             L5:shr ebx, 1
   129             rcr ecx, 1
   130             shr edx, 1
   131             rcr eax, 1
   132             or ebx, ebx jne L5 div ecx mov esi, eax mul dword ptr[esp + 1 Ch]
   133         mov ecx, eax mov eax, dword ptr[esp + 18 h]
   134         mul esi add edx, ecx jb L6 cmp edx, dword ptr[esp + 14 h]
   135         ja L6 jb L7 cmp eax, dword ptr[esp + 10 h]
   136     jbe L7
   137             L6:dec esi
   138             L7:xor edx, edx
   139             mov eax, esi
   140             L4:dec edi
   141             jne L8
   142             neg edx neg eax sbb edx, 0 L8:pop ebx pop esi pop edi ret 10 h}
   143 }
   144 void
   145 __declspec (naked)
   146 _aulldiv ()
   147 {
   148     __asm {
   149         push ebx push esi mov eax, dword ptr[esp + 18 h]
   150         or eax, eax jne L1 mov ecx, dword ptr[esp + 14 h]
   151         mov eax, dword ptr[esp + 10 h]
   152         xor edx, edx div ecx mov ebx, eax mov eax, dword ptr[esp + 0 Ch]
   153         div ecx
   154             mov edx, ebx jmp L2 L1:mov ecx, eax mov ebx, dword ptr[esp + 14 h]
   155         mov edx, dword ptr[esp + 10 h]
   156         mov eax, dword ptr[esp + 0 Ch]
   157 
   158 
   159 
   160 
   161 
   162 
   163          
   164             L3:shr ecx, 1
   165             rcr ebx, 1
   166             shr edx, 1
   167             rcr eax, 1
   168             or ecx, ecx jne L3 div ebx mov esi, eax mul dword ptr[esp + 18 h]
   169         mov ecx, eax mov eax, dword ptr[esp + 14 h]
   170         mul esi add edx, ecx jb L4 cmp edx, dword ptr[esp + 10 h]
   171         ja L4 jb L5 cmp eax, dword ptr[esp + 0 Ch]
   172     jbe L5
   173             L4:dec esi
   174             L5:xor edx, edx mov eax, esi L2:pop esi pop ebx ret 10 h}
   175 }
   176 void
   177 __declspec (naked)
   178 _allrem ()
   179 {
   180     __asm {
   181         push ebx push edi xor edi, edi mov eax, dword ptr[esp + 10 h]
   182         or eax, eax jge L1 inc edi mov edx, dword ptr[esp + 0 Ch]
   183         neg eax
   184             neg edx
   185             sbb eax, 0
   186             mov dword ptr[esp + 10 h], eax
   187             mov dword ptr[esp + 0 Ch], edx L1:mov eax, dword ptr[esp + 18 h]
   188         or eax, eax jge L2 mov edx, dword ptr[esp + 14 h]
   189         neg eax
   190             neg edx
   191             sbb eax, 0
   192             mov dword ptr[esp + 18 h], eax
   193             mov dword ptr[esp + 14 h], edx
   194             L2:or eax, eax jne L3 mov ecx, dword ptr[esp + 14 h]
   195         mov eax, dword ptr[esp + 10 h]
   196         xor edx, edx div ecx mov eax, dword ptr[esp + 0 Ch]
   197         div ecx
   198             mov eax, edx
   199             xor edx, edx
   200             dec edi
   201             jns L4 jmp L8 L3:mov ebx, eax mov ecx, dword ptr[esp + 14 h]
   202         mov edx, dword ptr[esp + 10 h]
   203         mov eax, dword ptr[esp + 0 Ch]
   204 
   205 
   206 
   207 
   208 
   209 
   210          
   211             L5:shr ebx, 1
   212             rcr ecx, 1
   213             shr edx, 1
   214             rcr eax, 1
   215             or ebx, ebx jne L5 div ecx mov ecx, eax mul dword ptr[esp + 18 h]
   216         xchg eax, ecx mul dword ptr[esp + 14 h]
   217         add edx, ecx jb L6 cmp edx, dword ptr[esp + 10 h]
   218         ja L6 jb L7 cmp eax, dword ptr[esp + 0 Ch]
   219         jbe L7 L6:sub eax, dword ptr[esp + 14 h]
   220         sbb edx, dword ptr[esp + 18 h]
   221           L7:sub eax, dword ptr[esp + 0 Ch]
   222         sbb edx, dword ptr[esp + 10 h]
   223     dec edi
   224             jns L8 L4:neg edx neg eax sbb edx, 0 L8:pop edi pop ebx ret 10 h}
   225 }
   226 void
   227 __declspec (naked)
   228 _aullrem ()
   229 {
   230     __asm {
   231         push ebx mov eax, dword ptr[esp + 14 h]
   232         or eax, eax jne L1 mov ecx, dword ptr[esp + 10 h]
   233         mov eax, dword ptr[esp + 0 Ch]
   234         xor edx, edx div ecx mov eax, dword ptr[esp + 8]
   235         div ecx
   236             mov eax, edx
   237             xor edx, edx jmp L2 L1:mov ecx, eax mov ebx, dword ptr[esp + 10 h]
   238         mov edx, dword ptr[esp + 0 Ch]
   239         mov eax, dword ptr[esp + 8]
   240 
   241 
   242 
   243 
   244 
   245 
   246          
   247             L3:shr ecx, 1
   248             rcr ebx, 1
   249             shr edx, 1
   250             rcr eax, 1
   251             or ecx, ecx jne L3 div ebx mov ecx, eax mul dword ptr[esp + 14 h]
   252         xchg eax, ecx mul dword ptr[esp + 10 h]
   253         add edx, ecx jb L4 cmp edx, dword ptr[esp + 0 Ch]
   254         ja L4 jb L5 cmp eax, dword ptr[esp + 8]
   255         jbe L5 L4:sub eax, dword ptr[esp + 10 h]
   256         sbb edx, dword ptr[esp + 14 h]
   257           L5:sub eax, dword ptr[esp + 8]
   258         sbb edx, dword ptr[esp + 0 Ch]
   259     neg edx neg eax sbb edx, 0 L2:pop ebx ret 10 h}
   260 }
   261 void
   262 __declspec (naked)
   263 _alldvrm ()
   264 {
   265     __asm {
   266         push edi
   267             push esi
   268             push ebp xor edi, edi xor ebp, ebp mov eax, dword ptr[esp + 14 h]
   269         or eax, eax jge L1 inc edi inc ebp mov edx, dword ptr[esp + 10 h]
   270         neg eax
   271             neg edx
   272             sbb eax, 0
   273             mov dword ptr[esp + 14 h], eax
   274             mov dword ptr[esp + 10 h], edx L1:mov eax, dword ptr[esp + 1 Ch]
   275         or eax, eax jge L2 inc edi mov edx, dword ptr[esp + 18 h]
   276         neg eax
   277             neg edx
   278             sbb eax, 0
   279             mov dword ptr[esp + 1 Ch], eax
   280             mov dword ptr[esp + 18 h], edx
   281             L2:or eax, eax jne L3 mov ecx, dword ptr[esp + 18 h]
   282         mov eax, dword ptr[esp + 14 h]
   283         xor edx, edx div ecx mov ebx, eax mov eax, dword ptr[esp + 10 h]
   284         div ecx mov esi, eax mov eax, ebx mul dword ptr[esp + 18 h]
   285         mov ecx, eax mov eax, esi mul dword ptr[esp + 18 h]
   286         add edx, ecx jmp L4 L3:mov ebx, eax mov ecx, dword ptr[esp + 18 h]
   287         mov edx, dword ptr[esp + 14 h]
   288         mov eax, dword ptr[esp + 10 h]
   289 
   290 
   291 
   292 
   293 
   294 
   295          
   296             L5:shr ebx, 1
   297             rcr ecx, 1
   298             shr edx, 1
   299             rcr eax, 1
   300             or ebx, ebx jne L5 div ecx mov esi, eax mul dword ptr[esp + 1 Ch]
   301         mov ecx, eax mov eax, dword ptr[esp + 18 h]
   302         mul esi add edx, ecx jb L6 cmp edx, dword ptr[esp + 14 h]
   303         ja L6 jb L7 cmp eax, dword ptr[esp + 10 h]
   304         jbe L7 L6:dec esi sub eax, dword ptr[esp + 18 h]
   305         sbb edx, dword ptr[esp + 1 Ch]
   306           L7:xor ebx, ebx L4:sub eax, dword ptr[esp + 10 h]
   307         sbb edx, dword ptr[esp + 14 h]
   308     dec ebp
   309             jns L9
   310             neg edx
   311             neg eax
   312             sbb edx, 0
   313             L9:mov ecx, edx
   314             mov edx, ebx
   315             mov ebx, ecx
   316             mov ecx, eax
   317             mov eax, esi
   318             dec edi
   319             jne L8
   320             neg edx neg eax sbb edx, 0 L8:pop ebp pop esi pop edi ret 10 h}
   321 }
   322 void
   323 __declspec (naked)
   324 _aulldvrm ()
   325 {
   326     __asm {
   327         push esi mov eax, dword ptr[esp + 14 h]
   328         or eax, eax jne L1 mov ecx, dword ptr[esp + 10 h]
   329         mov eax, dword ptr[esp + 0 Ch]
   330         xor edx, edx div ecx mov ebx, eax mov eax, dword ptr[esp + 8]
   331         div ecx mov esi, eax mov eax, ebx mul dword ptr[esp + 10 h]
   332         mov ecx, eax mov eax, esi mul dword ptr[esp + 10 h]
   333         add edx, ecx jmp L2 L1:mov ecx, eax mov ebx, dword ptr[esp + 10 h]
   334         mov edx, dword ptr[esp + 0 Ch]
   335         mov eax, dword ptr[esp + 8]
   336 
   337 
   338 
   339 
   340 
   341 
   342          
   343             L3:shr ecx, 1
   344             rcr ebx, 1
   345             shr edx, 1
   346             rcr eax, 1
   347             or ecx, ecx jne L3 div ebx mov esi, eax mul dword ptr[esp + 14 h]
   348         mov ecx, eax mov eax, dword ptr[esp + 10 h]
   349         mul esi add edx, ecx jb L4 cmp edx, dword ptr[esp + 0 Ch]
   350         ja L4 jb L5 cmp eax, dword ptr[esp + 8]
   351         jbe L5 L4:dec esi sub eax, dword ptr[esp + 10 h]
   352         sbb edx, dword ptr[esp + 14 h]
   353           L5:xor ebx, ebx L2:sub eax, dword ptr[esp + 8]
   354         sbb edx, dword ptr[esp + 0 Ch]
   355     neg edx
   356             neg eax
   357             sbb edx, 0
   358             mov ecx, edx
   359             mov edx, ebx
   360             mov ebx, ecx mov ecx, eax mov eax, esi pop esi ret 10 h}
   361 }
   362 void
   363 __declspec (naked)
   364 _allshl ()
   365 {
   366     __asm {
   367     cmp cl, 40 h
   368             jae RETZERO
   369             cmp cl, 20 h
   370             jae MORE32
   371             shld edx, eax, cl
   372             shl eax, cl
   373             ret
   374             MORE32:mov edx, eax
   375             xor eax, eax
   376             and cl, 1F h
   377             shl edx, cl ret RETZERO:xor eax, eax xor edx, edx ret}
   378 }
   379 void
   380 __declspec (naked)
   381 _aullshr ()
   382 {
   383     __asm {
   384     cmp cl, 40 h
   385             jae RETZERO
   386             cmp cl, 20 h
   387             jae MORE32
   388             shrd eax, edx, cl
   389             shr edx, cl
   390             ret
   391             MORE32:mov eax, edx
   392             xor edx, edx
   393             and cl, 1F h
   394             shr eax, cl ret RETZERO:xor eax, eax xor edx, edx ret}
   395 }
   396 
   397 #endif                          /* MSC_VER */
   398 
   399 #endif                          /* !HAVE_LIBC */
   400 /* vi: set ts=4 sw=4 expandtab: */