ishftc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Implementation of ishftc intrinsic.
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. Contributed by Paul Brook <paul@nowt.org>
  4. This file is part of the GNU Fortran 95 runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran 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. #include "libgfortran.h"
  21. extern GFC_INTEGER_4 ishftc4 (GFC_INTEGER_4, GFC_INTEGER_4, GFC_INTEGER_4);
  22. export_proto(ishftc4);
  23. GFC_INTEGER_4
  24. ishftc4 (GFC_INTEGER_4 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
  25. {
  26. GFC_UINTEGER_4 mask, bits;
  27. if (shift < 0)
  28. shift = shift + size;
  29. if (shift == 0 || shift == size)
  30. return i;
  31. /* In C, the result of the shift operator is undefined if the right operand
  32. is greater than or equal to the number of bits in the left operand. So we
  33. have to special case it for fortran. */
  34. mask = ~((size == 32) ? (GFC_UINTEGER_4)0 : (~(GFC_UINTEGER_4)0 << size));
  35. bits = i & mask;
  36. return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift));
  37. }
  38. extern GFC_INTEGER_8 ishftc8 (GFC_INTEGER_8, GFC_INTEGER_4, GFC_INTEGER_4);
  39. export_proto(ishftc8);
  40. GFC_INTEGER_8
  41. ishftc8 (GFC_INTEGER_8 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
  42. {
  43. GFC_UINTEGER_8 mask, bits;
  44. if (shift < 0)
  45. shift = shift + size;
  46. if (shift == 0 || shift == size)
  47. return i;
  48. /* In C, the result of the shift operator is undefined if the right operand
  49. is greater than or equal to the number of bits in the left operand. So we
  50. have to special case it for fortran. */
  51. mask = ~((size == 64) ? (GFC_UINTEGER_8)0 : (~(GFC_UINTEGER_8)0 << size));
  52. bits = i & mask;
  53. return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift));
  54. }
  55. #ifdef HAVE_GFC_INTEGER_16
  56. extern GFC_INTEGER_16 ishftc16 (GFC_INTEGER_16, GFC_INTEGER_4, GFC_INTEGER_4);
  57. export_proto(ishftc16);
  58. GFC_INTEGER_16
  59. ishftc16 (GFC_INTEGER_16 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
  60. {
  61. GFC_UINTEGER_16 mask, bits;
  62. if (shift < 0)
  63. shift = shift + size;
  64. if (shift == 0 || shift == size)
  65. return i;
  66. /* In C, the result of the shift operator is undefined if the right operand
  67. is greater than or equal to the number of bits in the left operand. So we
  68. have to special case it for fortran. */
  69. mask = ~((size == 128) ? (GFC_UINTEGER_16)0 : (~(GFC_UINTEGER_16)0 << size));
  70. bits = i & mask;
  71. return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift));
  72. }
  73. #endif