pasholnahuy

maul8

Dec 26th, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include <float.h>
  2. #include <math.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. enum {
  8. FRAC_MASK = (1 << 23) - 1,
  9. FRAC_WIDTH = 23,
  10. EXP_MASK = (255 << 23),
  11. SIGN_MASK = (1 << 31),
  12. MAX_EXP = 255
  13. };
  14.  
  15. union {
  16. float fl;
  17. uint32_t dw;
  18. } f;
  19.  
  20. void f_mul8(float *x) {
  21. if (isnan(*x)) {
  22. return;
  23. }
  24. f.fl = *x;
  25. uint32_t exp = (f.dw & EXP_MASK) >> FRAC_WIDTH;
  26. exp += 3;
  27. if (exp >= MAX_EXP) {
  28. *x = (f.dw | SIGN_MASK) | EXP_MASK;
  29. return;
  30. }
  31. f.dw = (f.dw & SIGN_MASK) | (exp << FRAC_WIDTH) | (f.dw & FRAC_MASK);
  32. *x = f.fl;
  33. }
  34.  
  35. int main() {
  36. float num;
  37. if (scanf("%f", &num) == 1) {
  38. f_mul8(&num);
  39. printf("%f ", num);
  40. }
  41. }
Add Comment
Please, Sign In to add comment