Click here to Skip to main content
15,898,134 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Things I find buried in the C++ reference Pin
Espen Harlinn14-Mar-21 12:42
professionalEspen Harlinn14-Mar-21 12:42 
GeneralRe: Things I find buried in the C++ reference Pin
honey the codewitch14-Mar-21 12:54
mvahoney the codewitch14-Mar-21 12:54 
GeneralRe: Things I find buried in the C++ reference Pin
Espen Harlinn15-Mar-21 11:29
professionalEspen Harlinn15-Mar-21 11:29 
GeneralRe: Things I find buried in the C++ reference Pin
honey the codewitch15-Mar-21 11:57
mvahoney the codewitch15-Mar-21 11:57 
GeneralRe: Things I find buried in the C++ reference Pin
Stuart Dootson15-Mar-21 1:09
professionalStuart Dootson15-Mar-21 1:09 
GeneralRe: Things I find buried in the C++ reference Pin
honey the codewitch15-Mar-21 2:22
mvahoney the codewitch15-Mar-21 2:22 
GeneralRe: Things I find buried in the C++ reference Pin
Stuart Dootson15-Mar-21 3:31
professionalStuart Dootson15-Mar-21 3:31 
GeneralRe: Things I find buried in the C++ reference Pin
honey the codewitch15-Mar-21 3:52
mvahoney the codewitch15-Mar-21 3:52 
That's cool. I'm sitting here super impressed by the compiler right now as I haven't done any hand optimizations to this yet except special casing when the pixel is represented natively (can be cast directly to the destination buffer):

C++
constexpr inline static int_type at(const uint8_t* bitmap,size_t index) {
    if(native_int) {
        // best case:
        const size_t offs = index;
        return order_guard(*(((int_type*)bitmap)+offs));
    } 
    const size_t ofsb=((index*bit_depth)/8);
    bitmap+=ofsb;
    const size_t ofsm=(index*bit_depth)-(ofsb*8);
    // most general case: (read 1 bit at a time)
    int_type result = 0;
    uint8_t dat = *(bitmap);
    for(size_t i = 0;i<bit_depth;++i) {
        const size_t bit = 7-((ofsm+i)&7);
        const uint8_t msk = (1<<bit);
        const bool mov = bit==7;
        result<<=1;
        result|=(0!=(dat&msk));
        if(mov) // get next byte
            dat=*(++bitmap);
    }
    return order_guard(result);
}
... (below is from main)

typedef channel_traits<channel_name::V,channel_kind::luminosity,uint8_t,1> mono_1bit_luminosity_channel_t;
typedef pixel<color_model::none, false, mono_1bit_luminosity_channel_t> mono1_t;

uint16_t data=0xCCCC;
for(size_t x=0;x<16;++x) {
    auto val = mono1_t::at((uint8_t*)&data,x);
    printf("%d",val);
}
printf("\r\n");
return 0;


Nevertheless, the assembly is better than i expected (this is the entire main but it pretty much goes from .L2:
ASM
main:
        push    rbp
        mov     ebp, 1
        push    rbx
        xor     ebx, ebx
        sub     rsp, 24
        mov     WORD PTR [rsp+14], -13108
.L2:
        mov     rcx, rbx
        mov     rdx, rbx
        mov     eax, ebp
        xor     esi, esi
        not     rcx
        shr     rdx, 3
        mov     edi, OFFSET FLAT:.LC0
        and     ecx, 7
        sal     eax, cl
        test    BYTE PTR [rsp+14+rdx], al
        setne   sil
        xor     eax, eax
        inc     rbx
        call    printf
        cmp     rbx, 16
        jne     .L2
        mov     edi, OFFSET FLAT:.LC1
        call    puts
        add     rsp, 24
        xor     eax, eax
        pop     rbx
        pop     rbp
        ret


Edit: VS Code (at least under Linux undoes clipboard copy commands when you use undo! Mad | :mad: )

I don't care that it's not optimized yet because I've set the code up where I can optimize by adding if statements which will always be settled at compile time.
Real programmers use butterflies


modified 15-Mar-21 10:35am.

GeneralRe: Things I find buried in the C++ reference Pin
James Curran15-Mar-21 4:43
James Curran15-Mar-21 4:43 
GeneralOh no! Pin
Jörgen Andersson12-Mar-21 9:45
professionalJörgen Andersson12-Mar-21 9:45 
GeneralRe: Oh no! Pin
NotTodayYo12-Mar-21 10:09
NotTodayYo12-Mar-21 10:09 
GeneralRe: Oh no! Pin
RickZeeland12-Mar-21 21:04
mveRickZeeland12-Mar-21 21:04 
GeneralExploding Plastic Pin
honey the codewitch12-Mar-21 9:08
mvahoney the codewitch12-Mar-21 9:08 
GeneralRe: Exploding Plastic Pin
OriginalGriff12-Mar-21 11:17
mveOriginalGriff12-Mar-21 11:17 
GeneralThought of the Day Pin
OriginalGriff12-Mar-21 4:44
mveOriginalGriff12-Mar-21 4:44 
GeneralRe: Thought of the Day Pin
W Balboos, GHB12-Mar-21 5:22
W Balboos, GHB12-Mar-21 5:22 
GeneralThis was sent to me today ... Pin
CHill6012-Mar-21 3:29
mveCHill6012-Mar-21 3:29 
GeneralRe: This was sent to me today ... Pin
PIEBALDconsult12-Mar-21 3:32
mvePIEBALDconsult12-Mar-21 3:32 
GeneralRe: This was sent to me today ... Pin
CHill6012-Mar-21 3:33
mveCHill6012-Mar-21 3:33 
GeneralRe: This was sent to me today ... Pin
PIEBALDconsult12-Mar-21 3:42
mvePIEBALDconsult12-Mar-21 3:42 
GeneralSound of the Week Pin
Sander Rossel11-Mar-21 23:36
professionalSander Rossel11-Mar-21 23:36 
GeneralRe: Sound of the Week Pin
CHill6012-Mar-21 3:30
mveCHill6012-Mar-21 3:30 
GeneralRe: Sound of the Week Pin
David O'Neil12-Mar-21 8:10
professionalDavid O'Neil12-Mar-21 8:10 
GeneralRe: Sound of the Week Pin
Sander Rossel13-Mar-21 0:58
professionalSander Rossel13-Mar-21 0:58 
GeneralRe: Sound of the Week Pin
David O'Neil13-Mar-21 23:19
professionalDavid O'Neil13-Mar-21 23:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Straw Poll

Were you affected by the geomagnetic storms this past weekend?
Communication disruptions, electrified pipes, random unexplained blue-screens in Windows - the list of effects is terrifying.
  Results   394 votes