Skip to content

Improve codegen for carry/borrow #10

@mratsim

Description

@mratsim

The current code add/sub/mul can be tuned to help GCC/Clang significantly improve the assembly generated.

Next commit will bring the ADD code from this:

2018-03-30_01-28-37

to this

2018-03-30_01-27-05

This is unrolled, add is just add + adc now instead of leaq, addq, cmpq, adcq

Notice how the leaq (corresponding to the "tmp" and the cmpq were removed.

Multiplications deserve the same.

Test case:

import ../src/mpint, times

let a = 42.initMpUint(128)

let b = 1000.initMpUint(128)

var c = 0.initMpUint(128)

proc carry(foo: var MpUint[128], a: MpUint[128]) =
  for _ in 0 ..< 1_000_000_000:
    foo += a


proc borrow(foo: var MpUint[128], b: MpUint[128]) =
  for _ in 0 ..< 1_000_000_000:
    foo -= a


var start = cpuTime()
carry(c, a)
var stop = cpuTime()

echo "Carry: " & $(stop - start) & "s"

start = cpuTime()
borrow(c, b)
stop = cpuTime()

echo "Borrow: " & $(stop - start) & "s"

echo c
echo (c - 1.initMpUint(128))

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions