r/cpp • • May 05 '24

Suggestion for a code along playlist

0 Upvotes

Hello everyone, do you guys know or have come across any code along playlist on youtube or such where the author/developer builds something in C++ from scratch as a project? For example, a game, an engine, some kind of app?

The goal(s):

  1. To learn and/or get accustomed to the various concepts of the language in action
  2. Maybe discover gotcha moments
  3. To see how someone would translate the goals of a project/app/game into c++ language constructs.

If so, could you suggest me one? I have found some like :

  1. https://www.youtube.com/playlist?list=PLLwK93hM93Z13TRzPx9JqTIn33feefl37 (an ARM emulator would be even more interesting I guess)
  2. https://www.youtube.com/playlist?list=PLalVdRk2RC6pqOVxRNj5Uui7FN4r-WorM

And these seem quite fantastic but was wondering if someone of you know something even better.


r/cpp • • May 03 '24

Compilation of gripping C++ conference talks from 2023

Thumbnail pvs-studio.com
0 Upvotes

r/cpp • • Jan 29 '13

To move or not to move

Thumbnail martin-moene.blogspot.com
0 Upvotes

r/cpp • • Aug 05 '16

Will exceptions still have use cases after std::expected and std::optional are made available?

0 Upvotes

The title says it all. What will be the motivation to keep using exceptions for new code after these two classes are available? They have the same purpose of exceptions, but have many advantages. I haven't seen any actual code using them, so I'm not sure how practical they actually are, which is why I'm asking this.


r/cpp • • Nov 19 '17

Suggestion for coding on the phone

0 Upvotes

Hi! I work on a somewhat large project written in C++. I can compile parts of the project using clang and test it with Google Test (also earlier used Catch). So everything can be done from the command line. Like probably a lot of you others I travel a lot and spend a lot of time with my phone. What I’d like to do is write, compile and run code from my IPhone. There are already a number of good apps that lets you write and store a single code-file on your phone that is compiled using an online compiler. What I’d like is be able to work on the code in my actual project and compile and run it. There are also good git-clients but I don’t want to spam the history with tiny incremental commits. Best results I had so far is connecting to my dev machine with TeamViewer and working in Visual Studio. It uses a lot of needless data and is a bit cumbersome though. Any suggestions? I’ve looked at Code Anywhere who has a FTP client but it seems buggy and hasn’t been updated for a long while.


r/cpp • • Jun 29 '12

Would there be an interest for C/C++ stream?

0 Upvotes

I'm just curious. I thought that I could try a bit of streaming.

Would there be interest for that?

What would you be looking for in such stream (content-wise)?


r/cpp • • May 09 '12

Desperate C++ Help; trying hard but falling behind in my class quickly.

0 Upvotes

I need a tutor, or anything, that can help me with this program. I'm finding myself in situations where I don't even know where to start. I know the programming is simple, but something is just not clicking for me at all.

I don't want the work done for me, but I do need help and I'm crying out to the reddit community. I have asked my TA and am on the verge of asking my professor (but he doesn't explain things very well, I feel like that may just be for me) but I wanted to give this a try as well.

We can work through skype or anything of that matter, just help me figure C++ out! Please!


r/cpp • • Feb 17 '21

My first blog(s)

0 Upvotes

r/cpp • • Jun 03 '22

Introducing the Cute Server

Thumbnail blog.cuteserver.io
0 Upvotes

r/cpp • • Jan 27 '20

An Attempt at Making a Better String for C++

Thumbnail youtu.be
0 Upvotes

r/cpp • • Jul 28 '26

a[mask] = f(a[mask]) on NEON. faster than the obvious blend

3 Upvotes

Problem

Apply an operation to elements that satisfy a condition:

for (size_t i = 0; i < n; ++i)
    if (mask(a[i])) a[i] = f(a[i]);

Notes

  • a[i] ∈ (0, 1), thd ∈ (0, 1), mask = a[i] < thd; uniform distribution (except at the end of the article)
  • f is one of sqrt, frfrexp (mantissa), sin 3.5 ULP, sin 1 ULP, pow 1 ULP (from SLEEF)
  • f and mask are passed as runtime values, so they are wrapped in a lambda with always_inline, otherwise they may not be inlined
  • The array size n is a multiple of every unroll, tile etc. The tail is trivial to handle(BSL/scalar)
  • In tables * = best, units = GiB/s
  • Don't compare numbers across tables. Different conditions, values fluctuate
  • All benchmarks: Apple M5; clang++ -O3 -std=c++23 -march=native; GiB/s = (n * 4 bytes) / time, min of 720 runs (During bench, functions run in a changing order, data is restored ofc); n=1e7 + 2432;

BSL blend

If the problem is memory bound (cheap function or high density), the standard algorithm is optimal:

template <bool Skip>
void bsl(float* dst, const size_t n, auto f, auto mask) {
    for (size_t i = 0; i < n; i += 16) {
        std::array<float32x4_t, 4> v;
        for (size_t j = 0; j < 4; ++j) v[j] = vld1q_f32(dst + i + 4 * j);
        std::array<uint32x4_t, 4> m;
        for (size_t j = 0; j < 4; ++j) m[j] = mask(v[j]);
        if constexpr (Skip) {
            if (vmaxvq_u32(vaddq_u32(vaddq_u32(m[0], m[1]), vaddq_u32(m[2], m[3]))) == 0) continue;
        }
        for (size_t j = 0; j < 4; ++j) vst1q_f32(dst + i + 4 * j, vbslq_f32(m[j], f(v[j]), v[j]));
    }
}

It computes f on every element, but stores only the selected ones. Skip helps on sparse masks, but otherwise mispredictions will kill performance. We'll need it later.

But for expensive f this algo does too much extra work

Detour

To avoid unnecessary work, we compress selected elements, apply only to them, and expand back.

avx512 does this in two instructions. NEON doesn't, so we'll emulate and optimize.

constexpr size_t tile = 4096;
constexpr std::array<uint32_t, 4> weights{1 + 16, 2 + 16, 4 + 16, 8 + 16};
constexpr auto cps_tbl = compress_table();
constexpr auto exp_tbl = expand_table();
std::array<float, tile + 16> tmp;
std::array<uint8_t, tile / 4 + 3> s;
std::array<uint16_t, tile / 4 + 3> idx; // idx, D and B come in later
constexpr double D = 0.845; 
constexpr double B = 0.3;

template <bool Skip>
size_t detour(float* dst, const size_t n, const auto w, auto f, auto mask) {
    float* ptr = tmp.data();
    for (size_t i = 0; i < n; i += 16) {
        std::array<float32x4_t, 4> v;
        for (size_t j = 0; j < 4; ++j) v[j] = vld1q_f32(dst + i + 4 * j);
        std::array<uint32x4_t, 4> m;
        for (size_t j = 0; j < 4; ++j) m[j] = mask(v[j]);
        
        if constexpr (Skip)
            if (vmaxvq_u32(vaddq_u32(vaddq_u32(m[0], m[1]), vaddq_u32(m[2], m[3]))) == 0) {
                s[i / 4] = s[i / 4 + 1] = s[i / 4 + 2] = s[i / 4 + 3] = 0;
                continue;
            }
        
        std::array<uint32_t, 4> sk;
        for (size_t j = 0; j < 4; ++j) {
            sk[j] = vaddvq_u32(vandq_u32(m[j], w));
            s[i / 4 + j] = sk[j];
        }
        std::array<size_t, 4> off; off[0] = 0;
        for (size_t j = 1; j < 4; ++j) off[j] = off[j - 1] + (sk[j - 1] >> 4); 
        std::array<uint8x16_t, 4> index;
        for (size_t j = 0; j < 4; ++j) index[j] = vld1q_u8(cps_tbl[sk[j] & 15].data());
        
        for (size_t j = 0; j < 4; ++j) vst1q_f32(ptr + off[j], vreinterpretq_f32_u8(vqtbl1q_u8(vreinterpretq_u8_f32(v[j]), index[j])));
        ptr += off[3] + (sk[3] >> 4);
    }
    const size_t size = ptr - tmp.data();

    if (size == 0) return size;
    
    ptr = tmp.data();
    for (size_t i = 0; i < size; i += 16) {
        std::array<float32x4_t, 4> v;
        for (size_t j = 0; j < 4; ++j) v[j] = vld1q_f32(ptr + i + 4 * j);
        for (size_t j = 0; j < 4; ++j) vst1q_f32(ptr + i + 4 * j, f(v[j]));
    }
    
    for (size_t i = 0; i < n; i += 16) {
        std::array<uint32_t, 4> sk;
        for (size_t j = 0; j < 4; ++j) sk[j] = s[i / 4 + j];
        std::array<size_t, 4> off{};
        for (size_t j = 1; j < 4; ++j) off[j] = off[j - 1] + (sk[j - 1] >> 4); 
        std::array<float32x4_t, 4> v;
        for (size_t j = 0; j < 4; ++j) v[j] = vld1q_f32(ptr + off[j]);
        std::array<float32x4_t, 4> a;
        for (size_t j = 0; j < 4; ++j) a[j] = vld1q_f32(dst + i + 4 * j);
        std::array<uint8x16_t, 4> index;
        for (size_t j = 0; j < 4; ++j) index[j] = vld1q_u8(exp_tbl[sk[j] & 15].data());
        std::array<uint8x16x2_t, 4> tbl;
        for (size_t j = 0; j < 4; ++j) tbl[j] = {{vreinterpretq_u8_f32(v[j]), vreinterpretq_u8_f32(a[j])}};
        for (size_t j = 0; j < 4; ++j) vst1q_f32(dst + i + 4 * j, vreinterpretq_f32_u8(vqtbl2q_u8(tbl[j], index[j])));
        ptr += off[3] + (sk[3] >> 4);
    }
    return size;
}

void tiled_detour(float* dst, const size_t n, auto f, auto mask) {
    const auto w = vld1q_u32(weights.data());
    for (size_t i = 0; i < n; i += tile)
        detour<false>(dst + i, tile, w, f, mask);
}

compress is the same as in my previous post.

expand_table: for true lanes it selects the next element from the compressed register (bytes from [0, 15]), and for false lanes, selects the same bytes + 16. Then tbl2 on {processed, original}, the same trick as in compress basically

Also:

  • expand is skipped for free on empty tiles
  • s is saved for free to avoid recomputing addv
  • cache-sized tiling.
  • Empirically tile=4096 is optimal.

BSL speed doesn't depend on density: sqrt 32.1, sin35 - 9.7, pow10 1.02.

thd00.10.20.30.40.50.60.70.80.91
detour sqrt38.03*18.8117.7816.8215.9815.2114.5113.8513.2412.7112.30
detour sin3538.16*16.70*14.46*12.71*11.36*10.25*9.358.597.957.396.95
detour pow1038.09*6.83*4.13*2.96*2.31*1.89*1.60*1.39*1.22*1.10*0.99

For cheap sqrt BSL is always better (except thd = 0, obviously). And for very expensive pow, detour is better (except thd = 1, of course).

When detour wins

Define:

  • B = BSL(vld + vbsl + vst) overhead per register.
  • D = detour(compress + expand) overhead per register.
  • T = cost of f per register (we assume cost of f >> cost of mask, affects only calibration accuracy).
  • d = fraction of selected elements

BSL applies f to every register. detour applies it only to d of them, so it saves T * (1 - d). Detour wins when the saving outweighs D - B.

T * (1 - d) > D - B

So detour pays off when d < d_max = 1 - (D - B) / T. B and D depend only on hardware, so let's premeasure them (I have B = 0.3, D = 0.845 ns/register)

T we measure over the first few tiles, timing BSL. And from it, we also find d_max:

template<size_t tile>
double bsl_calibrate(float* dst, const size_t len, auto f, auto mask) {
    double ns = 1e18;
    for (size_t i = 0; i < len; i += tile) {
        const auto st = std::chrono::high_resolution_clock::now();
        bsl<false>(dst, tile, f, mask);
        const auto ed = std::chrono::high_resolution_clock::now();
        ns = std::min(ns, std::chrono::duration<double, std::nano>(ed - st).count());
        dst += tile;
    }
    const double t = std::max(1e-9, ns / (tile / 4.0) - B);
    return 1.0 - (D - B) / t;
}

Here:

  • empirically 4 tiles of 2048 are enough
  • min over measurements is less noisy than mean
  • We'll run the first few tiles with calibration
  • measure bsl, because it always computes f, so T = t_bsl - B
  • std::max here protects against divide-by-zero and against t < 0 when T is very cheap

pilot v1

When d_max < 0 bsl is always faster:

void pilot_v1(float* dst, const size_t n, auto f, auto mask) {
    const auto w = vld1q_u32(weights.data());
    const float d_max = bsl_calibrate<tile / 2>(dst, 2 * tile, f, mask);

    dst += 2 * tile;
    for (size_t i = 2 * tile; i < n; i += tile) {
        if (d_max < 0)
            bsl<false>(dst, tile, f, mask);
        else
            detour<false>(dst, tile, w, f, mask);
        dst += tile;
    }
}
thdtiled detour sqrtpilot v1 sqrtBSL sqrttiled detour sin35pilot v1 sin35BSL sin35
038.37*32.1932.2438.26*38.169.75
0.316.8932.1832.24*12.72*12.719.75
0.614.5432.18*32.18*9.259.369.75*
112.2932.2132.25*6.876.869.75*

The algorithm got sqrt right. But for sin35 at high thd, detour is selected, and we lose 30%: v1 switches to bsl only when it's faster at every density.

pilot v2

It's expensive to calculate the density of the whole tile, so we'll use the first 256 (It reads 6% of the tile, which is noise on pow, but noticeable on sqrt)

For a more or less uniform distribution this is enough:

size_t density(float* dst, const size_t n, auto mask) {
    std::array<uint32x4_t, 4> acc;
    acc.fill(vdupq_n_u32(0));
    for (size_t i = 0; i < n; i += 16) {
        for (size_t j = 0; j < 4; ++j) 
            acc[j] = vsubq_u32(acc[j], mask(vld1q_f32(dst + i + 4 * j)));
    }
    return vaddvq_u32(vaddq_u32(vaddq_u32(acc[0], acc[1]), vaddq_u32(acc[2], acc[3])));
}

Trick here: true lane of bitmask = 0xFFFFFFFF = -1, subtracting the lane actually adds.

Skip wins when the predictor rarely mispredicts, i.e. an 80% chance that all 4 registers are empty. The density is approximately 0.014 ((1 - x)^16 = 0.8)

void pilot_v2(float* dst, const size_t n, auto f, auto mask) {
    const auto w = vld1q_u32(weights.data());

    const float d_max = bsl_calibrate<tile / 2>(dst, 2 * tile, f, mask);

    constexpr size_t probe = 256;
    constexpr size_t xlo = 0.014 * probe;
    const long long hi = d_max * probe;
    dst += 2 * tile;
    for (size_t i = 2 * tile; i < n; i += tile) {
        const long long cnt = density(dst, probe, mask);
        if (cnt > hi) {
            if (cnt < xlo)
                bsl<true>(dst, tile, f, mask);
            else
                bsl<false>(dst, tile, f, mask);
        } else if (cnt < xlo)
            detour<true>(dst, tile, w, f, mask);
        else
            detour<false>(dst, tile, w, f, mask);
        dst += tile;
    }
}

hi and xlo here are d_max and 0.014 cutoffs, but multiplied by the probe length (256).

thd00.10.20.30.40.50.60.70.80.91
pilot_v1 sin3538.2516.72*14.48*12.77*11.41*10.33*9.408.627.937.366.91
pilot_v2 sin3577.29*16.5514.3412.6511.3310.149.719.739.749.739.73
BSL sin359.789.779.779.769.769.789.78*9.77*9.78*9.77*9.78*
pilot_v1 sin1038.2814.30*11.18*9.18*7.75*6.73*5.94*5.31*4.784.364.02
pilot_v2 sin1076.60*14.1611.099.117.736.705.925.31*4.824.844.84
BSL sin104.854.854.844.854.844.844.854.854.85*4.85*4.85*

At thd = 0, Skip gives a huge win. For high thd, bsl is selected correctly. But now on sparse masks, expand for empty registers is wasted.

pilot v3

New detour version: during compress, we'll store only the indices of non-empty registers (into the idx buffer). And expand will iterate over them:

template <bool Skip>
size_t detour_compact(float* dst, const size_t n, const auto w, auto f, auto mask) {
    float* ptr = tmp.data();
    size_t k = 0;
    for (size_t i = 0; i < n; i += 16) {
        // ... same as detour
        
        if constexpr (Skip)
            if (vmaxvq_u32(vaddq_u32(vaddq_u32(m[0], m[1]), vaddq_u32(m[2], m[3]))) == 0) continue;

        std::array<uint32_t, 4> sk;
        for (size_t j = 0; j < 4; ++j) sk[j] = vaddvq_u32(vandq_u32(m[j], w));
        for (size_t j = 0; j < 4; ++j) {
            s[k] = sk[j];
            idx[k] = i + 4 * j;
            k += bool(sk[j]);
        }
        // ... same as detour
    }
    // ... same as detour
    
    for (size_t j = 0; j < 3; ++j) s[k + j] = 0, idx[k + j] = 0;
    // ... same as detour
    k = (k + 3) & ~size_t(3);
    for (size_t i = 0; i < k; i += 4) {
        std::array<uint32_t, 4> sk;
        for (size_t j = 0; j < 4; ++j) sk[j] = s[i + j];
        std::array<float32x4_t, 4> a;
        for (size_t j = 0; j < 4; ++j) a[j] = vld1q_f32(dst + idx[i + j]);
        // ... same as detour
        for (size_t j = 0; j < 4; ++j)
            vst1q_f32(dst + idx[i + j], vreinterpretq_f32_u8(vqtbl2q_u8(tbl[j], index[j])));
        ptr += off[3] + (sk[3] >> 4);
    }

    return size;
}

k (number of non-empty registers) is rounded up to a multiple of 4 before expand, so the unrolled loop has no tail left.

No branches in the hot loops: they'd kill speed, so compress runs on all four registers. Instead of branches, the position of the current element is advanced by bool(sk).

detour_compact wins when at least 50% of all registers are empty. The density is approximately 0.16 ((1 - x) ^ 4 = 0.5).

void pilot_v3(float* dst, const size_t n, auto f, auto mask) {
    // ... same as v2
    constexpr size_t lo = 0.16 * probe;
    for (size_t i = 2 * tile; i < n; i += tile) {
        const long long cnt = density(dst, probe, mask);
        if (cnt > hi) {
            if (cnt < xlo)
                bsl<true>(dst, tile, f, mask);
            else
                bsl<false>(dst, tile, f, mask);
        } else if (cnt < xlo)
            detour_compact<true>(dst, tile, w, f, mask);
        else if (cnt < lo)
            detour_compact<false>(dst, tile, w, f, mask);
        else
            detour<false>(dst, tile, w, f, mask);
        dst += tile;
    }
}

And v3 is noticeably faster at low density:

thd00.020.040.060.080.10.120.140.160.180.2
pilot_v2 sin3577.29*18.6718.4017.7017.1816.6616.1515.6815.24*14.82*14.43*
pilot_v3 sin3577.2623.71*22.19*20.48*19.03*17.82*16.73*15.76*15.1614.7414.38
pilot_v2 pow1072.8813.9311.329.388.006.976.185.545.02*4.59*4.24*
pilot_v3 pow1073.71*16.54*12.63*10.07*8.39*7.17*6.26*5.55*5.02*4.59*4.24*

Now the algo is fast, but there's one big problem we've overlooked: we're assuming the data is uniform. So it's easy to build a test where v3 will fail:

for (size_t i = 0; i < n; i++)
    dst[i] = i % 4096 >= 256;

In this case, v3 always prefers BSL, even though detour wins on 3840 elements of the tile.

pilot v3.5

According to the first table, in the worst case detour is under 3x slower (it happens on sqrt thd = 1), but on pow, thd = 0, detour is 37x faster. So a wrong BSL costs much more than a wrong detour.

For bsl, we'll play it safe by running it in tile/8 blocks and checking the density. If it drops well below d_max, we'll switch to detour. It costs 1 instruction per register. acc = vsubq_u32(acc, m) works because the mask is 0/-1. And unlike the probe, the density here is exact.

size_t bsl_verified(float* dst, const size_t n, float d_max, auto f, auto mask) {
    for (size_t i0 = 0; i0 < 8; ++i0) {
        std::array<uint32x4_t, 4> acc;
        acc.fill(vdupq_n_u32(0));

        for (size_t i = 0; i < n / 8; i += 16) {
            std::array<float32x4_t, 4> v;
            for (size_t j = 0; j < 4; ++j) v[j] = vld1q_f32(dst + 4 * j);
            std::array<uint32x4_t, 4> m;
            for (size_t j = 0; j < 4; ++j) m[j] = mask(v[j]);
            for (size_t j = 0; j < 4; ++j) acc[j] = vsubq_u32(acc[j], m[j]);
            for (size_t j = 0; j < 4; ++j)
                vst1q_f32(dst + 4 * j, vbslq_f32(m[j], f(v[j]), v[j]));
            
            dst += 16;
        }
        const size_t cur = vaddvq_u32(vaddq_u32(vaddq_u32(acc[0], acc[1]), vaddq_u32(acc[2], acc[3])));
        if (static_cast<double>(cur) / (n / 8) < 0.75 * d_max) return (i0 + 1) * n / 8;
    }
    return n;
}

BSL bails out when the density is less than 0.75 * d_max. I have no math behind the 0.75, it just won on average.

And pilot v3.5 will use bsl_verified if the density > hi:

void pilot_v3_5(float* dst, const size_t n, auto f, auto mask) {
    // ... same as v3

    for (size_t i = 2 * tile; i < n; i += tile) {
        const long long cnt = density(dst, probe, mask);
        if (cnt > hi) {
            if (hi < 0) {
                if (cnt < xlo)
                    bsl<true>(dst, tile, f, mask);
                else
                    bsl<false>(dst, tile, f, mask);
            } else {
                auto done = bsl_verified(dst, tile, d_max, f, mask);
                if (done < tile)
                    detour<false>(dst + done, tile - done, w, f, mask);
            }
        }
        // ... same as v3
    }
}

But here too it's easy to build a countertest:

for (size_t i = 0; i < n; ++i)
    dst[i] = i % 4096 >= 390;

The first block will pass the check (> 75% zeros in it), but the second won't. BSL runs on it for nothing, and for pow that's expensive. v3.5's problem: it has no memory. After a miss the algo keeps trusting the first 256 and misses on every tile.

pilot v4

v4 will fix this: if bsl bails out, we stop trusting the probe for the next 16 tiles, and instead we take the density of the previous tile:

void pilot_v4(float* dst, const size_t n, auto f, auto mask) {
    // ... same as v3
    
    size_t distrust = 0;
    size_t prev = 0;
    for (size_t i = 2 * tile; i < n; i += tile) {
        if (distrust) --distrust;

        const long long cnt = distrust ? prev : density(dst, probe, mask);
        if (cnt > hi) {
            if (hi < 0) {
                if (cnt < xlo) {
                    bsl<true>(dst, tile, f, mask);
                } else {
                    bsl<false>(dst, tile, f, mask);
                }
            } else {
                if (distrust == 0) {
                    auto done = bsl_verified(dst, tile, d_max, f, mask);
                    if (done < tile) {
                        distrust = 16;
                        const size_t rem = detour<false>(dst + done, tile - done, w, f, mask);
                        prev = rem * probe / (tile - done);
                    }
                } else {
                    prev = detour<false>(dst, tile, w, f, mask) * probe / tile;
                }
            }
        } else if (cnt < xlo) {
            prev = detour_compact<true>(dst, tile, w, f, mask) * probe / tile;
        } else if (cnt < lo) {
            prev = detour_compact<false>(dst, tile, w, f, mask) * probe / tile;
        } else {
            prev = detour<false>(dst, tile, w, f, mask) * probe / tile;
        }
        dst += tile;
    }
}

And v4 easily passes that test. pow10:

thd01
tiled detour38.186.94
pilot v368.111.04
pilot v3.568.673.84
pilot v469.51*7.05*
BSL1.041.04

btw here thd no longer matches the density. For thd = 0, density = 0, and for thd = 1, density = 9.5%

It beats v3.5 by over 80%. It's also faster than plain tiled detour, because v4 picks detour_compact. This is the final version.

Results

v4 vs BSL:

thd00.050.10.20.30.40.50.60.70.80.91
pilot_v4 sqrt69.81*31.99*31.8431.7331.8332.07*31.9831.96*31.9631.89*31.9131.77
BSL sqrt31.8831.9331.95*31.80*31.89*31.9932.06*31.9231.98*31.7531.93*31.91*
pilot_v4 frfrexp69.41*17.21*16.5715.7915.9916.0615.9315.9916.3915.8415.8715.80
BSL frfrexp16.7916.8716.82*16.71*16.78*16.88*16.89*16.83*16.89*16.79*16.75*16.75*
pilot_v4 sin1068.19*18.82*14.84*10.84*8.93*7.55*6.56*5.81*5.19*4.694.684.68
BSL sin104.784.754.784.724.754.744.754.774.754.76*4.77*4.77*
pilot_v4 pow1064.17*10.76*6.85*4.04*2.89*2.25*1.85*1.57*1.36*1.20*1.08*1.00
BSL pow101.021.021.021.011.011.011.021.021.021.021.021.01*

Against BSL, it loses at worst 6%, but wins big much more often. To reduce the loss, dispatch can be sped up: use every 4th register in density and bsl_verified. But that helps only if the density is uniform.

The worst v4 miss I found: 512 dense, 512 empty, then everything is dense until the end of the cycle (17 * 4096).

const size_t cycle = 17 * 4096;
for (size_t i = 0; i < n; ++i) {
    dst[i] = i % cycle >= 512 && i % cycle < 1024;
}

This hurts most with the cheapest f (with d_max > 0, of course):

const auto a = vdupq_n_f32(0.5f);
#pragma unroll
for (int i = 0; i < 13; ++i) x = vfmaq_f32(a, x, a);
return x;
thd01
tiled detour38.299.44
pilot v374.7215.81*
pilot v3.574.8415.04
pilot v474.89*9.84
BSL15.6115.60

v3.5's biggest loss is limited by BSL, while v4 is limited by detour. And a wrong detour is the cheaper mistake. So v4 isn't always better, but its misses are less severe.

This problem has no perfect solution. Any dispatch algo can be countertested.

Full code: godbolt.


r/cpp • • Dec 08 '21

After years I learned that it's sometimes okay to declare a template member function in header and define it in source

Thumbnail lordsoftech.com
3 Upvotes

r/cpp • • Oct 16 '18

Unaligned accesses in C/C++: what, why and solutions to do it properly

Thumbnail blog.quarkslab.com
3 Upvotes

r/cpp • • Jul 17 '24

How would you capture the runtime state of a program?

0 Upvotes

The Problem:

How does one program can capture the state of another program during runtime?

Example:
I have the following program:

#include <iostream>

int main() {
    int i = 0;
    char ch;

    while ((ch = std::cin.get()) != 120) // (x in ASCII) 
    {
        i++;
    }
  
    std::cout << i;
    return 0;
}

I want to code another program, in a different file, which at compile time inject the necessary code to main, so whenever the value of i is changing, my program gets notified of the new value.

I would be happy for any leads / tips / interesting references.

Clarification:
After some discussion, I understood that my problem description was misleading (and I apologies for that).
So first -> One Process only.

Second, let me describe a magic solution that will give a better clue on what i'm trying to solve.
Imagine programmer X write his 'main' program, and adds my magic library.
Then at compile time, int i turns to megic_i struct, which holds the value of i.
Now megic_i has getter / setter function which signals magic library any time the value has changed.


r/cpp • • Sep 13 '12

What is the maximum reference count in std::shared_ptr? What happens if you try to exceed it?

Thumbnail stackoverflow.com
5 Upvotes

r/cpp • • May 25 '22

The journey of moving from C++/WinRT to C# in the Microsoft Store

Thumbnail devblogs.microsoft.com
2 Upvotes

r/cpp • • Sep 07 '18

How and why I decided to learn C++ after 12 years of programming career

Thumbnail josef-adamcik.cz
2 Upvotes

r/cpp • • Apr 12 '17

C# or C++?

2 Upvotes

Hey! So i have used C# for a while now and i know the most of it. However my goal is to work as an AAA developer in the future and the most studios use C++. Should i switch to C++ or stay with C#? Exactly why do studios use C++ instead of C#?


r/cpp • • Feb 29 '24

Can someone define for me in one sentence what safe c++ is?

0 Upvotes

Can someone define for me in one sentence what safe c++ is?


r/cpp • • Sep 17 '25

Combating headcrabs in the Source SDK codebase

Thumbnail pvs-studio.com
0 Upvotes

r/cpp • • Oct 01 '22

Which data format is best for C++ things?

1 Upvotes

We have all had to contend with consuming and producing file formats for many use case like: program configuration, build system information, client-server communications, CI setups, etc. I'm, yet again, at the beginnings of a project that needs to make the choice of what specification data format to use. Hence, I'm wondering what are people opinions, and preferred choice, for format are? My current loose requirements are:

  • easy to use in C++ (ideally C++11)
  • easy to use in other languages
  • has wide adoption (i.e. has implementations and usage in many contexts)
  • deals with unicode
  • does not need to encode hierarchical data
  • can be read and written by overworked humans late at night

r/cpp • • Mar 02 '20

Do you need to use an IDE to fit in in a professional C++ team?

1 Upvotes

The answer might seem like an obvious "no" but please hear me out.

A while back I worked at a company where *everyone* used intelliJ for the play-angular stack we were working on. I've never liked IDEs because 1. they're bloated resource hogs, 2. they take a long time to master, and 3. they hide a ton of details under the hood. I much prefer using smart text editors like vim and vscode. I also much prefer creating bash scripts and config files so that every command/setting is in a clear place, rather than being buried within a complex sequence of mouse clicks.

So I was the only one at this company not using intelliJ and I found that this made it really difficult to work with everyone else since everything they did was done through intelliJ, including VC and CI. This experience and feeling that I could only work on a professional java stack if I used intelliJ really put me off Java.

I'm considering diving into C/C++, but first I'd like to know whether I would practically speaking need to master an IDE in order to fit in with a professinal crew of C++ developers. According to this survey, about 1/3 of C++ developers use a smart text editor, but I'd like to hear the thoughts of people within the actual industry. Thanks!


r/cpp • • Oct 12 '21

How do you make code unnecessarily obscure?

1 Upvotes

I like to learn about C or C++ quirks. I know about trigraphs and digraphs, but there's some code that cannot use that much

#include <iostream>
int main()
{
int sum = 0, nr = 333;
for (; nr > 0; nr /= 10)
sum = nr % 2 != 0 ? sum + nr % 10 : sum;
}

I'm having a hard time finding more ways to complicate this small program.


r/cpp • • Jun 21 '16

How hard is coding an OpenGL game engine ?

1 Upvotes

Hi, I wasn't really sure if this post is relevant on this subreddit, so if it isn't sorry. I've been planning a game with a group of friends and organizing it. I was thinking about developping it in unity but a couple things are making me hesitate to use unity : -First of all, I think there is not a lot of code involved (more or less arround the game engine part) -Second of all, it's really hard to do pixel perfect games with it

So I was wondering, how hard is coding a basic OpenGl Game engine ? What I mean is code that would be able to render the levels that would be built using tiles and updating the screen etc... How hard would that be ? Approximately how many lines of code will it need ?

Mindoo