Muqsit / Muqsit/arithmexp

Optimizer does not account for INF and NAN

Open
#16 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
26
Forks
2
PR merge metrics
No merged PRs in 30d

Description

Description

Operator Strength Reduction optimizer directly substitutes a sub-expression x - x with 0, not accounting for INF or NAN values being supplied to x (inf - inf and nan - nan must produce nan). Other affected cases include 0 * inf returning 0 instead of NAN, and sqrt(-1) / sqrt(-1) getting substituted for 1 despite sqrt(-1) === NAN (NAN / NAN === NAN).

As variable resolution occurs during evaluation, x - x cannot be resolved during parsing although 1 - 1 and inf - inf can. One way to address this would be to resolve operations between numeric literal operands during parsing and implement a mechanism to toggle optimizations that disregard INF and NAN (like GCC's FloatingPointMath flags).

Workarounds
  1. Filter out operator strength reduction optimization from ExpressionOptimizerRegistry
    $parser = Parser::createDefault();
    
    $optimizations = new ExpressionOptimizerRegistry();
    foreach($parser->getExpressionOptimizerRegistry()->getRegistered() as $identifier => $value){
    	if(!($value instanceof OperatorStrengthReductionExpressionOptimizer)){
    		$optimizations->register($identifier, $value);
    	}
    }
    
    $parser = new Parser(
    	$parser->getBinaryOperatorRegistry(),
    	$parser->getUnaryOperatorRegistry(),
    	$parser->getConstantRegistry(),
    	$parser->getFunctionRegistry(),
    	$optimizations,
    	$parser->getScanner()
    );
    
  2. Disable optimizations altogether — $parser = Parser::createUnoptimized();

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with OperatorStrengthReductionExpressionOptimizer and its registration in ExpressionOptimizerRegistry, then reproduce the listed INF and NAN cases using Parser::createDefault() and Parser::createUnoptimized(). Define regression coverage for IEEE floating-point results while preserving valid optimizations; the issue leaves the optimization policy and parsing mechanism open.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
compilers, performance
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.