Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

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

    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

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    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

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.