📄 primitives.h
字号:
// Primitives.cc New primitives not in primitives.cxx// W. Langdon cs.ucl.ac.uk 25 January 1995// $Revision: 1.29 $//Modifications (in reverse order)//WBL 4 Apr 97 Make compatible with DEC UNIX Alpha C Compiler// replace macro inner_loop_value by extern// comment out get_address//WBL 5 May 96 Add get_address, blockset, R0..R9//WBL 17 Mar 96 Add IfltEval//WBL 4 Mar 96 Add ForSumEval and ForMinSumEval//WBL 26 Oct 95 Add intEval, setEval. Ensure works ok with float retval//WBL 28 Sep 95 Add NegoneEval//WBL 2 Sep 95 Support VAR_TIMINGS in caches//WBL 12 May 95 Add Memory_errors//WBL 2 May 95 Protect calls from divide by zero//WBL 18 Apr 95 Add retval_cache from Primitives.cc//WBL 18 Apr 95 Add swapEval//WBL 8 Apr 95 Change sematics of arg2_address, remove code_address//WBL 6 Apr 95 add ARG1, ARG2, ARG1Eval and ARG2Eval// add 3rd and 4th arguments to calleval//WBL 31 Mar 95 add ForWhileEval, calleval//WBL 11 Mar 95 add tree_contains//WBL 18 Feb 95 add nested_tree_or_i0//WBL 25 Jan 95 New file#ifdef VAR_TIMINGS#include "time.h"#endifextern int iterate_count; //May used by caches//**************************************************************************/////////////////// Problem specific functions and terminals// Use the OPDEF,EVAL,IP and CHROMEP defines// and the code will recompile for different eval methodsOPDEF(Prog2Eval);OPDEF(Prog3Eval);OPDEF(Qrog2Eval);OPDEF(Qrog3Eval);OPDEF(zeroEval);OPDEF(oneEval);OPDEF(NegoneEval);OPDEF(twoEval);OPDEF(MaxEval);const scoretype RandomValue[10] = {-0.474888,-0.425068,0.0317671,0.903066,-0.291892,0.101003,-0.63852,0.840462,-0.530013,0.999284};const char RandomLable0[] = "-0.474888";const char RandomLable1[] = "-0.425068";const char RandomLable2[] = "0.0317671";const char RandomLable3[] = "0.903066";const char RandomLable4[] = "-0.291892";const char RandomLable5[] = "0.101003";const char RandomLable6[] = "-0.63852";const char RandomLable7[] = "0.840462";const char RandomLable8[] = "-0.530013";const char RandomLable9[] = "0.999284";OPDEF(R0Eval);OPDEF(R1Eval);OPDEF(R2Eval);OPDEF(R3Eval);OPDEF(R4Eval);OPDEF(R5Eval);OPDEF(R6Eval);OPDEF(R7Eval);OPDEF(R8Eval);OPDEF(R9Eval);OPDEF(modEval);OPDEF(intEval);OPDEF(Arg1Eval);OPDEF(Arg2Eval);OPDEF(ARG1Eval);OPDEF(ARG2Eval);OPDEF(I0Eval);OPDEF(I1Eval);OPDEF(I2Eval);OPDEF(I3Eval);OPDEF(I4Eval);OPDEF(IfeqEval);OPDEF(IfltEval);OPDEF(ForEval);OPDEF(DownEval);OPDEF(WhileEval);OPDEF(ForWhileEval);OPDEF(ForMinSumEval);OPDEF(ForSumEval);BOOL nested_tree_or_i0(Chrome* chrome, int tree);BOOL i0_not_in_loop (Chrome* chrome, int tree);BOOL tree_contains (Chrome* chrome, int tree, int funcnum);OPDEF(BotEval);OPDEF(TopEval);OPDEF(Aux1Eval);OPDEF(Aux2Eval);OPDEF(Aux3Eval);OPDEF(inc_Aux1Eval);OPDEF(Minc_Aux1Eval);OPDEF(dec_Aux1Eval);OPDEF(inc_Aux2Eval);OPDEF(Minc_Aux2Eval);OPDEF(dec_Aux2Eval);OPDEF(inc_Aux3Eval);OPDEF(Minc_Aux3Eval);OPDEF(dec_Aux3Eval);//int get_address();retval readdata(int addr);OPDEF(readEval);OPDEF(writeEval);OPDEF(setEval);OPDEF(swapEval);void blockset(const int address, const int size, const retval data[]);OPDEF(set_Aux1Eval);OPDEF(set_Aux2Eval);OPDEF(set_Aux3Eval);#ifdef ADF#ifdef ADF_CACHEclass retval_cache{ private: retval* answerptr;#ifdef VAR_TIMINGS run_time::timetyp* timerptr; int* loopsptr;#endif int cache_size;// = 0; enum{magic = -50505050};//statistics int calls; int hits; int magic_hits; retval min; retval max; public: int cache_used;// = 0; retval_cache( int size ) : cache_size(0), cache_used(0) { //cout<<"retval_cache"<<endl;//debug answerptr = new retval [ size ]; #ifdef VAR_TIMINGS timerptr = new run_time::timetyp[ size ]; loopsptr = new int[ size ];#endif cache_size = size; stats_init(); }; ~retval_cache() { //cout<<"deleting retval_cache"<<endl;//debug delete[] answerptr; } void init() { //cout<<"init cache"<<endl;//debug cache_used = 0; for (int i = 0; i < cache_size; i++) answerptr[i] = magic; };#ifdef VAR_TIMINGS BOOL held( retval input, retval* output, run_time* timmer )#else BOOL held( retval input, retval* output )#endif { calls++; if ( input >= (0-cache_size/2) && input < cache_size/2 ) { if ( answerptr[input+cache_size/2] == magic ) {return FALSE;};#ifdef VAR_TIMINGS if ( loopsptr [input+cache_size/2] + iterate_count >= max_iterates ) return FALSE;//force code execution#endif hits++;//cout<<"heldFound "<<input<<". Returning ";//cout<<answerptr[input+cache_size/2]<<flush; *output = answerptr[input+cache_size/2];//cout<<" returned value="<<*output<<endl;#ifdef VAR_TIMINGS iterate_count += loopsptr[input+cache_size/2]; timmer->add_time(timerptr[input+cache_size/2]);#endif return TRUE; }//cout<<"held "<<input<<" outside cache "<<endl; return FALSE; };#ifdef VAR_TIMINGS void add( retval input, retval answer, run_time::timetyp delta, int num_loops )#else void add( retval input, retval answer )#endif { if (input < min) min = input; if (input > max) max = input;//cout<<"Add "<<input<<","<<answer<<flush; if ( input >= (0-cache_size/2) && input < cache_size/2 ) { if (answer==magic) { magic_hits++;} else { answerptr[input+cache_size/2] = answer;#ifdef VAR_TIMINGS timerptr[input+cache_size/2] = delta; loopsptr[input+cache_size/2] = num_loops;#endif //VAR_TIMINGS//cout<<". Slots in use "<<cache_used<<flush; cache_used++; } }//cout<<endl;//debug }; void stats_init(); void write( ostream& fout = cout ) { fout << "Cache used "<< calls << " times, hits " << hits; if(calls != 0) { fout << " (" << float(hits)/float(calls) << ")."; fout << " Least input " << min; fout << " highest input " << max; fout << " magic number " << magic; fout << " hit " << magic_hits; }; fout << endl; };};//end retval_cache#endif //ADF_CACHE#ifdef ADF_CACHEretval calleval (int numarg, int tree, BOOL update_arg1 = FALSE, int arg2_address = -1, retval_cache* cache = NULL );#else /*ADF_CACHE*/#ifdef BYREFretval calleval (int numarg, int tree, BOOL update_arg1 = FALSE, int arg2_address = -1// ,evalnode* code_address = NULL );#else //BYREFretval calleval (int numarg, int tree );#endif //BYREF#endif //ADF_CACHE#endif //ADFOPDEF(printEval);OPDEF(randEval); //assumes retval=float//////////////////////////////////////////////////////////////////////#ifdef ADFextern int calls [ NUM_TREES]; //used to prevent recursive calls#define recurse_error 1#endif //ADF#define memory_error 2#define loop_too_nested_error 4#define loop_too_long_error 8#define print_overrun_error 16#ifdef GRID_LIB#define prim_index_error 32#endif#define stop_on_error 1024extern int Primitive_error;extern int Memory_errors;#ifdef ORIGINAL_LOOPextern retval inner_loop_value;#elseextern int loop_depth;extern retval inner_loop_value;inline void clear_loop_value() {loop_depth=0; inner_loop_value=0;#if(loop_max_depth>1) memset(loop_value,0,sizeof(loop_value));}extern retval loop_value[loop_max_depth+2];#define inner_loop_value loop_value[0]#else}#endif#endif /*ORIGINAL_LOOP*/extern retval Arg1, Arg2; extern retval ARG1, ARG2; class indexed_memory {retval Aux1, Aux2, Aux3;retval store [store_limit];#define store_written 1#define store_read_unassigned 2int store_used [store_limit]; //bit masks#ifdef BYREFenum {Vnone = (-4)};enum {Vaux3 = (-3)};enum {Vaux2 = (-2)};enum {Vaux1 = (-1)};//else store[x]int last_var_used;//=Vnonepublic: indexed_memory(): last_var_used(Vnone) {};#else //BYREFpublic: indexed_memory() {};#endif //BYREFvoid load_active();void save_active();void clear(int& seed = 0);void clear_stats();int cells_used();int cells_used_spanned();int cells_written();int cells_written_spanned();int cells_read_before_write();void write(ostream& out = cout);};void clear(int& seed = 0);void clear_stats();int cells_used();int cells_used_spanned();int cells_written();int cells_written_spanned();int cells_read_before_write();void write(ostream& out = cout);extern int print_buffer_position;inline void print_clear() {print_buffer_position = 0;}void print_write(ostream& out = cout);BOOL print_equals(int length, retval* other);float print_match (int length, retval* other, BOOL& exact);extern int randEval_seed;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -