Skip to content

Sufficiently big BigUint multiplications fail during CTFE #11047

Description

@just-harry

When both operands of a std.internal.math.biguintcore.BigUint multiplication exceed a certain threshold (the threshold appears to be 1 << 32) during CTFE, the multiplication will fail as std.internal.math.biguintcore.mulInternal attempts to use core.cpuid.dataCaches to optimise algorithm selection.

This happens only if the operands differ, as squaring follows a different code-path.

Test case:

import std.internal.math.biguintcore : BigUint;
enum largeMultiplication = BigUint.mul(BigUint(ulong(1)) << ulong(33), BigUint(ulong(1)) << ulong(32));

resulting in:

import\core\cpuid.d(124): Error: static variable `_dataCaches` cannot be read at compile time
    const(CacheInfo)[5] dataCaches() { return _dataCaches; }
                                              ^
import\std\internal\math\biguintcore.d(239):        called from here: `dataCaches()`
    return dataCaches[0].size * 1024 / 2;
           ^
import\std\internal\math\biguintcore.d(1715):        called from here: `getCacheLimit()`
        immutable CACHELIMIT = getCacheLimit;
                               ^
import\std\internal\math\biguintcore.d(915):        called from here: `mulInternal(result, cast(const(uint)[])x.data, cast(const(uint)[])y.data)`
            else mulInternal(result, x.data, y.data);
                            ^
example.d(2):        called from here: `mul(BigUint([0u]).this(1LU).opBinary(33LU), BigUint([0u]).this(1LU).opBinary(32LU))`
enum largeMultiplication = BigUint.mul(BigUint(ulong(1)) << ulong(33), BigUint(ulong(1)) << ulong(32));

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions