sp_fint.c 938 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* IEEE754 floating point arithmetic
  3. * single precision
  4. */
  5. /*
  6. * MIPS floating point support
  7. * Copyright (C) 1994-2000 Algorithmics Ltd.
  8. */
  9. #include "ieee754sp.h"
  10. union ieee754sp ieee754sp_fint(int x)
  11. {
  12. unsigned int xm;
  13. int xe;
  14. int xs;
  15. ieee754_clearcx();
  16. if (x == 0)
  17. return ieee754sp_zero(0);
  18. if (x == 1 || x == -1)
  19. return ieee754sp_one(x < 0);
  20. if (x == 10 || x == -10)
  21. return ieee754sp_ten(x < 0);
  22. xs = (x < 0);
  23. if (xs) {
  24. if (x == (1 << 31))
  25. xm = ((unsigned) 1 << 31); /* max neg can't be safely negated */
  26. else
  27. xm = -x;
  28. } else {
  29. xm = x;
  30. }
  31. xe = SP_FBITS + 3;
  32. if (xm >> (SP_FBITS + 1 + 3)) {
  33. /* shunt out overflow bits
  34. */
  35. while (xm >> (SP_FBITS + 1 + 3)) {
  36. SPXSRSX1();
  37. }
  38. } else {
  39. /* normalize in grs extended single precision
  40. */
  41. while ((xm >> (SP_FBITS + 3)) == 0) {
  42. xm <<= 1;
  43. xe--;
  44. }
  45. }
  46. return ieee754sp_format(xs, xe, xm);
  47. }