Skip to content

Commit cd4a490

Browse files
ericonrebassi
authored andcommitted
ray: simplify nan checking by creating a macro.
Avoids the #ifdef forest and code duplication resulting from it. There was mismatch in the two code paths, see [1] and [2], and this commit avoids repeating the same mistake. While there, use isnan() instead of fpclassify() == FP_NAN for the case where isnanf() isn't available. We use isnanf() (if available) instead of isnan() due to [3]: I initially used isinf() and isnan(), but those ended up breaking when using GCC because it tried to promote floats to doubles, and the results wouldn't match any more—especially when using GCC vectorisation. It could very well be a bug in GCC. [1] #223 [2] https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/3976 [3] #174 (comment)
1 parent f532ede commit cd4a490

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

src/graphene-private.h

+8
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@
105105

106106
#endif /* __GNUC__ */
107107

108+
#if defined(_M_X64) && ! defined(isnanf)
109+
# define graphene_isnan(x) _isnanf(x)
110+
#elif defined(HAVE_ISNANF)
111+
# define graphene_isnan(x) isnanf(x)
112+
#else
113+
# define graphene_isnan(x) isnan(x)
114+
#endif
115+
108116
static inline bool
109117
graphene_approx_val (float a, float b)
110118
{

src/graphene-ray.c

+4-23
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@
5252
#include <math.h>
5353
#include <float.h>
5454

55-
#if defined(_M_X64) && ! defined(isnanf)
56-
# define isnanf(x) _isnanf(x)
57-
# define HAVE_ISNANF
58-
#endif
59-
6055
/**
6156
* graphene_ray_alloc: (constructor)
6257
*
@@ -555,17 +550,10 @@ graphene_ray_intersect_box (const graphene_ray_t *r,
555550
/* These lines also handle the case where tx_min or tx_max is NaN
556551
* (result of 0 * INFINITY): NaN != NaN
557552
*/
558-
#ifdef HAVE_ISNANF
559-
if (ty_min > tx_min || isnanf (tx_min))
560-
tx_min = ty_min;
561-
if (ty_max < tx_max || isnanf (tx_max))
562-
tx_max = ty_max;
563-
#else
564-
if (ty_min > tx_min || fpclassify (tx_min) == FP_NAN)
553+
if (ty_min > tx_min || graphene_isnan (tx_min))
565554
tx_min = ty_min;
566-
if (ty_max > tx_max || fpclassify (tx_max) == FP_NAN)
555+
if (ty_max < tx_max || graphene_isnan (tx_max))
567556
tx_max = ty_max;
568-
#endif
569557

570558
float tz_min, tz_max;
571559
if (graphene_vec3_get_z (&inv_dir) >= 0.f)
@@ -582,17 +570,10 @@ graphene_ray_intersect_box (const graphene_ray_t *r,
582570
if ((tx_min > tz_max) || (tz_min > tx_max))
583571
return GRAPHENE_RAY_INTERSECTION_KIND_NONE;
584572

585-
#ifdef HAVE_ISNANF
586-
if (tz_min > tx_min || isnanf (tx_min))
587-
tx_min = tz_min;
588-
if (tz_max < tx_max || isnanf (tx_max))
589-
tx_max = tz_max;
590-
#else
591-
if (tz_min > tx_min || fpclassify (tx_min) == FP_NAN)
573+
if (tz_min > tx_min || graphene_isnan (tx_min))
592574
tx_min = tz_min;
593-
if (tz_max < tx_max || fpclassify (tx_max) == FP_NAN)
575+
if (tz_max < tx_max || graphene_isnan (tx_max))
594576
tx_max = tz_max;
595-
#endif
596577

597578
/* return the point closest to the ray (positive side) */
598579
if (tx_max < 0)

0 commit comments

Comments
 (0)