Carsten\'s encode functions for pseudo-fp

This commit is contained in:
Olaf Bergmann committed 2010-07-11 19:56:58 +02:00
1 parent b325dfbdd4
commit d91feeb790
2 files changed
+25 -27

No files matched your search

+8 -21
View File
@@ -1,6 +1,6 @@
/* encode.c -- encoding and decoding of CoAP data types
*
* (c) 2010 Olaf Bergmann <bergmann@tzi.org>
* (c) 2010 Carsten Bormann
*/
#ifndef NDEBUG
@@ -9,24 +9,11 @@
#include "encode.h"
unsigned int
coap_pseudo_encode( unsigned int val ) {
unsigned int e = 0;
if ( val < HIBIT )
return val;
if ( val > MAX_VALUE ) {
#ifndef NDEBUG
fprintf(stderr, "cannot encode value %u\n", val);
#endif
return ( 1 << N ) - 1;
}
while ( HIBIT < (val & ~0xff) ) {
val = val >> 1;
e += 1;
}
return (val & ~EMASK) | e;
/* Carsten suggested this when fls() is not available: */
int coap_fls(unsigned int i) {
int n;
for (n = 0; i; n++)
i >>= 1;
return n;
}
+17 -6
View File
@@ -1,12 +1,16 @@
/* encode.h -- encoding and decoding of CoAP data types
*
* (c) 2010 Olaf Bergmann <bergmann@tzi.org>
* (c) 2010 Carsten Bormann
*/
#ifndef _COAP_ENCODE_H_
#define _COAP_ENCODE_H_
#include "encode.h"
#if (BSD >= 199103)
# include <string.h>
#else
# include <strings.h>
#endif
#define N 8
#define E 4
@@ -15,10 +19,17 @@
#define MMASK ((1 << N) - 1 - EMASK)
#define MAX_VALUE ( (1 << N) - (1 << E) ) * (1 << ((1 << E) - 1))
/* internal function, better use macro COAP_PSEUDOFP_ENCODE() instead */
unsigned int coap_pseudo_encode( unsigned int val );
#define COAP_PSEUDOFP_DECODE_8_4(r) (r < HIBIT ? r : (r & MMASK) << (r & EMASK))
#define COAP_PSEUDOFP_ENCODE(r) coap_pseudo_encode( (r) )
#define COAP_PSEUDOFP_DECODE(r) (r < HIBIT ? r : (r & MMASK) << (r & EMASK))
#if 0
/* include this only if fls() is not available */
extern int coap_fls(unsigned int i);
#else
#define coap_fls(i) fls(i)
#endif
/* ls and s must be integer variables */
#define COAP_PSEUDOFP_ENCODE_8_4_DOWN(v,ls) (v < HIBIT ? v : (ls = coap_fls(v) - N, (v >> ls) & MMASK) + ls)
#define COAP_PSEUDOFP_ENCODE_8_4_UP(v,ls,s) (v < HIBIT ? v : (ls = coap_fls(v) - N, (s = (((v + ((1<<E<<ls)-1)) >> ls) & MMASK)), s == 0 ? HIBIT + ls + 1 : s + ls))
#endif /* _COAP_ENCODE_H_ */