2011-02-06 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgfortran / intrinsics / ishftc.c
blob054c3167b1db86d1e08988c4278356ec4dbbfafb
1 /* Implementation of ishftc intrinsic.
2 Copyright 2002, 2004, 2009 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
28 extern GFC_INTEGER_4 ishftc4 (GFC_INTEGER_4, GFC_INTEGER_4, GFC_INTEGER_4);
29 export_proto(ishftc4);
31 GFC_INTEGER_4
32 ishftc4 (GFC_INTEGER_4 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
34 GFC_UINTEGER_4 mask, bits;
36 if (shift < 0)
37 shift = shift + size;
39 if (shift == 0 || shift == size)
40 return i;
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));
47 bits = i & mask;
49 return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift));
52 extern GFC_INTEGER_8 ishftc8 (GFC_INTEGER_8, GFC_INTEGER_4, GFC_INTEGER_4);
53 export_proto(ishftc8);
55 GFC_INTEGER_8
56 ishftc8 (GFC_INTEGER_8 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
58 GFC_UINTEGER_8 mask, bits;
60 if (shift < 0)
61 shift = shift + size;
63 if (shift == 0 || shift == size)
64 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 == 64) ? (GFC_UINTEGER_8)0 : (~(GFC_UINTEGER_8)0 << size));
71 bits = i & mask;
73 return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift));
76 #ifdef HAVE_GFC_INTEGER_16
77 extern GFC_INTEGER_16 ishftc16 (GFC_INTEGER_16, GFC_INTEGER_4, GFC_INTEGER_4);
78 export_proto(ishftc16);
80 GFC_INTEGER_16
81 ishftc16 (GFC_INTEGER_16 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
83 GFC_UINTEGER_16 mask, bits;
85 if (shift < 0)
86 shift = shift + size;
88 if (shift == 0 || shift == size)
89 return i;
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));
96 bits = i & mask;
98 return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift));
100 #endif