Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / native_client / tests / toolchain / intrinsics.cc
1 /*
2  * Copyright 2011 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 #include <stdio.h>
8
9 // use volatile to confuse llmv optimizer
10 volatile long long s64a = 0x12345678;
11 volatile long long s64b = 0x1;
12
13 volatile unsigned long long u64a = 0x12345678;
14 volatile unsigned long long u64b = 0x1;
15
16 volatile int s32a = 0x1234;
17 volatile int s32b = 0x1;
18
19 volatile unsigned int u32a = 0x1234;
20 volatile unsigned int u32b = 0x1;
21
22 volatile int failures = 0;
23
24 template<typename Integer> void Check(const char* mode, Integer a, Integer b) {
25   for (int i = 0; i < 16; ++i) {
26     Integer c = a * b;
27     // this relies on b == 1
28     if (c >> i != a) {
29       ++failures;
30       printf("failure in mult %s %d\n", mode, i);
31     }
32
33     c += failures; /* hopefully a nop */
34     Integer div = c / (b + 1);
35     b += failures; /* hopefully a nop */
36     Integer rem = c % (b + 1);
37     b += failures; /* hopefully a nop */
38     if (div * (b + 1) + rem != c) {
39       ++failures;
40       printf("failure in div/mod %s %d\n", mode, i);
41     }
42
43     b <<= 1;
44   }
45 }
46
47 int main() {
48   Check<int>("s32", s32a, s32b);
49   Check<unsigned int>("u32", u32a, u32b);
50   Check<long long>("s64", s64a, s64b);
51   Check<unsigned long long>("u64", u64a, u64b);
52   return 55 + failures;
53 }