Wanted: constant folding in arithmetic functions

Bug #676414 reported by Heka Treep
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
Fix Released
Wishlist
Unassigned

Bug Description

Currently, Python does not reduce the constants in expressions such as:

  (+ 3 a 5 b 7 c)

For fixnums this gives:

  ; A7: 83C203 ADD EDX, 3
  ; AA: 83C205 ADD EDX, 5
  ; AD: C1FF02 SAR EDI, 2
  ; B0: 01FA ADD EDX, EDI
  ; B2: 83C207 ADD EDX, 7
  ; B5: C1FE02 SAR ESI, 2
  ; B8: 8D0432 LEA EAX, [EDX+ESI]
  ; BB: 6BD004 IMUL EDX, EAX, 4
  ; BE: 7107 JNO L0
  ; C0: 8BD0 MOV EDX, EAX

and for generic numbers this generate five calls to GENERIC-+.

It would be nice to collect all numbers at compile-time, doing the following transformation:

  (+ 3 a 5 b 7 c) -> (+ 15 a b c)

schematically:

  (+ list) -> `(+ ,(fold-left '+ identity (filter const-p list))
                  ,@(filter not-const-p list))

Heka Treep (zena-treep)
description: updated
Revision history for this message
Heka Treep (zena-treep) wrote :

I wrote a simple version. In the attached patch constants are collected by calling the `reduce-constants' function from the `source-transform-transitive' and `source-transform-intransitive' functions (which is used by `define-source-transform' forms for transitive and intransitive arithmetic functions) form compiler/srctran.lisp.

It works fine for building process and tests, but when I see this comment:

  ;;; Only safely applicable for exact numbers. For floating-point
  ;;; x, one would have to first show that neither x or y are signed
  ;;; 0s, and that x isn't an SNaN.

for (+ X 0) folder - it is safely to collect all type of numbers in compile time?

Changed in sbcl:
importance: Undecided → Wishlist
status: New → Triaged
tags: added: review
Changed in sbcl:
assignee: nobody → Nikodemus Siivola (nikodemus)
tags: removed: review
Changed in sbcl:
status: Triaged → In Progress
Revision history for this message
Nikodemus Siivola (nikodemus) wrote :

In 1.0.45.25.

Changed in sbcl:
assignee: Nikodemus Siivola (nikodemus) → nobody
status: In Progress → Fix Committed
Changed in sbcl:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.