liveBook · Manning

Reproducible attribute

As explained in Modern C, Third Edition

The reproducible attribute is a C23 function attribute declaring that a call produces consistent results and effects for given arguments while permitting limited temporary state changes.

`[[reproducible]]` is a C23 function attribute. It promises consistent results and effects for given arguments while allowing limited temporary state changes. The promise is the programmer’s responsibility: the compiler may use it to optimize calls, but the attribute does not make a function reproducible. section 16.0 section 16.3

Pointers make “given arguments” more than a comparison of pointer values. Objects visible through pointer parameters or returns can matter to a call. Check those objects and any relevant execution state before relying on repeated calls to behave alike. section 10.2.3 section 16.3

#include <math.h>
#include <fenv.h>
inline double distance (double const x[restrict static 2]) 
            [[reproducible]] {
#   pragma FP_CONTRACT OFF
#   pragma FENV_ROUND FE_TONEAREST
    // We assert that sqrt will not be called with invalid arguments
    // and the result only depends on the argument value.
    extern double sqrt(double) [[unsequenced]];
    return sqrt(x[0]*x[0] + x[1]*x[1]);
}

The `distance` definition carries `[[reproducible]]`. Its floating-point pragmas and annotated `sqrt` declaration show why checking an apparent function of its arguments requires attention to the floating-point environment and called functions. section 16.3

double g (double y[static 1], double const x[static 2]) {
  // We assert that distance will not see different states of the floating
  // point environment.
  extern double distance (double const x[restrict static 2]) 
        [[unsequenced]];
  y[0] = distance(x);
  ...
  return distance(x);   // Replacement by y[0] is valid.
}

The caller makes an additional, local `[[unsequenced]]` assertion about `distance`, explicitly assuming it will not observe different floating-point states. Under that assertion, it can replace the second call with the saved result. section 16.3

TAKEAWAY: Treat `[[reproducible]]` as a checked optimization promise, not a request to trade correctness for speed. Its potential performance benefit still calls for measurement. section 16.0 section 16.5

How Modern C, Third Edition covers it

How deep it goes
Explains verification through function state, pointer-visible objects, and a conditional call replacement; stops short of reporting a measured speedup.
Examples
Uses an annotated distance calculation and a caller that conditionally replaces a repeated call with a saved result.
What it assumes
Assumes familiarity with C functions and pointers while developing the relevant purity and state checks.
What you can do after
Evaluate/understand when a reproducible declaration is justified and why a stronger call-site assertion needs verification.
How it connects
Connects reproducible functions to pure functions, `[[unsequenced]]`, pointer parameters, the floating-point environment, and compiler optimization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.