[Olsr-cvs] olsrd-current/src rebuild_packet.c, 1.21, 1.22 print_packet.c, 1.10, 1.11 mantissa.h, 1.9, 1.10 mantissa.c, 1.8, 1.9 lq_packet.c, 1.25, 1.26

Bernd Petrovitsch (spam-protected)
Tue Aug 28 22:45:19 CEST 2007


Update of /cvsroot/olsrd/olsrd-current/src
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv25179/src

Modified Files:
	rebuild_packet.c print_packet.c mantissa.h mantissa.c 
	lq_packet.c 
Log Message:
- Made a function from the ME_TO_DOUBLE() macro (in src/mantissa.h).
  This saves code throughout the code even on i386 and will even more
  on architectures without floating point units and "-msoft-float".
- And the mathemathics in src/mantissa.h is reformulated to minimize
  floating point operations to save CPU power - especially on embedded
  devices.


Index: mantissa.h
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/src/mantissa.h,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** mantissa.h	29 May 2005 12:47:45 -0000	1.9
--- mantissa.h	28 Aug 2007 20:45:17 -0000	1.10
***************
*** 61,69 ****
   *
   */
! #define ME_TO_DOUBLE(me) \
!   (double)(VTIME_SCALE_FACTOR*(1+(double)(me>>4)/16)*(double)(1<<(me&0x0F)))
  
! olsr_u8_t
! double_to_me(double);
  
  #endif
--- 61,67 ----
   *
   */
! double me_to_double(const olsr_u8_t);
  
! olsr_u8_t double_to_me(const double);
  
  #endif

Index: rebuild_packet.c
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/src/rebuild_packet.c,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** rebuild_packet.c	25 Apr 2007 22:08:13 -0000	1.21
--- rebuild_packet.c	28 Aug 2007 20:45:17 -0000	1.22
***************
*** 88,92 ****
  
        /* Get vtime */
!       hmsg->vtime = ME_TO_DOUBLE(m->v4.olsr_vtime);
  
        tmp_pairs = NULL;
--- 88,92 ----
  
        /* Get vtime */
!       hmsg->vtime = me_to_double(m->v4.olsr_vtime);
  
        tmp_pairs = NULL;
***************
*** 128,132 ****
        
        /* Get vtime */
!       hmsg->vtime = ME_TO_DOUBLE(m->v6.olsr_vtime);
        
        tmp_pairs = NULL;
--- 128,132 ----
        
        /* Get vtime */
!       hmsg->vtime = me_to_double(m->v6.olsr_vtime);
        
        tmp_pairs = NULL;
***************
*** 213,217 ****
  
        /* Get vtime */
!       mmsg->vtime = ME_TO_DOUBLE(m->v4.olsr_vtime);
  
        //printf("Sequencenuber of MID from %s is %d\n", ip_to_string(&mmsg->addr), mmsg->mid_seqno);
--- 213,217 ----
  
        /* Get vtime */
!       mmsg->vtime = me_to_double(m->v4.olsr_vtime);
  
        //printf("Sequencenuber of MID from %s is %d\n", ip_to_string(&mmsg->addr), mmsg->mid_seqno);
***************
*** 263,267 ****
  
        /* Get vtime */
!       mmsg->vtime = ME_TO_DOUBLE(m->v6.olsr_vtime);
  
        //printf("Sequencenuber of MID from %s is %d\n", ip_to_string(&mmsg->addr), mmsg->mid_seqno);
--- 263,267 ----
  
        /* Get vtime */
!       mmsg->vtime = me_to_double(m->v6.olsr_vtime);
  
        //printf("Sequencenuber of MID from %s is %d\n", ip_to_string(&mmsg->addr), mmsg->mid_seqno);
***************
*** 374,381 ****
  
        /* Get vtime */
!       hmsg->vtime = ME_TO_DOUBLE(m->v4.olsr_vtime);
  
        /* Get htime */
!       hmsg->htime = ME_TO_DOUBLE(m->v4.message.hello.htime);
  
        /* Willingness */
--- 374,381 ----
  
        /* Get vtime */
!       hmsg->vtime = me_to_double(m->v4.olsr_vtime);
  
        /* Get htime */
!       hmsg->htime = me_to_double(m->v4.message.hello.htime);
  
        /* Willingness */
***************
*** 418,425 ****
  
        /* Get vtime */
!       hmsg->vtime = ME_TO_DOUBLE(m->v6.olsr_vtime);
  
        /* Get htime */
!       hmsg->htime = ME_TO_DOUBLE(m->v6.message.hello.htime);
  
        /* Willingness */
--- 418,425 ----
  
        /* Get vtime */
!       hmsg->vtime = me_to_double(m->v6.olsr_vtime);
  
        /* Get htime */
!       hmsg->htime = me_to_double(m->v6.message.hello.htime);
  
        /* Willingness */
***************
*** 497,501 ****
  
        /* Get vtime */
!       tmsg->vtime = ME_TO_DOUBLE(m->v4.olsr_vtime);
  
        OLSR_PRINTF(3, "Got TC vtime: %f\n", tmsg->vtime);
--- 497,501 ----
  
        /* Get vtime */
!       tmsg->vtime = me_to_double(m->v4.olsr_vtime);
  
        OLSR_PRINTF(3, "Got TC vtime: %f\n", tmsg->vtime);
***************
*** 539,543 ****
        
        /* Get vtime */
!       tmsg->vtime = ME_TO_DOUBLE(m->v6.olsr_vtime);
  
        OLSR_PRINTF(3, "Got TC vtime: %f\n", tmsg->vtime);
--- 539,543 ----
        
        /* Get vtime */
!       tmsg->vtime = me_to_double(m->v6.olsr_vtime);
  
        OLSR_PRINTF(3, "Got TC vtime: %f\n", tmsg->vtime);

Index: lq_packet.c
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/src/lq_packet.c,v
retrieving revision 1.25
retrieving revision 1.26
diff -C2 -d -r1.25 -r1.26
*** lq_packet.c	19 Aug 2007 20:37:41 -0000	1.25
--- lq_packet.c	28 Aug 2007 20:45:17 -0000	1.26
***************
*** 70,74 ****
  
    lq_hello->comm.type = LQ_HELLO_MESSAGE;
!   lq_hello->comm.vtime = ME_TO_DOUBLE(outif->valtimes.hello);
    lq_hello->comm.size = 0;
  
--- 70,74 ----
  
    lq_hello->comm.type = LQ_HELLO_MESSAGE;
!   lq_hello->comm.vtime = me_to_double(outif->valtimes.hello);
    lq_hello->comm.size = 0;
  
***************
*** 161,165 ****
  
    lq_tc->comm.type = LQ_TC_MESSAGE;
!   lq_tc->comm.vtime = ME_TO_DOUBLE(outif->valtimes.tc);
    lq_tc->comm.size = 0;
  
--- 161,165 ----
  
    lq_tc->comm.type = LQ_TC_MESSAGE;
!   lq_tc->comm.vtime = me_to_double(outif->valtimes.tc);
    lq_tc->comm.size = 0;
  
***************
*** 580,584 ****
  
        comm->type = olsr_head_v4->type;
!       comm->vtime = ME_TO_DOUBLE(olsr_head_v4->vtime);
        comm->size = ntohs(olsr_head_v4->size);
  
--- 580,584 ----
  
        comm->type = olsr_head_v4->type;
!       comm->vtime = me_to_double(olsr_head_v4->vtime);
        comm->size = ntohs(olsr_head_v4->size);
  
***************
*** 597,601 ****
  
    comm->type = olsr_head_v6->type;
!   comm->vtime = ME_TO_DOUBLE(olsr_head_v6->vtime);
    comm->size = ntohs(olsr_head_v6->size);
  
--- 597,601 ----
  
    comm->type = olsr_head_v6->type;
!   comm->vtime = me_to_double(olsr_head_v6->vtime);
    comm->size = ntohs(olsr_head_v6->size);
  
***************
*** 630,634 ****
    limit = ((unsigned char *)ser) + lq_hello->comm.size;
  
!   lq_hello->htime = ME_TO_DOUBLE(head->htime);
    lq_hello->will = head->will;
  
--- 630,634 ----
    limit = ((unsigned char *)ser) + lq_hello->comm.size;
  
!   lq_hello->htime = me_to_double(head->htime);
    lq_hello->will = head->will;
  

Index: mantissa.c
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/src/mantissa.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** mantissa.c	25 May 2005 13:36:26 -0000	1.8
--- mantissa.c	28 Aug 2007 20:45:17 -0000	1.9
***************
*** 40,44 ****
   */
  
- 
  #include "mantissa.h"
  
--- 40,43 ----
***************
*** 59,95 ****
  
  olsr_u8_t
! double_to_me(double interval)
  {
!   olsr_u8_t a, b;
! 
!   b = 0;
  
!   while(interval / VTIME_SCALE_FACTOR >= (1<<b))
!     b++;
  
!   if(b == 0)
!     {
!       a = 1;
!       b = 0;
!     } 
!   else 
!     {
!       b--;
!       if (b > 15)
! 	{
! 	  a = 15;
! 	  b = 15;
! 	} 
!       else 
! 	{ 
! 	  a = (int)(16*((double)interval/(VTIME_SCALE_FACTOR*(double)(1<<b))-1));
! 	  while(a >= 16)
! 	    {
! 	      a -= 16;
! 	      b++;
! 	    }
! 	}
      }
!   //printf("Generated mantissa/exponent(%d/%d): %d from %f\n", a, b, (olsr_u8_t) (a*16+b), interval);  //printf("Resolves back to: %f\n", ME_TO_DOUBLE(((a<<4)|(b&0x0F))));
!   return (olsr_u8_t) ((a<<4)|(b&0x0F));
  }
--- 58,133 ----
  
  olsr_u8_t
! double_to_me(const double interval)
  {
!     olsr_u8_t a, b;
  
!     /* It is sufficent to compare the integer part since we test on >=.
!      * So we have now only a floating point division and the rest of the loop
!      * are only integer operations.
!      */
!     const unsigned int unscaled_interval = interval / VTIME_SCALE_FACTOR;
!     b = 0;
!     while (unscaled_interval >= (1U << b)) {
!         b++;
!     }
  
!     if(b == 0) {
!         a = 1;
!         b = 0;
!     } else {
!         b--;
!         if (b > 15) {
!             a = 15;
!             b = 15;
!         } else {
!             /* And again some maths simplification from the former version:
!              *    a = 16 * ((double)interval / (VTIME_SCALE_FACTOR * (double)(1 << b)) - 1)
!              * Since interval is already double:
!              *    a = 16 * (interval / (VTIME_SCALE_FACTOR * (double)(1 << b)) - 1)
!              * first, we can get rid of parentheses and change the * to a /
!              *    a = 16 * (interval / VTIME_SCALE_FACTOR / (double)(1 << b) - 1)
!              * then we make an integer addition from the floating point addition
!              *    a = (int)(16.0 * interval / VTIME_SCALE_FACTOR / (double)(1 << b)) - 16
!              * and we loose an unnecessary cast
!              *    a = (int)(16.0 * interval / VTIME_SCALE_FACTOR / (1 << b)) - 16
!              */
!             a = (int)(16.0 * interval / VTIME_SCALE_FACTOR / (1 << b)) - 16;
!             b += a >> 4;
!             a &= 0x0f;
!         }
      }
!     //printf("Generated mantissa/exponent(%d/%d): %d from %f\n", a, b, (olsr_u8_t) (a*16+b), interval);  //printf("Resolves back to: %f\n", me_to_double(((a<<4)|(b&0x0F))));
!     return (a << 4) | (b & 0x0F);
! }
! 
! /**
!  * Function for converting a mantissa/exponent 8bit value back
!  * to double as described in RFC3626:
!  *
!  * value = C*(1+a/16)*2^b [in seconds]
!  *
!  *  where a is the integer represented by the four highest bits of the
!  *  field and b the integer represented by the four lowest bits of the
!  *  field.
!  *
!  * me is the 8 bit mantissa/exponent value
!  *
!  * To avoid expensive floating maths, we transform the equation:
!  *     value = C * (1 + a / 16) * 2^b
!  * first, we make an int addition from the floating point addition:
!  *     value = C * ((16 + a) / 16) * 2^b
!  * then we get rid of a pair of parentheses
!  *     value = C * (16 + a) / 16 * 2^b
!  * and now we make an int multiplication from the floating point one
!  *     value = C * (16 + a) * 2^b / 16
!  * so that we can make a shift from the multiplication
!  *     value = C * ((16 + a) << b) / 16
!  * and sionce C and 16 are constants
!  *     value = ((16 + a) << b) * C / 16
!  */
! double me_to_double(const olsr_u8_t me)
! {
!     const olsr_u8_t a = me >> 4;
!     const olsr_u8_t b = me & 0x0F;
!     return ((16 + a) << b) * VTIME_SCALE_FACTOR / 16.0;
  }

Index: print_packet.c
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/src/print_packet.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** print_packet.c	22 Apr 2007 20:25:48 -0000	1.10
--- print_packet.c	28 Aug 2007 20:45:17 -0000	1.11
***************
*** 110,114 ****
  	  olsr_msgtype_to_string(msg->v4.olsr_msgtype), 
  	  ntohs(msg->v4.olsr_msgsize),
! 	  ME_TO_DOUBLE(msg->v4.olsr_vtime));
    fprintf(handle, "    TTL: %d, Hopcnt: %d, seqno: %d\n",
  	  (olsr_cnf->ip_version == AF_INET) ? msg->v4.ttl : msg->v6.ttl,
--- 110,114 ----
  	  olsr_msgtype_to_string(msg->v4.olsr_msgtype), 
  	  ntohs(msg->v4.olsr_msgsize),
! 	  me_to_double(msg->v4.olsr_vtime));
    fprintf(handle, "    TTL: %d, Hopcnt: %d, seqno: %d\n",
  	  (olsr_cnf->ip_version == AF_INET) ? msg->v4.ttl : msg->v6.ttl,
***************
*** 193,197 ****
    int hellosize = totsize - ((olsr_cnf->ip_version == AF_INET) ? OLSR_MSGHDRSZ_IPV4 : OLSR_MSGHDRSZ_IPV6);
  
!   fprintf(handle, "    +Htime: %0.2f\n", ME_TO_DOUBLE(data[2]));
  
    fprintf(handle, "    +Willingness: %d\n", data[3]);
--- 193,197 ----
    int hellosize = totsize - ((olsr_cnf->ip_version == AF_INET) ? OLSR_MSGHDRSZ_IPV4 : OLSR_MSGHDRSZ_IPV6);
  
!   fprintf(handle, "    +Htime: %0.2f\n", me_to_double(data[2]));
  
    fprintf(handle, "    +Willingness: %d\n", data[3]);
***************
*** 260,264 ****
    int hellosize = totsize - ((olsr_cnf->ip_version == AF_INET) ? OLSR_MSGHDRSZ_IPV4 : OLSR_MSGHDRSZ_IPV6);
  
!   fprintf(handle, "    +Htime: %0.2f\n", ME_TO_DOUBLE(data[2]));
  
    fprintf(handle, "    +Willingness: %d\n", data[3]);
--- 260,264 ----
    int hellosize = totsize - ((olsr_cnf->ip_version == AF_INET) ? OLSR_MSGHDRSZ_IPV4 : OLSR_MSGHDRSZ_IPV6);
  
!   fprintf(handle, "    +Htime: %0.2f\n", me_to_double(data[2]));
  
    fprintf(handle, "    +Willingness: %d\n", data[3]);





More information about the Olsr-cvs mailing list