char *floattohexfloat(long double value, char *buffer) { /* Convert an float value in an hexadecimal string stored in the given pointer; ******************************************************************************/ if (buffer == NULL) { /** Given an NULL pointer as argument **/ buffer=calloc(128,sizeof(char)) ; } #ifdef HAVE_MALLOC_USABLE_SIZE if (malloc_usable_size(buffer) < 128) { if ( realloc(buffer,128) == NULL) { errno=EINVAL ; return NULL ; } } #endif long double int_part=0.0 ; long double float_part=0.0 ; _Bool is_negativ = false ; if (value < 0) { value=fabs(value) ; is_negativ=true ; } float_part=modfl(value,&int_part) ; /** modulo float */ /** variables for splitting in integer and float part */ char *int_part_str_hex=malloc(128) ; char *float_part_str_hex=malloc(128) ; memset(int_part_str_hex,'\0',128) ; memset(float_part_str_hex,'\0',128) ; /** Perform splitting in integer and float part */ inttohex((long long) int_part,int_part_str_hex) ; __inttohexfloatpart(float_part,float_part_str_hex,127) ; /** result binar string variable (pointer given as argument) */ memset(buffer,'\0',128) ; if ((is_negativ) && (int_part_str_hex[0] != '-')) { /** Case value to convert in binfloat string is negativ */ strcpy(buffer,"-") ; } /** Assemble final binar string */ strcat(buffer,int_part_str_hex) ; strcat(buffer,".") ; strcat(buffer,float_part_str_hex) ; return buffer ; }
void ALU(int A, int B, char Ctrl) { int Res; AFFECT(cvect(), "A", inttohex(A)); AFFECT(cvect(), "B", inttohex(B)); AFFECT(cvect(), "Ctrl", inttostr(Ctrl)); switch(Ctrl) { case 0b000: Res = A & B; break; case 0b001: Res = A | B; break; case 0b010: Res = A + B; break; case 0b100: Res = A & ~B; break; case 0b101: Res = A | ~B; break; case 0b110: Res = A - B; break; case 0b111: Res = A < B; break; } AFFECT(cvect(), "Res", inttohex(Res)); AFFECT(cvect(), "Zero", inttostr(Res == 0)); AFFECT(cvect(), "Vdd", "1"); curvect++; }
void inttohexfloatpart(long double float_to_hex,char *hex_str,int precision) { /* Set the float part value in an hexadecimal string stored in the given pointer ; *********************************************************************************/ /** temporary final result container variable used for computing */ char *hex_str_saved=malloc(96) ; memset(hex_str_saved,'\0',96) ; int c=0 ; if (float_to_hex < 0) { /** value needed for computing */ float_to_hex=fabs(float_to_hex) ; } while (c < (precision)) { /** loop for computing until precision is reach */ char *to_hex=malloc(32) ; memset(to_hex,'\0',32) ; long double res_int ; long double res_float ; res_float = modfl(float_to_hex * 16.0,&res_int) ; /** modulo float */ inttohex((long long) res_int,to_hex) ; /** getting the hexadecimal string of the integer to add to float part */ strcat(hex_str_saved,to_hex) ; if ( res_float == 0.0 ) { break ; /** reach end of hexadecimal string computing */ } float_to_hex=res_float ; /** updating value */ c++ ; } snprintf(hex_str,(size_t) precision,"%s",hex_str_saved) ; free(hex_str_saved) ; hex_str[++c]='\0' ; }
void floattohexfloat(long double int_to_hex,char *hex_str) { /* Convert an float value in an hexadecimal string stored in the given pointer; ******************************************************************************/ long double int_part=0.0 ; long double float_part=0.0 ; _Bool is_negativ = false ; if (int_to_hex < 0) { int_to_hex=fabs(int_to_hex) ; is_negativ=true ; } float_part=modfl(int_to_hex,&int_part) ; /** modulo float */ /** variables for splitting in integer and float part */ char *int_part_str_hex=malloc(129) ; char *float_part_str_hex=malloc(129) ; memset(int_part_str_hex,'\0',129) ; memset(float_part_str_hex,'\0',129) ; /** Perform splitting in integer and float part */ inttohex((long long) int_part,int_part_str_hex) ; inttohexfloatpart(float_part,float_part_str_hex,128) ; /** result binar string variable (pointer given as argument) */ memset(hex_str,'\0',129) ; if ((is_negativ) && (int_part_str_hex[0] != '-')) { /** Case value to convert in binfloat string is negativ */ strcpy(hex_str,"-") ; } /** Assemble final binar string */ strcat(hex_str,int_part_str_hex) ; strcat(hex_str,".") ; strcat(hex_str,float_part_str_hex) ; }