SDL 3.0
SDL_stdinc.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * # CategoryStdinc
24 *
25 * This is a general header that includes C language support. It implements a
26 * subset of the C runtime APIs, but with an `SDL_` prefix. For most common
27 * use cases, these should behave the same way as their C runtime equivalents,
28 * but they may differ in how or whether they handle certain edge cases. When
29 * in doubt, consult the documentation for details.
30 */
31
32#ifndef SDL_stdinc_h_
33#define SDL_stdinc_h_
34
36
37#include <stdarg.h>
38#include <stdint.h>
39#include <string.h>
40#include <wchar.h>
41
42#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
43 defined(SDL_INCLUDE_INTTYPES_H)
44#include <inttypes.h>
45#endif
46
47#ifndef __cplusplus
48#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
49 (defined(_MSC_VER) && (_MSC_VER >= 1910 /* Visual Studio 2017 */)) || \
50 defined(SDL_INCLUDE_STDBOOL_H)
51#include <stdbool.h>
52#elif !defined(__bool_true_false_are_defined) && !defined(bool)
53#define bool unsigned char
54#define false 0
55#define true 1
56#define __bool_true_false_are_defined 1
57#endif
58#endif /* !__cplusplus */
59
60#ifndef SDL_DISABLE_ALLOCA
61# ifndef alloca
62# ifdef HAVE_ALLOCA_H
63# include <alloca.h>
64# elif defined(SDL_PLATFORM_NETBSD)
65# if defined(__STRICT_ANSI__)
66# define SDL_DISABLE_ALLOCA
67# else
68# include <stdlib.h>
69# endif
70# elif defined(__GNUC__)
71# define alloca __builtin_alloca
72# elif defined(_MSC_VER)
73# include <malloc.h>
74# define alloca _alloca
75# elif defined(__WATCOMC__)
76# include <malloc.h>
77# elif defined(__BORLANDC__)
78# include <malloc.h>
79# elif defined(__DMC__)
80# include <stdlib.h>
81# elif defined(SDL_PLATFORM_AIX)
82# pragma alloca
83# elif defined(__MRC__)
84void *alloca(unsigned);
85# else
86void *alloca(size_t);
87# endif
88# endif
89#endif
90
91#ifdef SIZE_MAX
92# define SDL_SIZE_MAX SIZE_MAX
93#else
94# define SDL_SIZE_MAX ((size_t) -1)
95#endif
96
97#ifndef SDL_COMPILE_TIME_ASSERT
98#if defined(__cplusplus)
99/* Keep C++ case alone: Some versions of gcc will define __STDC_VERSION__ even when compiling in C++ mode. */
100#if (__cplusplus >= 201103L)
101#define SDL_COMPILE_TIME_ASSERT(name, x) static_assert(x, #x)
102#endif
103#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)
104#define SDL_COMPILE_TIME_ASSERT(name, x) static_assert(x, #x)
105#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
106#define SDL_COMPILE_TIME_ASSERT(name, x) _Static_assert(x, #x)
107#endif
108#endif /* !SDL_COMPILE_TIME_ASSERT */
109
110#ifndef SDL_COMPILE_TIME_ASSERT
111/* universal, but may trigger -Wunused-local-typedefs */
112#define SDL_COMPILE_TIME_ASSERT(name, x) \
113 typedef int SDL_compile_time_assert_ ## name[(x) * 2 - 1]
114#endif
115
116/**
117 * Check if the compiler supports a given builtin.
118 * Supported by virtually all clang versions and recent gcc. Use this
119 * instead of checking the clang version if possible.
120 */
121#ifdef __has_builtin
122#define SDL_HAS_BUILTIN(x) __has_builtin(x)
123#else
124#define SDL_HAS_BUILTIN(x) 0
125#endif
126
127/**
128 * The number of elements in a static array.
129 *
130 * This will compile but return incorrect results for a pointer to an array;
131 * it has to be an array the compiler knows the size of.
132 *
133 * This macro looks like it double-evaluates the argument, but it does so
134 * inside of `sizeof`, so there are no side-effects here, as expressions do
135 * not actually run any code in these cases.
136 *
137 * \since This macro is available since SDL 3.1.3.
138 */
139#define SDL_arraysize(array) (sizeof(array)/sizeof(array[0]))
140
141/**
142 * Macro useful for building other macros with strings in them.
143 *
144 * For example:
145 *
146 * ```c
147 * #define LOG_ERROR(X) OutputDebugString(SDL_STRINGIFY_ARG(__FUNCTION__) ": " X "\n")`
148 * ```
149 *
150 * \since This macro is available since SDL 3.1.3.
151 */
152#define SDL_STRINGIFY_ARG(arg) #arg
153
154/**
155 * \name Cast operators
156 *
157 * Use proper C++ casts when compiled as C++ to be compatible with the option
158 * -Wold-style-cast of GCC (and -Werror=old-style-cast in GCC 4.2 and above).
159 */
160/* @{ */
161
162#ifdef SDL_WIKI_DOCUMENTATION_SECTION
163
164/**
165 * Handle a Reinterpret Cast properly whether using C or C++.
166 *
167 * If compiled as C++, this macro offers a proper C++ reinterpret_cast<>.
168 *
169 * If compiled as C, this macro does a normal C-style cast.
170 *
171 * This is helpful to avoid compiler warnings in C++.
172 *
173 * \param type the type to cast the expression to.
174 * \param expression the expression to cast to a different type.
175 * \returns `expression`, cast to `type`.
176 *
177 * \threadsafety It is safe to call this macro from any thread.
178 *
179 * \since This macro is available since SDL 3.1.3.
180 *
181 * \sa SDL_static_cast
182 * \sa SDL_const_cast
183 */
184#define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression) /* or `((type)(expression))` in C */
185
186/**
187 * Handle a Static Cast properly whether using C or C++.
188 *
189 * If compiled as C++, this macro offers a proper C++ static_cast<>.
190 *
191 * If compiled as C, this macro does a normal C-style cast.
192 *
193 * This is helpful to avoid compiler warnings in C++.
194 *
195 * \param type the type to cast the expression to.
196 * \param expression the expression to cast to a different type.
197 * \returns `expression`, cast to `type`.
198 *
199 * \threadsafety It is safe to call this macro from any thread.
200 *
201 * \since This macro is available since SDL 3.1.3.
202 *
203 * \sa SDL_reinterpret_cast
204 * \sa SDL_const_cast
205 */
206#define SDL_static_cast(type, expression) static_cast<type>(expression) /* or `((type)(expression))` in C */
207
208/**
209 * Handle a Const Cast properly whether using C or C++.
210 *
211 * If compiled as C++, this macro offers a proper C++ const_cast<>.
212 *
213 * If compiled as C, this macro does a normal C-style cast.
214 *
215 * This is helpful to avoid compiler warnings in C++.
216 *
217 * \param type the type to cast the expression to.
218 * \param expression the expression to cast to a different type.
219 * \returns `expression`, cast to `type`.
220 *
221 * \threadsafety It is safe to call this macro from any thread.
222 *
223 * \since This macro is available since SDL 3.1.3.
224 *
225 * \sa SDL_reinterpret_cast
226 * \sa SDL_static_cast
227 */
228#define SDL_const_cast(type, expression) const_cast<type>(expression) /* or `((type)(expression))` in C */
229
230#elif defined(__cplusplus)
231#define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression)
232#define SDL_static_cast(type, expression) static_cast<type>(expression)
233#define SDL_const_cast(type, expression) const_cast<type>(expression)
234#else
235#define SDL_reinterpret_cast(type, expression) ((type)(expression))
236#define SDL_static_cast(type, expression) ((type)(expression))
237#define SDL_const_cast(type, expression) ((type)(expression))
238#endif
239
240/* @} *//* Cast operators */
241
242/**
243 * Define a four character code as a Uint32.
244 *
245 * \param A the first ASCII character.
246 * \param B the second ASCII character.
247 * \param C the third ASCII character.
248 * \param D the fourth ASCII character.
249 * \returns the four characters converted into a Uint32, one character
250 * per-byte.
251 *
252 * \threadsafety It is safe to call this macro from any thread.
253 *
254 * \since This macro is available since SDL 3.1.3.
255 */
256#define SDL_FOURCC(A, B, C, D) \
257 ((SDL_static_cast(Uint32, SDL_static_cast(Uint8, (A))) << 0) | \
258 (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (B))) << 8) | \
259 (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (C))) << 16) | \
260 (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (D))) << 24))
261
262#ifdef SDL_WIKI_DOCUMENTATION_SECTION
263
264/**
265 * Append the 64 bit integer suffix to a signed integer literal.
266 *
267 * This helps compilers that might believe a integer literal larger than
268 * 0xFFFFFFFF is overflowing a 32-bit value. Use `SDL_SINT64_C(0xFFFFFFFF1)`
269 * instead of `0xFFFFFFFF1` by itself.
270 *
271 * \since This macro is available since SDL 3.1.3.
272 *
273 * \sa SDL_UINT64_C
274 */
275#define SDL_SINT64_C(c) c ## LL /* or whatever the current compiler uses. */
276
277/**
278 * Append the 64 bit integer suffix to an unsigned integer literal.
279 *
280 * This helps compilers that might believe a integer literal larger than
281 * 0xFFFFFFFF is overflowing a 32-bit value. Use `SDL_UINT64_C(0xFFFFFFFF1)`
282 * instead of `0xFFFFFFFF1` by itself.
283 *
284 * \since This macro is available since SDL 3.1.3.
285 *
286 * \sa SDL_SINT64_C
287 */
288#define SDL_UINT64_C(c) c ## ULL /* or whatever the current compiler uses. */
289
290#else /* !SDL_WIKI_DOCUMENTATION_SECTION */
291
292#ifndef SDL_SINT64_C
293#if defined(INT64_C)
294#define SDL_SINT64_C(c) INT64_C(c)
295#elif defined(_MSC_VER)
296#define SDL_SINT64_C(c) c ## i64
297#elif defined(__LP64__) || defined(_LP64)
298#define SDL_SINT64_C(c) c ## L
299#else
300#define SDL_SINT64_C(c) c ## LL
301#endif
302#endif /* !SDL_SINT64_C */
303
304#ifndef SDL_UINT64_C
305#if defined(UINT64_C)
306#define SDL_UINT64_C(c) UINT64_C(c)
307#elif defined(_MSC_VER)
308#define SDL_UINT64_C(c) c ## ui64
309#elif defined(__LP64__) || defined(_LP64)
310#define SDL_UINT64_C(c) c ## UL
311#else
312#define SDL_UINT64_C(c) c ## ULL
313#endif
314#endif /* !SDL_UINT64_C */
315
316#endif /* !SDL_WIKI_DOCUMENTATION_SECTION */
317
318/**
319 * \name Basic data types
320 */
321/* @{ */
322
323/**
324 * A signed 8-bit integer type.
325 *
326 * \since This macro is available since SDL 3.1.3.
327 */
328typedef int8_t Sint8;
329#define SDL_MAX_SINT8 ((Sint8)0x7F) /* 127 */
330#define SDL_MIN_SINT8 ((Sint8)(~0x7F)) /* -128 */
331
332/**
333 * An unsigned 8-bit integer type.
334 *
335 * \since This macro is available since SDL 3.1.3.
336 */
337typedef uint8_t Uint8;
338#define SDL_MAX_UINT8 ((Uint8)0xFF) /* 255 */
339#define SDL_MIN_UINT8 ((Uint8)0x00) /* 0 */
340
341/**
342 * A signed 16-bit integer type.
343 *
344 * \since This macro is available since SDL 3.1.3.
345 */
346typedef int16_t Sint16;
347#define SDL_MAX_SINT16 ((Sint16)0x7FFF) /* 32767 */
348#define SDL_MIN_SINT16 ((Sint16)(~0x7FFF)) /* -32768 */
349
350/**
351 * An unsigned 16-bit integer type.
352 *
353 * \since This macro is available since SDL 3.1.3.
354 */
355typedef uint16_t Uint16;
356#define SDL_MAX_UINT16 ((Uint16)0xFFFF) /* 65535 */
357#define SDL_MIN_UINT16 ((Uint16)0x0000) /* 0 */
358
359/**
360 * A signed 32-bit integer type.
361 *
362 * \since This macro is available since SDL 3.1.3.
363 */
364typedef int32_t Sint32;
365#define SDL_MAX_SINT32 ((Sint32)0x7FFFFFFF) /* 2147483647 */
366#define SDL_MIN_SINT32 ((Sint32)(~0x7FFFFFFF)) /* -2147483648 */
367
368/**
369 * An unsigned 32-bit integer type.
370 *
371 * \since This macro is available since SDL 3.1.3.
372 */
373typedef uint32_t Uint32;
374#define SDL_MAX_UINT32 ((Uint32)0xFFFFFFFFu) /* 4294967295 */
375#define SDL_MIN_UINT32 ((Uint32)0x00000000) /* 0 */
376
377/**
378 * A signed 64-bit integer type.
379 *
380 * \since This macro is available since SDL 3.1.3.
381 *
382 * \sa SDL_SINT64_C
383 */
384typedef int64_t Sint64;
385#define SDL_MAX_SINT64 SDL_SINT64_C(0x7FFFFFFFFFFFFFFF) /* 9223372036854775807 */
386#define SDL_MIN_SINT64 ~SDL_SINT64_C(0x7FFFFFFFFFFFFFFF) /* -9223372036854775808 */
387
388/**
389 * An unsigned 64-bit integer type.
390 *
391 * \since This macro is available since SDL 3.1.3.
392 *
393 * \sa SDL_UINT64_C
394 */
395typedef uint64_t Uint64;
396#define SDL_MAX_UINT64 SDL_UINT64_C(0xFFFFFFFFFFFFFFFF) /* 18446744073709551615 */
397#define SDL_MIN_UINT64 SDL_UINT64_C(0x0000000000000000) /* 0 */
398
399/**
400 * SDL times are signed, 64-bit integers representing nanoseconds since the
401 * Unix epoch (Jan 1, 1970).
402 *
403 * They can be converted between POSIX time_t values with SDL_NS_TO_SECONDS()
404 * and SDL_SECONDS_TO_NS(), and between Windows FILETIME values with
405 * SDL_TimeToWindows() and SDL_TimeFromWindows().
406 *
407 * \since This macro is available since SDL 3.1.3.
408 *
409 * \sa SDL_MAX_SINT64
410 * \sa SDL_MIN_SINT64
411 */
413#define SDL_MAX_TIME SDL_MAX_SINT64
414#define SDL_MIN_TIME SDL_MIN_SINT64
415
416/* @} *//* Basic data types */
417
418/**
419 * \name Floating-point constants
420 */
421/* @{ */
422
423#ifdef FLT_EPSILON
424#define SDL_FLT_EPSILON FLT_EPSILON
425#else
426
427/**
428 * Epsilon constant, used for comparing floating-point numbers.
429 *
430 * Equals by default to platform-defined `FLT_EPSILON`, or
431 * `1.1920928955078125e-07F` if that's not available.
432 *
433 * \since This macro is available since SDL 3.1.3.
434 */
435#define SDL_FLT_EPSILON 1.1920928955078125e-07F /* 0x0.000002p0 */
436#endif
437
438/* @} *//* Floating-point constants */
439
440/* Make sure we have macros for printing width-based integers.
441 * <inttypes.h> should define these but this is not true all platforms.
442 * (for example win32) */
443#ifndef SDL_PRIs64
444#if defined(SDL_PLATFORM_WINDOWS)
445#define SDL_PRIs64 "I64d"
446#elif defined(PRId64)
447#define SDL_PRIs64 PRId64
448#elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE) && !defined(__EMSCRIPTEN__)
449#define SDL_PRIs64 "ld"
450#else
451#define SDL_PRIs64 "lld"
452#endif
453#endif
454#ifndef SDL_PRIu64
455#if defined(SDL_PLATFORM_WINDOWS)
456#define SDL_PRIu64 "I64u"
457#elif defined(PRIu64)
458#define SDL_PRIu64 PRIu64
459#elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE) && !defined(__EMSCRIPTEN__)
460#define SDL_PRIu64 "lu"
461#else
462#define SDL_PRIu64 "llu"
463#endif
464#endif
465#ifndef SDL_PRIx64
466#if defined(SDL_PLATFORM_WINDOWS)
467#define SDL_PRIx64 "I64x"
468#elif defined(PRIx64)
469#define SDL_PRIx64 PRIx64
470#elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
471#define SDL_PRIx64 "lx"
472#else
473#define SDL_PRIx64 "llx"
474#endif
475#endif
476#ifndef SDL_PRIX64
477#if defined(SDL_PLATFORM_WINDOWS)
478#define SDL_PRIX64 "I64X"
479#elif defined(PRIX64)
480#define SDL_PRIX64 PRIX64
481#elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
482#define SDL_PRIX64 "lX"
483#else
484#define SDL_PRIX64 "llX"
485#endif
486#endif
487#ifndef SDL_PRIs32
488#ifdef PRId32
489#define SDL_PRIs32 PRId32
490#else
491#define SDL_PRIs32 "d"
492#endif
493#endif
494#ifndef SDL_PRIu32
495#ifdef PRIu32
496#define SDL_PRIu32 PRIu32
497#else
498#define SDL_PRIu32 "u"
499#endif
500#endif
501#ifndef SDL_PRIx32
502#ifdef PRIx32
503#define SDL_PRIx32 PRIx32
504#else
505#define SDL_PRIx32 "x"
506#endif
507#endif
508#ifndef SDL_PRIX32
509#ifdef PRIX32
510#define SDL_PRIX32 PRIX32
511#else
512#define SDL_PRIX32 "X"
513#endif
514#endif
515/* Specifically for the `long long` -- SDL-specific. */
516#ifdef SDL_PLATFORM_WINDOWS
517SDL_COMPILE_TIME_ASSERT(longlong_size64, sizeof(long long) == 8); /* using I64 for windows - make sure `long long` is 64 bits. */
518#define SDL_PRILL_PREFIX "I64"
519#else
520#define SDL_PRILL_PREFIX "ll"
521#endif
522#ifndef SDL_PRILLd
523#define SDL_PRILLd SDL_PRILL_PREFIX "d"
524#endif
525#ifndef SDL_PRILLu
526#define SDL_PRILLu SDL_PRILL_PREFIX "u"
527#endif
528#ifndef SDL_PRILLx
529#define SDL_PRILLx SDL_PRILL_PREFIX "x"
530#endif
531#ifndef SDL_PRILLX
532#define SDL_PRILLX SDL_PRILL_PREFIX "X"
533#endif
534
535/* Annotations to help code analysis tools */
536#ifdef SDL_DISABLE_ANALYZE_MACROS
537#define SDL_IN_BYTECAP(x)
538#define SDL_INOUT_Z_CAP(x)
539#define SDL_OUT_Z_CAP(x)
540#define SDL_OUT_CAP(x)
541#define SDL_OUT_BYTECAP(x)
542#define SDL_OUT_Z_BYTECAP(x)
543#define SDL_PRINTF_FORMAT_STRING
544#define SDL_SCANF_FORMAT_STRING
545#define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
546#define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
547#define SDL_SCANF_VARARG_FUNC( fmtargnumber )
548#define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
549#define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
550#define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber )
551#else
552#if defined(_MSC_VER) && (_MSC_VER >= 1600) /* VS 2010 and above */
553#include <sal.h>
554
555#define SDL_IN_BYTECAP(x) _In_bytecount_(x)
556#define SDL_INOUT_Z_CAP(x) _Inout_z_cap_(x)
557#define SDL_OUT_Z_CAP(x) _Out_z_cap_(x)
558#define SDL_OUT_CAP(x) _Out_cap_(x)
559#define SDL_OUT_BYTECAP(x) _Out_bytecap_(x)
560#define SDL_OUT_Z_BYTECAP(x) _Out_z_bytecap_(x)
561
562#define SDL_PRINTF_FORMAT_STRING _Printf_format_string_
563#define SDL_SCANF_FORMAT_STRING _Scanf_format_string_impl_
564#else
565#define SDL_IN_BYTECAP(x)
566#define SDL_INOUT_Z_CAP(x)
567#define SDL_OUT_Z_CAP(x)
568#define SDL_OUT_CAP(x)
569#define SDL_OUT_BYTECAP(x)
570#define SDL_OUT_Z_BYTECAP(x)
571#define SDL_PRINTF_FORMAT_STRING
572#define SDL_SCANF_FORMAT_STRING
573#endif
574#if defined(__GNUC__) || defined(__clang__)
575#define SDL_PRINTF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __printf__, fmtargnumber, fmtargnumber+1 )))
576#define SDL_PRINTF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __printf__, fmtargnumber, 0 )))
577#define SDL_SCANF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __scanf__, fmtargnumber, fmtargnumber+1 )))
578#define SDL_SCANF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __scanf__, fmtargnumber, 0 )))
579#define SDL_WPRINTF_VARARG_FUNC( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, fmtargnumber+1 ))) */
580#define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, 0 ))) */
581#else
582#define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
583#define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
584#define SDL_SCANF_VARARG_FUNC( fmtargnumber )
585#define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
586#define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
587#define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber )
588#endif
589#endif /* SDL_DISABLE_ANALYZE_MACROS */
590
591/** \cond */
592#ifndef DOXYGEN_SHOULD_IGNORE_THIS
593SDL_COMPILE_TIME_ASSERT(bool_size, sizeof(bool) == 1);
594SDL_COMPILE_TIME_ASSERT(uint8_size, sizeof(Uint8) == 1);
595SDL_COMPILE_TIME_ASSERT(sint8_size, sizeof(Sint8) == 1);
596SDL_COMPILE_TIME_ASSERT(uint16_size, sizeof(Uint16) == 2);
597SDL_COMPILE_TIME_ASSERT(sint16_size, sizeof(Sint16) == 2);
598SDL_COMPILE_TIME_ASSERT(uint32_size, sizeof(Uint32) == 4);
599SDL_COMPILE_TIME_ASSERT(sint32_size, sizeof(Sint32) == 4);
600SDL_COMPILE_TIME_ASSERT(uint64_size, sizeof(Uint64) == 8);
601SDL_COMPILE_TIME_ASSERT(sint64_size, sizeof(Sint64) == 8);
602SDL_COMPILE_TIME_ASSERT(uint64_longlong, sizeof(Uint64) <= sizeof(unsigned long long));
603SDL_COMPILE_TIME_ASSERT(size_t_longlong, sizeof(size_t) <= sizeof(unsigned long long));
604typedef struct SDL_alignment_test
605{
606 Uint8 a;
607 void *b;
608} SDL_alignment_test;
609SDL_COMPILE_TIME_ASSERT(struct_alignment, sizeof(SDL_alignment_test) == (2 * sizeof(void *)));
610SDL_COMPILE_TIME_ASSERT(two_s_complement, (int)~(int)0 == (int)(-1));
611#endif /* DOXYGEN_SHOULD_IGNORE_THIS */
612/** \endcond */
613
614/* Check to make sure enums are the size of ints, for structure packing.
615 For both Watcom C/C++ and Borland C/C++ the compiler option that makes
616 enums having the size of an int must be enabled.
617 This is "-b" for Borland C/C++ and "-ei" for Watcom C/C++ (v11).
618*/
619
620/** \cond */
621#ifndef DOXYGEN_SHOULD_IGNORE_THIS
622#if !defined(SDL_PLATFORM_VITA) && !defined(SDL_PLATFORM_3DS)
623/* TODO: include/SDL_stdinc.h:390: error: size of array 'SDL_dummy_enum' is negative */
624typedef enum SDL_DUMMY_ENUM
625{
626 DUMMY_ENUM_VALUE
627} SDL_DUMMY_ENUM;
628
629SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
630#endif
631#endif /* DOXYGEN_SHOULD_IGNORE_THIS */
632/** \endcond */
633
634#include <SDL3/SDL_begin_code.h>
635/* Set up for C function definitions, even when using C++ */
636#ifdef __cplusplus
637extern "C" {
638#endif
639
640/**
641 * A macro to initialize an SDL interface.
642 *
643 * This macro will initialize an SDL interface structure and should be called
644 * before you fill out the fields with your implementation.
645 *
646 * You can use it like this:
647 *
648 * ```c
649 * SDL_IOStreamInterface iface;
650 *
651 * SDL_INIT_INTERFACE(&iface);
652 *
653 * // Fill in the interface function pointers with your implementation
654 * iface.seek = ...
655 *
656 * stream = SDL_OpenIO(&iface, NULL);
657 * ```
658 *
659 * If you are using designated initializers, you can use the size of the
660 * interface as the version, e.g.
661 *
662 * ```c
663 * SDL_IOStreamInterface iface = {
664 * .version = sizeof(iface),
665 * .seek = ...
666 * };
667 * stream = SDL_OpenIO(&iface, NULL);
668 * ```
669 *
670 * \threadsafety It is safe to call this macro from any thread.
671 *
672 * \since This macro is available since SDL 3.1.3.
673 *
674 * \sa SDL_IOStreamInterface
675 * \sa SDL_StorageInterface
676 * \sa SDL_VirtualJoystickDesc
677 */
678#define SDL_INIT_INTERFACE(iface) \
679 do { \
680 SDL_zerop(iface); \
681 (iface)->version = sizeof(*(iface)); \
682 } while (0)
683
684
685#ifndef SDL_DISABLE_ALLOCA
686#define SDL_stack_alloc(type, count) (type*)alloca(sizeof(type)*(count))
687#define SDL_stack_free(data)
688#else
689#define SDL_stack_alloc(type, count) (type*)SDL_malloc(sizeof(type)*(count))
690#define SDL_stack_free(data) SDL_free(data)
691#endif
692
693/**
694 * Allocate uninitialized memory.
695 *
696 * The allocated memory returned by this function must be freed with
697 * SDL_free().
698 *
699 * If `size` is 0, it will be set to 1.
700 *
701 * If you want to allocate memory aligned to a specific alignment, consider
702 * using SDL_aligned_alloc().
703 *
704 * \param size the size to allocate.
705 * \returns a pointer to the allocated memory, or NULL if allocation failed.
706 *
707 * \threadsafety It is safe to call this function from any thread.
708 *
709 * \since This function is available since SDL 3.1.3.
710 *
711 * \sa SDL_free
712 * \sa SDL_calloc
713 * \sa SDL_realloc
714 * \sa SDL_aligned_alloc
715 */
716extern SDL_DECLSPEC SDL_MALLOC void * SDLCALL SDL_malloc(size_t size);
717
718/**
719 * Allocate a zero-initialized array.
720 *
721 * The memory returned by this function must be freed with SDL_free().
722 *
723 * If either of `nmemb` or `size` is 0, they will both be set to 1.
724 *
725 * \param nmemb the number of elements in the array.
726 * \param size the size of each element of the array.
727 * \returns a pointer to the allocated array, or NULL if allocation failed.
728 *
729 * \threadsafety It is safe to call this function from any thread.
730 *
731 * \since This function is available since SDL 3.1.3.
732 *
733 * \sa SDL_free
734 * \sa SDL_malloc
735 * \sa SDL_realloc
736 */
737extern SDL_DECLSPEC SDL_MALLOC SDL_ALLOC_SIZE2(1, 2) void * SDLCALL SDL_calloc(size_t nmemb, size_t size);
738
739/**
740 * Change the size of allocated memory.
741 *
742 * The memory returned by this function must be freed with SDL_free().
743 *
744 * If `size` is 0, it will be set to 1. Note that this is unlike some other C
745 * runtime `realloc` implementations, which may treat `realloc(mem, 0)` the
746 * same way as `free(mem)`.
747 *
748 * If `mem` is NULL, the behavior of this function is equivalent to
749 * SDL_malloc(). Otherwise, the function can have one of three possible
750 * outcomes:
751 *
752 * - If it returns the same pointer as `mem`, it means that `mem` was resized
753 * in place without freeing.
754 * - If it returns a different non-NULL pointer, it means that `mem` was freed
755 * and cannot be dereferenced anymore.
756 * - If it returns NULL (indicating failure), then `mem` will remain valid and
757 * must still be freed with SDL_free().
758 *
759 * \param mem a pointer to allocated memory to reallocate, or NULL.
760 * \param size the new size of the memory.
761 * \returns a pointer to the newly allocated memory, or NULL if allocation
762 * failed.
763 *
764 * \threadsafety It is safe to call this function from any thread.
765 *
766 * \since This function is available since SDL 3.1.3.
767 *
768 * \sa SDL_free
769 * \sa SDL_malloc
770 * \sa SDL_calloc
771 */
772extern SDL_DECLSPEC SDL_ALLOC_SIZE(2) void * SDLCALL SDL_realloc(void *mem, size_t size);
773
774/**
775 * Free allocated memory.
776 *
777 * The pointer is no longer valid after this call and cannot be dereferenced
778 * anymore.
779 *
780 * If `mem` is NULL, this function does nothing.
781 *
782 * \param mem a pointer to allocated memory, or NULL.
783 *
784 * \threadsafety It is safe to call this function from any thread.
785 *
786 * \since This function is available since SDL 3.1.3.
787 *
788 * \sa SDL_malloc
789 * \sa SDL_calloc
790 * \sa SDL_realloc
791 */
792extern SDL_DECLSPEC void SDLCALL SDL_free(void *mem);
793
794/**
795 * A callback used to implement SDL_malloc().
796 *
797 * SDL will always ensure that the passed `size` is greater than 0.
798 *
799 * \param size the size to allocate.
800 * \returns a pointer to the allocated memory, or NULL if allocation failed.
801 *
802 * \threadsafety It should be safe to call this callback from any thread.
803 *
804 * \since This datatype is available since SDL 3.1.3.
805 *
806 * \sa SDL_malloc
807 * \sa SDL_GetOriginalMemoryFunctions
808 * \sa SDL_GetMemoryFunctions
809 * \sa SDL_SetMemoryFunctions
810 */
811typedef void *(SDLCALL *SDL_malloc_func)(size_t size);
812
813/**
814 * A callback used to implement SDL_calloc().
815 *
816 * SDL will always ensure that the passed `nmemb` and `size` are both greater
817 * than 0.
818 *
819 * \param nmemb the number of elements in the array.
820 * \param size the size of each element of the array.
821 * \returns a pointer to the allocated array, or NULL if allocation failed.
822 *
823 * \threadsafety It should be safe to call this callback from any thread.
824 *
825 * \since This datatype is available since SDL 3.1.3.
826 *
827 * \sa SDL_calloc
828 * \sa SDL_GetOriginalMemoryFunctions
829 * \sa SDL_GetMemoryFunctions
830 * \sa SDL_SetMemoryFunctions
831 */
832typedef void *(SDLCALL *SDL_calloc_func)(size_t nmemb, size_t size);
833
834/**
835 * A callback used to implement SDL_realloc().
836 *
837 * SDL will always ensure that the passed `size` is greater than 0.
838 *
839 * \param mem a pointer to allocated memory to reallocate, or NULL.
840 * \param size the new size of the memory.
841 * \returns a pointer to the newly allocated memory, or NULL if allocation
842 * failed.
843 *
844 * \threadsafety It should be safe to call this callback from any thread.
845 *
846 * \since This datatype is available since SDL 3.1.3.
847 *
848 * \sa SDL_realloc
849 * \sa SDL_GetOriginalMemoryFunctions
850 * \sa SDL_GetMemoryFunctions
851 * \sa SDL_SetMemoryFunctions
852 */
853typedef void *(SDLCALL *SDL_realloc_func)(void *mem, size_t size);
854
855/**
856 * A callback used to implement SDL_free().
857 *
858 * SDL will always ensure that the passed `mem` is a non-NULL pointer.
859 *
860 * \param mem a pointer to allocated memory.
861 *
862 * \threadsafety It should be safe to call this callback from any thread.
863 *
864 * \since This datatype is available since SDL 3.1.3.
865 *
866 * \sa SDL_free
867 * \sa SDL_GetOriginalMemoryFunctions
868 * \sa SDL_GetMemoryFunctions
869 * \sa SDL_SetMemoryFunctions
870 */
871typedef void (SDLCALL *SDL_free_func)(void *mem);
872
873/**
874 * Get the original set of SDL memory functions.
875 *
876 * This is what SDL_malloc and friends will use by default, if there has been
877 * no call to SDL_SetMemoryFunctions. This is not necessarily using the C
878 * runtime's `malloc` functions behind the scenes! Different platforms and
879 * build configurations might do any number of unexpected things.
880 *
881 * \param malloc_func filled with malloc function.
882 * \param calloc_func filled with calloc function.
883 * \param realloc_func filled with realloc function.
884 * \param free_func filled with free function.
885 *
886 * \threadsafety It is safe to call this function from any thread.
887 *
888 * \since This function is available since SDL 3.1.3.
889 */
890extern SDL_DECLSPEC void SDLCALL SDL_GetOriginalMemoryFunctions(SDL_malloc_func *malloc_func,
891 SDL_calloc_func *calloc_func,
892 SDL_realloc_func *realloc_func,
893 SDL_free_func *free_func);
894
895/**
896 * Get the current set of SDL memory functions.
897 *
898 * \param malloc_func filled with malloc function.
899 * \param calloc_func filled with calloc function.
900 * \param realloc_func filled with realloc function.
901 * \param free_func filled with free function.
902 *
903 * \threadsafety This does not hold a lock, so do not call this in the
904 * unlikely event of a background thread calling
905 * SDL_SetMemoryFunctions simultaneously.
906 *
907 * \since This function is available since SDL 3.1.3.
908 *
909 * \sa SDL_SetMemoryFunctions
910 * \sa SDL_GetOriginalMemoryFunctions
911 */
912extern SDL_DECLSPEC void SDLCALL SDL_GetMemoryFunctions(SDL_malloc_func *malloc_func,
913 SDL_calloc_func *calloc_func,
914 SDL_realloc_func *realloc_func,
915 SDL_free_func *free_func);
916
917/**
918 * Replace SDL's memory allocation functions with a custom set.
919 *
920 * It is not safe to call this function once any allocations have been made,
921 * as future calls to SDL_free will use the new allocator, even if they came
922 * from an SDL_malloc made with the old one!
923 *
924 * If used, usually this needs to be the first call made into the SDL library,
925 * if not the very first thing done at program startup time.
926 *
927 * \param malloc_func custom malloc function.
928 * \param calloc_func custom calloc function.
929 * \param realloc_func custom realloc function.
930 * \param free_func custom free function.
931 * \returns true on success or false on failure; call SDL_GetError() for more
932 * information.
933 *
934 * \threadsafety It is safe to call this function from any thread, but one
935 * should not replace the memory functions once any allocations
936 * are made!
937 *
938 * \since This function is available since SDL 3.1.3.
939 *
940 * \sa SDL_GetMemoryFunctions
941 * \sa SDL_GetOriginalMemoryFunctions
942 */
943extern SDL_DECLSPEC bool SDLCALL SDL_SetMemoryFunctions(SDL_malloc_func malloc_func,
944 SDL_calloc_func calloc_func,
945 SDL_realloc_func realloc_func,
946 SDL_free_func free_func);
947
948/**
949 * Allocate memory aligned to a specific alignment.
950 *
951 * The memory returned by this function must be freed with SDL_aligned_free(),
952 * _not_ SDL_free().
953 *
954 * If `alignment` is less than the size of `void *`, it will be increased to
955 * match that.
956 *
957 * The returned memory address will be a multiple of the alignment value, and
958 * the size of the memory allocated will be a multiple of the alignment value.
959 *
960 * \param alignment the alignment of the memory.
961 * \param size the size to allocate.
962 * \returns a pointer to the aligned memory, or NULL if allocation failed.
963 *
964 * \threadsafety It is safe to call this function from any thread.
965 *
966 * \since This function is available since SDL 3.1.3.
967 *
968 * \sa SDL_aligned_free
969 */
970extern SDL_DECLSPEC SDL_MALLOC void * SDLCALL SDL_aligned_alloc(size_t alignment, size_t size);
971
972/**
973 * Free memory allocated by SDL_aligned_alloc().
974 *
975 * The pointer is no longer valid after this call and cannot be dereferenced
976 * anymore.
977 *
978 * If `mem` is NULL, this function does nothing.
979 *
980 * \param mem a pointer previously returned by SDL_aligned_alloc(), or NULL.
981 *
982 * \threadsafety It is safe to call this function from any thread.
983 *
984 * \since This function is available since SDL 3.1.3.
985 *
986 * \sa SDL_aligned_alloc
987 */
988extern SDL_DECLSPEC void SDLCALL SDL_aligned_free(void *mem);
989
990/**
991 * Get the number of outstanding (unfreed) allocations.
992 *
993 * \returns the number of allocations.
994 *
995 * \threadsafety It is safe to call this function from any thread.
996 *
997 * \since This function is available since SDL 3.1.3.
998 */
999extern SDL_DECLSPEC int SDLCALL SDL_GetNumAllocations(void);
1000
1001/**
1002 * A thread-safe set of environment variables
1003 *
1004 * \since This struct is available since SDL 3.1.3.
1005 *
1006 * \sa SDL_GetEnvironment
1007 * \sa SDL_CreateEnvironment
1008 * \sa SDL_GetEnvironmentVariable
1009 * \sa SDL_GetEnvironmentVariables
1010 * \sa SDL_SetEnvironmentVariable
1011 * \sa SDL_UnsetEnvironmentVariable
1012 * \sa SDL_DestroyEnvironment
1013 */
1015
1016/**
1017 * Get the process environment.
1018 *
1019 * This is initialized at application start and is not affected by setenv()
1020 * and unsetenv() calls after that point. Use SDL_SetEnvironmentVariable() and
1021 * SDL_UnsetEnvironmentVariable() if you want to modify this environment, or
1022 * SDL_setenv_unsafe() or SDL_unsetenv_unsafe() if you want changes to persist
1023 * in the C runtime environment after SDL_Quit().
1024 *
1025 * \returns a pointer to the environment for the process or NULL on failure;
1026 * call SDL_GetError() for more information.
1027 *
1028 * \threadsafety It is safe to call this function from any thread.
1029 *
1030 * \since This function is available since SDL 3.1.3.
1031 *
1032 * \sa SDL_GetEnvironmentVariable
1033 * \sa SDL_GetEnvironmentVariables
1034 * \sa SDL_SetEnvironmentVariable
1035 * \sa SDL_UnsetEnvironmentVariable
1036 */
1037extern SDL_DECLSPEC SDL_Environment * SDLCALL SDL_GetEnvironment(void);
1038
1039/**
1040 * Create a set of environment variables
1041 *
1042 * \param populated true to initialize it from the C runtime environment,
1043 * false to create an empty environment.
1044 * \returns a pointer to the new environment or NULL on failure; call
1045 * SDL_GetError() for more information.
1046 *
1047 * \threadsafety If `populated` is false, it is safe to call this function
1048 * from any thread, otherwise it is safe if no other threads are
1049 * calling setenv() or unsetenv()
1050 *
1051 * \since This function is available since SDL 3.1.3.
1052 *
1053 * \sa SDL_GetEnvironmentVariable
1054 * \sa SDL_GetEnvironmentVariables
1055 * \sa SDL_SetEnvironmentVariable
1056 * \sa SDL_UnsetEnvironmentVariable
1057 * \sa SDL_DestroyEnvironment
1058 */
1059extern SDL_DECLSPEC SDL_Environment * SDLCALL SDL_CreateEnvironment(bool populated);
1060
1061/**
1062 * Get the value of a variable in the environment.
1063 *
1064 * \param env the environment to query.
1065 * \param name the name of the variable to get.
1066 * \returns a pointer to the value of the variable or NULL if it can't be
1067 * found.
1068 *
1069 * \threadsafety It is safe to call this function from any thread.
1070 *
1071 * \since This function is available since SDL 3.1.3.
1072 *
1073 * \sa SDL_GetEnvironment
1074 * \sa SDL_CreateEnvironment
1075 * \sa SDL_GetEnvironmentVariables
1076 * \sa SDL_SetEnvironmentVariable
1077 * \sa SDL_UnsetEnvironmentVariable
1078 */
1079extern SDL_DECLSPEC const char * SDLCALL SDL_GetEnvironmentVariable(SDL_Environment *env, const char *name);
1080
1081/**
1082 * Get all variables in the environment.
1083 *
1084 * \param env the environment to query.
1085 * \returns a NULL terminated array of pointers to environment variables in
1086 * the form "variable=value" or NULL on failure; call SDL_GetError()
1087 * for more information. This is a single allocation that should be
1088 * freed with SDL_free() when it is no longer needed.
1089 *
1090 * \threadsafety It is safe to call this function from any thread.
1091 *
1092 * \since This function is available since SDL 3.1.3.
1093 *
1094 * \sa SDL_GetEnvironment
1095 * \sa SDL_CreateEnvironment
1096 * \sa SDL_GetEnvironmentVariables
1097 * \sa SDL_SetEnvironmentVariable
1098 * \sa SDL_UnsetEnvironmentVariable
1099 */
1100extern SDL_DECLSPEC char ** SDLCALL SDL_GetEnvironmentVariables(SDL_Environment *env);
1101
1102/**
1103 * Set the value of a variable in the environment.
1104 *
1105 * \param env the environment to modify.
1106 * \param name the name of the variable to set.
1107 * \param value the value of the variable to set.
1108 * \param overwrite true to overwrite the variable if it exists, false to
1109 * return success without setting the variable if it already
1110 * exists.
1111 * \returns true on success or false on failure; call SDL_GetError() for more
1112 * information.
1113 *
1114 * \threadsafety It is safe to call this function from any thread.
1115 *
1116 * \since This function is available since SDL 3.1.3.
1117 *
1118 * \sa SDL_GetEnvironment
1119 * \sa SDL_CreateEnvironment
1120 * \sa SDL_GetEnvironmentVariable
1121 * \sa SDL_GetEnvironmentVariables
1122 * \sa SDL_UnsetEnvironmentVariable
1123 */
1124extern SDL_DECLSPEC bool SDLCALL SDL_SetEnvironmentVariable(SDL_Environment *env, const char *name, const char *value, bool overwrite);
1125
1126/**
1127 * Clear a variable from the environment.
1128 *
1129 * \param env the environment to modify.
1130 * \param name the name of the variable to unset.
1131 * \returns true on success or false on failure; call SDL_GetError() for more
1132 * information.
1133 *
1134 * \threadsafety It is safe to call this function from any thread.
1135 *
1136 * \since This function is available since SDL 3.1.3.
1137 *
1138 * \sa SDL_GetEnvironment
1139 * \sa SDL_CreateEnvironment
1140 * \sa SDL_GetEnvironmentVariable
1141 * \sa SDL_GetEnvironmentVariables
1142 * \sa SDL_SetEnvironmentVariable
1143 * \sa SDL_UnsetEnvironmentVariable
1144 */
1145extern SDL_DECLSPEC bool SDLCALL SDL_UnsetEnvironmentVariable(SDL_Environment *env, const char *name);
1146
1147/**
1148 * Destroy a set of environment variables.
1149 *
1150 * \param env the environment to destroy.
1151 *
1152 * \threadsafety It is safe to call this function from any thread, as long as
1153 * the environment is no longer in use.
1154 *
1155 * \since This function is available since SDL 3.1.3.
1156 *
1157 * \sa SDL_CreateEnvironment
1158 */
1159extern SDL_DECLSPEC void SDLCALL SDL_DestroyEnvironment(SDL_Environment *env);
1160
1161/**
1162 * Get the value of a variable in the environment.
1163 *
1164 * This function uses SDL's cached copy of the environment and is thread-safe.
1165 *
1166 * \param name the name of the variable to get.
1167 * \returns a pointer to the value of the variable or NULL if it can't be
1168 * found.
1169 *
1170 * \threadsafety It is safe to call this function from any thread.
1171 *
1172 * \since This function is available since SDL 3.1.3.
1173 */
1174extern SDL_DECLSPEC const char * SDLCALL SDL_getenv(const char *name);
1175
1176/**
1177 * Get the value of a variable in the environment.
1178 *
1179 * This function bypasses SDL's cached copy of the environment and is not
1180 * thread-safe.
1181 *
1182 * \param name the name of the variable to get.
1183 * \returns a pointer to the value of the variable or NULL if it can't be
1184 * found.
1185 *
1186 * \threadsafety This function is not thread safe, consider using SDL_getenv()
1187 * instead.
1188 *
1189 * \since This function is available since SDL 3.1.3.
1190 *
1191 * \sa SDL_getenv
1192 */
1193extern SDL_DECLSPEC const char * SDLCALL SDL_getenv_unsafe(const char *name);
1194
1195/**
1196 * Set the value of a variable in the environment.
1197 *
1198 * \param name the name of the variable to set.
1199 * \param value the value of the variable to set.
1200 * \param overwrite 1 to overwrite the variable if it exists, 0 to return
1201 * success without setting the variable if it already exists.
1202 * \returns 0 on success, -1 on error.
1203 *
1204 * \threadsafety This function is not thread safe, consider using
1205 * SDL_SetEnvironmentVariable() instead.
1206 *
1207 * \since This function is available since SDL 3.1.3.
1208 *
1209 * \sa SDL_SetEnvironmentVariable
1210 */
1211extern SDL_DECLSPEC int SDLCALL SDL_setenv_unsafe(const char *name, const char *value, int overwrite);
1212
1213/**
1214 * Clear a variable from the environment.
1215 *
1216 * \param name the name of the variable to unset.
1217 * \returns 0 on success, -1 on error.
1218 *
1219 * \threadsafety This function is not thread safe, consider using
1220 * SDL_UnsetEnvironmentVariable() instead.
1221 *
1222 * \since This function is available since SDL 3.1.3.
1223 *
1224 * \sa SDL_UnsetEnvironmentVariable
1225 */
1226extern SDL_DECLSPEC int SDLCALL SDL_unsetenv_unsafe(const char *name);
1227
1228/**
1229 * A callback used with SDL sorting and binary search functions.
1230 *
1231 * \param a a pointer to the first element being compared.
1232 * \param b a pointer to the second element being compared.
1233 * \returns -1 if `a` should be sorted before `b`, 1 if `b` should be sorted
1234 * before `a`, 0 if they are equal. If two elements are equal, their
1235 * order in the sorted array is undefined.
1236 *
1237 * \since This callback is available since SDL 3.1.3.
1238 *
1239 * \sa SDL_bsearch
1240 * \sa SDL_qsort
1241 */
1242typedef int (SDLCALL *SDL_CompareCallback)(const void *a, const void *b);
1243
1244/**
1245 * Sort an array.
1246 *
1247 * For example:
1248 *
1249 * ```c
1250 * typedef struct {
1251 * int key;
1252 * const char *string;
1253 * } data;
1254 *
1255 * int SDLCALL compare(const void *a, const void *b)
1256 * {
1257 * const data *A = (const data *)a;
1258 * const data *B = (const data *)b;
1259 *
1260 * if (A->n < B->n) {
1261 * return -1;
1262 * } else if (B->n < A->n) {
1263 * return 1;
1264 * } else {
1265 * return 0;
1266 * }
1267 * }
1268 *
1269 * data values[] = {
1270 * { 3, "third" }, { 1, "first" }, { 2, "second" }
1271 * };
1272 *
1273 * SDL_qsort(values, SDL_arraysize(values), sizeof(values[0]), compare);
1274 * ```
1275 *
1276 * \param base a pointer to the start of the array.
1277 * \param nmemb the number of elements in the array.
1278 * \param size the size of the elements in the array.
1279 * \param compare a function used to compare elements in the array.
1280 *
1281 * \threadsafety It is safe to call this function from any thread.
1282 *
1283 * \since This function is available since SDL 3.1.3.
1284 *
1285 * \sa SDL_bsearch
1286 * \sa SDL_qsort_r
1287 */
1288extern SDL_DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, SDL_CompareCallback compare);
1289
1290/**
1291 * Perform a binary search on a previously sorted array.
1292 *
1293 * For example:
1294 *
1295 * ```c
1296 * typedef struct {
1297 * int key;
1298 * const char *string;
1299 * } data;
1300 *
1301 * int SDLCALL compare(const void *a, const void *b)
1302 * {
1303 * const data *A = (const data *)a;
1304 * const data *B = (const data *)b;
1305 *
1306 * if (A->n < B->n) {
1307 * return -1;
1308 * } else if (B->n < A->n) {
1309 * return 1;
1310 * } else {
1311 * return 0;
1312 * }
1313 * }
1314 *
1315 * data values[] = {
1316 * { 1, "first" }, { 2, "second" }, { 3, "third" }
1317 * };
1318 * data key = { 2, NULL };
1319 *
1320 * data *result = SDL_bsearch(&key, values, SDL_arraysize(values), sizeof(values[0]), compare);
1321 * ```
1322 *
1323 * \param key a pointer to a key equal to the element being searched for.
1324 * \param base a pointer to the start of the array.
1325 * \param nmemb the number of elements in the array.
1326 * \param size the size of the elements in the array.
1327 * \param compare a function used to compare elements in the array.
1328 * \returns a pointer to the matching element in the array, or NULL if not
1329 * found.
1330 *
1331 * \threadsafety It is safe to call this function from any thread.
1332 *
1333 * \since This function is available since SDL 3.1.3.
1334 *
1335 * \sa SDL_bsearch_r
1336 * \sa SDL_qsort
1337 */
1338extern SDL_DECLSPEC void * SDLCALL SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback compare);
1339
1340/**
1341 * A callback used with SDL sorting and binary search functions.
1342 *
1343 * \param userdata the `userdata` pointer passed to the sort function.
1344 * \param a a pointer to the first element being compared.
1345 * \param b a pointer to the second element being compared.
1346 * \returns -1 if `a` should be sorted before `b`, 1 if `b` should be sorted
1347 * before `a`, 0 if they are equal. If two elements are equal, their
1348 * order in the sorted array is undefined.
1349 *
1350 * \since This callback is available since SDL 3.1.3.
1351 *
1352 * \sa SDL_qsort_r
1353 * \sa SDL_bsearch_r
1354 */
1355typedef int (SDLCALL *SDL_CompareCallback_r)(void *userdata, const void *a, const void *b);
1356
1357/**
1358 * Sort an array, passing a userdata pointer to the compare function.
1359 *
1360 * For example:
1361 *
1362 * ```c
1363 * typedef enum {
1364 * sort_increasing,
1365 * sort_decreasing,
1366 * } sort_method;
1367 *
1368 * typedef struct {
1369 * int key;
1370 * const char *string;
1371 * } data;
1372 *
1373 * int SDLCALL compare(const void *userdata, const void *a, const void *b)
1374 * {
1375 * sort_method method = (sort_method)(uintptr_t)userdata;
1376 * const data *A = (const data *)a;
1377 * const data *B = (const data *)b;
1378 *
1379 * if (A->key < B->key) {
1380 * return (method == sort_increasing) ? -1 : 1;
1381 * } else if (B->key < A->key) {
1382 * return (method == sort_increasing) ? 1 : -1;
1383 * } else {
1384 * return 0;
1385 * }
1386 * }
1387 *
1388 * data values[] = {
1389 * { 3, "third" }, { 1, "first" }, { 2, "second" }
1390 * };
1391 *
1392 * SDL_qsort_r(values, SDL_arraysize(values), sizeof(values[0]), compare, (const void *)(uintptr_t)sort_increasing);
1393 * ```
1394 *
1395 * \param base a pointer to the start of the array.
1396 * \param nmemb the number of elements in the array.
1397 * \param size the size of the elements in the array.
1398 * \param compare a function used to compare elements in the array.
1399 * \param userdata a pointer to pass to the compare function.
1400 *
1401 * \threadsafety It is safe to call this function from any thread.
1402 *
1403 * \since This function is available since SDL 3.1.3.
1404 *
1405 * \sa SDL_bsearch_r
1406 * \sa SDL_qsort
1407 */
1408extern SDL_DECLSPEC void SDLCALL SDL_qsort_r(void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata);
1409
1410/**
1411 * Perform a binary search on a previously sorted array, passing a userdata
1412 * pointer to the compare function.
1413 *
1414 * For example:
1415 *
1416 * ```c
1417 * typedef enum {
1418 * sort_increasing,
1419 * sort_decreasing,
1420 * } sort_method;
1421 *
1422 * typedef struct {
1423 * int key;
1424 * const char *string;
1425 * } data;
1426 *
1427 * int SDLCALL compare(const void *userdata, const void *a, const void *b)
1428 * {
1429 * sort_method method = (sort_method)(uintptr_t)userdata;
1430 * const data *A = (const data *)a;
1431 * const data *B = (const data *)b;
1432 *
1433 * if (A->key < B->key) {
1434 * return (method == sort_increasing) ? -1 : 1;
1435 * } else if (B->key < A->key) {
1436 * return (method == sort_increasing) ? 1 : -1;
1437 * } else {
1438 * return 0;
1439 * }
1440 * }
1441 *
1442 * data values[] = {
1443 * { 1, "first" }, { 2, "second" }, { 3, "third" }
1444 * };
1445 * data key = { 2, NULL };
1446 *
1447 * data *result = SDL_bsearch_r(&key, values, SDL_arraysize(values), sizeof(values[0]), compare, (const void *)(uintptr_t)sort_increasing);
1448 * ```
1449 *
1450 * \param key a pointer to a key equal to the element being searched for.
1451 * \param base a pointer to the start of the array.
1452 * \param nmemb the number of elements in the array.
1453 * \param size the size of the elements in the array.
1454 * \param compare a function used to compare elements in the array.
1455 * \param userdata a pointer to pass to the compare function.
1456 * \returns a pointer to the matching element in the array, or NULL if not
1457 * found.
1458 *
1459 * \threadsafety It is safe to call this function from any thread.
1460 *
1461 * \since This function is available since SDL 3.1.3.
1462 *
1463 * \sa SDL_bsearch
1464 * \sa SDL_qsort_r
1465 */
1466extern SDL_DECLSPEC void * SDLCALL SDL_bsearch_r(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata);
1467
1468extern SDL_DECLSPEC int SDLCALL SDL_abs(int x);
1469
1470/* NOTE: these double-evaluate their arguments, so you should never have side effects in the parameters */
1471#define SDL_min(x, y) (((x) < (y)) ? (x) : (y))
1472#define SDL_max(x, y) (((x) > (y)) ? (x) : (y))
1473#define SDL_clamp(x, a, b) (((x) < (a)) ? (a) : (((x) > (b)) ? (b) : (x)))
1474
1475/**
1476 * Query if a character is alphabetic (a letter).
1477 *
1478 * **WARNING**: Regardless of system locale, this will only treat ASCII values
1479 * for English 'a-z' and 'A-Z' as true.
1480 *
1481 * \param x character value to check.
1482 * \returns non-zero if x falls within the character class, zero otherwise.
1483 *
1484 * \threadsafety It is safe to call this function from any thread.
1485 *
1486 * \since This function is available since SDL 3.1.3.
1487 */
1488extern SDL_DECLSPEC int SDLCALL SDL_isalpha(int x);
1489
1490/**
1491 * Query if a character is alphabetic (a letter) or a number.
1492 *
1493 * **WARNING**: Regardless of system locale, this will only treat ASCII values
1494 * for English 'a-z', 'A-Z', and '0-9' as true.
1495 *
1496 * \param x character value to check.
1497 * \returns non-zero if x falls within the character class, zero otherwise.
1498 *
1499 * \threadsafety It is safe to call this function from any thread.
1500 *
1501 * \since This function is available since SDL 3.1.3.
1502 */
1503extern SDL_DECLSPEC int SDLCALL SDL_isalnum(int x);
1504
1505/**
1506 * Report if a character is blank (a space or tab).
1507 *
1508 * **WARNING**: Regardless of system locale, this will only treat ASCII values
1509 * 0x20 (space) or 0x9 (tab) as true.
1510 *
1511 * \param x character value to check.
1512 * \returns non-zero if x falls within the character class, zero otherwise.
1513 *
1514 * \threadsafety It is safe to call this function from any thread.
1515 *
1516 * \since This function is available since SDL 3.1.3.
1517 */
1518extern SDL_DECLSPEC int SDLCALL SDL_isblank(int x);
1519
1520/**
1521 * Report if a character is a control character.
1522 *
1523 * **WARNING**: Regardless of system locale, this will only treat ASCII values
1524 * 0 through 0x1F, and 0x7F, as true.
1525 *
1526 * \param x character value to check.
1527 * \returns non-zero if x falls within the character class, zero otherwise.
1528 *
1529 * \threadsafety It is safe to call this function from any thread.
1530 *
1531 * \since This function is available since SDL 3.1.3.
1532 */
1533extern SDL_DECLSPEC int SDLCALL SDL_iscntrl(int x);
1534
1535/**
1536 * Report if a character is a numeric digit.
1537 *
1538 * **WARNING**: Regardless of system locale, this will only treat ASCII values
1539 * '0' (0x30) through '9' (0x39), as true.
1540 *
1541 * \param x character value to check.
1542 * \returns non-zero if x falls within the character class, zero otherwise.
1543 *
1544 * \threadsafety It is safe to call this function from any thread.
1545 *
1546 * \since This function is available since SDL 3.1.3.
1547 */
1548extern SDL_DECLSPEC int SDLCALL SDL_isdigit(int x);
1549
1550/**
1551 * Report if a character is a hexadecimal digit.
1552 *
1553 * **WARNING**: Regardless of system locale, this will only treat ASCII values
1554 * 'A' through 'F', 'a' through 'f', and '0' through '9', as true.
1555 *
1556 * \param x character value to check.
1557 * \returns non-zero if x falls within the character class, zero otherwise.
1558 *
1559 * \threadsafety It is safe to call this function from any thread.
1560 *
1561 * \since This function is available since SDL 3.1.3.
1562 */
1563extern SDL_DECLSPEC int SDLCALL SDL_isxdigit(int x);
1564
1565/**
1566 * Report if a character is a punctuation mark.
1567 *
1568 * **WARNING**: Regardless of system locale, this is equivalent to
1569 * `((SDL_isgraph(x)) && (!SDL_isalnum(x)))`.
1570 *
1571 * \param x character value to check.
1572 * \returns non-zero if x falls within the character class, zero otherwise.
1573 *
1574 * \threadsafety It is safe to call this function from any thread.
1575 *
1576 * \since This function is available since SDL 3.1.3.
1577 *
1578 * \sa SDL_isgraph
1579 * \sa SDL_isalnum
1580 */
1581extern SDL_DECLSPEC int SDLCALL SDL_ispunct(int x);
1582
1583/**
1584 * Report if a character is whitespace.
1585 *
1586 * **WARNING**: Regardless of system locale, this will only treat the
1587 * following ASCII values as true:
1588 *
1589 * - space (0x20)
1590 * - tab (0x09)
1591 * - newline (0x0A)
1592 * - vertical tab (0x0B)
1593 * - form feed (0x0C)
1594 * - return (0x0D)
1595 *
1596 * \param x character value to check.
1597 * \returns non-zero if x falls within the character class, zero otherwise.
1598 *
1599 * \threadsafety It is safe to call this function from any thread.
1600 *
1601 * \since This function is available since SDL 3.1.3.
1602 */
1603extern SDL_DECLSPEC int SDLCALL SDL_isspace(int x);
1604
1605/**
1606 * Report if a character is upper case.
1607 *
1608 * **WARNING**: Regardless of system locale, this will only treat ASCII values
1609 * 'A' through 'Z' as true.
1610 *
1611 * \param x character value to check.
1612 * \returns non-zero if x falls within the character class, zero otherwise.
1613 *
1614 * \threadsafety It is safe to call this function from any thread.
1615 *
1616 * \since This function is available since SDL 3.1.3.
1617 */
1618extern SDL_DECLSPEC int SDLCALL SDL_isupper(int x);
1619
1620/**
1621 * Report if a character is lower case.
1622 *
1623 * **WARNING**: Regardless of system locale, this will only treat ASCII values
1624 * 'a' through 'z' as true.
1625 *
1626 * \param x character value to check.
1627 * \returns non-zero if x falls within the character class, zero otherwise.
1628 *
1629 * \threadsafety It is safe to call this function from any thread.
1630 *
1631 * \since This function is available since SDL 3.1.3.
1632 */
1633extern SDL_DECLSPEC int SDLCALL SDL_islower(int x);
1634
1635/**
1636 * Report if a character is "printable".
1637 *
1638 * Be advised that "printable" has a definition that goes back to text
1639 * terminals from the dawn of computing, making this a sort of special case
1640 * function that is not suitable for Unicode (or most any) text management.
1641 *
1642 * **WARNING**: Regardless of system locale, this will only treat ASCII values
1643 * ' ' (0x20) through '~' (0x7E) as true.
1644 *
1645 * \param x character value to check.
1646 * \returns non-zero if x falls within the character class, zero otherwise.
1647 *
1648 * \threadsafety It is safe to call this function from any thread.
1649 *
1650 * \since This function is available since SDL 3.1.3.
1651 */
1652extern SDL_DECLSPEC int SDLCALL SDL_isprint(int x);
1653
1654/**
1655 * Report if a character is any "printable" except space.
1656 *
1657 * Be advised that "printable" has a definition that goes back to text
1658 * terminals from the dawn of computing, making this a sort of special case
1659 * function that is not suitable for Unicode (or most any) text management.
1660 *
1661 * **WARNING**: Regardless of system locale, this is equivalent to
1662 * `(SDL_isprint(x)) && ((x) != ' ')`.
1663 *
1664 * \param x character value to check.
1665 * \returns non-zero if x falls within the character class, zero otherwise.
1666 *
1667 * \threadsafety It is safe to call this function from any thread.
1668 *
1669 * \since This function is available since SDL 3.1.3.
1670 *
1671 * \sa SDL_isprint
1672 */
1673extern SDL_DECLSPEC int SDLCALL SDL_isgraph(int x);
1674
1675/**
1676 * Convert low-ASCII English letters to uppercase.
1677 *
1678 * **WARNING**: Regardless of system locale, this will only convert ASCII
1679 * values 'a' through 'z' to uppercase.
1680 *
1681 * This function returns the uppercase equivalent of `x`. If a character
1682 * cannot be converted, or is already uppercase, this function returns `x`.
1683 *
1684 * \param x character value to check.
1685 * \returns capitalized version of x, or x if no conversion available.
1686 *
1687 * \threadsafety It is safe to call this function from any thread.
1688 *
1689 * \since This function is available since SDL 3.1.3.
1690 */
1691extern SDL_DECLSPEC int SDLCALL SDL_toupper(int x);
1692
1693/**
1694 * Convert low-ASCII English letters to lowercase.
1695 *
1696 * **WARNING**: Regardless of system locale, this will only convert ASCII
1697 * values 'A' through 'Z' to lowercase.
1698 *
1699 * This function returns the lowercase equivalent of `x`. If a character
1700 * cannot be converted, or is already lowercase, this function returns `x`.
1701 *
1702 * \param x character value to check.
1703 * \returns lowercase version of x, or x if no conversion available.
1704 *
1705 * \threadsafety It is safe to call this function from any thread.
1706 *
1707 * \since This function is available since SDL 3.1.3.
1708 */
1709extern SDL_DECLSPEC int SDLCALL SDL_tolower(int x);
1710
1711extern SDL_DECLSPEC Uint16 SDLCALL SDL_crc16(Uint16 crc, const void *data, size_t len);
1712extern SDL_DECLSPEC Uint32 SDLCALL SDL_crc32(Uint32 crc, const void *data, size_t len);
1713extern SDL_DECLSPEC Uint32 SDLCALL SDL_murmur3_32(const void *data, size_t len, Uint32 seed);
1714
1715/**
1716 * Copy non-overlapping memory.
1717 *
1718 * The memory regions must not overlap. If they do, use SDL_memmove() instead.
1719 *
1720 * \param dst The destination memory region. Must not be NULL, and must not
1721 * overlap with `src`.
1722 * \param src The source memory region. Must not be NULL, and must not overlap
1723 * with `dst`.
1724 * \param len The length in bytes of both `dst` and `src`.
1725 * \returns `dst`.
1726 *
1727 * \threadsafety It is safe to call this function from any thread.
1728 *
1729 * \since This function is available since SDL 3.1.3.
1730 *
1731 * \sa SDL_memmove
1732 */
1733extern SDL_DECLSPEC void * SDLCALL SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
1734
1735/* Take advantage of compiler optimizations for memcpy */
1736#ifndef SDL_SLOW_MEMCPY
1737#ifdef SDL_memcpy
1738#undef SDL_memcpy
1739#endif
1740#define SDL_memcpy memcpy
1741#endif
1742
1743#define SDL_copyp(dst, src) \
1744 { SDL_COMPILE_TIME_ASSERT(SDL_copyp, sizeof (*(dst)) == sizeof (*(src))); } \
1745 SDL_memcpy((dst), (src), sizeof(*(src)))
1746
1747/**
1748 * Copy memory.
1749 *
1750 * It is okay for the memory regions to overlap. If you are confident that the
1751 * regions never overlap, using SDL_memcpy() may improve performance.
1752 *
1753 * \param dst The destination memory region. Must not be NULL.
1754 * \param src The source memory region. Must not be NULL.
1755 * \param len The length in bytes of both `dst` and `src`.
1756 * \returns `dst`.
1757 *
1758 * \threadsafety It is safe to call this function from any thread.
1759 *
1760 * \since This function is available since SDL 3.1.3.
1761 *
1762 * \sa SDL_memcpy
1763 */
1764extern SDL_DECLSPEC void * SDLCALL SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
1765
1766/* Take advantage of compiler optimizations for memmove */
1767#ifndef SDL_SLOW_MEMMOVE
1768#ifdef SDL_memmove
1769#undef SDL_memmove
1770#endif
1771#define SDL_memmove memmove
1772#endif
1773
1774extern SDL_DECLSPEC void * SDLCALL SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len);
1775extern SDL_DECLSPEC void * SDLCALL SDL_memset4(void *dst, Uint32 val, size_t dwords);
1776
1777/* Take advantage of compiler optimizations for memset */
1778#ifndef SDL_SLOW_MEMSET
1779#ifdef SDL_memset
1780#undef SDL_memset
1781#endif
1782#define SDL_memset memset
1783#endif
1784
1785#define SDL_zero(x) SDL_memset(&(x), 0, sizeof((x)))
1786#define SDL_zerop(x) SDL_memset((x), 0, sizeof(*(x)))
1787#define SDL_zeroa(x) SDL_memset((x), 0, sizeof((x)))
1788
1789extern SDL_DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
1790
1791extern SDL_DECLSPEC size_t SDLCALL SDL_wcslen(const wchar_t *wstr);
1792extern SDL_DECLSPEC size_t SDLCALL SDL_wcsnlen(const wchar_t *wstr, size_t maxlen);
1793
1794/**
1795 * Copy a wide string.
1796 *
1797 * This function copies `maxlen` - 1 wide characters from `src` to `dst`, then
1798 * appends a null terminator.
1799 *
1800 * `src` and `dst` must not overlap.
1801 *
1802 * If `maxlen` is 0, no wide characters are copied and no null terminator is
1803 * written.
1804 *
1805 * \param dst The destination buffer. Must not be NULL, and must not overlap
1806 * with `src`.
1807 * \param src The null-terminated wide string to copy. Must not be NULL, and
1808 * must not overlap with `dst`.
1809 * \param maxlen The length (in wide characters) of the destination buffer.
1810 * \returns The length (in wide characters, excluding the null terminator) of
1811 * `src`.
1812 *
1813 * \threadsafety It is safe to call this function from any thread.
1814 *
1815 * \since This function is available since SDL 3.1.3.
1816 *
1817 * \sa SDL_wcslcat
1818 */
1819extern SDL_DECLSPEC size_t SDLCALL SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
1820
1821/**
1822 * Concatenate wide strings.
1823 *
1824 * This function appends up to `maxlen` - SDL_wcslen(dst) - 1 wide characters
1825 * from `src` to the end of the wide string in `dst`, then appends a null
1826 * terminator.
1827 *
1828 * `src` and `dst` must not overlap.
1829 *
1830 * If `maxlen` - SDL_wcslen(dst) - 1 is less than or equal to 0, then `dst` is
1831 * unmodified.
1832 *
1833 * \param dst The destination buffer already containing the first
1834 * null-terminated wide string. Must not be NULL and must not
1835 * overlap with `src`.
1836 * \param src The second null-terminated wide string. Must not be NULL, and
1837 * must not overlap with `dst`.
1838 * \param maxlen The length (in wide characters) of the destination buffer.
1839 * \returns The length (in wide characters, excluding the null terminator) of
1840 * the string in `dst` plus the length of `src`.
1841 *
1842 * \threadsafety It is safe to call this function from any thread.
1843 *
1844 * \since This function is available since SDL 3.1.3.
1845 *
1846 * \sa SDL_wcslcpy
1847 */
1848extern SDL_DECLSPEC size_t SDLCALL SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
1849
1850extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsdup(const wchar_t *wstr);
1851extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle);
1852extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsnstr(const wchar_t *haystack, const wchar_t *needle, size_t maxlen);
1853
1854/**
1855 * Compare two null-terminated wide strings.
1856 *
1857 * This only compares wchar_t values until it hits a null-terminating
1858 * character; it does not care if the string is well-formed UTF-16 (or UTF-32,
1859 * depending on your platform's wchar_t size), or uses valid Unicode values.
1860 *
1861 * \param str1 the first string to compare. NULL is not permitted!
1862 * \param str2 the second string to compare. NULL is not permitted!
1863 * \returns less than zero if str1 is "less than" str2, greater than zero if
1864 * str1 is "greater than" str2, and zero if the strings match
1865 * exactly.
1866 *
1867 * \threadsafety It is safe to call this function from any thread.
1868 *
1869 * \since This function is available since SDL 3.1.3.
1870 */
1871extern SDL_DECLSPEC int SDLCALL SDL_wcscmp(const wchar_t *str1, const wchar_t *str2);
1872
1873/**
1874 * Compare two wide strings up to a number of wchar_t values.
1875 *
1876 * This only compares wchar_t values; it does not care if the string is
1877 * well-formed UTF-16 (or UTF-32, depending on your platform's wchar_t size),
1878 * or uses valid Unicode values.
1879 *
1880 * Note that while this function is intended to be used with UTF-16 (or
1881 * UTF-32, depending on your platform's definition of wchar_t), it is
1882 * comparing raw wchar_t values and not Unicode codepoints: `maxlen` specifies
1883 * a wchar_t limit! If the limit lands in the middle of a multi-wchar UTF-16
1884 * sequence, it will only compare a portion of the final character.
1885 *
1886 * `maxlen` specifies a maximum number of wchar_t to compare; if the strings
1887 * match to this number of wide chars (or both have matched to a
1888 * null-terminator character before this count), they will be considered
1889 * equal.
1890 *
1891 * \param str1 the first string to compare. NULL is not permitted!
1892 * \param str2 the second string to compare. NULL is not permitted!
1893 * \param maxlen the maximum number of wchar_t to compare.
1894 * \returns less than zero if str1 is "less than" str2, greater than zero if
1895 * str1 is "greater than" str2, and zero if the strings match
1896 * exactly.
1897 *
1898 * \threadsafety It is safe to call this function from any thread.
1899 *
1900 * \since This function is available since SDL 3.1.3.
1901 */
1902extern SDL_DECLSPEC int SDLCALL SDL_wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
1903
1904/**
1905 * Compare two null-terminated wide strings, case-insensitively.
1906 *
1907 * This will work with Unicode strings, using a technique called
1908 * "case-folding" to handle the vast majority of case-sensitive human
1909 * languages regardless of system locale. It can deal with expanding values: a
1910 * German Eszett character can compare against two ASCII 's' chars and be
1911 * considered a match, for example. A notable exception: it does not handle
1912 * the Turkish 'i' character; human language is complicated!
1913 *
1914 * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
1915 * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
1916 * handles Unicode, it expects the string to be well-formed and not a
1917 * null-terminated string of arbitrary bytes. Characters that are not valid
1918 * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
1919 * CHARACTER), which is to say two strings of random bits may turn out to
1920 * match if they convert to the same amount of replacement characters.
1921 *
1922 * \param str1 the first string to compare. NULL is not permitted!
1923 * \param str2 the second string to compare. NULL is not permitted!
1924 * \returns less than zero if str1 is "less than" str2, greater than zero if
1925 * str1 is "greater than" str2, and zero if the strings match
1926 * exactly.
1927 *
1928 * \threadsafety It is safe to call this function from any thread.
1929 *
1930 * \since This function is available since SDL 3.1.3.
1931 */
1932extern SDL_DECLSPEC int SDLCALL SDL_wcscasecmp(const wchar_t *str1, const wchar_t *str2);
1933
1934/**
1935 * Compare two wide strings, case-insensitively, up to a number of wchar_t.
1936 *
1937 * This will work with Unicode strings, using a technique called
1938 * "case-folding" to handle the vast majority of case-sensitive human
1939 * languages regardless of system locale. It can deal with expanding values: a
1940 * German Eszett character can compare against two ASCII 's' chars and be
1941 * considered a match, for example. A notable exception: it does not handle
1942 * the Turkish 'i' character; human language is complicated!
1943 *
1944 * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
1945 * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
1946 * handles Unicode, it expects the string to be well-formed and not a
1947 * null-terminated string of arbitrary bytes. Characters that are not valid
1948 * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
1949 * CHARACTER), which is to say two strings of random bits may turn out to
1950 * match if they convert to the same amount of replacement characters.
1951 *
1952 * Note that while this function might deal with variable-sized characters,
1953 * `maxlen` specifies a _wchar_ limit! If the limit lands in the middle of a
1954 * multi-byte UTF-16 sequence, it may convert a portion of the final character
1955 * to one or more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not
1956 * to overflow a buffer.
1957 *
1958 * `maxlen` specifies a maximum number of wchar_t values to compare; if the
1959 * strings match to this number of wchar_t (or both have matched to a
1960 * null-terminator character before this number of bytes), they will be
1961 * considered equal.
1962 *
1963 * \param str1 the first string to compare. NULL is not permitted!
1964 * \param str2 the second string to compare. NULL is not permitted!
1965 * \param maxlen the maximum number of wchar_t values to compare.
1966 * \returns less than zero if str1 is "less than" str2, greater than zero if
1967 * str1 is "greater than" str2, and zero if the strings match
1968 * exactly.
1969 *
1970 * \threadsafety It is safe to call this function from any thread.
1971 *
1972 * \since This function is available since SDL 3.1.3.
1973 */
1974extern SDL_DECLSPEC int SDLCALL SDL_wcsncasecmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
1975
1976/**
1977 * Parse a `long` from a wide string.
1978 *
1979 * If `str` starts with whitespace, then those whitespace characters are
1980 * skipped before attempting to parse the number.
1981 *
1982 * If the parsed number does not fit inside a `long`, the result is clamped to
1983 * the minimum and maximum representable `long` values.
1984 *
1985 * \param str The null-terminated wide string to read. Must not be NULL.
1986 * \param endp If not NULL, the address of the first invalid wide character
1987 * (i.e. the next character after the parsed number) will be
1988 * written to this pointer.
1989 * \param base The base of the integer to read. Supported values are 0 and 2
1990 * to 36 inclusive. If 0, the base will be inferred from the
1991 * number's prefix (0x for hexadecimal, 0 for octal, decimal
1992 * otherwise).
1993 * \returns The parsed `long`, or 0 if no number could be parsed.
1994 *
1995 * \threadsafety It is safe to call this function from any thread.
1996 *
1997 * \since This function is available since SDL 3.1.3.
1998 *
1999 * \sa SDL_strtol
2000 */
2001extern SDL_DECLSPEC long SDLCALL SDL_wcstol(const wchar_t *str, wchar_t **endp, int base);
2002
2003/**
2004 * This works exactly like strlen() but doesn't require access to a C runtime.
2005 *
2006 * Counts the bytes in `str`, excluding the null terminator.
2007 *
2008 * If you need the length of a UTF-8 string, consider using SDL_utf8strlen().
2009 *
2010 * \param str The null-terminated string to read. Must not be NULL.
2011 * \returns The length (in bytes, excluding the null terminator) of `src`.
2012 *
2013 * \threadsafety It is safe to call this function from any thread.
2014 *
2015 * \since This function is available since SDL 3.1.3.
2016 *
2017 * \sa SDL_strnlen
2018 * \sa SDL_utf8strlen
2019 * \sa SDL_utf8strnlen
2020 */
2021extern SDL_DECLSPEC size_t SDLCALL SDL_strlen(const char *str);
2022
2023/**
2024 * This works exactly like strnlen() but doesn't require access to a C
2025 * runtime.
2026 *
2027 * Counts up to a maximum of `maxlen` bytes in `str`, excluding the null
2028 * terminator.
2029 *
2030 * If you need the length of a UTF-8 string, consider using SDL_utf8strnlen().
2031 *
2032 * \param str The null-terminated string to read. Must not be NULL.
2033 * \param maxlen The maximum amount of bytes to count.
2034 * \returns The length (in bytes, excluding the null terminator) of `src` but
2035 * never more than `maxlen`.
2036 *
2037 * \threadsafety It is safe to call this function from any thread.
2038 *
2039 * \since This function is available since SDL 3.1.3.
2040 *
2041 * \sa SDL_strlen
2042 * \sa SDL_utf8strlen
2043 * \sa SDL_utf8strnlen
2044 */
2045extern SDL_DECLSPEC size_t SDLCALL SDL_strnlen(const char *str, size_t maxlen);
2046
2047/**
2048 * Copy a string.
2049 *
2050 * This function copies up to `maxlen` - 1 characters from `src` to `dst`,
2051 * then appends a null terminator.
2052 *
2053 * If `maxlen` is 0, no characters are copied and no null terminator is
2054 * written.
2055 *
2056 * If you want to copy an UTF-8 string but need to ensure that multi-byte
2057 * sequences are not truncated, consider using SDL_utf8strlcpy().
2058 *
2059 * \param dst The destination buffer. Must not be NULL, and must not overlap
2060 * with `src`.
2061 * \param src The null-terminated string to copy. Must not be NULL, and must
2062 * not overlap with `dst`.
2063 * \param maxlen The length (in characters) of the destination buffer.
2064 * \returns The length (in characters, excluding the null terminator) of
2065 * `src`.
2066 *
2067 * \threadsafety It is safe to call this function from any thread.
2068 *
2069 * \since This function is available since SDL 3.1.3.
2070 *
2071 * \sa SDL_strlcat
2072 * \sa SDL_utf8strlcpy
2073 */
2074extern SDL_DECLSPEC size_t SDLCALL SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
2075
2076/**
2077 * Copy an UTF-8 string.
2078 *
2079 * This function copies up to `dst_bytes` - 1 bytes from `src` to `dst` while
2080 * also ensuring that the string written to `dst` does not end in a truncated
2081 * multi-byte sequence. Finally, it appends a null terminator.
2082 *
2083 * `src` and `dst` must not overlap.
2084 *
2085 * Note that unlike SDL_strlcpy(), this function returns the number of bytes
2086 * written, not the length of `src`.
2087 *
2088 * \param dst The destination buffer. Must not be NULL, and must not overlap
2089 * with `src`.
2090 * \param src The null-terminated UTF-8 string to copy. Must not be NULL, and
2091 * must not overlap with `dst`.
2092 * \param dst_bytes The length (in bytes) of the destination buffer. Must not
2093 * be 0.
2094 * \returns The number of bytes written, excluding the null terminator.
2095 *
2096 * \threadsafety It is safe to call this function from any thread.
2097 *
2098 * \since This function is available since SDL 3.1.3.
2099 *
2100 * \sa SDL_strlcpy
2101 */
2102extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes);
2103
2104/**
2105 * Concatenate strings.
2106 *
2107 * This function appends up to `maxlen` - SDL_strlen(dst) - 1 characters from
2108 * `src` to the end of the string in `dst`, then appends a null terminator.
2109 *
2110 * `src` and `dst` must not overlap.
2111 *
2112 * If `maxlen` - SDL_strlen(dst) - 1 is less than or equal to 0, then `dst` is
2113 * unmodified.
2114 *
2115 * \param dst The destination buffer already containing the first
2116 * null-terminated string. Must not be NULL and must not overlap
2117 * with `src`.
2118 * \param src The second null-terminated string. Must not be NULL, and must
2119 * not overlap with `dst`.
2120 * \param maxlen The length (in characters) of the destination buffer.
2121 * \returns The length (in characters, excluding the null terminator) of the
2122 * string in `dst` plus the length of `src`.
2123 *
2124 * \threadsafety It is safe to call this function from any thread.
2125 *
2126 * \since This function is available since SDL 3.1.3.
2127 *
2128 * \sa SDL_strlcpy
2129 */
2130extern SDL_DECLSPEC size_t SDLCALL SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
2131
2132extern SDL_DECLSPEC SDL_MALLOC char * SDLCALL SDL_strdup(const char *str);
2133extern SDL_DECLSPEC SDL_MALLOC char * SDLCALL SDL_strndup(const char *str, size_t maxlen);
2134extern SDL_DECLSPEC char * SDLCALL SDL_strrev(char *str);
2135
2136/**
2137 * Convert a string to uppercase.
2138 *
2139 * **WARNING**: Regardless of system locale, this will only convert ASCII
2140 * values 'A' through 'Z' to uppercase.
2141 *
2142 * This function operates on a null-terminated string of bytes--even if it is
2143 * malformed UTF-8!--and converts ASCII characters 'a' through 'z' to their
2144 * uppercase equivalents in-place, returning the original `str` pointer.
2145 *
2146 * \param str the string to convert in-place. Can not be NULL.
2147 * \returns the `str` pointer passed into this function.
2148 *
2149 * \threadsafety It is safe to call this function from any thread.
2150 *
2151 * \since This function is available since SDL 3.1.3.
2152 *
2153 * \sa SDL_strlwr
2154 */
2155extern SDL_DECLSPEC char * SDLCALL SDL_strupr(char *str);
2156
2157/**
2158 * Convert a string to lowercase.
2159 *
2160 * **WARNING**: Regardless of system locale, this will only convert ASCII
2161 * values 'A' through 'Z' to lowercase.
2162 *
2163 * This function operates on a null-terminated string of bytes--even if it is
2164 * malformed UTF-8!--and converts ASCII characters 'A' through 'Z' to their
2165 * lowercase equivalents in-place, returning the original `str` pointer.
2166 *
2167 * \param str the string to convert in-place. Can not be NULL.
2168 * \returns the `str` pointer passed into this function.
2169 *
2170 * \threadsafety It is safe to call this function from any thread.
2171 *
2172 * \since This function is available since SDL 3.1.3.
2173 *
2174 * \sa SDL_strupr
2175 */
2176extern SDL_DECLSPEC char * SDLCALL SDL_strlwr(char *str);
2177
2178extern SDL_DECLSPEC char * SDLCALL SDL_strchr(const char *str, int c);
2179extern SDL_DECLSPEC char * SDLCALL SDL_strrchr(const char *str, int c);
2180extern SDL_DECLSPEC char * SDLCALL SDL_strstr(const char *haystack, const char *needle);
2181extern SDL_DECLSPEC char * SDLCALL SDL_strnstr(const char *haystack, const char *needle, size_t maxlen);
2182extern SDL_DECLSPEC char * SDLCALL SDL_strcasestr(const char *haystack, const char *needle);
2183extern SDL_DECLSPEC char * SDLCALL SDL_strtok_r(char *s1, const char *s2, char **saveptr);
2184extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strlen(const char *str);
2185extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strnlen(const char *str, size_t bytes);
2186
2187extern SDL_DECLSPEC char * SDLCALL SDL_itoa(int value, char *str, int radix);
2188extern SDL_DECLSPEC char * SDLCALL SDL_uitoa(unsigned int value, char *str, int radix);
2189extern SDL_DECLSPEC char * SDLCALL SDL_ltoa(long value, char *str, int radix);
2190extern SDL_DECLSPEC char * SDLCALL SDL_ultoa(unsigned long value, char *str, int radix);
2191extern SDL_DECLSPEC char * SDLCALL SDL_lltoa(long long value, char *str, int radix);
2192extern SDL_DECLSPEC char * SDLCALL SDL_ulltoa(unsigned long long value, char *str, int radix);
2193
2194/**
2195 * Parse an `int` from a string.
2196 *
2197 * The result of calling `SDL_atoi(str)` is equivalent to
2198 * `(int)SDL_strtol(str, NULL, 10)`.
2199 *
2200 * \param str The null-terminated string to read. Must not be NULL.
2201 * \returns The parsed `int`.
2202 *
2203 * \threadsafety It is safe to call this function from any thread.
2204 *
2205 * \since This function is available since SDL 3.1.3.
2206 *
2207 * \sa SDL_atof
2208 * \sa SDL_strtol
2209 * \sa SDL_strtoul
2210 * \sa SDL_strtoll
2211 * \sa SDL_strtoull
2212 * \sa SDL_strtod
2213 * \sa SDL_itoa
2214 */
2215extern SDL_DECLSPEC int SDLCALL SDL_atoi(const char *str);
2216
2217/**
2218 * Parse a `double` from a string.
2219 *
2220 * The result of calling `SDL_atof(str)` is equivalent to `SDL_strtod(str,
2221 * NULL)`.
2222 *
2223 * \param str The null-terminated string to read. Must not be NULL.
2224 * \returns The parsed `double`.
2225 *
2226 * \threadsafety It is safe to call this function from any thread.
2227 *
2228 * \since This function is available since SDL 3.1.3.
2229 *
2230 * \sa SDL_atoi
2231 * \sa SDL_strtol
2232 * \sa SDL_strtoul
2233 * \sa SDL_strtoll
2234 * \sa SDL_strtoull
2235 * \sa SDL_strtod
2236 */
2237extern SDL_DECLSPEC double SDLCALL SDL_atof(const char *str);
2238
2239/**
2240 * Parse a `long` from a string.
2241 *
2242 * If `str` starts with whitespace, then those whitespace characters are
2243 * skipped before attempting to parse the number.
2244 *
2245 * If the parsed number does not fit inside a `long`, the result is clamped to
2246 * the minimum and maximum representable `long` values.
2247 *
2248 * \param str The null-terminated string to read. Must not be NULL.
2249 * \param endp If not NULL, the address of the first invalid character (i.e.
2250 * the next character after the parsed number) will be written to
2251 * this pointer.
2252 * \param base The base of the integer to read. Supported values are 0 and 2
2253 * to 36 inclusive. If 0, the base will be inferred from the
2254 * number's prefix (0x for hexadecimal, 0 for octal, decimal
2255 * otherwise).
2256 * \returns The parsed `long`, or 0 if no number could be parsed.
2257 *
2258 * \threadsafety It is safe to call this function from any thread.
2259 *
2260 * \since This function is available since SDL 3.1.3.
2261 *
2262 * \sa SDL_atoi
2263 * \sa SDL_atof
2264 * \sa SDL_strtoul
2265 * \sa SDL_strtoll
2266 * \sa SDL_strtoull
2267 * \sa SDL_strtod
2268 * \sa SDL_ltoa
2269 * \sa SDL_wcstol
2270 */
2271extern SDL_DECLSPEC long SDLCALL SDL_strtol(const char *str, char **endp, int base);
2272
2273/**
2274 * Parse an `unsigned long` from a string.
2275 *
2276 * If `str` starts with whitespace, then those whitespace characters are
2277 * skipped before attempting to parse the number.
2278 *
2279 * If the parsed number does not fit inside an `unsigned long`, the result is
2280 * clamped to the maximum representable `unsigned long` value.
2281 *
2282 * \param str The null-terminated string to read. Must not be NULL.
2283 * \param endp If not NULL, the address of the first invalid character (i.e.
2284 * the next character after the parsed number) will be written to
2285 * this pointer.
2286 * \param base The base of the integer to read. Supported values are 0 and 2
2287 * to 36 inclusive. If 0, the base will be inferred from the
2288 * number's prefix (0x for hexadecimal, 0 for octal, decimal
2289 * otherwise).
2290 * \returns The parsed `unsigned long`, or 0 if no number could be parsed.
2291 *
2292 * \threadsafety It is safe to call this function from any thread.
2293 *
2294 * \since This function is available since SDL 3.1.3.
2295 *
2296 * \sa SDL_atoi
2297 * \sa SDL_atof
2298 * \sa SDL_strtol
2299 * \sa SDL_strtoll
2300 * \sa SDL_strtoull
2301 * \sa SDL_strtod
2302 * \sa SDL_ultoa
2303 */
2304extern SDL_DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **endp, int base);
2305
2306/**
2307 * Parse a `long long` from a string.
2308 *
2309 * If `str` starts with whitespace, then those whitespace characters are
2310 * skipped before attempting to parse the number.
2311 *
2312 * If the parsed number does not fit inside a `long long`, the result is
2313 * clamped to the minimum and maximum representable `long long` values.
2314 *
2315 * \param str The null-terminated string to read. Must not be NULL.
2316 * \param endp If not NULL, the address of the first invalid character (i.e.
2317 * the next character after the parsed number) will be written to
2318 * this pointer.
2319 * \param base The base of the integer to read. Supported values are 0 and 2
2320 * to 36 inclusive. If 0, the base will be inferred from the
2321 * number's prefix (0x for hexadecimal, 0 for octal, decimal
2322 * otherwise).
2323 * \returns The parsed `long long`, or 0 if no number could be parsed.
2324 *
2325 * \threadsafety It is safe to call this function from any thread.
2326 *
2327 * \since This function is available since SDL 3.1.3.
2328 *
2329 * \sa SDL_atoi
2330 * \sa SDL_atof
2331 * \sa SDL_strtol
2332 * \sa SDL_strtoul
2333 * \sa SDL_strtoull
2334 * \sa SDL_strtod
2335 * \sa SDL_lltoa
2336 */
2337extern SDL_DECLSPEC long long SDLCALL SDL_strtoll(const char *str, char **endp, int base);
2338
2339/**
2340 * Parse an `unsigned long long` from a string.
2341 *
2342 * If `str` starts with whitespace, then those whitespace characters are
2343 * skipped before attempting to parse the number.
2344 *
2345 * If the parsed number does not fit inside an `unsigned long long`, the
2346 * result is clamped to the maximum representable `unsigned long long` value.
2347 *
2348 * \param str The null-terminated string to read. Must not be NULL.
2349 * \param endp If not NULL, the address of the first invalid character (i.e.
2350 * the next character after the parsed number) will be written to
2351 * this pointer.
2352 * \param base The base of the integer to read. Supported values are 0 and 2
2353 * to 36 inclusive. If 0, the base will be inferred from the
2354 * number's prefix (0x for hexadecimal, 0 for octal, decimal
2355 * otherwise).
2356 * \returns The parsed `unsigned long long`, or 0 if no number could be
2357 * parsed.
2358 *
2359 * \threadsafety It is safe to call this function from any thread.
2360 *
2361 * \since This function is available since SDL 3.1.3.
2362 *
2363 * \sa SDL_atoi
2364 * \sa SDL_atof
2365 * \sa SDL_strtol
2366 * \sa SDL_strtoll
2367 * \sa SDL_strtoul
2368 * \sa SDL_strtod
2369 * \sa SDL_ulltoa
2370 */
2371extern SDL_DECLSPEC unsigned long long SDLCALL SDL_strtoull(const char *str, char **endp, int base);
2372
2373/**
2374 * Parse a `double` from a string.
2375 *
2376 * This function makes fewer guarantees than the C runtime `strtod`:
2377 *
2378 * - Only decimal notation is guaranteed to be supported. The handling of
2379 * scientific and hexadecimal notation is unspecified.
2380 * - Whether or not INF and NAN can be parsed is unspecified.
2381 * - The precision of the result is unspecified.
2382 *
2383 * \param str The null-terminated string to read. Must not be NULL.
2384 * \param endp If not NULL, the address of the first invalid character (i.e.
2385 * the next character after the parsed number) will be written to
2386 * this pointer.
2387 * \returns The parsed `double`, or 0 if no number could be parsed.
2388 *
2389 * \threadsafety It is safe to call this function from any thread.
2390 *
2391 * \since This function is available since SDL 3.1.3.
2392 *
2393 * \sa SDL_atoi
2394 * \sa SDL_atof
2395 * \sa SDL_strtol
2396 * \sa SDL_strtoll
2397 * \sa SDL_strtoul
2398 * \sa SDL_strtoull
2399 */
2400extern SDL_DECLSPEC double SDLCALL SDL_strtod(const char *str, char **endp);
2401
2402/**
2403 * Compare two null-terminated UTF-8 strings.
2404 *
2405 * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
2406 * since effectively this function just compares bytes until it hits a
2407 * null-terminating character. Also due to the nature of UTF-8, this can be
2408 * used with SDL_qsort() to put strings in (roughly) alphabetical order.
2409 *
2410 * \param str1 the first string to compare. NULL is not permitted!
2411 * \param str2 the second string to compare. NULL is not permitted!
2412 * \returns less than zero if str1 is "less than" str2, greater than zero if
2413 * str1 is "greater than" str2, and zero if the strings match
2414 * exactly.
2415 *
2416 * \threadsafety It is safe to call this function from any thread.
2417 *
2418 * \since This function is available since SDL 3.1.3.
2419 */
2420extern SDL_DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
2421
2422/**
2423 * Compare two UTF-8 strings up to a number of bytes.
2424 *
2425 * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
2426 * since effectively this function just compares bytes until it hits a
2427 * null-terminating character. Also due to the nature of UTF-8, this can be
2428 * used with SDL_qsort() to put strings in (roughly) alphabetical order.
2429 *
2430 * Note that while this function is intended to be used with UTF-8, it is
2431 * doing a bytewise comparison, and `maxlen` specifies a _byte_ limit! If the
2432 * limit lands in the middle of a multi-byte UTF-8 sequence, it will only
2433 * compare a portion of the final character.
2434 *
2435 * `maxlen` specifies a maximum number of bytes to compare; if the strings
2436 * match to this number of bytes (or both have matched to a null-terminator
2437 * character before this number of bytes), they will be considered equal.
2438 *
2439 * \param str1 the first string to compare. NULL is not permitted!
2440 * \param str2 the second string to compare. NULL is not permitted!
2441 * \param maxlen the maximum number of _bytes_ to compare.
2442 * \returns less than zero if str1 is "less than" str2, greater than zero if
2443 * str1 is "greater than" str2, and zero if the strings match
2444 * exactly.
2445 *
2446 * \threadsafety It is safe to call this function from any thread.
2447 *
2448 * \since This function is available since SDL 3.1.3.
2449 */
2450extern SDL_DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
2451
2452/**
2453 * Compare two null-terminated UTF-8 strings, case-insensitively.
2454 *
2455 * This will work with Unicode strings, using a technique called
2456 * "case-folding" to handle the vast majority of case-sensitive human
2457 * languages regardless of system locale. It can deal with expanding values: a
2458 * German Eszett character can compare against two ASCII 's' chars and be
2459 * considered a match, for example. A notable exception: it does not handle
2460 * the Turkish 'i' character; human language is complicated!
2461 *
2462 * Since this handles Unicode, it expects the string to be well-formed UTF-8
2463 * and not a null-terminated string of arbitrary bytes. Bytes that are not
2464 * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
2465 * CHARACTER), which is to say two strings of random bits may turn out to
2466 * match if they convert to the same amount of replacement characters.
2467 *
2468 * \param str1 the first string to compare. NULL is not permitted!
2469 * \param str2 the second string to compare. NULL is not permitted!
2470 * \returns less than zero if str1 is "less than" str2, greater than zero if
2471 * str1 is "greater than" str2, and zero if the strings match
2472 * exactly.
2473 *
2474 * \threadsafety It is safe to call this function from any thread.
2475 *
2476 * \since This function is available since SDL 3.1.3.
2477 */
2478extern SDL_DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
2479
2480
2481/**
2482 * Compare two UTF-8 strings, case-insensitively, up to a number of bytes.
2483 *
2484 * This will work with Unicode strings, using a technique called
2485 * "case-folding" to handle the vast majority of case-sensitive human
2486 * languages regardless of system locale. It can deal with expanding values: a
2487 * German Eszett character can compare against two ASCII 's' chars and be
2488 * considered a match, for example. A notable exception: it does not handle
2489 * the Turkish 'i' character; human language is complicated!
2490 *
2491 * Since this handles Unicode, it expects the string to be well-formed UTF-8
2492 * and not a null-terminated string of arbitrary bytes. Bytes that are not
2493 * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
2494 * CHARACTER), which is to say two strings of random bits may turn out to
2495 * match if they convert to the same amount of replacement characters.
2496 *
2497 * Note that while this function is intended to be used with UTF-8, `maxlen`
2498 * specifies a _byte_ limit! If the limit lands in the middle of a multi-byte
2499 * UTF-8 sequence, it may convert a portion of the final character to one or
2500 * more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not to overflow
2501 * a buffer.
2502 *
2503 * `maxlen` specifies a maximum number of bytes to compare; if the strings
2504 * match to this number of bytes (or both have matched to a null-terminator
2505 * character before this number of bytes), they will be considered equal.
2506 *
2507 * \param str1 the first string to compare. NULL is not permitted!
2508 * \param str2 the second string to compare. NULL is not permitted!
2509 * \param maxlen the maximum number of bytes to compare.
2510 * \returns less than zero if str1 is "less than" str2, greater than zero if
2511 * str1 is "greater than" str2, and zero if the strings match
2512 * exactly.
2513 *
2514 * \threadsafety It is safe to call this function from any thread.
2515 *
2516 * \since This function is available since SDL 3.1.3.
2517 */
2518extern SDL_DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen);
2519
2520/**
2521 * Searches a string for the first occurence of any character contained in a
2522 * breakset, and returns a pointer from the string to that character.
2523 *
2524 * \param str The null-terminated string to be searched. Must not be NULL, and
2525 * must not overlap with `breakset`.
2526 * \param breakset A null-terminated string containing the list of characters
2527 * to look for. Must not be NULL, and must not overlap with
2528 * `str`.
2529 * \returns A pointer to the location, in str, of the first occurence of a
2530 * character present in the breakset, or NULL if none is found.
2531 *
2532 * \threadsafety It is safe to call this function from any thread.
2533 *
2534 * \since This function is available since SDL 3.1.3.
2535 */
2536extern SDL_DECLSPEC char * SDLCALL SDL_strpbrk(const char *str, const char *breakset);
2537
2538/**
2539 * The Unicode REPLACEMENT CHARACTER codepoint.
2540 *
2541 * SDL_StepUTF8() and SDL_StepBackUTF8() report this codepoint when they
2542 * encounter a UTF-8 string with encoding errors.
2543 *
2544 * This tends to render as something like a question mark in most places.
2545 *
2546 * \since This macro is available since SDL 3.1.3.
2547 *
2548 * \sa SDL_StepBackUTF8
2549 * \sa SDL_StepUTF8
2550 */
2551#define SDL_INVALID_UNICODE_CODEPOINT 0xFFFD
2552
2553/**
2554 * Decode a UTF-8 string, one Unicode codepoint at a time.
2555 *
2556 * This will return the first Unicode codepoint in the UTF-8 encoded string in
2557 * `*pstr`, and then advance `*pstr` past any consumed bytes before returning.
2558 *
2559 * It will not access more than `*pslen` bytes from the string. `*pslen` will
2560 * be adjusted, as well, subtracting the number of bytes consumed.
2561 *
2562 * `pslen` is allowed to be NULL, in which case the string _must_ be
2563 * NULL-terminated, as the function will blindly read until it sees the NULL
2564 * char.
2565 *
2566 * if `*pslen` is zero, it assumes the end of string is reached and returns a
2567 * zero codepoint regardless of the contents of the string buffer.
2568 *
2569 * If the resulting codepoint is zero (a NULL terminator), or `*pslen` is
2570 * zero, it will not advance `*pstr` or `*pslen` at all.
2571 *
2572 * Generally this function is called in a loop until it returns zero,
2573 * adjusting its parameters each iteration.
2574 *
2575 * If an invalid UTF-8 sequence is encountered, this function returns
2576 * SDL_INVALID_UNICODE_CODEPOINT and advances the string/length by one byte
2577 * (which is to say, a multibyte sequence might produce several
2578 * SDL_INVALID_UNICODE_CODEPOINT returns before it syncs to the next valid
2579 * UTF-8 sequence).
2580 *
2581 * Several things can generate invalid UTF-8 sequences, including overlong
2582 * encodings, the use of UTF-16 surrogate values, and truncated data. Please
2583 * refer to
2584 * [RFC3629](https://www.ietf.org/rfc/rfc3629.txt)
2585 * for details.
2586 *
2587 * \param pstr a pointer to a UTF-8 string pointer to be read and adjusted.
2588 * \param pslen a pointer to the number of bytes in the string, to be read and
2589 * adjusted. NULL is allowed.
2590 * \returns the first Unicode codepoint in the string.
2591 *
2592 * \threadsafety It is safe to call this function from any thread.
2593 *
2594 * \since This function is available since SDL 3.1.3.
2595 */
2596extern SDL_DECLSPEC Uint32 SDLCALL SDL_StepUTF8(const char **pstr, size_t *pslen);
2597
2598/**
2599 * Decode a UTF-8 string in reverse, one Unicode codepoint at a time.
2600 *
2601 * This will go to the start of the previous Unicode codepoint in the string,
2602 * move `*pstr` to that location and return that codepoint.
2603 *
2604 * If `*pstr` is already at the start of the string), it will not advance
2605 * `*pstr` at all.
2606 *
2607 * Generally this function is called in a loop until it returns zero,
2608 * adjusting its parameter each iteration.
2609 *
2610 * If an invalid UTF-8 sequence is encountered, this function returns
2611 * SDL_INVALID_UNICODE_CODEPOINT.
2612 *
2613 * Several things can generate invalid UTF-8 sequences, including overlong
2614 * encodings, the use of UTF-16 surrogate values, and truncated data. Please
2615 * refer to
2616 * [RFC3629](https://www.ietf.org/rfc/rfc3629.txt)
2617 * for details.
2618 *
2619 * \param start a pointer to the beginning of the UTF-8 string.
2620 * \param pstr a pointer to a UTF-8 string pointer to be read and adjusted.
2621 * \returns the previous Unicode codepoint in the string.
2622 *
2623 * \threadsafety It is safe to call this function from any thread.
2624 *
2625 * \since This function is available since SDL 3.1.6.
2626 */
2627extern SDL_DECLSPEC Uint32 SDLCALL SDL_StepBackUTF8(const char *start, const char **pstr);
2628
2629/**
2630 * Convert a single Unicode codepoint to UTF-8.
2631 *
2632 * The buffer pointed to by `dst` must be at least 4 bytes long, as this
2633 * function may generate between 1 and 4 bytes of output.
2634 *
2635 * This function returns the first byte _after_ the newly-written UTF-8
2636 * sequence, which is useful for encoding multiple codepoints in a loop, or
2637 * knowing where to write a NULL-terminator character to end the string (in
2638 * either case, plan to have a buffer of _more_ than 4 bytes!).
2639 *
2640 * If `codepoint` is an invalid value (outside the Unicode range, or a UTF-16
2641 * surrogate value, etc), this will use U+FFFD (REPLACEMENT CHARACTER) for the
2642 * codepoint instead, and not set an error.
2643 *
2644 * If `dst` is NULL, this returns NULL immediately without writing to the
2645 * pointer and without setting an error.
2646 *
2647 * \param codepoint a Unicode codepoint to convert to UTF-8.
2648 * \param dst the location to write the encoded UTF-8. Must point to at least
2649 * 4 bytes!
2650 * \returns the first byte past the newly-written UTF-8 sequence.
2651 *
2652 * \threadsafety It is safe to call this function from any thread.
2653 *
2654 * \since This function is available since SDL 3.1.3.
2655 */
2656extern SDL_DECLSPEC char * SDLCALL SDL_UCS4ToUTF8(Uint32 codepoint, char *dst);
2657
2658
2659extern SDL_DECLSPEC int SDLCALL SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...) SDL_SCANF_VARARG_FUNC(2);
2660extern SDL_DECLSPEC int SDLCALL SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_list ap) SDL_SCANF_VARARG_FUNCV(2);
2661extern SDL_DECLSPEC int SDLCALL SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(3);
2662extern SDL_DECLSPEC int SDLCALL SDL_swprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, ...) SDL_WPRINTF_VARARG_FUNC(3);
2663extern SDL_DECLSPEC int SDLCALL SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(3);
2664extern SDL_DECLSPEC int SDLCALL SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, va_list ap) SDL_WPRINTF_VARARG_FUNCV(3);
2665extern SDL_DECLSPEC int SDLCALL SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
2666extern SDL_DECLSPEC int SDLCALL SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
2667
2668/**
2669 * Seeds the pseudo-random number generator.
2670 *
2671 * Reusing the seed number will cause SDL_rand_*() to repeat the same stream
2672 * of 'random' numbers.
2673 *
2674 * \param seed the value to use as a random number seed, or 0 to use
2675 * SDL_GetPerformanceCounter().
2676 *
2677 * \threadsafety This should be called on the same thread that calls
2678 * SDL_rand*()
2679 *
2680 * \since This function is available since SDL 3.1.3.
2681 *
2682 * \sa SDL_rand
2683 * \sa SDL_rand_bits
2684 * \sa SDL_randf
2685 */
2686extern SDL_DECLSPEC void SDLCALL SDL_srand(Uint64 seed);
2687
2688/**
2689 * Generate a pseudo-random number less than n for positive n
2690 *
2691 * The method used is faster and of better quality than `rand() % n`. Odds are
2692 * roughly 99.9% even for n = 1 million. Evenness is better for smaller n, and
2693 * much worse as n gets bigger.
2694 *
2695 * Example: to simulate a d6 use `SDL_rand(6) + 1` The +1 converts 0..5 to
2696 * 1..6
2697 *
2698 * If you want to generate a pseudo-random number in the full range of Sint32,
2699 * you should use: (Sint32)SDL_rand_bits()
2700 *
2701 * If you want reproducible output, be sure to initialize with SDL_srand()
2702 * first.
2703 *
2704 * There are no guarantees as to the quality of the random sequence produced,
2705 * and this should not be used for security (cryptography, passwords) or where
2706 * money is on the line (loot-boxes, casinos). There are many random number
2707 * libraries available with different characteristics and you should pick one
2708 * of those to meet any serious needs.
2709 *
2710 * \param n the number of possible outcomes. n must be positive.
2711 * \returns a random value in the range of [0 .. n-1].
2712 *
2713 * \threadsafety All calls should be made from a single thread
2714 *
2715 * \since This function is available since SDL 3.1.3.
2716 *
2717 * \sa SDL_srand
2718 * \sa SDL_randf
2719 */
2720extern SDL_DECLSPEC Sint32 SDLCALL SDL_rand(Sint32 n);
2721
2722/**
2723 * Generate a uniform pseudo-random floating point number less than 1.0
2724 *
2725 * If you want reproducible output, be sure to initialize with SDL_srand()
2726 * first.
2727 *
2728 * There are no guarantees as to the quality of the random sequence produced,
2729 * and this should not be used for security (cryptography, passwords) or where
2730 * money is on the line (loot-boxes, casinos). There are many random number
2731 * libraries available with different characteristics and you should pick one
2732 * of those to meet any serious needs.
2733 *
2734 * \returns a random value in the range of [0.0, 1.0).
2735 *
2736 * \threadsafety All calls should be made from a single thread
2737 *
2738 * \since This function is available since SDL 3.1.3.
2739 *
2740 * \sa SDL_srand
2741 * \sa SDL_rand
2742 */
2743extern SDL_DECLSPEC float SDLCALL SDL_randf(void);
2744
2745/**
2746 * Generate 32 pseudo-random bits.
2747 *
2748 * You likely want to use SDL_rand() to get a psuedo-random number instead.
2749 *
2750 * There are no guarantees as to the quality of the random sequence produced,
2751 * and this should not be used for security (cryptography, passwords) or where
2752 * money is on the line (loot-boxes, casinos). There are many random number
2753 * libraries available with different characteristics and you should pick one
2754 * of those to meet any serious needs.
2755 *
2756 * \returns a random value in the range of [0-SDL_MAX_UINT32].
2757 *
2758 * \threadsafety All calls should be made from a single thread
2759 *
2760 * \since This function is available since SDL 3.1.3.
2761 *
2762 * \sa SDL_rand
2763 * \sa SDL_randf
2764 * \sa SDL_srand
2765 */
2766extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_bits(void);
2767
2768/**
2769 * Generate a pseudo-random number less than n for positive n
2770 *
2771 * The method used is faster and of better quality than `rand() % n`. Odds are
2772 * roughly 99.9% even for n = 1 million. Evenness is better for smaller n, and
2773 * much worse as n gets bigger.
2774 *
2775 * Example: to simulate a d6 use `SDL_rand_r(state, 6) + 1` The +1 converts
2776 * 0..5 to 1..6
2777 *
2778 * If you want to generate a pseudo-random number in the full range of Sint32,
2779 * you should use: (Sint32)SDL_rand_bits_r(state)
2780 *
2781 * There are no guarantees as to the quality of the random sequence produced,
2782 * and this should not be used for security (cryptography, passwords) or where
2783 * money is on the line (loot-boxes, casinos). There are many random number
2784 * libraries available with different characteristics and you should pick one
2785 * of those to meet any serious needs.
2786 *
2787 * \param state a pointer to the current random number state, this may not be
2788 * NULL.
2789 * \param n the number of possible outcomes. n must be positive.
2790 * \returns a random value in the range of [0 .. n-1].
2791 *
2792 * \threadsafety This function is thread-safe, as long as the state pointer
2793 * isn't shared between threads.
2794 *
2795 * \since This function is available since SDL 3.1.3.
2796 *
2797 * \sa SDL_rand
2798 * \sa SDL_rand_bits_r
2799 * \sa SDL_randf_r
2800 */
2801extern SDL_DECLSPEC Sint32 SDLCALL SDL_rand_r(Uint64 *state, Sint32 n);
2802
2803/**
2804 * Generate a uniform pseudo-random floating point number less than 1.0
2805 *
2806 * If you want reproducible output, be sure to initialize with SDL_srand()
2807 * first.
2808 *
2809 * There are no guarantees as to the quality of the random sequence produced,
2810 * and this should not be used for security (cryptography, passwords) or where
2811 * money is on the line (loot-boxes, casinos). There are many random number
2812 * libraries available with different characteristics and you should pick one
2813 * of those to meet any serious needs.
2814 *
2815 * \param state a pointer to the current random number state, this may not be
2816 * NULL.
2817 * \returns a random value in the range of [0.0, 1.0).
2818 *
2819 * \threadsafety This function is thread-safe, as long as the state pointer
2820 * isn't shared between threads.
2821 *
2822 * \since This function is available since SDL 3.1.3.
2823 *
2824 * \sa SDL_rand_bits_r
2825 * \sa SDL_rand_r
2826 * \sa SDL_randf
2827 */
2828extern SDL_DECLSPEC float SDLCALL SDL_randf_r(Uint64 *state);
2829
2830/**
2831 * Generate 32 pseudo-random bits.
2832 *
2833 * You likely want to use SDL_rand_r() to get a psuedo-random number instead.
2834 *
2835 * There are no guarantees as to the quality of the random sequence produced,
2836 * and this should not be used for security (cryptography, passwords) or where
2837 * money is on the line (loot-boxes, casinos). There are many random number
2838 * libraries available with different characteristics and you should pick one
2839 * of those to meet any serious needs.
2840 *
2841 * \param state a pointer to the current random number state, this may not be
2842 * NULL.
2843 * \returns a random value in the range of [0-SDL_MAX_UINT32].
2844 *
2845 * \threadsafety This function is thread-safe, as long as the state pointer
2846 * isn't shared between threads.
2847 *
2848 * \since This function is available since SDL 3.1.3.
2849 *
2850 * \sa SDL_rand_r
2851 * \sa SDL_randf_r
2852 */
2853extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_bits_r(Uint64 *state);
2854
2855
2856#ifndef SDL_PI_D
2857#define SDL_PI_D 3.141592653589793238462643383279502884 /**< pi (double) */
2858#endif
2859#ifndef SDL_PI_F
2860#define SDL_PI_F 3.141592653589793238462643383279502884F /**< pi (float) */
2861#endif
2862
2863/**
2864 * Compute the arc cosine of `x`.
2865 *
2866 * The definition of `y = acos(x)` is `x = cos(y)`.
2867 *
2868 * Domain: `-1 <= x <= 1`
2869 *
2870 * Range: `0 <= y <= Pi`
2871 *
2872 * This function operates on double-precision floating point values, use
2873 * SDL_acosf for single-precision floats.
2874 *
2875 * This function may use a different approximation across different versions,
2876 * platforms and configurations. i.e, it can return a different value given
2877 * the same input on different machines or operating systems, or if SDL is
2878 * updated.
2879 *
2880 * \param x floating point value.
2881 * \returns arc cosine of `x`, in radians.
2882 *
2883 * \threadsafety It is safe to call this function from any thread.
2884 *
2885 * \since This function is available since SDL 3.1.3.
2886 *
2887 * \sa SDL_acosf
2888 * \sa SDL_asin
2889 * \sa SDL_cos
2890 */
2891extern SDL_DECLSPEC double SDLCALL SDL_acos(double x);
2892
2893/**
2894 * Compute the arc cosine of `x`.
2895 *
2896 * The definition of `y = acos(x)` is `x = cos(y)`.
2897 *
2898 * Domain: `-1 <= x <= 1`
2899 *
2900 * Range: `0 <= y <= Pi`
2901 *
2902 * This function operates on single-precision floating point values, use
2903 * SDL_acos for double-precision floats.
2904 *
2905 * This function may use a different approximation across different versions,
2906 * platforms and configurations. i.e, it can return a different value given
2907 * the same input on different machines or operating systems, or if SDL is
2908 * updated.
2909 *
2910 * \param x floating point value.
2911 * \returns arc cosine of `x`, in radians.
2912 *
2913 * \threadsafety It is safe to call this function from any thread.
2914 *
2915 * \since This function is available since SDL 3.1.3.
2916 *
2917 * \sa SDL_acos
2918 * \sa SDL_asinf
2919 * \sa SDL_cosf
2920 */
2921extern SDL_DECLSPEC float SDLCALL SDL_acosf(float x);
2922
2923/**
2924 * Compute the arc sine of `x`.
2925 *
2926 * The definition of `y = asin(x)` is `x = sin(y)`.
2927 *
2928 * Domain: `-1 <= x <= 1`
2929 *
2930 * Range: `-Pi/2 <= y <= Pi/2`
2931 *
2932 * This function operates on double-precision floating point values, use
2933 * SDL_asinf for single-precision floats.
2934 *
2935 * This function may use a different approximation across different versions,
2936 * platforms and configurations. i.e, it can return a different value given
2937 * the same input on different machines or operating systems, or if SDL is
2938 * updated.
2939 *
2940 * \param x floating point value.
2941 * \returns arc sine of `x`, in radians.
2942 *
2943 * \threadsafety It is safe to call this function from any thread.
2944 *
2945 * \since This function is available since SDL 3.1.3.
2946 *
2947 * \sa SDL_asinf
2948 * \sa SDL_acos
2949 * \sa SDL_sin
2950 */
2951extern SDL_DECLSPEC double SDLCALL SDL_asin(double x);
2952
2953/**
2954 * Compute the arc sine of `x`.
2955 *
2956 * The definition of `y = asin(x)` is `x = sin(y)`.
2957 *
2958 * Domain: `-1 <= x <= 1`
2959 *
2960 * Range: `-Pi/2 <= y <= Pi/2`
2961 *
2962 * This function operates on single-precision floating point values, use
2963 * SDL_asin for double-precision floats.
2964 *
2965 * This function may use a different approximation across different versions,
2966 * platforms and configurations. i.e, it can return a different value given
2967 * the same input on different machines or operating systems, or if SDL is
2968 * updated.
2969 *
2970 * \param x floating point value.
2971 * \returns arc sine of `x`, in radians.
2972 *
2973 * \threadsafety It is safe to call this function from any thread.
2974 *
2975 * \since This function is available since SDL 3.1.3.
2976 *
2977 * \sa SDL_asin
2978 * \sa SDL_acosf
2979 * \sa SDL_sinf
2980 */
2981extern SDL_DECLSPEC float SDLCALL SDL_asinf(float x);
2982
2983/**
2984 * Compute the arc tangent of `x`.
2985 *
2986 * The definition of `y = atan(x)` is `x = tan(y)`.
2987 *
2988 * Domain: `-INF <= x <= INF`
2989 *
2990 * Range: `-Pi/2 <= y <= Pi/2`
2991 *
2992 * This function operates on double-precision floating point values, use
2993 * SDL_atanf for single-precision floats.
2994 *
2995 * To calculate the arc tangent of y / x, use SDL_atan2.
2996 *
2997 * This function may use a different approximation across different versions,
2998 * platforms and configurations. i.e, it can return a different value given
2999 * the same input on different machines or operating systems, or if SDL is
3000 * updated.
3001 *
3002 * \param x floating point value.
3003 * \returns arc tangent of of `x` in radians, or 0 if `x = 0`.
3004 *
3005 * \threadsafety It is safe to call this function from any thread.
3006 *
3007 * \since This function is available since SDL 3.1.3.
3008 *
3009 * \sa SDL_atanf
3010 * \sa SDL_atan2
3011 * \sa SDL_tan
3012 */
3013extern SDL_DECLSPEC double SDLCALL SDL_atan(double x);
3014
3015/**
3016 * Compute the arc tangent of `x`.
3017 *
3018 * The definition of `y = atan(x)` is `x = tan(y)`.
3019 *
3020 * Domain: `-INF <= x <= INF`
3021 *
3022 * Range: `-Pi/2 <= y <= Pi/2`
3023 *
3024 * This function operates on single-precision floating point values, use
3025 * SDL_atan for dboule-precision floats.
3026 *
3027 * To calculate the arc tangent of y / x, use SDL_atan2f.
3028 *
3029 * This function may use a different approximation across different versions,
3030 * platforms and configurations. i.e, it can return a different value given
3031 * the same input on different machines or operating systems, or if SDL is
3032 * updated.
3033 *
3034 * \param x floating point value.
3035 * \returns arc tangent of of `x` in radians, or 0 if `x = 0`.
3036 *
3037 * \threadsafety It is safe to call this function from any thread.
3038 *
3039 * \since This function is available since SDL 3.1.3.
3040 *
3041 * \sa SDL_atan
3042 * \sa SDL_atan2f
3043 * \sa SDL_tanf
3044 */
3045extern SDL_DECLSPEC float SDLCALL SDL_atanf(float x);
3046
3047/**
3048 * Compute the arc tangent of `y / x`, using the signs of x and y to adjust
3049 * the result's quadrant.
3050 *
3051 * The definition of `z = atan2(x, y)` is `y = x tan(z)`, where the quadrant
3052 * of z is determined based on the signs of x and y.
3053 *
3054 * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
3055 *
3056 * Range: `-Pi/2 <= y <= Pi/2`
3057 *
3058 * This function operates on double-precision floating point values, use
3059 * SDL_atan2f for single-precision floats.
3060 *
3061 * To calculate the arc tangent of a single value, use SDL_atan.
3062 *
3063 * This function may use a different approximation across different versions,
3064 * platforms and configurations. i.e, it can return a different value given
3065 * the same input on different machines or operating systems, or if SDL is
3066 * updated.
3067 *
3068 * \param y floating point value of the numerator (y coordinate).
3069 * \param x floating point value of the denominator (x coordinate).
3070 * \returns arc tangent of of `y / x` in radians, or, if `x = 0`, either
3071 * `-Pi/2`, `0`, or `Pi/2`, depending on the value of `y`.
3072 *
3073 * \threadsafety It is safe to call this function from any thread.
3074 *
3075 * \since This function is available since SDL 3.1.3.
3076 *
3077 * \sa SDL_atan2f
3078 * \sa SDL_atan
3079 * \sa SDL_tan
3080 */
3081extern SDL_DECLSPEC double SDLCALL SDL_atan2(double y, double x);
3082
3083/**
3084 * Compute the arc tangent of `y / x`, using the signs of x and y to adjust
3085 * the result's quadrant.
3086 *
3087 * The definition of `z = atan2(x, y)` is `y = x tan(z)`, where the quadrant
3088 * of z is determined based on the signs of x and y.
3089 *
3090 * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
3091 *
3092 * Range: `-Pi/2 <= y <= Pi/2`
3093 *
3094 * This function operates on single-precision floating point values, use
3095 * SDL_atan2 for double-precision floats.
3096 *
3097 * To calculate the arc tangent of a single value, use SDL_atanf.
3098 *
3099 * This function may use a different approximation across different versions,
3100 * platforms and configurations. i.e, it can return a different value given
3101 * the same input on different machines or operating systems, or if SDL is
3102 * updated.
3103 *
3104 * \param y floating point value of the numerator (y coordinate).
3105 * \param x floating point value of the denominator (x coordinate).
3106 * \returns arc tangent of of `y / x` in radians, or, if `x = 0`, either
3107 * `-Pi/2`, `0`, or `Pi/2`, depending on the value of `y`.
3108 *
3109 * \threadsafety It is safe to call this function from any thread.
3110 *
3111 * \since This function is available since SDL 3.1.3.
3112 *
3113 * \sa SDL_atan2f
3114 * \sa SDL_atan
3115 * \sa SDL_tan
3116 */
3117extern SDL_DECLSPEC float SDLCALL SDL_atan2f(float y, float x);
3118
3119/**
3120 * Compute the ceiling of `x`.
3121 *
3122 * The ceiling of `x` is the smallest integer `y` such that `y > x`, i.e `x`
3123 * rounded up to the nearest integer.
3124 *
3125 * Domain: `-INF <= x <= INF`
3126 *
3127 * Range: `-INF <= y <= INF`, y integer
3128 *
3129 * This function operates on double-precision floating point values, use
3130 * SDL_ceilf for single-precision floats.
3131 *
3132 * \param x floating point value.
3133 * \returns the ceiling of `x`.
3134 *
3135 * \threadsafety It is safe to call this function from any thread.
3136 *
3137 * \since This function is available since SDL 3.1.3.
3138 *
3139 * \sa SDL_ceilf
3140 * \sa SDL_floor
3141 * \sa SDL_trunc
3142 * \sa SDL_round
3143 * \sa SDL_lround
3144 */
3145extern SDL_DECLSPEC double SDLCALL SDL_ceil(double x);
3146
3147/**
3148 * Compute the ceiling of `x`.
3149 *
3150 * The ceiling of `x` is the smallest integer `y` such that `y > x`, i.e `x`
3151 * rounded up to the nearest integer.
3152 *
3153 * Domain: `-INF <= x <= INF`
3154 *
3155 * Range: `-INF <= y <= INF`, y integer
3156 *
3157 * This function operates on single-precision floating point values, use
3158 * SDL_ceil for double-precision floats.
3159 *
3160 * \param x floating point value.
3161 * \returns the ceiling of `x`.
3162 *
3163 * \threadsafety It is safe to call this function from any thread.
3164 *
3165 * \since This function is available since SDL 3.1.3.
3166 *
3167 * \sa SDL_ceil
3168 * \sa SDL_floorf
3169 * \sa SDL_truncf
3170 * \sa SDL_roundf
3171 * \sa SDL_lroundf
3172 */
3173extern SDL_DECLSPEC float SDLCALL SDL_ceilf(float x);
3174
3175/**
3176 * Copy the sign of one floating-point value to another.
3177 *
3178 * The definition of copysign is that ``copysign(x, y) = abs(x) * sign(y)``.
3179 *
3180 * Domain: `-INF <= x <= INF`, ``-INF <= y <= f``
3181 *
3182 * Range: `-INF <= z <= INF`
3183 *
3184 * This function operates on double-precision floating point values, use
3185 * SDL_copysignf for single-precision floats.
3186 *
3187 * \param x floating point value to use as the magnitude.
3188 * \param y floating point value to use as the sign.
3189 * \returns the floating point value with the sign of y and the magnitude of
3190 * x.
3191 *
3192 * \threadsafety It is safe to call this function from any thread.
3193 *
3194 * \since This function is available since SDL 3.1.3.
3195 *
3196 * \sa SDL_copysignf
3197 * \sa SDL_fabs
3198 */
3199extern SDL_DECLSPEC double SDLCALL SDL_copysign(double x, double y);
3200
3201/**
3202 * Copy the sign of one floating-point value to another.
3203 *
3204 * The definition of copysign is that ``copysign(x, y) = abs(x) * sign(y)``.
3205 *
3206 * Domain: `-INF <= x <= INF`, ``-INF <= y <= f``
3207 *
3208 * Range: `-INF <= z <= INF`
3209 *
3210 * This function operates on single-precision floating point values, use
3211 * SDL_copysign for double-precision floats.
3212 *
3213 * \param x floating point value to use as the magnitude.
3214 * \param y floating point value to use as the sign.
3215 * \returns the floating point value with the sign of y and the magnitude of
3216 * x.
3217 *
3218 * \threadsafety It is safe to call this function from any thread.
3219 *
3220 * \since This function is available since SDL 3.1.3.
3221 *
3222 * \sa SDL_copysignf
3223 * \sa SDL_fabsf
3224 */
3225extern SDL_DECLSPEC float SDLCALL SDL_copysignf(float x, float y);
3226
3227/**
3228 * Compute the cosine of `x`.
3229 *
3230 * Domain: `-INF <= x <= INF`
3231 *
3232 * Range: `-1 <= y <= 1`
3233 *
3234 * This function operates on double-precision floating point values, use
3235 * SDL_cosf for single-precision floats.
3236 *
3237 * This function may use a different approximation across different versions,
3238 * platforms and configurations. i.e, it can return a different value given
3239 * the same input on different machines or operating systems, or if SDL is
3240 * updated.
3241 *
3242 * \param x floating point value, in radians.
3243 * \returns cosine of `x`.
3244 *
3245 * \threadsafety It is safe to call this function from any thread.
3246 *
3247 * \since This function is available since SDL 3.1.3.
3248 *
3249 * \sa SDL_cosf
3250 * \sa SDL_acos
3251 * \sa SDL_sin
3252 */
3253extern SDL_DECLSPEC double SDLCALL SDL_cos(double x);
3254
3255/**
3256 * Compute the cosine of `x`.
3257 *
3258 * Domain: `-INF <= x <= INF`
3259 *
3260 * Range: `-1 <= y <= 1`
3261 *
3262 * This function operates on single-precision floating point values, use
3263 * SDL_cos for double-precision floats.
3264 *
3265 * This function may use a different approximation across different versions,
3266 * platforms and configurations. i.e, it can return a different value given
3267 * the same input on different machines or operating systems, or if SDL is
3268 * updated.
3269 *
3270 * \param x floating point value, in radians.
3271 * \returns cosine of `x`.
3272 *
3273 * \threadsafety It is safe to call this function from any thread.
3274 *
3275 * \since This function is available since SDL 3.1.3.
3276 *
3277 * \sa SDL_cos
3278 * \sa SDL_acosf
3279 * \sa SDL_sinf
3280 */
3281extern SDL_DECLSPEC float SDLCALL SDL_cosf(float x);
3282
3283/**
3284 * Compute the exponential of `x`.
3285 *
3286 * The definition of `y = exp(x)` is `y = e^x`, where `e` is the base of the
3287 * natural logarithm. The inverse is the natural logarithm, SDL_log.
3288 *
3289 * Domain: `-INF <= x <= INF`
3290 *
3291 * Range: `0 <= y <= INF`
3292 *
3293 * The output will overflow if `exp(x)` is too large to be represented.
3294 *
3295 * This function operates on double-precision floating point values, use
3296 * SDL_expf for single-precision floats.
3297 *
3298 * This function may use a different approximation across different versions,
3299 * platforms and configurations. i.e, it can return a different value given
3300 * the same input on different machines or operating systems, or if SDL is
3301 * updated.
3302 *
3303 * \param x floating point value.
3304 * \returns value of `e^x`.
3305 *
3306 * \threadsafety It is safe to call this function from any thread.
3307 *
3308 * \since This function is available since SDL 3.1.3.
3309 *
3310 * \sa SDL_expf
3311 * \sa SDL_log
3312 */
3313extern SDL_DECLSPEC double SDLCALL SDL_exp(double x);
3314
3315/**
3316 * Compute the exponential of `x`.
3317 *
3318 * The definition of `y = exp(x)` is `y = e^x`, where `e` is the base of the
3319 * natural logarithm. The inverse is the natural logarithm, SDL_logf.
3320 *
3321 * Domain: `-INF <= x <= INF`
3322 *
3323 * Range: `0 <= y <= INF`
3324 *
3325 * The output will overflow if `exp(x)` is too large to be represented.
3326 *
3327 * This function operates on single-precision floating point values, use
3328 * SDL_exp for double-precision floats.
3329 *
3330 * This function may use a different approximation across different versions,
3331 * platforms and configurations. i.e, it can return a different value given
3332 * the same input on different machines or operating systems, or if SDL is
3333 * updated.
3334 *
3335 * \param x floating point value.
3336 * \returns value of `e^x`.
3337 *
3338 * \threadsafety It is safe to call this function from any thread.
3339 *
3340 * \since This function is available since SDL 3.1.3.
3341 *
3342 * \sa SDL_exp
3343 * \sa SDL_logf
3344 */
3345extern SDL_DECLSPEC float SDLCALL SDL_expf(float x);
3346
3347/**
3348 * Compute the absolute value of `x`
3349 *
3350 * Domain: `-INF <= x <= INF`
3351 *
3352 * Range: `0 <= y <= INF`
3353 *
3354 * This function operates on double-precision floating point values, use
3355 * SDL_copysignf for single-precision floats.
3356 *
3357 * \param x floating point value to use as the magnitude.
3358 * \returns the absolute value of `x`.
3359 *
3360 * \threadsafety It is safe to call this function from any thread.
3361 *
3362 * \since This function is available since SDL 3.1.3.
3363 *
3364 * \sa SDL_fabsf
3365 */
3366extern SDL_DECLSPEC double SDLCALL SDL_fabs(double x);
3367
3368/**
3369 * Compute the absolute value of `x`
3370 *
3371 * Domain: `-INF <= x <= INF`
3372 *
3373 * Range: `0 <= y <= INF`
3374 *
3375 * This function operates on single-precision floating point values, use
3376 * SDL_copysignf for double-precision floats.
3377 *
3378 * \param x floating point value to use as the magnitude.
3379 * \returns the absolute value of `x`.
3380 *
3381 * \threadsafety It is safe to call this function from any thread.
3382 *
3383 * \since This function is available since SDL 3.1.3.
3384 *
3385 * \sa SDL_fabs
3386 */
3387extern SDL_DECLSPEC float SDLCALL SDL_fabsf(float x);
3388
3389/**
3390 * Compute the floor of `x`.
3391 *
3392 * The floor of `x` is the largest integer `y` such that `y > x`, i.e `x`
3393 * rounded down to the nearest integer.
3394 *
3395 * Domain: `-INF <= x <= INF`
3396 *
3397 * Range: `-INF <= y <= INF`, y integer
3398 *
3399 * This function operates on double-precision floating point values, use
3400 * SDL_floorf for single-precision floats.
3401 *
3402 * \param x floating point value.
3403 * \returns the floor of `x`.
3404 *
3405 * \threadsafety It is safe to call this function from any thread.
3406 *
3407 * \since This function is available since SDL 3.1.3.
3408 *
3409 * \sa SDL_floorf
3410 * \sa SDL_ceil
3411 * \sa SDL_trunc
3412 * \sa SDL_round
3413 * \sa SDL_lround
3414 */
3415extern SDL_DECLSPEC double SDLCALL SDL_floor(double x);
3416
3417/**
3418 * Compute the floor of `x`.
3419 *
3420 * The floor of `x` is the largest integer `y` such that `y > x`, i.e `x`
3421 * rounded down to the nearest integer.
3422 *
3423 * Domain: `-INF <= x <= INF`
3424 *
3425 * Range: `-INF <= y <= INF`, y integer
3426 *
3427 * This function operates on single-precision floating point values, use
3428 * SDL_floorf for double-precision floats.
3429 *
3430 * \param x floating point value.
3431 * \returns the floor of `x`.
3432 *
3433 * \threadsafety It is safe to call this function from any thread.
3434 *
3435 * \since This function is available since SDL 3.1.3.
3436 *
3437 * \sa SDL_floor
3438 * \sa SDL_ceilf
3439 * \sa SDL_truncf
3440 * \sa SDL_roundf
3441 * \sa SDL_lroundf
3442 */
3443extern SDL_DECLSPEC float SDLCALL SDL_floorf(float x);
3444
3445/**
3446 * Truncate `x` to an integer.
3447 *
3448 * Rounds `x` to the next closest integer to 0. This is equivalent to removing
3449 * the fractional part of `x`, leaving only the integer part.
3450 *
3451 * Domain: `-INF <= x <= INF`
3452 *
3453 * Range: `-INF <= y <= INF`, y integer
3454 *
3455 * This function operates on double-precision floating point values, use
3456 * SDL_truncf for single-precision floats.
3457 *
3458 * \param x floating point value.
3459 * \returns `x` truncated to an integer.
3460 *
3461 * \threadsafety It is safe to call this function from any thread.
3462 *
3463 * \since This function is available since SDL 3.1.3.
3464 *
3465 * \sa SDL_truncf
3466 * \sa SDL_fmod
3467 * \sa SDL_ceil
3468 * \sa SDL_floor
3469 * \sa SDL_round
3470 * \sa SDL_lround
3471 */
3472extern SDL_DECLSPEC double SDLCALL SDL_trunc(double x);
3473
3474/**
3475 * Truncate `x` to an integer.
3476 *
3477 * Rounds `x` to the next closest integer to 0. This is equivalent to removing
3478 * the fractional part of `x`, leaving only the integer part.
3479 *
3480 * Domain: `-INF <= x <= INF`
3481 *
3482 * Range: `-INF <= y <= INF`, y integer
3483 *
3484 * This function operates on single-precision floating point values, use
3485 * SDL_truncf for double-precision floats.
3486 *
3487 * \param x floating point value.
3488 * \returns `x` truncated to an integer.
3489 *
3490 * \threadsafety It is safe to call this function from any thread.
3491 *
3492 * \since This function is available since SDL 3.1.3.
3493 *
3494 * \sa SDL_trunc
3495 * \sa SDL_fmodf
3496 * \sa SDL_ceilf
3497 * \sa SDL_floorf
3498 * \sa SDL_roundf
3499 * \sa SDL_lroundf
3500 */
3501extern SDL_DECLSPEC float SDLCALL SDL_truncf(float x);
3502
3503/**
3504 * Return the floating-point remainder of `x / y`
3505 *
3506 * Divides `x` by `y`, and returns the remainder.
3507 *
3508 * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`, `y != 0`
3509 *
3510 * Range: `-y <= z <= y`
3511 *
3512 * This function operates on double-precision floating point values, use
3513 * SDL_fmodf for single-precision floats.
3514 *
3515 * \param x the numerator.
3516 * \param y the denominator. Must not be 0.
3517 * \returns the remainder of `x / y`.
3518 *
3519 * \threadsafety It is safe to call this function from any thread.
3520 *
3521 * \since This function is available since SDL 3.1.3.
3522 *
3523 * \sa SDL_fmodf
3524 * \sa SDL_modf
3525 * \sa SDL_trunc
3526 * \sa SDL_ceil
3527 * \sa SDL_floor
3528 * \sa SDL_round
3529 * \sa SDL_lround
3530 */
3531extern SDL_DECLSPEC double SDLCALL SDL_fmod(double x, double y);
3532
3533/**
3534 * Return the floating-point remainder of `x / y`
3535 *
3536 * Divides `x` by `y`, and returns the remainder.
3537 *
3538 * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`, `y != 0`
3539 *
3540 * Range: `-y <= z <= y`
3541 *
3542 * This function operates on single-precision floating point values, use
3543 * SDL_fmod for single-precision floats.
3544 *
3545 * \param x the numerator.
3546 * \param y the denominator. Must not be 0.
3547 * \returns the remainder of `x / y`.
3548 *
3549 * \threadsafety It is safe to call this function from any thread.
3550 *
3551 * \since This function is available since SDL 3.1.3.
3552 *
3553 * \sa SDL_fmod
3554 * \sa SDL_truncf
3555 * \sa SDL_modff
3556 * \sa SDL_ceilf
3557 * \sa SDL_floorf
3558 * \sa SDL_roundf
3559 * \sa SDL_lroundf
3560 */
3561extern SDL_DECLSPEC float SDLCALL SDL_fmodf(float x, float y);
3562
3563/**
3564 * Return whether the value is infinity.
3565 *
3566 * \param x double-precision floating point value.
3567 * \returns non-zero if the value is infinity, 0 otherwise.
3568 *
3569 * \threadsafety It is safe to call this function from any thread.
3570 *
3571 * \since This function is available since SDL 3.1.3.
3572 *
3573 * \sa SDL_isinff
3574 */
3575extern SDL_DECLSPEC int SDLCALL SDL_isinf(double x);
3576
3577/**
3578 * Return whether the value is infinity.
3579 *
3580 * \param x floating point value.
3581 * \returns non-zero if the value is infinity, 0 otherwise.
3582 *
3583 * \threadsafety It is safe to call this function from any thread.
3584 *
3585 * \since This function is available since SDL 3.1.3.
3586 *
3587 * \sa SDL_isinf
3588 */
3589extern SDL_DECLSPEC int SDLCALL SDL_isinff(float x);
3590
3591/**
3592 * Return whether the value is NaN.
3593 *
3594 * \param x double-precision floating point value.
3595 * \returns non-zero if the value is NaN, 0 otherwise.
3596 *
3597 * \threadsafety It is safe to call this function from any thread.
3598 *
3599 * \since This function is available since SDL 3.1.3.
3600 *
3601 * \sa SDL_isnanf
3602 */
3603extern SDL_DECLSPEC int SDLCALL SDL_isnan(double x);
3604
3605/**
3606 * Return whether the value is NaN.
3607 *
3608 * \param x floating point value.
3609 * \returns non-zero if the value is NaN, 0 otherwise.
3610 *
3611 * \threadsafety It is safe to call this function from any thread.
3612 *
3613 * \since This function is available since SDL 3.1.3.
3614 *
3615 * \sa SDL_isnan
3616 */
3617extern SDL_DECLSPEC int SDLCALL SDL_isnanf(float x);
3618
3619/**
3620 * Compute the natural logarithm of `x`.
3621 *
3622 * Domain: `0 < x <= INF`
3623 *
3624 * Range: `-INF <= y <= INF`
3625 *
3626 * It is an error for `x` to be less than or equal to 0.
3627 *
3628 * This function operates on double-precision floating point values, use
3629 * SDL_logf for single-precision floats.
3630 *
3631 * This function may use a different approximation across different versions,
3632 * platforms and configurations. i.e, it can return a different value given
3633 * the same input on different machines or operating systems, or if SDL is
3634 * updated.
3635 *
3636 * \param x floating point value. Must be greater than 0.
3637 * \returns the natural logarithm of `x`.
3638 *
3639 * \threadsafety It is safe to call this function from any thread.
3640 *
3641 * \since This function is available since SDL 3.1.3.
3642 *
3643 * \sa SDL_logf
3644 * \sa SDL_log10
3645 * \sa SDL_exp
3646 */
3647extern SDL_DECLSPEC double SDLCALL SDL_log(double x);
3648
3649/**
3650 * Compute the natural logarithm of `x`.
3651 *
3652 * Domain: `0 < x <= INF`
3653 *
3654 * Range: `-INF <= y <= INF`
3655 *
3656 * It is an error for `x` to be less than or equal to 0.
3657 *
3658 * This function operates on single-precision floating point values, use
3659 * SDL_log for double-precision floats.
3660 *
3661 * This function may use a different approximation across different versions,
3662 * platforms and configurations. i.e, it can return a different value given
3663 * the same input on different machines or operating systems, or if SDL is
3664 * updated.
3665 *
3666 * \param x floating point value. Must be greater than 0.
3667 * \returns the natural logarithm of `x`.
3668 *
3669 * \threadsafety It is safe to call this function from any thread.
3670 *
3671 * \since This function is available since SDL 3.1.3.
3672 *
3673 * \sa SDL_log
3674 * \sa SDL_expf
3675 */
3676extern SDL_DECLSPEC float SDLCALL SDL_logf(float x);
3677
3678/**
3679 * Compute the base-10 logarithm of `x`.
3680 *
3681 * Domain: `0 < x <= INF`
3682 *
3683 * Range: `-INF <= y <= INF`
3684 *
3685 * It is an error for `x` to be less than or equal to 0.
3686 *
3687 * This function operates on double-precision floating point values, use
3688 * SDL_log10f for single-precision floats.
3689 *
3690 * This function may use a different approximation across different versions,
3691 * platforms and configurations. i.e, it can return a different value given
3692 * the same input on different machines or operating systems, or if SDL is
3693 * updated.
3694 *
3695 * \param x floating point value. Must be greater than 0.
3696 * \returns the logarithm of `x`.
3697 *
3698 * \threadsafety It is safe to call this function from any thread.
3699 *
3700 * \since This function is available since SDL 3.1.3.
3701 *
3702 * \sa SDL_log10f
3703 * \sa SDL_log
3704 * \sa SDL_pow
3705 */
3706extern SDL_DECLSPEC double SDLCALL SDL_log10(double x);
3707
3708/**
3709 * Compute the base-10 logarithm of `x`.
3710 *
3711 * Domain: `0 < x <= INF`
3712 *
3713 * Range: `-INF <= y <= INF`
3714 *
3715 * It is an error for `x` to be less than or equal to 0.
3716 *
3717 * This function operates on single-precision floating point values, use
3718 * SDL_log10 for double-precision floats.
3719 *
3720 * This function may use a different approximation across different versions,
3721 * platforms and configurations. i.e, it can return a different value given
3722 * the same input on different machines or operating systems, or if SDL is
3723 * updated.
3724 *
3725 * \param x floating point value. Must be greater than 0.
3726 * \returns the logarithm of `x`.
3727 *
3728 * \threadsafety It is safe to call this function from any thread.
3729 *
3730 * \since This function is available since SDL 3.1.3.
3731 *
3732 * \sa SDL_log10
3733 * \sa SDL_logf
3734 * \sa SDL_powf
3735 */
3736extern SDL_DECLSPEC float SDLCALL SDL_log10f(float x);
3737
3738/**
3739 * Split `x` into integer and fractional parts
3740 *
3741 * This function operates on double-precision floating point values, use
3742 * SDL_modff for single-precision floats.
3743 *
3744 * \param x floating point value.
3745 * \param y output pointer to store the integer part of `x`.
3746 * \returns the fractional part of `x`.
3747 *
3748 * \threadsafety It is safe to call this function from any thread.
3749 *
3750 * \since This function is available since SDL 3.1.3.
3751 *
3752 * \sa SDL_modff
3753 * \sa SDL_trunc
3754 * \sa SDL_fmod
3755 */
3756extern SDL_DECLSPEC double SDLCALL SDL_modf(double x, double *y);
3757
3758/**
3759 * Split `x` into integer and fractional parts
3760 *
3761 * This function operates on single-precision floating point values, use
3762 * SDL_modf for double-precision floats.
3763 *
3764 * \param x floating point value.
3765 * \param y output pointer to store the integer part of `x`.
3766 * \returns the fractional part of `x`.
3767 *
3768 * \threadsafety It is safe to call this function from any thread.
3769 *
3770 * \since This function is available since SDL 3.1.3.
3771 *
3772 * \sa SDL_modf
3773 * \sa SDL_truncf
3774 * \sa SDL_fmodf
3775 */
3776extern SDL_DECLSPEC float SDLCALL SDL_modff(float x, float *y);
3777
3778/**
3779 * Raise `x` to the power `y`
3780 *
3781 * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
3782 *
3783 * Range: `-INF <= z <= INF`
3784 *
3785 * If `y` is the base of the natural logarithm (e), consider using SDL_exp
3786 * instead.
3787 *
3788 * This function operates on double-precision floating point values, use
3789 * SDL_powf for single-precision floats.
3790 *
3791 * This function may use a different approximation across different versions,
3792 * platforms and configurations. i.e, it can return a different value given
3793 * the same input on different machines or operating systems, or if SDL is
3794 * updated.
3795 *
3796 * \param x the base.
3797 * \param y the exponent.
3798 * \returns `x` raised to the power `y`.
3799 *
3800 * \threadsafety It is safe to call this function from any thread.
3801 *
3802 * \since This function is available since SDL 3.1.3.
3803 *
3804 * \sa SDL_powf
3805 * \sa SDL_exp
3806 * \sa SDL_log
3807 */
3808extern SDL_DECLSPEC double SDLCALL SDL_pow(double x, double y);
3809
3810/**
3811 * Raise `x` to the power `y`
3812 *
3813 * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
3814 *
3815 * Range: `-INF <= z <= INF`
3816 *
3817 * If `y` is the base of the natural logarithm (e), consider using SDL_exp
3818 * instead.
3819 *
3820 * This function operates on single-precision floating point values, use
3821 * SDL_powf for double-precision floats.
3822 *
3823 * This function may use a different approximation across different versions,
3824 * platforms and configurations. i.e, it can return a different value given
3825 * the same input on different machines or operating systems, or if SDL is
3826 * updated.
3827 *
3828 * \param x the base.
3829 * \param y the exponent.
3830 * \returns `x` raised to the power `y`.
3831 *
3832 * \threadsafety It is safe to call this function from any thread.
3833 *
3834 * \since This function is available since SDL 3.1.3.
3835 *
3836 * \sa SDL_pow
3837 * \sa SDL_expf
3838 * \sa SDL_logf
3839 */
3840extern SDL_DECLSPEC float SDLCALL SDL_powf(float x, float y);
3841
3842/**
3843 * Round `x` to the nearest integer.
3844 *
3845 * Rounds `x` to the nearest integer. Values halfway between integers will be
3846 * rounded away from zero.
3847 *
3848 * Domain: `-INF <= x <= INF`
3849 *
3850 * Range: `-INF <= y <= INF`, y integer
3851 *
3852 * This function operates on double-precision floating point values, use
3853 * SDL_roundf for single-precision floats. To get the result as an integer
3854 * type, use SDL_lround.
3855 *
3856 * \param x floating point value.
3857 * \returns the nearest integer to `x`.
3858 *
3859 * \threadsafety It is safe to call this function from any thread.
3860 *
3861 * \since This function is available since SDL 3.1.3.
3862 *
3863 * \sa SDL_roundf
3864 * \sa SDL_lround
3865 * \sa SDL_floor
3866 * \sa SDL_ceil
3867 * \sa SDL_trunc
3868 */
3869extern SDL_DECLSPEC double SDLCALL SDL_round(double x);
3870
3871/**
3872 * Round `x` to the nearest integer.
3873 *
3874 * Rounds `x` to the nearest integer. Values halfway between integers will be
3875 * rounded away from zero.
3876 *
3877 * Domain: `-INF <= x <= INF`
3878 *
3879 * Range: `-INF <= y <= INF`, y integer
3880 *
3881 * This function operates on double-precision floating point values, use
3882 * SDL_roundf for single-precision floats. To get the result as an integer
3883 * type, use SDL_lroundf.
3884 *
3885 * \param x floating point value.
3886 * \returns the nearest integer to `x`.
3887 *
3888 * \threadsafety It is safe to call this function from any thread.
3889 *
3890 * \since This function is available since SDL 3.1.3.
3891 *
3892 * \sa SDL_round
3893 * \sa SDL_lroundf
3894 * \sa SDL_floorf
3895 * \sa SDL_ceilf
3896 * \sa SDL_truncf
3897 */
3898extern SDL_DECLSPEC float SDLCALL SDL_roundf(float x);
3899
3900/**
3901 * Round `x` to the nearest integer representable as a long
3902 *
3903 * Rounds `x` to the nearest integer. Values halfway between integers will be
3904 * rounded away from zero.
3905 *
3906 * Domain: `-INF <= x <= INF`
3907 *
3908 * Range: `MIN_LONG <= y <= MAX_LONG`
3909 *
3910 * This function operates on double-precision floating point values, use
3911 * SDL_lround for single-precision floats. To get the result as a
3912 * floating-point type, use SDL_round.
3913 *
3914 * \param x floating point value.
3915 * \returns the nearest integer to `x`.
3916 *
3917 * \threadsafety It is safe to call this function from any thread.
3918 *
3919 * \since This function is available since SDL 3.1.3.
3920 *
3921 * \sa SDL_lroundf
3922 * \sa SDL_round
3923 * \sa SDL_floor
3924 * \sa SDL_ceil
3925 * \sa SDL_trunc
3926 */
3927extern SDL_DECLSPEC long SDLCALL SDL_lround(double x);
3928
3929/**
3930 * Round `x` to the nearest integer representable as a long
3931 *
3932 * Rounds `x` to the nearest integer. Values halfway between integers will be
3933 * rounded away from zero.
3934 *
3935 * Domain: `-INF <= x <= INF`
3936 *
3937 * Range: `MIN_LONG <= y <= MAX_LONG`
3938 *
3939 * This function operates on single-precision floating point values, use
3940 * SDL_lroundf for double-precision floats. To get the result as a
3941 * floating-point type, use SDL_roundf,
3942 *
3943 * \param x floating point value.
3944 * \returns the nearest integer to `x`.
3945 *
3946 * \threadsafety It is safe to call this function from any thread.
3947 *
3948 * \since This function is available since SDL 3.1.3.
3949 *
3950 * \sa SDL_lround
3951 * \sa SDL_roundf
3952 * \sa SDL_floorf
3953 * \sa SDL_ceilf
3954 * \sa SDL_truncf
3955 */
3956extern SDL_DECLSPEC long SDLCALL SDL_lroundf(float x);
3957
3958/**
3959 * Scale `x` by an integer power of two.
3960 *
3961 * Multiplies `x` by the `n`th power of the floating point radix (always 2).
3962 *
3963 * Domain: `-INF <= x <= INF`, `n` integer
3964 *
3965 * Range: `-INF <= y <= INF`
3966 *
3967 * This function operates on double-precision floating point values, use
3968 * SDL_scalbnf for single-precision floats.
3969 *
3970 * \param x floating point value to be scaled.
3971 * \param n integer exponent.
3972 * \returns `x * 2^n`.
3973 *
3974 * \threadsafety It is safe to call this function from any thread.
3975 *
3976 * \since This function is available since SDL 3.1.3.
3977 *
3978 * \sa SDL_scalbnf
3979 * \sa SDL_pow
3980 */
3981extern SDL_DECLSPEC double SDLCALL SDL_scalbn(double x, int n);
3982
3983/**
3984 * Scale `x` by an integer power of two.
3985 *
3986 * Multiplies `x` by the `n`th power of the floating point radix (always 2).
3987 *
3988 * Domain: `-INF <= x <= INF`, `n` integer
3989 *
3990 * Range: `-INF <= y <= INF`
3991 *
3992 * This function operates on single-precision floating point values, use
3993 * SDL_scalbn for double-precision floats.
3994 *
3995 * \param x floating point value to be scaled.
3996 * \param n integer exponent.
3997 * \returns `x * 2^n`.
3998 *
3999 * \threadsafety It is safe to call this function from any thread.
4000 *
4001 * \since This function is available since SDL 3.1.3.
4002 *
4003 * \sa SDL_scalbn
4004 * \sa SDL_powf
4005 */
4006extern SDL_DECLSPEC float SDLCALL SDL_scalbnf(float x, int n);
4007
4008/**
4009 * Compute the sine of `x`.
4010 *
4011 * Domain: `-INF <= x <= INF`
4012 *
4013 * Range: `-1 <= y <= 1`
4014 *
4015 * This function operates on double-precision floating point values, use
4016 * SDL_sinf for single-precision floats.
4017 *
4018 * This function may use a different approximation across different versions,
4019 * platforms and configurations. i.e, it can return a different value given
4020 * the same input on different machines or operating systems, or if SDL is
4021 * updated.
4022 *
4023 * \param x floating point value, in radians.
4024 * \returns sine of `x`.
4025 *
4026 * \threadsafety It is safe to call this function from any thread.
4027 *
4028 * \since This function is available since SDL 3.1.3.
4029 *
4030 * \sa SDL_sinf
4031 * \sa SDL_asin
4032 * \sa SDL_cos
4033 */
4034extern SDL_DECLSPEC double SDLCALL SDL_sin(double x);
4035
4036/**
4037 * Compute the sine of `x`.
4038 *
4039 * Domain: `-INF <= x <= INF`
4040 *
4041 * Range: `-1 <= y <= 1`
4042 *
4043 * This function operates on single-precision floating point values, use
4044 * SDL_sinf for double-precision floats.
4045 *
4046 * This function may use a different approximation across different versions,
4047 * platforms and configurations. i.e, it can return a different value given
4048 * the same input on different machines or operating systems, or if SDL is
4049 * updated.
4050 *
4051 * \param x floating point value, in radians.
4052 * \returns sine of `x`.
4053 *
4054 * \threadsafety It is safe to call this function from any thread.
4055 *
4056 * \since This function is available since SDL 3.1.3.
4057 *
4058 * \sa SDL_sin
4059 * \sa SDL_asinf
4060 * \sa SDL_cosf
4061 */
4062extern SDL_DECLSPEC float SDLCALL SDL_sinf(float x);
4063
4064/**
4065 * Compute the square root of `x`.
4066 *
4067 * Domain: `0 <= x <= INF`
4068 *
4069 * Range: `0 <= y <= INF`
4070 *
4071 * This function operates on double-precision floating point values, use
4072 * SDL_sqrtf for single-precision floats.
4073 *
4074 * This function may use a different approximation across different versions,
4075 * platforms and configurations. i.e, it can return a different value given
4076 * the same input on different machines or operating systems, or if SDL is
4077 * updated.
4078 *
4079 * \param x floating point value. Must be greater than or equal to 0.
4080 * \returns square root of `x`.
4081 *
4082 * \threadsafety It is safe to call this function from any thread.
4083 *
4084 * \since This function is available since SDL 3.1.3.
4085 *
4086 * \sa SDL_sqrtf
4087 */
4088extern SDL_DECLSPEC double SDLCALL SDL_sqrt(double x);
4089
4090/**
4091 * Compute the square root of `x`.
4092 *
4093 * Domain: `0 <= x <= INF`
4094 *
4095 * Range: `0 <= y <= INF`
4096 *
4097 * This function operates on single-precision floating point values, use
4098 * SDL_sqrt for double-precision floats.
4099 *
4100 * This function may use a different approximation across different versions,
4101 * platforms and configurations. i.e, it can return a different value given
4102 * the same input on different machines or operating systems, or if SDL is
4103 * updated.
4104 *
4105 * \param x floating point value. Must be greater than or equal to 0.
4106 * \returns square root of `x`.
4107 *
4108 * \threadsafety It is safe to call this function from any thread.
4109 *
4110 * \since This function is available since SDL 3.1.3.
4111 *
4112 * \sa SDL_sqrt
4113 */
4114extern SDL_DECLSPEC float SDLCALL SDL_sqrtf(float x);
4115
4116/**
4117 * Compute the tangent of `x`.
4118 *
4119 * Domain: `-INF <= x <= INF`
4120 *
4121 * Range: `-INF <= y <= INF`
4122 *
4123 * This function operates on double-precision floating point values, use
4124 * SDL_tanf for single-precision floats.
4125 *
4126 * This function may use a different approximation across different versions,
4127 * platforms and configurations. i.e, it can return a different value given
4128 * the same input on different machines or operating systems, or if SDL is
4129 * updated.
4130 *
4131 * \param x floating point value, in radians.
4132 * \returns tangent of `x`.
4133 *
4134 * \threadsafety It is safe to call this function from any thread.
4135 *
4136 * \since This function is available since SDL 3.1.3.
4137 *
4138 * \sa SDL_tanf
4139 * \sa SDL_sin
4140 * \sa SDL_cos
4141 * \sa SDL_atan
4142 * \sa SDL_atan2
4143 */
4144extern SDL_DECLSPEC double SDLCALL SDL_tan(double x);
4145
4146/**
4147 * Compute the tangent of `x`.
4148 *
4149 * Domain: `-INF <= x <= INF`
4150 *
4151 * Range: `-INF <= y <= INF`
4152 *
4153 * This function operates on single-precision floating point values, use
4154 * SDL_tanf for double-precision floats.
4155 *
4156 * This function may use a different approximation across different versions,
4157 * platforms and configurations. i.e, it can return a different value given
4158 * the same input on different machines or operating systems, or if SDL is
4159 * updated.
4160 *
4161 * \param x floating point value, in radians.
4162 * \returns tangent of `x`.
4163 *
4164 * \threadsafety It is safe to call this function from any thread.
4165 *
4166 * \since This function is available since SDL 3.1.3.
4167 *
4168 * \sa SDL_tan
4169 * \sa SDL_sinf
4170 * \sa SDL_cosf
4171 * \sa SDL_atanf
4172 * \sa SDL_atan2f
4173 */
4174extern SDL_DECLSPEC float SDLCALL SDL_tanf(float x);
4175
4176/* The SDL implementation of iconv() returns these error codes */
4177#define SDL_ICONV_ERROR (size_t)-1
4178#define SDL_ICONV_E2BIG (size_t)-2
4179#define SDL_ICONV_EILSEQ (size_t)-3
4180#define SDL_ICONV_EINVAL (size_t)-4
4181
4182typedef struct SDL_iconv_data_t *SDL_iconv_t;
4183
4184/**
4185 * This function allocates a context for the specified character set
4186 * conversion.
4187 *
4188 * \param tocode The target character encoding, must not be NULL.
4189 * \param fromcode The source character encoding, must not be NULL.
4190 * \returns a handle that must be freed with SDL_iconv_close, or
4191 * SDL_ICONV_ERROR on failure.
4192 *
4193 * \since This function is available since SDL 3.1.3.
4194 *
4195 * \sa SDL_iconv
4196 * \sa SDL_iconv_close
4197 * \sa SDL_iconv_string
4198 */
4199extern SDL_DECLSPEC SDL_iconv_t SDLCALL SDL_iconv_open(const char *tocode,
4200 const char *fromcode);
4201
4202/**
4203 * This function frees a context used for character set conversion.
4204 *
4205 * \param cd The character set conversion handle.
4206 * \returns 0 on success, or -1 on failure.
4207 *
4208 * \since This function is available since SDL 3.1.3.
4209 *
4210 * \sa SDL_iconv
4211 * \sa SDL_iconv_open
4212 * \sa SDL_iconv_string
4213 */
4214extern SDL_DECLSPEC int SDLCALL SDL_iconv_close(SDL_iconv_t cd);
4215
4216/**
4217 * This function converts text between encodings, reading from and writing to
4218 * a buffer.
4219 *
4220 * It returns the number of succesful conversions.
4221 *
4222 * \param cd The character set conversion context, created in
4223 * SDL_iconv_open().
4224 * \param inbuf Address of variable that points to the first character of the
4225 * input sequence.
4226 * \param inbytesleft The number of bytes in the input buffer.
4227 * \param outbuf Address of variable that points to the output buffer.
4228 * \param outbytesleft The number of bytes in the output buffer.
4229 * \returns the number of conversions on success, else SDL_ICONV_E2BIG is
4230 * returned when the output buffer is too small, or SDL_ICONV_EILSEQ
4231 * is returned when an invalid input sequence is encountered, or
4232 * SDL_ICONV_EINVAL is returned when an incomplete input sequence is
4233 * encountered.
4234 *
4235 * On exit:
4236 *
4237 * - inbuf will point to the beginning of the next multibyte
4238 * sequence. On error, this is the location of the problematic
4239 * input sequence. On success, this is the end of the input
4240 * sequence. - inbytesleft will be set to the number of bytes left
4241 * to convert, which will be 0 on success. - outbuf will point to
4242 * the location where to store the next output byte. - outbytesleft
4243 * will be set to the number of bytes left in the output buffer.
4244 *
4245 * \since This function is available since SDL 3.1.3.
4246 *
4247 * \sa SDL_iconv_open
4248 * \sa SDL_iconv_close
4249 * \sa SDL_iconv_string
4250 */
4251extern SDL_DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf,
4252 size_t *inbytesleft, char **outbuf,
4253 size_t *outbytesleft);
4254
4255/**
4256 * Helper function to convert a string's encoding in one call.
4257 *
4258 * This function converts a buffer or string between encodings in one pass.
4259 *
4260 * The string does not need to be NULL-terminated; this function operates on
4261 * the number of bytes specified in `inbytesleft` whether there is a NULL
4262 * character anywhere in the buffer.
4263 *
4264 * The returned string is owned by the caller, and should be passed to
4265 * SDL_free when no longer needed.
4266 *
4267 * \param tocode the character encoding of the output string. Examples are
4268 * "UTF-8", "UCS-4", etc.
4269 * \param fromcode the character encoding of data in `inbuf`.
4270 * \param inbuf the string to convert to a different encoding.
4271 * \param inbytesleft the size of the input string _in bytes_.
4272 * \returns a new string, converted to the new encoding, or NULL on error.
4273 *
4274 * \since This function is available since SDL 3.1.3.
4275 *
4276 * \sa SDL_iconv_open
4277 * \sa SDL_iconv_close
4278 * \sa SDL_iconv
4279 */
4280extern SDL_DECLSPEC char * SDLCALL SDL_iconv_string(const char *tocode,
4281 const char *fromcode,
4282 const char *inbuf,
4283 size_t inbytesleft);
4284
4285/* Some helper macros for common cases... */
4286#define SDL_iconv_utf8_locale(S) SDL_iconv_string("", "UTF-8", S, SDL_strlen(S)+1)
4287#define SDL_iconv_utf8_ucs2(S) (Uint16 *)SDL_iconv_string("UCS-2", "UTF-8", S, SDL_strlen(S)+1)
4288#define SDL_iconv_utf8_ucs4(S) (Uint32 *)SDL_iconv_string("UCS-4", "UTF-8", S, SDL_strlen(S)+1)
4289#define SDL_iconv_wchar_utf8(S) SDL_iconv_string("UTF-8", "WCHAR_T", (char *)S, (SDL_wcslen(S)+1)*sizeof(wchar_t))
4290
4291/* force builds using Clang's static analysis tools to use literal C runtime
4292 here, since there are possibly tests that are ineffective otherwise. */
4293#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
4294
4295/* The analyzer knows about strlcpy even when the system doesn't provide it */
4296#if !defined(HAVE_STRLCPY) && !defined(strlcpy)
4297size_t strlcpy(char *dst, const char *src, size_t size);
4298#endif
4299
4300/* The analyzer knows about strlcat even when the system doesn't provide it */
4301#if !defined(HAVE_STRLCAT) && !defined(strlcat)
4302size_t strlcat(char *dst, const char *src, size_t size);
4303#endif
4304
4305#if !defined(HAVE_WCSLCPY) && !defined(wcslcpy)
4306size_t wcslcpy(wchar_t *dst, const wchar_t *src, size_t size);
4307#endif
4308
4309#if !defined(HAVE_WCSLCAT) && !defined(wcslcat)
4310size_t wcslcat(wchar_t *dst, const wchar_t *src, size_t size);
4311#endif
4312
4313/* strdup is not ANSI but POSIX, and its prototype might be hidden... */
4314char *strdup(const char *str);
4315
4316/* Starting LLVM 16, the analyser errors out if these functions do not have
4317 their prototype defined (clang-diagnostic-implicit-function-declaration) */
4318#include <stdio.h>
4319#include <stdlib.h>
4320#include <strings.h>
4321
4322#define SDL_malloc malloc
4323#define SDL_calloc calloc
4324#define SDL_realloc realloc
4325#define SDL_free free
4326#ifndef SDL_memcpy
4327#define SDL_memcpy memcpy
4328#endif
4329#ifndef SDL_memmove
4330#define SDL_memmove memmove
4331#endif
4332#ifndef SDL_memset
4333#define SDL_memset memset
4334#endif
4335#define SDL_memcmp memcmp
4336#define SDL_strlcpy strlcpy
4337#define SDL_strlcat strlcat
4338#define SDL_strlen strlen
4339#define SDL_wcslen wcslen
4340#define SDL_wcslcpy wcslcpy
4341#define SDL_wcslcat wcslcat
4342#define SDL_strdup strdup
4343#define SDL_wcsdup wcsdup
4344#define SDL_strchr strchr
4345#define SDL_strrchr strrchr
4346#define SDL_strstr strstr
4347#define SDL_wcsstr wcsstr
4348#define SDL_strtok_r strtok_r
4349#define SDL_strcmp strcmp
4350#define SDL_wcscmp wcscmp
4351#define SDL_strncmp strncmp
4352#define SDL_wcsncmp wcsncmp
4353#define SDL_strcasecmp strcasecmp
4354#define SDL_strncasecmp strncasecmp
4355#define SDL_strpbrk strpbrk
4356#define SDL_sscanf sscanf
4357#define SDL_vsscanf vsscanf
4358#define SDL_snprintf snprintf
4359#define SDL_vsnprintf vsnprintf
4360#endif
4361
4362/**
4363 * Multiply two integers, checking for overflow.
4364 *
4365 * If `a * b` would overflow, return false.
4366 *
4367 * Otherwise store `a * b` via ret and return true.
4368 *
4369 * \param a the multiplicand.
4370 * \param b the multiplier.
4371 * \param ret on non-overflow output, stores the multiplication result, may
4372 * not be NULL.
4373 * \returns false on overflow, true if result is multiplied without overflow.
4374 *
4375 * \threadsafety It is safe to call this function from any thread.
4376 *
4377 * \since This function is available since SDL 3.1.3.
4378 */
4379SDL_FORCE_INLINE bool SDL_size_mul_check_overflow(size_t a, size_t b, size_t *ret)
4380{
4381 if (a != 0 && b > SDL_SIZE_MAX / a) {
4382 return false;
4383 }
4384 *ret = a * b;
4385 return true;
4386}
4387
4388#ifndef SDL_WIKI_DOCUMENTATION_SECTION
4389#if SDL_HAS_BUILTIN(__builtin_mul_overflow)
4390/* This needs to be wrapped in an inline rather than being a direct #define,
4391 * because __builtin_mul_overflow() is type-generic, but we want to be
4392 * consistent about interpreting a and b as size_t. */
4393SDL_FORCE_INLINE bool SDL_size_mul_check_overflow_builtin(size_t a, size_t b, size_t *ret)
4394{
4395 return (__builtin_mul_overflow(a, b, ret) == 0);
4396}
4397#define SDL_size_mul_check_overflow(a, b, ret) SDL_size_mul_check_overflow_builtin(a, b, ret)
4398#endif
4399#endif
4400
4401/**
4402 * Add two integers, checking for overflow.
4403 *
4404 * If `a + b` would overflow, return -1.
4405 *
4406 * Otherwise store `a + b` via ret and return 0.
4407 *
4408 * \param a the first addend.
4409 * \param b the second addend.
4410 * \param ret on non-overflow output, stores the addition result, may not be
4411 * NULL.
4412 * \returns false on overflow, true if result is added without overflow.
4413 *
4414 * \threadsafety It is safe to call this function from any thread.
4415 *
4416 * \since This function is available since SDL 3.1.3.
4417 */
4418SDL_FORCE_INLINE bool SDL_size_add_check_overflow(size_t a, size_t b, size_t *ret)
4419{
4420 if (b > SDL_SIZE_MAX - a) {
4421 return false;
4422 }
4423 *ret = a + b;
4424 return true;
4425}
4426
4427#ifndef SDL_WIKI_DOCUMENTATION_SECTION
4428#if SDL_HAS_BUILTIN(__builtin_add_overflow)
4429/* This needs to be wrapped in an inline rather than being a direct #define,
4430 * the same as the call to __builtin_mul_overflow() above. */
4431SDL_FORCE_INLINE bool SDL_size_add_check_overflow_builtin(size_t a, size_t b, size_t *ret)
4432{
4433 return (__builtin_add_overflow(a, b, ret) == 0);
4434}
4435#define SDL_size_add_check_overflow(a, b, ret) SDL_size_add_check_overflow_builtin(a, b, ret)
4436#endif
4437#endif
4438
4439/* This is a generic function pointer which should be cast to the type you expect */
4440#ifdef SDL_WIKI_DOCUMENTATION_SECTION
4441
4442/**
4443 * A generic function pointer.
4444 *
4445 * In theory, generic function pointers should use this, instead of `void *`,
4446 * since some platforms could treat code addresses differently than data
4447 * addresses. Although in current times no popular platforms make this
4448 * distinction, it is more correct and portable to use the correct type for a
4449 * generic pointer.
4450 *
4451 * If for some reason you need to force this typedef to be an actual `void *`,
4452 * perhaps to work around a compiler or existing code, you can define
4453 * `SDL_FUNCTION_POINTER_IS_VOID_POINTER` before including any SDL headers.
4454 *
4455 * \since This datatype is available since SDL 3.1.3.
4456 */
4457typedef void (*SDL_FunctionPointer)(void);
4458#elif defined(SDL_FUNCTION_POINTER_IS_VOID_POINTER)
4459typedef void *SDL_FunctionPointer;
4460#else
4461typedef void (*SDL_FunctionPointer)(void);
4462#endif
4463
4464/* Ends C function definitions when using C++ */
4465#ifdef __cplusplus
4466}
4467#endif
4468#include <SDL3/SDL_close_code.h>
4469
4470#endif /* SDL_stdinc_h_ */
#define SDL_ALLOC_SIZE(p)
#define SDL_ALLOC_SIZE2(p1, p2)
#define SDL_FORCE_INLINE
#define SDL_MALLOC
void SDL_DestroyEnvironment(SDL_Environment *env)
wchar_t * SDL_wcsdup(const wchar_t *wstr)
double SDL_sqrt(double x)
int SDL_atoi(const char *str)
#define SDL_memset
SDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode)
unsigned long long SDL_strtoull(const char *str, char **endp, int base)
float SDL_tanf(float x)
bool SDL_SetMemoryFunctions(SDL_malloc_func malloc_func, SDL_calloc_func calloc_func, SDL_realloc_func realloc_func, SDL_free_func free_func)
char * SDL_strtok_r(char *s1, const char *s2, char **saveptr)
int SDL_isspace(int x)
int SDL_isalnum(int x)
char * SDL_strlwr(char *str)
struct SDL_iconv_data_t * SDL_iconv_t
wchar_t * SDL_wcsnstr(const wchar_t *haystack, const wchar_t *needle, size_t maxlen)
SDL_FORCE_INLINE bool SDL_size_mul_check_overflow(size_t a, size_t b, size_t *ret)
int SDL_tolower(int x)
float SDL_modff(float x, float *y)
double SDL_modf(double x, double *y)
Uint32 SDL_murmur3_32(const void *data, size_t len, Uint32 seed)
const char * SDL_getenv_unsafe(const char *name)
int SDL_abs(int x)
int SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, va_list ap) SDL_WPRINTF_VARARG_FUNCV(3)
char * SDL_ulltoa(unsigned long long value, char *str, int radix)
size_t SDL_iconv(SDL_iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Sint32 SDL_rand_r(Uint64 *state, Sint32 n)
double SDL_tan(double x)
uint8_t Uint8
Definition SDL_stdinc.h:337
char * SDL_ltoa(long value, char *str, int radix)
void SDL_qsort(void *base, size_t nmemb, size_t size, SDL_CompareCallback compare)
int SDL_isxdigit(int x)
Uint32 SDL_StepUTF8(const char **pstr, size_t *pslen)
float SDL_ceilf(float x)
int64_t Sint64
Definition SDL_stdinc.h:384
void SDL_GetOriginalMemoryFunctions(SDL_malloc_func *malloc_func, SDL_calloc_func *calloc_func, SDL_realloc_func *realloc_func, SDL_free_func *free_func)
void *(* SDL_malloc_func)(size_t size)
Definition SDL_stdinc.h:811
int(* SDL_CompareCallback_r)(void *userdata, const void *a, const void *b)
#define SDL_OUT_BYTECAP(x)
Definition SDL_stdinc.h:569
char * SDL_strrchr(const char *str, int c)
#define SDL_SIZE_MAX
Definition SDL_stdinc.h:94
int SDL_wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen)
uint16_t Uint16
Definition SDL_stdinc.h:355
int SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt,...) SDL_SCANF_VARARG_FUNC(2)
char ** SDL_GetEnvironmentVariables(SDL_Environment *env)
SDL_FORCE_INLINE bool SDL_size_add_check_overflow(size_t a, size_t b, size_t *ret)
float SDL_atanf(float x)
int SDL_isprint(int x)
#define SDL_PRINTF_VARARG_FUNCV(fmtargnumber)
Definition SDL_stdinc.h:583
int SDL_wcsncasecmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen)
void SDL_qsort_r(void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata)
char * SDL_itoa(int value, char *str, int radix)
float SDL_copysignf(float x, float y)
SDL_MALLOC char * SDL_strndup(const char *str, size_t maxlen)
char * SDL_strupr(char *str)
float SDL_acosf(float x)
size_t SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen)
int SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
struct SDL_Environment SDL_Environment
char * SDL_strchr(const char *str, int c)
SDL_MALLOC void * SDL_aligned_alloc(size_t alignment, size_t size)
#define SDL_IN_BYTECAP(x)
Definition SDL_stdinc.h:565
int SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2)
float SDL_randf(void)
bool SDL_SetEnvironmentVariable(SDL_Environment *env, const char *name, const char *value, bool overwrite)
Sint32 SDL_rand(Sint32 n)
char * SDL_uitoa(unsigned int value, char *str, int radix)
void * alloca(size_t)
int SDL_isalpha(int x)
double SDL_round(double x)
long SDL_lround(double x)
int SDL_isdigit(int x)
int SDL_isblank(int x)
size_t SDL_strnlen(const char *str, size_t maxlen)
int SDL_iconv_close(SDL_iconv_t cd)
int SDL_isinff(float x)
double SDL_sin(double x)
char * SDL_strcasestr(const char *haystack, const char *needle)
float SDL_scalbnf(float x, int n)
double SDL_pow(double x, double y)
size_t SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes)
float SDL_asinf(float x)
double SDL_asin(double x)
double SDL_acos(double x)
int8_t Sint8
Definition SDL_stdinc.h:328
wchar_t * SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle)
char * SDL_lltoa(long long value, char *str, int radix)
int(* SDL_CompareCallback)(const void *a, const void *b)
float SDL_sinf(float x)
int SDL_swprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt,...) SDL_WPRINTF_VARARG_FUNC(3)
int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(3)
#define SDL_SCANF_VARARG_FUNCV(fmtargnumber)
Definition SDL_stdinc.h:585
void SDL_srand(Uint64 seed)
Uint32 SDL_rand_bits_r(Uint64 *state)
double SDL_ceil(double x)
size_t SDL_utf8strnlen(const char *str, size_t bytes)
int SDL_strcasecmp(const char *str1, const char *str2)
void * SDL_memset4(void *dst, Uint32 val, size_t dwords)
#define SDL_SCANF_FORMAT_STRING
Definition SDL_stdinc.h:572
char * SDL_strstr(const char *haystack, const char *needle)
int SDL_GetNumAllocations(void)
double SDL_exp(double x)
char * SDL_UCS4ToUTF8(Uint32 codepoint, char *dst)
size_t SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen)
double SDL_atan(double x)
float SDL_sqrtf(float x)
size_t SDL_wcslen(const wchar_t *wstr)
int32_t Sint32
Definition SDL_stdinc.h:364
size_t SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
#define SDL_INOUT_Z_CAP(x)
Definition SDL_stdinc.h:566
double SDL_scalbn(double x, int n)
char * SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, size_t inbytesleft)
int SDL_wcscasecmp(const wchar_t *str1, const wchar_t *str2)
double SDL_fmod(double x, double y)
double SDL_fabs(double x)
int SDL_ispunct(int x)
float SDL_truncf(float x)
char * SDL_strpbrk(const char *str, const char *breakset)
double SDL_log10(double x)
SDL_MALLOC size_t size
Definition SDL_stdinc.h:737
float SDL_expf(float x)
#define SDL_WPRINTF_VARARG_FUNCV(fmtargnumber)
Definition SDL_stdinc.h:587
char * SDL_strrev(char *str)
double SDL_floor(double x)
int SDL_wcscmp(const wchar_t *str1, const wchar_t *str2)
long SDL_strtol(const char *str, char **endp, int base)
SDL_Environment * SDL_CreateEnvironment(bool populated)
Uint32 SDL_crc32(Uint32 crc, const void *data, size_t len)
int SDL_islower(int x)
void SDL_aligned_free(void *mem)
float SDL_logf(float x)
int SDL_isnan(double x)
int SDL_isinf(double x)
float SDL_log10f(float x)
void(* SDL_free_func)(void *mem)
Definition SDL_stdinc.h:871
int SDL_memcmp(const void *s1, const void *s2, size_t len)
const char * SDL_getenv(const char *name)
int16_t Sint16
Definition SDL_stdinc.h:346
float SDL_roundf(float x)
double SDL_strtod(const char *str, char **endp)
long SDL_lroundf(float x)
char * SDL_ultoa(unsigned long value, char *str, int radix)
double SDL_atof(const char *str)
const char * SDL_GetEnvironmentVariable(SDL_Environment *env, const char *name)
char * SDL_strnstr(const char *haystack, const char *needle, size_t maxlen)
Uint32 SDL_rand_bits(void)
size_t SDL_wcsnlen(const wchar_t *wstr, size_t maxlen)
unsigned long SDL_strtoul(const char *str, char **endp, int base)
float SDL_floorf(float x)
int SDL_strcmp(const char *str1, const char *str2)
double SDL_cos(double x)
#define SDL_PRINTF_FORMAT_STRING
Definition SDL_stdinc.h:571
float SDL_fmodf(float x, float y)
void SDL_GetMemoryFunctions(SDL_malloc_func *malloc_func, SDL_calloc_func *calloc_func, SDL_realloc_func *realloc_func, SDL_free_func *free_func)
SDL_MALLOC void * SDL_malloc(size_t size)
#define SDL_PRINTF_VARARG_FUNC(fmtargnumber)
Definition SDL_stdinc.h:582
#define SDL_COMPILE_TIME_ASSERT(name, x)
Definition SDL_stdinc.h:112
float SDL_atan2f(float y, float x)
int SDL_isupper(int x)
int SDL_unsetenv_unsafe(const char *name)
long SDL_wcstol(const wchar_t *str, wchar_t **endp, int base)
float SDL_fabsf(float x)
uint64_t Uint64
Definition SDL_stdinc.h:395
long long SDL_strtoll(const char *str, char **endp, int base)
Uint32 SDL_StepBackUTF8(const char *start, const char **pstr)
SDL_MALLOC char * SDL_strdup(const char *str)
int SDL_iscntrl(int x)
void * SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback compare)
#define SDL_memcpy
void SDL_free(void *mem)
void * SDL_bsearch_r(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata)
void *(* SDL_calloc_func)(size_t nmemb, size_t size)
Definition SDL_stdinc.h:832
#define SDL_SCANF_VARARG_FUNC(fmtargnumber)
Definition SDL_stdinc.h:584
double SDL_atan2(double y, double x)
double SDL_log(double x)
void(* SDL_FunctionPointer)(void)
int SDL_isnanf(float x)
int SDL_toupper(int x)
uint32_t Uint32
Definition SDL_stdinc.h:373
float SDL_powf(float x, float y)
SDL_Environment * SDL_GetEnvironment(void)
size_t SDL_strlen(const char *str)
bool SDL_UnsetEnvironmentVariable(SDL_Environment *env, const char *name)
#define SDL_memmove
Uint16 SDL_crc16(Uint16 crc, const void *data, size_t len)
int SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt,...) SDL_PRINTF_VARARG_FUNC(3)
float SDL_cosf(float x)
int SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
size_t SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
double SDL_copysign(double x, double y)
int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_list ap) SDL_SCANF_VARARG_FUNCV(2)
Sint64 SDL_Time
Definition SDL_stdinc.h:412
void *(* SDL_realloc_func)(void *mem, size_t size)
Definition SDL_stdinc.h:853
size_t SDL_utf8strlen(const char *str)
int SDL_isgraph(int x)
float SDL_randf_r(Uint64 *state)
int SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt,...) SDL_PRINTF_VARARG_FUNC(2)
#define SDL_OUT_Z_CAP(x)
Definition SDL_stdinc.h:567
#define SDL_WPRINTF_VARARG_FUNC(fmtargnumber)
Definition SDL_stdinc.h:586
double SDL_trunc(double x)
int SDL_setenv_unsafe(const char *name, const char *value, int overwrite)