« June 2017 | Main | August 2017 »

July 25, 2017

��������DZ�ڵĽ����һ������

���찢骷�������Ŀ�е�һ�� bug ������Ϊ���������ǰ��һ�µ��µġ���������ȫ��ͬ�� C ���룬����Ҳ�ϸ�һ�£����Ǽ�����˲���ͬ�Ľ�����Ҷ��������dz�����Ȥ����ϸ�о���һ�³���

ԭʼ����ȽϷ��ӡ���Ū���ԭ�����Ҽ��˳�����Ĵ��룬������������⣺

static void
foo(float x) {
    float xx = x * 0.01f;
    printf("%d\n", (int)(x * 0.01f));
    printf("%d\n", (int)xx);
}

int
main() {
    foo(2000.0f);
    return 0;
}

ʹ�� gcc 4.9.2 ��ǿ��ʹ�� x87 ��������������У���ᷢ�����˲���Ľ����

gcc a.c -mfpmath=387

19
20

ǰһ�ε������ 19 ����һ���� 20 ��

����Ϊʲô�أ������������� gcc ���ɵĴ��룬�ҽ�ȡ����صĶ��䣺

    flds    16(%rbp)
    flds    .LC0(%rip)
    fmulp   %st, %st(1)
    fstps   -4(%rbp)          ; 1. x * 0.01f ������浽�ڴ��е� float ������
    flds    16(%rbp)
    flds    .LC0(%rip)
    fmulp   %st, %st(1)
    fisttpl -20(%rbp)        ; 2. x * 0.01f ���ֱ��ת��Ϊ����
    movl    -20(%rbp), %eax
    movl    %eax, %edx
    leaq    .LC1(%rip), %rcx
    call    printf
    flds    -4(%rbp)                 ; 3. ���� 1. ����ij˷����
    fisttpl -20(%rbp)
    movl    -20(%rbp), %eax
    movl    %eax, %edx
    leaq    .LC1(%rip), %rcx
    call    printf

��������������ע�͡�

���ȣ�0.01 ���޷���ȷ��ʾ�� 2 ���Ƶģ����� * 0.01 �������һ���������

�������㶼�� x * 0.01f ����Ȼ�� C ���Ե�ת�����򣬱���ʽ�ж��� float ʱ���� float �������㡣������ gcc ���ɵĴ��벢û���ϸ����� FPU �ľ��ȿ��ƣ���ע�� 2 ����ط����˷������ֱ�ӴӸ���Ĵ���ת��Ϊ�����ġ�����ע�� 1 ����ط����ѳ˷����ͨ�� fstps �Ե;�����ʽ���浽�ڴ棬����ע�� 3 �ĵط� flds ���ء�

������ע�� 2 ��ע�� 3 �ĵط�������Ĵ��� st �ڵ�ֵ��ʵ���в��ģ��⵼���� fisttpl ת��Ϊ����������ͬ��


2018 �� 6 �� 14 �ղ��䣺

�� DDJ 1997 ���һƪ��̸��������̸�������Ƶ����⡣

�������£�

Let me give an example, which arose yesterday. A student was doing a computation involving a simulation of a plasma. It turns out that as you go through this computation there will be certain barriers. This may be a reflecting barrier. That means if a particle moves through this barrier, it should not really go through, it should be reflected. Others may be absorbing, others may be periodic. He has a piece of code that is roughly

float x, y, z;

int j;
...
x = y + z; 
if (x >= j) replace (x);
y = x;

...

As far as we can tell, when he turns on optimization, the value of x is computed in a register with extra width. This happens on the Intel machine because the registers have extra width. It also happens on some others. The value of x that he computes is not, in fact, a float. It's in a register that's wider. The machine stores x somewhere, and in the course of doing that, converts it to a float. But the value used in the comparison is the value in the register. That x is wider. The condition that the register x be greater than or equal to j doesn't guarantee that x, when stored in y, will be less than j. Sometimes y=j, and that should never be. My student counted on what compiler writers call "referential transparency" and was disappointed because the compiler writer saved some time in optimization by not reloading the register from the stored value.

July 21, 2017

��ֹ��Ȱ�����һ������

��Ȼ�����ڵļ��ܼ�������Ҫѡ��ļ����㷨û���⣬�ںܳ�һ��ʱ�䶼��̫�õ��ļ���ͨѶ���˽��ܻ�����ġ���������ض��ļ���ͨѶЭ�飬���Ǻܿ����ҵ������ҵ�ij��ģʽ�����ģʽ����ת��Ϊ���ģ������Բ²�����Ƿ���ʹ���ض�Э�顣

���⣬��������ô����ͨѶ�������ض�����������ʱ������Ҳ����й¶������ܣ���ʲô����ͨѶ��ÿ�� ip �������Щ���ǿɹ�ƥ���������

����Ϊ�����������£�ͨѶ���ȶ����Ǵ��ڴ���������ġ���ô�����ñ������ַ���Ӧ����ȥ��������Щ����������

��˵˵������ʱ��������

��νʱ������������ͨѶ������ÿ���ֽ��ʹ��ʱ�������С����Ǵ���������㷨�����Ե���Ϣ����Ϊͨ�����Dz�����������������������Ϣ��Ҳ��û�б�Ҫ��������н��д���������ȴ���ױ�¶Э��������

������������ķ���Ҳ�ܼ򵥣��������������Ծ����ȶ������ʴ���ͺ��ˡ�������û����Ч�������Ҷ����־�����ͬ�İ���С�����̶�Ƶ�ʷ������ݡ�ֻ����������������Ҫ����ʱ���ٰ����ݸ��ص��ŵ��ϣ��滻�������������ɡ�

��������� 64KBps ���ȶ�ͨѶ������7 * 24 Сʱ������һ���µ������ܺʹ�Լ�� 160GB ���ң��� linode ����˵� 5$ ����һ�����ṩ������Ҳ�� 1TB ���ɱ����൱�͵ġ�

�������һ�������ӣ��ڽ�����֮��Ͳ���Ҫ�������������ӽ�����������Ч��ֹ��̽������Э��Ҳֻ����һ�Σ����ԱȽϰ�ȫ��

�Ҳ�����һ�죬�Ӱ칫�ҿ���ͬ����һ�������Ļ����������ӣ����ȶ����ʷ��ʹ�������ַ�����һ����������û���κθ��š������ε� log �������м�û�з�����������


�ڶ��Ǽ��ܡ�

��Ȼ���д�������ʱ����������Ҳ�Ͳ����˷ѣ���ȫ����������ʵ��һ�����õļ��ܡ�

��������ͨѶ���ܵ�ԭ����������ͬ��һ����Կ���й�ȥ��Ȼ���ô�ͳ�ŵ��������� xor ��Կ �����ַ�ʽֻҪ��֤����Կ�����ܱ���������������ľ�����ȫû�б�������ԭ�������ġ�

��������������ʱ�����������������һЩ�򵥵ļ��ܣ�����ʹ��Ԥ�����Կ������ô�����������غ��ϣ�ֻҪ xor ��ȥ���͵�������Ϳ������������ˡ��ڼ�������£�Ԥ�ȷ��͵�����������Ĺ⣬����Ҳ���ٿ��Ի�ù̶�������һ����������Ч���ݡ�

ֻҪ����������е��㷨���ã����� linux ������������豸�ͻ��ռ�����ϵ�y������������������Դ�Ļ����������ͻ���������� key �����������е��㷨������ rc4 �ȣ�Ҫǿ�Ķࡣ�������Խ�������Խ�����ܲ���ģʽ������������ֹ�����ߵ�ģʽ������

ͨѶЭ������Ҳ�ܼ򵥣���������� 1bit �ı�ʶ + 7bit ���� + ���ݿ�ķ�ʽ�����롣ֻ������ 1/255 �Ķ��⸺�ء�

��ʵ���ϣ��������̻߳�������������������飬һ���̸߳����Թ̶�Ƶ�ʷ�������������һ���̸߳��������Ч�����������ݸ������̼߳��ɡ�


ͨѶ�ŵ��ϣ��ҽ��������ͨ��������ͨ�����롣����Ҫ�������� TCP ���ӣ�һ��ֻ�������ݣ���һ��ֻ�������ݡ�

����������Ļ������԰����������߲�ͬ�� ISP ������ͬ������ͬ�Ļ����������ӣ��ڶԶ������������ٽ���ͨ���ϲ���һ��������


��л���������� dou4cc �ṩ������ϣ�һ������ض���վ������ҳָ��ʶ�𷽷� CN 105281973 A

July 18, 2017

skynet 1.1

���˺ö��죬���ھ������� skynet 1.1 �ˡ�

���ϴμƻ�������� ���������ǵ� bugfix ��������һЩ�Ƚϴ�ı䶯��

skynet �� lua ģ��ȫ�������� skynet ǰ׺���������ݿ� driver �ŵ��� skynet.db �¡������Ҫ���� 1.0 ��·���������� config ������ lualib/compat10 ���Ŀ¼��

�����߳�����д���д������Ӧ�����˺ܴ���Ż� ����һ�� ʵ�ʰ��� ������� 3.5 ����Ч�ʡ�

������һ������ DataSheet ����ģ�飬������Ϊ ShareData ��һ�����ѡ��

���ڲ��Ϸ�����ͬѧ��ʹ�� skynet ʱ�������Լ��� C ��չģ��ʵ�ֻ�ʹ�ò�����������崻�ȴ���������ε��ԡ�Ŀǰ�� skynet ���ڴ���� API Ԥ���˵��Խӿڡ�����ͨ���� make ʱ����Ԥ����� MEMORY_CHECK �򿪡�

���磬�� linux �£������� make linux SKYNET_DEFINES=-DMEMORY_CHECK �򿪡�

btw������һЩͬѧ�� C ��д lua ��չģ��û�о��飬���� lua ��ջ�����������ʹ����������Ƽ��ڱ��� lua ��ʱ����Ϻ궨�壺LUA_USE_APICHECK ��

July 06, 2017

Paradox �������ļ���ʽ

Paradox ���Һ�ϲ����һ����Ϸ��˾������ν P �� 5 ���У�ʮ�־�֮���͸������Ķ�ֻ��dz��������ά�����Ǻ�Ⱥ���Ͼ�Ͷ���˴���ʱ��;�����

��Щ��Ϸ����ͬһ�����棬���������ļ���ʽҲ�ǹ�ͨ�ġ�P �翪���� Mod ������������޸���Ϸ�����������ļ����������ı�������ļ�ϵͳ�У����������һ�����õ�ѧϰ���᣺������Ϸ��ҵ�ߣ��Һ�����Ȥ����������������ι�����Ϸ���ݺ���Ϸ�߼��ġ�

�������Ӵ����Ĺ�����Ϸ��˾�����������Լ���˾���ڣ���Ϸ���ݴ��ǻ��� excel ���ֶ�ά��������ġ��Ұ�����Ϊ csv ģʽ������ģʽ���ص��ǣ��������ݽṹ���������Ŷ�ά����ÿ�ű��в�ȷ������������ÿ���й̶����������������������ݽṹ��ȱ���Ǻ����Եģ����������ѱ�����״�㼶�ṹ����������������һ���м�㣬�淶һЩʹ�ø�ʽ��������ģ����������ݽṹ��

��һ����������ҵ�㷺ʹ�õĻ������ݽṹ�� json/xml ģʽ��json �� xml Ҫ�򵥡������ص���Ƕ��������ֻ����ĸ��Ͻṹ���ֵ�����飬�����ṹǶ�ס���������ģʽ������Ϸ���ݵ���Ҳ����һЩ���������ڲ߻���˵���༭���ṹ�������վ����� excel �������㡣�鿴����Ҳû���ر�õĿ��ӻ����ߣ����Ըо��õ���Ҫ��һЩ��

�ʼ������Ϊ P ��������ļ���ƫ���ں�һ�� json ģʽ����ʵ���о������־����кܴ�IJ�ͬ�������ҳ����� lpeg д��һ���򵥵� parser ��ͼ�������� lua vm ��д�� parser ��ͻȻ�����������ʵ�����ǻ��ڵ�Ƕ�� list �������� lisp ���������������������ඥ�ĸо�����ȷ lisp ģʽҪ�� json ģʽ���Ķ࣬������ csv ģʽ���ӡ�����������ȴǿ���������ߣ���ȷ��һ�����õ�������֯������

��������һ����Ⱥ�������ժ¼�����ӣ��е㳤����ͦ�д����ԣ���

country_event = {
    id = primitive.16
    hide_window = yes

    trigger = {
        is_country_type = primitive
        has_country_flag = early_space_age
        #NOT = { has_country_flag = recently_advanced }
        OR = {
            AND = {
                exists = from
                from = {
                    OR = {
                        is_country_type = default
                        is_country_type = awakened_fallen_empire
                    }
                }
            }
            years_passed > 25
        }
    }

    mean_time_to_happen = {
        years = 100

        modifier = {
            factor = 0.6
            has_country_flag = acquired_tech
        }
    }

    immediate = {
        remove_country_flag = early_space_age
        set_country_flag = primitives_can_into_space
        set_country_type = default
        change_country_flag = random
        if = {
            limit = { is_species_class = MAM }
            set_graphical_culture = mammalian_01
        }
        if = {
            limit = { is_species_class = REP }
            set_graphical_culture = reptilian_01
        }
        if = {
            limit = { is_species_class = AVI }
            set_graphical_culture = avian_01
        }
        if = {
            limit = { is_species_class = ART }
            set_graphical_culture = arthropoid_01
        }
        if = {
            limit = { is_species_class = MOL }
            set_graphical_culture = molluscoid_01
        }
        if = {
            limit = { is_species_class = FUN }
            set_graphical_culture = fungoid_01
        }
        change_government = {
            authority = random
            civics = random
        }
        set_name = random
        if = {
            limit = {
                home_planet = {
                    has_observation_outpost = yes
                }
            }
            home_planet = {
                observation_outpost_owner = {
                    country_event = { id = primitive.17 }
                }
            }
        }
        add_minerals = 1000 # enough for a spaceport and then some
        add_energy = 500
        add_influence = 300
        capital_scope = {
            every_tile = {
                limit = {
                    has_blocker = yes
                    NOR = {
                        has_blocker = tb_decrepit_dwellings
                        has_blocker = tb_failing_infrastructure
                    }
                }
                remove_blocker = yes
            }
            while = {
                limit = { 
                    num_pops < 8
                    any_tile = {
                        has_grown_pop = no
                        has_growing_pop = no
                        has_blocker = no
                    }
                }
                random_tile = {
                    limit = {
                        has_grown_pop = no
                        has_growing_pop = no
                        has_blocker = no
                    }
                    create_pop = {
                        species = owner
                    }
                }
            }
            random_tile = {
                limit = {
                    has_grown_pop = yes
                    OR = {
                        has_building = "building_primitive_farm"
                        has_building = "building_primitive_factory"
                        has_building = no
                    }
                }
                clear_deposits = yes
                add_deposit = d_mineral_food_deposit
                set_building = "building_capital_2"
            }
            random_tile = {
                limit = {
                    has_grown_pop = yes
                    OR = {
                        has_building = "building_primitive_farm"
                        has_building = "building_primitive_factory"
                        has_building = no
                    }
                }
                clear_deposits = yes
                add_deposit = d_mineral_deposit
                set_building = "building_mining_network_1"
            }
            random_tile = {
                limit = {
                    has_grown_pop = yes
                    OR = {
                        has_building = "building_primitive_farm"
                        has_building = "building_primitive_factory"
                        has_building = no
                    }
                }
                clear_deposits = yes
                add_deposit = d_mineral_deposit
                set_building = "building_mining_network_1"
            }
            random_tile = {
                limit = {
                    has_grown_pop = yes
                    OR = {
                        has_building = "building_primitive_farm"
                        has_building = "building_primitive_factory"
                        has_building = no
                    }
                }
                clear_deposits = yes
                add_deposit = d_farmland_deposit
                set_building = "building_hydroponics_farm_1"
            }
            random_tile = {
                limit = {
                    has_grown_pop = yes
                    OR = {
                        has_building = "building_primitive_farm"
                        has_building = "building_primitive_factory"
                        has_building = no
                    }
                }
                clear_deposits = yes
                add_deposit = d_farmland_deposit
                set_building = "building_hydroponics_farm_1"
            }
            random_tile = {
                limit = {
                    has_grown_pop = yes
                    OR = {
                        has_building = "building_primitive_farm"
                        has_building = "building_primitive_factory"
                        has_building = no
                    }
                }
                clear_deposits = yes
                add_deposit = d_energy_deposit
                set_building = "building_power_plant_1"
            }
            random_tile = {
                limit = {
                    has_grown_pop = yes
                    OR = {
                        has_building = "building_primitive_farm"
                        has_building = "building_primitive_factory"
                        has_building = no
                    }
                }
                clear_deposits = yes
                add_deposit = d_energy_deposit
                set_building = "building_power_plant_1"
            }
            random_tile = {
                limit = {
                    has_grown_pop = yes
                    OR = {
                        has_building = "building_primitive_farm"
                        has_building = "building_primitive_factory"
                        has_building = no
                    }
                }
                clear_deposits = yes
                add_deposit = d_energy_deposit
                set_building = "building_power_plant_1"
            }
            remove_all_armies = yes
            create_army = {
                name = random
                owner = PREV
                species = owner_main_species
                type = "defense_army"
            }
            create_army = {
                name = random
                owner = PREV
                species = owner_main_species
                type = "defense_army"
            }
            create_army = {
                name = random
                owner = PREV
                species = owner_main_species
                type = "defense_army"
            }
            create_army = {
                name = random
                owner = PREV
                species = owner_main_species
                type = "defense_army"
            }
        }
        random_owned_ship = {
            limit = { is_ship_size = primitive_space_station }
            fleet = { destroy_fleet = THIS }
        }
    }
}

������Һ��ɻ��������ʽ�У�Ϊɶ��ֵ����ȶ��õ� = ���ⲻ��������������ô��������� lisp �ĽǶ������ͼ��ˡ����ں�ֻ��Ϊ�˱��ڲ߻���д���Ķ���һ�����Ρ���ν id = primitive.16 ���������Ϊ ( id, primitive.16 ) �� iscountrytype = default һ����������Ϊ ( iscountrytype , default ) �� ��

create_army = {
                name = random
                owner = PREV
                species = owner_main_species
                type = "defense_army"
            }

�������� ( create_army , ( ( name, random ) , (owner, PREV), (species, owner_main_species), (type, "defense_army") ) )��

�������ݽṹֻҪ�ܱ����������ô������Щ list �Ǹ��ϲ�Ĺ�������ͺ������� csv ��ȥģ�����ṹ��һ���ĵ�����ֻ���� years_passed > 25 �����Ķ������������ ( years_passed, > , 25 ) ������Ԫ�ء��ϲ������ʱ�����ȷ������һ���߼�����ʽ���ͺ������� 2 ��Ԫ�ص� list �м����һ�� = ��ȫ��

���ֽṹ����������һЩ���ƽṹ���������������е� if ���һ������������з����� repeat while �ȿ��ƽṹ����Щ�����ϲ�Ĺ������͵ײ�����ģ���޹ء������ò�˵��lisp ģʽ�� csv ģʽ��������������ƽṹ��

���������ݽṹ����� lua Ҳ�����ף�ֻ��Ҫ�� lua table �� array �����漴�ɡ���Ϊ��ʹ�÷��㣬���Լ�һ�������ṹ������ϲ�ҵ�����һ�� list �������ֵ䣬���� cache ����ʱ����һ�� hash ���ӿ��ѯ���ɡ������������԰���ֱ�Ӵ��� C �ڴ��У�ֻ�� lua �б�¶�������Լ��߲�ķ��ʷ�������ν�߲�ķ��ʷ���ָ������ֱ�Ӷ�ȡ if repeat �ȿ��ƽṹ�����ǰѴ� AND OR �����ĸ��� list ֱ�ӷ����һ����������ʽ��


8 �� 8 �ղ��䣺

��ʵ����һ���򵥵� lua parser : https://github.com/cloudwu/pdxparser ��