/ src / exec / div.c
div.c
 1  /*
 2   *  Bean Java VM
 3   *  Copyright (C) 2005-2015 Christian Lins <christian@lins.me>
 4   *
 5   *  Licensed under the Apache License, Version 2.0 (the "License");
 6   *  you may not use this file except in compliance with the License.
 7   *  You may obtain a copy of the License at
 8   *
 9   *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  #include <stdlib.h>
19  
20  #include <debug.h>
21  #include <vm.h>
22  
23  /* Integer division */
24  void do_IDIV(Thread *thread)
25  {
26      dbgmsg("IDIV");
27  
28      int *value1;
29      int *value2;
30      int *result = malloc(sizeof(int));
31      Stackframe *frame = current_frame(thread);
32  
33      Stack_pop(&(frame->operandStack), (void **) &value2);
34      Stack_pop(&(frame->operandStack), (void **) &value1);
35  
36      *result = *value1 / *value2;
37  
38      Stack_push(&(frame->operandStack), result);
39  }
40  
41  /* Long integer division */
42  void do_LDIV(Thread *thread)
43  {
44      dbgmsg("LDIV");
45  
46      int64_t *value1;
47      int64_t *value2;
48      int64_t *result = malloc(sizeof(int64_t));
49      Stackframe *frame = current_frame(thread);
50  
51      Stack_pop(&(frame->operandStack), (void **) &value2);
52      Stack_pop(&(frame->operandStack), (void **) &value1);
53  
54      *result = *value1 / *value2;
55  
56      Stack_push(&(frame->operandStack), result);
57  }
58  
59  /* Float division */
60  void do_FDIV(Thread *thread)
61  {
62      dbgmsg("FDIV");
63  
64      float *value1;
65      float *value2;
66      float *result = malloc(sizeof(float));
67      Stackframe *frame = current_frame(thread);
68  
69      Stack_pop(&(frame->operandStack), (void **) &value2);
70      Stack_pop(&(frame->operandStack), (void **) &value1);
71  
72      *result = *value1 / *value2;
73  
74      Stack_push(&(frame->operandStack), result);
75  }
76  
77  /* Double division */
78  void do_DDIV(Thread *thread)
79  {
80      dbgmsg("DDIV");
81  
82      double *value1;
83      double *value2;
84      double *result = malloc(sizeof(double));
85      Stackframe *frame = current_frame(thread);
86  
87      Stack_pop(&(frame->operandStack), (void **) &value2);
88      Stack_pop(&(frame->operandStack), (void **) &value1);
89  
90      *result = *value1 / *value2;
91  
92      Stack_push(&(frame->operandStack), result);
93  }