[Mesa-dev] [PATCH 2/5] glsl: Extend s-expression parsing to handle infinity.

Kenneth Graunke kenneth at whitecape.org
Thu Oct 27 20:13:03 PDT 2011


On 10/26/2011 06:42 PM, Paul Berry wrote:
> In order to implement the GLSL 1.30 isinf() function, it will be
> necessary to be able to represent infinity in the GLSL IR s-expression
> format.  This patch extends the s-expression parser so that it treats
> the string "#inf" as a floating point value representing positive
> infinity.
> ---
>  src/glsl/s_expression.cpp |   33 +++++++++++++++++++--------------
>  1 files changed, 19 insertions(+), 14 deletions(-)

Two kind of stupid comments:

Scheme represents infinity as +inf.0 or -inf.0 and NaN as +nan.0.  If
you wanted to be more Scheme-like, you could do that (and also gain a
representation of -infinity if you wanted...)

Also...strtod() already accepts "INF"/"INFINITY" and "NAN" for these
cases.  Which actually kind of sucks because somebody could totally name
their variables that (unlike #inf or +inf.0).

That should be fixed someday.  But there are a lot of things the reader
can't do (like structs), so...not terribly important.  It works well
enough for now and we can always fix it when it actually matters.

So, I guess you've got a few options:
1. Keep patch as is (#inf)
2. Switch to +inf.0, -inf.0, +nan.0 to be like Scheme
3. Drop this patch and just use INFINITY in the built-in files.

I'm honestly okay with any of these.  For the series:
Reviewed-by: Kenneth Graunke <kenneth at whitecape.org>

> diff --git a/src/glsl/s_expression.cpp b/src/glsl/s_expression.cpp
> index e704a3b..4391488 100644
> --- a/src/glsl/s_expression.cpp
> +++ b/src/glsl/s_expression.cpp
> @@ -64,21 +64,26 @@ read_atom(void *ctx, const char *&src, char *&symbol_buffer)
>     if (n == 0)
>        return NULL; // no atom
>  
> -   // Check if the atom is a number.
> -   char *float_end = NULL;
> -   double f = glsl_strtod(src, &float_end);
> -   if (float_end != src) {
> -      char *int_end = NULL;
> -      int i = strtol(src, &int_end, 10);
> -      // If strtod matched more characters, it must have a decimal part
> -      if (float_end > int_end)
> -	 expr = new(ctx) s_float(f);
> -      else
> -	 expr = new(ctx) s_int(i);
> +   // Check for the special symbol '#inf', which means +Infinity
> +   if (n == 4 && strncmp(src, "#inf", 4) == 0) {
> +      expr = new(ctx) s_float(INFINITY);
>     } else {
> -      // Not a number; return a symbol.
> -      symbol_buffer[n] = '\0';
> -      expr = new(ctx) s_symbol(symbol_buffer, n);
> +      // Check if the atom is a number.
> +      char *float_end = NULL;
> +      double f = glsl_strtod(src, &float_end);
> +      if (float_end != src) {
> +         char *int_end = NULL;
> +         int i = strtol(src, &int_end, 10);
> +         // If strtod matched more characters, it must have a decimal part
> +         if (float_end > int_end)
> +            expr = new(ctx) s_float(f);
> +         else
> +            expr = new(ctx) s_int(i);
> +      } else {
> +         // Not a number; return a symbol.
> +         symbol_buffer[n] = '\0';
> +         expr = new(ctx) s_symbol(symbol_buffer, n);
> +      }
>     }
>  
>     src += n;



More information about the mesa-dev mailing list