SDL_mslibc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
  19. #define SDL_DISABLE_ANALYZE_MACROS 1
  20. #endif
  21. #include "../SDL_internal.h"
  22. /* This file contains SDL replacements for functions in the C library */
  23. #if !defined(HAVE_LIBC) && !defined(SDL_STATIC_LIB)
  24. /* These are some C runtime intrinsics that need to be defined */
  25. #if defined(_MSC_VER)
  26. #ifndef __FLTUSED__
  27. #define __FLTUSED__
  28. __declspec(selectany) int _fltused = 1;
  29. #endif
  30. /* The optimizer on Visual Studio 2005 and later generates memcpy() and memset() calls.
  31. Always provide it for the SDL2 DLL, but skip it when building static lib w/ static runtime. */
  32. #if (_MSC_VER >= 1400) && (!defined(_MT) || defined(DLL_EXPORT))
  33. /* NOLINTNEXTLINE(readability-redundant-declaration) */
  34. extern void *memcpy(void *dst, const void *src, size_t len);
  35. #pragma intrinsic(memcpy)
  36. #if !defined(__clang__)
  37. #pragma function(memcpy)
  38. #endif
  39. /* NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) */
  40. void *memcpy(void *dst, const void *src, size_t len)
  41. {
  42. return SDL_memcpy(dst, src, len);
  43. }
  44. /* NOLINTNEXTLINE(readability-redundant-declaration) */
  45. extern void *memset(void *dst, int c, size_t len);
  46. #pragma intrinsic(memset)
  47. #if !defined(__clang__)
  48. #pragma function(memset)
  49. #endif
  50. /* NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) */
  51. void *memset(void *dst, int c, size_t len)
  52. {
  53. return SDL_memset(dst, c, len);
  54. }
  55. #endif /* (_MSC_VER >= 1400) && (!defined(_MT) || defined(DLL_EXPORT)) */
  56. #ifdef _M_IX86
  57. /* Float to long */
  58. void __declspec(naked) _ftol()
  59. {
  60. /* *INDENT-OFF* */
  61. __asm {
  62. push ebp
  63. mov ebp,esp
  64. sub esp,20h
  65. and esp,0FFFFFFF0h
  66. fld st(0)
  67. fst dword ptr [esp+18h]
  68. fistp qword ptr [esp+10h]
  69. fild qword ptr [esp+10h]
  70. mov edx,dword ptr [esp+18h]
  71. mov eax,dword ptr [esp+10h]
  72. test eax,eax
  73. je integer_QnaN_or_zero
  74. arg_is_not_integer_QnaN:
  75. fsubp st(1),st
  76. test edx,edx
  77. jns positive
  78. fstp dword ptr [esp]
  79. mov ecx,dword ptr [esp]
  80. xor ecx,80000000h
  81. add ecx,7FFFFFFFh
  82. adc eax,0
  83. mov edx,dword ptr [esp+14h]
  84. adc edx,0
  85. jmp localexit
  86. positive:
  87. fstp dword ptr [esp]
  88. mov ecx,dword ptr [esp]
  89. add ecx,7FFFFFFFh
  90. sbb eax,0
  91. mov edx,dword ptr [esp+14h]
  92. sbb edx,0
  93. jmp localexit
  94. integer_QnaN_or_zero:
  95. mov edx,dword ptr [esp+14h]
  96. test edx,7FFFFFFFh
  97. jne arg_is_not_integer_QnaN
  98. fstp dword ptr [esp+18h]
  99. fstp dword ptr [esp+18h]
  100. localexit:
  101. leave
  102. ret
  103. }
  104. /* *INDENT-ON* */
  105. }
  106. void _ftol2_sse()
  107. {
  108. _ftol();
  109. }
  110. void _ftol2()
  111. {
  112. _ftol();
  113. }
  114. /* 64-bit math operators for 32-bit systems */
  115. void __declspec(naked) _allmul()
  116. {
  117. /* *INDENT-OFF* */
  118. __asm {
  119. mov eax, dword ptr[esp+8]
  120. mov ecx, dword ptr[esp+10h]
  121. or ecx, eax
  122. mov ecx, dword ptr[esp+0Ch]
  123. jne hard
  124. mov eax, dword ptr[esp+4]
  125. mul ecx
  126. ret 10h
  127. hard:
  128. push ebx
  129. mul ecx
  130. mov ebx, eax
  131. mov eax, dword ptr[esp+8]
  132. mul dword ptr[esp+14h]
  133. add ebx, eax
  134. mov eax, dword ptr[esp+8]
  135. mul ecx
  136. add edx, ebx
  137. pop ebx
  138. ret 10h
  139. }
  140. /* *INDENT-ON* */
  141. }
  142. void __declspec(naked) _alldiv()
  143. {
  144. /* *INDENT-OFF* */
  145. __asm {
  146. push edi
  147. push esi
  148. push ebx
  149. xor edi,edi
  150. mov eax,dword ptr [esp+14h]
  151. or eax,eax
  152. jge L1
  153. inc edi
  154. mov edx,dword ptr [esp+10h]
  155. neg eax
  156. neg edx
  157. sbb eax,0
  158. mov dword ptr [esp+14h],eax
  159. mov dword ptr [esp+10h],edx
  160. L1:
  161. mov eax,dword ptr [esp+1Ch]
  162. or eax,eax
  163. jge L2
  164. inc edi
  165. mov edx,dword ptr [esp+18h]
  166. neg eax
  167. neg edx
  168. sbb eax,0
  169. mov dword ptr [esp+1Ch],eax
  170. mov dword ptr [esp+18h],edx
  171. L2:
  172. or eax,eax
  173. jne L3
  174. mov ecx,dword ptr [esp+18h]
  175. mov eax,dword ptr [esp+14h]
  176. xor edx,edx
  177. div ecx
  178. mov ebx,eax
  179. mov eax,dword ptr [esp+10h]
  180. div ecx
  181. mov edx,ebx
  182. jmp L4
  183. L3:
  184. mov ebx,eax
  185. mov ecx,dword ptr [esp+18h]
  186. mov edx,dword ptr [esp+14h]
  187. mov eax,dword ptr [esp+10h]
  188. L5:
  189. shr ebx,1
  190. rcr ecx,1
  191. shr edx,1
  192. rcr eax,1
  193. or ebx,ebx
  194. jne L5
  195. div ecx
  196. mov esi,eax
  197. mul dword ptr [esp+1Ch]
  198. mov ecx,eax
  199. mov eax,dword ptr [esp+18h]
  200. mul esi
  201. add edx,ecx
  202. jb L6
  203. cmp edx,dword ptr [esp+14h]
  204. ja L6
  205. jb L7
  206. cmp eax,dword ptr [esp+10h]
  207. jbe L7
  208. L6:
  209. dec esi
  210. L7:
  211. xor edx,edx
  212. mov eax,esi
  213. L4:
  214. dec edi
  215. jne L8
  216. neg edx
  217. neg eax
  218. sbb edx,0
  219. L8:
  220. pop ebx
  221. pop esi
  222. pop edi
  223. ret 10h
  224. }
  225. /* *INDENT-ON* */
  226. }
  227. void __declspec(naked) _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 __declspec(naked) _allrem()
  283. {
  284. /* *INDENT-OFF* */
  285. __asm {
  286. push ebx
  287. push edi
  288. xor edi,edi
  289. mov eax,dword ptr [esp+10h]
  290. or eax,eax
  291. jge L1
  292. inc edi
  293. mov edx,dword ptr [esp+0Ch]
  294. neg eax
  295. neg edx
  296. sbb eax,0
  297. mov dword ptr [esp+10h],eax
  298. mov dword ptr [esp+0Ch],edx
  299. L1:
  300. mov eax,dword ptr [esp+18h]
  301. or eax,eax
  302. jge L2
  303. mov edx,dword ptr [esp+14h]
  304. neg eax
  305. neg edx
  306. sbb eax,0
  307. mov dword ptr [esp+18h],eax
  308. mov dword ptr [esp+14h],edx
  309. L2:
  310. or eax,eax
  311. jne L3
  312. mov ecx,dword ptr [esp+14h]
  313. mov eax,dword ptr [esp+10h]
  314. xor edx,edx
  315. div ecx
  316. mov eax,dword ptr [esp+0Ch]
  317. div ecx
  318. mov eax,edx
  319. xor edx,edx
  320. dec edi
  321. jns L4
  322. jmp L8
  323. L3:
  324. mov ebx,eax
  325. mov ecx,dword ptr [esp+14h]
  326. mov edx,dword ptr [esp+10h]
  327. mov eax,dword ptr [esp+0Ch]
  328. L5:
  329. shr ebx,1
  330. rcr ecx,1
  331. shr edx,1
  332. rcr eax,1
  333. or ebx,ebx
  334. jne L5
  335. div ecx
  336. mov ecx,eax
  337. mul dword ptr [esp+18h]
  338. xchg eax,ecx
  339. mul dword ptr [esp+14h]
  340. add edx,ecx
  341. jb L6
  342. cmp edx,dword ptr [esp+10h]
  343. ja L6
  344. jb L7
  345. cmp eax,dword ptr [esp+0Ch]
  346. jbe L7
  347. L6:
  348. sub eax,dword ptr [esp+14h]
  349. sbb edx,dword ptr [esp+18h]
  350. L7:
  351. sub eax,dword ptr [esp+0Ch]
  352. sbb edx,dword ptr [esp+10h]
  353. dec edi
  354. jns L8
  355. L4:
  356. neg edx
  357. neg eax
  358. sbb edx,0
  359. L8:
  360. pop edi
  361. pop ebx
  362. ret 10h
  363. }
  364. /* *INDENT-ON* */
  365. }
  366. void __declspec(naked) _aullrem()
  367. {
  368. /* *INDENT-OFF* */
  369. __asm {
  370. push ebx
  371. mov eax,dword ptr [esp+14h]
  372. or eax,eax
  373. jne L1
  374. mov ecx,dword ptr [esp+10h]
  375. mov eax,dword ptr [esp+0Ch]
  376. xor edx,edx
  377. div ecx
  378. mov eax,dword ptr [esp+8]
  379. div ecx
  380. mov eax,edx
  381. xor edx,edx
  382. jmp L2
  383. L1:
  384. mov ecx,eax
  385. mov ebx,dword ptr [esp+10h]
  386. mov edx,dword ptr [esp+0Ch]
  387. mov eax,dword ptr [esp+8]
  388. L3:
  389. shr ecx,1
  390. rcr ebx,1
  391. shr edx,1
  392. rcr eax,1
  393. or ecx,ecx
  394. jne L3
  395. div ebx
  396. mov ecx,eax
  397. mul dword ptr [esp+14h]
  398. xchg eax,ecx
  399. mul dword ptr [esp+10h]
  400. add edx,ecx
  401. jb L4
  402. cmp edx,dword ptr [esp+0Ch]
  403. ja L4
  404. jb L5
  405. cmp eax,dword ptr [esp+8]
  406. jbe L5
  407. L4:
  408. sub eax,dword ptr [esp+10h]
  409. sbb edx,dword ptr [esp+14h]
  410. L5:
  411. sub eax,dword ptr [esp+8]
  412. sbb edx,dword ptr [esp+0Ch]
  413. neg edx
  414. neg eax
  415. sbb edx,0
  416. L2:
  417. pop ebx
  418. ret 10h
  419. }
  420. /* *INDENT-ON* */
  421. }
  422. void __declspec(naked) _alldvrm()
  423. {
  424. /* *INDENT-OFF* */
  425. __asm {
  426. push edi
  427. push esi
  428. push ebp
  429. xor edi,edi
  430. xor ebp,ebp
  431. mov eax,dword ptr [esp+14h]
  432. or eax,eax
  433. jge L1
  434. inc edi
  435. inc ebp
  436. mov edx,dword ptr [esp+10h]
  437. neg eax
  438. neg edx
  439. sbb eax,0
  440. mov dword ptr [esp+14h],eax
  441. mov dword ptr [esp+10h],edx
  442. L1:
  443. mov eax,dword ptr [esp+1Ch]
  444. or eax,eax
  445. jge L2
  446. inc edi
  447. mov edx,dword ptr [esp+18h]
  448. neg eax
  449. neg edx
  450. sbb eax,0
  451. mov dword ptr [esp+1Ch],eax
  452. mov dword ptr [esp+18h],edx
  453. L2:
  454. or eax,eax
  455. jne L3
  456. mov ecx,dword ptr [esp+18h]
  457. mov eax,dword ptr [esp+14h]
  458. xor edx,edx
  459. div ecx
  460. mov ebx,eax
  461. mov eax,dword ptr [esp+10h]
  462. div ecx
  463. mov esi,eax
  464. mov eax,ebx
  465. mul dword ptr [esp+18h]
  466. mov ecx,eax
  467. mov eax,esi
  468. mul dword ptr [esp+18h]
  469. add edx,ecx
  470. jmp L4
  471. L3:
  472. mov ebx,eax
  473. mov ecx,dword ptr [esp+18h]
  474. mov edx,dword ptr [esp+14h]
  475. mov eax,dword ptr [esp+10h]
  476. L5:
  477. shr ebx,1
  478. rcr ecx,1
  479. shr edx,1
  480. rcr eax,1
  481. or ebx,ebx
  482. jne L5
  483. div ecx
  484. mov esi,eax
  485. mul dword ptr [esp+1Ch]
  486. mov ecx,eax
  487. mov eax,dword ptr [esp+18h]
  488. mul esi
  489. add edx,ecx
  490. jb L6
  491. cmp edx,dword ptr [esp+14h]
  492. ja L6
  493. jb L7
  494. cmp eax,dword ptr [esp+10h]
  495. jbe L7
  496. L6:
  497. dec esi
  498. sub eax,dword ptr [esp+18h]
  499. sbb edx,dword ptr [esp+1Ch]
  500. L7:
  501. xor ebx,ebx
  502. L4:
  503. sub eax,dword ptr [esp+10h]
  504. sbb edx,dword ptr [esp+14h]
  505. dec ebp
  506. jns L9
  507. neg edx
  508. neg eax
  509. sbb edx,0
  510. L9:
  511. mov ecx,edx
  512. mov edx,ebx
  513. mov ebx,ecx
  514. mov ecx,eax
  515. mov eax,esi
  516. dec edi
  517. jne L8
  518. neg edx
  519. neg eax
  520. sbb edx,0
  521. L8:
  522. pop ebp
  523. pop esi
  524. pop edi
  525. ret 10h
  526. }
  527. /* *INDENT-ON* */
  528. }
  529. void __declspec(naked) _aulldvrm()
  530. {
  531. /* *INDENT-OFF* */
  532. __asm {
  533. push esi
  534. mov eax,dword ptr [esp+14h]
  535. or eax,eax
  536. jne L1
  537. mov ecx,dword ptr [esp+10h]
  538. mov eax,dword ptr [esp+0Ch]
  539. xor edx,edx
  540. div ecx
  541. mov ebx,eax
  542. mov eax,dword ptr [esp+8]
  543. div ecx
  544. mov esi,eax
  545. mov eax,ebx
  546. mul dword ptr [esp+10h]
  547. mov ecx,eax
  548. mov eax,esi
  549. mul dword ptr [esp+10h]
  550. add edx,ecx
  551. jmp L2
  552. L1:
  553. mov ecx,eax
  554. mov ebx,dword ptr [esp+10h]
  555. mov edx,dword ptr [esp+0Ch]
  556. mov eax,dword ptr [esp+8]
  557. L3:
  558. shr ecx,1
  559. rcr ebx,1
  560. shr edx,1
  561. rcr eax,1
  562. or ecx,ecx
  563. jne L3
  564. div ebx
  565. mov esi,eax
  566. mul dword ptr [esp+14h]
  567. mov ecx,eax
  568. mov eax,dword ptr [esp+10h]
  569. mul esi
  570. add edx,ecx
  571. jb L4
  572. cmp edx,dword ptr [esp+0Ch]
  573. ja L4
  574. jb L5
  575. cmp eax,dword ptr [esp+8]
  576. jbe L5
  577. L4:
  578. dec esi
  579. sub eax,dword ptr [esp+10h]
  580. sbb edx,dword ptr [esp+14h]
  581. L5:
  582. xor ebx,ebx
  583. L2:
  584. sub eax,dword ptr [esp+8]
  585. sbb edx,dword ptr [esp+0Ch]
  586. neg edx
  587. neg eax
  588. sbb edx,0
  589. mov ecx,edx
  590. mov edx,ebx
  591. mov ebx,ecx
  592. mov ecx,eax
  593. mov eax,esi
  594. pop esi
  595. ret 10h
  596. }
  597. /* *INDENT-ON* */
  598. }
  599. void __declspec(naked) _allshl()
  600. {
  601. /* *INDENT-OFF* */
  602. __asm {
  603. cmp cl,40h
  604. jae RETZERO
  605. cmp cl,20h
  606. jae MORE32
  607. shld edx,eax,cl
  608. shl eax,cl
  609. ret
  610. MORE32:
  611. mov edx,eax
  612. xor eax,eax
  613. and cl,1Fh
  614. shl edx,cl
  615. ret
  616. RETZERO:
  617. xor eax,eax
  618. xor edx,edx
  619. ret
  620. }
  621. /* *INDENT-ON* */
  622. }
  623. void __declspec(naked) _allshr()
  624. {
  625. /* *INDENT-OFF* */
  626. __asm {
  627. cmp cl,3Fh
  628. jae RETSIGN
  629. cmp cl,20h
  630. jae MORE32
  631. shrd eax,edx,cl
  632. sar edx,cl
  633. ret
  634. MORE32:
  635. mov eax,edx
  636. sar edx,1Fh
  637. and cl,1Fh
  638. sar eax,cl
  639. ret
  640. RETSIGN:
  641. sar edx,1Fh
  642. mov eax,edx
  643. ret
  644. }
  645. /* *INDENT-ON* */
  646. }
  647. void __declspec(naked) _aullshr()
  648. {
  649. /* *INDENT-OFF* */
  650. __asm {
  651. cmp cl,40h
  652. jae RETZERO
  653. cmp cl,20h
  654. jae MORE32
  655. shrd eax,edx,cl
  656. shr edx,cl
  657. ret
  658. MORE32:
  659. mov eax,edx
  660. xor edx,edx
  661. and cl,1Fh
  662. shr eax,cl
  663. ret
  664. RETZERO:
  665. xor eax,eax
  666. xor edx,edx
  667. ret
  668. }
  669. /* *INDENT-ON* */
  670. }
  671. #endif /* _M_IX86 */
  672. #endif /* MSC_VER */
  673. #if defined(__ICL)
  674. /* The classic Intel compiler generates calls to _intel_fast_memcpy
  675. * and _intel_fast_memset when building an optimized SDL library */
  676. void *_intel_fast_memcpy(void *dst, const void *src, size_t len)
  677. {
  678. return SDL_memcpy(dst, src, len);
  679. }
  680. void *_intel_fast_memset(void *dst, int c, size_t len)
  681. {
  682. return SDL_memset(dst, c, len);
  683. }
  684. #endif
  685. #endif /* !HAVE_LIBC && !SDL_STATIC_LIB */
  686. /* vi: set ts=4 sw=4 expandtab: */