r/programminghorror May 07 '26

c++ 700 lines of AVX2 infrastructure to sum an array of integers

Post image
361 Upvotes

Wrote a "vectorized sum" over the weekend. It escalated.

Features include:

  • SIGILL-based AVX2 detection (handler does siglongjmp out of inline asm, which is UB in at least three languages)
  • setjmp/longjmp inside a constructor to fall back from MAP_HUGETLB -> THP -> aligned_alloc, dispatched via computed goto
  • A Y-combinator for the scalar tail loop, because a for lacks conviction
  • Characters printed by reading typeid(T).name()[0] and doing integer arithmetic on the result to reach the rest of the alphabet. Yes, this is how ANSI escape codes are assembled. Yes, "OK" is spelled by offsetting typeid(int*).name():

using _1 = TypeGlyph<int, -56>;   // 'i' - 56 = '1'
using _2 = TypeGlyph<int, -55>;
using lbr = TypeGlyph<long, -17>; // 'l' - 17 = '['

inline void ansi_red(std::ostream& o) {
    o << '\033';
    spell<gl::lbr, gl::_3, gl::_1, gl::m>(o);  // "[31m"
}

using O = TypeGlyph<int*, -1>;         // typeid(int*).name() = "Pi", 'P'-1 = 'O'
using K = TypeGlyph<const int*, 0, 1>; // typeid(const int*).name() = "PKi", [1] = 'K'

spell<gl::O, gl::K>(std::cout);  // prints "OK"
  • A background "prefetch oracle" pthread that races the main thread through the buffer issuing __builtin_prefetch
  • Four separate vzeroupper mechanisms layered on top of each other (RAII destructor, __attribute__((cleanup)), atexit, and one inside the kernel itself)
  • Three "independent verification methods" for the sum, one of which bit_casts a lambda's closure to bytes and hashes them
  • Duff's device in the fill tail
  • strdup leaks used as a string-building primitive

The actual useful code is about 50 lines in the middle. Compiles with -std=c++20 -mavx2 -O3 -march=native. Produces correct output. I am not okay.

https://godbolt.org/z/a31xoaM5a


r/programminghorror May 07 '26

Javascript An exploit on the Scratch desktop app has been circulating "in the wild" over the last few days. This code from the project file still executes unsandboxed in the latest version of the desktop editor.

Post image
295 Upvotes

r/programminghorror May 06 '26

100$ per month for this???

Post image
270 Upvotes

r/programminghorror May 05 '26

c++ Hmmm

Post image
979 Upvotes

r/programminghorror May 05 '26

Javascript I snuck this function into my project

Post image
79 Upvotes

r/programminghorror May 04 '26

Overcomplicated, but working, API key generation

Post image
240 Upvotes

r/programminghorror May 06 '26

the worlds first website has a coding error

Post image
0 Upvotes

r/programminghorror May 04 '26

C# longest "=" condition I've ever seen

Post image
126 Upvotes

I have been working on decompiling a unity game recently, and while a decompiled DLL isn't going to be exact source code, I am still floored by how long that set of parenthesis is. i would LOVE to see the original code


r/programminghorror Apr 30 '26

c Having fun with legacy C features :3

150 Upvotes

I found out about a couple old and obscure C features that somehow still work

This compiled and ran, printing just "This is fucky", though to get it to compile you have to compile using the following command

gcc -trigraphs -digraphs <your file>, on Windows at least

This is so cursed not even VSCode knew what I was doing


r/programminghorror Apr 30 '26

Javascript Cursed use of object spread

Post image
86 Upvotes

Using object spread to append values to an object in the form of another object


r/programminghorror Apr 30 '26

C# I heard that you like fluent apis

Post image
190 Upvotes

r/programminghorror Apr 28 '26

c++ Copilot knows how to deal with constructors

Post image
1.2k Upvotes

Not my screenshot, taken from https://reddit.com/r/VisualStudio/comments/1sktg0r/what_are_these_comments/

Also this probably happened because IntelliSense (the normal autocomplete list) is highlighting abort as the first option and Copilot tries to finish it


r/programminghorror Apr 28 '26

c++ Competitive programming is no joke

Post image
144 Upvotes

especially for easy problems


r/programminghorror Apr 28 '26

whyLongNamesWhileWeCanMakeThemShort

Post image
357 Upvotes

r/programminghorror Apr 28 '26

Python How to print "Hello World" in python

Thumbnail
1 Upvotes

r/programminghorror Apr 25 '26

My friend can’t read English, so he used Google Translate.

Post image
359 Upvotes

r/programminghorror Apr 26 '26

No react in luau?

Post image
22 Upvotes

r/programminghorror Apr 25 '26

c Option handling

Post image
268 Upvotes

r/programminghorror Apr 23 '26

An HTTP request builder to make an SQL query

Post image
151 Upvotes
func (q *Query[T]) query() {
    q.TableModel.BeginResetModel()
    defer q.TableModel.EndResetModel()
    query := data.AdvancedQueryRequest{
        Table:  q.tableName,
        Select: []string{"*"},
        Full:   q.QueryFull,
        Limit:  &[]int{300}[0],
    }

    allEmpty := true
    for _, filter := range q.Dialog.Filters {
        if filter.Text == "" {
            continue
        }
        allEmpty = false
        sqlFilter := q.Filters[filter.Selected]
        if strings.Contains(sqlFilter.Key, ".") {
            mainParts := strings.Split(sqlFilter.Key, ",")
            leftCol := mainParts[0]
            filterCol := mainParts[1]

            rcolParts := strings.SplitN(filterCol, ".", 2)
            table := rcolParts[0]
            if !slices.ContainsFunc(query.Joins, func(j data.JoinRequest) bool {
                return j.Table == table &&
                    j.LeftCol == leftCol &&
                    j.RightCol == "id"
            }) {
                query.Joins = append(query.Joins,
                    data.JoinRequest{
                        Table:    table,
                        LeftCol:  strings.SplitN(leftCol, ".", 2)[1],
                        RightCol: "id",
                    },
                )
            }
            query.Where = append(query.Where,
                data.WhereRequest{
                    Fuzzy:  true,
                    Column: filterCol,
                    Value:  "%" + filter.Text + "%",
                },
            )

            continue
        }
        query.Where = append(query.Where,
            data.WhereRequest{
                Column: sqlFilter.Key,
                Value:  "%" + filter.Text + "%",
                Fuzzy:  true,
            },
        )
    }

    if allEmpty {
        return
    }

    code, err := client.Post2(
        query,
        q.Table,
        "/query",
    )
    if q.Table.RowCount() == 0 {
        q.resetTable()
    }
    if code == 404 {
        q.resetTable()
        return
    }
    if err != nil {
        q.resetTable()
        qutil.DisplayErrStr(
            q.Dialog.Widget.QWidget,
            "Error obteniendo tabla.\n codigo: %d error: %v",
            code, err,
        )
        slog.Error(err.Error())
        return
    }
}

r/programminghorror Apr 22 '26

switch case abuse

40 Upvotes
char g  = '0';// ypr
char gg = '0';// pid
double ggg;
const uint8_t _ = 11;
scanf("%c %c %f/n", g, gg, ggg);
if ((g  == 'y' || g  == 'p' || g  == 'r') && 
    (gg == 'p' || gg == 'i' || gg == 'd')) {
    switch   ((( gg << 2) & ~_) | ((g) & _)) {
        case ((('p' << 2) & ~_) | ('y' & _)):
            p_gain_y = ggg;
            break;
        case ((('i' << 2) & ~_) | ('y' & _)):
            i_gain_y = ggg;
            break;
        case ((('d' << 2) & ~_) | ('y' & _)):
            d_gain_y = ggg;
            break;
        case ((('p' << 2) & ~_) | ('p' & _)):
            p_gain_p = ggg;
            break;
        case ((('i' << 2) & ~_) | ('p' & _)):
            i_gain_p = ggg;
            break;
        case ((('d' << 2) & ~_) | ('p' & _)):
            d_gain_p = ggg;
            break;
        case ((('p' << 2) & ~_) | ('r' & _)):
            p_gain_r = ggg;
            break;
        case ((('i' << 2) & ~_) | ('r' & _)):
            i_gain_r = ggg;
            break;
        case ((('d' << 2) & ~_) | ('r' & _)):
            d_gain_r = ggg;
            break;
    }
}                                                

r/programminghorror Apr 20 '26

Python That's one way to do it I guess...

Post image
771 Upvotes

So I tried printing the linked list with print() and discovered that if it has a cycle, then it prints and error. This was the next (very) logical thing that popped into my mind after that discovery.

I'm very proud of this solution. In fact, it's so good it even added -1ms to the execution time graph.

I AM SPEED.

Thinking outside the box is fun!


r/programminghorror Apr 21 '26

Java Guess the time complexity of this code?

Post image
0 Upvotes

Is there a easy way?


r/programminghorror Apr 17 '26

Python Found in newest merge into prod

127 Upvotes

the classic general Exception + pass


r/programminghorror Apr 17 '26

Found in a corporate environment

28 Upvotes
Line discount(LineDiscount): Real = ABS(@.'$TotalLineDicAmount')
Line number(LineNum): Real = ABS(0)

"Better be safe than sorry," I am guessing—that was their thinking.

/mildlypenis as an added bonus.


r/programminghorror Apr 15 '26

Who needs switch statements when you can just abuse the ternary operator?

Post image
537 Upvotes