Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

How we expanded GCC value range propagation to floats

January 18, 2023
Aldy Hernandez
Related topics:
CompilersC, C#, C++
Related products:
Red Hat Enterprise Linux

Share:

    Value range propagation (VRP) is an optimization tool used in compilers. The article Value range propagation in GCC with Project Ranger describes how this optimization works and how the GCC team implements it for C and C++ programs. This article will explain how we expand VRP beyond integers and pointers to other data types, particularly floating-point numbers.

    Expanding data types in range tracking

    A generic, type-agnostic way to keep track of ranges was part of our original goals for the Ranger project. At first, VRP tracked just integers and pointers, and we also wanted to apply it to floats, strings, and other data types.

    We spent a good chunk of the GCC 13 release ridding Ranger of any dependencies on integers baked in the original VRP implementation. We made Ranger work with a generic vrange class instead of the irange that was specific to integers and pointers. Then we developed an infrastructure to declare typeless temporaries that could be used in intermediate computations within Ranger. When all was said and done, we had a core that could provide VRP, jump threading, and other range-aware optimizations on anything we could express in terms of vrange.

    For the curious, vrange is nothing more than an abstract class that describes operations on properties (ranges, in our case). It is basically a class to make it easy for Ranger to do operations on sets (union, intersect, etc.):

    class vrange
    
    {
    
    public:
    
      virtual void set (tree, tree, value_range_kind = VR_RANGE);
    
      virtual tree type () const;
    
      virtual bool supports_type_p (const_tree type) const;
    
      virtual void set_varying (tree type);
    
      virtual void set_undefined ();
    
      virtual bool union_ (const vrange &);
    
      virtual bool intersect (const vrange &);
    
      virtual bool singleton_p (tree *result = NULL) const;
    
      virtual bool contains_p (tree cst) const;
    
      virtual bool zero_p () const;
    
      virtual bool nonzero_p () const;
    
      virtual void set_nonzero (tree type);
    
      virtual void set_zero (tree type);
    
      virtual void set_nonnegative (tree type);
    
      virtual bool fits_p (const vrange &r) const;
    
    …
    
    …
    
    };

    Current support for floating-point numbers

    Once the vrange class was in place, our next step was to provide a barebones implementation of frange (floating point range) that would give us enough tools to fold conditionals and perhaps keep track of "not a number" values (NaNs). We accomplished this task in the simplest way we could find, through a class that keeps track of endpoints and whether a positive or negative NaN is possible:

    class frange : public vrange
    
    {
    
    …
    
    …
    
    private:
    
      tree m_type;
    
      REAL_VALUE_TYPE m_min;
    
      REAL_VALUE_TYPE m_max;
    
      bool m_pos_nan;
    
      bool m_neg_nan;
    
    };

    After much more work than anticipated (because floats are painfully hard, and things never go according to plan), here are a handful of things we now support in GCC 13:

    • Folding of symbolic relational operators:

      if (x > y) {
      
          if (x == y)
      
      link_error ();
      
      }
    • Propagation of NaNs and infinities:

      if (x > y) {
      
          // x is not a NAN
      
          // y is not a NAN
      
          // x is not -INF
      
          // y is not +INF
      
      }
    • Intervals for ranges:

      if (x >= 5.0) {
      
          // x is not a NAN
      
          // x is [5.0, +INF]
      
      } else {
      
          // x is [-INF, 4.99999952316] U [NAN]
      
      }
    • Signed zeros:

      if (x == 0.0) {
      
          if (__builtin_sign (x))
      
          y = x;    // y = -0.0
      
          else
      
          z = x;    // z = +0.0
      
      }
    • A handful of operations, including +, -, *, /, negate, abs, relational operators, unordered relational operators, etc.

    Even with this initial implementation, we have been able to close quite a few long-standing PRs (bug reports and enhancement requests) related to floats, including PR24021 (VRP does not work with floating points), which has been with us for 17 years!

    Using optimized processor instructions 

    The goal of this work is to provide an infrastructure to increasingly flesh out floating point operations that can help us do better range propagation, as well as aid other optimizations that generate better code. For example, some architectures provide a cheap sqrt instruction that works on positive numbers but not on NaNs. With floating point ranges, the instruction selection pass might determine that the argument to sqrt is neither a negative value nor a NaN and can replace an expensive sqrt instruction with a cheaper one.

    We also hope to make use of this work with glibc in the next release to provide entry points into libm when the operands like sin() or cos() functions are known to be in a certain range and void of NaNs and INFs. This improvement will allow us to generate faster code because the compiler can choose cheaper versions of said functions.

    Last updated: August 14, 2023

    Related Posts

    • Value range propagation in GCC with Project Ranger

    • An upside-down approach to GCC optimizations

    • New C++ features in GCC 10

    • Mostly harmless: An account of pseudo-normal floating point numbers

    Recent Posts

    • Staying ahead of artificial intelligence threats

    • Strengthen privacy and security with encrypted DNS in RHEL

    • How to enable Ansible Lightspeed intelligent assistant

    • Why some agentic AI developers are moving code from Python to Rust

    • Confidential VMs: The core of confidential containers

    What’s up next?

    kafka pizza

    Learn how to use Kafka in your app without installing it! Follow this step-by-step experience for using Red Hat OpenShift Streams for Apache Kafka from code running in a different cluster on the Developer Sandbox for Red Hat OpenShift. You will run your code for this fun pizza stock application in the cloud as you discover the attractions of distributed computing.

    Learn by doing
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue