lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


because I want to know use“cast_int“ to force “ui” to int ,why it's
going to be fast。。

 am just working with  [MacBook Air  Chip:APPLE M1]  (64bit, C++ compiler)

use the code build and run

#include <iostream>
#include <chrono>

int test1(signed int a, int b) {
    auto r = a % b;
    return r;
}

unsigned int test2(unsigned int a, int b) {
    auto r = a % b;
    return r;
}

using std::chrono::steady_clock;

int main() {

    int  count = 0;
    std::cin >> count;
    steady_clock::time_point tp1 = steady_clock::now();
    for (int i = 0; i < count; i++) {
        test1(i, 2);
    }
    steady_clock::time_point tp2 = steady_clock::now();
    std::cout << (tp2 - tp1).count() << std::endl;

    steady_clock::time_point tp3 = steady_clock::now();
    for (int i = 0; i < count; i++) {
        test2(i, 2);
    }
    steady_clock::time_point tp4 = steady_clock::now();
    std::cout << (tp4 - tp3).count() << std::endl;

    return 0;
}


the generated assembly for this the "int test1(signed int a, int b)"
function comes like this
LDR w10,[SP, #O×10+var_8]
SDIV w9,w8,w10
MULw9,W9,10

the generated assembly for this the "int test2(signed int a, int b)"
function comes like this
LDR w10,[SP,#Ox10+var_8]
UDIV w9,w8,w10
MUL w9,w8,w10


the results:
(tp2 - tp1).count()    2299667
 (tp4 - tp3).count()   3260583

bil til <biltil52@gmail.com> 于2023年1月21日周六 01:30写道:
>
> Am Fr., 20. Jan. 2023 um 17:23 Uhr schrieb game frog
> <froggame505384662@gmail.com>:
> >
> >  I found this explanation on this webpage
> > https://answerbun.com/stack-overflow/why-is-__int128_t-faster-than-long-long-on-x86-64-gcc/
>
> ... really funny / interesting. So speciality on Intel CPU but not on
> AMD, I would not have thought this... .
>
> div of course always ist th the hardest operation - principally every
> CPU does this like you learn "devide by hand" in the grammar school
> ... . It needs many multiply operations (typically ca. the number you
> bits you want to devide... but some CPUs have "tricks" / do part of
> the work in Silicon in parallel).
>
> thank you for this interesting input.
>
> ... but why are you interested SO much in this ... do you have any
> problems with this hash function, any timing problems?