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

stillwater-sc / universal / 30545755105

30 Jul 2026 01:10PM UTC coverage: 85.197% (-0.02%) from 85.216%
30545755105

push

github

web-flow
fix: eliminate shift-mask narrowing via shared bit_high_mask helper (#1261) (#1267)

The left-shift operators build the upper-bits mask as an unsuffixed 64-bit
literal and implicitly narrow it to the limb type bt:

    bt mask = 0xFFFFFFFFFFFFFFFF << (bitsInBlock - bitsToShift);

so GCC/Clang -Wconversion and MSVC C4244 fire at every instantiation. The
literal's type is also platform-dependent (unsigned long on LP64 vs unsigned
long long on Windows), which is why the two report different messages.

Sibling of #1260 (same warning-clean Epic #1265), so extend the same shared
helper header. Add bit_high_mask<bt>(count, bitsInBlock) to
internal/bit_manipulation.hpp -- the highest `count` bits of a limb, all-ones
shifted left and narrowed explicitly (warning-free, platform-independent) -- and
route the sites through it.

The issue listed 2 files; the idiom was actually in 5 (blockbinary,
blocksignificand, blockfraction, areal, cfloat). The issue's proposed inline
`ALL_ONES` fix would not compile for areal/cfloat (no ALL_ONES limb constant --
cfloat only has ALL_ONES_FR/ALL_ONES_ES), so the shared helper is the uniform
fix and matches the Epic's intent. All 5 files already include
bit_manipulation.hpp from #1260, so no new includes.

Verified: bit_high_mask is -Wconversion/-Werror clean on gcc + clang and matches
the original literal for every shift of uint8/16/32/64; blockbinary<64,bt> <<= k
matches a uint64 shift for all bt and all k (the exercised code path). Regression
added to internal/blockbinary/api/bit_clear_mask.cpp. bb_api, cfloat_api/
assignment, areal_api, posit_api PASS on both compilers; all consumers compile
clean.

Resolves #1261

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

18 of 23 new or added lines in 6 files covered. (78.26%)

8 existing lines in 1 file now uncovered.

41634 of 48868 relevant lines covered (85.2%)

7274196.97 hits per line

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

60.71
/internal/blockbinary/api/bit_clear_mask.cpp
1
//  bit_clear_mask.cpp : test suite for the shared limb-mask helpers bit_clear_mask (#1260) and bit_high_mask (#1261)
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✔
29
                                ++nrOfFailures;
×
30
                                if (reportTestCases) std::cout << "    FAIL " << tag << " i=" << i
×
31
                                        << " observed=0x" << std::hex << static_cast<std::uint64_t>(observed)
×
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✔
37
                        ++nrOfFailures;
×
38
                        if (reportTestCases) std::cout << "    FAIL " << tag << " modulo reduction\n";
×
39
                }
40
                return nrOfFailures;
4✔
41
        }
42

43
        // bit_high_mask<bt>(count, bitsInBlock) must equal all-ones shifted left by
44
        // (bitsInBlock - count): the highest `count` bits of the limb set.
45
        template<typename bt>
46
        int VerifyBitHighMask(const char* tag, bool reportTestCases) {
4✔
47
                constexpr unsigned bitsInBlock = sizeof(bt) * 8u;
4✔
48
                int nrOfFailures = 0;
4✔
49
                for (unsigned count = 1; count < bitsInBlock; ++count) {   // callers early-return count(bitsToShift)==0
120✔
50
                        bt observed = bit_high_mask<bt>(count, bitsInBlock);
116✔
51
                        bt expected = static_cast<bt>(~(static_cast<std::uint64_t>(0)) << (bitsInBlock - count));
116✔
52
                        if (observed != expected) {
116✔
NEW
53
                                ++nrOfFailures;
×
NEW
54
                                if (reportTestCases) std::cout << "    FAIL " << tag << " count=" << count
×
NEW
55
                                        << " observed=0x" << std::hex << static_cast<std::uint64_t>(observed)
×
NEW
56
                                        << " expected=0x" << static_cast<std::uint64_t>(expected) << std::dec << '\n';
×
57
                        }
58
                }
59
                return nrOfFailures;
4✔
60
        }
61

62
}  // anonymous namespace
63

64
int main()
1✔
65
try {
66
        using namespace sw::universal;
67

68
        std::string test_suite  = "limb-mask helpers bit_clear_mask (#1260) / bit_high_mask (#1261)";
2✔
69
        std::string test_tag    = "bit_clear_mask";
1✔
70
        bool reportTestCases    = true;
1✔
71
        int nrOfFailedTestCases = 0;
1✔
72

73
        ReportTestSuiteHeader(test_suite, reportTestCases);
1✔
74

75
        // compile-time: the helper is constexpr and the truncation is exact
76
        static_assert(bit_clear_mask<std::uint8_t>(0, 8) == std::uint8_t(0xFE), "bit_clear_mask<uint8_t>(0) must be 0xFE");
77
        static_assert(bit_clear_mask<std::uint8_t>(7, 8) == std::uint8_t(0x7F), "bit_clear_mask<uint8_t>(7) must be 0x7F");
78
        static_assert(bit_clear_mask<std::uint16_t>(15, 16) == std::uint16_t(0x7FFF), "bit_clear_mask<uint16_t>(15)");
79
        // bit_high_mask (#1261): the highest `count` bits of the limb
80
        static_assert(bit_high_mask<std::uint8_t>(3, 8) == std::uint8_t(0xE0), "bit_high_mask<uint8_t>(3) must be 0xE0");
81
        static_assert(bit_high_mask<std::uint8_t>(1, 8) == std::uint8_t(0x80), "bit_high_mask<uint8_t>(1) must be 0x80");
82
        static_assert(bit_high_mask<std::uint16_t>(4, 16) == std::uint16_t(0xF000), "bit_high_mask<uint16_t>(4)");
83

84
        nrOfFailedTestCases += ReportTestResult(VerifyBitClearMask<std::uint8_t>("uint8_t", reportTestCases), "bit_clear_mask<uint8_t>", test_tag);
2✔
85
        nrOfFailedTestCases += ReportTestResult(VerifyBitClearMask<std::uint16_t>("uint16_t", reportTestCases), "bit_clear_mask<uint16_t>", test_tag);
2✔
86
        nrOfFailedTestCases += ReportTestResult(VerifyBitClearMask<std::uint32_t>("uint32_t", reportTestCases), "bit_clear_mask<uint32_t>", test_tag);
2✔
87
        nrOfFailedTestCases += ReportTestResult(VerifyBitClearMask<std::uint64_t>("uint64_t", reportTestCases), "bit_clear_mask<uint64_t>", test_tag);
2✔
88
        nrOfFailedTestCases += ReportTestResult(VerifyBitHighMask<std::uint8_t>("uint8_t", reportTestCases), "bit_high_mask<uint8_t>", test_tag);
3✔
89
        nrOfFailedTestCases += ReportTestResult(VerifyBitHighMask<std::uint16_t>("uint16_t", reportTestCases), "bit_high_mask<uint16_t>", test_tag);
2✔
90
        nrOfFailedTestCases += ReportTestResult(VerifyBitHighMask<std::uint32_t>("uint32_t", reportTestCases), "bit_high_mask<uint32_t>", test_tag);
2✔
91
        nrOfFailedTestCases += ReportTestResult(VerifyBitHighMask<std::uint64_t>("uint64_t", reportTestCases), "bit_high_mask<uint64_t>", test_tag);
1✔
92

93
        ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
1✔
94
        return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
1✔
95
}
1✔
96
catch (char const* msg) {
×
97
        std::cerr << msg << '\n';
×
98
        return EXIT_FAILURE;
×
99
}
×
100
catch (const std::runtime_error& err) {
×
101
        std::cerr << "Caught unexpected runtime exception: " << err.what() << '\n';
×
102
        return EXIT_FAILURE;
×
103
}
×
104
catch (...) {
×
105
        std::cerr << "Caught unknown exception" << '\n';
×
106
        return EXIT_FAILURE;
×
107
}
×
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