Skip to content

JIT: (CQ) redundant bound check #134368

Closed

Description

@EgorBo

From a user's code:

    static int GetClosestCosineIndex(int value)
    {
        ReadOnlySpan<short> table = CosineTable;
        var index = 0;

        // Waterfall binary search finds the lower bound; this picks the closest neighbor.
        index |= table[index | 128] > value ? 128 : 0;
        index |= table[index | 64] > value ? 64 : 0;
        index |= table[index | 32] > value ? 32 : 0;
        index |= table[index | 16] > value ? 16 : 0;
        index |= table[index | 8] > value ? 8 : 0;
        index |= table[index | 4] > value ? 4 : 0;
        index |= table[index | 2] > value ? 2 : 0;
        index |= table[index | 1] > value ? 1 : 0;

        int val0 = table[index];
        int val1 = table[index + 1];

        // Pick the one with the smallest absolute difference.
        return val0 - value > value - val1 ? index + 1 : index;
    }
    static ReadOnlySpan<short> CosineTable =>
    [
        1024, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1022, 1022, 1022, 1021,
            1021, 1020, 1020, 1019, 1019, 1018, 1017, 1017, 1016, 1015, 1014, 1013,
            1012, 1011, 1010, 1009, 1008, 1007, 1006, 1005, 1004, 1003, 1001, 1000,
            999, 997, 996, 994, 993, 991, 990, 988, 986, 985, 983, 981, 979, 978,
            976, 974, 972, 970, 968, 966, 964, 962, 959, 957, 955, 953, 950, 948,
            946, 943, 941, 938, 936, 933, 930, 928, 925, 922, 920, 917, 914, 911,
            908, 906, 903, 900, 897, 894, 890, 887, 884, 881, 878, 875, 871, 868,
            865, 861, 858, 854, 851, 847, 844, 840, 837, 833, 829, 826, 822, 818,
            814, 811, 807, 803, 799, 795, 791, 787, 783, 779, 775, 771, 767, 762,
            758, 754, 750, 745, 741, 737, 732, 728, 724, 719, 715, 710, 706, 701,
            696, 692, 687, 683, 678, 673, 668, 664, 659, 654, 649, 644, 639, 634,
            629, 625, 620, 615, 609, 604, 599, 594, 589, 584, 579, 574, 568, 563,
            558, 553, 547, 542, 537, 531, 526, 521, 515, 510, 504, 499, 493, 488,
            482, 477, 471, 466, 460, 454, 449, 443, 437, 432, 426, 420, 414, 409,
            403, 397, 391, 386, 380, 374, 368, 362, 356, 350, 344, 339, 333, 327,
            321, 315, 309, 303, 297, 291, 285, 279, 273, 267, 260, 254, 248, 242,
            236, 230, 224, 218, 212, 205, 199, 193, 187, 181, 175, 168, 162, 156,
            150, 144, 137, 131, 125, 119, 112, 106, 100, 94, 87, 81, 75, 69, 62,
            56, 50, 43, 37, 31, 25, 18, 12, 6, 0
    ];

GetClosestCosineIndex currently contains 2 redundant bound checks.

Activity

  1. added
    untriaged
    on Sep 21, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 21, 2026
  3. added this to the Future milestone on Sep 21, 2026
  4. removed
    untriaged
    on Sep 21, 2026
  5. dotnet-policy-service commented on Sep 21, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  6. added a commit that references this issue on Sep 24, 2026
    117f900
  7. locked and limited conversation to collaborators on Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions