OSDN Git Service

PR debug/29609
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / ishftc.c
1 /* Implementation of ishftc intrinsic.
2    Copyright 2002, 2004 Free Software Foundation, Inc.
3    Contributed by Paul Brook <paul@nowt.org>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
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 2 of the License, or (at your option) any later version.
11
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file.  (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
20
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING.  If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA.  */
30
31 #include "libgfortran.h"
32
33 extern GFC_INTEGER_4 ishftc4 (GFC_INTEGER_4, GFC_INTEGER_4, GFC_INTEGER_4);
34 export_proto(ishftc4);
35
36 GFC_INTEGER_4
37 ishftc4 (GFC_INTEGER_4 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
38 {
39   GFC_UINTEGER_4 mask, bits;
40
41   if (shift < 0)
42     shift = shift + size;
43
44   if (shift == 0 || shift == size)
45     return i;
46
47   /* In C, the result of the shift operator is undefined if the right operand
48      is greater than or equal to the number of bits in the left operand. So we
49      have to special case it for fortran.  */
50   mask = ~((size == 32) ? (GFC_UINTEGER_4)0 : (~(GFC_UINTEGER_4)0 << size));
51
52   bits = i & mask;
53   
54   return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift));
55 }
56
57 extern GFC_INTEGER_8 ishftc8 (GFC_INTEGER_8, GFC_INTEGER_4, GFC_INTEGER_4);
58 export_proto(ishftc8);
59
60 GFC_INTEGER_8
61 ishftc8 (GFC_INTEGER_8 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
62 {
63   GFC_UINTEGER_8 mask, bits;
64
65   if (shift < 0)
66     shift = shift + size;
67
68   if (shift == 0 || shift == size)
69     return i;
70
71   /* In C, the result of the shift operator is undefined if the right operand
72      is greater than or equal to the number of bits in the left operand. So we
73      have to special case it for fortran.  */
74   mask = ~((size == 64) ? (GFC_UINTEGER_8)0 : (~(GFC_UINTEGER_8)0 << size));
75
76   bits = i & mask;
77   
78   return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift));
79 }
80
81 #ifdef HAVE_GFC_INTEGER_16
82 extern GFC_INTEGER_16 ishftc16 (GFC_INTEGER_16, GFC_INTEGER_4, GFC_INTEGER_4);
83 export_proto(ishftc16);
84
85 GFC_INTEGER_16
86 ishftc16 (GFC_INTEGER_16 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
87 {
88   GFC_UINTEGER_16 mask, bits;
89
90   if (shift < 0)
91     shift = shift + size;
92
93   if (shift == 0 || shift == size)
94     return i;
95
96   /* In C, the result of the shift operator is undefined if the right operand
97      is greater than or equal to the number of bits in the left operand. So we
98      have to special case it for fortran.  */
99   mask = ~((size == 128) ? (GFC_UINTEGER_16)0 : (~(GFC_UINTEGER_16)0 << size));
100
101   bits = i & mask;
102   
103   return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift));
104 }
105 #endif