summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/gallivm/lp_bld_arit_overflow.c
blob: 91247fdbc954644fb34c5f92edd55a28d34f375f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**************************************************************************
 *
 * Copyright 2013
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/


/**
 * @file
 * Helper
 *
 * The functions in this file implement arthmetic operations with support
 * for overflow detection and reporting.
 *
 */

#include "lp_bld_arit_overflow.h"

#include "lp_bld_type.h"
#include "lp_bld_const.h"
#include "lp_bld_init.h"
#include "lp_bld_intr.h"
#include "lp_bld_logic.h"
#include "lp_bld_pack.h"
#include "lp_bld_debug.h"
#include "lp_bld_bitarit.h"

#include "util/u_memory.h"
#include "util/u_debug.h"
#include "util/u_math.h"
#include "util/u_string.h"
#include "util/u_cpu_detect.h"

#include <float.h>


static LLVMValueRef
build_binary_int_overflow(struct gallivm_state *gallivm,
                          const char *intr_prefix,
                          LLVMValueRef a,
                          LLVMValueRef b,
                          LLVMValueRef *ofbit)
{
   LLVMBuilderRef builder = gallivm->builder;
   char intr_str[256];
   LLVMTypeRef type_ref;
   LLVMTypeKind type_kind;
   unsigned type_width;
   LLVMTypeRef oelems[2];
   LLVMValueRef oresult;
   LLVMTypeRef otype;

   debug_assert(LLVMTypeOf(a) == LLVMTypeOf(b));
   type_ref = LLVMTypeOf(a);
   type_kind = LLVMGetTypeKind(type_ref);

   debug_assert(type_kind == LLVMIntegerTypeKind);
   type_width = LLVMGetIntTypeWidth(type_ref);

   debug_assert(type_width == 16 || type_width == 32 || type_width == 64);

   util_snprintf(intr_str, sizeof intr_str, "%s.i%u",
                 intr_prefix, type_width);

   oelems[0] = type_ref;
   oelems[1] = LLVMInt1TypeInContext(gallivm->context);

   otype = LLVMStructTypeInContext(gallivm->context, oelems, 2, FALSE);
   oresult = lp_build_intrinsic_binary(builder, intr_str,
                                       otype, a, b);
   if (ofbit) {
      if (*ofbit) {
         *ofbit = LLVMBuildOr(
            builder, *ofbit,
            LLVMBuildExtractValue(builder, oresult, 1, ""), "");
      } else {
         *ofbit = LLVMBuildExtractValue(builder, oresult, 1, "");
      }
   }

   return LLVMBuildExtractValue(builder, oresult, 0, "");
}

/**
 * Performs unsigned addition of two integers and reports 
 * overflow if detected.
 *
 * The values @a and @b must be of the same integer type. If
 * an overflow is detected the IN/OUT @ofbit parameter is used:
 * - if it's pointing to a null value, the overflow bit is simply
 *   stored inside the variable it's pointing to,
 * - if it's pointing to a valid value, then that variable,
 *   which must be of i1 type, is ORed with the newly detected
 *   overflow bit. This is done to allow chaining of a number of
 *   overflow functions together without having to test the 
 *   overflow bit after every single one.
 */
LLVMValueRef
lp_build_uadd_overflow(struct gallivm_state *gallivm,
                       LLVMValueRef a,
                       LLVMValueRef b,
                       LLVMValueRef *ofbit)
{
   return build_binary_int_overflow(gallivm, "llvm.uadd.with.overflow",
                                    a, b, ofbit);
}

/**
 * Performs unsigned multiplication of  two integers and 
 * reports overflow if detected.
 *
 * The values @a and @b must be of the same integer type. If
 * an overflow is detected the IN/OUT @ofbit parameter is used:
 * - if it's pointing to a null value, the overflow bit is simply
 *   stored inside the variable it's pointing to,
 * - if it's pointing to a valid value, then that variable,
 *   which must be of i1 type, is ORed with the newly detected
 *   overflow bit. This is done to allow chaining of a number of
 *   overflow functions together without having to test the 
 *   overflow bit after every single one.
 */
LLVMValueRef
lp_build_umul_overflow(struct gallivm_state *gallivm,
                       LLVMValueRef a,
                       LLVMValueRef b,
                       LLVMValueRef *ofbit)
{
   return build_binary_int_overflow(gallivm, "llvm.umul.with.overflow",
                                    a, b, ofbit);
}