Path: blob/master/thirdparty/sdl/stdlib/SDL_mslibc.c
21337 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"2122// This file contains SDL replacements for functions in the C library2324#if !defined(HAVE_LIBC) && !defined(SDL_STATIC_LIB)2526// These are some C runtime intrinsics that need to be defined2728#ifdef _MSC_VER2930#ifndef __FLTUSED__31#define __FLTUSED__32__declspec(selectany) int _fltused = 1;33#endif3435#ifdef _M_IX863637// Float to long38void __declspec(naked) _ftol()39{40/* *INDENT-OFF* */41__asm {42push ebp43mov ebp,esp44sub esp,20h45and esp,0FFFFFFF0h46fld st(0)47fst dword ptr [esp+18h]48fistp qword ptr [esp+10h]49fild qword ptr [esp+10h]50mov edx,dword ptr [esp+18h]51mov eax,dword ptr [esp+10h]52test eax,eax53je integer_QnaN_or_zero54arg_is_not_integer_QnaN:55fsubp st(1),st56test edx,edx57jns positive58fstp dword ptr [esp]59mov ecx,dword ptr [esp]60xor ecx,80000000h61add ecx,7FFFFFFFh62adc eax,063mov edx,dword ptr [esp+14h]64adc edx,065jmp localexit66positive:67fstp dword ptr [esp]68mov ecx,dword ptr [esp]69add ecx,7FFFFFFFh70sbb eax,071mov edx,dword ptr [esp+14h]72sbb edx,073jmp localexit74integer_QnaN_or_zero:75mov edx,dword ptr [esp+14h]76test edx,7FFFFFFFh77jne arg_is_not_integer_QnaN78fstp dword ptr [esp+18h]79fstp dword ptr [esp+18h]80localexit:81leave82ret83}84/* *INDENT-ON* */85}8687void _ftol2_sse()88{89_ftol();90}9192void _ftol2()93{94_ftol();95}9697void __declspec(naked) _ftoul2_legacy()98{99static const Uint64 LLONG_MAX_PLUS_ONE = 0x43e0000000000000ULL;100/* *INDENT-OFF* */101__asm {102fld qword ptr [LLONG_MAX_PLUS_ONE]103fcom104fnstsw ax105test ah, 41h106jnp greater_than_int64107108fstp st(0)109jmp _ftol110111greater_than_int64:112fsub st(1), st(0)113fcomp114fnstsw ax115test ah, 41h116jnz greater_than_uint64117118call _ftol119add edx, 80000000h120ret121122greater_than_uint64:123xor eax, eax124mov edx, 80000000h125ret126}127/* *INDENT-ON* */128}129130// 64-bit math operators for 32-bit systems131void __declspec(naked) _allmul()132{133/* *INDENT-OFF* */134__asm {135mov eax, dword ptr[esp+8]136mov ecx, dword ptr[esp+10h]137or ecx, eax138mov ecx, dword ptr[esp+0Ch]139jne hard140mov eax, dword ptr[esp+4]141mul ecx142ret 10h143hard:144push ebx145mul ecx146mov ebx, eax147mov eax, dword ptr[esp+8]148mul dword ptr[esp+14h]149add ebx, eax150mov eax, dword ptr[esp+8]151mul ecx152add edx, ebx153pop ebx154ret 10h155}156/* *INDENT-ON* */157}158159void __declspec(naked) _alldiv()160{161/* *INDENT-OFF* */162__asm {163push edi164push esi165push ebx166xor edi,edi167mov eax,dword ptr [esp+14h]168or eax,eax169jge L1170inc edi171mov edx,dword ptr [esp+10h]172neg eax173neg edx174sbb eax,0175mov dword ptr [esp+14h],eax176mov dword ptr [esp+10h],edx177L1:178mov eax,dword ptr [esp+1Ch]179or eax,eax180jge L2181inc edi182mov edx,dword ptr [esp+18h]183neg eax184neg edx185sbb eax,0186mov dword ptr [esp+1Ch],eax187mov dword ptr [esp+18h],edx188L2:189or eax,eax190jne L3191mov ecx,dword ptr [esp+18h]192mov eax,dword ptr [esp+14h]193xor edx,edx194div ecx195mov ebx,eax196mov eax,dword ptr [esp+10h]197div ecx198mov edx,ebx199jmp L4200L3:201mov ebx,eax202mov ecx,dword ptr [esp+18h]203mov edx,dword ptr [esp+14h]204mov eax,dword ptr [esp+10h]205L5:206shr ebx,1207rcr ecx,1208shr edx,1209rcr eax,1210or ebx,ebx211jne L5212div ecx213mov esi,eax214mul dword ptr [esp+1Ch]215mov ecx,eax216mov eax,dword ptr [esp+18h]217mul esi218add edx,ecx219jb L6220cmp edx,dword ptr [esp+14h]221ja L6222jb L7223cmp eax,dword ptr [esp+10h]224jbe L7225L6:226dec esi227L7:228xor edx,edx229mov eax,esi230L4:231dec edi232jne L8233neg edx234neg eax235sbb edx,0236L8:237pop ebx238pop esi239pop edi240ret 10h241}242/* *INDENT-ON* */243}244245void __declspec(naked) _aulldiv()246{247/* *INDENT-OFF* */248__asm {249push ebx250push esi251mov eax,dword ptr [esp+18h]252or eax,eax253jne L1254mov ecx,dword ptr [esp+14h]255mov eax,dword ptr [esp+10h]256xor edx,edx257div ecx258mov ebx,eax259mov eax,dword ptr [esp+0Ch]260div ecx261mov edx,ebx262jmp L2263L1:264mov ecx,eax265mov ebx,dword ptr [esp+14h]266mov edx,dword ptr [esp+10h]267mov eax,dword ptr [esp+0Ch]268L3:269shr ecx,1270rcr ebx,1271shr edx,1272rcr eax,1273or ecx,ecx274jne L3275div ebx276mov esi,eax277mul dword ptr [esp+18h]278mov ecx,eax279mov eax,dword ptr [esp+14h]280mul esi281add edx,ecx282jb L4283cmp edx,dword ptr [esp+10h]284ja L4285jb L5286cmp eax,dword ptr [esp+0Ch]287jbe L5288L4:289dec esi290L5:291xor edx,edx292mov eax,esi293L2:294pop esi295pop ebx296ret 10h297}298/* *INDENT-ON* */299}300301void __declspec(naked) _allrem()302{303/* *INDENT-OFF* */304__asm {305push ebx306push edi307xor edi,edi308mov eax,dword ptr [esp+10h]309or eax,eax310jge L1311inc edi312mov edx,dword ptr [esp+0Ch]313neg eax314neg edx315sbb eax,0316mov dword ptr [esp+10h],eax317mov dword ptr [esp+0Ch],edx318L1:319mov eax,dword ptr [esp+18h]320or eax,eax321jge L2322mov edx,dword ptr [esp+14h]323neg eax324neg edx325sbb eax,0326mov dword ptr [esp+18h],eax327mov dword ptr [esp+14h],edx328L2:329or eax,eax330jne L3331mov ecx,dword ptr [esp+14h]332mov eax,dword ptr [esp+10h]333xor edx,edx334div ecx335mov eax,dword ptr [esp+0Ch]336div ecx337mov eax,edx338xor edx,edx339dec edi340jns L4341jmp L8342L3:343mov ebx,eax344mov ecx,dword ptr [esp+14h]345mov edx,dword ptr [esp+10h]346mov eax,dword ptr [esp+0Ch]347L5:348shr ebx,1349rcr ecx,1350shr edx,1351rcr eax,1352or ebx,ebx353jne L5354div ecx355mov ecx,eax356mul dword ptr [esp+18h]357xchg eax,ecx358mul dword ptr [esp+14h]359add edx,ecx360jb L6361cmp edx,dword ptr [esp+10h]362ja L6363jb L7364cmp eax,dword ptr [esp+0Ch]365jbe L7366L6:367sub eax,dword ptr [esp+14h]368sbb edx,dword ptr [esp+18h]369L7:370sub eax,dword ptr [esp+0Ch]371sbb edx,dword ptr [esp+10h]372dec edi373jns L8374L4:375neg edx376neg eax377sbb edx,0378L8:379pop edi380pop ebx381ret 10h382}383/* *INDENT-ON* */384}385386void __declspec(naked) _aullrem()387{388/* *INDENT-OFF* */389__asm {390push ebx391mov eax,dword ptr [esp+14h]392or eax,eax393jne L1394mov ecx,dword ptr [esp+10h]395mov eax,dword ptr [esp+0Ch]396xor edx,edx397div ecx398mov eax,dword ptr [esp+8]399div ecx400mov eax,edx401xor edx,edx402jmp L2403L1:404mov ecx,eax405mov ebx,dword ptr [esp+10h]406mov edx,dword ptr [esp+0Ch]407mov eax,dword ptr [esp+8]408L3:409shr ecx,1410rcr ebx,1411shr edx,1412rcr eax,1413or ecx,ecx414jne L3415div ebx416mov ecx,eax417mul dword ptr [esp+14h]418xchg eax,ecx419mul dword ptr [esp+10h]420add edx,ecx421jb L4422cmp edx,dword ptr [esp+0Ch]423ja L4424jb L5425cmp eax,dword ptr [esp+8]426jbe L5427L4:428sub eax,dword ptr [esp+10h]429sbb edx,dword ptr [esp+14h]430L5:431sub eax,dword ptr [esp+8]432sbb edx,dword ptr [esp+0Ch]433neg edx434neg eax435sbb edx,0436L2:437pop ebx438ret 10h439}440/* *INDENT-ON* */441}442443void __declspec(naked) _alldvrm()444{445/* *INDENT-OFF* */446__asm {447push edi448push esi449push ebp450xor edi,edi451xor ebp,ebp452mov eax,dword ptr [esp+14h]453or eax,eax454jge L1455inc edi456inc ebp457mov edx,dword ptr [esp+10h]458neg eax459neg edx460sbb eax,0461mov dword ptr [esp+14h],eax462mov dword ptr [esp+10h],edx463L1:464mov eax,dword ptr [esp+1Ch]465or eax,eax466jge L2467inc edi468mov edx,dword ptr [esp+18h]469neg eax470neg edx471sbb eax,0472mov dword ptr [esp+1Ch],eax473mov dword ptr [esp+18h],edx474L2:475or eax,eax476jne L3477mov ecx,dword ptr [esp+18h]478mov eax,dword ptr [esp+14h]479xor edx,edx480div ecx481mov ebx,eax482mov eax,dword ptr [esp+10h]483div ecx484mov esi,eax485mov eax,ebx486mul dword ptr [esp+18h]487mov ecx,eax488mov eax,esi489mul dword ptr [esp+18h]490add edx,ecx491jmp L4492L3:493mov ebx,eax494mov ecx,dword ptr [esp+18h]495mov edx,dword ptr [esp+14h]496mov eax,dword ptr [esp+10h]497L5:498shr ebx,1499rcr ecx,1500shr edx,1501rcr eax,1502or ebx,ebx503jne L5504div ecx505mov esi,eax506mul dword ptr [esp+1Ch]507mov ecx,eax508mov eax,dword ptr [esp+18h]509mul esi510add edx,ecx511jb L6512cmp edx,dword ptr [esp+14h]513ja L6514jb L7515cmp eax,dword ptr [esp+10h]516jbe L7517L6:518dec esi519sub eax,dword ptr [esp+18h]520sbb edx,dword ptr [esp+1Ch]521L7:522xor ebx,ebx523L4:524sub eax,dword ptr [esp+10h]525sbb edx,dword ptr [esp+14h]526dec ebp527jns L9528neg edx529neg eax530sbb edx,0531L9:532mov ecx,edx533mov edx,ebx534mov ebx,ecx535mov ecx,eax536mov eax,esi537dec edi538jne L8539neg edx540neg eax541sbb edx,0542L8:543pop ebp544pop esi545pop edi546ret 10h547}548/* *INDENT-ON* */549}550551void __declspec(naked) _aulldvrm()552{553/* *INDENT-OFF* */554__asm {555push esi556mov eax,dword ptr [esp+14h]557or eax,eax558jne L1559mov ecx,dword ptr [esp+10h]560mov eax,dword ptr [esp+0Ch]561xor edx,edx562div ecx563mov ebx,eax564mov eax,dword ptr [esp+8]565div ecx566mov esi,eax567mov eax,ebx568mul dword ptr [esp+10h]569mov ecx,eax570mov eax,esi571mul dword ptr [esp+10h]572add edx,ecx573jmp L2574L1:575mov ecx,eax576mov ebx,dword ptr [esp+10h]577mov edx,dword ptr [esp+0Ch]578mov eax,dword ptr [esp+8]579L3:580shr ecx,1581rcr ebx,1582shr edx,1583rcr eax,1584or ecx,ecx585jne L3586div ebx587mov esi,eax588mul dword ptr [esp+14h]589mov ecx,eax590mov eax,dword ptr [esp+10h]591mul esi592add edx,ecx593jb L4594cmp edx,dword ptr [esp+0Ch]595ja L4596jb L5597cmp eax,dword ptr [esp+8]598jbe L5599L4:600dec esi601sub eax,dword ptr [esp+10h]602sbb edx,dword ptr [esp+14h]603L5:604xor ebx,ebx605L2:606sub eax,dword ptr [esp+8]607sbb edx,dword ptr [esp+0Ch]608neg edx609neg eax610sbb edx,0611mov ecx,edx612mov edx,ebx613mov ebx,ecx614mov ecx,eax615mov eax,esi616pop esi617ret 10h618}619/* *INDENT-ON* */620}621622void __declspec(naked) _allshl()623{624/* *INDENT-OFF* */625__asm {626cmp cl,40h627jae RETZERO628cmp cl,20h629jae MORE32630shld edx,eax,cl631shl eax,cl632ret633MORE32:634mov edx,eax635xor eax,eax636and cl,1Fh637shl edx,cl638ret639RETZERO:640xor eax,eax641xor edx,edx642ret643}644/* *INDENT-ON* */645}646647void __declspec(naked) _allshr()648{649/* *INDENT-OFF* */650__asm {651cmp cl,3Fh652jae RETSIGN653cmp cl,20h654jae MORE32655shrd eax,edx,cl656sar edx,cl657ret658MORE32:659mov eax,edx660sar edx,1Fh661and cl,1Fh662sar eax,cl663ret664RETSIGN:665sar edx,1Fh666mov eax,edx667ret668}669/* *INDENT-ON* */670}671672void __declspec(naked) _aullshr()673{674/* *INDENT-OFF* */675__asm {676cmp cl,40h677jae RETZERO678cmp cl,20h679jae MORE32680shrd eax,edx,cl681shr edx,cl682ret683MORE32:684mov eax,edx685xor edx,edx686and cl,1Fh687shr eax,cl688ret689RETZERO:690xor eax,eax691xor edx,edx692ret693}694/* *INDENT-ON* */695}696697void __declspec(naked) _chkstk(void)698{699__asm {700push ecx701mov ecx,esp ; lea ecx,dword ptr [esp]+4702add ecx,4703sub ecx,eax704sbb eax,eax705not eax706and ecx,eax707mov eax,esp708and eax,0xfffff000709L1:710cmp ecx,eax711jb short L2712mov eax,ecx713pop ecx714xchg esp,eax715mov eax,dword ptr [eax]716mov dword ptr [esp],eax717ret718L2:719sub eax,0x1000720test dword ptr [eax],eax721jmp short L1722}723}724725void __declspec(naked) _alloca_probe_8(void)726{727/* *INDENT-OFF* */728__asm {729push ecx730mov ecx,esp ; lea ecx,dword ptr [esp]+8731add ecx,8732sub ecx,eax733and ecx,0x7734add eax,ecx735sbb ecx,ecx736or eax,ecx737pop ecx738jmp _chkstk739}740/* *INDENT-ON* */741}742743void __declspec(naked) _alloca_probe_16(void)744{745/* *INDENT-OFF* */746__asm {747push ecx748mov ecx,esp ; lea ecx,dword ptr [esp]+8749add ecx,8750sub ecx,eax751and ecx,0xf752add eax,ecx753sbb ecx,ecx754or eax,ecx755pop ecx756jmp _chkstk757}758/* *INDENT-ON* */759}760761#endif // _M_IX86762763#endif // MSC_VER764765#ifdef __ICL766/* The classic Intel compiler generates calls to _intel_fast_memcpy767* and _intel_fast_memset when building an optimized SDL library */768void *_intel_fast_memcpy(void *dst, const void *src, size_t len)769{770return SDL_memcpy(dst, src, len);771}772void *_intel_fast_memset(void *dst, int c, size_t len)773{774return SDL_memset(dst, c, len);775}776#endif777778#endif // !HAVE_LIBC && !SDL_STATIC_LIB779780781