1/* Implementation of ishftc intrinsic. 2 Copyright (C) 2002-2022 Free Software Foundation, Inc. 3 Contributed by Paul Brook <paul@nowt.org> 4 5This file is part of the GNU Fortran 95 runtime library (libgfortran). 6 7Libgfortran is free software; you can redistribute it and/or 8modify it under the terms of the GNU General Public 9License as published by the Free Software Foundation; either 10version 3 of the License, or (at your option) any later version. 11 12Libgfortran is distributed in the hope that it will be useful, 13but WITHOUT ANY WARRANTY; without even the implied warranty of 14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15GNU General Public License for more details. 16 17Under Section 7 of GPL version 3, you are granted additional 18permissions described in the GCC Runtime Library Exception, version 193.1, as published by the Free Software Foundation. 20 21You should have received a copy of the GNU General Public License and 22a copy of the GCC Runtime Library Exception along with this program; 23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24<http://www.gnu.org/licenses/>. */ 25 26#include "libgfortran.h" 27 28extern GFC_INTEGER_4 ishftc4 (GFC_INTEGER_4, GFC_INTEGER_4, GFC_INTEGER_4); 29export_proto(ishftc4); 30 31GFC_INTEGER_4 32ishftc4 (GFC_INTEGER_4 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size) 33{ 34 GFC_UINTEGER_4 mask, bits; 35 36 if (shift < 0) 37 shift = shift + size; 38 39 if (shift == 0 || shift == size) 40 return i; 41 42 /* In C, the result of the shift operator is undefined if the right operand 43 is greater than or equal to the number of bits in the left operand. So we 44 have to special case it for fortran. */ 45 mask = ~((size == 32) ? (GFC_UINTEGER_4)0 : (~(GFC_UINTEGER_4)0 << size)); 46 47 bits = i & mask; 48 49 return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift)); 50} 51 52extern GFC_INTEGER_8 ishftc8 (GFC_INTEGER_8, GFC_INTEGER_4, GFC_INTEGER_4); 53export_proto(ishftc8); 54 55GFC_INTEGER_8 56ishftc8 (GFC_INTEGER_8 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size) 57{ 58 GFC_UINTEGER_8 mask, bits; 59 60 if (shift < 0) 61 shift = shift + size; 62 63 if (shift == 0 || shift == size) 64 return i; 65 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 == 64) ? (GFC_UINTEGER_8)0 : (~(GFC_UINTEGER_8)0 << size)); 70 71 bits = i & mask; 72 73 return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift)); 74} 75 76#ifdef HAVE_GFC_INTEGER_16 77extern GFC_INTEGER_16 ishftc16 (GFC_INTEGER_16, GFC_INTEGER_4, GFC_INTEGER_4); 78export_proto(ishftc16); 79 80GFC_INTEGER_16 81ishftc16 (GFC_INTEGER_16 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size) 82{ 83 GFC_UINTEGER_16 mask, bits; 84 85 if (shift < 0) 86 shift = shift + size; 87 88 if (shift == 0 || shift == size) 89 return i; 90 91 /* In C, the result of the shift operator is undefined if the right operand 92 is greater than or equal to the number of bits in the left operand. So we 93 have to special case it for fortran. */ 94 mask = ~((size == 128) ? (GFC_UINTEGER_16)0 : (~(GFC_UINTEGER_16)0 << size)); 95 96 bits = i & mask; 97 98 return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift)); 99} 100#endif 101