Path: blob/master/thirdparty/sdl/stdlib/SDL_mslibc.c
10279 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}9697// 64-bit math operators for 32-bit systems98void __declspec(naked) _allmul()99{100/* *INDENT-OFF* */101__asm {102mov eax, dword ptr[esp+8]103mov ecx, dword ptr[esp+10h]104or ecx, eax105mov ecx, dword ptr[esp+0Ch]106jne hard107mov eax, dword ptr[esp+4]108mul ecx109ret 10h110hard:111push ebx112mul ecx113mov ebx, eax114mov eax, dword ptr[esp+8]115mul dword ptr[esp+14h]116add ebx, eax117mov eax, dword ptr[esp+8]118mul ecx119add edx, ebx120pop ebx121ret 10h122}123/* *INDENT-ON* */124}125126void __declspec(naked) _alldiv()127{128/* *INDENT-OFF* */129__asm {130push edi131push esi132push ebx133xor edi,edi134mov eax,dword ptr [esp+14h]135or eax,eax136jge L1137inc edi138mov edx,dword ptr [esp+10h]139neg eax140neg edx141sbb eax,0142mov dword ptr [esp+14h],eax143mov dword ptr [esp+10h],edx144L1:145mov eax,dword ptr [esp+1Ch]146or eax,eax147jge L2148inc edi149mov edx,dword ptr [esp+18h]150neg eax151neg edx152sbb eax,0153mov dword ptr [esp+1Ch],eax154mov dword ptr [esp+18h],edx155L2:156or eax,eax157jne L3158mov ecx,dword ptr [esp+18h]159mov eax,dword ptr [esp+14h]160xor edx,edx161div ecx162mov ebx,eax163mov eax,dword ptr [esp+10h]164div ecx165mov edx,ebx166jmp L4167L3:168mov ebx,eax169mov ecx,dword ptr [esp+18h]170mov edx,dword ptr [esp+14h]171mov eax,dword ptr [esp+10h]172L5:173shr ebx,1174rcr ecx,1175shr edx,1176rcr eax,1177or ebx,ebx178jne L5179div ecx180mov esi,eax181mul dword ptr [esp+1Ch]182mov ecx,eax183mov eax,dword ptr [esp+18h]184mul esi185add edx,ecx186jb L6187cmp edx,dword ptr [esp+14h]188ja L6189jb L7190cmp eax,dword ptr [esp+10h]191jbe L7192L6:193dec esi194L7:195xor edx,edx196mov eax,esi197L4:198dec edi199jne L8200neg edx201neg eax202sbb edx,0203L8:204pop ebx205pop esi206pop edi207ret 10h208}209/* *INDENT-ON* */210}211212void __declspec(naked) _aulldiv()213{214/* *INDENT-OFF* */215__asm {216push ebx217push esi218mov eax,dword ptr [esp+18h]219or eax,eax220jne L1221mov ecx,dword ptr [esp+14h]222mov eax,dword ptr [esp+10h]223xor edx,edx224div ecx225mov ebx,eax226mov eax,dword ptr [esp+0Ch]227div ecx228mov edx,ebx229jmp L2230L1:231mov ecx,eax232mov ebx,dword ptr [esp+14h]233mov edx,dword ptr [esp+10h]234mov eax,dword ptr [esp+0Ch]235L3:236shr ecx,1237rcr ebx,1238shr edx,1239rcr eax,1240or ecx,ecx241jne L3242div ebx243mov esi,eax244mul dword ptr [esp+18h]245mov ecx,eax246mov eax,dword ptr [esp+14h]247mul esi248add edx,ecx249jb L4250cmp edx,dword ptr [esp+10h]251ja L4252jb L5253cmp eax,dword ptr [esp+0Ch]254jbe L5255L4:256dec esi257L5:258xor edx,edx259mov eax,esi260L2:261pop esi262pop ebx263ret 10h264}265/* *INDENT-ON* */266}267268void __declspec(naked) _allrem()269{270/* *INDENT-OFF* */271__asm {272push ebx273push edi274xor edi,edi275mov eax,dword ptr [esp+10h]276or eax,eax277jge L1278inc edi279mov edx,dword ptr [esp+0Ch]280neg eax281neg edx282sbb eax,0283mov dword ptr [esp+10h],eax284mov dword ptr [esp+0Ch],edx285L1:286mov eax,dword ptr [esp+18h]287or eax,eax288jge L2289mov edx,dword ptr [esp+14h]290neg eax291neg edx292sbb eax,0293mov dword ptr [esp+18h],eax294mov dword ptr [esp+14h],edx295L2:296or eax,eax297jne L3298mov ecx,dword ptr [esp+14h]299mov eax,dword ptr [esp+10h]300xor edx,edx301div ecx302mov eax,dword ptr [esp+0Ch]303div ecx304mov eax,edx305xor edx,edx306dec edi307jns L4308jmp L8309L3:310mov ebx,eax311mov ecx,dword ptr [esp+14h]312mov edx,dword ptr [esp+10h]313mov eax,dword ptr [esp+0Ch]314L5:315shr ebx,1316rcr ecx,1317shr edx,1318rcr eax,1319or ebx,ebx320jne L5321div ecx322mov ecx,eax323mul dword ptr [esp+18h]324xchg eax,ecx325mul dword ptr [esp+14h]326add edx,ecx327jb L6328cmp edx,dword ptr [esp+10h]329ja L6330jb L7331cmp eax,dword ptr [esp+0Ch]332jbe L7333L6:334sub eax,dword ptr [esp+14h]335sbb edx,dword ptr [esp+18h]336L7:337sub eax,dword ptr [esp+0Ch]338sbb edx,dword ptr [esp+10h]339dec edi340jns L8341L4:342neg edx343neg eax344sbb edx,0345L8:346pop edi347pop ebx348ret 10h349}350/* *INDENT-ON* */351}352353void __declspec(naked) _aullrem()354{355/* *INDENT-OFF* */356__asm {357push ebx358mov eax,dword ptr [esp+14h]359or eax,eax360jne L1361mov ecx,dword ptr [esp+10h]362mov eax,dword ptr [esp+0Ch]363xor edx,edx364div ecx365mov eax,dword ptr [esp+8]366div ecx367mov eax,edx368xor edx,edx369jmp L2370L1:371mov ecx,eax372mov ebx,dword ptr [esp+10h]373mov edx,dword ptr [esp+0Ch]374mov eax,dword ptr [esp+8]375L3:376shr ecx,1377rcr ebx,1378shr edx,1379rcr eax,1380or ecx,ecx381jne L3382div ebx383mov ecx,eax384mul dword ptr [esp+14h]385xchg eax,ecx386mul dword ptr [esp+10h]387add edx,ecx388jb L4389cmp edx,dword ptr [esp+0Ch]390ja L4391jb L5392cmp eax,dword ptr [esp+8]393jbe L5394L4:395sub eax,dword ptr [esp+10h]396sbb edx,dword ptr [esp+14h]397L5:398sub eax,dword ptr [esp+8]399sbb edx,dword ptr [esp+0Ch]400neg edx401neg eax402sbb edx,0403L2:404pop ebx405ret 10h406}407/* *INDENT-ON* */408}409410void __declspec(naked) _alldvrm()411{412/* *INDENT-OFF* */413__asm {414push edi415push esi416push ebp417xor edi,edi418xor ebp,ebp419mov eax,dword ptr [esp+14h]420or eax,eax421jge L1422inc edi423inc ebp424mov edx,dword ptr [esp+10h]425neg eax426neg edx427sbb eax,0428mov dword ptr [esp+14h],eax429mov dword ptr [esp+10h],edx430L1:431mov eax,dword ptr [esp+1Ch]432or eax,eax433jge L2434inc edi435mov edx,dword ptr [esp+18h]436neg eax437neg edx438sbb eax,0439mov dword ptr [esp+1Ch],eax440mov dword ptr [esp+18h],edx441L2:442or eax,eax443jne L3444mov ecx,dword ptr [esp+18h]445mov eax,dword ptr [esp+14h]446xor edx,edx447div ecx448mov ebx,eax449mov eax,dword ptr [esp+10h]450div ecx451mov esi,eax452mov eax,ebx453mul dword ptr [esp+18h]454mov ecx,eax455mov eax,esi456mul dword ptr [esp+18h]457add edx,ecx458jmp L4459L3:460mov ebx,eax461mov ecx,dword ptr [esp+18h]462mov edx,dword ptr [esp+14h]463mov eax,dword ptr [esp+10h]464L5:465shr ebx,1466rcr ecx,1467shr edx,1468rcr eax,1469or ebx,ebx470jne L5471div ecx472mov esi,eax473mul dword ptr [esp+1Ch]474mov ecx,eax475mov eax,dword ptr [esp+18h]476mul esi477add edx,ecx478jb L6479cmp edx,dword ptr [esp+14h]480ja L6481jb L7482cmp eax,dword ptr [esp+10h]483jbe L7484L6:485dec esi486sub eax,dword ptr [esp+18h]487sbb edx,dword ptr [esp+1Ch]488L7:489xor ebx,ebx490L4:491sub eax,dword ptr [esp+10h]492sbb edx,dword ptr [esp+14h]493dec ebp494jns L9495neg edx496neg eax497sbb edx,0498L9:499mov ecx,edx500mov edx,ebx501mov ebx,ecx502mov ecx,eax503mov eax,esi504dec edi505jne L8506neg edx507neg eax508sbb edx,0509L8:510pop ebp511pop esi512pop edi513ret 10h514}515/* *INDENT-ON* */516}517518void __declspec(naked) _aulldvrm()519{520/* *INDENT-OFF* */521__asm {522push esi523mov eax,dword ptr [esp+14h]524or eax,eax525jne L1526mov ecx,dword ptr [esp+10h]527mov eax,dword ptr [esp+0Ch]528xor edx,edx529div ecx530mov ebx,eax531mov eax,dword ptr [esp+8]532div ecx533mov esi,eax534mov eax,ebx535mul dword ptr [esp+10h]536mov ecx,eax537mov eax,esi538mul dword ptr [esp+10h]539add edx,ecx540jmp L2541L1:542mov ecx,eax543mov ebx,dword ptr [esp+10h]544mov edx,dword ptr [esp+0Ch]545mov eax,dword ptr [esp+8]546L3:547shr ecx,1548rcr ebx,1549shr edx,1550rcr eax,1551or ecx,ecx552jne L3553div ebx554mov esi,eax555mul dword ptr [esp+14h]556mov ecx,eax557mov eax,dword ptr [esp+10h]558mul esi559add edx,ecx560jb L4561cmp edx,dword ptr [esp+0Ch]562ja L4563jb L5564cmp eax,dword ptr [esp+8]565jbe L5566L4:567dec esi568sub eax,dword ptr [esp+10h]569sbb edx,dword ptr [esp+14h]570L5:571xor ebx,ebx572L2:573sub eax,dword ptr [esp+8]574sbb edx,dword ptr [esp+0Ch]575neg edx576neg eax577sbb edx,0578mov ecx,edx579mov edx,ebx580mov ebx,ecx581mov ecx,eax582mov eax,esi583pop esi584ret 10h585}586/* *INDENT-ON* */587}588589void __declspec(naked) _allshl()590{591/* *INDENT-OFF* */592__asm {593cmp cl,40h594jae RETZERO595cmp cl,20h596jae MORE32597shld edx,eax,cl598shl eax,cl599ret600MORE32:601mov edx,eax602xor eax,eax603and cl,1Fh604shl edx,cl605ret606RETZERO:607xor eax,eax608xor edx,edx609ret610}611/* *INDENT-ON* */612}613614void __declspec(naked) _allshr()615{616/* *INDENT-OFF* */617__asm {618cmp cl,3Fh619jae RETSIGN620cmp cl,20h621jae MORE32622shrd eax,edx,cl623sar edx,cl624ret625MORE32:626mov eax,edx627sar edx,1Fh628and cl,1Fh629sar eax,cl630ret631RETSIGN:632sar edx,1Fh633mov eax,edx634ret635}636/* *INDENT-ON* */637}638639void __declspec(naked) _aullshr()640{641/* *INDENT-OFF* */642__asm {643cmp cl,40h644jae RETZERO645cmp cl,20h646jae MORE32647shrd eax,edx,cl648shr edx,cl649ret650MORE32:651mov eax,edx652xor edx,edx653and cl,1Fh654shr eax,cl655ret656RETZERO:657xor eax,eax658xor edx,edx659ret660}661/* *INDENT-ON* */662}663664void __declspec(naked) _chkstk(void)665{666__asm {667push ecx668mov ecx,esp ; lea ecx,dword ptr [esp]+4669add ecx,4670sub ecx,eax671sbb eax,eax672not eax673and ecx,eax674mov eax,esp675and eax,0xfffff000676L1:677cmp ecx,eax678jb short L2679mov eax,ecx680pop ecx681xchg esp,eax682mov eax,dword ptr [eax]683mov dword ptr [esp],eax684ret685L2:686sub eax,0x1000687test dword ptr [eax],eax688jmp short L1689}690}691692void __declspec(naked) _alloca_probe_8(void)693{694/* *INDENT-OFF* */695__asm {696push ecx697mov ecx,esp ; lea ecx,dword ptr [esp]+8698add ecx,8699sub ecx,eax700and ecx,0x7701add eax,ecx702sbb ecx,ecx703or eax,ecx704pop ecx705jmp _chkstk706}707/* *INDENT-ON* */708}709710void __declspec(naked) _alloca_probe_16(void)711{712/* *INDENT-OFF* */713__asm {714push ecx715mov ecx,esp ; lea ecx,dword ptr [esp]+8716add ecx,8717sub ecx,eax718and ecx,0xf719add eax,ecx720sbb ecx,ecx721or eax,ecx722pop ecx723jmp _chkstk724}725/* *INDENT-ON* */726}727728#endif // _M_IX86729730#endif // MSC_VER731732#ifdef __ICL733/* The classic Intel compiler generates calls to _intel_fast_memcpy734* and _intel_fast_memset when building an optimized SDL library */735void *_intel_fast_memcpy(void *dst, const void *src, size_t len)736{737return SDL_memcpy(dst, src, len);738}739void *_intel_fast_memset(void *dst, int c, size_t len)740{741return SDL_memset(dst, c, len);742}743#endif744745#endif // !HAVE_LIBC && !SDL_STATIC_LIB746747748