Skip to content

math/big: mulAddVWW too slow #22643

@TuomLarsen

Description

@TuomLarsen

What version of Go are you using?

go1.9.2 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using?

darwin/amd64

What did you do?

The following Go program multiplies big.Int with one Word constant:

package main

import (
	"fmt"
	"math/big"
)

func main() {
	a := big.NewInt(1)
	b := big.NewInt(1)
	a.Lsh(a, 8096)
	for i := uint64(1); i <= 10000000; i++ {
		a.Mul(a, b)
	}
	fmt.Println(a.BitLen())
}

What did you expect to see?

The following C program uses GMP 6.1.2:

#include <stdio.h>
#include <stdint.h>
#include "gmp.h"
 
int main(int argc, char *argv[]) {
    mpz_t a, b;
    mpz_init_set_ui(a, 1);
    mpz_init_set_ui(b, 1);
    mpz_mul_2exp(a, a, 8096);
    for (uint64_t i = 1; i <= 10000000ULL; i++) {
        mpz_mul(a, a, b);
    }
    printf("%lu\n", mpz_sizeinbase(a, 2));
    return 0;
}

and terminates under around 1s.

What did you see instead?

The Go program completes on my machine in 1.87s, i.e. almost twice as slow.

mulAddVWW is building block for multiplication (by one word, or more) so a better performance would benefit many applications.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performance

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions