• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stillwater-sc / universal / 30536515033

30 Jul 2026 10:54AM UTC coverage: 85.216% (-0.03%) from 85.244%
30536515033

push

github

web-flow
fix: eliminate implicit limb-mask narrowing via shared bit_clear_mask helper (#1260) (#1266)

The idiom `bt null = ~(1ull << (i % bitsInBlock))` builds a 64-bit mask and
implicitly narrows it to the limb type bt in every setbit. The truncation is
intended (only the low bitsInBlock bits matter) but implicit, so GCC/Clang
-Wconversion and MSVC C4244 fire at every template instantiation site (observed
flooding stillwater-sc/mp-blas CI).

Add a dependency-free constexpr helper in internal/bit_manipulation.hpp:

    template<typename bt>
    constexpr bt bit_clear_mask(unsigned i, unsigned bitsInBlock) noexcept {
        return static_cast<bt>(~(bt(1) << (i % bitsInBlock)));
    }

making the truncation explicit (warning-free) in ONE place, and route all sites
through it. The issue listed 5 files; the identical idiom was actually in 10
(blockbinary, blockfraction, blocksignificand, cfloat, lns, posit_exponent,
integer, takum, areal, dbns) -- all converted so the idiom cannot drift into the
next number system. posit_exponent's std::uint32_t variant uses
bit_clear_mask<std::uint32_t>(i, 32).

Verified: the helper is -Wconversion/-Werror clean on gcc + clang and produces
the identical mask for every bit position of uint8/16/32/64 (new regression
internal/blockbinary/api/bit_clear_mask.cpp, with constexpr static_asserts). All
10 consumers compile + run clean on both compilers; bb_api, cfloat_api/assignment,
posit_api/conversion, lns_api, areal_api PASS (setbit behavior unchanged).

Resolves #1260

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

34 of 52 new or added lines in 12 files covered. (65.38%)

10 existing lines in 1 file now uncovered.

41628 of 48850 relevant lines covered (85.22%)

7263004.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.0
/internal/blockbinary/api/bit_clear_mask.cpp
1
//  bit_clear_mask.cpp : test suite for the shared bit_clear_mask limb-mask helper (#1260)
2
//
3
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
4
// SPDX-License-Identifier: MIT
5
//
6
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
7
#include <universal/utility/directives.hpp>
8
#include <cstdint>
9
#include <iostream>
10

11
#include <universal/internal/bit_manipulation.hpp>
12
#include <universal/verification/test_suite.hpp>
13

14
namespace {
15

16
        using namespace sw::universal;
17

18
        // bit_clear_mask<bt>(i, bitsInBlock) must equal a bt with every bit set except
19
        // bit (i % bitsInBlock) -- the mask used to clear one bit via (limb & mask).
20
        template<typename bt>
21
        int VerifyBitClearMask(const char* tag, bool reportTestCases) {
4✔
22
                constexpr unsigned bitsInBlock = sizeof(bt) * 8u;
4✔
23
                int nrOfFailures = 0;
4✔
24
                for (unsigned i = 0; i < bitsInBlock; ++i) {
124✔
25
                        bt observed = bit_clear_mask<bt>(i, bitsInBlock);
120✔
26
                        // reference built in 64-bit then explicitly narrowed (the intended value)
27
                        bt expected = static_cast<bt>(~(static_cast<std::uint64_t>(1) << i));
120✔
28
                        if (observed != expected) {
120✔
NEW
29
                                ++nrOfFailures;
×
NEW
30
                                if (reportTestCases) std::cout << "    FAIL " << tag << " i=" << i
×
NEW
31
                                        << " observed=0x" << std::hex << static_cast<std::uint64_t>(observed)
×
NEW
32
                                        << " expected=0x" << static_cast<std::uint64_t>(expected) << std::dec << '\n';
×
33
                        }
34
                }
35
                // i beyond bitsInBlock must wrap via the % reduction (matches the call sites)
36
                if (bit_clear_mask<bt>(bitsInBlock + 3u, bitsInBlock) != bit_clear_mask<bt>(3u, bitsInBlock)) {
4✔
NEW
37
                        ++nrOfFailures;
×
NEW
38
                        if (reportTestCases) std::cout << "    FAIL " << tag << " modulo reduction\n";
×
39
                }
40
                return nrOfFailures;
4✔
41
        }
42

43
}  // anonymous namespace
44

45
int main()
1✔
46
try {
47
        using namespace sw::universal;
48

49
        std::string test_suite  = "bit_clear_mask limb-mask helper (#1260)";
2✔
50
        std::string test_tag    = "bit_clear_mask";
1✔
51
        bool reportTestCases    = true;
1✔
52
        int nrOfFailedTestCases = 0;
1✔
53

54
        ReportTestSuiteHeader(test_suite, reportTestCases);
1✔
55

56
        // compile-time: the helper is constexpr and the truncation is exact
57
        static_assert(bit_clear_mask<std::uint8_t>(0, 8) == std::uint8_t(0xFE), "bit_clear_mask<uint8_t>(0) must be 0xFE");
58
        static_assert(bit_clear_mask<std::uint8_t>(7, 8) == std::uint8_t(0x7F), "bit_clear_mask<uint8_t>(7) must be 0x7F");
59
        static_assert(bit_clear_mask<std::uint16_t>(15, 16) == std::uint16_t(0x7FFF), "bit_clear_mask<uint16_t>(15)");
60

61
        nrOfFailedTestCases += ReportTestResult(VerifyBitClearMask<std::uint8_t>("uint8_t", reportTestCases), "bit_clear_mask<uint8_t>", test_tag);
2✔
62
        nrOfFailedTestCases += ReportTestResult(VerifyBitClearMask<std::uint16_t>("uint16_t", reportTestCases), "bit_clear_mask<uint16_t>", test_tag);
2✔
63
        nrOfFailedTestCases += ReportTestResult(VerifyBitClearMask<std::uint32_t>("uint32_t", reportTestCases), "bit_clear_mask<uint32_t>", test_tag);
2✔
64
        nrOfFailedTestCases += ReportTestResult(VerifyBitClearMask<std::uint64_t>("uint64_t", reportTestCases), "bit_clear_mask<uint64_t>", test_tag);
1✔
65

66
        ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
1✔
67
        return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
1✔
68
}
1✔
NEW
69
catch (char const* msg) {
×
NEW
70
        std::cerr << msg << '\n';
×
NEW
71
        return EXIT_FAILURE;
×
NEW
72
}
×
NEW
73
catch (const std::runtime_error& err) {
×
NEW
74
        std::cerr << "Caught unexpected runtime exception: " << err.what() << '\n';
×
NEW
75
        return EXIT_FAILURE;
×
NEW
76
}
×
NEW
77
catch (...) {
×
NEW
78
        std::cerr << "Caught unknown exception" << '\n';
×
NEW
79
        return EXIT_FAILURE;
×
NEW
80
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc