lib2shift.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Shift functions for the GCC support library for the MSP430
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. typedef int sint32_type __attribute__ ((mode (SI)));
  21. typedef unsigned int uint32_type __attribute__ ((mode (SI)));
  22. typedef int sint16_type __attribute__ ((mode (HI)));
  23. typedef unsigned int uint16_type __attribute__ ((mode (HI)));
  24. uint32_type __ashlsi3 (uint32_type, signed char);
  25. sint32_type __ashrsi3 (sint32_type, signed char);
  26. int __clrsbhi2 (sint16_type);
  27. extern int __clrsbsi2 (sint32_type);
  28. typedef struct
  29. {
  30. union
  31. {
  32. uint32_type u;
  33. uint16_type h[2];
  34. } u;
  35. } dd;
  36. uint32_type
  37. __ashlsi3 (uint32_type in, signed char bit)
  38. {
  39. uint16_type h, l;
  40. dd d;
  41. if (bit > 32)
  42. return 0;
  43. if (bit < 0)
  44. return in;
  45. d.u.u = in;
  46. h = d.u.h[1];
  47. l = d.u.h[0];
  48. if (bit > 15)
  49. {
  50. h = l;
  51. l = 0;
  52. bit -= 16;
  53. }
  54. while (bit)
  55. {
  56. h = (h << 1) | (l >> 15);
  57. l <<= 1;
  58. bit --;
  59. }
  60. d.u.h[1] = h;
  61. d.u.h[0] = l;
  62. return d.u.u;
  63. }
  64. sint32_type
  65. __ashrsi3 (sint32_type in, signed char bit)
  66. {
  67. sint16_type h;
  68. uint16_type l;
  69. dd d;
  70. if (bit > 32)
  71. return 0;
  72. if (bit < 0)
  73. return in;
  74. d.u.u = in;
  75. h = d.u.h[1];
  76. l = d.u.h[0];
  77. while (bit)
  78. {
  79. l = (h << 15) | (l >> 1);
  80. h >>= 1;
  81. bit --;
  82. }
  83. d.u.h[1] = h;
  84. d.u.h[0] = l;
  85. return d.u.u;
  86. }
  87. int
  88. __clrsbhi2 (sint16_type x)
  89. {
  90. if (x == 0)
  91. return 15;
  92. return __clrsbsi2 ((sint32_type) x) - 16;
  93. }