Skip to content

Commit

Permalink
Added SDL_nextafter() and SDL_nextafterf()
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jun 13, 2023
1 parent b0677f4 commit bc5d074
Show file tree
Hide file tree
Showing 20 changed files with 319 additions and 47 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ if(SDL_LIBC)
set(${_HAVE_H} 1)
endforeach()
set(HAVE_SIGNAL_H 1)
foreach(_FN abs acos acosf asin asinf atan atan2 atan2f atanf atof atoi bsearch calloc ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf fmod fmodf free itoa log log10 log10f logf lround lroundf _ltoa malloc memcmp memcpy memmove memset modf modff pow powf qsort realloc round roundf scalbn scalbnf sin sinf sqrt sqrtf sscanf strchr strcmp _stricmp strlen _strlwr strncmp _strnicmp strrchr _strrev strstr strtod strtol strtoll strtoul _strupr tan tanf trunc truncf _ultoa wcscmp _wcsdup wcsdup _wcsicmp wcslen wcsncmp _wcsnicmp wcsstr wcstol)
foreach(_FN abs acos acosf asin asinf atan atan2 atan2f atanf atof atoi bsearch calloc ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf fmod fmodf free itoa log log10 log10f logf lround lroundf _ltoa malloc memcmp memcpy memmove memset modf modff nextafter nextafterf pow powf qsort realloc round roundf scalbn scalbnf sin sinf sqrt sqrtf sscanf strchr strcmp _stricmp strlen _strlwr strncmp _strnicmp strrchr _strrev strstr strtod strtol strtoll strtoul _strupr tan tanf trunc truncf _ultoa wcscmp _wcsdup wcsdup _wcsicmp wcslen wcsncmp _wcsnicmp wcsstr wcstol)
string(TOUPPER ${_FN} _UPPER)
set(HAVE_${_UPPER} 1)
endforeach()
Expand Down Expand Up @@ -1167,8 +1167,8 @@ if(SDL_LIBC)
foreach(_FN
atan atan2 atanf atan2f ceil ceilf copysign copysignf cos cosf
exp expf fabs fabsf floor floorf fmod fmodf log logf log10 log10f
lround lroundf pow powf round roundf scalbn scalbnf sin sinf sqrt
sqrtf tan tanf acos acosf asin asinf trunc truncf)
lround lroundf modf modff nextafter nextafterf pow powf round roundf
scalbn scalbnf sin sinf sqrt sqrtf tan tanf acos acosf asin asinf trunc truncf)
string(TOUPPER ${_FN} _UPPER)
set(LIBC_HASVAR "LIBC_HAS_${_UPPER}")
check_symbol_exists("${_FN}" "math.h" ${LIBC_HASVAR})
Expand Down
4 changes: 3 additions & 1 deletion VisualC-GDK/SDL/SDL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,8 @@
<ClCompile Include="..\..\src\libm\s_fabs.c" />
<ClCompile Include="..\..\src\libm\s_floor.c" />
<ClCompile Include="..\..\src\libm\s_modf.c" />
<ClCompile Include="..\..\src\libm\s_nextafter.c" />
<ClCompile Include="..\..\src\libm\s_nextafterf.c" />
<ClCompile Include="..\..\src\libm\s_scalbn.c" />
<ClCompile Include="..\..\src\libm\s_sin.c" />
<ClCompile Include="..\..\src\libm\s_tan.c" />
Expand Down Expand Up @@ -785,4 +787,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
8 changes: 7 additions & 1 deletion VisualC-GDK/SDL/SDL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,12 @@
<ClCompile Include="..\..\src\libm\s_modf.c">
<Filter>libm</Filter>
</ClCompile>
<ClCompile Include="..\..\src\libm\s_nextafter.c">
<Filter>libm</Filter>
</ClCompile>
<ClCompile Include="..\..\src\libm\s_nextafterf.c">
<Filter>libm</Filter>
</ClCompile>
<ClCompile Include="..\..\src\libm\s_scalbn.c">
<Filter>libm</Filter>
</ClCompile>
Expand Down Expand Up @@ -1386,4 +1392,4 @@
<ItemGroup>
<ResourceCompile Include="..\..\src\core\windows\version.rc" />
</ItemGroup>
</Project>
</Project>
2 changes: 2 additions & 0 deletions VisualC/SDL/SDL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@
<ClCompile Include="..\..\src\libm\s_fabs.c" />
<ClCompile Include="..\..\src\libm\s_floor.c" />
<ClCompile Include="..\..\src\libm\s_modf.c" />
<ClCompile Include="..\..\src\libm\s_nextafter.c" />
<ClCompile Include="..\..\src\libm\s_nextafterf.c" />
<ClCompile Include="..\..\src\libm\s_scalbn.c" />
<ClCompile Include="..\..\src\libm\s_sin.c" />
<ClCompile Include="..\..\src\libm\s_tan.c" />
Expand Down
6 changes: 6 additions & 0 deletions VisualC/SDL/SDL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,12 @@
<ClCompile Include="..\..\src\libm\s_modf.c">
<Filter>libm</Filter>
</ClCompile>
<ClCompile Include="..\..\src\libm\s_nextafter.c">
<Filter>libm</Filter>
</ClCompile>
<ClCompile Include="..\..\src\libm\s_nextafterf.c">
<Filter>libm</Filter>
</ClCompile>
<ClCompile Include="..\..\src\libm\s_scalbn.c">
<Filter>libm</Filter>
</ClCompile>
Expand Down
2 changes: 2 additions & 0 deletions include/SDL3/SDL_stdinc.h
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,8 @@ extern DECLSPEC double SDLCALL SDL_log10(double x);
extern DECLSPEC float SDLCALL SDL_log10f(float x);
extern DECLSPEC double SDLCALL SDL_modf(double x, double *y);
extern DECLSPEC float SDLCALL SDL_modff(float x, float *y);
extern DECLSPEC double SDLCALL SDL_nextafter(double from, double to);
extern DECLSPEC float SDLCALL SDL_nextafterf(float from, float to);
extern DECLSPEC double SDLCALL SDL_pow(double x, double y);
extern DECLSPEC float SDLCALL SDL_powf(float x, float y);
extern DECLSPEC double SDLCALL SDL_round(double x);
Expand Down
2 changes: 2 additions & 0 deletions include/build_config/SDL_build_config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@
#cmakedefine HAVE_LROUNDF 1
#cmakedefine HAVE_MODF 1
#cmakedefine HAVE_MODFF 1
#cmakedefine HAVE_NEXTAFTER 1
#cmakedefine HAVE_NEXTAFTERF 1
#cmakedefine HAVE_POW 1
#cmakedefine HAVE_POWF 1
#cmakedefine HAVE_ROUND 1
Expand Down
3 changes: 3 additions & 0 deletions include/build_config/SDL_build_config_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
#define HAVE_LROUND 1
#define HAVE_LROUNDF 1
#define HAVE_MODF 1
#define HAVE_MODFF 1
#define HAVE_NEXTAFTER 1
#define HAVE_NEXTAFTERF 1
#define HAVE_POW 1
#define HAVE_POWF 1
#define HAVE_ROUND 1
Expand Down
3 changes: 3 additions & 0 deletions include/build_config/SDL_build_config_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@
#define HAVE_LROUND 1
#define HAVE_LROUNDF 1
#define HAVE_MODF 1
#define HAVE_MODFF 1
#define HAVE_NEXTAFTER 1
#define HAVE_NEXTAFTERF 1
#define HAVE_POW 1
#define HAVE_POWF 1
#define HAVE_ROUND 1
Expand Down
3 changes: 3 additions & 0 deletions include/build_config/SDL_build_config_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
#define HAVE_LROUND 1
#define HAVE_LROUNDF 1
#define HAVE_MODF 1
#define HAVE_MODFF 1
#define HAVE_NEXTAFTER 1
#define HAVE_NEXTAFTERF 1
#define HAVE_POW 1
#define HAVE_POWF 1
#define HAVE_ROUND 1
Expand Down
3 changes: 3 additions & 0 deletions include/build_config/SDL_build_config_winrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@
#define HAVE_LROUND 1
#define HAVE_LROUNDF 1
#define HAVE_MODF 1
#define HAVE_MODFF 1
#define HAVE_NEXTAFTER 1
#define HAVE_NEXTAFTERF 1
#define HAVE_POW 1
#define HAVE_POWF 1
#define HAVE_ROUND 1
Expand Down
2 changes: 2 additions & 0 deletions src/dynapi/SDL_dynapi.sym
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,8 @@ SDL3_0.0.0 {
SDL_hid_get_report_descriptor;
SDL_HasWindowSurface;
SDL_DestroyWindowSurface;
SDL_nextafter;
SDL_nextafterf;
# extra symbols go here (don't modify this line)
local: *;
};
2 changes: 2 additions & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -892,3 +892,5 @@
#define SDL_hid_get_report_descriptor SDL_hid_get_report_descriptor_REAL
#define SDL_HasWindowSurface SDL_HasWindowSurface_REAL
#define SDL_DestroyWindowSurface SDL_DestroyWindowSurface_REAL
#define SDL_nextafter SDL_nextafter_REAL
#define SDL_nextafterf SDL_nextafterf_REAL
2 changes: 2 additions & 0 deletions src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -937,3 +937,5 @@ SDL_DYNAPI_PROC(SDL_hid_device_info*,SDL_hid_get_device_info,(SDL_hid_device *a)
SDL_DYNAPI_PROC(int,SDL_hid_get_report_descriptor,(SDL_hid_device *a, unsigned char *b, size_t c),(a,b,c),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_HasWindowSurface,(SDL_Window *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_DestroyWindowSurface,(SDL_Window *a),(a),return)
SDL_DYNAPI_PROC(double,SDL_nextafter,(double a, double b),(a,b),return)
SDL_DYNAPI_PROC(float,SDL_nextafterf,(float a, float b),(a,b),return)
2 changes: 2 additions & 0 deletions src/libm/math_libm.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ double SDL_uclibc_fmod(double x, double y);
double SDL_uclibc_log(double x);
double SDL_uclibc_log10(double x);
double SDL_uclibc_modf(double x, double *y);
double SDL_uclibc_nextafter(double from, double to);
float SDL_uclibc_nextafterf(float from, float to);
double SDL_uclibc_pow(double x, double y);
double SDL_uclibc_scalbn(double x, int n);
double SDL_uclibc_sin(double x);
Expand Down
5 changes: 5 additions & 0 deletions src/libm/math_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ typedef unsigned int u_int32_t;
#define __ieee754_log SDL_uclibc_log
#define __ieee754_log10 SDL_uclibc_log10
#define modf SDL_uclibc_modf
#define nextafter SDL_uclibc_nextafter
#define nextafterf SDL_uclibc_nextafterf
#define __ieee754_pow SDL_uclibc_pow
#define scalbln SDL_uclibc_scalbln
#define scalbn SDL_uclibc_scalbn
Expand Down Expand Up @@ -225,4 +227,7 @@ __ieee754_sqrt(double)
extern int32_t __kernel_rem_pio2(const double *, double *, int, int, const unsigned int,
const int32_t *) attribute_hidden;

/* FIXME: We don't have a cross-platform implementation of this */
#define math_force_eval(x) (void)x

#endif /* _MATH_PRIVATE_H_ */
71 changes: 71 additions & 0 deletions src/libm/s_nextafter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "SDL_internal.h"
/*
* ====================================================
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunPro, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ====================================================
*/

/* IEEE functions
* nextafter(x,y)
* return the next machine floating-point number of x in the
* direction toward y.
* Special cases:
*/

#include "math.h"
#include "math_private.h"

double nextafter(double x, double y)
{
int32_t hx,hy,ix,iy;
u_int32_t lx,ly;

EXTRACT_WORDS(hx,lx,x);
EXTRACT_WORDS(hy,ly,y);
ix = hx&0x7fffffff; /* |x| */
iy = hy&0x7fffffff; /* |y| */

if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */
((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */
return x+y;
if(x==y) return y; /* x=y, return y */
if((ix|lx)==0) { /* x == 0 */
INSERT_WORDS(x,hy&0x80000000,1); /* return +-minsubnormal */
y = x*x;
if(y==x) return y; else return x; /* raise underflow flag */
}
if(hx>=0) { /* x > 0 */
if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */
if(lx==0) hx -= 1;
lx -= 1;
} else { /* x < y, x += ulp */
lx += 1;
if(lx==0) hx += 1;
}
} else { /* x < 0 */
if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */
if(lx==0) hx -= 1;
lx -= 1;
} else { /* x > y, x += ulp */
lx += 1;
if(lx==0) hx += 1;
}
}
hy = hx&0x7ff00000;
if(hy>=0x7ff00000) return x+x; /* overflow */
if(hy<0x00100000) { /* underflow */
y = x*x;
if(y!=x) { /* raise underflow flag */
INSERT_WORDS(y,hx,lx);
return y;
}
}
INSERT_WORDS(x,hx,lx);
return x;
}
libm_hidden_def(nextafter)
97 changes: 97 additions & 0 deletions src/libm/s_nextafterf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "SDL_internal.h"
/* s_nextafterf.c -- float version of s_nextafter.c.
* Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
*/

/*
* ====================================================
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunPro, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ====================================================
*/

#include "math.h"
#include "math_private.h"

float nextafterf(float x, float y)
{
int32_t hx, hy, ix, iy;

GET_FLOAT_WORD(hx, x);
GET_FLOAT_WORD(hy, y);
ix = hx & 0x7fffffff; /* |x| */
iy = hy & 0x7fffffff; /* |y| */

/* x is nan or y is nan? */
if ((ix > 0x7f800000) || (iy > 0x7f800000))
return x + y;

if (x == y)
return y;

if (ix == 0) { /* x == 0? */
/* glibc 2.4 does not seem to set underflow? */
/* float u; */
/* return +-minsubnormal */
SET_FLOAT_WORD(x, (hy & 0x80000000) | 1);
/* u = x * x; raise underflow flag */
/* math_force_eval(u); */
return x;
}

if (hx >= 0) { /* x > 0 */
if (hx > hy) { /* x > y: x -= ulp */
hx -= 1;
} else { /* x < y: x += ulp */
hx += 1;
}
} else { /* x < 0 */
if (hy >= 0 || hx > hy) { /* x < y: x -= ulp */
hx -= 1;
} else { /* x > y: x += ulp */
hx += 1;
}
}
hy = hx & 0x7f800000;
if (hy >= 0x7f800000) {
x = x + x; /* overflow */
return x; /* overflow */
}
if (hy < 0x00800000) {
float u = x * x; /* underflow */
math_force_eval(u); /* raise underflow flag */
}
SET_FLOAT_WORD(x, hx);
return x;
}

#if 0
/* "testprog N a b"
* calculates a = nextafterf(a, b) and prints a as float
* and as raw bytes; repeats it N times.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char **argv)
{
int cnt, i;
float a, b;
cnt = atoi(argv[1]);
a = strtod(argv[2], NULL);
b = strtod(argv[3], NULL);
while (cnt-- > 0) {
for (i = 0; i < sizeof(a); i++) {
unsigned char c = ((char*)(&a))[i];
printf("%x%x", (c >> 4), (c & 0xf));
}
printf(" %f\n", a);
a = nextafterf(a, b);
}
return 0;
}
#endif
Loading

0 comments on commit bc5d074

Please sign in to comment.