-
Notifications
You must be signed in to change notification settings - Fork 1.6k
constexpr all the generate_canonical parameters #2498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+140
−8
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
107b24a
constexpr all the generate_canonical paramters
MattStephanson bc48cce
fix test
MattStephanson 4523960
clang-format
MattStephanson fb630e3
code review feedback
MattStephanson 0c5ae59
Merge remote-tracking branch 'upstream/main' into canon-alt
MattStephanson 0386512
remove non-constexpr call, expand testing to 0 bits
MattStephanson 2aed18f
Code review feedback.
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
tests/std/tests/GH_001964_constexpr_generate_canonical/env.lst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
StephanTLavavej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RUNALL_INCLUDE ..\usual_matrix.lst |
64 changes: 64 additions & 0 deletions
64
tests/std/tests/GH_001964_constexpr_generate_canonical/test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include <cassert> | ||
StephanTLavavej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <cmath> | ||
#include <cstdint> | ||
#include <random> | ||
#include <utility> | ||
|
||
using namespace std; | ||
|
||
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) | ||
|
||
int naive_iterations(const int bits, const uint64_t gmin, const uint64_t gmax) { | ||
// Naive implementation of [rand.util.canonical]. Note that for large values of range, it's possible that | ||
// log2(range) == bits when range < 2^bits. This can lead to incorrect results, so we can't use this function as | ||
// a reference for all values. | ||
|
||
const double range = static_cast<double>(gmax) - static_cast<double>(gmin) + 1.0; | ||
return max(1, static_cast<int>(ceil(static_cast<double>(bits) / log2(range)))); | ||
} | ||
|
||
void test(const int target_bits) { | ||
// Increase the range until the number of iterations repeats. | ||
uint64_t range = 2; | ||
int k = 0; | ||
int prev_k = -1; | ||
while (k != prev_k) { | ||
prev_k = exchange(k, naive_iterations(target_bits, 1, range)); | ||
const int k1 = _Generate_canonical_iterations(target_bits, 1, range); | ||
MattStephanson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert(k == k1); | ||
++range; | ||
} | ||
|
||
// Now only check the crossover points, where incrementing the range actually causes the number of iterations to | ||
// increase. | ||
while (--k != 0) { | ||
// The largest range such that k iterations generating [1,range] produces less than target_bits bits. | ||
if (k == 1) { | ||
range = ~uint64_t{0} >> (64 - target_bits); | ||
} else { | ||
range = static_cast<uint64_t>(ceil(pow(2.0, static_cast<double>(target_bits) / k))) - 1; | ||
} | ||
|
||
int k0 = (k == 1) ? 2 : naive_iterations(target_bits, 1, range); | ||
int k1 = _Generate_canonical_iterations(target_bits, 1, range); | ||
assert(k0 == k1); | ||
assert(k1 == k + 1); | ||
|
||
k0 = (k == 1) ? 1 : naive_iterations(target_bits, 0, range); | ||
k1 = _Generate_canonical_iterations(target_bits, 0, range); | ||
assert(k0 == k1); | ||
assert(k1 == k); | ||
} | ||
} | ||
|
||
int main() { | ||
STATIC_ASSERT(_Generate_canonical_iterations(53, 1, uint64_t{1} << 32) == 2); | ||
STATIC_ASSERT(_Generate_canonical_iterations(64, 0, ~uint64_t{0}) == 1); | ||
|
||
for (int bits = 0; bits <= 64; ++bits) { | ||
test(bits); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.