Ejemplo n.º 1
0
void main( )
{
    struct sparse s[5] ;
    int i ;

    clrscr( ) ;

    for ( i = 0 ; i <= 4 ; i++ )
    	initsparse ( &s[i] ) ;

	create_array ( &s[0] ) ;

	create_tuple ( &s[1], s[0] ) ;
	display_tuple ( s[1] ) ;

	create_array ( &s[2] ) ;

	create_tuple ( &s[3], s[2] ) ;
	display_tuple ( s[3] ) ;

	addmat ( &s[4], s[1], s[3] ) ;

	printf ( "\nResult of addition of two matrices: " ) ;
	display_result ( s[4] ) ;

    for ( i = 0 ; i <= 4 ; i++ )
		delsparse ( &s[i] ) ;

    getch( ) ;
}
Ejemplo n.º 2
0
static void
test_prepost(void)
{
    int i, j, k;

    tmp = 0;
    total = 0;

    shmem_barrier_all();

    for (i = 0 ; i < niters - 1 ; ++i) {
        cache_invalidate();

        shmem_barrier_all();

        tmp = timer();
        for (j = 0 ; j < npeers ; ++j) {
            for (k = 0 ; k < nmsgs ; ++k) {
                shmem_putmem(recv_buf + (nbytes * (k + j * nmsgs)), 
                             send_buf + (nbytes * (k + j * nmsgs)), 
                             nbytes, send_peers[npeers - j - 1]);
            }
        }
        shmem_quiet();
        shmem_short_wait((short*) (recv_buf + (nbytes * ((nmsgs - 1) + (npeers - 1) * nmsgs))), 0);
        total += (timer() - tmp);
        memset(recv_buf, 0, npeers * nmsgs * nbytes);
    }

    shmem_double_sum_to_all(&tmp, &total, 1, 0, 0, world_size, reduce_pWrk, reduce_pSync);
    display_result("pre-post", (niters * npeers * nmsgs * 2) / (tmp / world_size));
}
Ejemplo n.º 3
0
int main( )
{
	struct sparse s[5] ;
	int i ;

	system ( "cls" ) ;

	for ( i = 0 ; i <= 4 ; i++ )
		initsparse ( &s[i] ) ;

	create_array ( &s[0] ) ;

	create_tuple ( &s[1], s[0] ) ;
	display_tuple ( s[1] ) ;

	create_array ( &s[2] ) ;

	create_tuple ( &s[3], s[2] ) ;
	display_tuple ( s[3] ) ;

	addmat ( &s[4], s[1], s[3] ) ;

	printf ( "Result of addition of two matrices:" ) ;
	display_result ( s[4] ) ;

	for ( i = 0 ; i <= 4 ; i++ )
		delsparse ( &s[i] ) ;

	return 0 ;
}
Ejemplo n.º 4
0
bool test_forward_and_rounding()
{
	cout << "Testing turtle_move_forward and rounding: " << endl;
	turtle t;

	turtle_init(&t, 500, 500, "");
	for (int i = 0; i < 10; i++)
	{
		turtle_rotate_left (&t, 33);
		turtle_move_forward(&t,10);
	}

	if (t.x != -10 || t.y != -1)
	{
		display_result (__FILE__, __LINE__, testcount);
		return false;
	}

	cout << "Test " << testcount << " passed." << endl;
	testcount++;

	turtle_end(&t);
	cout << endl;	
	return true;
}
Ejemplo n.º 5
0
void sort_with_predicate() {
    std::vector<int> vec = {3, 9, 0, 6, 4, 8, 2, 5, 7, 1};

    // And overload that takes a sorting predicate is also available:
    boost::range::sort(vec, std::greater<int>());

    display_result(vec);
}
Ejemplo n.º 6
0
void sort_without_predicate() {
    std::vector<int> vec = {3, 9, 0, 6, 4, 8, 2, 5, 7, 1};

    // sort(vec) rearranges vec so that the elements are in ascending order.
    // Returns a reference to the input range.
    boost::range::sort(vec);

    display_result(vec);
}
Ejemplo n.º 7
0
bool test_pen ()
{
	cout << "Testing turtle_pen_up and turtle_pen_down:" << endl;
	turtle t;
	
	/* 
 	 * This code one of the problems with using structures.  Even though
	 * we want all the modification of the turtle structure to happen through
	 * our functions, the compiler will not prevent someone from changing
	 * the contents directly. 
	 * This is helpful for my test program, but bad in general.
	 */
	t.x = 99;
	t.y = 99;
	t.angle = 99;
	t.penDown = false;
	t.writeToFile = true;
	
	turtle_init(&t, 500,500, NULL);
	
	turtle_pen_up(&t);
	if (t.penDown)
	{
		display_result (__FILE__, __LINE__, testcount);
		return false;
	}
	cout << "Test " << testcount << " passed." << endl;
	testcount++;

	turtle_pen_down(&t);

	if (!t.penDown)
	{
		display_result (__FILE__, __LINE__, testcount);
		return false;
	}
	cout << "Test " << testcount << " passed." << endl;
	testcount++;	

	turtle_end(&t);
	cout << endl;
	return true;

}
void first_input_N(int input_N){
     //procedure that responsibility for check input of integers 
     // is correct or not.
     if(input_N >= 1 && input_N <= 100){ 
       input_integers(input_N); //if input of integers 1-100
       cout << "\n" << "\n" << "Output : \n";
       display_result(input_N); //after previous process has done
    }else{
    integers_constrain(0);} //if input of integers are not 1-100
}
int main() {

   int a[] =
    { 2, 8,12,14,16,19,24,28,31,33,// 0-9
     39,40,45,49,51,53,54,56,57,60,// 10-19
     63,69,77,82,88,89,94,96,97};  // 20-28
   int i,val;
   // for (i = 0; i < 29; i++)
   //    printf("%i ", binarysearch(LEN, a, a[i]));
   printf("\n");
   // printf("Found On Index: %i ", binarysearch(LEN, a, 57));
   // val=binarysearch(LEN, a, 57);
   display_result(binarysearch(LEN, a, 57),57);
   // val=binarysearch(LEN, a, 1);
   display_result(binarysearch(LEN, a, 1),1);
   // printf("%i ", binarysearch(LEN, a, 17));
   // printf("%i ", binarysearch(LEN, a, 42));
   // printf("%i ", binarysearch(LEN, a, 99));
   printf("\n");
}
Ejemplo n.º 10
0
static void
test_one_way(void)
{
    int i, k;
    int pe_size  = world_size;

    tmp = 0;
    total = 0;

    shmem_barrier_all();

    if (world_size % 2 == 1) {
        pe_size = world_size - 1;
    }

    if (!(world_size % 2 == 1 && rank == (world_size - 1))) {
        if (rank < world_size / 2) {
            for (i = 0 ; i < niters ; ++i) {
                cache_invalidate();

                shmem_barrier(0, 0, pe_size, barrier_pSync);

                tmp = timer();
                for (k = 0 ; k < nmsgs ; ++k) {
                    shmem_putmem(recv_buf + (nbytes * k), 
                                 send_buf + (nbytes * k), 
                                 nbytes, rank + (world_size / 2));
                }
                shmem_quiet();
                total += (timer() - tmp);
            }
        } else {
            for (i = 0 ; i < niters ; ++i) {
                cache_invalidate();

                shmem_barrier(0, 0, pe_size, barrier_pSync);

                tmp = timer();
                shmem_short_wait((short*) (recv_buf + (nbytes * (nmsgs - 1))), 0);
                total += (timer() - tmp);
                memset(recv_buf, 0, npeers * nmsgs * nbytes);
            }
        }

        shmem_double_sum_to_all(&tmp, &total, 1, 0, 0, pe_size, reduce_pWrk, reduce_pSync);
        display_result("single direction", (niters * nmsgs) / (tmp / world_size));
    }

    shmem_barrier_all();
}
Ejemplo n.º 11
0
/* Requirement 3 - controls the flow of play in the game */
void play_game(void)
{
	do
	{
		enum move_result move_result = SUCCESSFUL_MOVE;
		enum cell_contents board[BOARD_HEIGHT][BOARD_WIDTH];
		init_board(board);

		/* game loop */
		while (!is_game_over(board) && move_result != QUIT_GAME)
			move_result = player_move(board);

		/* if result is QUIT_GAME, break prematurely */
		if (move_result == QUIT_GAME)
			break;

		display_result(board);
	}
	while (process_ask_play_again());
}
Ejemplo n.º 12
0
void	parsingv3(char square[26][27], t_tetri *tab, int nbtetri, t_var *var)
{
	inityx(var);
	var->pos = 0;
	while (var->pos != nbtetri)
	{
		if (trytoput(square, tab, var))
			var->pos++;
		else if (var->pos > 0)
		{
			var->pos--;
			cor_error(tab, square, var->pos, 4);
			while (trytoputprev(square, tab, var) && var->pos > 0)
			{
				var->pos--;
				cor_error(tab, square, var->pos, 4);
			}
		}
	}
	display_result(square, var);
}
Ejemplo n.º 13
0
bool test_rotate()
{
	cout << "Testing turtle_rotate_left and turtle_rotate_right:" << endl;
	turtle t;
	
	turtle_init(&t, 500,500, NULL);

	turtle_rotate_left(&t, 90);

	if (t.angle != 90)
	{
		display_result (__FILE__, __LINE__, testcount);
		return false;
	}
	cout << "Test " << testcount << " passed." << endl;
	testcount++;

	turtle_rotate_right(&t, 90);

	if (t.angle != 0)
	{
		display_result (__FILE__, __LINE__, testcount);
		return false;
	}
	cout << "Test " << testcount << " passed." << endl;
	testcount++;

	for (int i = 1; i <= 16; i++)
	{
		turtle_rotate_left(&t,45);
		int expected = (i*45) % 360;

		if (t.angle != expected)
		{
			display_result (__FILE__, __LINE__, testcount);
			return false;
		}
	}
	cout << "Test " << testcount << " passed." << endl;
	testcount++;


	for (int i = 1; i <= 16; i++)
	{
		turtle_rotate_right(&t,45);
		int expected = (360 - ((i*45) % 360)) % 360;
		if (t.angle != expected)
		{
			display_result (__FILE__, __LINE__, testcount);
			return false;
		}
	}

	cout << "Test " << testcount << " passed." << endl;
	testcount++;

	cout << endl;
	turtle_end(&t);

	return true;
}
Ejemplo n.º 14
0
void test_prepostME(int             cache_size,
                    int            *cache_buf,
                    ptl_handle_ni_t ni,
                    int             npeers,
                    int             nmsgs,
                    int             nbytes,
                    int             niters)
{
    int    i, j, k;
    double tmp, total = 0;

    ptl_handle_md_t send_md_handle;
    ptl_md_t        send_md;
    ptl_process_t   dest;
    ptl_size_t      offset;
    ptl_pt_index_t  index;
    ptl_handle_eq_t recv_eq_handle;
    ptl_handle_me_t me_handles[npeers * nmsgs];
    ptl_event_t     event;

    ptl_assert(PtlEQAlloc(ni, nmsgs * npeers + 1,
                          &send_md.eq_handle), PTL_OK);

    send_md.start     = send_buf;
    send_md.length    = SEND_BUF_SIZE;
    send_md.options   = PTL_MD_UNORDERED;
    send_md.ct_handle = PTL_CT_NONE;

    ptl_assert(PtlMDBind(ni, &send_md, &send_md_handle), PTL_OK);

    ptl_assert(PtlEQAlloc(ni, nmsgs * npeers + 1, &recv_eq_handle), PTL_OK);

    ptl_assert(PtlPTAlloc(ni, 0, recv_eq_handle, TestSameDirectionIndex,
                          &index), PTL_OK);

    ptl_assert(TestSameDirectionIndex, index);

    tmp = timer();
    for (j = 0; j < npeers; ++j) {
        for (k = 0; k < nmsgs; ++k) {
            ptl_process_t src;
            src.rank = recv_peers[j];
            postME(ni, index, recv_buf + (nbytes * (k + j * nmsgs)),
                   nbytes, src, magic_tag, &me_handles[k + j * nmsgs]);
        }
    }
    total += (timer() - tmp);

    for (i = 0; i < niters - 1; ++i) {
        cache_invalidate(cache_size, cache_buf);

        libtest_Barrier();

        tmp = timer();
        for (j = 0; j < npeers; ++j) {
            for (k = 0; k < nmsgs; ++k) {
                offset    = (nbytes * (k + j * nmsgs));
                dest.rank = send_peers[npeers - j - 1],
                ptl_assert(libtest_Put_offset(send_md_handle, offset, nbytes,
                                           dest, index, magic_tag, offset), PTL_OK);
            }
        }

        /* wait for sends */
        for (j = 0; j < npeers * nmsgs; ++j) {
            ptl_assert(PtlEQWait(send_md.eq_handle, &event), PTL_OK);
            ptl_assert(event.type, PTL_EVENT_SEND);
        }

        /* wait for receives */
        for (j = 0; j < npeers * nmsgs; j++) {
            PtlEQWait(recv_eq_handle, &event);
        }

        for (j = 0; j < npeers; ++j) {
            for (k = 0; k < nmsgs; ++k) {
                ptl_process_t src;
                src.rank = recv_peers[j];
                postME(ni, index, recv_buf + (nbytes * (k + j * nmsgs)),
                       nbytes, src, magic_tag, &me_handles[k + j * nmsgs]);
            }
        }
        total += (timer() - tmp);
    }

    libtest_Barrier();

    tmp = timer();
    for (j = 0; j < npeers; ++j) {
        for (k = 0; k < nmsgs; ++k) {
            offset    = (nbytes * (k + j * nmsgs));
            dest.rank = send_peers[npeers - j - 1],
            ptl_assert(libtest_Put_offset(send_md_handle, offset, nbytes, dest,
                                       index, magic_tag, offset), PTL_OK);
        }
    }
    /* wait for sends */
    for (j = 0; j < npeers * nmsgs; ++j) {
        ptl_assert(PtlEQWait(send_md.eq_handle, &event), PTL_OK);
        ptl_assert(event.type, PTL_EVENT_SEND);
    }

    /* wait for receives */
    for (j = 0; j < npeers * nmsgs; j++) {
        PtlEQWait(recv_eq_handle, &event);
    }

    total += (timer() - tmp);

    ptl_assert(PtlEQFree(send_md.eq_handle), PTL_OK);

    ptl_assert(PtlMDRelease(send_md_handle), PTL_OK);

    ptl_assert(PtlEQFree(recv_eq_handle), PTL_OK);

    ptl_assert(PtlPTFree(ni, index), PTL_OK);

    tmp = libtest_AllreduceDouble(total, PTL_SUM);
    display_result("pre-post", (niters * npeers * nmsgs * 2) / (tmp / world_size));
}
Ejemplo n.º 15
0
static void solve_gains(int *p_counter, double dx, int n, int nb_bands, double *p_best_gains, double *p_best_error, t_mdrc_gains *p_mdrc_gains, double *p_weigthing, double error_max, int counter_max)
{
    double gains[NB_BANDS_MAX], x1[NB_BANDS_MAX], x2[NB_BANDS_MAX], x3[NB_BANDS_MAX], x4[NB_BANDS_MAX], x5[NB_BANDS_MAX], error;
    int    i1, i2, i3, i4, i5;

    memcpy(gains, p_best_gains, nb_bands * sizeof(double));
    for(i1 = -n; i1 <= n; i1++)
    {
        memcpy(x1, gains, nb_bands * sizeof(double));
        x1[0] = gains[0] * pow(1.0 + dx, i1);
        if(nb_bands >= 2)
        {
            memcpy(x2, x1, nb_bands * sizeof(double));
            for(i2 = -n; i2 <= n; i2++)
            {
                x2[1] = x1[1] * pow(1.0 + dx, i2);
                if(nb_bands >= 3)
                {
                    memcpy(x3, x2, nb_bands * sizeof(double));
                    for(i3 = -n; i3 <= n; i3++)
                    {
                        x3[2] = x2[2] * pow(1.0 + dx, i3);
                        if(nb_bands >= 4)
                        {
                            memcpy(x4, x3, nb_bands * sizeof(double));
                            for(i4 = -n; i4 <= n; i4++)
                            {
                                x4[3] = x3[3] * pow(1.0 + dx, i4);
                                if(nb_bands >= 5)
                                {
                                    memcpy(x5, x4, nb_bands * sizeof(double));
                                    for(i5 = -n; i5 <= n; i5++)
                                    {
                                        x5[4] = x4[4] * pow(1.0 + dx, i5);
                                        error = error_response(p_mdrc_gains, x5, nb_bands, p_weigthing);
                                        (*p_counter)++;
                                        if(error < *p_best_error)
                                        {
                                            memcpy(p_best_gains, x5, nb_bands * sizeof(double));
                                            *p_best_error = error;
                                            display_result(*p_counter, error, dx);
                                        }
                                    }
                                    memcpy(gains, p_best_gains, nb_bands * sizeof(double));
                                    error = *p_best_error;
                                    if((error < error_max) || (*p_counter > counter_max))
                                    {
                                        return;
                                    }
                                }
                                else
                                {
                                    error = error_response(p_mdrc_gains, x4, nb_bands, p_weigthing);
                                    (*p_counter)++;
                                    if(error < *p_best_error)
                                    {
                                        memcpy(p_best_gains, x4, nb_bands * sizeof(double));
                                        *p_best_error = error;
                                        display_result(*p_counter, error, dx);
                                    }
                                }
                            }
                            memcpy(gains, p_best_gains, nb_bands * sizeof(double));
                            error = *p_best_error;
                            if((error < error_max) || (*p_counter > counter_max))
                            {
                                return;
                            }
                        }
                        else
                        {
                            error = error_response(p_mdrc_gains, x3, nb_bands, p_weigthing);
                            (*p_counter)++;
                            if(error < *p_best_error)
                            {
                                memcpy(p_best_gains, x3, nb_bands * sizeof(double));
                                *p_best_error = error;
                                display_result(*p_counter, error, dx);
                            }
                        }
                    }
                    memcpy(gains, p_best_gains, nb_bands * sizeof(double));
                    error = *p_best_error;
                    if((error < error_max) || (*p_counter > counter_max))
                    {
                        return;
                    }
                }
                else
                {
                    error = error_response(p_mdrc_gains, x2, nb_bands, p_weigthing);
                    (*p_counter)++;
                    if(error < *p_best_error)
                    {
                        memcpy(p_best_gains, x2, nb_bands * sizeof(double));
                        *p_best_error = error;
                        display_result(*p_counter, error, dx);
                    }
                }
            }
            memcpy(gains, p_best_gains, nb_bands * sizeof(double));
            error = *p_best_error;
            if((error < error_max) || (*p_counter > counter_max))
            {
                return;
            }
        }
        else
        {
            error = error_response(p_mdrc_gains, x1, nb_bands, p_weigthing);
            (*p_counter)++;
            if(error < *p_best_error)
            {
                memcpy(p_best_gains, x1, nb_bands * sizeof(double));
                *p_best_error = error;
                display_result(*p_counter, error, dx);
            }
        }
    }
}
Ejemplo n.º 16
0
static int mdrc_gains(double *bands, int nb_bands, t_mdrc_gains **p_p_mdrc_gains)
// inputs :
//      bands           : band cut off frequencies (first band cut off frequency = 0)
//      nb_bands        : number of bands (1 to 5)
//
// outputs (into **p_p_mdrc_gains) :
//      index           : index of filter for each band
//      gains           : best found gains for each band
//      band_responses  : response of each band
//      global_response : global response
//      error           : error
{
    double FreqCutoff[]   = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 110, 120, 130, 140, 150, 160, 170};

    //                             b2,    -b1/2,      b0,   -a1/2,      a2
    double LPCoefTab[][5] = {{      0,       -0,       0, 8388607, 8388607},
                             {    356,     -356,     356, 8310966, 8234749},
                             {   1411,    -1411,    1411, 8233344, 8083724},
                             {   3146,    -3146,    3146, 8155760, 7935495},
                             {   5541,    -5541,    5541, 8078234, 7790021},
                             {   8577,    -8577,    8577, 8000780, 7647261},
                             {  12238,   -12238,   12238, 7923418, 7507178},
                             {  16504,   -16504,   16504, 7846162, 7369732},
                             {  21358,   -21358,   21358, 7769030, 7234882},
                             {  26783,   -26783,   26783, 7692034, 7102590},
                             {  32761,   -32761,   32761, 7615190, 6972818},
                             {  70399,   -70399,   70399, 7233707, 6360402},
                             { 119556,  -119556,  119556, 6857840, 5805295},
                             { 178504,  -178504,  178504, 6488789, 5302984},
                             { 245707,  -245707,  245707, 6127486, 4849190},
                             { 319807,  -319807,  319807, 5774638, 4439894},
                             { 399607,  -399607,  399607, 5430758, 4071336},
                             { 484057,  -484057,  484057, 5096202, 3740025},
                             { 572239,  -572239,  572239, 4771190, 3442727},
                             { 663351,  -663351,  663351, 4455832, 3176458},
                             { 756697,  -756697,  756697, 4150147, 2938474},
                             { 851674,  -851674,  851674, 3854084, 2726259},
                             { 947762,  -947762,  947762, 3567532, 2537505},
                             {1044512, -1044512, 1044512, 3290333, 2370105},
                             {1141538, -1141538, 1141538, 3022295, 2222134},
                             {1238513, -1238513, 1238513, 2763196, 2091835},
                             {1335156, -1335156, 1335156, 2512796, 1977608},
                             {1431229, -1431229, 1431229, 2270842, 1877993},
                             {1526534, -1526534, 1526534, 2037068, 1791664},
                             {1714194, -1714194, 1714194, 1592982, 1654134},
                             {1897115, -1897115, 1897115, 1178372, 1556594},
                             {2074617, -2074617, 2074617,  791105, 1492071},
                             {2246282, -2246282, 2246282,  429132, 1454786},
                             {2411881, -2411881, 2411881,   90515, 1439948},
                             {2571327, -2571327, 2571327, -226558, 1443585},
                             {2724636, -2724636, 2724636, -523771, 1462394}};

    double HPCoefTab[][5] = {{8388607,  8388607, 8388607, 8388607, 8388607},
                             {8311322,  8311322, 8311322, 8310966, 8234749},
                             {8234756,  8234756, 8234756, 8233344, 8083724},
                             {8158906,  8158906, 8158906, 8155760, 7935495},
                             {8083774,  8083774, 8083774, 8078234, 7790021},
                             {8009357,  8009357, 8009357, 8000780, 7647261},
                             {7935656,  7935656, 7935656, 7923418, 7507178},
                             {7862666,  7862666, 7862666, 7846162, 7369732},
                             {7790387,  7790387, 7790387, 7769030, 7234882},
                             {7718816,  7718816, 7718816, 7692034, 7102590},
                             {7647952,  7647952, 7647952, 7615190, 6972818},
                             {7304106,  7304106, 7304106, 7233707, 6360402},
                             {6977396,  6977396, 6977396, 6857840, 5805295},
                             {6667292,  6667292, 6667292, 6488789, 5302984},
                             {6373192,  6373192, 6373192, 6127486, 4849190},
                             {6094444,  6094444, 6094444, 5774638, 4439894},
                             {5830366,  5830366, 5830366, 5430758, 4071336},
                             {5580260,  5580260, 5580260, 5096202, 3740025},
                             {5343428,  5343428, 5343428, 4771190, 3442727},
                             {5119182,  5119182, 5119182, 4455832, 3176458},
                             {4906844,  4906844, 4906844, 4150147, 2938474},
                             {4705759,  4705759, 4705759, 3854084, 2726259},
                             {4515294,  4515294, 4515294, 3567532, 2537505},
                             {4334844,  4334844, 4334844, 3290333, 2370105},
                             {4163833,  4163833, 4163833, 3022295, 2222134},
                             {4001709,  4001709, 4001709, 2763196, 2091835},
                             {3847952,  3847952, 3847952, 2512796, 1977608},
                             {3702071,  3702071, 3702071, 2270842, 1877993},
                             {3563602,  3563602, 3563602, 2037068, 1791664},
                             {3307176,  3307176, 3307176, 1592982, 1654134},
                             {3075486,  3075486, 3075486, 1178372, 1556594},
                             {2865722,  2865722, 2865722,  791105, 1492071},
                             {2675414,  2675414, 2675414,  429132, 1454786},
                             {2502396,  2502396, 2502396,   90515, 1439948},
                             {2344769,  2344769, 2344769, -226558, 1443585},
                             {2200865,  2200865, 2200865, -523771, 1462394}};

    const int    nb_filters = sizeof(FreqCutoff) / sizeof(*FreqCutoff);
    double       coef = pow(2.0, -23.0);
    double       swap_tmp, max_response;
    double       error, best_error, error_max, dx;
    double       weigthing[N];
    double       best_gains[NB_BANDS_MAX];
    int          i, j, counter, counter_max;
    t_mdrc_gains *p_mdrc_gains;
    int          *p_index;
    double       *p_gains;
    t_complex    *p_band_response;
    t_complex    *p_global_response;


    nb_bands = (nb_bands > NB_BANDS_MAX ? NB_BANDS_MAX : nb_bands);

    p_mdrc_gains = (t_mdrc_gains *) malloc(sizeof(t_mdrc_gains));
    if(p_mdrc_gains == NULL)
    {
        return -1;
    }
    p_index           = p_mdrc_gains->index;
    p_gains           = p_mdrc_gains->gains;
    p_global_response = p_mdrc_gains->global_response;
    *p_p_mdrc_gains   = p_mdrc_gains;

    for(i = 0; i < sizeof(FreqCutoff) / sizeof(*FreqCutoff); i++)
    {
        FreqCutoff[i] *= 100.0;
    }
    for(i = 0; i < sizeof(LPCoefTab) / sizeof(*LPCoefTab); i++)
    {
        swap_tmp         = LPCoefTab[i][2] * coef;
        LPCoefTab[i][2]  = LPCoefTab[i][0] * coef;
        LPCoefTab[i][0]  = swap_tmp;
        LPCoefTab[i][1] *= coef * -2.0;
        LPCoefTab[i][3] *= coef * -2.0;
        LPCoefTab[i][4] *= coef;
    }
    for(i = 0; i < sizeof(HPCoefTab) / sizeof(*HPCoefTab); i++)
    {
        swap_tmp         = HPCoefTab[i][2] * coef;
        HPCoefTab[i][2]  = HPCoefTab[i][0] * coef;
        HPCoefTab[i][0]  = swap_tmp;
        HPCoefTab[i][1] *= coef * -2.0;
        HPCoefTab[i][3] *= coef * -2.0;
        HPCoefTab[i][4] *= coef;
    }
    compute_weigthing(weigthing);

    if(nb_bands > 1)
    {
        for(i = 1; i < nb_bands; i++)
        {
            for(j = 0; j < nb_filters; j++)
            {
                if(bands[i] <= FreqCutoff[j])
                {
                    p_index[i - 1] = j;

                    // select nearest frequency
                    if(j > 0)
                    {
                        if((FreqCutoff[j] - bands[i]) > (bands[i] - FreqCutoff[j - 1]))
                        {
                            p_index[i - 1] = j - 1;
                        }
                    }
                    break;
                }
            }
        }


        for(i = 0; i < nb_bands; i++)
        {
            double max_square;
            int    index_LP, index_HP;

            p_band_response = p_mdrc_gains->band_responses[i];
            if(i == 0)
            {
                index_LP = p_index[i];
                response(LPCoefTab[index_LP][0], LPCoefTab[index_LP][1], LPCoefTab[index_LP][2], LPCoefTab[index_LP][3], LPCoefTab[index_LP][4],
                         1.0,                    0.0,                    0.0,                    0.0,                    0.0,
                         p_band_response);
            }
            else if(i < nb_bands - 1)
            {
                index_LP = p_index[i];
                index_HP = p_index[i - 1];
                response(LPCoefTab[index_LP][0], LPCoefTab[index_LP][1], LPCoefTab[index_LP][2], LPCoefTab[index_LP][3], LPCoefTab[index_LP][4],
                         HPCoefTab[index_HP][0], HPCoefTab[index_HP][1], HPCoefTab[index_HP][2], HPCoefTab[index_HP][3], HPCoefTab[index_HP][4],
                         p_band_response);
            }
            else
            {
                index_HP = p_index[i - 1];
                response(1.0,                    0.0,                    0.0,                    0.0,                    0.0,
                         HPCoefTab[index_HP][0], HPCoefTab[index_HP][1], HPCoefTab[index_HP][2], HPCoefTab[index_HP][3], HPCoefTab[index_HP][4],
                         p_band_response);
            }

            max_square = 0.0;
            for(j = 0; j < N; j++)
            {
                double square = p_band_response[j].re * p_band_response[j].re + p_band_response[j].im * p_band_response[j].im;

                if(square > max_square)
                {
                    max_square = square;
                }
            }
            p_gains[i] = 1.0 / sqrt(max_square);
        }

        error = error_response(p_mdrc_gains, p_gains, nb_bands, weigthing);
        memcpy(best_gains, p_gains, nb_bands * sizeof(double));
        best_error  = error;
        dx          = 0.1;
        error_max   = 0.05;
        counter     = 1;
        counter_max = 1000000;
        display_result(counter, error, dx);

        // coarse gains search
        while((best_error > error_max) && (counter < counter_max))
        {
            solve_gains(&counter, dx, 5, nb_bands, best_gains, &best_error, p_mdrc_gains, weigthing, error_max, counter_max);
            dx *= 0.5;
        }

        // fine gains search
        counter_max *= 2;
        error_max   *= 0.2;
        dx          *= 0.2;
        solve_gains(&counter, dx, 10, nb_bands, best_gains, &best_error, p_mdrc_gains, weigthing, error_max, counter_max);

        memcpy(p_gains, best_gains, nb_bands * sizeof(double));
        error = best_error;
        display_result(counter, error, dx);
    }
    else
    {
        p_index[0] = 0;
        p_gains[0] = 1.0;
        response(1.0, 0.0, 0.0, 0.0, 0.0,
                 1.0, 0.0, 0.0, 0.0, 0.0,
                 p_mdrc_gains->band_responses[0]);
        error = error_response(p_mdrc_gains, p_gains, nb_bands, weigthing);
        printf("no sub-band\n");
    }

    for(i = 0; i < N; i++)
    {
        p_global_response[i].re = 0.0;
        p_global_response[i].im = 0.0;
        for(j = 0; j < nb_bands; j++)
        {
            p_global_response[i].re += p_gains[j] * p_mdrc_gains->band_responses[j][i].re;
            p_global_response[i].im += p_gains[j] * p_mdrc_gains->band_responses[j][i].im;
        }
    }

    p_mdrc_gains->error = error;

    return 0;
}
Ejemplo n.º 17
0
Archivo: http.c Proyecto: sjbawpn/c
int main(int argc, char* argv[]) {
    int client_connection;
    char *host, *path;
    struct hostent *host_name;
    struct sockaddr_in host_address;

#ifdef WIN32
    WSDATA wsaData;
#endif

    if (argc < 2) {
        fprintf(stderr, "Usage: %s: <URL>\n", argv[0]);
        return 1;
    }

    if (parse_url(argv[1], &host, &path) == -1) {
        fprintf(stderr, "Error - malformed URL '%s' .\n", argv[1]);
        return 1;
    }

    printf("Connecting to host '%s'\n", host);

    // Step 1 :open a socket connection on http port with the destination host.

#ifdef WIN32
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) {
        fprintf(stderr, "ERROR, unable to initialize winsock.\n");
        return 2;
    }
#endif

    client_connection = socket(PF_INET, SOCK_STREAM, 0);

    if (!client_connection) {
        perror("Unable to create local socket");
        return 2;
    }

    host_name = gethostbyname(host);

    if (!host_name) {
        perror( "Error in name resolution");
        return 3;
    }

    host_address.sin_family = AF_INET;
    host_address.sin_port = htons(HTTP_PORT);

    memcpy(&host_address.sin_addr, host_name->h_addr_list[0], sizeof(struct in_addr));

    if (connect(client_connection, (struct sockaddr*) &host_address, sizeof(host_address)) == -1) {
        perror("Unable to connect to host");
        return 4;
    }

    printf("Retrieving document: '%s'\n", path);


    http_get(client_connection, path, host);
    
    display_result(client_connection);

    printf("Shutting down.\n");

#ifdef WIN32
    if (closesocket(client_connection) == -1)
#else
    if (close(client_connection) == -1)
#endif
    {
        perror("Error closing clienet connection");
        return 5;
    }

#ifdef WIN32
    WSACleanup();
#endif

    return 0;
} 
Ejemplo n.º 18
0
/**
 * Simple command-line HTTP client.
 */
int main( int argc, char *argv[ ] )
{
  int client_connection;
  char *host, *path;
  char *proxy_host, *proxy_user, *proxy_password;
  int proxy_port;
  struct hostent *host_name;
  struct sockaddr_in host_address;
  int port = HTTPS_PORT;
  int ind;
  int master_secret_length;
  unsigned char *master_secret;
  int session_id_length;
  unsigned char *session_id;
#ifdef WIN32
  WSADATA wsaData;
#endif

  TLSParameters tls_context;

  if ( argc < 2 )
  {
    fprintf( stderr, 
      "Usage: %s: [-p http://[username:password@]proxy-host:proxy-port] <URL>\n", 
      argv[ 0 ] );
    return 1;
  }

  proxy_host = proxy_user = proxy_password = host = path = session_id = master_secret = NULL;
  session_id_length = master_secret_length = 0;

  for ( ind = 1; ind < ( argc - 1 ); ind++ )
  {
    if ( !strcmp( "-p", argv[ ind ] ) )
    {
      if ( !parse_proxy_param( argv[ ++ind ], &proxy_host, &proxy_port,
                               &proxy_user, &proxy_password ) )
      {
        fprintf( stderr, "Error - malformed proxy parameter '%s'.\n", argv[ 2 ] );
        return 2;
      }
    }
    else if ( !strcmp( "-s", argv[ ind ] ) )
    {
      session_id_length = hex_decode( argv[ ++ind ], &session_id );
    }
    else if ( !strcmp( "-m", argv[ ind ] ) )
    {
      master_secret_length = hex_decode( argv[ ++ind ], &master_secret );
    }
  }

  if ( ( ( master_secret_length > 0 ) && ( session_id_length == 0 ) ) ||
       ( ( master_secret_length == 0 ) && ( session_id_length > 0 ) ) )
  {
    fprintf( stderr, "session id and master secret must both be provided.\n" );
    return 3;
  }

  if ( parse_url( argv[ ind ], &host, &path ) == -1 )
  {
    fprintf( stderr, "Error - malformed URL '%s'.\n", argv[ 1 ] );
    return 1;
  }

  printf( "Connecting to host '%s'\n", host );
  // Step 1: open a socket connection on http port with the destination host.
#ifdef WIN32
  if ( WSAStartup( MAKEWORD( 2, 2 ), &wsaData ) != NO_ERROR )
  {
    fprintf( stderr, "Error, unable to initialize winsock.\n" );
    return 2;
  }
#endif

  client_connection = socket( PF_INET, SOCK_STREAM, 0 );

  if ( !client_connection )
  {
    perror( "Unable to create local socket" );
    return 2;
  }

  if ( proxy_host )
  {
    printf( "Connecting to host '%s'\n", proxy_host );
    host_name = gethostbyname( proxy_host );
  } 
  else
  {
    host_name = gethostbyname( host );
  }

  if ( !host_name )
  {
    perror( "Error in name resolution" );
    return 3;
  }

  host_address.sin_family = AF_INET;
  host_address.sin_port = htons( proxy_host ? proxy_port : HTTPS_PORT  );
  memcpy( &host_address.sin_addr, host_name->h_addr_list[ 0 ], 
          sizeof( struct in_addr ) );

  if ( connect( client_connection, ( struct sockaddr * ) &host_address, 
       sizeof( host_address ) ) == -1 )
  {
    perror( "Unable to connect to host" );
    return 4;
  }

  printf( "Connection complete; negotiating TLS parameters\n" );

  if ( proxy_host )
  {
    if ( !http_connect( client_connection, host, port, proxy_user, 
                        proxy_password ) )
    {
      perror( "Unable to establish proxy tunnel" );
      if ( close( client_connection ) == -1 )
      {
        perror( "Error closing client connection" );
        return 2;
      }
      return 3;
    }
  }

  if ( session_id != NULL )
  {
    if ( tls_resume( client_connection, session_id_length,
         session_id, master_secret, &tls_context ) )
    {
      fprintf( stderr, "Error: unable to negotiate SSL connection.\n" );
      if ( close( client_connection ) == -1 )
      {
        perror( "Error closing client connection" );
        return 2;
      }
      return 3;
    }
  }
  else
  {
    if ( tls_connect( client_connection, &tls_context, 0 ) )
    {
      fprintf( stderr, "Error: unable to negotiate TLS connection.\n" );
      return 3;
    }
  }

  printf( "Retrieving document: '%s'\n", path );
  http_get( client_connection, path, host, &tls_context );

  display_result( client_connection, &tls_context );

  tls_shutdown( client_connection, &tls_context );

  printf( "Session ID was: " );
  show_hex( tls_context.session_id, tls_context.session_id_length );
  printf( "Master secret was: " );
  show_hex( tls_context.master_secret, MASTER_SECRET_LENGTH );

  printf( "Shutting down.\n" );

#ifdef WIN32
  if ( closesocket( client_connection ) == -1 )
#else
  if ( close( client_connection ) == -1 )
#endif
  {
    perror( "Error closing client connection" );
    return 5;
  }

  if ( session_id != NULL )
  {
    free( session_id );
  } 
  
  if ( master_secret != NULL )
  {
    free( master_secret );
  } 

#ifdef WIN32
  WSACleanup();
#endif

  return 0;
}
Ejemplo n.º 19
0
int hash_file(state *s, TCHAR *fn)
{
  size_t fn_length;
  char *sum;
  TCHAR *my_filename, *msg;
  FILE *handle;
  
  handle = _tfopen(fn,_TEXT("rb"));
  if (NULL == handle)
  {
    print_error_unicode(s,fn,"%s", strerror(errno));
    return TRUE;
  }
 
  if ((sum = (char *)malloc(sizeof(char) * FUZZY_MAX_RESULT)) == NULL)
  {
    fclose(handle);
    print_error_unicode(s,fn,"%s", strerror(errno));
    return TRUE;
  }

  if ((msg = (TCHAR *)malloc(sizeof(TCHAR) * (MAX_STATUS_MSG + 2))) == NULL)
  {
    free(sum);
    fclose(handle);
    print_error_unicode(s,fn,"%s", strerror(errno));
    return TRUE;
  }

  if (MODE(mode_verbose))
  {
    fn_length = _tcslen(fn);
    if (fn_length > MAX_STATUS_MSG)
    {
      // We have to make a duplicate of the string to call basename on it
      // We need the original name for the output later on
      my_filename = _tcsdup(fn);
      my_basename(my_filename);
    }
    else
      my_filename = fn;

    _sntprintf(msg,
	       MAX_STATUS_MSG-1,
	       _TEXT("Hashing: %s%s"), 
	       my_filename, 
	       _TEXT(BLANK_LINE));
    _ftprintf(stderr,_TEXT("%s\r"), msg);

    if (fn_length > MAX_STATUS_MSG)
      free(my_filename);
  }

  fuzzy_hash_file(handle,sum);
  prepare_filename(s,fn);
  display_result(s,fn,sum);

  if (find_file_size(handle) > SSDEEP_MIN_FILE_SIZE)
    s->found_meaningful_file = true;
  s->processed_file = true;

  fclose(handle);
  free(sum);
  free(msg);
  return FALSE;
}
Ejemplo n.º 20
0
/**
 * Simple command-line HTTP client.
 */
int main( int argc, char *argv[ ] )
{
  int client_connection;
  char *host, *path;
  char *proxy_host, *proxy_user, *proxy_password;
  int proxy_port;
  struct hostent *host_name;
  struct sockaddr_in host_address;
  int ind;
#ifdef WIN32
  WSADATA wsaData;
#endif

  if ( argc < 2 )
  {
    fprintf( stderr, 
      "Usage: %s: [-p http://[username:password@]proxy-host:proxy-port] <URL>\n", 
      argv[ 0 ] );
    return 1;
  }

  proxy_host = proxy_user = proxy_password = host = path = NULL;
  ind = 1;
  if ( !strcmp( "-p", argv[ ind ] ) )
  {
    if ( !parse_proxy_param( argv[ ++ind ], &proxy_host, &proxy_port,
                             &proxy_user, &proxy_password ) )
    {
      fprintf( stderr, "Error - malformed proxy parameter '%s'.\n", argv[ 2 ] );
      return 2;
    }
    ind++;
  }

  if ( parse_url( argv[ ind ], &host, &path ) == -1 )
  {
    fprintf( stderr, "Error - malformed URL '%s'.\n", argv[ 1 ] );
    return 1;
  }

  printf( "Connecting to host '%s'\n", host );
  // Step 1: open a socket connection on http port with the destination host.
#ifdef WIN32
  if ( WSAStartup( MAKEWORD( 2, 2 ), &wsaData ) != NO_ERROR )
  {
    fprintf( stderr, "Error, unable to initialize winsock.\n" );
    return 2;
  }
#endif

  client_connection = socket( PF_INET, SOCK_STREAM, 0 );

  if ( !client_connection )
  {
    perror( "Unable to create local socket" );
    return 2;
  }

  if ( proxy_host )
  {
    printf( "Connecting to host '%s'\n", proxy_host );
    host_name = gethostbyname( proxy_host );
  } 
  else
  {
    host_name = gethostbyname( host );
  }

  if ( !host_name )
  {
    perror( "Error in name resolution" );
    return 3;
  }

  host_address.sin_family = AF_INET;
  host_address.sin_port = htons( proxy_host ? proxy_port : HTTP_PORT  );
  memcpy( &host_address.sin_addr, host_name->h_addr_list[ 0 ], 
          sizeof( struct in_addr ) );

  if ( connect( client_connection, ( struct sockaddr * ) &host_address, 
       sizeof( host_address ) ) == -1 )
  {
    perror( "Unable to connect to host" );
    return 4;
  }

  printf( "Retrieving document: '%s'\n", path );
  http_get( client_connection, path, host, proxy_host,
            proxy_user, proxy_password );

  display_result( client_connection );

  printf( "Shutting down.\n" );

#ifdef WIN32
  if ( closesocket( client_connection ) == -1 )
#else
  if ( close( client_connection ) == -1 )
#endif
  {
    perror( "Error closing client connection" );
    return 5;
  }

#ifdef WIN32
  WSACleanup();
#endif

  return 0;
}
Ejemplo n.º 21
0
int main(int argc, char **argv) {
  int c;
  struct ipcheck ipc;
  char *nameserver = NULL;
  int zgiven = 0;

  if (!(progname = strrchr(argv[0], '/'))) progname = argv[0];
  else argv[0] = ++progname;

  while((c = getopt(argc, argv, "hqtvms:S:cn:")) != EOF) switch(c) {
  case 's': ++zgiven; addzone(optarg); break;
  case 'S':
    ++zgiven;
    if (addzonefile(optarg)) break;
    fprintf(stderr, "%s: unable to read %s\n", progname, optarg);
    return 1;
  case 'c': ++zgiven; nzones = 0; break;
  case 'q': --verbose; break;
  case 'v': ++verbose; break;
  case 't': do_txt = 1; break;
  case 'n': nameserver = optarg; break;
  case 'm': ++stopfirst; break;
  case 'h':
    printf("%s: %s.\n", progname, version);
    printf("Usage is: %s [options] address..\n", progname);
    printf(
"Where options are:\n"
" -h - print this help and exit\n"
" -s service - add the service (DNSBL zone) to the serice list\n"
" -S service-file - add the DNSBL zone(s) read from the given file\n"
" -c - clear service list\n"
" -v - increase verbosity level (more -vs => more verbose)\n"
" -q - decrease verbosity level (opposite of -v)\n"
" -t - obtain and print TXT records if any\n"
" -m - stop checking after first address match in any list\n"
" -n ipaddr - use the given nameserver instead of the default\n"
"(if no -s or -S option is given, use $RBLCHECK_ZONES, ~/.rblcheckrc\n"
"or /etc/rblcheckrc in that order)\n"
    );
    return 0;
  default:
    fprintf(stderr, "%s: use `%s -h' for help\n", progname, progname);
    return 1;
  }

  if (!zgiven) {
    char *s = getenv("RBLCHECK_ZONES");
    if (s) {
      char *k;
      s = strdup(s);
      k = strtok(s, " \t");
      while(k) {
        addzone(k);
        k = strtok(NULL, " \t");
      }
      free(s);
    }
    else {
      char *path;
      char *home = getenv("HOME");
      if (!home) home = ".";
      path = malloc(strlen(home) + 1 + sizeof(".rblcheckrc"));
      sprintf(path, "%s/.rblcheckrc", home);
      if (!addzonefile(path))
        addzonefile("/etc/rblcheckrc");
      free(path);
    }
  }
  if (!nzones) {
    fprintf(stderr, "%s: no service (zone) list specified (-s or -S option)\n",
            progname);
    return 1;
  }

  argv += optind;
  argc -= optind;

  if (!argc)
    return 0;

  if (dns_init(0) < 0) {
    fprintf(stderr, "%s: unable to initialize DNS library: %s\n",
            progname, strerror(errno));
    return 1;
  }
  if (nameserver) {
    dns_add_serv(NULL, NULL);
    if (dns_add_serv(NULL, nameserver) < 0)
      fprintf(stderr, "%s: unable to use nameserver %s: %s\n",
              progname, nameserver, strerror(errno));
  }
  if (dns_open(NULL) < 0) {
    fprintf(stderr, "%s: unable to initialize DNS library: %s\n",
            progname, strerror(errno));
    return 1;
  }

  for (c = 0; c < argc; ++c) {
    if (c && (verbose > 1 || (verbose == 1 && do_txt))) putchar('\n');
    ipc.name = argv[c];
    submit(&ipc);
    waitdns(&ipc);
    display_result(&ipc);
    if (stopfirst > 1 && listed) break;
  }

  return listed ? 100 : failures ? 2 : 0;
}
Ejemplo n.º 22
0
/**
 * \brief main function
 */
int main(void)
{
	static const gpio_map_t USART_GPIO_MAP = {
		{EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION},
		{EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION}
	};

	// USART options
	static const usart_options_t USART_OPTIONS = {
		.baudrate     = 57600,
		.charlength   = 8,
		.paritytype   = USART_NO_PARITY,
		.stopbits     = USART_1_STOPBIT,
		.channelmode  = 0
	};

#if BOARD == UC3L_EK

	scif_osc32_opt_t  opt_osc32 = {
		// 2-pin Crystal and high current mode
		SCIF_OSC_MODE_2PIN_CRYSTAL_HICUR,
		// oscillator startup time
		AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC,
		/*
		 * select the alternate xin32_2 and xout32_2 for the
		 * 32kHz crystal oscillator
		 */
		true,
		// disable the 1kHz output
		false,
		// enable the 32kHz output
		true
	};

#else
	scif_osc32_opt_t opt_osc32 = {
		// 2-pin crystal mode
		.mode = SCIF_OSC_MODE_2PIN_CRYSTAL,
		// startup time
		.startup = AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC
	};
#endif
	// Set system clock
	sysclk_init();

	// Start OSC_32KHZ
	scif_start_osc32(&opt_osc32,true);

	// Assign GPIO pins to USART0.
	gpio_enable_module(USART_GPIO_MAP,
		sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));

	// Initialize USART in RS232 mode
	usart_init_rs232(EXAMPLE_USART, &USART_OPTIONS, FPBA);

	// Welcome message
	usart_write_line(EXAMPLE_USART, "\x1B[2J\x1B[H\r\nATMEL\r\n");
	usart_write_line(EXAMPLE_USART, "AVR UC3 - FREQM example\r\n");

#ifndef INTERRUPT_MODE_SUPPORT
	uint32_t result;
	status_code_t status;
	uint32_t duration = 128;
	usart_write_line(EXAMPLE_USART, "/*** Normal Mode ***/\r\n");
	freqm_enable();
	status = freqm_write_config(AVR32_FREQM_REF_OSC32, AVR32_FREQM_CPU,
			duration);
	if (status == ERR_TIMEOUT) {
		usart_write_line(EXAMPLE_USART, "FREQM Module Config Fail!");
		while(true);
	}
	result = clock_measure();
	usart_write_line(EXAMPLE_USART, "CPU Clock:\r\n");
	display_result(OSC32K_FREQ_HZ, duration, result);

	usart_write_line(EXAMPLE_USART, "HSB Clock:\r\n");
	freqm_set_clock_source(AVR32_FREQM_HSB);
	result = clock_measure();
	display_result(OSC32K_FREQ_HZ, duration, result);

	usart_write_line(EXAMPLE_USART, "PBA Clock:\r\n");
	freqm_set_clock_source(AVR32_FREQM_PBA);
	result = clock_measure();
	display_result(OSC32K_FREQ_HZ, duration, result);

	usart_write_line(EXAMPLE_USART, "PBB Clock:\r\n");
	freqm_set_clock_source(AVR32_FREQM_PBB);
	result = clock_measure();
	display_result(OSC32K_FREQ_HZ, duration, result);

	pm_set_clk_domain_div(PM_CLK_DOMAIN_3,PM_CKSEL_DIVRATIO_16);
	usart_write_line(EXAMPLE_USART, "PBB Clock(Main clock/16):\r\n");
	freqm_set_clock_source(AVR32_FREQM_PBB);
	result = clock_measure();
	display_result(OSC32K_FREQ_HZ, duration, result);

	usart_write_line(EXAMPLE_USART, "Test Complete!\r\n");
	while (true) {
		/*
		 * Force a NOP instruction for an eventual placement of a debug
		 * session breakpoint.
		 */
		asm("nop\n");
	}
#else
	INTC_init_interrupts();
	INTC_register_interrupt(&freqm_int_handler, AVR32_FREQM_IRQ,
			AVR32_INTC_INT0);
	freqm_enable_measurement_done_int();
	cpu_irq_enable();
	usart_write_line(EXAMPLE_USART, "/*** Interrupt Mode ***/\r\n");
	uint32_t duration = 128;
	uint32_t result;
	status_code_t status;
	freqm_enable();
	status = freqm_write_config(AVR32_FREQM_REF_OSC32, AVR32_FREQM_CPU,
			duration);
	if (status == ERR_TIMEOUT) {
		usart_write_line(EXAMPLE_USART, "FREQM Module Config Fail!");
		while(true);
	}
	freqm_start();
	while(true) {
		if(flag == 0) {
			result =(uint32_t)(((F32)value / duration)
					* OSC32K_FREQ_HZ);
			usart_write_line(EXAMPLE_USART, "CPU Clock:\r\n");
			display_result(OSC32K_FREQ_HZ, duration, result);
			usart_write_line(EXAMPLE_USART, "Test Complete!\r\n");
			flag = 1;
		}
		/*
		 * Force a NOP instruction for an eventual placement of a debug
		 * session breakpoint.
		 */
		asm("nop\n");
	};
#endif
  return 0;
}