SURFEX v7.3
General documentation of Surfex
 All Classes Files Functions Variables Typedefs
/home/dasprezs/EXPORT_v7_3/src/LIB/LFI_COMPRESS/srcc/nearestpow2.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "ieee754.h"
00003 #include <math.h>
00004 
00005 #ifdef NO_UNDERSCORE
00006 # define NEAREST_POW2 nearest_pow2
00007 # define MINBITS_IN_WORD minbits_in_word
00008 # define FMINBITS_IN_WORD fminbits_in_word
00009 #else
00010 # define NEAREST_POW2 nearest_pow2_
00011 # define MINBITS_IN_WORD minbits_in_word_
00012 # define FMINBITS_IN_WORD fminbits_in_word_
00013 #endif
00014 
00015 void NEAREST_POW2(union ieee754_double *xval, unsigned int *pow)
00016 {
00017 
00018   if (xval->d != 0.0)
00019     *pow = xval->ieee.exponent - IEEE754_DOUBLE_BIAS;
00020   else {
00021       printf("Warning : NEAREST_POW2 ne traite que des reels > 0.0\n");
00022       *pow = 0;
00023   }
00024 
00025 }
00026 
00027 void MINBITS_IN_WORD(int *nval, unsigned int *nbit)
00028 {
00029   union ieee754_double xval;
00030   int ival = *nval;
00031 
00032   /* ne fonctionne qu'avec des entiers non signés */
00033   if (ival-- < 0){
00034     printf("Warning : MINBITS_IN_WORD ne traite que des entiers POSITIFS.\n");
00035     *nbit = -1;
00036     return;
00037   } else
00038     if (ival > 0){
00039       xval.d = (double)ival;
00040       NEAREST_POW2(&xval,nbit);
00041       (*nbit)++;
00042     } else 
00043       *nbit = 0 ;
00044     
00045 }
00046 
00047 int FMINBITS_IN_WORD(int *nval)
00048 {
00049   union ieee754_double xval;
00050   int ival = *nval;
00051   unsigned int nbit;
00052 
00053   /* ne fonctionne qu'avec des entiers non signés */
00054   if (ival < 0){
00055     printf("Warning : MINBITS_IN_WORD ne traite que des entiers POSITIFS.\n");
00056     return -1;
00057   } else {
00058     if (ival > 0){
00059       xval.d = (double)ival;
00060       NEAREST_POW2(&xval,&nbit);
00061       nbit++;
00062     } else 
00063       nbit = 0 ;
00064     return nbit;
00065   }
00066 }
00067 
00068 /* int main(){ */
00069 
00070 /*   double x; */
00071 /*   int i,nbit; */
00072 /*   int exp2; */
00073 
00074 /*   printf("Reel : "); */
00075 /*   scanf("%lf",&x); */
00076   
00077 /*   nearest_pow2_((union ieee754_double*)&x,&exp2); */
00078 
00079 /*   printf("2**%d = %lf est la puissance de 2 la plus proche et inferieure à %lf\n", */
00080 /*       exp2,pow(2.,exp2),x); */
00081 /*   printf("%lf <= %lf <= %lf\n",pow(2.,(double)exp2),x,pow(2.,(double)exp2+1.)); */
00082   
00083 /*   printf("Entier positif : "); */
00084 /*   scanf("%d",&i); */
00085 /*   minbits_in_word_(&i,&nbit); */
00086 /*   printf("%d valeurs : %d bits (2**%d = %d).\n",i,nbit,nbit,(1<<nbit)); */
00087 /* } */