evolve4src/src/evolve_simulator/kforth_mutate.cpp
static KFORTH_PROGRAM *kforth_merge_with_mask(int mask, KFORTH_PROGRAM *kfp1, KFORTH_PROGRAM *kfp2)
{
	KFORTH_PROGRAM *p, *kfp;
	int cb, pc, len;
	int bit, curmask;

	ASSERT( mask >= 0 && mask <= 0xFFFF );
	ASSERT( kfp1 != NULL );
	ASSERT( kfp2 != NULL );
	ASSERT( kfp1->kfops == kfp2->kfops );

	kfp = (KFORTH_PROGRAM*) CALLOC(1, sizeof(KFORTH_PROGRAM));

	kfp->nblocks = (kfp1->nblocks > kfp2->nblocks) ?
				kfp1->nblocks : kfp2->nblocks;

	kfp->block_len = (int*) CALLOC(kfp->nblocks, sizeof(int));
	kfp->opcode = (unsigned char**) CALLOC(kfp->nblocks, sizeof(unsigned char*));
	kfp->operand = (KFORTH_INTEGER**) CALLOC(kfp->nblocks, sizeof(KFORTH_INTEGER));
	kfp->kfops = kfp1->kfops;

	bit = 0;
	curmask = mask;
	for(cb=0; cb < kfp->nblocks; cb++) {
		
		if( (curmask & 0x0001) == 0 ) {
			if( cb < kfp1->nblocks )
				p = kfp1;
			else
				p = kfp2;
		} else {
			if( cb < kfp2->nblocks )
				p = kfp2;
			else
				p = kfp1;
		}

		len = p->block_len[cb];
		kfp->block_len[cb] = len;
		kfp->opcode[cb] = (unsigned char*) CALLOC(len, sizeof(unsigned char));
		kfp->operand[cb] = (KFORTH_INTEGER*) CALLOC(len, sizeof(KFORTH_INTEGER));

		for(pc=0; pc < len; pc++) {
			kfp->opcode[cb][pc] = p->opcode[cb][pc];
			kfp->operand[cb][pc] = p->operand[cb][pc];
		}

		curmask = curmask >> 1;
		bit += 1;
		if( bit >= 16 ) {
			curmask = mask;
		}
	}

	return kfp;
}
/***********************************************************************
 *
 * Merge two kforth program. Use a 'mask' parameter to decide
 * which program (kfp1 or kfp2) donates a code block.
 *
 * The only the 16 lowest order bits in 'mask' are used.
 *
 * Bit 0 determines the first code block
 * bit 1 determine the second code block,
 * etc..
 *
 * If the 16-bits are exhausted, the mask is repeated.
 *
 * A bit value of 0, means that the code block from kfp1 is donated.
 * A bit value of 1, means that the code block from kfp2 is donated.
 *
 *
 * Example,
 *
 * If mask is 0000 0000 1011 0001
 *
 *	PROGRAM 1	PROGRAM 2	RESULT
 *	---------	---------	----------
 *	ABBA		BLAG		BLAG
 *	JUNK		CRAP		JUNK
 *	FOOBAR		GREPLINUX	FOOBAR
 *	STUFF		XXYYZZ		STUFF
 *	SUPERDUPER	FOOFOOFOO	FOOFOOFOO
 *	FOO12h		FOO666		FOO666
 *			HHIIGG		HHIIGG		<- donated from kfp2
 *			HHUUPP		HHUUPP
 *			JJJJJJ		JJJJJJ
 *
 * Create a new program from 'kfp1' and 'kfp2'. 
 * Even numbered code blocks are donated by PROGRAM 1.
 * Odd numbered code blocks are donated by PROGRAM 2.
 *
 * If programs have different number of code blocks, then
 * extra code blocks from the longer program are appended
 * to the new program.
 *
 * NOTE: kforth_merge(m, x, x) is allowed, and will return a copy of 'x'
 *
 */
Merge two kforth program. Use a 'mask' parameter to decide
which program (kfp1 or kfp2) donates a code block.

The only the 16 lowest order bits in 'mask' are used.

Bit 0 determines the first code block
bit 1 determine the second code block,
etc

If the 16-bits are exhausted, the mask is repeated.

A bit value of 0, means that the code block from kfp1 is donated.
A bit value of 1, means that the code block from kfp2 is donated.

Example.





Create a new program from 'kfp1' and 'kfp2'.
Even numbered code blocks are donated by PROGRAM 1.
Odd numbered code blocks are donated by PROGRAM 2.

If programs have different number of code blocks, then
extra code blocks from the longer program are appended
to the new program.

kforth_merge(m, x, x) is allowed, and will return a copy of 'x'
Merge two kforth program . Use a ' mask ' parameter to decide which program ( kfp1 or kfp2 ) donates a code block . The only the 16 lowest order bits in ' mask ' are used . Bit 0 determines the first code block bit 1 determine the second code block etc If the 16 - bits are exhausted the mask is repeated . A bit value of 0 means that the code block from kfp1 is donated . A bit value of 1 means that the code block from kfp2 is donated . Example . Create a new program from ' kfp1 ' and ' kfp2 ' . Even numbered code blocks are donated by PROGRAM 1 . Odd numbered code blocks are donated by PROGRAM 2 . If programs have different number of code blocks then extra code blocks from the longer program are appended to the new program . kforth_merge ( m x x ) is allowed and will return a copy of ' x '
static KFORTH_PROGRAM *kforth_merge_with_mask(int mask, KFORTH_PROGRAM *kfp1, KFORTH_PROGRAM *kfp2)
{
	KFORTH_PROGRAM *p, *kfp;
	int cb, pc, len;
	int bit, curmask;
	ASSERT( mask >= 0 && mask <= 0xFFFF );
	ASSERT( kfp1 != NULL );
	ASSERT( kfp2 != NULL );
	ASSERT( kfp1->kfops == kfp2->kfops );
	kfp = (KFORTH_PROGRAM*) CALLOC(1, sizeof(KFORTH_PROGRAM));
	kfp->nblocks = (kfp1->nblocks > kfp2->nblocks) ?
				kfp1->nblocks : kfp2->nblocks;
	kfp->block_len = (int*) CALLOC(kfp->nblocks, sizeof(int));
	kfp->opcode = (unsigned char**) CALLOC(kfp->nblocks, sizeof(unsigned char*));
	kfp->operand = (KFORTH_INTEGER**) CALLOC(kfp->nblocks, sizeof(KFORTH_INTEGER));
	kfp->kfops = kfp1->kfops;
	bit = 0;
	curmask = mask;
	for(cb=0; cb < kfp->nblocks; cb++) {
		if( (curmask & 0x0001) == 0 ) {
			if( cb < kfp1->nblocks )
				p = kfp1;
			else
				p = kfp2;
		} else {
			if( cb < kfp2->nblocks )
				p = kfp2;
			else
				p = kfp1;
		}
		len = p->block_len[cb];
		kfp->block_len[cb] = len;
		kfp->opcode[cb] = (unsigned char*) CALLOC(len, sizeof(unsigned char));
		kfp->operand[cb] = (KFORTH_INTEGER*) CALLOC(len, sizeof(KFORTH_INTEGER));
		for(pc=0; pc < len; pc++) {
			kfp->opcode[cb][pc] = p->opcode[cb][pc];
			kfp->operand[cb][pc] = p->operand[cb][pc];
		}
		curmask = curmask >> 1;
		bit += 1;
		if( bit >= 16 ) {
			curmask = mask;
		}
	}
	return kfp;
}
static KFORTH_PROGRAM * kforth_merge_with_mask ( int mask , KFORTH_PROGRAM * kfp1 , KFORTH_PROGRAM * kfp2 ) { KFORTH_PROGRAM * p , * kfp ; int cb , pc , len ; int bit , curmask ; ASSERT ( mask >= 0 && mask <= 0xFFFF ) ; ASSERT ( kfp1 != NULL ) ; ASSERT ( kfp2 != NULL ) ; ASSERT ( kfp1 -> kfops == kfp2 -> kfops ) ; kfp = ( KFORTH_PROGRAM * ) CALLOC ( 1 , sizeof ( KFORTH_PROGRAM ) ) ; kfp -> nblocks = ( kfp1 -> nblocks > kfp2 -> nblocks ) ? kfp1 -> nblocks : kfp2 -> nblocks ; kfp -> block_len = ( int * ) CALLOC ( kfp -> nblocks , sizeof ( int ) ) ; kfp -> opcode = ( unsigned char * * ) CALLOC ( kfp -> nblocks , sizeof ( unsigned char * ) ) ; kfp -> operand = ( KFORTH_INTEGER * * ) CALLOC ( kfp -> nblocks , sizeof ( KFORTH_INTEGER ) ) ; kfp -> kfops = kfp1 -> kfops ; bit = 0 ; curmask = mask ; for ( cb = 0 ; cb < kfp -> nblocks ; cb ++ ) { if ( ( curmask & 0x0001 ) == 0 ) { if ( cb < kfp1 -> nblocks ) p = kfp1 ; else p = kfp2 ; } else { if ( cb < kfp2 -> nblocks ) p = kfp2 ; else p = kfp1 ; } len = p -> block_len [ cb ] ; kfp -> block_len [ cb ] = len ; kfp -> opcode [ cb ] = ( unsigned char * ) CALLOC ( len , sizeof ( unsigned char ) ) ; kfp -> operand [ cb ] = ( KFORTH_INTEGER * ) CALLOC ( len , sizeof ( KFORTH_INTEGER ) ) ; for ( pc = 0 ; pc < len ; pc ++ ) { kfp -> opcode [ cb ] [ pc ] = p -> opcode [ cb ] [ pc ] ; kfp -> operand [ cb ] [ pc ] = p -> operand [ cb ] [ pc ] ; } curmask = curmask >> 1 ; bit += 1 ; if ( bit >= 16 ) { curmask = mask ; } } return kfp ; }
Merge two kforth program.
Merge two kforth program .