Home | History | Annotate | Download | only in xla
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/compiler/xla/primitive_util.h"
     17 
     18 #include "tensorflow/core/platform/logging.h"
     19 
     20 namespace xla {
     21 namespace primitive_util {
     22 
     23 bool IsFloatingPointType(PrimitiveType type) {
     24   return type == F16 || type == F32 || type == F64 || type == BF16;
     25 }
     26 
     27 bool IsComplexType(PrimitiveType type) { return type == C64; }
     28 
     29 bool IsSignedIntegralType(PrimitiveType type) {
     30   return type == S8 || type == S16 || type == S32 || type == S64;
     31 }
     32 
     33 bool IsUnsignedIntegralType(PrimitiveType type) {
     34   return type == U8 || type == U16 || type == U32 || type == U64;
     35 }
     36 
     37 bool IsIntegralType(PrimitiveType type) {
     38   return IsUnsignedIntegralType(type) || IsSignedIntegralType(type);
     39 }
     40 
     41 int BitWidth(PrimitiveType type) {
     42   switch (type) {
     43     case PRED:
     44       return 1;
     45 
     46     case S8:
     47     case U8:
     48       return 8;
     49 
     50     case S16:
     51     case U16:
     52     case F16:
     53     case BF16:
     54       return 16;
     55 
     56     case U32:
     57     case S32:
     58     case F32:
     59       return 32;
     60 
     61     case U64:
     62     case S64:
     63     case F64:
     64     case C64:
     65       return 64;
     66 
     67     case TUPLE:
     68       LOG(FATAL) << "TUPLE is an invalid type for BitWidth";
     69 
     70     case OPAQUE:
     71       LOG(FATAL) << "OPAQUE is an invalid type for BitWidth";
     72 
     73     default:
     74       LOG(FATAL) << "Unhandled primitive type " << type;
     75   }
     76 }
     77 
     78 PrimitiveType ComplexComponentType(PrimitiveType complex_type) {
     79   switch (complex_type) {
     80     case C64:
     81       return F32;
     82     default:
     83       LOG(FATAL) << "Primitive type is not complex: "
     84                  << PrimitiveType_Name(complex_type);
     85   }
     86 }
     87 
     88 }  // namespace primitive_util
     89 }  // namespace xla
     90