# HG changeset patch # User JeanDo # Date 1305640569 -7200 # Node ID b75564fb3d4b9a156503a541010b5fe56ac350b9 # Parent 4ccdc72ec0e57c79d18b9534a9d1562c5598714e Optimizing access to B?hlmann coefficient (speed). Define number of compartiments, stops and gases. diff -r 4ccdc72ec0e5 -r b75564fb3d4b code_part1/OSTC_code_c_part2/p2_deco.c --- a/code_part1/OSTC_code_c_part2/p2_deco.c Thu May 12 13:59:13 2011 +0200 +++ b/code_part1/OSTC_code_c_part2/p2_deco.c Tue May 17 15:56:09 2011 +0200 @@ -77,6 +77,7 @@ // 2011/04/25: [jDG] Added 1mn mode for CNS calculation, to allow it for decoplanning. // 2011/04/27: [jDG] Fixed char_O_gradient_factor calculation when model uses gradient-factor. // 2011/05/02: [jDG] Added "Future TTS" function (CF58). +// 2011/05/17: [jDG] Various cleanups. // // TODO: // + Allow to abort MD2 calculation (have to restart next time). @@ -163,11 +164,11 @@ // Real context: what we are doing now. static float calc_lead_tissue_limit; // -static unsigned char internal_deco_time[32]; -static unsigned char internal_deco_depth[32]; +static unsigned char internal_deco_time[NUM_STOPS]; +static unsigned char internal_deco_depth[NUM_STOPS]; static float cns_vault; -static float pres_tissue_vault[32]; +static float pres_tissue_vault[2*NUM_COMP]; //---- Bank 5 parameters ----------------------------------------------------- #pragma udata bank5=0x500 @@ -188,8 +189,8 @@ static float var_N2_e; // Exposition, for current N2 tissue. static float var_He_e; // Exposition, for current He tissue. -static float pres_diluent; // new in v.101 -static float const_ppO2; // new in v.101 +static float pres_diluent; // new in v.101 +static float const_ppO2; // new in v.101 static unsigned char sim_gas_last_depth; // Depth of last used gas, to detected a gas switch. static unsigned char sim_gas_last_used; // Number of last used gas, to detected a gas switch. @@ -200,21 +201,21 @@ static float CNS_fraction; // new in v.101 static float float_saturation_multiplier; // new in v.101 static float float_desaturation_multiplier; // new in v.101 -static float float_deco_distance; // new in v.101 -static char flag_in_divemode; // new in v.108 +static float float_deco_distance; // new in v.101 +static char flag_in_divemode; // new in v.108 -static unsigned char deco_gas_change[5]; // new in v.109 +static unsigned char deco_gas_change[NUM_GAS]; // new in v.109 //---- Bank 6 parameters ----------------------------------------------------- #pragma udata bank6=0x600 -float pres_tissue[32]; +float pres_tissue[2*NUM_COMP]; //---- Bank 7 parameters ----------------------------------------------------- #pragma udata bank7=0x700 -float sim_pres_tissue[32]; // 32 floats = 128 bytes. -static float sim_pres_tissue_backup[32]; +float sim_pres_tissue[2*NUM_COMP]; // 32 floats = 128 bytes. +static float sim_pres_tissue_backup[2*NUM_COMP]; //---- Bank 8 parameters ----------------------------------------------------- #pragma udata bank8=0x800 @@ -328,7 +329,7 @@ int_O_DBS_bitfield |= DBS_mode; if(const_ppO2) int_O_DBS_bitfield |= DBS_ppO2; - for(i = 16; i < 32; i++) + for(i = NUM_COMP; i < 2*NUM_COMP; i++) if(pres_tissue[i]) int_O_DBS_bitfield |= DBS_HE_sat; if(float_saturation_multiplier < 0.99) @@ -417,7 +418,7 @@ temp_DBS |= DBG_C_SURF; if( !DBS_HE_sat && !He_ratio) - for(i = 16; i < 32; i++) + for(i = NUM_COMP; i < 2*NUM_COMP; i++) if(pres_tissue[i]) temp_DBS |= DBG_HEwoHE; @@ -549,9 +550,10 @@ // static void read_buhlmann_coefficients(void) { + #ifndef CROSS_COMPILE // Note: we don't use far rom pointer, because the - // 24 bits is to complex, hence we have to set + // 24 bits is too complex, hence we have to set // the UPPER page ourself... // --> Set zero if tables are moved to lower pages ! _asm @@ -560,11 +562,17 @@ _endasm #endif - assert( 0 <= ci && ci < 16 ); - var_N2_a = buhlmann_a[ci]; - var_N2_b = buhlmann_b[ci]; - var_He_a = (buhlmann_a+16)[ci]; - var_He_b = (buhlmann_b+16)[ci]; + assert( 0 <= ci && ci < NUM_COMP ); + + // Use an interleaved array (AoS) to access coefficients with a + // single addressing. + { + overlay rom const float* ptr = &buhlmann_ab[4*ci]; + var_N2_a = *ptr++; + var_N2_b = *ptr++; + var_He_a = *ptr++; + var_He_b = *ptr++; + } // Check reading consistency: if( (var_N2_a < 0.231) @@ -596,14 +604,18 @@ movwf TBLPTRU,0 _endasm #endif - assert( 0 <= ci && ci < 16 ); + + assert( 0 <= ci && ci < NUM_COMP ); // Integration intervals. switch(period) { case 0: //---- 2 sec ----------------------------------------------------- - var_N2_e = e2secs[ci]; - var_He_e = (e2secs+16)[ci]; + { + overlay rom const float* ptr = &e2secs[2*ci]; + var_N2_e = *ptr++; + var_He_e = *ptr++; + } // Check reading consistency: if( (var_N2_e < 0.0000363) @@ -616,8 +628,11 @@ break; case 1: //---- 1 min ----------------------------------------------------- - var_N2_e = e1min[ci]; - var_He_e = (e1min+16)[ci]; + { + overlay rom const float* ptr = &e1min[2*ci]; + var_N2_e = *ptr++; + var_He_e = *ptr++; + } // Check reading consistency: if( (var_N2_e < 1.09E-3) @@ -630,8 +645,11 @@ break; case 2: //---- 10 min ---------------------------------------------------- - var_N2_e = e10min[ci]; - var_He_e = (e10min+16)[ci]; + { + overlay rom const float* ptr = &e10min[2*ci]; + var_N2_e = *ptr++; + var_He_e = *ptr++; + } // Check reading consistency: if( (var_N2_e < 0.01085) @@ -837,7 +855,7 @@ if( internal_deco_depth[x] != 0 ) break; //---- Second: copy to output table (in reverse order) - for(y=0; y<32; y++, --x) + for(y=0; y depth ) { - for(j=0; j<5; ++j) + for(j=0; j deco_gas_change[j] ) @@ -1092,7 +1110,7 @@ // static void gas_switch_set(void) { - assert( 0 <= sim_gas_last_used && sim_gas_last_used <= 5 ); + assert( 0 <= sim_gas_last_used && sim_gas_last_used <= NUM_GAS ); if( sim_gas_last_used == 0 ) { @@ -1177,15 +1195,15 @@ N2_ratio = 0.7902; pres_respiration = int_I_pres_respiration * 0.001; - for(ci=0; ci<16; ci++) + for(ci=0; ci pres_surface && char_O_deco_status == 0) @@ -1616,7 +1634,7 @@ assert( 0.00 <= ppN2 && ppN2 < 11.2 ); // 80% N2 at 130m assert( 0.00 <= ppHe && ppHe < 12.6 ); // 90% He at 130m - for (ci=0;ci<16;ci++) + for (ci=0;ci 0 ); // No stop at surface... - for(x=0; x<32; ++x) + for(x=0; x respiration (currently off-gasing) @@ -2018,6 +2036,8 @@ // void deco_calc_desaturation_time(void) { + overlay rom const float *ptr; + RESET_C_STACK assert( 800 < int_I_pres_surface && int_I_pres_surface < 1100 ); @@ -2040,14 +2060,20 @@ int_O_desaturation_time = 0; float_desaturation_multiplier = char_I_desaturation_multiplier / 142.0; // new in v.101 (70,42%/100.=142) - for (ci=0;ci<16;ci++) + ptr = &buhlmann_ht[0]; + for (ci=0;ci= 0.0) { temp3 = 0.0; temp4 = 0.0; } else - temp3 = - temp3 / (pres_tissue+16)[ci]; + temp3 = - temp3 / (pres_tissue+NUM_COMP)[ci]; if( 0.0 < temp3 && temp3 < 1.0 ) { - overlay float var_He_halftime = (buhlmann_ht+16)[ci]; - assert( 1.5099 <= var_He_halftime && var_He_halftime <= 240.03 ); - temp3 = log(1.0 - temp3) / -0.6931; // temp1 is the multiples of half times necessary. // 0.6931 is ln(2), because the math function log() calculates with a base of e not 2 as requested. // minus because log is negative @@ -2129,7 +2149,7 @@ temp4 = 0.0; if (temp4 > 255.0) temp4 = 255.0; - (char_O_tissue_saturation+16)[ci] = (char)temp4; + (char_O_tissue_saturation+NUM_COMP)[ci] = (char)temp4; } // for } @@ -2161,7 +2181,7 @@ float_desaturation_multiplier = char_I_desaturation_multiplier / 142.0; // new in v.101 (70,42%/100.=142) float_saturation_multiplier = char_I_saturation_multiplier * 0.01; - calc_tissue(1); // update the pressure in the 32 tissues in accordance with the new ambient pressure + calc_tissue(1); // update the pressure in the 2*NUM_COMP tissues in accordance with the new ambient pressure clear_deco_table(); char_O_deco_status = 3; // surface new in v.102 : stays in surface state. @@ -2406,17 +2426,17 @@ // void deco_gas_volumes(void) { - overlay float volumes[5]; + overlay float volumes[NUM_GAS]; overlay float bottom_usage, ascent_usage; overlay unsigned char i, deepest_first; overlay unsigned char gas; RESET_C_STACK //---- initialize with bottom consumption -------------------------------- - for(i=0; i<5; ++i) // Nothing yet... + for(i=0; i char_I_deco_gas_change[j]) ) @@ -2494,7 +2514,7 @@ } //---- convert results for the ASM interface ----------------------------- - for(i=0; i<5; ++i) + for(i=0; i 6553.4 ) int_O_gas_volumes[i] = 65535; else @@ -2509,7 +2529,7 @@ RESET_C_STACK cns_vault = CNS_fraction; - for (x=0;x<32;x++) + for (x=0;x<2*NUM_COMP;x++) pres_tissue_vault[x] = pres_tissue[x]; } @@ -2518,7 +2538,7 @@ overlay unsigned char x; RESET_C_STACK - for (x=0;x<32;x++) + for (x=0;x<2*NUM_COMP;x++) pres_tissue[x] = pres_tissue_vault[x]; // Restore both CNS variable, too. diff -r 4ccdc72ec0e5 -r b75564fb3d4b code_part1/OSTC_code_c_part2/p2_deco.o Binary file code_part1/OSTC_code_c_part2/p2_deco.o has changed diff -r 4ccdc72ec0e5 -r b75564fb3d4b code_part1/OSTC_code_c_part2/p2_tables.romdata --- a/code_part1/OSTC_code_c_part2/p2_tables.romdata Thu May 12 13:59:13 2011 +0200 +++ b/code_part1/OSTC_code_c_part2/p2_tables.romdata Tue May 17 15:56:09 2011 +0200 @@ -18,236 +18,117 @@ // HISTORY // 2011-01-20: jDG Cleanup addressing. // 2011-02-13: jDG Correct some typos. +// 2011-05-17: jDG Optimized using interleaved arrays. // // ************************************************************** -rom const float buhlmann_a[32] = -{ // Data ZH-L16C, from Bühlmann Tauchmedizin 2002, option 1a (4mn) -//---a for N2 ---------------------------------------------------------------- - 1.2599, - 1.0000, - 0.8618, - 0.7562, - 0.6200, - 0.5043, - 0.4410, - 0.4000, - 0.3750, - 0.3500, - 0.3295, - 0.3065, - 0.2835, - 0.2610, - 0.2480, - 0.2327, -//---- a of He --------------------------------------------------------------- - 1.7424, - 1.3830, - 1.1919, - 1.0458, - .9220, - .8205, - .7305, - .6502, - .5950, - .5545, - .5333, - .5189, - .5181, - .5176, - .5172, - .5119 +rom const float buhlmann_ab[] = { +// Data ZH-L16C, from Bühlmann Tauchmedizin 2002, option 1a (4mn) +// a for N2 b for N2 a of He b for He + 1.2599, 0.5050, 1.7424, 0.4245, + 1.0000, 0.6514, 1.3830, 0.5747, + 0.8618, 0.7222, 1.1919, 0.6527, + 0.7562, 0.7825, 1.0458, 0.7223, + 0.6200, 0.8126, 0.9220, 0.7582, + 0.5043, 0.8434, 0.8205, 0.7957, + 0.4410, 0.8693, 0.7305, 0.8279, + 0.4000, 0.8910, 0.6502, 0.8553, + 0.3750, 0.9092, 0.5950, 0.8757, + 0.3500, 0.9222, 0.5545, 0.8903, + 0.3295, 0.9319, 0.5333, 0.8997, + 0.3065, 0.9403, 0.5189, 0.9073, + 0.2835, 0.9477, 0.5181, 0.9122, + 0.2610, 0.9544, 0.5176, 0.9171, + 0.2480, 0.9602, 0.5172, 0.9217, + 0.2327, 0.9653, 0.5119, 0.9267 }; -rom const float buhlmann_b[] = -{ -//---- b for N2 -------------------------------------------------------------- - 0.5050, - 0.6514, - 0.7222, - 0.7825, - 0.8126, - 0.8434, - 0.8693, - 0.8910, - 0.9092, - 0.9222, - 0.9319, - 0.9403, - 0.9477, - 0.9544, - 0.9602, - 0.9653, -//---- b for He -------------------------------------------------------------- - 0.4245, - 0.5747, - 0.6527, - 0.7223, - 0.7582, - 0.7957, - 0.8279, - 0.8553, - 0.8757, - 0.8903, - 0.8997, - 0.9073, - 0.9122, - 0.9171, - 0.9217, - 0.9267 -}; - -rom const float buhlmann_ht[] = -{ -//---- N2 -------------------------------------------------------------------- - 4, // Compartiment half-life, in minute, for nitrogen. - 8, - 12, - 18, - 27, - 39, - 55, - 77, - 109, - 146, - 187, - 239, - 305, - 390, - 498, - 635, -//---- He -------------------------------------------------------------------- - 1.51, // Compartiment half-life, in minute, for helium. - 3.02, - 4.72, - 6.99, - 10.21, - 14.48, - 20.53, - 29.11, - 41.20, - 55.19, - 70.69, - 90.34, - 115.29, - 147.42, - 188.24, - 240.03 +rom const float buhlmann_ht[] = { +// Compartiment half-life, in minute +//-- N2 ---- He --------------------------------------------------------------------- + 4, 1.51, + 8, 3.02, + 12, 4.72, + 18, 6.99, + 27, 10.21, + 39, 14.48, + 55, 20.53, + 77, 29.11, + 109, 41.20, + 146, 55.19, + 187, 70.69, + 239, 90.34, + 305, 115.29, + 390, 147.42, + 498, 188.24, + 635, 240.03 }; -rom const float e2secs[] = -{ -//---- N2 -------------------------------------------------------------------- - 5.75958E-03, // result of 1 - 2^(-1/(30sec*HT)) - 2.88395E-03, - 1.92356E-03, - 1.28278E-03, - 8.55371E-04, - 5.92258E-04, - 4.20001E-04, - 3.00019E-04, - 2.11949E-04, - 1.58240E-04, - 1.23548E-04, - 9.66686E-05, - 7.57509E-05, - 5.92416E-05, - 4.63943E-05, - 3.63850E-05, -//---- He -------------------------------------------------------------------- - 1.51848E-02, - 7.62144E-03, - 4.88315E-03, - 3.29997E-03, - 2.26041E-03, - 1.59437E-03, - 1.12479E-03, - 7.93395E-04, - 5.60641E-04, - 4.18555E-04, - 3.26795E-04, - 2.55722E-04, - 2.00387E-04, - 1.56716E-04, - 1.22734E-04, - 9.62538E-05 +rom const float e2secs[] = { +// result of 1 - 2^(-1/(30sec*HT)) +//---- N2 ------------- He ------------ + 5.75958E-03, 1.51848E-02, + 2.88395E-03, 7.62144E-03, + 1.92356E-03, 4.88315E-03, + 1.28278E-03, 3.29997E-03, + 8.55371E-04, 2.26041E-03, + 5.92258E-04, 1.59437E-03, + 4.20001E-04, 1.12479E-03, + 3.00019E-04, 7.93395E-04, + 2.11949E-04, 5.60641E-04, + 1.58240E-04, 4.18555E-04, + 1.23548E-04, 3.26795E-04, + 9.66686E-05, 2.55722E-04, + 7.57509E-05, 2.00387E-04, + 5.92416E-05, 1.56716E-04, + 4.63943E-05, 1.22734E-04, + 3.63850E-05, 9.62538E-05 +//------------------------------------- }; -rom const float e1min[] = -{ -//---- N2 -------------------------------------------------------------------- - 1.59104E-01, // Integration constant for 1 minute, - 8.29960E-02, // Ie. 1- 2^(-1/HT) - 5.61257E-02, - 3.77762E-02, - 2.53454E-02, - 1.76160E-02, - 1.25236E-02, - 8.96152E-03, - 6.33897E-03, - 4.73633E-03, - 3.69981E-03, - 2.89600E-03, - 2.27003E-03, - 1.77572E-03, - 1.39089E-03, - 1.09097E-03, -//---- e 1min He ------------------------------------------------------------- - 3.68109E-01, - 2.05084E-01, - 1.36579E-01, - 9.44046E-02, - 6.56359E-02, - 4.67416E-02, - 3.31991E-02, - 2.35301E-02, - 1.66832E-02, - 1.24808E-02, - 9.75753E-03, - 7.64329E-03, - 5.99417E-03, - 4.69082E-03, - 3.67548E-03, - 2.88359E-03 +rom const float e1min[] = { +// Integration constant for 1 minute, +// Ie. 1- 2^(-1/HT) +//----- N2 --------- e 1min He -------- + 1.59104E-01, 3.68109E-01, + 8.29960E-02, 2.05084E-01, + 5.61257E-02, 1.36579E-01, + 3.77762E-02, 9.44046E-02, + 2.53454E-02, 6.56359E-02, + 1.76160E-02, 4.67416E-02, + 1.25236E-02, 3.31991E-02, + 8.96152E-03, 2.35301E-02, + 6.33897E-03, 1.66832E-02, + 4.73633E-03, 1.24808E-02, + 3.69981E-03, 9.75753E-03, + 2.89600E-03, 7.64329E-03, + 2.27003E-03, 5.99417E-03, + 1.77572E-03, 4.69082E-03, + 1.39089E-03, 3.67548E-03, + 1.09097E-03, 2.88359E-03 +//------------------------------------- }; -rom const float e10min[] = -{ -//---- N2 -------------------------------------------------------------------- +rom const float e10min[] = { // The 10 min Value in float notation: // result of 1 - 2^(-10/ht) - 8.23223E-01, // 1 - 2^(-10/4.0) - 5.79552E-01, - 4.38769E-01, - 3.19605E-01, - 2.26416E-01, - 1.62832E-01, - 1.18409E-01, - 8.60863E-02, - 6.16117E-02, - 4.63665E-02, - 3.63881E-02, - 2.85855E-02, - 2.24698E-02, - 1.76160E-02, - 1.38222E-02, - 1.08563E-02, -//---- He -------------------------------------------------------------------- - 9.89851E-01, - 8.99258E-01, - 7.69737E-01, - 6.29027E-01, - 4.92821E-01, - 3.80407E-01, - 2.86538E-01, - 2.11886E-01, - 1.54849E-01, - 1.18026E-01, - 9.34005E-02, - 7.38569E-02, - 5.83504E-02, - 4.59303E-02, - 3.61528E-02, - 2.84646E-02 +//---- N2 -------------- He ----------- + 8.23223E-01, 9.89851E-01, + 5.79552E-01, 8.99258E-01, + 4.38769E-01, 7.69737E-01, + 3.19605E-01, 6.29027E-01, + 2.26416E-01, 4.92821E-01, + 1.62832E-01, 3.80407E-01, + 1.18409E-01, 2.86538E-01, + 8.60863E-02, 2.11886E-01, + 6.16117E-02, 1.54849E-01, + 4.63665E-02, 1.18026E-01, + 3.63881E-02, 9.34005E-02, + 2.85855E-02, 7.38569E-02, + 2.24698E-02, 5.83504E-02, + 1.76160E-02, 4.59303E-02, + 1.38222E-02, 3.61528E-02, + 1.08563E-02, 2.84646E-02 +//------------------------------------- }; diff -r 4ccdc72ec0e5 -r b75564fb3d4b code_part1/OSTC_code_c_part2/shared_definitions.h --- a/code_part1/OSTC_code_c_part2/shared_definitions.h Thu May 12 13:59:13 2011 +0200 +++ b/code_part1/OSTC_code_c_part2/shared_definitions.h Tue May 17 15:56:09 2011 +0200 @@ -74,6 +74,21 @@ bank2 udata_ovr 0x200 #endif +#ifdef xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + Define model dimensions. + NUM_COMP is the number of compartiments in the Bühlmann ZH-L16 model, ie 16. + NUM_STOPS is the maximum number of stops computed by decoplanning. + Note that the deapest stop is roughly limited to 3m * NUM_STOPS + (this is assuming all stops up to the surface are used). + Note also that if the table overflow, extra stops are ignored, + and not reported in TTS summing. + NUM_GAS is the number of (potentially) active gas considered during + ascent simulation. +#endif +#define NUM_COMP 0x10 +#define NUM_STOPS 0x20 +#define NUM_GAS 5 + VAR_UINT (int_O_gtissue_limit); VAR_UINT (int_O_gtissue_press); VAR_UINT (int_O_desaturation_time); // @@ -94,10 +109,10 @@ VAR_UCHAR (char_O_first_deco_depth); // Depth of first stop. VAR_UCHAR (char_O_first_deco_time) ; // Duration of first stop. -TAB_UCHAR (char_O_deco_depth, 0x20); // Fusionned decompression table: -TAB_UCHAR (char_O_deco_time, 0x20); // Both ZH-L16 and L16-GF models. +TAB_UCHAR (char_O_deco_depth, NUM_STOPS); // Fusionned decompression table: +TAB_UCHAR (char_O_deco_time, NUM_STOPS); // Both ZH-L16 and L16-GF models. -TAB_UCHAR (char_O_tissue_saturation, 0x20); // Compartiment desaturation time, in min. +TAB_UCHAR (char_O_tissue_saturation, 2*NUM_COMP); // Compartiment desaturation time, in min. VAR_UINT (int_O_DBS_bitfield); // NOTE: 9 bytes dumped to divelog by store_dive_decodebug VAR_UINT (int_O_DBS2_bitfield); @@ -140,9 +155,9 @@ VAR_UCHAR (char_I_bottom_depth); // Bottom depth for planning (used in gas volume evaluation). VAR_UCHAR (char_I_bottom_time); // Bottom time for planning (used in gas volume evaluation). -TAB_UCHAR (char_I_deco_gas_change, 5); // new in v.101 -TAB_UCHAR (char_I_deco_N2_ratio, 5); // new in v.101 -TAB_UCHAR (char_I_deco_He_ratio, 5); // new in v.101 +TAB_UCHAR (char_I_deco_gas_change,NUM_GAS);// new in v.101 +TAB_UCHAR (char_I_deco_N2_ratio, NUM_GAS); // new in v.101 +TAB_UCHAR (char_I_deco_He_ratio, NUM_GAS); // new in v.101 #ifdef __18CXX //----------------------------------------------------------------------------