1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. 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 | package org.apache.hadoop.record.compiler; |
---|
20 | |
---|
21 | /** |
---|
22 | */ |
---|
23 | public class JFloat extends JType { |
---|
24 | |
---|
25 | class JavaFloat extends JavaType { |
---|
26 | |
---|
27 | JavaFloat() { |
---|
28 | super("float", "Float", "Float", "TypeID.RIOType.FLOAT"); |
---|
29 | } |
---|
30 | |
---|
31 | String getTypeIDObjectString() { |
---|
32 | return "org.apache.hadoop.record.meta.TypeID.FloatTypeID"; |
---|
33 | } |
---|
34 | |
---|
35 | void genHashCode(CodeBuffer cb, String fname) { |
---|
36 | cb.append(Consts.RIO_PREFIX + "ret = Float.floatToIntBits("+fname+");\n"); |
---|
37 | } |
---|
38 | |
---|
39 | void genSlurpBytes(CodeBuffer cb, String b, String s, String l) { |
---|
40 | cb.append("{\n"); |
---|
41 | cb.append("if ("+l+"<4) {\n"); |
---|
42 | cb.append("throw new java.io.IOException(\"Float is exactly 4 bytes."+ |
---|
43 | " Provided buffer is smaller.\");\n"); |
---|
44 | cb.append("}\n"); |
---|
45 | cb.append(s+"+=4; "+l+"-=4;\n"); |
---|
46 | cb.append("}\n"); |
---|
47 | } |
---|
48 | |
---|
49 | void genCompareBytes(CodeBuffer cb) { |
---|
50 | cb.append("{\n"); |
---|
51 | cb.append("if (l1<4 || l2<4) {\n"); |
---|
52 | cb.append("throw new java.io.IOException(\"Float is exactly 4 bytes."+ |
---|
53 | " Provided buffer is smaller.\");\n"); |
---|
54 | cb.append("}\n"); |
---|
55 | cb.append("float f1 = org.apache.hadoop.record.Utils.readFloat(b1, s1);\n"); |
---|
56 | cb.append("float f2 = org.apache.hadoop.record.Utils.readFloat(b2, s2);\n"); |
---|
57 | cb.append("if (f1 != f2) {\n"); |
---|
58 | cb.append("return ((f1-f2) < 0) ? -1 : 0;\n"); |
---|
59 | cb.append("}\n"); |
---|
60 | cb.append("s1+=4; s2+=4; l1-=4; l2-=4;\n"); |
---|
61 | cb.append("}\n"); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | class CppFloat extends CppType { |
---|
66 | |
---|
67 | CppFloat() { |
---|
68 | super("float"); |
---|
69 | } |
---|
70 | |
---|
71 | String getTypeIDObjectString() { |
---|
72 | return "new ::hadoop::TypeID(::hadoop::RIOTYPE_FLOAT)"; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | /** Creates a new instance of JFloat */ |
---|
77 | public JFloat() { |
---|
78 | setJavaType(new JavaFloat()); |
---|
79 | setCppType(new CppFloat()); |
---|
80 | setCType(new CType()); |
---|
81 | } |
---|
82 | |
---|
83 | String getSignature() { |
---|
84 | return "f"; |
---|
85 | } |
---|
86 | } |
---|