Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
3 : *
4 : * Copyright 2016 Mozilla Foundation
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : #include "wasm/WasmTextUtils.h"
20 :
21 : #include "vm/StringBuffer.h"
22 : #include "wasm/WasmTypes.h"
23 :
24 : using namespace js;
25 : using namespace jit;
26 : using namespace wasm;
27 :
28 : using mozilla::IsNaN;
29 :
30 : template<size_t base>
31 : bool
32 0 : wasm::RenderInBase(StringBuffer& sb, uint64_t num)
33 : {
34 0 : uint64_t n = num;
35 0 : uint64_t pow = 1;
36 0 : while (n) {
37 0 : pow *= base;
38 0 : n /= base;
39 : }
40 0 : pow /= base;
41 :
42 0 : n = num;
43 0 : while (pow) {
44 0 : if (!sb.append("0123456789abcdef"[n / pow]))
45 0 : return false;
46 0 : n -= (n / pow) * pow;
47 0 : pow /= base;
48 : }
49 :
50 0 : return true;
51 : }
52 :
53 : template bool wasm::RenderInBase<10>(StringBuffer& sb, uint64_t num);
54 :
55 : template<class T>
56 : bool
57 0 : wasm::RenderNaN(StringBuffer& sb, T num)
58 : {
59 : typedef typename mozilla::SelectTrait<T> Traits;
60 : typedef typename Traits::Bits Bits;
61 :
62 0 : MOZ_ASSERT(IsNaN(num));
63 :
64 0 : Bits bits = mozilla::BitwiseCast<Bits>(num);
65 0 : if ((bits & Traits::kSignBit) && !sb.append("-"))
66 0 : return false;
67 0 : if (!sb.append("nan"))
68 0 : return false;
69 :
70 0 : Bits payload = bits & Traits::kSignificandBits;
71 : // Only render the payload if it's not the spec's default NaN.
72 0 : if (payload == ((Traits::kSignificandBits + 1) >> 1))
73 0 : return true;
74 :
75 0 : return sb.append(":0x") &&
76 0 : RenderInBase<16>(sb, payload);
77 : }
78 :
79 : template MOZ_MUST_USE bool wasm::RenderNaN(StringBuffer& b, float num);
80 9 : template MOZ_MUST_USE bool wasm::RenderNaN(StringBuffer& b, double num);
|