Home | History | Annotate | Line # | Download | only in intrinsics
ishftc.c revision 1.1
      1 /* Implementation of ishftc intrinsic.
      2    Copyright (C) 2002-2019 Free Software Foundation, Inc.
      3    Contributed by Paul Brook <paul (at) 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 3 of the License, or (at your option) any later version.
     11 
     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.
     16 
     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.
     20 
     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/>.  */
     25 
     26 #include "libgfortran.h"
     27 
     28 extern GFC_INTEGER_4 ishftc4 (GFC_INTEGER_4, GFC_INTEGER_4, GFC_INTEGER_4);
     29 export_proto(ishftc4);
     30 
     31 GFC_INTEGER_4
     32 ishftc4 (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 
     52 extern GFC_INTEGER_8 ishftc8 (GFC_INTEGER_8, GFC_INTEGER_4, GFC_INTEGER_4);
     53 export_proto(ishftc8);
     54 
     55 GFC_INTEGER_8
     56 ishftc8 (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
     77 extern GFC_INTEGER_16 ishftc16 (GFC_INTEGER_16, GFC_INTEGER_4, GFC_INTEGER_4);
     78 export_proto(ishftc16);
     79 
     80 GFC_INTEGER_16
     81 ishftc16 (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